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