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