blob: cca5406ee3a7fe4bac9934306599144bdb7a7016 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersena8991081999-12-29 03:34:00 +00002/*
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
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000010#include "internal.h"
Erik Andersena8991081999-12-29 03:34:00 +000011#include <stdio.h>
12#include <stdlib.h>
13#include <memory.h>
14#include <string.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <assert.h>
18#include <errno.h>
19#include <signal.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <sys/ioctl.h>
23#include <sys/kd.h>
24
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000025static const char loadacm_usage[] = "loadacm\n"
26#ifndef BB_FEATURE_TRIVIAL_HELP
27 "\nLoads an acm from standard input.\n"
28#endif
29 ;
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000030
Erik Andersena8991081999-12-29 03:34:00 +000031typedef unsigned short unicode;
32
33static long int ctoi(unsigned char *s, int *is_unicode);
Erik Andersene49d5ec2000-02-08 19:58:47 +000034int old_screen_map_read_ascii(FILE * fp, unsigned char buf[]);
35int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode);
36unicode utf8_to_ucs2(char *buf);
37int screen_map_load(int fd, FILE * fp);
Erik Andersena8991081999-12-29 03:34:00 +000038
39int loadacm_main(int argc, char **argv)
40{
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 int fd;
42
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000043 if (argc>=2 && *argv[1]=='-') {
44 usage(loadacm_usage);
45 }
46
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 fd = open("/dev/tty", O_RDWR);
48 if (fd < 0) {
49 fprintf(stderr, "Error opening /dev/tty1: %s\n", strerror(errno));
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000050 return( FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 }
52
53 if (screen_map_load(fd, stdin)) {
54 fprintf(stderr, "Error loading acm: %s\n", strerror(errno));
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000055 return( FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 }
57
58 write(fd, "\033(K", 3);
59
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000060 return( TRUE);
Erik Andersena8991081999-12-29 03:34:00 +000061}
62
Erik Andersene49d5ec2000-02-08 19:58:47 +000063int screen_map_load(int fd, FILE * fp)
Erik Andersena8991081999-12-29 03:34:00 +000064{
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 struct stat stbuf;
66 unicode wbuf[E_TABSZ];
67 unsigned char buf[E_TABSZ];
68 int parse_failed = 0;
69 int is_unicode;
Erik Andersena8991081999-12-29 03:34:00 +000070
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 if (fstat(fp->_fileno, &stbuf))
72 perror("Cannot stat map file"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +000073
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */
75 if (!
76 (parse_failed =
77 (-1 == uni_screen_map_read_ascii(fp, wbuf, &is_unicode)))
78|| (S_ISREG(stbuf.st_mode) && (stbuf.st_size == (sizeof(unicode) * E_TABSZ)))) { /* test for binary UTF map by size */
79 if (parse_failed) {
80 if (-1 == fseek(fp, 0, SEEK_SET)) {
81 if (errno == ESPIPE)
82 fprintf(stderr,
83 "16bit screen-map MUST be a regular file.\n"),
84 exit(1);
85 else
86 perror("fseek failed reading binary 16bit screen-map"),
87 exit(1);
88 }
89
90 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1)
91 perror("Cannot read [new] map from file"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +000092#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 else
94 fprintf(stderr, "Input screen-map is binary.\n");
Erik Andersena8991081999-12-29 03:34:00 +000095#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 }
97
98 /* if it was effectively a 16-bit ASCII, OK, else try to read as 8-bit map */
99 /* same if it was binary, ie. if parse_failed */
100 if (parse_failed || is_unicode) {
101 if (ioctl(fd, PIO_UNISCRNMAP, wbuf))
102 perror("PIO_UNISCRNMAP ioctl"), exit(1);
103 else
104 return 0;
105 }
Erik Andersena8991081999-12-29 03:34:00 +0000106 }
107
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 /* rewind... */
109 if (-1 == fseek(fp, 0, SEEK_SET)) {
110 if (errno == ESPIPE)
111 fprintf(stderr,
112 "Assuming 8bit screen-map - MUST be a regular file.\n"),
113 exit(1);
114 else
115 perror("fseek failed assuming 8bit screen-map"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000116 }
Erik Andersena8991081999-12-29 03:34:00 +0000117
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 /* ... and try an old 8-bit screen-map */
119 if (!(parse_failed = (-1 == old_screen_map_read_ascii(fp, buf))) ||
120 (S_ISREG(stbuf.st_mode) && (stbuf.st_size == E_TABSZ))) { /* test for binary old 8-bit map by size */
121 if (parse_failed) {
122 if (-1 == fseek(fp, 0, SEEK_SET)) {
123 if (errno == ESPIPE)
124 /* should not - it succedeed above */
125 fprintf(stderr, "fseek() returned ESPIPE !\n"),
126 exit(1);
127 else
128 perror("fseek for binary 8bit screen-map"), exit(1);
129 }
130
131 if (fread(buf, E_TABSZ, 1, fp) != 1)
132 perror("Cannot read [old] map from file"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000133#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 else
135 fprintf(stderr, "Input screen-map is binary.\n");
Erik Andersena8991081999-12-29 03:34:00 +0000136#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 }
138
139 if (ioctl(fd, PIO_SCRNMAP, buf))
140 perror("PIO_SCRNMAP ioctl"), exit(1);
141 else
142 return 0;
143 } else {
144 fprintf(stderr, "Error parsing symbolic map\n");
145 exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000146 }
Erik Andersena8991081999-12-29 03:34:00 +0000147}
148
149
150/*
151 * - reads `fp' as a 16-bit ASCII SFM file.
152 * - returns -1 on error.
153 * - returns it in `unicode' in an E_TABSZ-elements array.
154 * - sets `*is_unicode' flagiff there were any non-8-bit
155 * (ie. real 16-bit) mapping.
156 *
157 * FIXME: ignores everything after second word
158 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
Erik Andersena8991081999-12-29 03:34:00 +0000160{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161 char buffer[256]; /* line buffer reading file */
162 char *p, *q; /* 1st + 2nd words in line */
163 int in, on; /* the same, as numbers */
164 int tmp_is_unicode; /* tmp for is_unicode calculation */
165 int i; /* loop index - result holder */
166 int ret_code = 0; /* return code */
167 sigset_t sigset, old_sigset;
Erik Andersena8991081999-12-29 03:34:00 +0000168
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169 assert(is_unicode);
170
171 *is_unicode = 0;
172
173 /* first 128 codes defaults to ASCII */
174 for (i = 0; i < 128; i++)
175 buf[i] = i;
176 /* remaining defaults to replacement char (usually E_TABSZ = 256) */
177 for (; i < E_TABSZ; i++)
178 buf[i] = 0xfffd;
179
180 /* block SIGCHLD */
181 sigemptyset(&sigset);
182 sigaddset(&sigset, SIGCHLD);
183 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
184
185 do {
186 if (NULL == fgets(buffer, sizeof(buffer), fp)) {
187 if (feof(fp))
188 break;
189 else {
190 perror("uni_screen_map_read_ascii() can't read line");
191 exit(2);
192 }
193 }
194
195 /* get "charset-relative charcode", stripping leading spaces */
196 p = strtok(buffer, " \t\n");
197
198 /* skip empty lines and comments */
199 if (!p || *p == '#')
200 continue;
201
202 /* get unicode mapping */
203 q = strtok(NULL, " \t\n");
204 if (q) {
205 in = ctoi(p, NULL);
206 if (in < 0 || in > 255) {
207 ret_code = -1;
208 break;
209 }
210
211 on = ctoi(q, &tmp_is_unicode);
212 if (in < 0 && on > 65535) {
213 ret_code = -1;
214 break;
215 }
216
217 *is_unicode |= tmp_is_unicode;
218 buf[in] = on;
219 } else {
220 ret_code = -1;
221 break;
222 }
Erik Andersena8991081999-12-29 03:34:00 +0000223 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224 while (1); /* terminated by break on feof() */
Erik Andersena8991081999-12-29 03:34:00 +0000225
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226 /* restore sig mask */
227 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
Erik Andersena8991081999-12-29 03:34:00 +0000228
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 return ret_code;
Erik Andersena8991081999-12-29 03:34:00 +0000230}
231
232
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
Erik Andersena8991081999-12-29 03:34:00 +0000234{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 char buffer[256];
236 int in, on;
237 char *p, *q;
Erik Andersena8991081999-12-29 03:34:00 +0000238
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 for (in = 0; in < 256; in++)
240 buf[in] = in;
241
242 while (fgets(buffer, sizeof(buffer) - 1, fp)) {
243 p = strtok(buffer, " \t\n");
244
245 if (!p || *p == '#')
246 continue;
247
248 q = strtok(NULL, " \t\n#");
249 if (q) {
250 in = ctoi(p, NULL);
251 if (in < 0 || in > 255)
252 return -1;
253
254 on = ctoi(q, NULL);
255 if (in < 0 && on > 255)
256 return -1;
257
258 buf[in] = on;
259 } else
260 return -1;
Erik Andersena8991081999-12-29 03:34:00 +0000261 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262
263 return (0);
Erik Andersena8991081999-12-29 03:34:00 +0000264}
265
266
267/*
268 * - converts a string into an int.
269 * - supports dec and hex bytes, hex UCS2, single-quoted byte and UTF8 chars.
270 * - returns the converted value
271 * - if `is_unicode != NULL', use it to tell whether it was unicode
272 *
273 * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones.
274 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275long int ctoi(unsigned char *s, int *is_unicode)
Erik Andersena8991081999-12-29 03:34:00 +0000276{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 int i;
278 size_t ls;
Erik Andersena8991081999-12-29 03:34:00 +0000279
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280 ls = strlen(s);
281 if (is_unicode)
282 *is_unicode = 0;
Erik Andersena8991081999-12-29 03:34:00 +0000283
Erik Andersene49d5ec2000-02-08 19:58:47 +0000284 /* hex-specified UCS2 */
285 if ((strncmp(s, "U+", 2) == 0) &&
286 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) {
287 sscanf(s + 2, "%x", &i);
288 if (is_unicode)
289 *is_unicode = 1;
290 }
291
292 /* hex-specified byte */
293 else if ((ls <= 4) && (strncmp(s, "0x", 2) == 0) &&
294 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2))
295 sscanf(s + 2, "%x", &i);
296
297 /* oct-specified number (byte) */
298 else if ((*s == '0') && (strspn(s, "01234567") == ls))
299 sscanf(s, "%o", &i);
300
301 /* dec-specified number (byte) */
302 else if (strspn(s, "0123456789") == ls)
303 sscanf(s, "%d", &i);
304
305 /* single-byte quoted char */
306 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
307 i = s[1];
308
309 /* multi-byte UTF8 quoted char */
310 else if ((s[0] == '\'') && (s[ls - 1] == '\'')) {
311 s[ls - 1] = 0; /* ensure we'll not "parse UTF too far" */
312 i = utf8_to_ucs2(s + 1);
313 if (is_unicode)
314 *is_unicode = 1;
315 } else
316 return (-1);
317
318 return (i);
Erik Andersena8991081999-12-29 03:34:00 +0000319}
320
321
Erik Andersene49d5ec2000-02-08 19:58:47 +0000322void saveoldmap(int fd, char *omfil)
Erik Andersena8991081999-12-29 03:34:00 +0000323{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000324 FILE *fp;
325 char buf[E_TABSZ];
Erik Andersena8991081999-12-29 03:34:00 +0000326
327#ifdef GIO_UNISCRNMAP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 unicode xbuf[E_TABSZ];
329 int is_old_map = 0;
330
331 if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) {
332 perror("GIO_UNISCRNMAP ioctl error");
Erik Andersena8991081999-12-29 03:34:00 +0000333#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334 if (ioctl(fd, GIO_SCRNMAP, buf)) {
335 perror("GIO_SCRNMAP ioctl error");
336 exit(1);
337 } else
338 is_old_map = 1;
339#ifdef GIO_UNISCRNMAP
340 }
341#endif
342
343 if ((fp = fopen(omfil, "w")) == NULL) {
344 perror(omfil);
345 exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000346 }
347#ifdef GIO_UNISCRNMAP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000348 if (is_old_map) {
349#endif
350 if (fwrite(buf, E_TABSZ, 1, fp) != 1) {
351 perror("Error writing map to file");
352 exit(1);
353 }
354#ifdef GIO_UNISCRNMAP
355 } else if (fwrite(xbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) {
356 perror("Error writing map to file");
357 exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000358 }
359#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360
361 fclose(fp);
Erik Andersena8991081999-12-29 03:34:00 +0000362}
363
Erik Andersene49d5ec2000-02-08 19:58:47 +0000364unicode utf8_to_ucs2(char *buf)
Erik Andersena8991081999-12-29 03:34:00 +0000365{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366 int utf_count = 0;
Erik Andersen61677fe2000-04-13 01:18:56 +0000367 long utf_char = 0;
368 unicode tc = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000369 unsigned char c;
Erik Andersena8991081999-12-29 03:34:00 +0000370
Erik Andersene49d5ec2000-02-08 19:58:47 +0000371 do {
372 c = *buf;
373 buf++;
Erik Andersena8991081999-12-29 03:34:00 +0000374
Erik Andersene49d5ec2000-02-08 19:58:47 +0000375 /* if byte should be part of multi-byte sequence */
376 if (c & 0x80) {
377 /* if we have already started to parse a UTF8 sequence */
378 if (utf_count > 0 && (c & 0xc0) == 0x80) {
379 utf_char = (utf_char << 6) | (c & 0x3f);
380 utf_count--;
381 if (utf_count == 0)
382 tc = utf_char;
383 else
384 continue;
385 } else { /* Possibly 1st char of a UTF8 sequence */
Erik Andersena8991081999-12-29 03:34:00 +0000386
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 if ((c & 0xe0) == 0xc0) {
388 utf_count = 1;
389 utf_char = (c & 0x1f);
390 } else if ((c & 0xf0) == 0xe0) {
391 utf_count = 2;
392 utf_char = (c & 0x0f);
393 } else if ((c & 0xf8) == 0xf0) {
394 utf_count = 3;
395 utf_char = (c & 0x07);
396 } else if ((c & 0xfc) == 0xf8) {
397 utf_count = 4;
398 utf_char = (c & 0x03);
399 } else if ((c & 0xfe) == 0xfc) {
400 utf_count = 5;
401 utf_char = (c & 0x01);
402 } else
403 utf_count = 0;
404 continue;
405 }
406 } else { /* not part of multi-byte sequence - treat as ASCII
407 * this makes incomplete sequences to be ignored
408 */
409 tc = c;
410 utf_count = 0;
411 }
412 }
413 while (utf_count);
414
415 return tc;
Erik Andersena8991081999-12-29 03:34:00 +0000416}