Change calls to error_msg.* and strerror to use perror_msg.*.
[oweals/busybox.git] / console-tools / loadacm.c
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 "busybox.h"
11 #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/ioctl.h>
22 #include <sys/kd.h>
23
24 typedef unsigned short unicode;
25
26 static long int ctoi(unsigned char *s, int *is_unicode);
27 int old_screen_map_read_ascii(FILE * fp, unsigned char buf[]);
28 int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode);
29 unicode utf8_to_ucs2(char *buf);
30 int screen_map_load(int fd, FILE * fp);
31
32 int loadacm_main(int argc, char **argv)
33 {
34         int fd;
35
36         if (argc>=2 && *argv[1]=='-') {
37                 usage(loadacm_usage);
38         }
39
40         fd = open("/dev/tty", O_RDWR);
41         if (fd < 0) {
42                 perror_msg_and_die("Error opening /dev/tty1");
43         }
44
45         if (screen_map_load(fd, stdin)) {
46                 perror_msg_and_die("Error loading acm");
47         }
48
49         write(fd, "\033(K", 3);
50
51         return EXIT_SUCCESS;
52 }
53
54 int screen_map_load(int fd, FILE * fp)
55 {
56         struct stat stbuf;
57         unicode wbuf[E_TABSZ];
58         unsigned char buf[E_TABSZ];
59         int parse_failed = 0;
60         int is_unicode;
61
62         if (fstat(fileno(fp), &stbuf))
63                 perror("Cannot stat map file"), exit(1);
64
65         /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */
66         if (!
67                 (parse_failed =
68                  (-1 == uni_screen_map_read_ascii(fp, wbuf, &is_unicode)))
69 || (S_ISREG(stbuf.st_mode) && (stbuf.st_size == (sizeof(unicode) * E_TABSZ)))) {        /* test for binary UTF map by size */
70                 if (parse_failed) {
71                         if (-1 == fseek(fp, 0, SEEK_SET)) {
72                                 if (errno == ESPIPE)
73                                         error_msg("16bit screen-map MUST be a regular file.\n"),
74                                                 exit(1);
75                                 else
76                                         perror("fseek failed reading binary 16bit screen-map"),
77                                                 exit(1);
78                         }
79
80                         if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1)
81                                 perror("Cannot read [new] map from file"), exit(1);
82 #if 0
83                         else
84                                 error_msg("Input screen-map is binary.\n");
85 #endif
86                 }
87
88                 /* if it was effectively a 16-bit ASCII, OK, else try to read as 8-bit map */
89                 /* same if it was binary, ie. if parse_failed */
90                 if (parse_failed || is_unicode) {
91                         if (ioctl(fd, PIO_UNISCRNMAP, wbuf))
92                                 perror("PIO_UNISCRNMAP ioctl"), exit(1);
93                         else
94                                 return 0;
95                 }
96         }
97
98         /* rewind... */
99         if (-1 == fseek(fp, 0, SEEK_SET)) {
100                 if (errno == ESPIPE)
101                         error_msg("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);
105         }
106
107         /* ... 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                                         error_msg("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);
122 #if 0
123                         else
124                                 error_msg("Input screen-map is binary.\n");
125 #endif
126                 }
127
128                 if (ioctl(fd, PIO_SCRNMAP, buf))
129                         perror("PIO_SCRNMAP ioctl"), exit(1);
130                 else
131                         return 0;
132         }
133         error_msg("Error parsing symbolic map\n");
134         return(1);
135 }
136
137
138 /*
139  * - reads `fp' as a 16-bit ASCII SFM file.
140  * - returns -1 on error.
141  * - returns it in `unicode' in an E_TABSZ-elements array.
142  * - sets `*is_unicode' flagiff there were any non-8-bit
143  *   (ie. real 16-bit) mapping.
144  *
145  * FIXME: ignores everything after second word
146  */
147 int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
148 {
149         char buffer[256];                       /* line buffer reading file */
150         char *p, *q;                            /* 1st + 2nd words in line */
151         int in, on;                                     /* the same, as numbers */
152         int tmp_is_unicode;                     /* tmp for is_unicode calculation */
153         int i;                                          /* loop index - result holder */
154         int ret_code = 0;                       /* return code */
155         sigset_t sigset, old_sigset;
156
157         assert(is_unicode);
158
159         *is_unicode = 0;
160
161         /* first 128 codes defaults to ASCII */
162         for (i = 0; i < 128; i++)
163                 buf[i] = i;
164         /* remaining defaults to replacement char (usually E_TABSZ = 256) */
165         for (; i < E_TABSZ; i++)
166                 buf[i] = 0xfffd;
167
168         /* block SIGCHLD */
169         sigemptyset(&sigset);
170         sigaddset(&sigset, SIGCHLD);
171         sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
172
173         do {
174                 if (NULL == fgets(buffer, sizeof(buffer), fp)) {
175                         if (feof(fp))
176                                 break;
177                         else {
178                                 perror("uni_screen_map_read_ascii() can't read line");
179                                 exit(2);
180                         }
181                 }
182
183                 /* get "charset-relative charcode", stripping leading spaces */
184                 p = strtok(buffer, " \t\n");
185
186                 /* skip empty lines and comments */
187                 if (!p || *p == '#')
188                         continue;
189
190                 /* get unicode mapping */
191                 q = strtok(NULL, " \t\n");
192                 if (q) {
193                         in = ctoi(p, NULL);
194                         if (in < 0 || in > 255) {
195                                 ret_code = -1;
196                                 break;
197                         }
198
199                         on = ctoi(q, &tmp_is_unicode);
200                         if (in < 0 && on > 65535) {
201                                 ret_code = -1;
202                                 break;
203                         }
204
205                         *is_unicode |= tmp_is_unicode;
206                         buf[in] = on;
207                 } else {
208                         ret_code = -1;
209                         break;
210                 }
211         }
212         while (1);                                      /* terminated by break on feof() */
213
214         /* restore sig mask */
215         sigprocmask(SIG_SETMASK, &old_sigset, NULL);
216
217         return ret_code;
218 }
219
220
221 int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
222 {
223         char buffer[256];
224         int in, on;
225         char *p, *q;
226
227         for (in = 0; in < 256; in++)
228                 buf[in] = in;
229
230         while (fgets(buffer, sizeof(buffer) - 1, fp)) {
231                 p = strtok(buffer, " \t\n");
232
233                 if (!p || *p == '#')
234                         continue;
235
236                 q = strtok(NULL, " \t\n#");
237                 if (q) {
238                         in = ctoi(p, NULL);
239                         if (in < 0 || in > 255)
240                                 return -1;
241
242                         on = ctoi(q, NULL);
243                         if (in < 0 && on > 255)
244                                 return -1;
245
246                         buf[in] = on;
247                 } else
248                         return -1;
249         }
250
251         return (0);
252 }
253
254
255 /*
256  * - converts a string into an int.
257  * - supports dec and hex bytes, hex UCS2, single-quoted byte and UTF8 chars.
258  * - returns the converted value
259  * - if `is_unicode != NULL', use it to tell whether it was unicode
260  *
261  * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones.
262  */
263 long int ctoi(unsigned char *s, int *is_unicode)
264 {
265         int i;
266         size_t ls;
267
268         ls = strlen(s);
269         if (is_unicode)
270                 *is_unicode = 0;
271
272         /* hex-specified UCS2 */
273         if ((strncmp(s, "U+", 2) == 0) &&
274                 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) {
275                 sscanf(s + 2, "%x", &i);
276                 if (is_unicode)
277                         *is_unicode = 1;
278         }
279
280         /* hex-specified byte */
281         else if ((ls <= 4) && (strncmp(s, "0x", 2) == 0) &&
282                          (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2))
283                 sscanf(s + 2, "%x", &i);
284
285         /* oct-specified number (byte) */
286         else if ((*s == '0') && (strspn(s, "01234567") == ls))
287                 sscanf(s, "%o", &i);
288
289         /* dec-specified number (byte) */
290         else if (strspn(s, "0123456789") == ls)
291                 sscanf(s, "%d", &i);
292
293         /* single-byte quoted char */
294         else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
295                 i = s[1];
296
297         /* multi-byte UTF8 quoted char */
298         else if ((s[0] == '\'') && (s[ls - 1] == '\'')) {
299                 s[ls - 1] = 0;                  /* ensure we'll not "parse UTF too far" */
300                 i = utf8_to_ucs2(s + 1);
301                 if (is_unicode)
302                         *is_unicode = 1;
303         } else
304                 return (-1);
305
306         return (i);
307 }
308
309
310 void saveoldmap(int fd, char *omfil)
311 {
312         FILE *fp;
313         char buf[E_TABSZ];
314
315 #ifdef GIO_UNISCRNMAP
316         unicode xbuf[E_TABSZ];
317         int is_old_map = 0;
318
319         if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) {
320                 perror("GIO_UNISCRNMAP ioctl error");
321 #endif
322                 if (ioctl(fd, GIO_SCRNMAP, buf)) {
323                         perror("GIO_SCRNMAP ioctl error");
324                         exit(1);
325                 } else
326                         is_old_map = 1;
327 #ifdef GIO_UNISCRNMAP
328         }
329 #endif
330
331         fp = xfopen(omfil, "w");
332 #ifdef GIO_UNISCRNMAP
333         if (is_old_map) {
334 #endif
335                 if (fwrite(buf, E_TABSZ, 1, fp) != 1) {
336                         perror("Error writing map to file");
337                         exit(1);
338                 }
339 #ifdef GIO_UNISCRNMAP
340         } else if (fwrite(xbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) {
341                 perror("Error writing map to file");
342                 exit(1);
343         }
344 #endif
345
346         fclose(fp);
347 }
348
349 unicode utf8_to_ucs2(char *buf)
350 {
351         int utf_count = 0;
352         long utf_char = 0;
353         unicode tc = 0;
354         unsigned char c;
355
356         do {
357                 c = *buf;
358                 buf++;
359
360                 /* if byte should be part of multi-byte sequence */
361                 if (c & 0x80) {
362                         /* if we have already started to parse a UTF8 sequence */
363                         if (utf_count > 0 && (c & 0xc0) == 0x80) {
364                                 utf_char = (utf_char << 6) | (c & 0x3f);
365                                 utf_count--;
366                                 if (utf_count == 0)
367                                         tc = utf_char;
368                                 else
369                                         continue;
370                         } else {                        /* Possibly 1st char of a UTF8 sequence */
371
372                                 if ((c & 0xe0) == 0xc0) {
373                                         utf_count = 1;
374                                         utf_char = (c & 0x1f);
375                                 } else if ((c & 0xf0) == 0xe0) {
376                                         utf_count = 2;
377                                         utf_char = (c & 0x0f);
378                                 } else if ((c & 0xf8) == 0xf0) {
379                                         utf_count = 3;
380                                         utf_char = (c & 0x07);
381                                 } else if ((c & 0xfc) == 0xf8) {
382                                         utf_count = 4;
383                                         utf_char = (c & 0x03);
384                                 } else if ((c & 0xfe) == 0xfc) {
385                                         utf_count = 5;
386                                         utf_char = (c & 0x01);
387                                 } else
388                                         utf_count = 0;
389                                 continue;
390                         }
391                 } else {                                /* not part of multi-byte sequence - treat as ASCII
392                                                                    * this makes incomplete sequences to be ignored
393                                                                  */
394                         tc = c;
395                         utf_count = 0;
396                 }
397         }
398         while (utf_count);
399
400         return tc;
401 }