aboutsummaryrefslogtreecommitdiff
path: root/console-tools/loadacm.c
diff options
context:
space:
mode:
Diffstat (limited to 'console-tools/loadacm.c')
-rw-r--r--console-tools/loadacm.c354
1 files changed, 0 insertions, 354 deletions
diff --git a/console-tools/loadacm.c b/console-tools/loadacm.c
deleted file mode 100644
index edaf51afb..000000000
--- a/console-tools/loadacm.c
+++ /dev/null
@@ -1,354 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Derived from
4 * mapscrn.c - version 0.92
5 *
6 * Was taken from console-tools and adapted by
7 * Peter Novodvorsky <petya@logic.ru>
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <memory.h>
13#include <string.h>
14#include <unistd.h>
15#include <fcntl.h>
16#include <assert.h>
17#include <errno.h>
18#include <signal.h>
19#include <sys/kd.h>
20#include <sys/types.h>
21#include <sys/ioctl.h>
22#include "busybox.h"
23
24typedef unsigned short unicode;
25
26static long int ctoi(unsigned char *s, int *is_unicode);
27static int old_screen_map_read_ascii(FILE * fp, unsigned char buf[]);
28static int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode);
29static unicode utf8_to_ucs2(char *buf);
30static int screen_map_load(int fd, FILE * fp);
31
32int loadacm_main(int argc, char **argv)
33{
34 int fd;
35
36 if (argc>=2 && *argv[1]=='-') {
37 bb_show_usage();
38 }
39
40 fd = bb_xopen(CURRENT_VC, O_RDWR);
41
42 if (screen_map_load(fd, stdin)) {
43 bb_perror_msg_and_die("Error loading acm");
44 }
45
46 write(fd, "\033(K", 3);
47
48 return EXIT_SUCCESS;
49}
50
51static int screen_map_load(int fd, FILE * fp)
52{
53 struct stat stbuf;
54 unicode wbuf[E_TABSZ];
55 unsigned char buf[E_TABSZ];
56 int parse_failed = 0;
57 int is_unicode;
58
59 if (fstat(fileno(fp), &stbuf))
60 bb_perror_msg_and_die("Cannot stat map file");
61
62 /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */
63 if (!
64 (parse_failed =
65 (-1 == uni_screen_map_read_ascii(fp, wbuf, &is_unicode)))
66|| (S_ISREG(stbuf.st_mode) && (stbuf.st_size == (sizeof(unicode) * E_TABSZ)))) { /* test for binary UTF map by size */
67 if (parse_failed) {
68 if (-1 == fseek(fp, 0, SEEK_SET)) {
69 if (errno == ESPIPE)
70 bb_error_msg_and_die("16bit screen-map MUST be a regular file.");
71 else
72 bb_perror_msg_and_die("fseek failed reading binary 16bit screen-map");
73 }
74
75 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1)
76 bb_perror_msg_and_die("Cannot read [new] map from file");
77#if 0
78 else
79 bb_error_msg("Input screen-map is binary.");
80#endif
81 }
82
83 /* if it was effectively a 16-bit ASCII, OK, else try to read as 8-bit map */
84 /* same if it was binary, ie. if parse_failed */
85 if (parse_failed || is_unicode) {
86 if (ioctl(fd, PIO_UNISCRNMAP, wbuf))
87 bb_perror_msg_and_die("PIO_UNISCRNMAP ioctl");
88 else
89 return 0;
90 }
91 }
92
93 /* rewind... */
94 if (-1 == fseek(fp, 0, SEEK_SET)) {
95 if (errno == ESPIPE)
96 bb_error_msg("Assuming 8bit screen-map - MUST be a regular file."),
97 exit(1);
98 else
99 bb_perror_msg_and_die("fseek failed assuming 8bit screen-map");
100 }
101
102 /* ... and try an old 8-bit screen-map */
103 if (!(parse_failed = (-1 == old_screen_map_read_ascii(fp, buf))) ||
104 (S_ISREG(stbuf.st_mode) && (stbuf.st_size == E_TABSZ))) { /* test for binary old 8-bit map by size */
105 if (parse_failed) {
106 if (-1 == fseek(fp, 0, SEEK_SET)) {
107 if (errno == ESPIPE)
108 /* should not - it succedeed above */
109 bb_error_msg_and_die("fseek() returned ESPIPE !");
110 else
111 bb_perror_msg_and_die("fseek for binary 8bit screen-map");
112 }
113
114 if (fread(buf, E_TABSZ, 1, fp) != 1)
115 bb_perror_msg_and_die("Cannot read [old] map from file");
116#if 0
117 else
118 bb_error_msg("Input screen-map is binary.");
119#endif
120 }
121
122 if (ioctl(fd, PIO_SCRNMAP, buf))
123 bb_perror_msg_and_die("PIO_SCRNMAP ioctl");
124 else
125 return 0;
126 }
127 bb_error_msg("Error parsing symbolic map");
128 return(1);
129}
130
131
132/*
133 * - reads `fp' as a 16-bit ASCII SFM file.
134 * - returns -1 on error.
135 * - returns it in `unicode' in an E_TABSZ-elements array.
136 * - sets `*is_unicode' flagiff there were any non-8-bit
137 * (ie. real 16-bit) mapping.
138 *
139 * FIXME: ignores everything after second word
140 */
141static int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
142{
143 char buffer[256]; /* line buffer reading file */
144 char *p, *q; /* 1st + 2nd words in line */
145 int in, on; /* the same, as numbers */
146 int tmp_is_unicode; /* tmp for is_unicode calculation */
147 int i; /* loop index - result holder */
148 int ret_code = 0; /* return code */
149 sigset_t acmsigset, old_sigset;
150
151 assert(is_unicode);
152
153 *is_unicode = 0;
154
155 /* first 128 codes defaults to ASCII */
156 for (i = 0; i < 128; i++)
157 buf[i] = i;
158 /* remaining defaults to replacement char (usually E_TABSZ = 256) */
159 for (; i < E_TABSZ; i++)
160 buf[i] = 0xfffd;
161
162 /* block SIGCHLD */
163 sigemptyset(&acmsigset);
164 sigaddset(&acmsigset, SIGCHLD);
165 sigprocmask(SIG_BLOCK, &acmsigset, &old_sigset);
166
167 do {
168 if (NULL == fgets(buffer, sizeof(buffer), fp)) {
169 if (feof(fp))
170 break;
171 else
172 bb_perror_msg_and_die("uni_screen_map_read_ascii() can't read line");
173 }
174
175 /* get "charset-relative charcode", stripping leading spaces */
176 p = strtok(buffer, " \t\n");
177
178 /* skip empty lines and comments */
179 if (!p || *p == '#')
180 continue;
181
182 /* get unicode mapping */
183 q = strtok(NULL, " \t\n");
184 if (q) {
185 in = ctoi(p, NULL);
186 if (in < 0 || in > 255) {
187 ret_code = -1;
188 break;
189 }
190
191 on = ctoi(q, &tmp_is_unicode);
192 if (in < 0 && on > 65535) {
193 ret_code = -1;
194 break;
195 }
196
197 *is_unicode |= tmp_is_unicode;
198 buf[in] = on;
199 } else {
200 ret_code = -1;
201 break;
202 }
203 }
204 while (1); /* terminated by break on feof() */
205
206 /* restore sig mask */
207 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
208
209 return ret_code;
210}
211
212
213static int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
214{
215 char buffer[256];
216 int in, on;
217 char *p, *q;
218
219 for (in = 0; in < 256; in++)
220 buf[in] = in;
221
222 while (fgets(buffer, sizeof(buffer) - 1, fp)) {
223 p = strtok(buffer, " \t\n");
224
225 if (!p || *p == '#')
226 continue;
227
228 q = strtok(NULL, " \t\n#");
229 if (q) {
230 in = ctoi(p, NULL);
231 if (in < 0 || in > 255)
232 return -1;
233
234 on = ctoi(q, NULL);
235 if (in < 0 && on > 255)
236 return -1;
237
238 buf[in] = on;
239 } else
240 return -1;
241 }
242
243 return (0);
244}
245
246
247/*
248 * - converts a string into an int.
249 * - supports dec and hex bytes, hex UCS2, single-quoted byte and UTF8 chars.
250 * - returns the converted value
251 * - if `is_unicode != NULL', use it to tell whether it was unicode
252 *
253 * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones.
254 */
255static long int ctoi(unsigned char *s, int *is_unicode)
256{
257 int i;
258 size_t ls;
259
260 ls = strlen(s);
261 if (is_unicode)
262 *is_unicode = 0;
263
264 /* hex-specified UCS2 */
265 if ((strncmp(s, "U+", 2) == 0) &&
266 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) {
267 sscanf(s + 2, "%x", &i);
268 if (is_unicode)
269 *is_unicode = 1;
270 }
271
272 /* hex-specified byte */
273 else if ((ls <= 4) && (strncmp(s, "0x", 2) == 0) &&
274 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2))
275 sscanf(s + 2, "%x", &i);
276
277 /* oct-specified number (byte) */
278 else if ((*s == '0') && (strspn(s, "01234567") == ls))
279 sscanf(s, "%o", &i);
280
281 /* dec-specified number (byte) */
282 else if (strspn(s, "0123456789") == ls)
283 sscanf(s, "%d", &i);
284
285 /* single-byte quoted char */
286 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
287 i = s[1];
288
289 /* multi-byte UTF8 quoted char */
290 else if ((s[0] == '\'') && (s[ls - 1] == '\'')) {
291 s[ls - 1] = 0; /* ensure we'll not "parse UTF too far" */
292 i = utf8_to_ucs2(s + 1);
293 if (is_unicode)
294 *is_unicode = 1;
295 } else
296 return (-1);
297
298 return (i);
299}
300
301
302static unicode utf8_to_ucs2(char *buf)
303{
304 int utf_count = 0;
305 long utf_char = 0;
306 unicode tc = 0;
307 unsigned char c;
308
309 do {
310 c = *buf;
311 buf++;
312
313 /* if byte should be part of multi-byte sequence */
314 if (c & 0x80) {
315 /* if we have already started to parse a UTF8 sequence */
316 if (utf_count > 0 && (c & 0xc0) == 0x80) {
317 utf_char = (utf_char << 6) | (c & 0x3f);
318 utf_count--;
319 if (utf_count == 0)
320 tc = utf_char;
321 else
322 continue;
323 } else { /* Possibly 1st char of a UTF8 sequence */
324
325 if ((c & 0xe0) == 0xc0) {
326 utf_count = 1;
327 utf_char = (c & 0x1f);
328 } else if ((c & 0xf0) == 0xe0) {
329 utf_count = 2;
330 utf_char = (c & 0x0f);
331 } else if ((c & 0xf8) == 0xf0) {
332 utf_count = 3;
333 utf_char = (c & 0x07);
334 } else if ((c & 0xfc) == 0xf8) {
335 utf_count = 4;
336 utf_char = (c & 0x03);
337 } else if ((c & 0xfe) == 0xfc) {
338 utf_count = 5;
339 utf_char = (c & 0x01);
340 } else
341 utf_count = 0;
342 continue;
343 }
344 } else { /* not part of multi-byte sequence - treat as ASCII
345 * this makes incomplete sequences to be ignored
346 */
347 tc = c;
348 utf_count = 0;
349 }
350 }
351 while (utf_count);
352
353 return tc;
354}