]> git.lyx.org Git - features.git/blob - src/frontends/xforms/forms/fdfix.sh
Finish the business of strengthening the sed scripts and beautifying
[features.git] / src / frontends / xforms / forms / fdfix.sh
1 #!/bin/sh
2 #
3 # file fdfix.sh
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6 #
7 # author Angus Leeming
8 #
9 # Full author contact details are available in file CREDITS
10 #
11 # This shell script takes the dialog created with fdesign and generates the
12 # .C and .h files used by LyX from it.
13 # Note that the REAL magic is to be found in the sed scripts fdfixc.sed and
14 # fdfixh.sed used by this shell script.
15
16 INTRO_MESSAGE ()
17 {
18 # Note that we can't create a variable containing this and then
19 # echo it across because some machines require -e to recognize \n et al.
20 # Other machines, of course output -e, it not being an option they
21 # recognise ;-)
22
23 # Set ${OUTPUT_FILE} to ${HOUT} or ${COUT} as appropriate
24 cat - > ${OUTPUT_FILE} <<EOF
25 // File generated by fdesign from ${FDFILE}
26 // and modified by fdfix.sh for use by LyX.
27
28 // WARNING! All changes made to this file will be lost!
29
30 // This file is part of LyX, the document processor.
31 // Licence details can be found in the file COPYING.
32
33 EOF
34 }
35
36 #===============
37 # Initial checks
38 if [ ! -f $1 ]; then
39         echo "Input file does not exist. Cannot continue"
40         exit 1
41 fi
42
43 DIRNAME=`dirname $1`
44 BASENAME=`basename $1 .fd`
45
46 if [ $1 = ${BASENAME} ]; then
47         echo "Input file is not a .fd file. Cannot continue"
48         exit 1
49 fi
50
51 #===================================
52 # Create the initial .c and .h files
53 FDESIGN=fdesign
54 FDFILE=${BASENAME}.fd
55
56 (cd ${DIRNAME} && ${FDESIGN} -convert ${FDFILE}) ||
57 {
58         echo "\"${FDESIGN} -convert ${FDFILE}}\" failed. Please investigate."
59         exit 1
60 }
61
62 #==================================
63 # Modify the .h file for use by LyX
64 HIN=${DIRNAME}/${BASENAME}.h
65 HPATCH=${DIRNAME}/${BASENAME}.h.patch
66 HOUT=${BASENAME}.hpp
67
68 # First clean up the "extern void func(arg);" declarations and
69 # put the sorted, unique list in file ${EXTERN_FUNCS}
70 # The contents of this file are used by ${FDFIXH} to replace the mess
71 # output by fdesign
72 EXTERN_FUNCS=extern.tmp
73 sed -n 's/extern void \(.*\)/void \1/p' ${HIN} > ${EXTERN_FUNCS}
74
75 if [ -s ${EXTERN_FUNCS} ]; then
76         sort -u ${EXTERN_FUNCS} > tmp
77         echo "extern \"C\" {" > ${EXTERN_FUNCS}
78         cat tmp >> ${EXTERN_FUNCS}
79         echo "}" >> ${EXTERN_FUNCS}
80         rm -f tmp
81 fi
82
83 FDFIXH=${DIRNAME}/fdfixh.sed
84
85 OUTPUT_FILE=${HOUT}; INTRO_MESSAGE
86
87 sed -f ${FDFIXH} < ${HIN} >> ${HOUT}
88 rm -f ${EXTERN_FUNCS}
89
90 # Patch the .h file if a patch exists
91 if [ -f "${HPATCH}" ] ; then
92         echo "Patching ${HOUT} with ${HPATCH}"
93         patch -s ${HOUT} < ${HPATCH}
94 fi
95
96 # Clean up, to leave the finished .h file. We can be a little tricky here
97 # testing to see if the finished file exists already and if it does
98 # testing whether there are any differences.
99 # If there are no differences, then don't overwrite to prevent unnecessary
100 # compilation in the xforms directory.
101 rm -f ${HIN}
102 MOVE_H_FILE=1
103 if [ -r ${BASENAME}.h ]; then
104         cmp -s ${HOUT} ${BASENAME}.h && MOVE_H_FILE=0
105 fi
106
107 if [ ${MOVE_H_FILE} -eq 1 ]; then
108         mv ${HOUT} ${BASENAME}.h
109 else
110         rm -f ${HOUT}
111 fi
112
113 #==================================
114 # Create the .C file for use by LyX
115 CIN=${DIRNAME}/${BASENAME}.c
116 CPATCH=${DIRNAME}/${BASENAME}.C.patch
117 COUT=${BASENAME}.cpp
118 FINAL_COUT=${BASENAME}.C
119
120 FDFIXC=${DIRNAME}/fdfixc.sed
121
122 OUTPUT_FILE=${COUT}; INTRO_MESSAGE
123
124 # This "c_str" is potentially used many times in many functions
125 # so add it to the top of the generated file.
126 grep -E 'fl_add.*".*[|].*"' ${CIN} > /dev/null &&
127         cat - >> ${COUT} <<EOF
128 namespace {
129 char const * c_str;
130 } // namespace anon
131
132
133 EOF
134
135 echo "#include <config.h>" >> ${COUT}
136 echo "#include \"forms_gettext.h\"" >> ${COUT}
137 echo "#include \"gettext.h\"" >> ${COUT}
138
139 grep bmtable ${CIN} > /dev/null &&
140         echo "#include \"bmtable.h\"" >> ${COUT}
141
142 sed -f ${FDFIXC} < ${CIN} >> ${COUT}
143
144 # Patch the .C file if a patch exists
145 if [ -f "${CPATCH}" ] ; then
146         echo "Patching ${COUT} with ${CPATCH}"
147         patch -s ${COUT} < ${CPATCH}
148 fi
149
150 # Clean up, to leave the finished .C file
151 rm -f ${CIN}
152 mv ${COUT} ${FINAL_COUT}
153
154 #========================================