3 # MKlib_gen.sh -- generate sources from curses.h macro definitions
5 # ($Id: MKlib_gen.sh,v 1.63 2020/02/02 23:34:34 tom Exp $)
7 ##############################################################################
8 # Copyright 2018,2020 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
72 # introduces spurious #line markers into the preprocessor output. The result
73 # appears in gcc 5.0 and (with modification) in 5.1, making it necessary to
74 # determine if we are using gcc, and if so, what version because the proposed
75 # solution uses a nonstandard option.
76 PRG
=`echo "$1" | $AWK '{ sub(/^[ ]*/,""); sub(/[ ].*$/, ""); print; }' || exit 0`
77 FSF
=`"$PRG" --version 2>/dev/null || exit 0 | fgrep "Free Software Foundation" | head -n 1`
78 ALL
=`"$PRG" -dumpversion 2>/dev/null || exit 0`
79 ONE
=`echo "$ALL" | sed -e 's/\..*$//'`
80 if test -n "$FSF" && test -n "$ALL" && test -n "$ONE" ; then
81 if test $ONE -ge 5 ; then
82 echo ".. adding -P option to work around $PRG $ALL" >&2
83 preprocessor
="$preprocessor -P"
95 trap "rm -f $ED1 $ED2 $ED3 $ED4 $AW1 $AW2 $TMP" 0 1 2 3 15
98 if test "$USE" = implemented
; then
101 /^extern.*implemented/{
103 s/GCC_DEPRECATED([^)]*)//
104 s/NCURSES_SP_NAME(\([^)]*\))/NCURSES_SP_NAME___\1/
106 s/^.*implemented:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
108 s/^extern \([^;]*\);.*/\1/p
110 s/^.*implemented:\([^ *]*\).*/P_POUNDCendif/p
112 /^extern.*generated/{
114 s/^.*generated:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
116 s/^extern \([^;]*\);.*/\1/p
118 s/^.*generated:\([^ *]*\).*/P_POUNDCendif/p
126 s/^.*${ALL}:\([^ *]*\).*/P_POUNDCif_USE_\1_SUPPORT/p
128 s/^extern \([^;]*\);.*/\1/p
130 s/^.*${ALL}:\([^ *]*\).*/P_POUNDCendif/p
165 s/^P_POUNDCendif/#endif/
179 if test "$USE" = generated
; then
181 s/^\(.*\) \(.*\) (\(.*\))\$/NCURSES_EXPORT(\1) (\2) (\3)/
182 /attr_[sg]et.* z)/s,z),z GCC_UNUSED),
186 /^\(.*\) \(.*\) (\(.*\))\$/ {
188 s/^\(.*\) \(.*\) (\(.*\))\$/extern \1 call_\2 (\3);/
191 s/^\(.*\) \(.*\) (\(.*\))\$/\1 call_\2 (\3)/
193 s/\([^_]\)NCURSES_SP_NAME___\([a-zA-Z][a-zA-Z_]*\)/\1NCURSES_SP_NAME(\2)/g
216 for (i
= 1; i
<= NF
; i
++) {
217 if ( $i != "NCURSES_CONST" ) {
224 if ( $first == "chtype" ) {
225 returnType
= "Chtype";
226 } else if ( $first == "SCREEN" ) {
228 } else if ( $first == "WINDOW" ) {
230 } else if ( $first == "attr_t" ||
$second == "attrset" ||
$second == "standout" ||
$second == "standend" ||
$second == "wattrset" ||
$second == "wstandout" ||
$second == "wstandend" ) {
231 returnType
= "IntAttr";
232 returnCast
= "(attr_t)";
233 } else if ( $first == "bool" ||
$first == "NCURSES_BOOL" ) {
235 } else if ( $second == "*" ) {
236 returnType
= ($1 == "NCURSES_CONST") ?
"CPtr" : "Ptr";
241 for (i
= second
; i
<= NF
; i
++) {
247 if (using
== "implemented") {
248 printf "#undef %s\n", $myfunc;
254 if ($check == "void")
257 for (i
= 1; i
<= NF
; i
++)
262 # suppress trace-code for functions that we cannot do properly here,
263 # since they return data.
265 if ($myfunc ~
/innstr
/)
267 if ($myfunc ~
/innwstr
/)
270 # workaround functions that we do not parse properly
271 if ($myfunc ~
/ripoffline
/) {
274 if ($myfunc ~
/NCURSES_SP_NAME
/) {
278 if ($myfunc ~
/wunctrl
/) {
283 if ($myfunc ~
/get
[n
]?str
/) {
287 call
= "@@T((T_CALLED(\""
295 for (i
= myfunc
; i
<= NF
; i
++) {
299 } else if ( ch
== "va_list" ) {
301 } else if ( ch
== "..." ) {
303 } else if ( ch
== "char" ) {
305 } else if ( ch
== "int" ) {
307 } else if ( ch
== "short" ) {
309 } else if ( ch
== "chtype" ) {
311 } else if ( ch
== "attr_t" || ch
== "NCURSES_ATTR_T" ) {
315 if ( ch
== "," || ch
== ")" ) {
319 } else if (varargs
) {
321 } else if (pointer
) {
322 if ( argtype
== "char" ) {
328 comma
= comma
"_nc_visbuf2(" num
","
332 comma
= comma
"(const void *)"
334 } else if (argcount
!= 0) {
335 if ( argtype
== "int" || argtype
== "short" ) {
338 } else if ( argtype
!= "" ) {
340 comma
= comma
"_trace" argtype
"2(" num
","
341 if (argtype
== "attr") {
342 argcast
= "(chtype)";
346 comma
= comma
"(long)"
350 args
= args comma
"a" ++num
;
351 } else if ( argcount
!= 0 ) {
353 args
= args comma
"\"va_list\""
354 } else if ( varargs
) {
355 args
= args comma
"\"...\""
357 args
= args comma argcast
"z"
361 if (pointer
== 0 && argcount
!= 0 && argtype
!= "" )
368 if ( i
== myfunc || ch
== "(" )
373 call
= call
", " args
377 printf "%s\n\t@@", call
379 if (match
($0, "^void")) {
381 } else if (dotrace
) {
382 call
= sprintf
("return%s( ", returnType
);
383 if (returnCast
!= "") {
384 call
= call returnCast
;
390 call
= call
$myfunc "(";
391 for (i
= 1; i
< argcount
; i
++) {
396 if ( argcount
!= 0 && $check != "..." ) {
401 if (!match
($0, "^void"))
408 if (match
($0, "^void"))
409 print
"@@returnVoid;"
416 printf "/* This file was generated by $0 $USE */\n"
419 print " * DO NOT EDIT THIS FILE BY HAND!"
420 if ( "$USE" == "generated" ) {
422 print " * This is a file of trivial functions generated from macro"
423 print " * definitions in curses.h to satisfy the XSI Curses requirement"
424 print " * that every macro also exist as a callable function."
426 print " * It will never be linked unless you call one of the entry"
427 print " * points with its normal macro definition disabled. In that"
428 print " * case, if you have no shared libraries, it will indirectly"
429 print " * pull most of the rest of the library into your link image."
432 print "#define NCURSES_ATTR_T int"
433 print "#include <ncurses_cfg.h>"
435 print "#undef NCURSES_NOMACROS /* _this_ file uses macros */"
437 print "#include <curses.priv.h>"
440 /^DECLARATIONS/ {start = 1; next;}
443 if ( "$USE" == "generated" ) {
445 } else if ( \$0 ~ /^[{}]?\$/ ) {
447 } else if ( \$0 ~ /;/ ) {
457 if ( "$USE" != "generated" ) {
458 print "int main(void)"
460 for (n = 1; n < start; ++n) {
462 if ( value !~ /P_POUNDC/ ) {
463 gsub(/[ \t]+/," ",value);
464 sub(/^[0-9a-zA-Z_]+ /,"",value);
465 sub(/^\* /,"",value);
466 gsub(/[0-9a-zA-Z_]+ \* /,"",value);
467 gsub(/ (const) /," ",value);
468 gsub(/ (int|short|attr_t|chtype|wchar_t|NCURSES_BOOL|NCURSES_OUTC|NCURSES_OUTC_sp|va_list) /," ",value);
469 gsub(/ void /,"",value);
470 sub(/^/,"call_",value);
471 gsub(/ (a[0-9]|z) /, " 0 ", value);
472 gsub(/ int[ \t]*[(][^)]+[)][(][^)]+[)]/, "0", value);
473 printf "\t%s;\n", value;
485 #include <ncurses_cfg.h>
486 #undef NCURSES_NOMACROS
496 |
sed -e 's/NCURSES_EXPORT(\(.*\)) \(.*\) (\(.*\))/\1 \2(\3)/' \
498 |
$AWK -f $AW1 using
=$USE \
501 -e 's/^\([a-zA-Z_][a-zA-Z_]*[ *]*\)/\1 gen_/' \
509 s/P_NCURSES_BOOL/NCURSES_BOOL/g
512 # A patch discussed here:
513 # https://gcc.gnu.org/ml/gcc-patches/2014-06/msg02185.html
514 # introduces spurious #line markers. Work around that by ignoring the system's
515 # attempt to define "bool" and using our own symbol here.
516 sed -e 's/bool/P_NCURSES_BOOL/g' $TMP > $ED2
519 $preprocessor $TMP 2>/dev
/null \
524 -e 's/^.*T_CALLED.*returnCode( \([a-z].*) \));/ return \1;/' \
525 -e 's/^.*T_CALLED.*returnCode( \((wmove.*) \));/ return \1;/' \