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