3 # MKlib_gen.sh -- generate sources from curses.h macro definitions
5 # ($Id: MKlib_gen.sh,v 1.60 2017/12/09 22:27:41 tom Exp $)
7 ##############################################################################
8 # Copyright (c) 1998-2017,2017 Free Software Foundation, Inc. #
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: #
18 # The above copyright notice and this permission notice shall be included in #
19 # all copies or substantial portions of the Software. #
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. #
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 #
33 ##############################################################################
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.
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.
45 # Here is what the pipeline stages are doing:
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
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.
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
65 preprocessor="$1 -DNCURSES_WATTR_MACROS -DNCURSES_INTERNALS -I../include"
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"
94 trap "rm -f $ED1 $ED2 $ED3 $ED4 $AW1 $AW2 $TMP" 0 1 2 3 15
97 if test "$USE" = implemented ; then
100 /^extern.*implemented/{
102 s/NCURSES_SP_NAME(\([^)]*\))/NCURSES_SP_NAME___\1/
104 s/^.*implemented:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
106 s/^extern \([^;]*\);.*/\1/p
108 s/^.*implemented:\([^ *]*\).*/P_POUNDCendif/p
110 /^extern.*generated/{
112 s/^.*generated:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
114 s/^extern \([^;]*\);.*/\1/p
116 s/^.*generated:\([^ *]*\).*/P_POUNDCendif/p
124 s/^.*${ALL}:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
126 s/^extern \([^;]*\);.*/\1/p
128 s/^.*${ALL}:\([^ *]*\).*/P_POUNDCendif/p
163 s/^P_POUNDCendif/#endif/
177 if test "$USE" = generated ; then
179 s/^\(.*\) \(.*\) (\(.*\))\$/NCURSES_EXPORT(\1) (\2) (\3)/
180 /attr_[sg]et.* z)/s,z),z GCC_UNUSED),
184 /^\(.*\) \(.*\) (\(.*\))\$/ {
186 s/^\(.*\) \(.*\) (\(.*\))\$/extern \1 call_\2 (\3);/
189 s/^\(.*\) \(.*\) (\(.*\))\$/\1 call_\2 (\3)/
191 s/\([^_]\)NCURSES_SP_NAME___\([a-zA-Z][a-zA-Z_]*\)/\1NCURSES_SP_NAME(\2)/g
214 for (i = 1; i <= NF; i++) {
215 if ( $i != "NCURSES_CONST" ) {
222 if ( $first == "chtype" ) {
223 returnType = "Chtype";
224 } else if ( $first == "SCREEN" ) {
226 } else if ( $first == "WINDOW" ) {
228 } else if ( $first == "attr_t" || $second == "attrset" || $second == "standout" || $second == "standend" || $second == "wattrset" || $second == "wstandout" || $second == "wstandend" ) {
229 returnType = "IntAttr";
230 returnCast = "(attr_t)";
231 } else if ( $first == "bool" || $first == "NCURSES_BOOL" ) {
233 } else if ( $second == "*" ) {
234 returnType = ($1 == "NCURSES_CONST") ? "CPtr" : "Ptr";
239 for (i = second; i <= NF; i++) {
245 if (using == "implemented") {
246 printf "#undef %s\n", $myfunc;
252 if ($check == "void")
255 for (i = 1; i <= NF; i++)
260 # suppress trace-code for functions that we cannot do properly here,
261 # since they return data.
263 if ($myfunc ~ /innstr/)
265 if ($myfunc ~ /innwstr/)
268 # workaround functions that we do not parse properly
269 if ($myfunc ~ /ripoffline/) {
272 if ($myfunc ~ /NCURSES_SP_NAME/) {
276 if ($myfunc ~ /wunctrl/) {
281 if ($myfunc ~ /get[n]?str/) {
285 call = "@@T((T_CALLED(\""
293 for (i = myfunc; i <= NF; i++) {
297 } else if ( ch == "va_list" ) {
299 } else if ( ch == "..." ) {
301 } else if ( ch == "char" ) {
303 } else if ( ch == "int" ) {
305 } else if ( ch == "short" ) {
307 } else if ( ch == "chtype" ) {
309 } else if ( ch == "attr_t" || ch == "NCURSES_ATTR_T" ) {
313 if ( ch == "," || ch == ")" ) {
317 } else if (varargs) {
319 } else if (pointer) {
320 if ( argtype == "char" ) {
326 comma = comma "_nc_visbuf2(" num ","
330 comma = comma "(const void *)"
332 } else if (argcount != 0) {
333 if ( argtype == "int" || argtype == "short" ) {
336 } else if ( argtype != "" ) {
338 comma = comma "_trace" argtype "2(" num ","
339 if (argtype == "attr") {
340 argcast = "(chtype)";
344 comma = comma "(long)"
348 args = args comma "a" ++num;
349 } else if ( argcount != 0 ) {
351 args = args comma "\"va_list\""
352 } else if ( varargs ) {
353 args = args comma "\"...\""
355 args = args comma argcast "z"
359 if (pointer == 0 && argcount != 0 && argtype != "" )
366 if ( i == myfunc || ch == "(" )
371 call = call ", " args
375 printf "%s\n\t@@", call
377 if (match($0, "^void")) {
379 } else if (dotrace) {
380 call = sprintf("return%s( ", returnType);
381 if (returnCast != "") {
382 call = call returnCast;
388 call = call $myfunc "(";
389 for (i = 1; i < argcount; i++) {
394 if ( argcount != 0 && $check != "..." ) {
399 if (!match($0, "^void"))
406 if (match($0, "^void"))
407 print "@@returnVoid;"
414 printf "/* This file was generated by $0 $USE */\n"
417 print " * DO NOT EDIT THIS FILE BY HAND!"
418 if ( "$USE" == "generated" ) {
420 print " * This is a file of trivial functions generated from macro"
421 print " * definitions in curses.h to satisfy the XSI Curses requirement"
422 print " * that every macro also exist as a callable function."
424 print " * It will never be linked unless you call one of the entry"
425 print " * points with its normal macro definition disabled. In that"
426 print " * case, if you have no shared libraries, it will indirectly"
427 print " * pull most of the rest of the library into your link image."
430 print "#define NCURSES_ATTR_T int"
431 print "#include <ncurses_cfg.h>"
433 print "#undef NCURSES_NOMACROS /* _this_ file uses macros */"
435 print "#include <curses.priv.h>"
437 print "#undef vw_scanw"
438 print "#undef vwscanw"
440 print "#undef vw_printw"
441 print "#undef vwprintw"
443 /^DECLARATIONS/ {start = 1; next;}
446 if ( "$USE" == "generated" ) {
448 } else if ( \$0 ~ /^[{}]?\$/ ) {
450 } else if ( \$0 ~ /;/ ) {
460 if ( "$USE" != "generated" ) {
461 print "int main(void)"
463 for (n = 1; n < start; ++n) {
465 if ( value !~ /P_POUNDC/ ) {
466 gsub(/[ \t]+/," ",value);
467 sub(/^[0-9a-zA-Z_]+ /,"",value);
468 sub(/^\* /,"",value);
469 gsub(/[0-9a-zA-Z_]+ \* /,"",value);
470 gsub(/ (const) /," ",value);
471 gsub(/ (int|short|attr_t|chtype|wchar_t|NCURSES_BOOL|NCURSES_OUTC|NCURSES_OUTC_sp|va_list) /," ",value);
472 gsub(/ void /,"",value);
473 sub(/^/,"call_",value);
474 gsub(/ (a[0-9]|z) /, " 0 ", value);
475 gsub(/ int[ \t]*[(][^)]+[)][(][^)]+[)]/, "0", value);
476 printf "\t%s;\n", value;
488 #include <ncurses_cfg.h>
489 #undef NCURSES_NOMACROS
499 | sed -e 's/NCURSES_EXPORT(\(.*\)) \(.*\) (\(.*\))/\1 \2(\3)/' \
501 | $AWK -f $AW1 using=$USE \
504 -e 's/^\([a-zA-Z_][a-zA-Z_]*[ *]*\)/\1 gen_/' \
508 $preprocessor $TMP 2>/dev/null \
512 -e 's/_Bool/NCURSES_BOOL/g' \
516 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
517 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/' \