Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Derived from |
| 4 | * mapscrn.c - version 0.92 |
| 5 | * |
| 6 | * Was taken from console-tools and adapted by |
| 7 | * Peter Novodvorsky <petya@logic.ru> |
| 8 | */ |
| 9 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <memory.h> |
| 14 | #include <string.h> |
| 15 | #include <unistd.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <assert.h> |
| 18 | #include <errno.h> |
| 19 | #include <signal.h> |
| 20 | #include <sys/types.h> |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 21 | #include <sys/ioctl.h> |
| 22 | #include <sys/kd.h> |
| 23 | |
| 24 | typedef unsigned short unicode; |
| 25 | |
| 26 | static long int ctoi(unsigned char *s, int *is_unicode); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 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); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 31 | |
| 32 | int loadacm_main(int argc, char **argv) |
| 33 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 34 | int fd; |
| 35 | |
Erik Andersen | 3fe7f9f | 2000-04-19 03:59:10 +0000 | [diff] [blame] | 36 | if (argc>=2 && *argv[1]=='-') { |
| 37 | usage(loadacm_usage); |
| 38 | } |
| 39 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 40 | fd = open("/dev/tty", O_RDWR); |
| 41 | if (fd < 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame^] | 42 | error_msg("Error opening /dev/tty1: %s\n", strerror(errno)); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 43 | return EXIT_FAILURE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | if (screen_map_load(fd, stdin)) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame^] | 47 | error_msg("Error loading acm: %s\n", strerror(errno)); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 48 | return EXIT_FAILURE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | write(fd, "\033(K", 3); |
| 52 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 53 | return EXIT_SUCCESS; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | int screen_map_load(int fd, FILE * fp) |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 57 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | struct stat stbuf; |
| 59 | unicode wbuf[E_TABSZ]; |
| 60 | unsigned char buf[E_TABSZ]; |
| 61 | int parse_failed = 0; |
| 62 | int is_unicode; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 63 | |
Eric Andersen | 999bf72 | 2000-07-09 06:59:58 +0000 | [diff] [blame] | 64 | if (fstat(fileno(fp), &stbuf)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | perror("Cannot stat map file"), exit(1); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 66 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */ |
| 68 | if (! |
| 69 | (parse_failed = |
| 70 | (-1 == uni_screen_map_read_ascii(fp, wbuf, &is_unicode))) |
| 71 | || (S_ISREG(stbuf.st_mode) && (stbuf.st_size == (sizeof(unicode) * E_TABSZ)))) { /* test for binary UTF map by size */ |
| 72 | if (parse_failed) { |
| 73 | if (-1 == fseek(fp, 0, SEEK_SET)) { |
| 74 | if (errno == ESPIPE) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame^] | 75 | error_msg("16bit screen-map MUST be a regular file.\n"), |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | exit(1); |
| 77 | else |
| 78 | perror("fseek failed reading binary 16bit screen-map"), |
| 79 | exit(1); |
| 80 | } |
| 81 | |
| 82 | if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) |
| 83 | perror("Cannot read [new] map from file"), exit(1); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 84 | #if 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 85 | else |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame^] | 86 | error_msg("Input screen-map is binary.\n"); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 87 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* if it was effectively a 16-bit ASCII, OK, else try to read as 8-bit map */ |
| 91 | /* same if it was binary, ie. if parse_failed */ |
| 92 | if (parse_failed || is_unicode) { |
| 93 | if (ioctl(fd, PIO_UNISCRNMAP, wbuf)) |
| 94 | perror("PIO_UNISCRNMAP ioctl"), exit(1); |
| 95 | else |
| 96 | return 0; |
| 97 | } |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 100 | /* rewind... */ |
| 101 | if (-1 == fseek(fp, 0, SEEK_SET)) { |
| 102 | if (errno == ESPIPE) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame^] | 103 | error_msg("Assuming 8bit screen-map - MUST be a regular file.\n"), |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | exit(1); |
| 105 | else |
| 106 | perror("fseek failed assuming 8bit screen-map"), exit(1); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 107 | } |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 108 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 109 | /* ... and try an old 8-bit screen-map */ |
| 110 | if (!(parse_failed = (-1 == old_screen_map_read_ascii(fp, buf))) || |
| 111 | (S_ISREG(stbuf.st_mode) && (stbuf.st_size == E_TABSZ))) { /* test for binary old 8-bit map by size */ |
| 112 | if (parse_failed) { |
| 113 | if (-1 == fseek(fp, 0, SEEK_SET)) { |
| 114 | if (errno == ESPIPE) |
| 115 | /* should not - it succedeed above */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame^] | 116 | error_msg("fseek() returned ESPIPE !\n"), |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | 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); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 124 | #if 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 125 | else |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame^] | 126 | error_msg("Input screen-map is binary.\n"); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 127 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (ioctl(fd, PIO_SCRNMAP, buf)) |
| 131 | perror("PIO_SCRNMAP ioctl"), exit(1); |
| 132 | else |
| 133 | return 0; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 134 | } |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame^] | 135 | error_msg("Error parsing symbolic map\n"); |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 136 | return(1); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | /* |
| 141 | * - reads `fp' as a 16-bit ASCII SFM file. |
| 142 | * - returns -1 on error. |
| 143 | * - returns it in `unicode' in an E_TABSZ-elements array. |
| 144 | * - sets `*is_unicode' flagiff there were any non-8-bit |
| 145 | * (ie. real 16-bit) mapping. |
| 146 | * |
| 147 | * FIXME: ignores everything after second word |
| 148 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 149 | int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode) |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 150 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 151 | char buffer[256]; /* line buffer reading file */ |
| 152 | char *p, *q; /* 1st + 2nd words in line */ |
| 153 | int in, on; /* the same, as numbers */ |
| 154 | int tmp_is_unicode; /* tmp for is_unicode calculation */ |
| 155 | int i; /* loop index - result holder */ |
| 156 | int ret_code = 0; /* return code */ |
| 157 | sigset_t sigset, old_sigset; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 158 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | assert(is_unicode); |
| 160 | |
| 161 | *is_unicode = 0; |
| 162 | |
| 163 | /* first 128 codes defaults to ASCII */ |
| 164 | for (i = 0; i < 128; i++) |
| 165 | buf[i] = i; |
| 166 | /* remaining defaults to replacement char (usually E_TABSZ = 256) */ |
| 167 | for (; i < E_TABSZ; i++) |
| 168 | buf[i] = 0xfffd; |
| 169 | |
| 170 | /* block SIGCHLD */ |
| 171 | sigemptyset(&sigset); |
| 172 | sigaddset(&sigset, SIGCHLD); |
| 173 | sigprocmask(SIG_BLOCK, &sigset, &old_sigset); |
| 174 | |
| 175 | do { |
| 176 | if (NULL == fgets(buffer, sizeof(buffer), fp)) { |
| 177 | if (feof(fp)) |
| 178 | break; |
| 179 | else { |
| 180 | perror("uni_screen_map_read_ascii() can't read line"); |
| 181 | exit(2); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* get "charset-relative charcode", stripping leading spaces */ |
| 186 | p = strtok(buffer, " \t\n"); |
| 187 | |
| 188 | /* skip empty lines and comments */ |
| 189 | if (!p || *p == '#') |
| 190 | continue; |
| 191 | |
| 192 | /* get unicode mapping */ |
| 193 | q = strtok(NULL, " \t\n"); |
| 194 | if (q) { |
| 195 | in = ctoi(p, NULL); |
| 196 | if (in < 0 || in > 255) { |
| 197 | ret_code = -1; |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | on = ctoi(q, &tmp_is_unicode); |
| 202 | if (in < 0 && on > 65535) { |
| 203 | ret_code = -1; |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | *is_unicode |= tmp_is_unicode; |
| 208 | buf[in] = on; |
| 209 | } else { |
| 210 | ret_code = -1; |
| 211 | break; |
| 212 | } |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 213 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 214 | while (1); /* terminated by break on feof() */ |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 215 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 216 | /* restore sig mask */ |
| 217 | sigprocmask(SIG_SETMASK, &old_sigset, NULL); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 218 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 219 | return ret_code; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 223 | int old_screen_map_read_ascii(FILE * fp, unsigned char buf[]) |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 224 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | char buffer[256]; |
| 226 | int in, on; |
| 227 | char *p, *q; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 228 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 229 | for (in = 0; in < 256; in++) |
| 230 | buf[in] = in; |
| 231 | |
| 232 | while (fgets(buffer, sizeof(buffer) - 1, fp)) { |
| 233 | p = strtok(buffer, " \t\n"); |
| 234 | |
| 235 | if (!p || *p == '#') |
| 236 | continue; |
| 237 | |
| 238 | q = strtok(NULL, " \t\n#"); |
| 239 | if (q) { |
| 240 | in = ctoi(p, NULL); |
| 241 | if (in < 0 || in > 255) |
| 242 | return -1; |
| 243 | |
| 244 | on = ctoi(q, NULL); |
| 245 | if (in < 0 && on > 255) |
| 246 | return -1; |
| 247 | |
| 248 | buf[in] = on; |
| 249 | } else |
| 250 | return -1; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 251 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 252 | |
| 253 | return (0); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
| 257 | /* |
| 258 | * - converts a string into an int. |
| 259 | * - supports dec and hex bytes, hex UCS2, single-quoted byte and UTF8 chars. |
| 260 | * - returns the converted value |
| 261 | * - if `is_unicode != NULL', use it to tell whether it was unicode |
| 262 | * |
| 263 | * CAVEAT: will report valid UTF mappings using only 1 byte as 8-bit ones. |
| 264 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 265 | long int ctoi(unsigned char *s, int *is_unicode) |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 266 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 267 | int i; |
| 268 | size_t ls; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 269 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 270 | ls = strlen(s); |
| 271 | if (is_unicode) |
| 272 | *is_unicode = 0; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 273 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 274 | /* hex-specified UCS2 */ |
| 275 | if ((strncmp(s, "U+", 2) == 0) && |
| 276 | (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) { |
| 277 | sscanf(s + 2, "%x", &i); |
| 278 | if (is_unicode) |
| 279 | *is_unicode = 1; |
| 280 | } |
| 281 | |
| 282 | /* hex-specified byte */ |
| 283 | else if ((ls <= 4) && (strncmp(s, "0x", 2) == 0) && |
| 284 | (strspn(s + 2, "0123456789abcdefABCDEF") == ls - 2)) |
| 285 | sscanf(s + 2, "%x", &i); |
| 286 | |
| 287 | /* oct-specified number (byte) */ |
| 288 | else if ((*s == '0') && (strspn(s, "01234567") == ls)) |
| 289 | sscanf(s, "%o", &i); |
| 290 | |
| 291 | /* dec-specified number (byte) */ |
| 292 | else if (strspn(s, "0123456789") == ls) |
| 293 | sscanf(s, "%d", &i); |
| 294 | |
| 295 | /* single-byte quoted char */ |
| 296 | else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\'')) |
| 297 | i = s[1]; |
| 298 | |
| 299 | /* multi-byte UTF8 quoted char */ |
| 300 | else if ((s[0] == '\'') && (s[ls - 1] == '\'')) { |
| 301 | s[ls - 1] = 0; /* ensure we'll not "parse UTF too far" */ |
| 302 | i = utf8_to_ucs2(s + 1); |
| 303 | if (is_unicode) |
| 304 | *is_unicode = 1; |
| 305 | } else |
| 306 | return (-1); |
| 307 | |
| 308 | return (i); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 312 | void saveoldmap(int fd, char *omfil) |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 313 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 314 | FILE *fp; |
| 315 | char buf[E_TABSZ]; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 316 | |
| 317 | #ifdef GIO_UNISCRNMAP |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 318 | unicode xbuf[E_TABSZ]; |
| 319 | int is_old_map = 0; |
| 320 | |
| 321 | if (ioctl(fd, GIO_UNISCRNMAP, xbuf)) { |
| 322 | perror("GIO_UNISCRNMAP ioctl error"); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 323 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 324 | if (ioctl(fd, GIO_SCRNMAP, buf)) { |
| 325 | perror("GIO_SCRNMAP ioctl error"); |
| 326 | exit(1); |
| 327 | } else |
| 328 | is_old_map = 1; |
| 329 | #ifdef GIO_UNISCRNMAP |
| 330 | } |
| 331 | #endif |
| 332 | |
Matt Kraai | bbaef66 | 2000-09-27 02:43:35 +0000 | [diff] [blame] | 333 | fp = xfopen(omfil, "w"); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 334 | #ifdef GIO_UNISCRNMAP |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 335 | if (is_old_map) { |
| 336 | #endif |
| 337 | if (fwrite(buf, E_TABSZ, 1, fp) != 1) { |
| 338 | perror("Error writing map to file"); |
| 339 | exit(1); |
| 340 | } |
| 341 | #ifdef GIO_UNISCRNMAP |
| 342 | } else if (fwrite(xbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) { |
| 343 | perror("Error writing map to file"); |
| 344 | exit(1); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 345 | } |
| 346 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 347 | |
| 348 | fclose(fp); |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 351 | unicode utf8_to_ucs2(char *buf) |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 352 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 353 | int utf_count = 0; |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 354 | long utf_char = 0; |
| 355 | unicode tc = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 356 | unsigned char c; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 357 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 358 | do { |
| 359 | c = *buf; |
| 360 | buf++; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 361 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 362 | /* if byte should be part of multi-byte sequence */ |
| 363 | if (c & 0x80) { |
| 364 | /* if we have already started to parse a UTF8 sequence */ |
| 365 | if (utf_count > 0 && (c & 0xc0) == 0x80) { |
| 366 | utf_char = (utf_char << 6) | (c & 0x3f); |
| 367 | utf_count--; |
| 368 | if (utf_count == 0) |
| 369 | tc = utf_char; |
| 370 | else |
| 371 | continue; |
| 372 | } else { /* Possibly 1st char of a UTF8 sequence */ |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 373 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 374 | if ((c & 0xe0) == 0xc0) { |
| 375 | utf_count = 1; |
| 376 | utf_char = (c & 0x1f); |
| 377 | } else if ((c & 0xf0) == 0xe0) { |
| 378 | utf_count = 2; |
| 379 | utf_char = (c & 0x0f); |
| 380 | } else if ((c & 0xf8) == 0xf0) { |
| 381 | utf_count = 3; |
| 382 | utf_char = (c & 0x07); |
| 383 | } else if ((c & 0xfc) == 0xf8) { |
| 384 | utf_count = 4; |
| 385 | utf_char = (c & 0x03); |
| 386 | } else if ((c & 0xfe) == 0xfc) { |
| 387 | utf_count = 5; |
| 388 | utf_char = (c & 0x01); |
| 389 | } else |
| 390 | utf_count = 0; |
| 391 | continue; |
| 392 | } |
| 393 | } else { /* not part of multi-byte sequence - treat as ASCII |
| 394 | * this makes incomplete sequences to be ignored |
| 395 | */ |
| 396 | tc = c; |
| 397 | utf_count = 0; |
| 398 | } |
| 399 | } |
| 400 | while (utf_count); |
| 401 | |
| 402 | return tc; |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 403 | } |