blob: 9eebf3bb57964cd585e03714a990ea6c35b6e689 [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) {
Matt Kraaid537a952000-07-14 01:51:25 +000049 errorMsg("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)) {
Matt Kraaid537a952000-07-14 01:51:25 +000054 errorMsg("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
Eric Andersen999bf722000-07-09 06:59:58 +000071 if (fstat(fileno(fp), &stbuf))
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 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)
Matt Kraaid537a952000-07-14 01:51:25 +000082 errorMsg("16bit screen-map MUST be a regular file.\n"),
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 exit(1);
84 else
85 perror("fseek failed reading binary 16bit screen-map"),
86 exit(1);
87 }
88
89 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1)
90 perror("Cannot read [new] map from file"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +000091#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 else
Matt Kraaid537a952000-07-14 01:51:25 +000093 errorMsg("Input screen-map is binary.\n");
Erik Andersena8991081999-12-29 03:34:00 +000094#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 }
96
97 /* if it was effectively a 16-bit ASCII, OK, else try to read as 8-bit map */
98 /* same if it was binary, ie. if parse_failed */
99 if (parse_failed || is_unicode) {
100 if (ioctl(fd, PIO_UNISCRNMAP, wbuf))
101 perror("PIO_UNISCRNMAP ioctl"), exit(1);
102 else
103 return 0;
104 }
Erik Andersena8991081999-12-29 03:34:00 +0000105 }
106
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 /* rewind... */
108 if (-1 == fseek(fp, 0, SEEK_SET)) {
109 if (errno == ESPIPE)
Matt Kraaid537a952000-07-14 01:51:25 +0000110 errorMsg("Assuming 8bit screen-map - MUST be a regular file.\n"),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 exit(1);
112 else
113 perror("fseek failed assuming 8bit screen-map"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000114 }
Erik Andersena8991081999-12-29 03:34:00 +0000115
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 /* ... and try an old 8-bit screen-map */
117 if (!(parse_failed = (-1 == old_screen_map_read_ascii(fp, buf))) ||
118 (S_ISREG(stbuf.st_mode) && (stbuf.st_size == E_TABSZ))) { /* test for binary old 8-bit map by size */
119 if (parse_failed) {
120 if (-1 == fseek(fp, 0, SEEK_SET)) {
121 if (errno == ESPIPE)
122 /* should not - it succedeed above */
Matt Kraaid537a952000-07-14 01:51:25 +0000123 errorMsg("fseek() returned ESPIPE !\n"),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 exit(1);
125 else
126 perror("fseek for binary 8bit screen-map"), exit(1);
127 }
128
129 if (fread(buf, E_TABSZ, 1, fp) != 1)
130 perror("Cannot read [old] map from file"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000131#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 else
Matt Kraaid537a952000-07-14 01:51:25 +0000133 errorMsg("Input screen-map is binary.\n");
Erik Andersena8991081999-12-29 03:34:00 +0000134#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 }
136
137 if (ioctl(fd, PIO_SCRNMAP, buf))
138 perror("PIO_SCRNMAP ioctl"), exit(1);
139 else
140 return 0;
Erik Andersena8991081999-12-29 03:34:00 +0000141 }
Matt Kraaid537a952000-07-14 01:51:25 +0000142 errorMsg("Error parsing symbolic map\n");
Eric Andersenb6106152000-06-19 17:25:40 +0000143 return(1);
Erik Andersena8991081999-12-29 03:34:00 +0000144}
145
146
147/*
148 * - reads `fp' as a 16-bit ASCII SFM file.
149 * - returns -1 on error.
150 * - returns it in `unicode' in an E_TABSZ-elements array.
151 * - sets `*is_unicode' flagiff there were any non-8-bit
152 * (ie. real 16-bit) mapping.
153 *
154 * FIXME: ignores everything after second word
155 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
Erik Andersena8991081999-12-29 03:34:00 +0000157{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 char buffer[256]; /* line buffer reading file */
159 char *p, *q; /* 1st + 2nd words in line */
160 int in, on; /* the same, as numbers */
161 int tmp_is_unicode; /* tmp for is_unicode calculation */
162 int i; /* loop index - result holder */
163 int ret_code = 0; /* return code */
164 sigset_t sigset, old_sigset;
Erik Andersena8991081999-12-29 03:34:00 +0000165
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166 assert(is_unicode);
167
168 *is_unicode = 0;
169
170 /* first 128 codes defaults to ASCII */
171 for (i = 0; i < 128; i++)
172 buf[i] = i;
173 /* remaining defaults to replacement char (usually E_TABSZ = 256) */
174 for (; i < E_TABSZ; i++)
175 buf[i] = 0xfffd;
176
177 /* block SIGCHLD */
178 sigemptyset(&sigset);
179 sigaddset(&sigset, SIGCHLD);
180 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
181
182 do {
183 if (NULL == fgets(buffer, sizeof(buffer), fp)) {
184 if (feof(fp))
185 break;
186 else {
187 perror("uni_screen_map_read_ascii() can't read line");
188 exit(2);
189 }
190 }
191
192 /* get "charset-relative charcode", stripping leading spaces */
193 p = strtok(buffer, " \t\n");
194
195 /* skip empty lines and comments */
196 if (!p || *p == '#')
197 continue;
198
199 /* get unicode mapping */
200 q = strtok(NULL, " \t\n");
201 if (q) {
202 in = ctoi(p, NULL);
203 if (in < 0 || in > 255) {
204 ret_code = -1;
205 break;
206 }
207
208 on = ctoi(q, &tmp_is_unicode);
209 if (in < 0 && on > 65535) {
210 ret_code = -1;
211 break;
212 }
213
214 *is_unicode |= tmp_is_unicode;
215 buf[in] = on;
216 } else {
217 ret_code = -1;
218 break;
219 }
Erik Andersena8991081999-12-29 03:34:00 +0000220 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221 while (1); /* terminated by break on feof() */
Erik Andersena8991081999-12-29 03:34:00 +0000222
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223 /* restore sig mask */
224 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
Erik Andersena8991081999-12-29 03:34:00 +0000225
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226 return ret_code;
Erik Andersena8991081999-12-29 03:34:00 +0000227}
228
229
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
Erik Andersena8991081999-12-29 03:34:00 +0000231{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232 char buffer[256];
233 int in, on;
234 char *p, *q;
Erik Andersena8991081999-12-29 03:34:00 +0000235
Erik Andersene49d5ec2000-02-08 19:58:47 +0000236 for (in = 0; in < 256; in++)
237 buf[in] = in;
238
239 while (fgets(buffer, sizeof(buffer) - 1, fp)) {
240 p = strtok(buffer, " \t\n");
241
242 if (!p || *p == '#')
243 continue;
244
245 q = strtok(NULL, " \t\n#");
246 if (q) {
247 in = ctoi(p, NULL);
248 if (in < 0 || in > 255)
249 return -1;
250
251 on = ctoi(q, NULL);
252 if (in < 0 && on > 255)
253 return -1;
254
255 buf[in] = on;
256 } else
257 return -1;
Erik Andersena8991081999-12-29 03:34:00 +0000258 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259
260 return (0);
Erik Andersena8991081999-12-29 03:34:00 +0000261}
262
263
264/*
265 * - converts a string into an int.
266 * - supports dec and hex bytes, hex UCS2, single-quoted byte and UTF8 chars.
267 * - returns the converted value
268 * - if `is_unicode != NULL', use it to tell whether it was unicode
269 *
270 * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones.
271 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272long int ctoi(unsigned char *s, int *is_unicode)
Erik Andersena8991081999-12-29 03:34:00 +0000273{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 int i;
275 size_t ls;
Erik Andersena8991081999-12-29 03:34:00 +0000276
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 ls = strlen(s);
278 if (is_unicode)
279 *is_unicode = 0;
Erik Andersena8991081999-12-29 03:34:00 +0000280
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281 /* hex-specified UCS2 */
282 if ((strncmp(s, "U+", 2) == 0) &&
283 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) {
284 sscanf(s + 2, "%x", &i);
285 if (is_unicode)
286 *is_unicode = 1;
287 }
288
289 /* hex-specified byte */
290 else if ((ls <= 4) && (strncmp(s, "0x", 2) == 0) &&
291 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2))
292 sscanf(s + 2, "%x", &i);
293
294 /* oct-specified number (byte) */
295 else if ((*s == '0') && (strspn(s, "01234567") == ls))
296 sscanf(s, "%o", &i);
297
298 /* dec-specified number (byte) */
299 else if (strspn(s, "0123456789") == ls)
300 sscanf(s, "%d", &i);
301
302 /* single-byte quoted char */
303 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
304 i = s[1];
305
306 /* multi-byte UTF8 quoted char */
307 else if ((s[0] == '\'') && (s[ls - 1] == '\'')) {
308 s[ls - 1] = 0; /* ensure we'll not "parse UTF too far" */
309 i = utf8_to_ucs2(s + 1);
310 if (is_unicode)
311 *is_unicode = 1;
312 } else
313 return (-1);
314
315 return (i);
Erik Andersena8991081999-12-29 03:34:00 +0000316}
317
318
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319void saveoldmap(int fd, char *omfil)
Erik Andersena8991081999-12-29 03:34:00 +0000320{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321 FILE *fp;
322 char buf[E_TABSZ];
Erik Andersena8991081999-12-29 03:34:00 +0000323
324#ifdef GIO_UNISCRNMAP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 unicode xbuf[E_TABSZ];
326 int is_old_map = 0;
327
328 if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) {
329 perror("GIO_UNISCRNMAP ioctl error");
Erik Andersena8991081999-12-29 03:34:00 +0000330#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 if (ioctl(fd, GIO_SCRNMAP, buf)) {
332 perror("GIO_SCRNMAP ioctl error");
333 exit(1);
334 } else
335 is_old_map = 1;
336#ifdef GIO_UNISCRNMAP
337 }
338#endif
339
340 if ((fp = fopen(omfil, "w")) == NULL) {
341 perror(omfil);
342 exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000343 }
344#ifdef GIO_UNISCRNMAP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 if (is_old_map) {
346#endif
347 if (fwrite(buf, E_TABSZ, 1, fp) != 1) {
348 perror("Error writing map to file");
349 exit(1);
350 }
351#ifdef GIO_UNISCRNMAP
352 } else if (fwrite(xbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) {
353 perror("Error writing map to file");
354 exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000355 }
356#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357
358 fclose(fp);
Erik Andersena8991081999-12-29 03:34:00 +0000359}
360
Erik Andersene49d5ec2000-02-08 19:58:47 +0000361unicode utf8_to_ucs2(char *buf)
Erik Andersena8991081999-12-29 03:34:00 +0000362{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000363 int utf_count = 0;
Erik Andersen61677fe2000-04-13 01:18:56 +0000364 long utf_char = 0;
365 unicode tc = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366 unsigned char c;
Erik Andersena8991081999-12-29 03:34:00 +0000367
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 do {
369 c = *buf;
370 buf++;
Erik Andersena8991081999-12-29 03:34:00 +0000371
Erik Andersene49d5ec2000-02-08 19:58:47 +0000372 /* if byte should be part of multi-byte sequence */
373 if (c & 0x80) {
374 /* if we have already started to parse a UTF8 sequence */
375 if (utf_count > 0 && (c & 0xc0) == 0x80) {
376 utf_char = (utf_char << 6) | (c & 0x3f);
377 utf_count--;
378 if (utf_count == 0)
379 tc = utf_char;
380 else
381 continue;
382 } else { /* Possibly 1st char of a UTF8 sequence */
Erik Andersena8991081999-12-29 03:34:00 +0000383
Erik Andersene49d5ec2000-02-08 19:58:47 +0000384 if ((c & 0xe0) == 0xc0) {
385 utf_count = 1;
386 utf_char = (c & 0x1f);
387 } else if ((c & 0xf0) == 0xe0) {
388 utf_count = 2;
389 utf_char = (c & 0x0f);
390 } else if ((c & 0xf8) == 0xf0) {
391 utf_count = 3;
392 utf_char = (c & 0x07);
393 } else if ((c & 0xfc) == 0xf8) {
394 utf_count = 4;
395 utf_char = (c & 0x03);
396 } else if ((c & 0xfe) == 0xfc) {
397 utf_count = 5;
398 utf_char = (c & 0x01);
399 } else
400 utf_count = 0;
401 continue;
402 }
403 } else { /* not part of multi-byte sequence - treat as ASCII
404 * this makes incomplete sequences to be ignored
405 */
406 tc = c;
407 utf_count = 0;
408 }
409 }
410 while (utf_count);
411
412 return tc;
Erik Andersena8991081999-12-29 03:34:00 +0000413}