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