xref: /openbsd/lib/libcurses/base/MKunctrl.awk (revision c7ef0cfc)
1# $OpenBSD: MKunctrl.awk,v 1.5 2023/10/17 09:52:08 nicm Exp $
2# $Id: MKunctrl.awk,v 1.5 2023/10/17 09:52:08 nicm Exp $
3##############################################################################
4# Copyright 2020 Thomas E. Dickey                                            #
5# Copyright 1998-2012,2017 Free Software Foundation, Inc.                    #
6#                                                                            #
7# Permission is hereby granted, free of charge, to any person obtaining a    #
8# copy of this software and associated documentation files (the "Software"), #
9# to deal in the Software without restriction, including without limitation  #
10# the rights to use, copy, modify, merge, publish, distribute, distribute    #
11# with modifications, sublicense, and/or sell copies of the Software, and to #
12# permit persons to whom the Software is furnished to do so, subject to the  #
13# following conditions:                                                      #
14#                                                                            #
15# The above copyright notice and this permission notice shall be included in #
16# all copies or substantial portions of the Software.                        #
17#                                                                            #
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
21# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
24# DEALINGS IN THE SOFTWARE.                                                  #
25#                                                                            #
26# Except as contained in this notice, the name(s) of the above copyright     #
27# holders shall not be used in advertising or otherwise to promote the sale, #
28# use or other dealings in this Software without prior written               #
29# authorization.                                                             #
30##############################################################################
31#
32# Author: Thomas E. Dickey (1997-on)
33#
34
35BEGIN	{
36		print "/* generated by MKunctrl.awk */"
37		print ""
38		print "#include <curses.priv.h>"
39		print "#include <ctype.h>"
40		print ""
41		print "#undef unctrl"
42		print ""
43	}
44END	{
45		print "NCURSES_EXPORT(NCURSES_CONST char *) safe_unctrl(SCREEN *sp, chtype ch)"
46		print "{"
47		blob=""
48		offset=0
49		if (bigstrings) {
50			printf "static const short unctrl_table[] = {"
51		} else {
52			printf "static const char* const unctrl_table[] = {"
53		}
54		for ( ch = 0; ch < 256; ch++ ) {
55			gap = ","
56			part=""
57			if ((ch % 8) == 0) {
58				printf "\n    "
59				if (ch != 0)
60					blob = blob "\""
61				blob = blob "\n    \""
62			}
63			if (bigstrings)
64				printf "%4d%s", offset, gap;
65			if (ch < 32) {
66				part = sprintf ("^\\%03o", ch + 64);
67				offset = offset + 3;
68			} else if (ch == 127) {
69				part = "^?";
70				offset = offset + 3;
71			} else if (ch >= 128 && ch < 160) {
72				part = sprintf("~\\%03o", ch - 64);
73				offset = offset + 3;
74			} else if (ch == 255) {
75				part = "~?";
76				offset = offset + 3;
77			} else if (ch >= 160) {
78				part = sprintf("M-\\%03o", ch - 128);
79				offset = offset + 4;
80			} else {
81				gap = gap " "
82				part = sprintf("\\%03o", ch);
83				offset = offset + 2;
84			}
85			if (ch == 255)
86				gap = "\n"
87			else if (((ch + 1) % 8) != 0)
88				gap = gap " "
89			if (bigstrings) {
90				blob = blob part "\\0";
91			} else {
92				printf "\"%s\"%s", part, gap
93			}
94		}
95		print "};"
96		blob = blob "\"";
97
98		print ""
99		printf "#if NCURSES_EXT_FUNCS\n";
100		if (bigstrings) {
101			blob = blob "\n/* printable values in 128-255 range */"
102			printf "static const short unctrl_c1[] = {"
103		} else {
104			printf "static const char* const unctrl_c1[] = {"
105		}
106		for ( ch = 128; ch < 256; ch++ ) {
107			gap = ","
108			if ((ch % 8) == 0) {
109				if (ch != 128)
110					blob = blob "\""
111				printf "\n    "
112				blob = blob "\n    \""
113			}
114			if (bigstrings) {
115				printf "%4d%s", offset, gap;
116				part = sprintf("\\%03o\\0", ch);
117				blob = blob part
118				offset = offset + 2;
119				if (((ch + 1) % 8) != 0)
120					gap = gap " "
121			} else {
122				if (ch >= 128) {
123					printf "\"\\%03o\"", ch
124					gap = gap " "
125				}
126				if (ch == 255)
127					gap = "\n"
128				else if (((ch + 1) % 8) != 0)
129					gap = gap " "
130				printf "%s", gap
131			}
132		}
133		print "};"
134		print "#endif /* NCURSES_EXT_FUNCS */"
135		blob = blob "\"\n"
136
137		print ""
138		if (bigstrings) {
139			print "static const char unctrl_blob[] = "blob";"
140			print ""
141			stringname = "unctrl_blob + unctrl"
142		} else {
143			stringname = "unctrl"
144		}
145		print  "\tint check = (int) ChCharOf(ch);"
146		print  "\tconst char *result;"
147		print  ""
148		print  "(void) sp;"
149		print  "\tif (check >= 0 && check < (int)SIZEOF(unctrl_table)) {"
150		print  "#if NCURSES_EXT_FUNCS"
151		print  "\t\tif ((sp != 0)"
152		print  "\t\t && (sp->_legacy_coding > 1)"
153		print  "\t\t && (check >= 128)"
154		print  "\t\t && (check < 160))"
155		printf "\t\t\tresult = %s_c1[check - 128];\n", stringname;
156		print  "\t\telse"
157		print  "\t\tif ((check >= 160)"
158		print  "\t\t && (check < 256)"
159		print  "\t\t && ((sp != 0)"
160		print  "\t\t  && ((sp->_legacy_coding > 0)"
161		print  "\t\t   || (sp->_legacy_coding == 0"
162		print  "\t\t       && isprint(check)))))"
163		printf "\t\t\tresult = %s_c1[check - 128];\n", stringname;
164		print  "\t\telse"
165		print  "#endif /* NCURSES_EXT_FUNCS */"
166		printf "\t\t\tresult = %s_table[check];\n", stringname;
167		print  "\t} else {"
168		print  "\t\tresult = 0;"
169		print  "\t}"
170		print  "\treturn (NCURSES_CONST char *)result;"
171		print  "}"
172		print  ""
173		print  "NCURSES_EXPORT(NCURSES_CONST char *) unctrl (chtype ch)"
174		print  "{"
175		print  "\treturn safe_unctrl(CURRENT_SCREEN, ch);"
176		print  "}"
177	}
178