]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/forms/fdfix.sh
Fix bug 944:
[lyx.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 test $# -eq 1 || {
19         echo "Expected a file name!"
20         exit 1
21 }
22
23 # Note that we can't create a variable containing this and then
24 # echo it across because some machines require -e to recognize \n et al.
25 # Other machines, of course output -e, it not being an option they
26 # recognise ;-)
27
28 cat - > $1 <<EOF
29 // File generated by fdesign from ${FDFILE}
30 // and modified by fdfix.sh for use by LyX.
31
32 // WARNING! All changes made to this file will be lost!
33
34 // This file is part of LyX, the document processor.
35 // Licence details can be found in the file COPYING.
36
37 EOF
38 }
39
40 #===============
41 # Initial checks
42 if [ ! -f $1 ]; then
43         echo "Input file does not exist. Cannot continue"
44         exit 1
45 fi
46
47 DIRNAME=`dirname $1`
48 BASENAME=`basename $1 .fd`
49
50 if [ $1 = ${BASENAME} ]; then
51         echo "Input file is not a .fd file. Cannot continue"
52         exit 1
53 fi
54
55 #===================================
56 # Create the initial .c and .h files
57 FDESIGN=fdesign
58 FDFILE=${BASENAME}.fd
59
60 (cd ${DIRNAME} && ${FDESIGN} -convert ${FDFILE}) ||
61 {
62         echo "\"${FDESIGN} -convert ${FDFILE}}\" failed. Please investigate."
63         exit 1
64 }
65
66 #==================================
67 # Modify the .h file for use by LyX
68 HIN=${DIRNAME}/${BASENAME}.h
69 HPATCH=${DIRNAME}/${BASENAME}.h.patch
70 HOUT=${BASENAME}.hpp
71
72 # First clean up the "extern void func(arg);" declarations and
73 # put the sorted, unique list in file ${EXTERN_FUNCS}
74 # The contents of this file are used by ${FDFIXH} to replace the mess
75 # output by fdesign
76 # Note that we use unique file names for temp files to enable re-entrant
77 # builds with SMP machines
78 EXTERN_FUNCS=extern.$$
79 sed -n 's/extern void \(.*\)/void \1/p' ${HIN} > ${EXTERN_FUNCS}
80
81 if [ -s ${EXTERN_FUNCS} ]; then
82         TMP=tmp.$$
83         sort -u ${EXTERN_FUNCS} > ${TMP}
84         echo "extern \"C\" {" > ${EXTERN_FUNCS}
85         cat ${TMP} >> ${EXTERN_FUNCS}
86         echo "}" >> ${EXTERN_FUNCS}
87         rm -f ${TMP}
88 fi
89
90 # First ensure that the sed script knows where to find ${EXTERN_FUNCS}
91 FDFIXH=fdfixh.$$
92 sed "s/EXTERN_FUNCS/${EXTERN_FUNCS}/" ${DIRNAME}/fdfixh.sed > ${FDFIXH}
93
94 INTRO_MESSAGE ${HOUT}
95
96 sed -f ${FDFIXH} < ${HIN} >> ${HOUT}
97
98 # Don't forget to clean up the temporary files.
99 rm -f ${EXTERN_FUNCS} ${FDFIXH}
100
101 # Patch the .h file if a patch exists
102 if [ -f "${HPATCH}" ] ; then
103         echo "Patching ${HOUT} with ${HPATCH}"
104         patch -s ${HOUT} < ${HPATCH}
105 fi
106
107 # Clean up, to leave the finished .h file. We can be a little tricky here
108 # testing to see if the finished file exists already and if it does
109 # testing whether there are any differences.
110 # If there are no differences, then don't overwrite to prevent unnecessary
111 # compilation in the xforms directory.
112 rm -f ${HIN}
113 MOVE_H_FILE=1
114 if [ -r ${BASENAME}.h ]; then
115         cmp -s ${HOUT} ${BASENAME}.h && MOVE_H_FILE=0
116 fi
117
118 if [ ${MOVE_H_FILE} -eq 1 ]; then
119         mv ${HOUT} ${BASENAME}.h
120 else
121         rm -f ${HOUT}
122 fi
123
124 #==================================
125 # Create the .C file for use by LyX
126 CIN=${DIRNAME}/${BASENAME}.c
127 CPATCH=${DIRNAME}/${BASENAME}.C.patch
128 COUT=${BASENAME}.cpp
129 FINAL_COUT=${BASENAME}.C
130
131 # We use a two pass algorithm to generate elegant C++ code whilst
132 # keeping the sed clean also.
133
134 # Pass 1. The bulk of the clean-up
135 FDFIXC=${DIRNAME}/fdfixc.sed
136
137 TMP=tmp.$$
138 INTRO_MESSAGE ${TMP}
139
140 echo "#include <config.h>" >> ${TMP}
141 echo "#include \"forms_gettext.h\"" >> ${TMP}
142 echo "#include \"gettext.h\"" >> ${TMP}
143
144 grep bmtable ${CIN} > /dev/null &&
145         echo "#include \"bmtable.h\"" >> ${TMP}
146
147 sed -f ${FDFIXC} < ${CIN} >> ${TMP}
148
149 # Pass 2. Ensure that any c_str variables inserted by fdfixc.sed
150 # are declared at the top of the appropriate function.
151 FDFIXC=${DIRNAME}/c_str.sed
152 sed -f ${FDFIXC} < ${TMP} > ${COUT}
153 rm -f ${TMP}
154
155 if [ -f "${CPATCH}" ] ; then
156         echo "Patching ${COUT} with ${CPATCH}"
157         patch -s ${COUT} < ${CPATCH}
158 fi
159
160 # Clean up, to leave the finished .C file
161 rm -f ${CIN}
162 mv ${COUT} ${FINAL_COUT}
163
164 #========================================