blob: cf393d92ddf495e6fc0cba52700dce844cf8de4b [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;
Erik Andersena8991081999-12-29 03:34:00 +0000143 }
Eric Andersenb6106152000-06-19 17:25:40 +0000144 fprintf(stderr, "Error parsing symbolic map\n");
145 return(1);
Erik Andersena8991081999-12-29 03:34:00 +0000146}
147
148
149/*
150 * - reads `fp' as a 16-bit ASCII SFM file.
151 * - returns -1 on error.
152 * - returns it in `unicode' in an E_TABSZ-elements array.
153 * - sets `*is_unicode' flagiff there were any non-8-bit
154 * (ie. real 16-bit) mapping.
155 *
156 * FIXME: ignores everything after second word
157 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
Erik Andersena8991081999-12-29 03:34:00 +0000159{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 char buffer[256]; /* line buffer reading file */
161 char *p, *q; /* 1st + 2nd words in line */
162 int in, on; /* the same, as numbers */
163 int tmp_is_unicode; /* tmp for is_unicode calculation */
164 int i; /* loop index - result holder */
165 int ret_code = 0; /* return code */
166 sigset_t sigset, old_sigset;
Erik Andersena8991081999-12-29 03:34:00 +0000167
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 assert(is_unicode);
169
170 *is_unicode = 0;
171
172 /* first 128 codes defaults to ASCII */
173 for (i = 0; i < 128; i++)
174 buf[i] = i;
175 /* remaining defaults to replacement char (usually E_TABSZ = 256) */
176 for (; i < E_TABSZ; i++)
177 buf[i] = 0xfffd;
178
179 /* block SIGCHLD */
180 sigemptyset(&sigset);
181 sigaddset(&sigset, SIGCHLD);
182 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
183
184 do {
185 if (NULL == fgets(buffer, sizeof(buffer), fp)) {
186 if (feof(fp))
187 break;
188 else {
189 perror("uni_screen_map_read_ascii() can't read line");
190 exit(2);
191 }
192 }
193
194 /* get "charset-relative charcode", stripping leading spaces */
195 p = strtok(buffer, " \t\n");
196
197 /* skip empty lines and comments */
198 if (!p || *p == '#')
199 continue;
200
201 /* get unicode mapping */
202 q = strtok(NULL, " \t\n");
203 if (q) {
204 in = ctoi(p, NULL);
205 if (in < 0 || in > 255) {
206 ret_code = -1;
207 break;
208 }
209
210 on = ctoi(q, &tmp_is_unicode);
211 if (in < 0 && on > 65535) {
212 ret_code = -1;
213 break;
214 }
215
216 *is_unicode |= tmp_is_unicode;
217 buf[in] = on;
218 } else {
219 ret_code = -1;
220 break;
221 }
Erik Andersena8991081999-12-29 03:34:00 +0000222 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223 while (1); /* terminated by break on feof() */
Erik Andersena8991081999-12-29 03:34:00 +0000224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 /* restore sig mask */
226 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
Erik Andersena8991081999-12-29 03:34:00 +0000227
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 return ret_code;
Erik Andersena8991081999-12-29 03:34:00 +0000229}
230
231
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
Erik Andersena8991081999-12-29 03:34:00 +0000233{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000234 char buffer[256];
235 int in, on;
236 char *p, *q;
Erik Andersena8991081999-12-29 03:34:00 +0000237
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 for (in = 0; in < 256; in++)
239 buf[in] = in;
240
241 while (fgets(buffer, sizeof(buffer) - 1, fp)) {
242 p = strtok(buffer, " \t\n");
243
244 if (!p || *p == '#')
245 continue;
246
247 q = strtok(NULL, " \t\n#");
248 if (q) {
249 in = ctoi(p, NULL);
250 if (in < 0 || in > 255)
251 return -1;
252
253 on = ctoi(q, NULL);
254 if (in < 0 && on > 255)
255 return -1;
256
257 buf[in] = on;
258 } else
259 return -1;
Erik Andersena8991081999-12-29 03:34:00 +0000260 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261
262 return (0);
Erik Andersena8991081999-12-29 03:34:00 +0000263}
264
265
266/*
267 * - converts a string into an int.
268 * - supports dec and hex bytes, hex UCS2, single-quoted byte and UTF8 chars.
269 * - returns the converted value
270 * - if `is_unicode != NULL', use it to tell whether it was unicode
271 *
272 * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones.
273 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274long int ctoi(unsigned char *s, int *is_unicode)
Erik Andersena8991081999-12-29 03:34:00 +0000275{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 int i;
277 size_t ls;
Erik Andersena8991081999-12-29 03:34:00 +0000278
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 ls = strlen(s);
280 if (is_unicode)
281 *is_unicode = 0;
Erik Andersena8991081999-12-29 03:34:00 +0000282
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 /* hex-specified UCS2 */
284 if ((strncmp(s, "U+", 2) == 0) &&
285 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) {
286 sscanf(s + 2, "%x", &i);
287 if (is_unicode)
288 *is_unicode = 1;
289 }
290
291 /* hex-specified byte */
292 else if ((ls <= 4) && (strncmp(s, "0x", 2) == 0) &&
293 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2))
294 sscanf(s + 2, "%x", &i);
295
296 /* oct-specified number (byte) */
297 else if ((*s == '0') && (strspn(s, "01234567") == ls))
298 sscanf(s, "%o", &i);
299
300 /* dec-specified number (byte) */
301 else if (strspn(s, "0123456789") == ls)
302 sscanf(s, "%d", &i);
303
304 /* single-byte quoted char */
305 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
306 i = s[1];
307
308 /* multi-byte UTF8 quoted char */
309 else if ((s[0] == '\'') && (s[ls - 1] == '\'')) {
310 s[ls - 1] = 0; /* ensure we'll not "parse UTF too far" */
311 i = utf8_to_ucs2(s + 1);
312 if (is_unicode)
313 *is_unicode = 1;
314 } else
315 return (-1);
316
317 return (i);
Erik Andersena8991081999-12-29 03:34:00 +0000318}
319
320
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321void saveoldmap(int fd, char *omfil)
Erik Andersena8991081999-12-29 03:34:00 +0000322{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 FILE *fp;
324 char buf[E_TABSZ];
Erik Andersena8991081999-12-29 03:34:00 +0000325
326#ifdef GIO_UNISCRNMAP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000327 unicode xbuf[E_TABSZ];
328 int is_old_map = 0;
329
330 if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) {
331 perror("GIO_UNISCRNMAP ioctl error");
Erik Andersena8991081999-12-29 03:34:00 +0000332#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000333 if (ioctl(fd, GIO_SCRNMAP, buf)) {
334 perror("GIO_SCRNMAP ioctl error");
335 exit(1);
336 } else
337 is_old_map = 1;
338#ifdef GIO_UNISCRNMAP
339 }
340#endif
341
342 if ((fp = fopen(omfil, "w")) == NULL) {
343 perror(omfil);
344 exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000345 }
346#ifdef GIO_UNISCRNMAP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000347 if (is_old_map) {
348#endif
349 if (fwrite(buf, E_TABSZ, 1, fp) != 1) {
350 perror("Error writing map to file");
351 exit(1);
352 }
353#ifdef GIO_UNISCRNMAP
354 } else if (fwrite(xbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) {
355 perror("Error writing map to file");
356 exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000357 }
358#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359
360 fclose(fp);
Erik Andersena8991081999-12-29 03:34:00 +0000361}
362
Erik Andersene49d5ec2000-02-08 19:58:47 +0000363unicode utf8_to_ucs2(char *buf)
Erik Andersena8991081999-12-29 03:34:00 +0000364{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365 int utf_count = 0;
Erik Andersen61677fe2000-04-13 01:18:56 +0000366 long utf_char = 0;
367 unicode tc = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 unsigned char c;
Erik Andersena8991081999-12-29 03:34:00 +0000369
Erik Andersene49d5ec2000-02-08 19:58:47 +0000370 do {
371 c = *buf;
372 buf++;
Erik Andersena8991081999-12-29 03:34:00 +0000373
Erik Andersene49d5ec2000-02-08 19:58:47 +0000374 /* if byte should be part of multi-byte sequence */
375 if (c & 0x80) {
376 /* if we have already started to parse a UTF8 sequence */
377 if (utf_count > 0 && (c & 0xc0) == 0x80) {
378 utf_char = (utf_char << 6) | (c & 0x3f);
379 utf_count--;
380 if (utf_count == 0)
381 tc = utf_char;
382 else
383 continue;
384 } else { /* Possibly 1st char of a UTF8 sequence */
Erik Andersena8991081999-12-29 03:34:00 +0000385
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386 if ((c & 0xe0) == 0xc0) {
387 utf_count = 1;
388 utf_char = (c & 0x1f);
389 } else if ((c & 0xf0) == 0xe0) {
390 utf_count = 2;
391 utf_char = (c & 0x0f);
392 } else if ((c & 0xf8) == 0xf0) {
393 utf_count = 3;
394 utf_char = (c & 0x07);
395 } else if ((c & 0xfc) == 0xf8) {
396 utf_count = 4;
397 utf_char = (c & 0x03);
398 } else if ((c & 0xfe) == 0xfc) {
399 utf_count = 5;
400 utf_char = (c & 0x01);
401 } else
402 utf_count = 0;
403 continue;
404 }
405 } else { /* not part of multi-byte sequence - treat as ASCII
406 * this makes incomplete sequences to be ignored
407 */
408 tc = c;
409 utf_count = 0;
410 }
411 }
412 while (utf_count);
413
414 return tc;
Erik Andersena8991081999-12-29 03:34:00 +0000415}