3 * mapscrn.c - version 0.92
5 * Was taken from console-tools and adapted by
6 * Peter Novodvorsky <petya@logic.ru>
18 #include <sys/types.h>
20 #include <sys/ioctl.h>
23 typedef unsigned short unicode;
25 static long int ctoi(unsigned char *s, int *is_unicode);
26 int old_screen_map_read_ascii(FILE *fp, unsigned char buf[]);
27 int uni_screen_map_read_ascii(FILE *fp, unicode buf[], int* is_unicode);
28 unicode utf8_to_ucs2 (char* buf);
29 int screen_map_load(int fd, FILE *fp);
31 int loadacm_main(int argc, char **argv)
35 fd = open("/dev/tty", O_RDWR);
37 fprintf(stderr, "Error opening /dev/tty1: %s\n", strerror(errno));
41 if (screen_map_load(fd, stdin))
43 fprintf(stderr, "Error loading acm: %s\n", strerror(errno));
47 write(fd, "\033(K", 3);
52 int screen_map_load(int fd, FILE *fp)
55 unicode wbuf[E_TABSZ];
56 unsigned char buf[E_TABSZ];
60 if (fstat(fp->_fileno, &stbuf))
61 perror("Cannot stat map file"), exit(1);
63 /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */
64 if (!(parse_failed = (-1 == uni_screen_map_read_ascii(fp,wbuf,&is_unicode))) ||
65 (S_ISREG(stbuf.st_mode) &&
66 (stbuf.st_size == (sizeof(unicode) * E_TABSZ)))) /* test for binary UTF map by size */
70 if (-1 == fseek (fp, 0, SEEK_SET))
73 fprintf (stderr, "16bit screen-map MUST be a regular file.\n"), exit (1);
75 perror ("fseek failed reading binary 16bit screen-map"), exit (1);
78 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1)
79 perror("Cannot read [new] map from file"), exit(1);
82 fprintf(stderr, "Input screen-map is binary.\n");
86 /* if it was effectively a 16-bit ASCII, OK, else try to read as 8-bit map */
87 /* same if it was binary, ie. if parse_failed */
88 if (parse_failed || is_unicode)
90 if (ioctl(fd,PIO_UNISCRNMAP,wbuf))
91 perror("PIO_UNISCRNMAP ioctl"), exit(1);
98 if (-1 == fseek (fp, 0, SEEK_SET))
101 fprintf (stderr, "Assuming 8bit screen-map - MUST be a regular file.\n"), exit (1);
103 perror ("fseek failed assuming 8bit screen-map"), exit (1);
106 /* ... and try an old 8-bit screen-map */
107 if (!(parse_failed = (-1 == old_screen_map_read_ascii(fp,buf))) ||
108 (S_ISREG(stbuf.st_mode) &&
109 (stbuf.st_size == E_TABSZ))) /* test for binary old 8-bit map by size */
113 if (-1 == fseek (fp, 0, SEEK_SET))
116 /* should not - it succedeed above */
117 fprintf (stderr, "fseek() returned ESPIPE !\n"), exit (1);
119 perror ("fseek for binary 8bit screen-map"), exit (1);
122 if (fread(buf,E_TABSZ,1,fp) != 1)
123 perror("Cannot read [old] map from file"), exit(1);
126 fprintf(stderr, "Input screen-map is binary.\n");
130 if (ioctl(fd,PIO_SCRNMAP,buf))
131 perror("PIO_SCRNMAP ioctl"), exit(1);
137 fprintf(stderr, "Error parsing symbolic map\n");
144 * - reads `fp' as a 16-bit ASCII SFM file.
145 * - returns -1 on error.
146 * - returns it in `unicode' in an E_TABSZ-elements array.
147 * - sets `*is_unicode' flagiff there were any non-8-bit
148 * (ie. real 16-bit) mapping.
150 * FIXME: ignores everything after second word
152 int uni_screen_map_read_ascii(FILE *fp, unicode buf[], int *is_unicode)
154 char buffer[256]; /* line buffer reading file */
155 char *p, *q; /* 1st + 2nd words in line */
156 int in, on; /* the same, as numbers */
157 int tmp_is_unicode; /* tmp for is_unicode calculation */
158 int i; /* loop index - result holder */
159 int ret_code = 0; /* return code */
160 sigset_t sigset, old_sigset;
166 /* first 128 codes defaults to ASCII */
167 for (i=0; i<128; i++) buf[i] = i;
168 /* remaining defaults to replacement char (usually E_TABSZ = 256) */
169 for ( ; i<E_TABSZ; i++) buf[i] = 0xfffd;
172 sigemptyset (&sigset);
173 sigaddset (&sigset, SIGCHLD);
174 sigprocmask (SIG_BLOCK, &sigset, &old_sigset);
178 if (NULL == fgets(buffer, sizeof(buffer),fp))
184 perror ("uni_screen_map_read_ascii() can't read line");
189 /* get "charset-relative charcode", stripping leading spaces */
190 p = strtok(buffer," \t\n");
192 /* skip empty lines and comments */
196 /* get unicode mapping */
197 q = strtok(NULL," \t\n");
201 if (in < 0 || in > 255)
207 on = ctoi(q, &tmp_is_unicode);
208 if (in < 0 && on > 65535)
214 *is_unicode |= tmp_is_unicode;
223 while (1); /* terminated by break on feof() */
225 /* restore sig mask */
226 sigprocmask (SIG_SETMASK, &old_sigset, NULL);
232 int old_screen_map_read_ascii(FILE *fp, unsigned char buf[])
238 for (in=0; in<256; in++) buf[in]=in;
240 while (fgets(buffer,sizeof(buffer)-1,fp))
242 p = strtok(buffer," \t\n");
247 q = strtok(NULL," \t\n#");
251 if (in < 0 || in > 255) return -1;
254 if (in < 0 && on > 255) return -1;
266 * - converts a string into an int.
267 * - supports dec and hex bytes, hex UCS2, single-quoted byte and UTF8 chars.
268 * - returns the converted value
269 * - if `is_unicode != NULL', use it to tell whether it was unicode
271 * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones.
273 long int ctoi(unsigned char *s, int *is_unicode)
279 if (is_unicode) *is_unicode = 0;
281 /* hex-specified UCS2 */
282 if ((strncmp(s,"U+",2) == 0) &&
283 (strspn(s+2,"0123456789abcdefABCDEF") == ls-2))
286 if (is_unicode) *is_unicode = 1;
289 /* hex-specified byte */
290 else if ((ls <= 4) && (strncmp(s,"0x",2) == 0) &&
291 (strspn(s+2,"0123456789abcdefABCDEF") == ls-2))
294 /* oct-specified number (byte) */
295 else if ((*s == '0') &&
296 (strspn(s,"01234567") == ls))
299 /* dec-specified number (byte) */
300 else if (strspn(s,"0123456789") == ls)
303 /* single-byte quoted char */
304 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
307 /* multi-byte UTF8 quoted char */
308 else if ((s[0] == '\'') && (s[ls-1] == '\''))
310 s[ls-1] = 0; /* ensure we'll not "parse UTF too far" */
311 i = utf8_to_ucs2(s+1);
312 if (is_unicode) *is_unicode = 1;
321 void saveoldmap(int fd, char *omfil)
325 #ifdef GIO_UNISCRNMAP
326 unicode xbuf[E_TABSZ];
329 if (ioctl(fd,GIO_UNISCRNMAP,xbuf))
331 perror("GIO_UNISCRNMAP ioctl error");
333 if (ioctl(fd,GIO_SCRNMAP,buf))
335 perror("GIO_SCRNMAP ioctl error");
340 #ifdef GIO_UNISCRNMAP
344 if ((fp = fopen(omfil, "w")) == NULL)
350 #ifdef GIO_UNISCRNMAP
354 if (fwrite(buf,E_TABSZ,1,fp) != 1)
356 perror("Error writing map to file");
359 #ifdef GIO_UNISCRNMAP
362 if (fwrite(xbuf, sizeof(unicode) * E_TABSZ,1,fp) != 1)
364 perror("Error writing map to file");
372 unicode utf8_to_ucs2 (char* buf)
384 /* if byte should be part of multi-byte sequence */
387 /* if we have already started to parse a UTF8 sequence */
388 if (utf_count > 0 && (c & 0xc0) == 0x80)
390 utf_char = (utf_char << 6) | (c & 0x3f);
397 else /* Possibly 1st char of a UTF8 sequence */
399 if ((c & 0xe0) == 0xc0)
402 utf_char = (c & 0x1f);
404 else if ((c & 0xf0) == 0xe0)
407 utf_char = (c & 0x0f);
409 else if ((c & 0xf8) == 0xf0)
412 utf_char = (c & 0x07);
414 else if ((c & 0xfc) == 0xf8)
417 utf_char = (c & 0x03);
419 else if ((c & 0xfe) == 0xfc)
422 utf_char = (c & 0x01);
429 else /* not part of multi-byte sequence - treat as ASCII
430 * this makes incomplete sequences to be ignored