xref: /openbsd/lib/libcurses/base/MKunctrl.awk (revision a6445c1d)
1# $OpenBSD: MKunctrl.awk,v 1.4 2010/01/12 23:22:05 nicm Exp $
2# $Id: MKunctrl.awk,v 1.4 2010/01/12 23:22:05 nicm Exp $
3##############################################################################
4# Copyright (c) 1998-2007,2008 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 "#if USE_WIDEC_SUPPORT"
41		print "#if HAVE_WCTYPE_H"
42		print "#include <wctype.h>"
43		print "#endif"
44		print "#endif"
45		print ""
46		print "#undef unctrl"
47		print ""
48	}
49END	{
50		print "NCURSES_EXPORT(NCURSES_CONST char *) _nc_unctrl (SCREEN *sp, chtype ch)"
51		print "{"
52
53		blob=""
54		offset=0
55		if (bigstrings) {
56			printf "static const short unctrl_table[] = {"
57		} else {
58			printf "static const char* const unctrl_table[] = {"
59		}
60		for ( ch = 0; ch < 256; ch++ ) {
61			gap = ","
62			part=""
63			if ((ch % 8) == 0) {
64				printf "\n    "
65				if (ch != 0)
66					blob = blob "\""
67				blob = blob "\n    \""
68			}
69			if (bigstrings)
70				printf "%4d%s", offset, gap;
71			if (ch < 32) {
72				part = sprintf ("^\\%03o", ch + 64);
73				offset = offset + 3;
74			} else if (ch == 127) {
75				part = "^?";
76				offset = offset + 3;
77			} else if (ch >= 128 && ch < 160) {
78				part = sprintf("~\\%03o", ch - 64);
79				offset = offset + 3;
80			} else if (ch == 255) {
81				part = "~?";
82				offset = offset + 3;
83			} else if (ch >= 160) {
84				part = sprintf("M-\\%03o", ch - 128);
85				offset = offset + 4;
86			} else {
87				gap = gap " "
88				part = sprintf("\\%03o", ch);
89				offset = offset + 2;
90			}
91			if (ch == 255)
92				gap = "\n"
93			else if (((ch + 1) % 8) != 0)
94				gap = gap " "
95			if (bigstrings) {
96				blob = blob part "\\0";
97			} else {
98				printf "\"%s\"%s", part, gap
99			}
100		}
101		print "};"
102		blob = blob "\"";
103
104		print ""
105		printf "#if NCURSES_EXT_FUNCS\n";
106		if (bigstrings) {
107			blob = blob "\n/* printable values in 128-255 range */"
108			printf "static const short unctrl_c1[] = {"
109		} else {
110			printf "static const char* const unctrl_c1[] = {"
111		}
112		for ( ch = 128; ch < 256; ch++ ) {
113			gap = ","
114			if ((ch % 8) == 0) {
115				if (ch != 128)
116					blob = blob "\""
117				printf "\n    "
118				blob = blob "\n    \""
119			}
120			if (bigstrings) {
121				printf "%4d%s", offset, gap;
122				part = sprintf("\\%03o\\0", ch);
123				blob = blob part
124				offset = offset + 2;
125				if (((ch + 1) % 8) != 0)
126					gap = gap " "
127			} else {
128				if (ch >= 128) {
129					printf "\"\\%03o\"", ch
130					gap = gap " "
131				}
132				if (ch == 255)
133					gap = "\n"
134				else if (((ch + 1) % 8) != 0)
135					gap = gap " "
136				printf "%s", gap
137			}
138		}
139		print "};"
140		print "#endif /* NCURSES_EXT_FUNCS */"
141		blob = blob "\"\n"
142
143		print ""
144		if (bigstrings) {
145			print "static const char unctrl_blob[] = "blob";"
146			print ""
147			stringname = "unctrl_blob + unctrl"
148		} else {
149			stringname = "unctrl"
150		}
151		print  "\tint check = ChCharOf(ch);"
152		print  "\tconst char *result;"
153		print  ""
154		print  "\tif (check >= 0 && check < (int)SIZEOF(unctrl_table)) {"
155		print  "#if NCURSES_EXT_FUNCS"
156		print  "\t\tif ((sp != 0)"
157		print  "\t\t && (sp->_legacy_coding > 1)"
158		print  "\t\t && (check >= 128)"
159		print  "\t\t && (check < 160))"
160		printf "\t\t\tresult = %s_c1[check - 128];\n", stringname;
161		print  "\t\telse"
162		print  "#if USE_WIDEC_SUPPORT"
163		print  "\t\tif ((check >= 160)"
164		print  "\t\t && (check < 256)"
165		print  "\t\t && ((sp != 0)"
166		print  "\t\t  && ((sp->_legacy_coding > 0)"
167		print  "\t\t   || (sp->_legacy_coding == 0"
168		print  "\t\t       && (isprint(check) || iswprint(check))))))"
169		printf "\t\t\tresult = %s_c1[check - 128];\n", stringname;
170		print  "\t\telse"
171		print  "#else"
172		print  "\t\tif ((check >= 160)"
173		print  "\t\t && (check < 256)"
174		print  "\t\t && ((sp != 0)"
175		print  "\t\t  && ((sp->_legacy_coding > 0)"
176		print  "\t\t   || (sp->_legacy_coding == 0"
177		print  "\t\t       && isprint(check)))))"
178		printf "\t\t\tresult = %s_c1[check - 128];\n", stringname;
179		print  "\t\telse"
180		print  "#endif /* USE_WIDEC_SUPPORT */"
181		print  "#endif /* NCURSES_EXT_FUNCS */"
182		printf "\t\t\tresult = %s_table[check];\n", stringname;
183		print  "\t} else {"
184		print  "\t\tresult = 0;"
185		print  "\t}"
186		print  "\treturn (NCURSES_CONST char *)result;"
187		print  "}"
188		print  ""
189		print  "NCURSES_EXPORT(NCURSES_CONST char *) unctrl (chtype ch)"
190		print  "{"
191		print  "\treturn _nc_unctrl(SP, ch);"
192		print  "}"
193	}
194