lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <ctype.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <sys/types.h> |
| 5 | |
| 6 | int |
| 7 | main (int argc, char *argv[]) |
| 8 | { |
| 9 | int n; |
| 10 | char *line = NULL; |
| 11 | size_t len = 0; |
| 12 | |
| 13 | if (argc == 1) |
| 14 | { |
| 15 | static char *new_argv[] = { NULL, (char *) "/dev/stdin", NULL }; |
| 16 | argv = new_argv; |
| 17 | argc = 2; |
| 18 | } |
| 19 | |
| 20 | for (n = 1; n < argc; ++n) |
| 21 | { |
| 22 | FILE *fp = fopen (argv[n], "r"); |
| 23 | if (fp == NULL) |
| 24 | continue; |
| 25 | |
| 26 | while (! feof (fp)) |
| 27 | { |
| 28 | ssize_t cnt = getline (&line, &len, fp); |
| 29 | char *runp; |
| 30 | if (cnt <= 0) |
| 31 | break; |
| 32 | |
| 33 | runp = line; |
| 34 | do |
| 35 | { |
| 36 | if (runp[0] == '<' && runp[1] == 'U' && isxdigit (runp[2]) |
| 37 | && isxdigit (runp[3]) && isxdigit (runp[4]) |
| 38 | && isxdigit (runp[5]) && runp[6] == '>') |
| 39 | { |
| 40 | unsigned int val = strtoul (runp + 2, NULL, 16); |
| 41 | |
| 42 | //putchar ('<'); |
| 43 | if (val < 128) |
| 44 | putchar (val); |
| 45 | else if (val < 0x800) |
| 46 | { |
| 47 | putchar (0xc0 | (val >> 6)); |
| 48 | putchar (0x80 | (val & 0x3f)); |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | putchar (0xe0 | (val >> 12)); |
| 53 | putchar (0x80 | ((val >> 6) & 0x3f)); |
| 54 | putchar (0x80 | (val & 0x3f)); |
| 55 | } |
| 56 | //putchar ('>'); |
| 57 | runp += 7; |
| 58 | } |
| 59 | else |
| 60 | putchar (*runp++); |
| 61 | } |
| 62 | while (runp < &line[cnt]); |
| 63 | } |
| 64 | |
| 65 | fclose (fp); |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |