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>
21 #include <sys/ioctl.h>
24 typedef unsigned short unicode;
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);
32 int loadacm_main(int argc, char **argv)
36 if (argc>=2 && *argv[1]=='-') {
40 fd = open("/dev/tty", O_RDWR);
42 perror_msg_and_die("Error opening /dev/tty1");
45 if (screen_map_load(fd, stdin)) {
46 perror_msg_and_die("Error loading acm");
49 write(fd, "\033(K", 3);
54 int screen_map_load(int fd, FILE * fp)
57 unicode wbuf[E_TABSZ];
58 unsigned char buf[E_TABSZ];
62 if (fstat(fileno(fp), &stbuf))
63 perror_msg_and_die("Cannot stat map file");
65 /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */
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 */
71 if (-1 == fseek(fp, 0, SEEK_SET)) {
73 error_msg_and_die("16bit screen-map MUST be a regular file.");
75 perror_msg_and_die("fseek failed reading binary 16bit screen-map");
78 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1)
79 perror_msg_and_die("Cannot read [new] map from file");
82 error_msg("Input screen-map is binary.");
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) {
89 if (ioctl(fd, PIO_UNISCRNMAP, wbuf))
90 perror_msg_and_die("PIO_UNISCRNMAP ioctl");
97 if (-1 == fseek(fp, 0, SEEK_SET)) {
99 error_msg("Assuming 8bit screen-map - MUST be a regular file."),
102 perror_msg_and_die("fseek failed assuming 8bit screen-map");
105 /* ... and try an old 8-bit screen-map */
106 if (!(parse_failed = (-1 == old_screen_map_read_ascii(fp, buf))) ||
107 (S_ISREG(stbuf.st_mode) && (stbuf.st_size == E_TABSZ))) { /* test for binary old 8-bit map by size */
109 if (-1 == fseek(fp, 0, SEEK_SET)) {
111 /* should not - it succedeed above */
112 error_msg_and_die("fseek() returned ESPIPE !");
114 perror_msg_and_die("fseek for binary 8bit screen-map");
117 if (fread(buf, E_TABSZ, 1, fp) != 1)
118 perror_msg_and_die("Cannot read [old] map from file");
121 error_msg("Input screen-map is binary.");
125 if (ioctl(fd, PIO_SCRNMAP, buf))
126 perror_msg_and_die("PIO_SCRNMAP ioctl");
130 error_msg("Error parsing symbolic map");
136 * - reads `fp' as a 16-bit ASCII SFM file.
137 * - returns -1 on error.
138 * - returns it in `unicode' in an E_TABSZ-elements array.
139 * - sets `*is_unicode' flagiff there were any non-8-bit
140 * (ie. real 16-bit) mapping.
142 * FIXME: ignores everything after second word
144 int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
146 char buffer[256]; /* line buffer reading file */
147 char *p, *q; /* 1st + 2nd words in line */
148 int in, on; /* the same, as numbers */
149 int tmp_is_unicode; /* tmp for is_unicode calculation */
150 int i; /* loop index - result holder */
151 int ret_code = 0; /* return code */
152 sigset_t sigset, old_sigset;
158 /* first 128 codes defaults to ASCII */
159 for (i = 0; i < 128; i++)
161 /* remaining defaults to replacement char (usually E_TABSZ = 256) */
162 for (; i < E_TABSZ; i++)
166 sigemptyset(&sigset);
167 sigaddset(&sigset, SIGCHLD);
168 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
171 if (NULL == fgets(buffer, sizeof(buffer), fp)) {
175 perror_msg_and_die("uni_screen_map_read_ascii() can't read line");
178 /* get "charset-relative charcode", stripping leading spaces */
179 p = strtok(buffer, " \t\n");
181 /* skip empty lines and comments */
185 /* get unicode mapping */
186 q = strtok(NULL, " \t\n");
189 if (in < 0 || in > 255) {
194 on = ctoi(q, &tmp_is_unicode);
195 if (in < 0 && on > 65535) {
200 *is_unicode |= tmp_is_unicode;
207 while (1); /* terminated by break on feof() */
209 /* restore sig mask */
210 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
216 int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
222 for (in = 0; in < 256; in++)
225 while (fgets(buffer, sizeof(buffer) - 1, fp)) {
226 p = strtok(buffer, " \t\n");
231 q = strtok(NULL, " \t\n#");
234 if (in < 0 || in > 255)
238 if (in < 0 && on > 255)
251 * - converts a string into an int.
252 * - supports dec and hex bytes, hex UCS2, single-quoted byte and UTF8 chars.
253 * - returns the converted value
254 * - if `is_unicode != NULL', use it to tell whether it was unicode
256 * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones.
258 long int ctoi(unsigned char *s, int *is_unicode)
267 /* hex-specified UCS2 */
268 if ((strncmp(s, "U+", 2) == 0) &&
269 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) {
270 sscanf(s + 2, "%x", &i);
275 /* hex-specified byte */
276 else if ((ls <= 4) && (strncmp(s, "0x", 2) == 0) &&
277 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2))
278 sscanf(s + 2, "%x", &i);
280 /* oct-specified number (byte) */
281 else if ((*s == '0') && (strspn(s, "01234567") == ls))
284 /* dec-specified number (byte) */
285 else if (strspn(s, "0123456789") == ls)
288 /* single-byte quoted char */
289 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
292 /* multi-byte UTF8 quoted char */
293 else if ((s[0] == '\'') && (s[ls - 1] == '\'')) {
294 s[ls - 1] = 0; /* ensure we'll not "parse UTF too far" */
295 i = utf8_to_ucs2(s + 1);
305 void saveoldmap(int fd, char *omfil)
310 #ifdef GIO_UNISCRNMAP
311 unicode xbuf[E_TABSZ];
314 if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) {
315 perror_msg("GIO_UNISCRNMAP ioctl error");
317 if (ioctl(fd, GIO_SCRNMAP, buf))
318 perror_msg_and_die("GIO_SCRNMAP ioctl error");
321 #ifdef GIO_UNISCRNMAP
325 fp = xfopen(omfil, "w");
326 #ifdef GIO_UNISCRNMAP
329 if (fwrite(buf, E_TABSZ, 1, fp) != 1)
330 perror_msg_and_die("Error writing map to file");
331 #ifdef GIO_UNISCRNMAP
332 } else if (fwrite(xbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) {
333 perror_msg_and_die("Error writing map to file");
340 unicode utf8_to_ucs2(char *buf)
351 /* if byte should be part of multi-byte sequence */
353 /* if we have already started to parse a UTF8 sequence */
354 if (utf_count > 0 && (c & 0xc0) == 0x80) {
355 utf_char = (utf_char << 6) | (c & 0x3f);
361 } else { /* Possibly 1st char of a UTF8 sequence */
363 if ((c & 0xe0) == 0xc0) {
365 utf_char = (c & 0x1f);
366 } else if ((c & 0xf0) == 0xe0) {
368 utf_char = (c & 0x0f);
369 } else if ((c & 0xf8) == 0xf0) {
371 utf_char = (c & 0x07);
372 } else if ((c & 0xfc) == 0xf8) {
374 utf_char = (c & 0x03);
375 } else if ((c & 0xfe) == 0xfc) {
377 utf_char = (c & 0x01);
382 } else { /* not part of multi-byte sequence - treat as ASCII
383 * this makes incomplete sequences to be ignored