3 # MKlib_gen.sh -- generate sources from curses.h macro definitions
5 # ($Id: MKlib_gen.sh,v 1.73 2022/10/01 13:14:20 tom Exp $)
7 ##############################################################################
8 # Copyright 2018-2021,2022 Thomas E. Dickey #
9 # Copyright 1998-2016,2017 Free Software Foundation, Inc. #
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: #
19 # The above copyright notice and this permission notice shall be included in #
20 # all copies or substantial portions of the Software. #
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. #
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 #
34 ##############################################################################
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.
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.
46 # Here is what the pipeline stages are doing:
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
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.
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
66 preprocessor="$1 -DNCURSES_WATTR_MACROS -DNCURSES_INTERNALS -I../include"
70 # A patch discussed here:
71 # https://gcc.gnu.org/ml/gcc-patches/2014-06/msg02185.html
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.
79 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60723
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
85 PRG=`echo "$1" | "$AWK" '{ sub(/^[ ]*/,""); sub(/[ ].*$/, ""); print; }' || exit 0`
86 FSF=`("$PRG" --version 2>/dev/null || exit 0) | ${FGREP-grep -F} "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"
104 trap "rm -f $ED1 $ED2 $ED3 $ED4 $AW1 $AW2 $TMP; exit 1" 1 2 3 15
105 trap "rm -f $ED1 $ED2 $ED3 $ED4 $AW1 $AW2 $TMP" 0
108 if test "$USE" = implemented ; then
110 /^extern.*implemented/{
112 s/GCC_DEPRECATED([^)]*)//
113 s/NCURSES_SP_NAME(\([^)]*\))/NCURSES_SP_NAME___\1/
115 s/^.*implemented:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
117 s/^extern \([^;]*\);.*/\1/p
119 s/^.*implemented:\([^ *]*\).*/P_POUNDCendif/p
121 /^extern.*generated/{
123 s/^.*generated:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
125 s/^extern \([^;]*\);.*/\1/p
127 s/^.*generated:\([^ *]*\).*/P_POUNDCendif/p
134 s/^.*${ALL}:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
136 s/^extern \([^;]*\);.*/\1/p
138 s/^.*${ALL}:\([^ *]*\).*/P_POUNDCendif/p
173 s/^P_POUNDCendif/#endif/
187 if test "$USE" = generated ; then
189 s/^\(.*\) \(.*\) (\(.*\))\$/NCURSES_EXPORT(\1) \2 (\3)/
190 /attr_[sg]et.* z)/s,z),z GCC_UNUSED),
194 /^\(.*\) \(.*\) (\(.*\))\$/ {
196 s/^\(.*\) \(.*\) (\(.*\))\$/extern \1 call_\2 (\3);/
199 s/^\(.*\) \(.*\) (\(.*\))\$/\1 call_\2 (\3)/
201 s/\([^_]\)NCURSES_SP_NAME___\([a-zA-Z][a-zA-Z_]*\)/\1NCURSES_SP_NAME(\2)/g
224 for (i = 1; i <= NF; i++) {
225 if ( $i != "NCURSES_CONST" ) {
232 if ( $first == "chtype" ) {
233 returnType = "Chtype";
234 } else if ( $first == "SCREEN" ) {
236 } else if ( $first == "WINDOW" ) {
238 } else if ( $first == "attr_t" || $second == "attrset" || $second == "standout" || $second == "standend" || $second == "wattrset" || $second == "wstandout" || $second == "wstandend" ) {
239 returnType = "IntAttr";
240 returnCast = "(attr_t)";
241 } else if ( $first == "bool" || $first == "NCURSES_BOOL" ) {
243 } else if ( $second == "*" ) {
244 returnType = ($1 == "NCURSES_CONST") ? "CPtr" : "Ptr";
249 for (i = second; i <= NF; i++) {
255 if (using == "implemented") {
256 printf "#undef %s\n", $myfunc;
262 if ($check == "void")
265 for (i = 1; i <= NF; i++)
270 # suppress trace-code for functions that we cannot do properly here,
271 # since they return data.
273 if ($myfunc ~ /innstr/)
275 if ($myfunc ~ /innwstr/)
278 # workaround functions that we do not parse properly
279 if ($myfunc ~ /ripoffline/) {
282 if ($myfunc ~ /NCURSES_SP_NAME/) {
286 if ($myfunc ~ /wunctrl/) {
291 if ($myfunc ~ /get[n]?str/) {
295 call = "@@T((T_CALLED(\""
303 for (i = myfunc; i <= NF; i++) {
307 } else if ( ch == "va_list" ) {
309 } else if ( ch == "..." ) {
311 } else if ( ch == "char" ) {
313 } else if ( ch == "int" ) {
315 } else if ( ch == "short" ) {
317 } else if ( ch == "chtype" ) {
319 } else if ( ch == "attr_t" || ch == "NCURSES_ATTR_T" ) {
323 if ( ch == "," || ch == ")" ) {
327 } else if (varargs) {
329 } else if (pointer) {
330 if ( argtype == "char" ) {
336 comma = comma "_nc_visbuf2(" num ","
340 comma = comma "(const void *)"
342 } else if (argcount != 0) {
343 if ( argtype == "int" || argtype == "short" ) {
346 } else if ( argtype != "" ) {
348 comma = comma "_trace" argtype "2(" num ","
349 if (argtype == "attr") {
350 argcast = "(chtype)";
354 comma = comma "(long)"
358 args = args comma "a" ++num;
359 } else if ( argcount != 0 ) {
361 args = args comma "\"va_list\""
362 } else if ( varargs ) {
363 args = args comma "\"...\""
365 args = args comma argcast "z"
369 if (pointer == 0 && argcount != 0 && argtype != "" )
376 if ( i == myfunc || ch == "(" )
381 call = call ", " args
385 printf "%s\n\t@@", call
387 if (match($0, "^void")) {
389 } else if (dotrace) {
390 call = sprintf("return%s( ", returnType);
391 if (returnCast != "") {
392 call = call returnCast;
398 call = call $myfunc "(";
399 for (i = 1; i < argcount; i++) {
404 if ( argcount != 0 && $check != "..." ) {
409 if (!match($0, "^void"))
416 if (match($0, "^void"))
417 print "@@returnVoid;"
424 printf "/* This file was generated by $0 $USE */\n"
427 print " * DO NOT EDIT THIS FILE BY HAND!"
428 if ( "$USE" == "generated" ) {
430 print " * This is a file of trivial functions generated from macro"
431 print " * definitions in curses.h to satisfy the XSI Curses requirement"
432 print " * that every macro also exist as a callable function."
434 print " * It will never be linked unless you call one of the entry"
435 print " * points with its normal macro definition disabled. In that"
436 print " * case, if you have no shared libraries, it will indirectly"
437 print " * pull most of the rest of the library into your link image."
440 print "#define NCURSES_ATTR_T int"
441 print "#include <ncurses_cfg.h>"
443 print "#undef NCURSES_NOMACROS /* _this_ file uses macros */"
444 print "#define NCURSES_NOMACROS 1"
446 print "#include <curses.priv.h>"
449 /^DECLARATIONS/ {start = 1; next;}
452 if ( "$USE" == "generated" ) {
454 } else if ( \$0 ~ /^[{}]?\$/ ) {
456 } else if ( \$0 ~ /;/ ) {
466 if ( "$USE" != "generated" ) {
467 print "int main(void)"
469 for (n = 1; n < start; ++n) {
471 if ( value !~ /P_POUNDC/ ) {
472 gsub(/[ \t]+/," ",value);
473 sub(/^[0-9a-zA-Z_]+ /,"",value);
474 sub(/^[*][ \t]*/,"",value);
475 gsub("struct[ \t]*[0-9a-zA-Z_]+[ \t]*[*]","",value);
476 gsub(/[0-9a-zA-Z_]+[ \t]*[*][ \t]*/,"",value);
477 gsub(/ (const) /," ",value);
478 gsub(/ (int|short|attr_t|chtype|wchar_t|NCURSES_BOOL|NCURSES_OUTC|NCURSES_OUTC_sp|va_list) /," ",value);
479 gsub(/ void /,"",value);
480 sub(/^/,"call_",value);
481 gsub(/ (a[0-9]|z) /, " 0 ", value);
482 gsub(/ int[ \t]*[(][^)]+[)][(][^)]+[)]/, "0", value);
483 printf "\t%s;\n", value;
495 #include <ncurses_cfg.h>
496 #undef NCURSES_NOMACROS
506 | sed -e 's/NCURSES_EXPORT(\(.*\)) \(.*\) (\(.*\))/\1 \2(\3)/' \
508 | "$AWK" -f $AW1 using="$USE" \
511 -e 's/^\([a-zA-Z_][a-zA-Z_]*[ *]*\)/\1 gen_/' \
515 $preprocessor $TMP 2>/dev/null \
519 -e 's/_Bool/NCURSES_BOOL/g' \
523 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
524 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/' \