blob: 072562f6bef28090f597ce52e40bd03ff4f1436b [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
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/types.h>
20#include <sys/stat.h>
21#include <sys/ioctl.h>
22#include <sys/kd.h>
23
24typedef unsigned short unicode;
25
26static long int ctoi(unsigned char *s, int *is_unicode);
Erik Andersene49d5ec2000-02-08 19:58:47 +000027int old_screen_map_read_ascii(FILE * fp, unsigned char buf[]);
28int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode);
29unicode utf8_to_ucs2(char *buf);
30int screen_map_load(int fd, FILE * fp);
Erik Andersena8991081999-12-29 03:34:00 +000031
32int loadacm_main(int argc, char **argv)
33{
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 int fd;
35
36 fd = open("/dev/tty", O_RDWR);
37 if (fd < 0) {
38 fprintf(stderr, "Error opening /dev/tty1: %s\n", strerror(errno));
39 return 1;
40 }
41
42 if (screen_map_load(fd, stdin)) {
43 fprintf(stderr, "Error loading acm: %s\n", strerror(errno));
44 return 1;
45 }
46
47 write(fd, "\033(K", 3);
48
49 return 0;
Erik Andersena8991081999-12-29 03:34:00 +000050}
51
Erik Andersene49d5ec2000-02-08 19:58:47 +000052int screen_map_load(int fd, FILE * fp)
Erik Andersena8991081999-12-29 03:34:00 +000053{
Erik Andersene49d5ec2000-02-08 19:58:47 +000054 struct stat stbuf;
55 unicode wbuf[E_TABSZ];
56 unsigned char buf[E_TABSZ];
57 int parse_failed = 0;
58 int is_unicode;
Erik Andersena8991081999-12-29 03:34:00 +000059
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 if (fstat(fp->_fileno, &stbuf))
61 perror("Cannot stat map file"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +000062
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */
64 if (!
65 (parse_failed =
66 (-1 == uni_screen_map_read_ascii(fp, wbuf, &is_unicode)))
67|| (S_ISREG(stbuf.st_mode) && (stbuf.st_size == (sizeof(unicode) * E_TABSZ)))) { /* test for binary UTF map by size */
68 if (parse_failed) {
69 if (-1 == fseek(fp, 0, SEEK_SET)) {
70 if (errno == ESPIPE)
71 fprintf(stderr,
72 "16bit screen-map MUST be a regular file.\n"),
73 exit(1);
74 else
75 perror("fseek failed reading binary 16bit screen-map"),
76 exit(1);
77 }
78
79 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1)
80 perror("Cannot read [new] map from file"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +000081#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 else
83 fprintf(stderr, "Input screen-map is binary.\n");
Erik Andersena8991081999-12-29 03:34:00 +000084#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 }
86
87 /* if it was effectively a 16-bit ASCII, OK, else try to read as 8-bit map */
88 /* same if it was binary, ie. if parse_failed */
89 if (parse_failed || is_unicode) {
90 if (ioctl(fd, PIO_UNISCRNMAP, wbuf))
91 perror("PIO_UNISCRNMAP ioctl"), exit(1);
92 else
93 return 0;
94 }
Erik Andersena8991081999-12-29 03:34:00 +000095 }
96
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 /* rewind... */
98 if (-1 == fseek(fp, 0, SEEK_SET)) {
99 if (errno == ESPIPE)
100 fprintf(stderr,
101 "Assuming 8bit screen-map - MUST be a regular file.\n"),
102 exit(1);
103 else
104 perror("fseek failed assuming 8bit screen-map"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000105 }
Erik Andersena8991081999-12-29 03:34:00 +0000106
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 /* ... and try an old 8-bit screen-map */
108 if (!(parse_failed = (-1 == old_screen_map_read_ascii(fp, buf))) ||
109 (S_ISREG(stbuf.st_mode) && (stbuf.st_size == E_TABSZ))) { /* test for binary old 8-bit map by size */
110 if (parse_failed) {
111 if (-1 == fseek(fp, 0, SEEK_SET)) {
112 if (errno == ESPIPE)
113 /* should not - it succedeed above */
114 fprintf(stderr, "fseek() returned ESPIPE !\n"),
115 exit(1);
116 else
117 perror("fseek for binary 8bit screen-map"), exit(1);
118 }
119
120 if (fread(buf, E_TABSZ, 1, fp) != 1)
121 perror("Cannot read [old] map from file"), exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000122#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 else
124 fprintf(stderr, "Input screen-map is binary.\n");
Erik Andersena8991081999-12-29 03:34:00 +0000125#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 }
127
128 if (ioctl(fd, PIO_SCRNMAP, buf))
129 perror("PIO_SCRNMAP ioctl"), exit(1);
130 else
131 return 0;
132 } else {
133 fprintf(stderr, "Error parsing symbolic map\n");
134 exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000135 }
Erik Andersena8991081999-12-29 03:34:00 +0000136}
137
138
139/*
140 * - reads `fp' as a 16-bit ASCII SFM file.
141 * - returns -1 on error.
142 * - returns it in `unicode' in an E_TABSZ-elements array.
143 * - sets `*is_unicode' flagiff there were any non-8-bit
144 * (ie. real 16-bit) mapping.
145 *
146 * FIXME: ignores everything after second word
147 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
Erik Andersena8991081999-12-29 03:34:00 +0000149{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000150 char buffer[256]; /* line buffer reading file */
151 char *p, *q; /* 1st + 2nd words in line */
152 int in, on; /* the same, as numbers */
153 int tmp_is_unicode; /* tmp for is_unicode calculation */
154 int i; /* loop index - result holder */
155 int ret_code = 0; /* return code */
156 sigset_t sigset, old_sigset;
Erik Andersena8991081999-12-29 03:34:00 +0000157
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 assert(is_unicode);
159
160 *is_unicode = 0;
161
162 /* first 128 codes defaults to ASCII */
163 for (i = 0; i < 128; i++)
164 buf[i] = i;
165 /* remaining defaults to replacement char (usually E_TABSZ = 256) */
166 for (; i < E_TABSZ; i++)
167 buf[i] = 0xfffd;
168
169 /* block SIGCHLD */
170 sigemptyset(&sigset);
171 sigaddset(&sigset, SIGCHLD);
172 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
173
174 do {
175 if (NULL == fgets(buffer, sizeof(buffer), fp)) {
176 if (feof(fp))
177 break;
178 else {
179 perror("uni_screen_map_read_ascii() can't read line");
180 exit(2);
181 }
182 }
183
184 /* get "charset-relative charcode", stripping leading spaces */
185 p = strtok(buffer, " \t\n");
186
187 /* skip empty lines and comments */
188 if (!p || *p == '#')
189 continue;
190
191 /* get unicode mapping */
192 q = strtok(NULL, " \t\n");
193 if (q) {
194 in = ctoi(p, NULL);
195 if (in < 0 || in > 255) {
196 ret_code = -1;
197 break;
198 }
199
200 on = ctoi(q, &tmp_is_unicode);
201 if (in < 0 && on > 65535) {
202 ret_code = -1;
203 break;
204 }
205
206 *is_unicode |= tmp_is_unicode;
207 buf[in] = on;
208 } else {
209 ret_code = -1;
210 break;
211 }
Erik Andersena8991081999-12-29 03:34:00 +0000212 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 while (1); /* terminated by break on feof() */
Erik Andersena8991081999-12-29 03:34:00 +0000214
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 /* restore sig mask */
216 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
Erik Andersena8991081999-12-29 03:34:00 +0000217
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218 return ret_code;
Erik Andersena8991081999-12-29 03:34:00 +0000219}
220
221
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
Erik Andersena8991081999-12-29 03:34:00 +0000223{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224 char buffer[256];
225 int in, on;
226 char *p, *q;
Erik Andersena8991081999-12-29 03:34:00 +0000227
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 for (in = 0; in < 256; in++)
229 buf[in] = in;
230
231 while (fgets(buffer, sizeof(buffer) - 1, fp)) {
232 p = strtok(buffer, " \t\n");
233
234 if (!p || *p == '#')
235 continue;
236
237 q = strtok(NULL, " \t\n#");
238 if (q) {
239 in = ctoi(p, NULL);
240 if (in < 0 || in > 255)
241 return -1;
242
243 on = ctoi(q, NULL);
244 if (in < 0 && on > 255)
245 return -1;
246
247 buf[in] = on;
248 } else
249 return -1;
Erik Andersena8991081999-12-29 03:34:00 +0000250 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251
252 return (0);
Erik Andersena8991081999-12-29 03:34:00 +0000253}
254
255
256/*
257 * - converts a string into an int.
258 * - supports dec and hex bytes, hex UCS2, single-quoted byte and UTF8 chars.
259 * - returns the converted value
260 * - if `is_unicode != NULL', use it to tell whether it was unicode
261 *
262 * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones.
263 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264long int ctoi(unsigned char *s, int *is_unicode)
Erik Andersena8991081999-12-29 03:34:00 +0000265{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 int i;
267 size_t ls;
Erik Andersena8991081999-12-29 03:34:00 +0000268
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269 ls = strlen(s);
270 if (is_unicode)
271 *is_unicode = 0;
Erik Andersena8991081999-12-29 03:34:00 +0000272
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 /* hex-specified UCS2 */
274 if ((strncmp(s, "U+", 2) == 0) &&
275 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) {
276 sscanf(s + 2, "%x", &i);
277 if (is_unicode)
278 *is_unicode = 1;
279 }
280
281 /* hex-specified byte */
282 else if ((ls <= 4) && (strncmp(s, "0x", 2) == 0) &&
283 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2))
284 sscanf(s + 2, "%x", &i);
285
286 /* oct-specified number (byte) */
287 else if ((*s == '0') && (strspn(s, "01234567") == ls))
288 sscanf(s, "%o", &i);
289
290 /* dec-specified number (byte) */
291 else if (strspn(s, "0123456789") == ls)
292 sscanf(s, "%d", &i);
293
294 /* single-byte quoted char */
295 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
296 i = s[1];
297
298 /* multi-byte UTF8 quoted char */
299 else if ((s[0] == '\'') && (s[ls - 1] == '\'')) {
300 s[ls - 1] = 0; /* ensure we'll not "parse UTF too far" */
301 i = utf8_to_ucs2(s + 1);
302 if (is_unicode)
303 *is_unicode = 1;
304 } else
305 return (-1);
306
307 return (i);
Erik Andersena8991081999-12-29 03:34:00 +0000308}
309
310
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311void saveoldmap(int fd, char *omfil)
Erik Andersena8991081999-12-29 03:34:00 +0000312{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000313 FILE *fp;
314 char buf[E_TABSZ];
Erik Andersena8991081999-12-29 03:34:00 +0000315
316#ifdef GIO_UNISCRNMAP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317 unicode xbuf[E_TABSZ];
318 int is_old_map = 0;
319
320 if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) {
321 perror("GIO_UNISCRNMAP ioctl error");
Erik Andersena8991081999-12-29 03:34:00 +0000322#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 if (ioctl(fd, GIO_SCRNMAP, buf)) {
324 perror("GIO_SCRNMAP ioctl error");
325 exit(1);
326 } else
327 is_old_map = 1;
328#ifdef GIO_UNISCRNMAP
329 }
330#endif
331
332 if ((fp = fopen(omfil, "w")) == NULL) {
333 perror(omfil);
334 exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000335 }
336#ifdef GIO_UNISCRNMAP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 if (is_old_map) {
338#endif
339 if (fwrite(buf, E_TABSZ, 1, fp) != 1) {
340 perror("Error writing map to file");
341 exit(1);
342 }
343#ifdef GIO_UNISCRNMAP
344 } else if (fwrite(xbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) {
345 perror("Error writing map to file");
346 exit(1);
Erik Andersena8991081999-12-29 03:34:00 +0000347 }
348#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000349
350 fclose(fp);
Erik Andersena8991081999-12-29 03:34:00 +0000351}
352
Erik Andersene49d5ec2000-02-08 19:58:47 +0000353unicode utf8_to_ucs2(char *buf)
Erik Andersena8991081999-12-29 03:34:00 +0000354{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355 int utf_count = 0;
356 long utf_char;
357 unicode tc;
358 unsigned char c;
Erik Andersena8991081999-12-29 03:34:00 +0000359
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 do {
361 c = *buf;
362 buf++;
Erik Andersena8991081999-12-29 03:34:00 +0000363
Erik Andersene49d5ec2000-02-08 19:58:47 +0000364 /* if byte should be part of multi-byte sequence */
365 if (c & 0x80) {
366 /* if we have already started to parse a UTF8 sequence */
367 if (utf_count > 0 && (c & 0xc0) == 0x80) {
368 utf_char = (utf_char << 6) | (c & 0x3f);
369 utf_count--;
370 if (utf_count == 0)
371 tc = utf_char;
372 else
373 continue;
374 } else { /* Possibly 1st char of a UTF8 sequence */
Erik Andersena8991081999-12-29 03:34:00 +0000375
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 if ((c & 0xe0) == 0xc0) {
377 utf_count = 1;
378 utf_char = (c & 0x1f);
379 } else if ((c & 0xf0) == 0xe0) {
380 utf_count = 2;
381 utf_char = (c & 0x0f);
382 } else if ((c & 0xf8) == 0xf0) {
383 utf_count = 3;
384 utf_char = (c & 0x07);
385 } else if ((c & 0xfc) == 0xf8) {
386 utf_count = 4;
387 utf_char = (c & 0x03);
388 } else if ((c & 0xfe) == 0xfc) {
389 utf_count = 5;
390 utf_char = (c & 0x01);
391 } else
392 utf_count = 0;
393 continue;
394 }
395 } else { /* not part of multi-byte sequence - treat as ASCII
396 * this makes incomplete sequences to be ignored
397 */
398 tc = c;
399 utf_count = 0;
400 }
401 }
402 while (utf_count);
403
404 return tc;
Erik Andersena8991081999-12-29 03:34:00 +0000405}