]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/MKlib_gen.sh
ncurses 6.1 - patch 20190504
[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.62 2018/06/09 19:27:00 tom Exp $)
6 #
7 ##############################################################################
8 # Copyright (c) 1998-2017,2018 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_WATTR_MACROS -DNCURSES_INTERNALS -I../include"
66 AWK="$2"
67 USE="$3"
68
69 # A patch discussed here:
70 #       https://gcc.gnu.org/ml/gcc-patches/2014-06/msg02185.html
71 # introduces spurious #line markers into the preprocessor output.  The result
72 # appears in gcc 5.0 and (with modification) in 5.1, making it necessary to
73 # determine if we are using gcc, and if so, what version because the proposed
74 # solution uses a nonstandard option.
75 PRG=`echo "$1" | $AWK '{ sub(/^[        ]*/,""); sub(/[         ].*$/, ""); print; }' || exit 0`
76 FSF=`"$PRG" --version 2>/dev/null || exit 0 | fgrep "Free Software Foundation" | head -n 1`
77 ALL=`"$PRG" -dumpversion 2>/dev/null || exit 0`
78 ONE=`echo "$ALL" | sed -e 's/\..*$//'`
79 if test -n "$FSF" && test -n "$ALL" && test -n "$ONE" ; then
80         if test $ONE -ge 5 ; then
81                 echo ".. adding -P option to work around $PRG $ALL" >&2
82                 preprocessor="$preprocessor -P"
83         fi
84 fi
85
86 PID=$$
87 ED1=sed1_${PID}.sed
88 ED2=sed2_${PID}.sed
89 ED3=sed3_${PID}.sed
90 ED4=sed4_${PID}.sed
91 AW1=awk1_${PID}.awk
92 AW2=awk2_${PID}.awk
93 TMP=gen__${PID}.c
94 trap "rm -f $ED1 $ED2 $ED3 $ED4 $AW1 $AW2 $TMP" 0 1 2 3 15
95
96 ALL=$USE
97 if test "$USE" = implemented ; then
98         CALL="call_"
99         cat >$ED1 <<EOF1
100 /^extern.*implemented/{
101         h
102         s/GCC_DEPRECATED([^)]*)//
103         s/NCURSES_SP_NAME(\([^)]*\))/NCURSES_SP_NAME___\1/
104         h
105         s/^.*implemented:\([^   *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
106         g
107         s/^extern \([^;]*\);.*/\1/p
108         g
109         s/^.*implemented:\([^   *]*\).*/P_POUNDCendif/p
110 }
111 /^extern.*generated/{
112         h
113         s/^.*generated:\([^     *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
114         g
115         s/^extern \([^;]*\);.*/\1/p
116         g
117         s/^.*generated:\([^     *]*\).*/P_POUNDCendif/p
118 }
119 EOF1
120 else
121         CALL=""
122         cat >$ED1 <<EOF1
123 /^extern.*${ALL}/{
124         h
125         s/^.*${ALL}:\([^        *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
126         g
127         s/^extern \([^;]*\);.*/\1/p
128         g
129         s/^.*${ALL}:\([^        *]*\).*/P_POUNDCendif/p
130 }
131 EOF1
132 fi
133
134 cat >$ED2 <<EOF2
135 /^P_/b nc
136 /(void)/b nc
137         s/,/ a1% /
138         s/,/ a2% /
139         s/,/ a3% /
140         s/,/ a4% /
141         s/,/ a5% /
142         s/,/ a6% /
143         s/,/ a7% /
144         s/,/ a8% /
145         s/,/ a9% /
146         s/,/ a10% /
147         s/,/ a11% /
148         s/,/ a12% /
149         s/,/ a13% /
150         s/,/ a14% /
151         s/,/ a15% /
152         s/*/ * /g
153         s/%/ , /g
154         s/)/ z)/
155         s/\.\.\. z)/...)/
156 :nc
157         s/(/ ( /
158         s/)/ )/
159 EOF2
160
161 cat >$ED3 <<EOF3
162 /^P_/{
163         s/^P_POUNDCif_/#if /
164         s/^P_POUNDCendif/#endif/
165         s/^P_//
166         b done
167 }
168         s/              */ /g
169         s/  */ /g
170         s/ ,/,/g
171         s/( /(/g
172         s/ )/)/g
173         s/ gen_/ /
174         s/^[    ]*@[    ]*@[    ]*/     /
175 :done
176 EOF3
177
178 if test "$USE" = generated ; then
179 cat >$ED4 <<EOF
180         s/^\(.*\) \(.*\) (\(.*\))\$/NCURSES_EXPORT(\1) (\2) (\3)/
181         /attr_[sg]et.* z)/s,z),z GCC_UNUSED),
182 EOF
183 else
184 cat >$ED4 <<EOF
185 /^\(.*\) \(.*\) (\(.*\))\$/ {
186         h
187         s/^\(.*\) \(.*\) (\(.*\))\$/extern \1 call_\2 (\3);/
188         p
189         g
190         s/^\(.*\) \(.*\) (\(.*\))\$/\1 call_\2 (\3)/
191         }
192 s/\([^_]\)NCURSES_SP_NAME___\([a-zA-Z][a-zA-Z_]*\)/\1NCURSES_SP_NAME(\2)/g
193 EOF
194 fi
195
196 cat >$AW1 <<\EOF1
197 BEGIN   {
198                 skip=0;
199         }
200 /^P_POUNDCif/ {
201                 print "\n"
202                 print $0
203                 skip=0;
204 }
205 /^P_POUNDCendif/ {
206                 print $0
207                 skip=1;
208 }
209 $0 !~ /^P_/ {
210         if (skip)
211                 print "\n"
212         skip=1;
213
214         first=$1
215         for (i = 1; i <= NF; i++) {
216                 if ( $i != "NCURSES_CONST" ) {
217                         first = i;
218                         break;
219                 }
220         }
221         second = first + 1;
222         returnCast = "";
223         if ( $first == "chtype" ) {
224                 returnType = "Chtype";
225         } else if ( $first == "SCREEN" ) {
226                 returnType = "SP";
227         } else if ( $first == "WINDOW" ) {
228                 returnType = "Win";
229         } else if ( $first == "attr_t" || $second == "attrset" || $second == "standout" || $second == "standend" || $second == "wattrset" || $second == "wstandout" || $second == "wstandend" ) {
230                 returnType = "IntAttr";
231                 returnCast = "(attr_t)";
232         } else if ( $first == "bool" || $first == "NCURSES_BOOL" ) {
233                 returnType = "Bool";
234         } else if ( $second == "*" ) {
235                 returnType = ($1 == "NCURSES_CONST") ? "CPtr" : "Ptr";
236         } else {
237                 returnType = "Code";
238         }
239         myfunc = second;
240         for (i = second; i <= NF; i++) {
241                 if ($i != "*") {
242                         myfunc = i;
243                         break;
244                 }
245         }
246         if (using == "implemented") {
247                 printf "#undef %s\n", $myfunc;
248         }
249         print $0;
250         print "{";
251         argcount = 1;
252         check = NF - 1;
253         if ($check == "void")
254                 argcount = 0;
255         if (argcount != 0) {
256                 for (i = 1; i <= NF; i++)
257                         if ($i == ",")
258                                 argcount++;
259         }
260
261         # suppress trace-code for functions that we cannot do properly here,
262         # since they return data.
263         dotrace = 1;
264         if ($myfunc ~ /innstr/)
265                 dotrace = 0;
266         if ($myfunc ~ /innwstr/)
267                 dotrace = 0;
268
269         # workaround functions that we do not parse properly
270         if ($myfunc ~ /ripoffline/) {
271                 dotrace = 0;
272                 argcount = 2;
273                 if ($myfunc ~ /NCURSES_SP_NAME/) {
274                         argcount = 3;
275                 }
276         }
277         if ($myfunc ~ /wunctrl/) {
278                 dotrace = 0;
279         }
280
281         do_getstr = 0;
282         if ($myfunc ~ /get[n]?str/) {
283                 do_getstr = 1;
284         }
285
286         call = "@@T((T_CALLED(\""
287         args = ""
288         comma = ""
289         num = 0;
290         pointer = 0;
291         va_list = 0;
292         varargs = 0;
293         argtype = ""
294         for (i = myfunc; i <= NF; i++) {
295                 ch = $i;
296                 if ( ch == "*" ) {
297                         pointer = 1;
298                 } else if ( ch == "va_list" ) {
299                         va_list = 1;
300                 } else if ( ch == "..." ) {
301                         varargs = 1;
302                 } else if ( ch == "char" ) {
303                         argtype = "char";
304                 } else if ( ch == "int" ) {
305                         argtype = "int";
306                 } else if ( ch == "short" ) {
307                         argtype = "short";
308                 } else if ( ch == "chtype" ) {
309                         argtype = "chtype";
310                 } else if ( ch == "attr_t" || ch == "NCURSES_ATTR_T" ) {
311                         argtype = "attr";
312                 }
313
314                 if ( ch == "," || ch == ")" ) {
315                         argcast = "";
316                         if (va_list) {
317                                 call = call "%s"
318                         } else if (varargs) {
319                                 call = call "%s"
320                         } else if (pointer) {
321                                 if ( argtype == "char" ) {
322                                         if (do_getstr) {
323                                                 call = call "%p"
324                                         } else {
325                                                 call = call "%s"
326                                         }
327                                         comma = comma "_nc_visbuf2(" num ","
328                                         pointer = 0;
329                                 } else {
330                                         call = call "%p"
331                                         comma = comma "(const void *)"
332                                 }
333                         } else if (argcount != 0) {
334                                 if ( argtype == "int" || argtype == "short" ) {
335                                         call = call "%d"
336                                         argtype = ""
337                                 } else if ( argtype != "" ) {
338                                         call = call "%s"
339                                         comma = comma "_trace" argtype "2(" num ","
340                                         if (argtype == "attr") {
341                                                 argcast = "(chtype)";
342                                         }
343                                 } else {
344                                         call = call "%#lx"
345                                         comma = comma "(long)"
346                                 }
347                         }
348                         if (ch == ",") {
349                                 args = args comma "a" ++num;
350                         } else if ( argcount != 0 ) {
351                                 if ( va_list ) {
352                                         args = args comma "\"va_list\""
353                                 } else if ( varargs ) {
354                                         args = args comma "\"...\""
355                                 } else {
356                                         args = args comma argcast "z"
357                                 }
358                         }
359                         call = call ch
360                         if (pointer == 0 && argcount != 0 && argtype != "" )
361                                 args = args ")"
362                         if (args != "")
363                                 comma = ", "
364                         pointer = 0;
365                         argtype = ""
366                 }
367                 if ( i == myfunc || ch == "(" )
368                         call = call ch
369         }
370         call = call "\")"
371         if (args != "")
372                 call = call ", " args
373         call = call ")); "
374
375         if (dotrace)
376                 printf "%s\n\t@@", call
377
378         if (match($0, "^void")) {
379                 call = ""
380         } else if (dotrace) {
381                 call = sprintf("return%s( ", returnType);
382                 if (returnCast != "") {
383                         call = call returnCast;
384                 }
385         } else {
386                 call = "@@return ";
387         }
388
389         call = call $myfunc "(";
390         for (i = 1; i < argcount; i++) {
391                 if (i != 1)
392                         call = call ", ";
393                 call = call "a" i;
394         }
395         if ( argcount != 0 && $check != "..." ) {
396                 if (argcount != 1)
397                         call = call ", ";
398                 call = call "z";
399         }
400         if (!match($0, "^void"))
401                 call = call ") ";
402         if (dotrace) {
403                 call = call ")";
404         }
405         print call ";"
406
407         if (match($0, "^void"))
408                 print "@@returnVoid;"
409         print "}";
410 }
411 EOF1
412
413 cat >$AW2 <<EOF1
414 BEGIN           {
415                 printf "/* This file was generated by $0 $USE */\n"
416                 print ""
417                 print "/*"
418                 print " * DO NOT EDIT THIS FILE BY HAND!"
419                 if ( "$USE" == "generated" ) {
420                         print " *"
421                         print " * This is a file of trivial functions generated from macro"
422                         print " * definitions in curses.h to satisfy the XSI Curses requirement"
423                         print " * that every macro also exist as a callable function."
424                         print " *"
425                         print " * It will never be linked unless you call one of the entry"
426                         print " * points with its normal macro definition disabled.  In that"
427                         print " * case, if you have no shared libraries, it will indirectly"
428                         print " * pull most of the rest of the library into your link image."
429                 }
430                 print " */"
431                 print "#define NCURSES_ATTR_T int"
432                 print "#include <ncurses_cfg.h>"
433                 print ""
434                 print "#undef NCURSES_NOMACROS  /* _this_ file uses macros */"
435                 print ""
436                 print "#include <curses.priv.h>"
437                 print ""
438                 }
439 /^DECLARATIONS/ {start = 1; next;}
440                 {
441                 if (start) {
442                         if ( "$USE" == "generated" ) {
443                                 print \$0;
444                         } else if ( \$0 ~ /^[{}]?\$/ ) {
445                                 print \$0;
446                         } else if ( \$0 ~ /;/ ) {
447                                 print \$0;
448                         } else {
449                                 calls[start] = \$0;
450                                 print \$0;
451                                 start++;
452                         }
453                 }
454                 }
455 END             {
456                 if ( "$USE" != "generated" ) {
457                         print "int main(void)"
458                         print "{"
459                         for (n = 1; n < start; ++n) {
460                                 value = calls[n];
461                                 if ( value !~ /P_POUNDC/ ) {
462                                         gsub(/[ \t]+/," ",value);
463                                         sub(/^[0-9a-zA-Z_]+ /,"",value);
464                                         sub(/^\* /,"",value);
465                                         gsub(/[0-9a-zA-Z_]+ \* /,"",value);
466                                         gsub(/ (const) /," ",value);
467                                         gsub(/ (int|short|attr_t|chtype|wchar_t|NCURSES_BOOL|NCURSES_OUTC|NCURSES_OUTC_sp|va_list) /," ",value);
468                                         gsub(/ void /,"",value);
469                                         sub(/^/,"call_",value);
470                                         gsub(/ (a[0-9]|z) /, " 0 ", value);
471                                         gsub(/ int[ \t]*[(][^)]+[)][(][^)]+[)]/, "0", value);
472                                         printf "\t%s;\n", value;
473                                 } else {
474                                         print value;
475                                 }
476                         }
477                         print " return 0;"
478                         print "}"
479                 }
480                 }
481 EOF1
482
483 cat >$TMP <<EOF
484 #include <ncurses_cfg.h>
485 #undef NCURSES_NOMACROS
486 #include <curses.h>
487 #include <term.h>
488 #include <unctrl.h>
489
490 DECLARATIONS
491
492 EOF
493
494 sed -n -f $ED1 \
495 | sed -e 's/NCURSES_EXPORT(\(.*\)) \(.*\) (\(.*\))/\1 \2(\3)/' \
496 | sed -f $ED2 \
497 | $AWK -f $AW1 using=$USE \
498 | sed \
499         -e 's/ [ ]*$//g' \
500         -e 's/^\([a-zA-Z_][a-zA-Z_]*[ *]*\)/\1 gen_/' \
501         -e 's/gen_$//' \
502         -e 's/  / /g' >>$TMP
503
504 $preprocessor $TMP 2>/dev/null \
505 | sed \
506         -e 's/  / /g' \
507         -e 's/^ //' \
508         -e 's/_Bool/NCURSES_BOOL/g' \
509 | $AWK -f $AW2 \
510 | sed -f $ED3 \
511 | sed \
512         -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
513         -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/        return \1;/' \
514         -e 's/gen_//' \
515         -e 's/^[        ]*#/#/' \
516         -e '/#ident/d' \
517         -e '/#line/d' \
518 | sed -f $ED4