3 # MKlib_gen.sh -- generate sources from curses.h macro definitions
5 # ($Id: MKlib_gen.sh,v 1.71 2021/09/26 22:08:53 tom Exp $)
7 ##############################################################################
8 # Copyright 2018-2020,2021 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 "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" 0 1 2 3 15
107 if test "$USE" = implemented ; then
109 /^extern.*implemented/{
111 s/GCC_DEPRECATED([^)]*)//
112 s/NCURSES_SP_NAME(\([^)]*\))/NCURSES_SP_NAME___\1/
114 s/^.*implemented:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
116 s/^extern \([^;]*\);.*/\1/p
118 s/^.*implemented:\([^ *]*\).*/P_POUNDCendif/p
120 /^extern.*generated/{
122 s/^.*generated:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
124 s/^extern \([^;]*\);.*/\1/p
126 s/^.*generated:\([^ *]*\).*/P_POUNDCendif/p
133 s/^.*${ALL}:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
135 s/^extern \([^;]*\);.*/\1/p
137 s/^.*${ALL}:\([^ *]*\).*/P_POUNDCendif/p
172 s/^P_POUNDCendif/#endif/
186 if test "$USE" = generated ; then
188 s/^\(.*\) \(.*\) (\(.*\))\$/NCURSES_EXPORT(\1) \2 (\3)/
189 /attr_[sg]et.* z)/s,z),z GCC_UNUSED),
193 /^\(.*\) \(.*\) (\(.*\))\$/ {
195 s/^\(.*\) \(.*\) (\(.*\))\$/extern \1 call_\2 (\3);/
198 s/^\(.*\) \(.*\) (\(.*\))\$/\1 call_\2 (\3)/
200 s/\([^_]\)NCURSES_SP_NAME___\([a-zA-Z][a-zA-Z_]*\)/\1NCURSES_SP_NAME(\2)/g
223 for (i = 1; i <= NF; i++) {
224 if ( $i != "NCURSES_CONST" ) {
231 if ( $first == "chtype" ) {
232 returnType = "Chtype";
233 } else if ( $first == "SCREEN" ) {
235 } else if ( $first == "WINDOW" ) {
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" ) {
242 } else if ( $second == "*" ) {
243 returnType = ($1 == "NCURSES_CONST") ? "CPtr" : "Ptr";
248 for (i = second; i <= NF; i++) {
254 if (using == "implemented") {
255 printf "#undef %s\n", $myfunc;
261 if ($check == "void")
264 for (i = 1; i <= NF; i++)
269 # suppress trace-code for functions that we cannot do properly here,
270 # since they return data.
272 if ($myfunc ~ /innstr/)
274 if ($myfunc ~ /innwstr/)
277 # workaround functions that we do not parse properly
278 if ($myfunc ~ /ripoffline/) {
281 if ($myfunc ~ /NCURSES_SP_NAME/) {
285 if ($myfunc ~ /wunctrl/) {
290 if ($myfunc ~ /get[n]?str/) {
294 call = "@@T((T_CALLED(\""
302 for (i = myfunc; i <= NF; i++) {
306 } else if ( ch == "va_list" ) {
308 } else if ( ch == "..." ) {
310 } else if ( ch == "char" ) {
312 } else if ( ch == "int" ) {
314 } else if ( ch == "short" ) {
316 } else if ( ch == "chtype" ) {
318 } else if ( ch == "attr_t" || ch == "NCURSES_ATTR_T" ) {
322 if ( ch == "," || ch == ")" ) {
326 } else if (varargs) {
328 } else if (pointer) {
329 if ( argtype == "char" ) {
335 comma = comma "_nc_visbuf2(" num ","
339 comma = comma "(const void *)"
341 } else if (argcount != 0) {
342 if ( argtype == "int" || argtype == "short" ) {
345 } else if ( argtype != "" ) {
347 comma = comma "_trace" argtype "2(" num ","
348 if (argtype == "attr") {
349 argcast = "(chtype)";
353 comma = comma "(long)"
357 args = args comma "a" ++num;
358 } else if ( argcount != 0 ) {
360 args = args comma "\"va_list\""
361 } else if ( varargs ) {
362 args = args comma "\"...\""
364 args = args comma argcast "z"
368 if (pointer == 0 && argcount != 0 && argtype != "" )
375 if ( i == myfunc || ch == "(" )
380 call = call ", " args
384 printf "%s\n\t@@", call
386 if (match($0, "^void")) {
388 } else if (dotrace) {
389 call = sprintf("return%s( ", returnType);
390 if (returnCast != "") {
391 call = call returnCast;
397 call = call $myfunc "(";
398 for (i = 1; i < argcount; i++) {
403 if ( argcount != 0 && $check != "..." ) {
408 if (!match($0, "^void"))
415 if (match($0, "^void"))
416 print "@@returnVoid;"
423 printf "/* This file was generated by $0 $USE */\n"
426 print " * DO NOT EDIT THIS FILE BY HAND!"
427 if ( "$USE" == "generated" ) {
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."
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."
439 print "#define NCURSES_ATTR_T int"
440 print "#include <ncurses_cfg.h>"
442 print "#undef NCURSES_NOMACROS /* _this_ file uses macros */"
443 print "#define NCURSES_NOMACROS 1"
445 print "#include <curses.priv.h>"
448 /^DECLARATIONS/ {start = 1; next;}
451 if ( "$USE" == "generated" ) {
453 } else if ( \$0 ~ /^[{}]?\$/ ) {
455 } else if ( \$0 ~ /;/ ) {
465 if ( "$USE" != "generated" ) {
466 print "int main(void)"
468 for (n = 1; n < start; ++n) {
470 if ( value !~ /P_POUNDC/ ) {
471 gsub(/[ \t]+/," ",value);
472 sub(/^[0-9a-zA-Z_]+ /,"",value);
473 sub(/^[*][ \t]*/,"",value);
474 gsub("struct[ \t]*[0-9a-zA-Z_]+[ \t]*[*]","",value);
475 gsub(/[0-9a-zA-Z_]+[ \t]*[*][ \t]*/,"",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;
494 #include <ncurses_cfg.h>
495 #undef NCURSES_NOMACROS
505 | sed -e 's/NCURSES_EXPORT(\(.*\)) \(.*\) (\(.*\))/\1 \2(\3)/' \
507 | "$AWK" -f $AW1 using="$USE" \
510 -e 's/^\([a-zA-Z_][a-zA-Z_]*[ *]*\)/\1 gen_/' \
514 $preprocessor $TMP 2>/dev/null \
518 -e 's/_Bool/NCURSES_BOOL/g' \
522 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
523 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/' \