aboutsummaryrefslogtreecommitdiff
path: root/loadacm.c
diff options
context:
space:
mode:
Diffstat (limited to 'loadacm.c')
-rw-r--r--loadacm.c664
1 files changed, 314 insertions, 350 deletions
diff --git a/loadacm.c b/loadacm.c
index 491bc5ce8..072562f6b 100644
--- a/loadacm.c
+++ b/loadacm.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Derived from 3 * Derived from
3 * mapscrn.c - version 0.92 4 * mapscrn.c - version 0.92
@@ -23,120 +24,115 @@
23typedef unsigned short unicode; 24typedef unsigned short unicode;
24 25
25static long int ctoi(unsigned char *s, int *is_unicode); 26static long int ctoi(unsigned char *s, int *is_unicode);
26int old_screen_map_read_ascii(FILE *fp, unsigned char buf[]); 27int old_screen_map_read_ascii(FILE * fp, unsigned char buf[]);
27int uni_screen_map_read_ascii(FILE *fp, unicode buf[], int* is_unicode); 28int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode);
28unicode utf8_to_ucs2 (char* buf); 29unicode utf8_to_ucs2(char *buf);
29int screen_map_load(int fd, FILE *fp); 30int screen_map_load(int fd, FILE * fp);
30 31
31int loadacm_main(int argc, char **argv) 32int loadacm_main(int argc, char **argv)
32{ 33{
33 int fd; 34 int fd;
34 35
35 fd = open("/dev/tty", O_RDWR); 36 fd = open("/dev/tty", O_RDWR);
36 if (fd < 0) { 37 if (fd < 0) {
37 fprintf(stderr, "Error opening /dev/tty1: %s\n", strerror(errno)); 38 fprintf(stderr, "Error opening /dev/tty1: %s\n", strerror(errno));
38 return 1; 39 return 1;
39 } 40 }
40 41
41 if (screen_map_load(fd, stdin)) 42 if (screen_map_load(fd, stdin)) {
42 { 43 fprintf(stderr, "Error loading acm: %s\n", strerror(errno));
43 fprintf(stderr, "Error loading acm: %s\n", strerror(errno)); 44 return 1;
44 return 1; 45 }
45 } 46
46 47 write(fd, "\033(K", 3);
47 write(fd, "\033(K", 3); 48
48 49 return 0;
49 return 0;
50} 50}
51 51
52int screen_map_load(int fd, FILE *fp) 52int screen_map_load(int fd, FILE * fp)
53{ 53{
54 struct stat stbuf; 54 struct stat stbuf;
55 unicode wbuf[E_TABSZ]; 55 unicode wbuf[E_TABSZ];
56 unsigned char buf[E_TABSZ]; 56 unsigned char buf[E_TABSZ];
57 int parse_failed = 0; 57 int parse_failed = 0;
58 int is_unicode; 58 int is_unicode;
59 59
60 if (fstat(fp->_fileno, &stbuf)) 60 if (fstat(fp->_fileno, &stbuf))
61 perror("Cannot stat map file"), exit(1); 61 perror("Cannot stat map file"), exit(1);
62 62
63 /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */ 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))) || 64 if (!
65 (S_ISREG(stbuf.st_mode) && 65 (parse_failed =
66 (stbuf.st_size == (sizeof(unicode) * E_TABSZ)))) /* test for binary UTF map by size */ 66 (-1 == uni_screen_map_read_ascii(fp, wbuf, &is_unicode)))
67 { 67|| (S_ISREG(stbuf.st_mode) && (stbuf.st_size == (sizeof(unicode) * E_TABSZ)))) { /* test for binary UTF map by size */
68 if (parse_failed) 68 if (parse_failed) {
69 { 69 if (-1 == fseek(fp, 0, SEEK_SET)) {
70 if (-1 == fseek (fp, 0, SEEK_SET)) 70 if (errno == ESPIPE)
71 { 71 fprintf(stderr,
72 if (errno == ESPIPE) 72 "16bit screen-map MUST be a regular file.\n"),
73 fprintf (stderr, "16bit screen-map MUST be a regular file.\n"), exit (1); 73 exit(1);
74 else 74 else
75 perror ("fseek failed reading binary 16bit screen-map"), exit (1); 75 perror("fseek failed reading binary 16bit screen-map"),
76 } 76 exit(1);
77 77 }
78 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) 78
79 perror("Cannot read [new] map from file"), exit(1); 79 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1)
80 perror("Cannot read [new] map from file"), exit(1);
80#if 0 81#if 0
81 else 82 else
82 fprintf(stderr, "Input screen-map is binary.\n"); 83 fprintf(stderr, "Input screen-map is binary.\n");
83#endif 84#endif
85 }
86
87 /* if it was effectively a 16-bit ASCII, OK, else try to read as 8-bit map */
88 /* same if it was binary, ie. if parse_failed */
89 if (parse_failed || is_unicode) {
90 if (ioctl(fd, PIO_UNISCRNMAP, wbuf))
91 perror("PIO_UNISCRNMAP ioctl"), exit(1);
92 else
93 return 0;
94 }
84 } 95 }
85 96
86 /* if it was effectively a 16-bit ASCII, OK, else try to read as 8-bit map */ 97 /* rewind... */
87 /* same if it was binary, ie. if parse_failed */ 98 if (-1 == fseek(fp, 0, SEEK_SET)) {
88 if (parse_failed || is_unicode) 99 if (errno == ESPIPE)
89 { 100 fprintf(stderr,
90 if (ioctl(fd,PIO_UNISCRNMAP,wbuf)) 101 "Assuming 8bit screen-map - MUST be a regular file.\n"),
91 perror("PIO_UNISCRNMAP ioctl"), exit(1); 102 exit(1);
92 else 103 else
93 return 0; 104 perror("fseek failed assuming 8bit screen-map"), exit(1);
94 } 105 }
95 } 106
96 107 /* ... and try an old 8-bit screen-map */
97 /* rewind... */ 108 if (!(parse_failed = (-1 == old_screen_map_read_ascii(fp, buf))) ||
98 if (-1 == fseek (fp, 0, SEEK_SET)) 109 (S_ISREG(stbuf.st_mode) && (stbuf.st_size == E_TABSZ))) { /* test for binary old 8-bit map by size */
99 { 110 if (parse_failed) {
100 if (errno == ESPIPE) 111 if (-1 == fseek(fp, 0, SEEK_SET)) {
101 fprintf (stderr, "Assuming 8bit screen-map - MUST be a regular file.\n"), exit (1); 112 if (errno == ESPIPE)
102 else 113 /* should not - it succedeed above */
103 perror ("fseek failed assuming 8bit screen-map"), exit (1); 114 fprintf(stderr, "fseek() returned ESPIPE !\n"),
104 } 115 exit(1);
105 116 else
106 /* ... and try an old 8-bit screen-map */ 117 perror("fseek for binary 8bit screen-map"), exit(1);
107 if (!(parse_failed = (-1 == old_screen_map_read_ascii(fp,buf))) || 118 }
108 (S_ISREG(stbuf.st_mode) && 119
109 (stbuf.st_size == E_TABSZ))) /* test for binary old 8-bit map by size */ 120 if (fread(buf, E_TABSZ, 1, fp) != 1)
110 { 121 perror("Cannot read [old] map from file"), exit(1);
111 if (parse_failed)
112 {
113 if (-1 == fseek (fp, 0, SEEK_SET))
114 {
115 if (errno == ESPIPE)
116 /* should not - it succedeed above */
117 fprintf (stderr, "fseek() returned ESPIPE !\n"), exit (1);
118 else
119 perror ("fseek for binary 8bit screen-map"), exit (1);
120 }
121
122 if (fread(buf,E_TABSZ,1,fp) != 1)
123 perror("Cannot read [old] map from file"), exit(1);
124#if 0 122#if 0
125 else 123 else
126 fprintf(stderr, "Input screen-map is binary.\n"); 124 fprintf(stderr, "Input screen-map is binary.\n");
127#endif 125#endif
126 }
127
128 if (ioctl(fd, PIO_SCRNMAP, buf))
129 perror("PIO_SCRNMAP ioctl"), exit(1);
130 else
131 return 0;
132 } else {
133 fprintf(stderr, "Error parsing symbolic map\n");
134 exit(1);
128 } 135 }
129
130 if (ioctl(fd,PIO_SCRNMAP,buf))
131 perror("PIO_SCRNMAP ioctl"), exit(1);
132 else
133 return 0;
134 }
135 else
136 {
137 fprintf(stderr, "Error parsing symbolic map\n");
138 exit(1);
139 }
140} 136}
141 137
142 138
@@ -149,116 +145,111 @@ int screen_map_load(int fd, FILE *fp)
149 * 145 *
150 * FIXME: ignores everything after second word 146 * FIXME: ignores everything after second word
151 */ 147 */
152int uni_screen_map_read_ascii(FILE *fp, unicode buf[], int *is_unicode) 148int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
153{ 149{
154 char buffer[256]; /* line buffer reading file */ 150 char buffer[256]; /* line buffer reading file */
155 char *p, *q; /* 1st + 2nd words in line */ 151 char *p, *q; /* 1st + 2nd words in line */
156 int in, on; /* the same, as numbers */ 152 int in, on; /* the same, as numbers */
157 int tmp_is_unicode; /* tmp for is_unicode calculation */ 153 int tmp_is_unicode; /* tmp for is_unicode calculation */
158 int i; /* loop index - result holder */ 154 int i; /* loop index - result holder */
159 int ret_code = 0; /* return code */ 155 int ret_code = 0; /* return code */
160 sigset_t sigset, old_sigset; 156 sigset_t sigset, old_sigset;
161 157
162 assert (is_unicode); 158 assert(is_unicode);
163 159
164 *is_unicode = 0; 160 *is_unicode = 0;
165 161
166 /* first 128 codes defaults to ASCII */ 162 /* first 128 codes defaults to ASCII */
167 for (i=0; i<128; i++) buf[i] = i; 163 for (i = 0; i < 128; i++)
168 /* remaining defaults to replacement char (usually E_TABSZ = 256) */ 164 buf[i] = i;
169 for ( ; i<E_TABSZ; i++) buf[i] = 0xfffd; 165 /* remaining defaults to replacement char (usually E_TABSZ = 256) */
170 166 for (; i < E_TABSZ; i++)
171 /* block SIGCHLD */ 167 buf[i] = 0xfffd;
172 sigemptyset (&sigset); 168
173 sigaddset (&sigset, SIGCHLD); 169 /* block SIGCHLD */
174 sigprocmask (SIG_BLOCK, &sigset, &old_sigset); 170 sigemptyset(&sigset);
175 171 sigaddset(&sigset, SIGCHLD);
176 do 172 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
177 { 173
178 if (NULL == fgets(buffer, sizeof(buffer),fp)) 174 do {
179 { 175 if (NULL == fgets(buffer, sizeof(buffer), fp)) {
180 if (feof (fp)) 176 if (feof(fp))
181 break; 177 break;
182 else 178 else {
183 { 179 perror("uni_screen_map_read_ascii() can't read line");
184 perror ("uni_screen_map_read_ascii() can't read line"); 180 exit(2);
185 exit (2); 181 }
186 } 182 }
187 } 183
188 184 /* get "charset-relative charcode", stripping leading spaces */
189 /* get "charset-relative charcode", stripping leading spaces */ 185 p = strtok(buffer, " \t\n");
190 p = strtok(buffer," \t\n"); 186
191 187 /* skip empty lines and comments */
192 /* skip empty lines and comments */ 188 if (!p || *p == '#')
193 if (!p || *p == '#') 189 continue;
194 continue; 190
195 191 /* get unicode mapping */
196 /* get unicode mapping */ 192 q = strtok(NULL, " \t\n");
197 q = strtok(NULL," \t\n"); 193 if (q) {
198 if (q) 194 in = ctoi(p, NULL);
199 { 195 if (in < 0 || in > 255) {
200 in = ctoi(p, NULL); 196 ret_code = -1;
201 if (in < 0 || in > 255) 197 break;
202 { 198 }
203 ret_code = -1; 199
204 break; 200 on = ctoi(q, &tmp_is_unicode);
205 } 201 if (in < 0 && on > 65535) {
206 202 ret_code = -1;
207 on = ctoi(q, &tmp_is_unicode); 203 break;
208 if (in < 0 && on > 65535) 204 }
209 { 205
210 ret_code = -1; 206 *is_unicode |= tmp_is_unicode;
211 break; 207 buf[in] = on;
212 } 208 } else {
213 209 ret_code = -1;
214 *is_unicode |= tmp_is_unicode; 210 break;
215 buf[in] = on; 211 }
216 } 212 }
217 else 213 while (1); /* terminated by break on feof() */
218 {
219 ret_code = -1;
220 break;
221 }
222 }
223 while (1); /* terminated by break on feof() */
224 214
225 /* restore sig mask */ 215 /* restore sig mask */
226 sigprocmask (SIG_SETMASK, &old_sigset, NULL); 216 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
227 217
228 return ret_code; 218 return ret_code;
229} 219}
230 220
231 221
232int old_screen_map_read_ascii(FILE *fp, unsigned char buf[]) 222int old_screen_map_read_ascii(FILE * fp, unsigned char buf[])
233{ 223{
234 char buffer[256]; 224 char buffer[256];
235 int in, on; 225 int in, on;
236 char *p, *q; 226 char *p, *q;
237 227
238 for (in=0; in<256; in++) buf[in]=in; 228 for (in = 0; in < 256; in++)
239 229 buf[in] = in;
240 while (fgets(buffer,sizeof(buffer)-1,fp)) 230
241 { 231 while (fgets(buffer, sizeof(buffer) - 1, fp)) {
242 p = strtok(buffer," \t\n"); 232 p = strtok(buffer, " \t\n");
243 233
244 if (!p || *p == '#') 234 if (!p || *p == '#')
245 continue; 235 continue;
246 236
247 q = strtok(NULL," \t\n#"); 237 q = strtok(NULL, " \t\n#");
248 if (q) 238 if (q) {
249 { 239 in = ctoi(p, NULL);
250 in = ctoi(p, NULL); 240 if (in < 0 || in > 255)
251 if (in < 0 || in > 255) return -1; 241 return -1;
252 242
253 on = ctoi(q, NULL); 243 on = ctoi(q, NULL);
254 if (in < 0 && on > 255) return -1; 244 if (in < 0 && on > 255)
255 245 return -1;
256 buf[in] = on; 246
247 buf[in] = on;
248 } else
249 return -1;
257 } 250 }
258 else return -1; 251
259 } 252 return (0);
260
261 return(0);
262} 253}
263 254
264 255
@@ -270,172 +261,145 @@ int old_screen_map_read_ascii(FILE *fp, unsigned char buf[])
270 * 261 *
271 * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones. 262 * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones.
272 */ 263 */
273long int ctoi(unsigned char *s, int *is_unicode) 264long int ctoi(unsigned char *s, int *is_unicode)
274{ 265{
275 int i; 266 int i;
276 size_t ls; 267 size_t ls;
277 268
278 ls = strlen(s); 269 ls = strlen(s);
279 if (is_unicode) *is_unicode = 0; 270 if (is_unicode)
280 271 *is_unicode = 0;
281 /* hex-specified UCS2 */ 272
282 if ((strncmp(s,"U+",2) == 0) && 273 /* hex-specified UCS2 */
283 (strspn(s+2,"0123456789abcdefABCDEF") == ls-2)) 274 if ((strncmp(s, "U+", 2) == 0) &&
284 { 275 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) {
285 sscanf(s+2,"%x",&i); 276 sscanf(s + 2, "%x", &i);
286 if (is_unicode) *is_unicode = 1; 277 if (is_unicode)
287 } 278 *is_unicode = 1;
288 279 }
289 /* hex-specified byte */ 280
290 else if ((ls <= 4) && (strncmp(s,"0x",2) == 0) && 281 /* hex-specified byte */
291 (strspn(s+2,"0123456789abcdefABCDEF") == ls-2)) 282 else if ((ls <= 4) && (strncmp(s, "0x", 2) == 0) &&
292 sscanf(s+2,"%x",&i); 283 (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2))
293 284 sscanf(s + 2, "%x", &i);
294 /* oct-specified number (byte) */ 285
295 else if ((*s == '0') && 286 /* oct-specified number (byte) */
296 (strspn(s,"01234567") == ls)) 287 else if ((*s == '0') && (strspn(s, "01234567") == ls))
297 sscanf(s,"%o",&i); 288 sscanf(s, "%o", &i);
298 289
299 /* dec-specified number (byte) */ 290 /* dec-specified number (byte) */
300 else if (strspn(s,"0123456789") == ls) 291 else if (strspn(s, "0123456789") == ls)
301 sscanf(s,"%d",&i); 292 sscanf(s, "%d", &i);
302 293
303 /* single-byte quoted char */ 294 /* single-byte quoted char */
304 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\'')) 295 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
305 i=s[1]; 296 i = s[1];
306 297
307 /* multi-byte UTF8 quoted char */ 298 /* multi-byte UTF8 quoted char */
308 else if ((s[0] == '\'') && (s[ls-1] == '\'')) 299 else if ((s[0] == '\'') && (s[ls - 1] == '\'')) {
309 { 300 s[ls - 1] = 0; /* ensure we'll not "parse UTF too far" */
310 s[ls-1] = 0; /* ensure we'll not "parse UTF too far" */ 301 i = utf8_to_ucs2(s + 1);
311 i = utf8_to_ucs2(s+1); 302 if (is_unicode)
312 if (is_unicode) *is_unicode = 1; 303 *is_unicode = 1;
313 } 304 } else
314 else 305 return (-1);
315 return(-1); 306
316 307 return (i);
317 return(i);
318} 308}
319 309
320 310
321void saveoldmap(int fd, char *omfil) 311void saveoldmap(int fd, char *omfil)
322{ 312{
323 FILE *fp; 313 FILE *fp;
324 char buf[E_TABSZ]; 314 char buf[E_TABSZ];
315
325#ifdef GIO_UNISCRNMAP 316#ifdef GIO_UNISCRNMAP
326 unicode xbuf[E_TABSZ]; 317 unicode xbuf[E_TABSZ];
327 int is_old_map = 0; 318 int is_old_map = 0;
328 319
329 if (ioctl(fd,GIO_UNISCRNMAP,xbuf)) 320 if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) {
330 { 321 perror("GIO_UNISCRNMAP ioctl error");
331 perror("GIO_UNISCRNMAP ioctl error");
332#endif 322#endif
333 if (ioctl(fd,GIO_SCRNMAP,buf)) 323 if (ioctl(fd, GIO_SCRNMAP, buf)) {
334 { 324 perror("GIO_SCRNMAP ioctl error");
335 perror("GIO_SCRNMAP ioctl error"); 325 exit(1);
336 exit(1); 326 } else
337 } 327 is_old_map = 1;
338 else
339 is_old_map = 1;
340#ifdef GIO_UNISCRNMAP 328#ifdef GIO_UNISCRNMAP
341 } 329 }
342#endif 330#endif
343
344 if ((fp = fopen(omfil, "w")) == NULL)
345 {
346 perror(omfil);
347 exit(1);
348 }
349 331
332 if ((fp = fopen(omfil, "w")) == NULL) {
333 perror(omfil);
334 exit(1);
335 }
350#ifdef GIO_UNISCRNMAP 336#ifdef GIO_UNISCRNMAP
351 if (is_old_map) 337 if (is_old_map) {
352 {
353#endif 338#endif
354 if (fwrite(buf,E_TABSZ,1,fp) != 1) 339 if (fwrite(buf, E_TABSZ, 1, fp) != 1) {
355 { 340 perror("Error writing map to file");
356 perror("Error writing map to file"); 341 exit(1);
357 exit(1); 342 }
358 }
359#ifdef GIO_UNISCRNMAP 343#ifdef GIO_UNISCRNMAP
360 } 344 } else if (fwrite(xbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) {
361 else 345 perror("Error writing map to file");
362 if (fwrite(xbuf, sizeof(unicode) * E_TABSZ,1,fp) != 1) 346 exit(1);
363 {
364 perror("Error writing map to file");
365 exit(1);
366 } 347 }
367#endif 348#endif
368 349
369 fclose(fp); 350 fclose(fp);
370} 351}
371 352
372unicode utf8_to_ucs2 (char* buf) 353unicode utf8_to_ucs2(char *buf)
373{ 354{
374 int utf_count = 0; 355 int utf_count = 0;
375 long utf_char; 356 long utf_char;
376 unicode tc; 357 unicode tc;
377 unsigned char c; 358 unsigned char c;
378 359
379 do 360 do {
380 { 361 c = *buf;
381 c = *buf; 362 buf++;
382 buf++; 363
383 364 /* if byte should be part of multi-byte sequence */
384 /* if byte should be part of multi-byte sequence */ 365 if (c & 0x80) {
385 if(c & 0x80) 366 /* if we have already started to parse a UTF8 sequence */
386 { 367 if (utf_count > 0 && (c & 0xc0) == 0x80) {
387 /* if we have already started to parse a UTF8 sequence */ 368 utf_char = (utf_char << 6) | (c & 0x3f);
388 if (utf_count > 0 && (c & 0xc0) == 0x80) 369 utf_count--;
389 { 370 if (utf_count == 0)
390 utf_char = (utf_char << 6) | (c & 0x3f); 371 tc = utf_char;
391 utf_count--; 372 else
392 if (utf_count == 0) 373 continue;
393 tc = utf_char; 374 } else { /* Possibly 1st char of a UTF8 sequence */
394 else 375
395 continue; 376 if ((c & 0xe0) == 0xc0) {
396 } 377 utf_count = 1;
397 else /* Possibly 1st char of a UTF8 sequence */ 378 utf_char = (c & 0x1f);
398 { 379 } else if ((c & 0xf0) == 0xe0) {
399 if ((c & 0xe0) == 0xc0) 380 utf_count = 2;
400 { 381 utf_char = (c & 0x0f);
401 utf_count = 1; 382 } else if ((c & 0xf8) == 0xf0) {
402 utf_char = (c & 0x1f); 383 utf_count = 3;
403 } 384 utf_char = (c & 0x07);
404 else if ((c & 0xf0) == 0xe0) 385 } else if ((c & 0xfc) == 0xf8) {
405 { 386 utf_count = 4;
406 utf_count = 2; 387 utf_char = (c & 0x03);
407 utf_char = (c & 0x0f); 388 } else if ((c & 0xfe) == 0xfc) {
408 } 389 utf_count = 5;
409 else if ((c & 0xf8) == 0xf0) 390 utf_char = (c & 0x01);
410 { 391 } else
411 utf_count = 3; 392 utf_count = 0;
412 utf_char = (c & 0x07); 393 continue;
413 } 394 }
414 else if ((c & 0xfc) == 0xf8) 395 } else { /* not part of multi-byte sequence - treat as ASCII
415 { 396 * this makes incomplete sequences to be ignored
416 utf_count = 4; 397 */
417 utf_char = (c & 0x03); 398 tc = c;
418 } 399 utf_count = 0;
419 else if ((c & 0xfe) == 0xfc) 400 }
420 { 401 }
421 utf_count = 5; 402 while (utf_count);
422 utf_char = (c & 0x01);
423 }
424 else
425 utf_count = 0;
426 continue;
427 }
428 }
429 else /* not part of multi-byte sequence - treat as ASCII
430 * this makes incomplete sequences to be ignored
431 */
432 {
433 tc = c;
434 utf_count = 0;
435 }
436 }
437 while (utf_count);
438
439 return tc;
440}
441 403
404 return tc;
405}