MKunctrl.awk revision 176188
1# $Id: MKunctrl.awk,v 1.21 2008/02/03 20:24:30 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 *) unctrl (register 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		if (bigstrings) {
105			blob = blob "\n/* printable values in 128-255 range */"
106			printf "static const short unctrl_c1[] = {"
107		} else {
108			printf "static const char* const unctrl_c1[] = {"
109		}
110		for ( ch = 128; ch < 256; ch++ ) {
111			gap = ","
112			if ((ch % 8) == 0) {
113				if (ch != 128)
114					blob = blob "\""
115				printf "\n    "
116				blob = blob "\n    \""
117			}
118			if (bigstrings) {
119				printf "%4d%s", offset, gap;
120				part = sprintf("\\%03o\\0", ch);
121				blob = blob part
122				offset = offset + 2;
123				if (((ch + 1) % 8) != 0)
124					gap = gap " "
125			} else {
126				if (ch >= 128) {
127					printf "\"\\%03o\"", ch
128					gap = gap " "
129				}
130				if (ch == 255)
131					gap = "\n"
132				else if (((ch + 1) % 8) != 0)
133					gap = gap " "
134				printf "%s", gap
135			}
136		}
137		print "};"
138		blob = blob "\"\n"
139
140		print ""
141		if (bigstrings) {
142			print "static const char unctrl_blob[] = "blob";"
143			print ""
144			stringname = "unctrl_blob + unctrl"
145		} else {
146			stringname = "unctrl"
147		}
148		print  "\tint check = ChCharOf(ch);"
149		print  "\tconst char *result;"
150		print  ""
151		print  "\tif (check >= 0 && check < (int)SIZEOF(unctrl_table)) {"
152		print  "#if NCURSES_EXT_FUNCS"
153		print  "\t\tif ((SP != 0)"
154		print  "\t\t && (SP->_legacy_coding > 1)"
155		print  "\t\t && (check >= 128)"
156		print  "\t\t && (check < 160))"
157		printf "\t\t\tresult = %s_c1[check - 128];\n", stringname;
158		print  "\t\telse"
159		print  "#if USE_WIDEC_SUPPORT"
160		print  "\t\tif ((check >= 160)"
161		print  "\t\t && (check < 256)"
162		print  "\t\t && ((SP != 0)"
163		print  "\t\t  && ((SP->_legacy_coding > 0)"
164		print  "\t\t   || (SP->_legacy_coding == 0"
165		print  "\t\t       && (isprint(check) || iswprint(check))))))"
166		printf "\t\t\tresult = %s_c1[check - 128];\n", stringname;
167		print  "\t\telse"
168		print  "#else"
169		print  "\t\tif ((check >= 160)"
170		print  "\t\t && (check < 256)"
171		print  "\t\t && ((SP != 0)"
172		print  "\t\t  && ((SP->_legacy_coding > 0)"
173		print  "\t\t   || (SP->_legacy_coding == 0"
174		print  "\t\t       && isprint(check)))))"
175		printf "\t\t\tresult = %s_c1[check - 128];\n", stringname;
176		print  "\t\telse"
177		print  "#endif /* USE_WIDEC_SUPPORT */"
178		print  "#endif /* NCURSES_EXT_FUNCS */"
179		printf "\t\t\tresult = %s_table[check];\n", stringname;
180		print  "\t} else {"
181		print  "\t\tresult = 0;"
182		print  "\t}"
183		print  "\treturn (NCURSES_CONST char *)result;"
184		print  "}"
185	}
186