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