1 /* vi: set sw=4 ts=4: */
4 * mapscrn.c - version 0.92
6 * Was taken from console-tools and adapted by
7 * Peter Novodvorsky <petya@logic.ru>
20 #include <sys/types.h>
22 #include <sys/ioctl.h>
25 static const char loadacm_usage[] = "loadacm\n"
26 #ifndef BB_FEATURE_TRIVIAL_HELP
27 "\nLoads an acm from standard input.\n"
31 typedef unsigned short unicode;
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);
39 int loadacm_main(int argc, char **argv)
43 if (argc>=2 && *argv[1]=='-') {
47 fd = open("/dev/tty", O_RDWR);
49 fprintf(stderr, "Error opening /dev/tty1: %s\n", strerror(errno));
53 if (screen_map_load(fd, stdin)) {
54 fprintf(stderr, "Error loading acm: %s\n", strerror(errno));
58 write(fd, "\033(K", 3);
63 int screen_map_load(int fd, FILE * fp)
66 unicode wbuf[E_TABSZ];
67 unsigned char buf[E_TABSZ];
71 if (fstat(fp->_fileno, &stbuf))
72 perror("Cannot stat map file"), exit(1);
74 /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */
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 */
80 if (-1 == fseek(fp, 0, SEEK_SET)) {
83 "16bit screen-map MUST be a regular file.\n"),
86 perror("fseek failed reading binary 16bit screen-map"),
90 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1)
91 perror("Cannot read [new] map from file"), exit(1);
94 fprintf(stderr, "Input screen-map is binary.\n");
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);
109 if (-1 == fseek(fp, 0, SEEK_SET)) {
112 "Assuming 8bit screen-map - MUST be a regular file.\n"),
115 perror("fseek failed assuming 8bit screen-map"), exit(1);
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 */
122 if (-1 == fseek(fp, 0, SEEK_SET)) {
124 /* should not - it succedeed above */
125 fprintf(stderr, "fseek() returned ESPIPE !\n"),
128 perror("fseek for binary 8bit screen-map"), exit(1);
131 if (fread(buf, E_TABSZ, 1, fp) != 1)
132 perror("Cannot read [old] map from file"), exit(1);
135 fprintf(stderr, "Input screen-map is binary.\n");
139 if (ioctl(fd, PIO_SCRNMAP, buf))
140 perror("PIO_SCRNMAP ioctl"), exit(1);
144 fprintf(stderr, "Error parsing symbolic map\n");
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.
157 * FIXME: ignores everything after second word
159 int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
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;
173 /* first 128 codes defaults to ASCII */
174 for (i = 0; i < 128; i++)
176 /* remaining defaults to replacement char (usually E_TABSZ = 256) */
177 for (; i < E_TABSZ; i++)
181 sigemptyset(&sigset);
182 sigaddset(&sigset, SIGCHLD);
183 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
186 if (NULL == fgets(buffer, sizeof(buffer), fp)) {
190 perror("uni_screen_map_read_ascii() can't read line");
195 /* get "charset-relative charcode", stripping leading spaces */
196 p = strtok(buffer, " \t\n");
198 /* skip empty lines and comments */
202 /* get unicode mapping */
203 q = strtok(NULL, " \t\n");
206 if (in < 0 || in > 255) {
211 on = ctoi(q, &tmp_is_unicode);
212 if (in < 0 && on > 65535) {
217 *is_unicode |= tmp_is_unicode;
224 while (1); /* terminated by break on feof() */
226 /* restore sig mask */
227 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
233 int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
239 for (in = 0; in < 256; in++)
242 while (fgets(buffer, sizeof(buffer) - 1, fp)) {
243 p = strtok(buffer, " \t\n");
248 q = strtok(NULL, " \t\n#");
251 if (in < 0 || in > 255)
255 if (in < 0 && on > 255)
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
273 * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones.
275 long int ctoi(unsigned char *s, int *is_unicode)
284 /* hex-specified UCS2 */
285 if ((strncmp(s, "U+", 2) == 0) &&
286 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) {
287 sscanf(s + 2, "%x", &i);
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);
297 /* oct-specified number (byte) */
298 else if ((*s == '0') && (strspn(s, "01234567") == ls))
301 /* dec-specified number (byte) */
302 else if (strspn(s, "0123456789") == ls)
305 /* single-byte quoted char */
306 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
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);
322 void saveoldmap(int fd, char *omfil)
327 #ifdef GIO_UNISCRNMAP
328 unicode xbuf[E_TABSZ];
331 if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) {
332 perror("GIO_UNISCRNMAP ioctl error");
334 if (ioctl(fd, GIO_SCRNMAP, buf)) {
335 perror("GIO_SCRNMAP ioctl error");
339 #ifdef GIO_UNISCRNMAP
343 if ((fp = fopen(omfil, "w")) == NULL) {
347 #ifdef GIO_UNISCRNMAP
350 if (fwrite(buf, E_TABSZ, 1, fp) != 1) {
351 perror("Error writing map to file");
354 #ifdef GIO_UNISCRNMAP
355 } else if (fwrite(xbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) {
356 perror("Error writing map to file");
364 unicode utf8_to_ucs2(char *buf)
375 /* if byte should be part of multi-byte sequence */
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);
385 } else { /* Possibly 1st char of a UTF8 sequence */
387 if ((c & 0xe0) == 0xc0) {
389 utf_char = (c & 0x1f);
390 } else if ((c & 0xf0) == 0xe0) {
392 utf_char = (c & 0x0f);
393 } else if ((c & 0xf8) == 0xf0) {
395 utf_char = (c & 0x07);
396 } else if ((c & 0xfc) == 0xf8) {
398 utf_char = (c & 0x03);
399 } else if ((c & 0xfe) == 0xfc) {
401 utf_char = (c & 0x01);
406 } else { /* not part of multi-byte sequence - treat as ASCII
407 * this makes incomplete sequences to be ignored