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