Use errorMsg rather than fprintf.
[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 "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                 errorMsg("Error opening /dev/tty1: %s\n", strerror(errno));
50                 return( FALSE);
51         }
52
53         if (screen_map_load(fd, stdin)) {
54                 errorMsg("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(fileno(fp), &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                                         errorMsg("16bit screen-map MUST be a regular file.\n"),
83                                                 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);
91 #if 0
92                         else
93                                 errorMsg("Input screen-map is binary.\n");
94 #endif
95                 }
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                 }
105         }
106
107         /* rewind... */
108         if (-1 == fseek(fp, 0, SEEK_SET)) {
109                 if (errno == ESPIPE)
110                         errorMsg("Assuming 8bit screen-map - MUST be a regular file.\n"),
111                                 exit(1);
112                 else
113                         perror("fseek failed assuming 8bit screen-map"), exit(1);
114         }
115
116         /* ... 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 */
123                                         errorMsg("fseek() returned ESPIPE !\n"),
124                                                 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);
131 #if 0
132                         else
133                                 errorMsg("Input screen-map is binary.\n");
134 #endif
135                 }
136
137                 if (ioctl(fd, PIO_SCRNMAP, buf))
138                         perror("PIO_SCRNMAP ioctl"), exit(1);
139                 else
140                         return 0;
141         }
142         errorMsg("Error parsing symbolic map\n");
143         return(1);
144 }
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  */
156 int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
157 {
158         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;
165
166         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                 }
220         }
221         while (1);                                      /* terminated by break on feof() */
222
223         /* restore sig mask */
224         sigprocmask(SIG_SETMASK, &old_sigset, NULL);
225
226         return ret_code;
227 }
228
229
230 int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
231 {
232         char buffer[256];
233         int in, on;
234         char *p, *q;
235
236         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;
258         }
259
260         return (0);
261 }
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  */
272 long int ctoi(unsigned char *s, int *is_unicode)
273 {
274         int i;
275         size_t ls;
276
277         ls = strlen(s);
278         if (is_unicode)
279                 *is_unicode = 0;
280
281         /* 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);
316 }
317
318
319 void saveoldmap(int fd, char *omfil)
320 {
321         FILE *fp;
322         char buf[E_TABSZ];
323
324 #ifdef GIO_UNISCRNMAP
325         unicode xbuf[E_TABSZ];
326         int is_old_map = 0;
327
328         if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) {
329                 perror("GIO_UNISCRNMAP ioctl error");
330 #endif
331                 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);
343         }
344 #ifdef GIO_UNISCRNMAP
345         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);
355         }
356 #endif
357
358         fclose(fp);
359 }
360
361 unicode utf8_to_ucs2(char *buf)
362 {
363         int utf_count = 0;
364         long utf_char = 0;
365         unicode tc = 0;
366         unsigned char c;
367
368         do {
369                 c = *buf;
370                 buf++;
371
372                 /* 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 */
383
384                                 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;
413 }