]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/MKlib_gen.sh
ncurses 5.7 - patch 20100605
[ncurses.git] / ncurses / base / MKlib_gen.sh
1 #!/bin/sh
2 #
3 # MKlib_gen.sh -- generate sources from curses.h macro definitions
4 #
5 # ($Id: MKlib_gen.sh,v 1.40 2010/03/30 22:42:16 tom Exp $)
6 #
7 ##############################################################################
8 # Copyright (c) 1998-2009,2010 Free Software Foundation, Inc.                #
9 #                                                                            #
10 # Permission is hereby granted, free of charge, to any person obtaining a    #
11 # copy of this software and associated documentation files (the "Software"), #
12 # to deal in the Software without restriction, including without limitation  #
13 # the rights to use, copy, modify, merge, publish, distribute, distribute    #
14 # with modifications, sublicense, and/or sell copies of the Software, and to #
15 # permit persons to whom the Software is furnished to do so, subject to the  #
16 # following conditions:                                                      #
17 #                                                                            #
18 # The above copyright notice and this permission notice shall be included in #
19 # all copies or substantial portions of the Software.                        #
20 #                                                                            #
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
23 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
24 # THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
25 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
26 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
27 # DEALINGS IN THE SOFTWARE.                                                  #
28 #                                                                            #
29 # Except as contained in this notice, the name(s) of the above copyright     #
30 # holders shall not be used in advertising or otherwise to promote the sale, #
31 # use or other dealings in this Software without prior written               #
32 # authorization.                                                             #
33 ##############################################################################
34 #
35 # The XSI Curses standard requires all curses entry points to exist as
36 # functions, even though many definitions would normally be shadowed
37 # by macros.  Rather than hand-hack all that code, we actually
38 # generate functions from the macros.
39 #
40 # This script accepts a file of prototypes on standard input.  It discards
41 # any that don't have a `generated' comment attached. It then parses each
42 # prototype (relying on the fact that none of the macros take function
43 # pointer or array arguments) and generates C source from it.
44 #
45 # Here is what the pipeline stages are doing:
46 #
47 # 1. sed: extract prototypes of generated functions
48 # 2. sed: decorate prototypes with generated arguments a1. a2,...z
49 # 3. awk: generate the calls with args matching the formals
50 # 4. sed: prefix function names in prototypes so the preprocessor won't expand
51 #         them.
52 # 5. cpp: macro-expand the file so the macro calls turn into C calls
53 # 6. awk: strip the expansion junk off the front and add the new header
54 # 7. sed: squeeze spaces, strip off gen_ prefix.
55 #
56
57 # keep the editing independent of locale:
58 if test "${LANGUAGE+set}"    = set; then LANGUAGE=C;    export LANGUAGE;    fi
59 if test "${LANG+set}"        = set; then LANG=C;        export LANG;        fi
60 if test "${LC_ALL+set}"      = set; then LC_ALL=C;      export LC_ALL;      fi
61 if test "${LC_MESSAGES+set}" = set; then LC_MESSAGES=C; export LC_MESSAGES; fi
62 if test "${LC_CTYPE+set}"    = set; then LC_CTYPE=C;    export LC_CTYPE;    fi
63 if test "${LC_COLLATE+set}"  = set; then LC_COLLATE=C;  export LC_COLLATE;  fi
64
65 preprocessor="$1 -DNCURSES_INTERNALS -I../include"
66 AWK="$2"
67 USE="$3"
68
69 PID=$$
70 ED1=sed1_${PID}.sed
71 ED2=sed2_${PID}.sed
72 ED3=sed3_${PID}.sed
73 ED4=sed4_${PID}.sed
74 AW1=awk1_${PID}.awk
75 AW2=awk2_${PID}.awk
76 TMP=gen__${PID}.c
77 trap "rm -f $ED1 $ED2 $ED3 $ED4 $AW1 $AW2 $TMP" 0 1 2 5 15
78
79 ALL=$USE
80 if test "$USE" = implemented ; then
81         CALL="call_"
82         cat >$ED1 <<EOF1
83 /^extern.*implemented/{
84         h
85         s/NCURSES_SP_NAME(\([^)]*\))/NCURSES_SP_NAME___\1/
86         h
87         s/^.*implemented:\([^   *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
88         g
89         s/^extern \([^;]*\);.*/\1/p
90         g
91         s/^.*implemented:\([^   *]*\).*/P_POUNDCendif/p
92 }
93 /^extern.*generated/{
94         h
95         s/^.*generated:\([^     *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
96         g
97         s/^extern \([^;]*\);.*/\1/p
98         g
99         s/^.*generated:\([^     *]*\).*/P_POUNDCendif/p
100 }
101 EOF1
102 else
103         CALL=""
104         cat >$ED1 <<EOF1
105 /^extern.*${ALL}/{
106         h
107         s/^.*${ALL}:\([^        *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
108         g
109         s/^extern \([^;]*\);.*/\1/p
110         g
111         s/^.*${ALL}:\([^        *]*\).*/P_POUNDCendif/p
112 }
113 EOF1
114 fi
115
116 cat >$ED2 <<EOF2
117 /^P_/b nc
118 /(void)/b nc
119         s/,/ a1% /
120         s/,/ a2% /
121         s/,/ a3% /
122         s/,/ a4% /
123         s/,/ a5% /
124         s/,/ a6% /
125         s/,/ a7% /
126         s/,/ a8% /
127         s/,/ a9% /
128         s/,/ a10% /
129         s/,/ a11% /
130         s/,/ a12% /
131         s/,/ a13% /
132         s/,/ a14% /
133         s/,/ a15% /
134         s/*/ * /g
135         s/%/ , /g
136         s/)/ z)/
137         s/\.\.\. z)/...)/
138 :nc
139         s/(/ ( /
140         s/)/ )/
141 EOF2
142
143 cat >$ED3 <<EOF3
144 /^P_/{
145         s/^P_POUNDCif_/#if /
146         s/^P_POUNDCendif/#endif/
147         s/^P_//
148         b done
149 }
150         s/              */ /g
151         s/  */ /g
152         s/ ,/,/g
153         s/( /(/g
154         s/ )/)/g
155         s/ gen_/ /
156         s/^[    ]*@[    ]*@[    ]*/     /
157 :done
158 EOF3
159
160 if test "$USE" = generated ; then
161 cat >$ED4 <<EOF
162         s/^\(.*\) \(.*\) (\(.*\))\$/NCURSES_EXPORT(\1) (\2) (\3)/
163 EOF
164 else
165 cat >$ED4 <<EOF
166 /^\(.*\) \(.*\) (\(.*\))\$/ {
167         h
168         s/^\(.*\) \(.*\) (\(.*\))\$/extern \1 call_\2 (\3);/
169         p
170         g
171         s/^\(.*\) \(.*\) (\(.*\))\$/\1 call_\2 (\3)/
172         }
173 s/\([^_]\)NCURSES_SP_NAME___\([a-zA-Z][a-zA-Z_]*\)/\1NCURSES_SP_NAME(\2)/g
174 EOF
175 fi
176
177 cat >$AW1 <<\EOF1
178 BEGIN   {
179                 skip=0;
180         }
181 /^P_POUNDCif/ {
182                 print "\n"
183                 print $0
184                 skip=0;
185 }
186 /^P_POUNDCendif/ {
187                 print $0
188                 skip=1;
189 }
190 $0 !~ /^P_/ {
191         if (skip)
192                 print "\n"
193         skip=1;
194
195         first=$1
196         for (i = 1; i <= NF; i++) {
197                 if ( $i != "NCURSES_CONST" ) {
198                         first = i;
199                         break;
200                 }
201         }
202         second = first + 1;
203         if ( $first == "chtype" ) {
204                 returnType = "Chtype";
205         } else if ( $first == "SCREEN" ) {
206                 returnType = "SP";
207         } else if ( $first == "WINDOW" ) {
208                 returnType = "Win";
209         } else if ( $first == "attr_t" || $second == "attrset" || $second == "standout" || $second == "standend" || $second == "wattrset" || $second == "wstandout" || $second == "wstandend" ) {
210                 returnType = "Attr";
211         } else if ( $first == "bool" || $first == "NCURSES_BOOL" ) {
212                 returnType = "Bool";
213         } else if ( $second == "*" ) {
214                 returnType = "Ptr";
215         } else {
216                 returnType = "Code";
217         }
218         myfunc = second;
219         for (i = second; i <= NF; i++) {
220                 if ($i != "*") {
221                         myfunc = i;
222                         break;
223                 }
224         }
225         print $0;
226         print "{";
227         argcount = 1;
228         check = NF - 1;
229         if ($check == "void")
230                 argcount = 0;
231         if (argcount != 0) {
232                 for (i = 1; i <= NF; i++)
233                         if ($i == ",")
234                                 argcount++;
235         }
236
237         # suppress trace-code for functions that we cannot do properly here,
238         # since they return data.
239         dotrace = 1;
240         if ($myfunc ~ /innstr/)
241                 dotrace = 0;
242         if ($myfunc ~ /innwstr/)
243                 dotrace = 0;
244
245         # workaround functions that we do not parse properly
246         if ($myfunc ~ /ripoffline/) {
247                 dotrace = 0;
248                 argcount = 2;
249                 if ($myfunc ~ /NCURSES_SP_NAME/) {
250                         argcount = 3;
251                 }
252         }
253         if ($myfunc ~ /wunctrl/) {
254                 dotrace = 0;
255         }
256
257         call = "@@T((T_CALLED(\""
258         args = ""
259         comma = ""
260         num = 0;
261         pointer = 0;
262         va_list = 0;
263         varargs = 0;
264         argtype = ""
265         for (i = myfunc; i <= NF; i++) {
266                 ch = $i;
267                 if ( ch == "*" )
268                         pointer = 1;
269                 else if ( ch == "va_list" )
270                         va_list = 1;
271                 else if ( ch == "..." )
272                         varargs = 1;
273                 else if ( ch == "char" )
274                         argtype = "char";
275                 else if ( ch == "int" )
276                         argtype = "int";
277                 else if ( ch == "short" )
278                         argtype = "short";
279                 else if ( ch == "chtype" )
280                         argtype = "chtype";
281                 else if ( ch == "attr_t" || ch == "NCURSES_ATTR_T" )
282                         argtype = "attr";
283
284                 if ( ch == "," || ch == ")" ) {
285                         if (va_list) {
286                                 call = call "%s"
287                         } else if (varargs) {
288                                 call = call "%s"
289                         } else if (pointer) {
290                                 if ( argtype == "char" ) {
291                                         call = call "%s"
292                                         comma = comma "_nc_visbuf2(" num ","
293                                         pointer = 0;
294                                 } else {
295                                         call = call "%p"
296                                         comma = comma "(const void *)"
297                                 }
298                         } else if (argcount != 0) {
299                                 if ( argtype == "int" || argtype == "short" ) {
300                                         call = call "%d"
301                                         argtype = ""
302                                 } else if ( argtype != "" ) {
303                                         call = call "%s"
304                                         comma = comma "_trace" argtype "2(" num ","
305                                 } else {
306                                         call = call "%#lx"
307                                         comma = comma "(long)"
308                                 }
309                         }
310                         if (ch == ",") {
311                                 args = args comma "a" ++num;
312                         } else if ( argcount != 0 ) {
313                                 if ( va_list ) {
314                                         args = args comma "\"va_list\""
315                                 } else if ( varargs ) {
316                                         args = args comma "\"...\""
317                                 } else {
318                                         args = args comma "z"
319                                 }
320                         }
321                         call = call ch
322                         if (pointer == 0 && argcount != 0 && argtype != "" )
323                                 args = args ")"
324                         if (args != "")
325                                 comma = ", "
326                         pointer = 0;
327                         argtype = ""
328                 }
329                 if ( i == myfunc || ch == "(" )
330                         call = call ch
331         }
332         call = call "\")"
333         if (args != "")
334                 call = call ", " args
335         call = call ")); "
336
337         if (dotrace)
338                 printf "%s", call
339
340         if (match($0, "^void"))
341                 call = ""
342         else if (dotrace)
343                 call = sprintf("return%s( ", returnType);
344         else
345                 call = "@@return ";
346
347         call = call $myfunc "(";
348         for (i = 1; i < argcount; i++) {
349                 if (i != 1)
350                         call = call ", ";
351                 call = call "a" i;
352         }
353         if ( argcount != 0 && $check != "..." ) {
354                 if (argcount != 1)
355                         call = call ", ";
356                 call = call "z";
357         }
358         if (!match($0, "^void"))
359                 call = call ") ";
360         if (dotrace)
361                 call = call ")";
362         print call ";"
363
364         if (match($0, "^void"))
365                 print "@@returnVoid;"
366         print "}";
367 }
368 EOF1
369
370 cat >$AW2 <<EOF1
371 BEGIN           {
372                 print "/*"
373                 print " * DO NOT EDIT THIS FILE BY HAND!"
374                 printf " * It is generated by $0 %s.\n", "$USE"
375                 if ( "$USE" == "generated" ) {
376                         print " *"
377                         print " * This is a file of trivial functions generated from macro"
378                         print " * definitions in curses.h to satisfy the XSI Curses requirement"
379                         print " * that every macro also exist as a callable function."
380                         print " *"
381                         print " * It will never be linked unless you call one of the entry"
382                         print " * points with its normal macro definition disabled.  In that"
383                         print " * case, if you have no shared libraries, it will indirectly"
384                         print " * pull most of the rest of the library into your link image."
385                 }
386                 print " */"
387                 print "#define NCURSES_ATTR_T int"
388                 print "#include <ncurses_cfg.h>"
389                 print ""
390                 print "#undef NCURSES_NOMACROS  /* _this_ file uses macros */"
391                 print ""
392                 print "#include <curses.priv.h>"
393                 print ""
394                 print "#undef vw_scanw"
395                 print "#undef vwscanw"
396                 print ""
397                 print "#undef vw_printw"
398                 print "#undef vwprintw"
399                 }
400 /^DECLARATIONS/ {start = 1; next;}
401                 {if (start) print \$0;}
402 END             {
403                 if ( "$USE" != "generated" ) {
404                         print "int main(void) { return 0; }"
405                 }
406                 }
407 EOF1
408
409 cat >$TMP <<EOF
410 #include <ncurses_cfg.h>
411 #undef NCURSES_NOMACROS
412 #include <curses.h>
413
414 DECLARATIONS
415
416 EOF
417
418 sed -n -f $ED1 \
419 | sed -e 's/NCURSES_EXPORT(\(.*\)) \(.*\) (\(.*\))/\1 \2(\3)/' \
420 | sed -f $ED2 \
421 | $AWK -f $AW1 using=$USE \
422 | sed \
423         -e 's/ [ ]*$//g' \
424         -e 's/^\([a-zA-Z_][a-zA-Z_]*[ *]*\)/\1 gen_/' \
425         -e 's/gen_$//' \
426         -e 's/  / /g' >>$TMP
427
428 $preprocessor $TMP 2>/dev/null \
429 | sed \
430         -e 's/  / /g' \
431         -e 's/^ //' \
432         -e 's/_Bool/NCURSES_BOOL/g' \
433 | $AWK -f $AW2 \
434 | sed -f $ED3 \
435 | sed \
436         -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
437         -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/        return \1;/' \
438         -e 's/gen_//' \
439         -e 's/^[        ]*#/#/' \
440         -e '/#ident/d' \
441         -e '/#line/d' \
442 | sed -f $ED4