Lots of updates. Finished implementing BB_FEATURE_TRIVIAL_HELP
[oweals/busybox.git] / 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 "internal.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/stat.h>
22 #include <sys/ioctl.h>
23 #include <sys/kd.h>
24
25 static const char loadacm_usage[] = "loadacm\n"
26 #ifndef BB_FEATURE_TRIVIAL_HELP
27         "\nLoads an acm from standard input.\n"
28 #endif
29         ;
30
31 typedef unsigned short unicode;
32
33 static long int ctoi(unsigned char *s, int *is_unicode);
34 int old_screen_map_read_ascii(FILE * fp, unsigned char buf[]);
35 int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode);
36 unicode utf8_to_ucs2(char *buf);
37 int screen_map_load(int fd, FILE * fp);
38
39 int loadacm_main(int argc, char **argv)
40 {
41         int fd;
42
43         if (argc>=2 && *argv[1]=='-') {
44                 usage(loadacm_usage);
45         }
46
47         fd = open("/dev/tty", O_RDWR);
48         if (fd < 0) {
49                 fprintf(stderr, "Error opening /dev/tty1: %s\n", strerror(errno));
50                 return( FALSE);
51         }
52
53         if (screen_map_load(fd, stdin)) {
54                 fprintf(stderr, "Error loading acm: %s\n", strerror(errno));
55                 return( FALSE);
56         }
57
58         write(fd, "\033(K", 3);
59
60         return( TRUE);
61 }
62
63 int screen_map_load(int fd, FILE * fp)
64 {
65         struct stat stbuf;
66         unicode wbuf[E_TABSZ];
67         unsigned char buf[E_TABSZ];
68         int parse_failed = 0;
69         int is_unicode;
70
71         if (fstat(fp->_fileno, &stbuf))
72                 perror("Cannot stat map file"), exit(1);
73
74         /* 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);
92 #if 0
93                         else
94                                 fprintf(stderr, "Input screen-map is binary.\n");
95 #endif
96                 }
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                 }
106         }
107
108         /* 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);
116         }
117
118         /* ... 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);
133 #if 0
134                         else
135                                 fprintf(stderr, "Input screen-map is binary.\n");
136 #endif
137                 }
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);
146         }
147 }
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  */
159 int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
160 {
161         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;
168
169         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                 }
223         }
224         while (1);                                      /* terminated by break on feof() */
225
226         /* restore sig mask */
227         sigprocmask(SIG_SETMASK, &old_sigset, NULL);
228
229         return ret_code;
230 }
231
232
233 int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
234 {
235         char buffer[256];
236         int in, on;
237         char *p, *q;
238
239         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;
261         }
262
263         return (0);
264 }
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  */
275 long int ctoi(unsigned char *s, int *is_unicode)
276 {
277         int i;
278         size_t ls;
279
280         ls = strlen(s);
281         if (is_unicode)
282                 *is_unicode = 0;
283
284         /* 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);
319 }
320
321
322 void saveoldmap(int fd, char *omfil)
323 {
324         FILE *fp;
325         char buf[E_TABSZ];
326
327 #ifdef GIO_UNISCRNMAP
328         unicode xbuf[E_TABSZ];
329         int is_old_map = 0;
330
331         if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) {
332                 perror("GIO_UNISCRNMAP ioctl error");
333 #endif
334                 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);
346         }
347 #ifdef GIO_UNISCRNMAP
348         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);
358         }
359 #endif
360
361         fclose(fp);
362 }
363
364 unicode utf8_to_ucs2(char *buf)
365 {
366         int utf_count = 0;
367         long utf_char = 0;
368         unicode tc = 0;
369         unsigned char c;
370
371         do {
372                 c = *buf;
373                 buf++;
374
375                 /* 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 */
386
387                                 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;
416 }