1 /* @(#)ctab.c	1.15 17/10/03 Copyright 1987-2017 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)ctab.c	1.15 17/10/03 Copyright 1987-2017 J. Schilling";
6 #endif
7 /*
8  *	Character expansion table
9  *
10  *	Copyright (c) 1987-2017 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #include <schily/stdio.h>
27 #include <schily/standard.h>
28 #include <schily/stdlib.h>
29 #include <schily/string.h>
30 #include <schily/schily.h>
31 
32 #define	STATIC_TABLE
33 #ifdef	STATIC_TABLE
34 unsigned char csize[256] = {
35 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
36 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
37 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
38 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
39 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
43 
44 	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
45 	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
46 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
47 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
48 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
49 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
50 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
51 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
52 };
53 
54 /* unsigned */ char *ctab[256] = {
55 	"^@",	"^A",	"^B",	"^C",	"^D",	"^E",	"^F",	"^G",
56 	"^H",	"^I",	"^J",	"^K",	"^L",	"^M",	"^N",	"^O",
57 	"^P",	"^Q",	"^R",	"^S",	"^T",	"^U",	"^V",	"^W",
58 	"^X",	"^Y",	"^Z",	"^[",	"^\\",	"^]",	"^^",	"^_",
59 	" ",	"!",	"\"",	"#",	"$",	"%",	"&",	"'",
60 	"(",	")",	"*",	"+",	",",	"-",	".",	"/",
61 	"0",	"1",	"2",	"3",	"4",	"5",	"6",	"7",
62 	"8",	"9",	":",	";",	"<",	"=",	">",	"?",
63 	"@",	"A",	"B",	"C",	"D",	"E",	"F",	"G",
64 	"H",	"I",	"J",	"K",	"L",	"M",	"N",	"O",
65 	"P",	"Q",	"R",	"S",	"T",	"U",	"V",	"W",
66 	"X",	"Y",	"Z",	"[",	"\\",	"]",	"^",	"_",
67 	"`",	"a",	"b",	"c",	"d",	"e",	"f",	"g",
68 	"h",	"i",	"j",	"k",	"l",	"m",	"n",	"o",
69 	"p",	"q",	"r",	"s",	"t",	"u",	"v",	"w",
70 	"x",	"y",	"z",	"{",	"|",	"}",	"~",	"^?",
71 
72 	"~^@",	"~^A",	"~^B",	"~^C",	"~^D",	"~^E",	"~^F",	"~^G",
73 	"~^H",	"~^I",	"~^J",	"~^K",	"~^L",	"~^M",	"~^N",	"~^O",
74 	"~^P",	"~^Q",	"~^R",	"~^S",	"~^T",	"~^U",	"~^V",	"~^W",
75 	"~^X",	"~^Y",	"~^Z",	"~^[",	"~^\\",	"~^]",	"~^^",	"~^_",
76 	"~ ",	"~!",	"~\"",	"~#",	"~$",	"~%",	"~&",	"~'",
77 	"~(",	"~)",	"~*",	"~+",	"~,",	"~-",	"~.",	"~/",
78 	"~0",	"~1",	"~2",	"~3",	"~4",	"~5",	"~6",	"~7",
79 	"~8",	"~9",	"~:",	"~;",	"~<",	"~=",	"~>",	"~?",
80 	"~@",	"~A",	"~B",	"~C",	"~D",	"~E",	"~F",	"~G",
81 	"~H",	"~I",	"~J",	"~K",	"~L",	"~M",	"~N",	"~O",
82 	"~P",	"~Q",	"~R",	"~S",	"~T",	"~U",	"~V",	"~W",
83 	"~X",	"~Y",	"~Z",	"~[",	"~\\",	"~]",	"~^",	"~_",
84 	"~`",	"~a",	"~b",	"~c",	"~d",	"~e",	"~f",	"~g",
85 	"~h",	"~i",	"~j",	"~k",	"~l",	"~m",	"~n",	"~o",
86 	"~p",	"~q",	"~r",	"~s",	"~t",	"~u",	"~v",	"~w",
87 	"~x",	"~y",	"~z",	"~{",	"~|",	"~}",	"~~",	"~^?"
88 };
89 #else
90 unsigned char csize[256];
91 /* unsigned */ char *ctab[256];
92 #endif
93 
94 
95 LOCAL	unsigned char	*makestr	__PR((unsigned char *s));
96 EXPORT	void		init_charset	__PR((void));
97 LOCAL	void		init_csize	__PR((void));
98 LOCAL	void		init_ctab	__PR((void));
99 
100 LOCAL unsigned char *
makestr(s)101 makestr(s)
102 	register unsigned char *s;
103 {
104 		unsigned char *tmp;
105 	register unsigned char *s1;
106 
107 	if ((tmp = (unsigned char *)malloc(strlen((char *)s)+1)) == NULL) {
108 		raisecond("makstr", 0L);
109 	}
110 	for (s1 = tmp; (*s1++ = *s++) != '\0'; );
111 	return (tmp);
112 }
113 
114 extern	int	raw8;
115 
116 EXPORT void
init_charset()117 init_charset()
118 {
119 #ifdef	STATIC_TABLE
120 	if (getenv("CTAB") || raw8) {
121 		init_ctab();
122 		init_csize();
123 	}
124 #else
125 	init_ctab();
126 	init_csize();
127 #endif
128 }
129 
130 #define	SP	0x20
131 #define	DEL	0x7F
132 #define	SP8	0xA0
133 #define	DEL8	0xFF
134 
135 LOCAL void
init_csize()136 init_csize()
137 {
138 	register unsigned c;
139 	register unsigned char *rcsize = csize;
140 
141 	for (c = 0; c <= 255; c++, rcsize++) {
142 		if (c < SP || c == DEL)			  /* ctl char B0 */
143 			*rcsize = 2;
144 		else if ((c > DEL && c < SP8) || c == DEL8)  /* 8bit ctl B0 */
145 			*rcsize = 3;
146 		else if (c >= SP8 && !raw8)		  /* 8bit norm B0 */
147 			*rcsize = 2;
148 		else					  /* 7bit norm B0 */
149 			*rcsize = 1;
150 	}
151 }
152 
153 LOCAL void
init_ctab()154 init_ctab()
155 {
156 	register unsigned c;
157 	register unsigned char *p;
158 	register unsigned char **rctab = (unsigned char **)ctab;
159 	register unsigned char *ctl = (unsigned char *) "^ ";
160 	register unsigned char *eight = (unsigned char *) "~ ";
161 	register unsigned char *eightctl = (unsigned char *) "~^ ";
162 	register unsigned char *ch = (unsigned char *) " ";
163 
164 	for (c = 0; c <= 255; c++, rctab++) {
165 		if (c < SP || c == DEL) {			/* ctl char */
166 			p = makestr(ctl);
167 			p[1] = c ^ 0100;
168 		} else if ((c > DEL && c < SP8) || c == DEL8) {  /* 8 bit ctl */
169 			p = makestr(eightctl);
170 			p[2] = c ^ 0300;
171 		} else if (c >= SP8 && !raw8) {			/* 8 bit char */
172 			p = makestr(eight);
173 			p[1] = c & 0177;
174 		} else {					/* 7 bit char */
175 			p = makestr(ch);
176 			p[0] = c;
177 		}
178 		*rctab = p;
179 	}
180 }
181