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