Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 2 | /* |
| 3 | * loadfont.c - Eugene Crosser & Andries Brouwer |
| 4 | * |
| 5 | * Version 0.96bb |
| 6 | * |
| 7 | * Loads the console font, and possibly the corresponding screen map(s). |
| 8 | * (Adapted for busybox by Matej Vela.) |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 9 | * |
| 10 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 11 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 13 | #include <sys/kd.h> |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 15 | #ifndef KDFONTOP |
| 16 | #define KDFONTOP 0x4B72 |
| 17 | struct console_font_op { |
| 18 | unsigned op; /* KD_FONT_OP_* */ |
| 19 | unsigned flags; /* KD_FONT_FLAG_* */ |
| 20 | unsigned width, height; |
| 21 | unsigned charcount; |
| 22 | unsigned char *data; /* font data with height fixed to 32 */ |
| 23 | }; |
| 24 | |
| 25 | #define KD_FONT_OP_SET 0 /* Set font */ |
| 26 | #define KD_FONT_OP_GET 1 /* Get font */ |
| 27 | #define KD_FONT_OP_SET_DEFAULT 2 /* Set font to default, |
| 28 | data points to name / NULL */ |
| 29 | #define KD_FONT_OP_COPY 3 /* Copy from another console */ |
| 30 | |
| 31 | #define KD_FONT_FLAG_OLD 0x80000000 /* Invoked via old interface */ |
| 32 | #define KD_FONT_FLAG_DONT_RECALC 1 /* Don't call adjust_height() */ |
| 33 | /* (Used internally for PIO_FONT support) */ |
| 34 | #endif /* KDFONTOP */ |
| 35 | |
| 36 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 37 | enum { |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 38 | PSF_MAGIC1 = 0x36, |
| 39 | PSF_MAGIC2 = 0x04, |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 40 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 41 | PSF_MODE512 = 0x01, |
| 42 | PSF_MODEHASTAB = 0x02, |
| 43 | PSF_MAXMODE = 0x03, |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 44 | PSF_SEPARATOR = 0xffff |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 45 | }; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 46 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | struct psf_header { |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 48 | unsigned char magic1, magic2; /* Magic number */ |
| 49 | unsigned char mode; /* PSF font mode */ |
| 50 | unsigned char charsize; /* Character size */ |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 53 | #define PSF_MAGIC_OK(x) ((x)->magic1 == PSF_MAGIC1 && (x)->magic2 == PSF_MAGIC2) |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 54 | |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 55 | static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | { |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 57 | char *buf; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 58 | int i; |
| 59 | |
Matt Kraai | ef5529b | 2000-10-25 17:00:36 +0000 | [diff] [blame] | 60 | if (unit < 1 || unit > 32) |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 61 | bb_error_msg_and_die("bad character size %d", unit); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 63 | buf = xzalloc(16 * 1024); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 64 | for (i = 0; i < fontsize; i++) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | memcpy(buf + (32 * i), inbuf + (unit * i), unit); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 67 | { /* KDFONTOP */ |
| 68 | struct console_font_op cfo; |
| 69 | |
| 70 | cfo.op = KD_FONT_OP_SET; |
| 71 | cfo.flags = 0; |
| 72 | cfo.width = 8; |
| 73 | cfo.height = unit; |
| 74 | cfo.charcount = fontsize; |
| 75 | cfo.data = (void*)buf; |
| 76 | #if 0 |
| 77 | if (!ioctl_or_perror(fd, KDFONTOP, &cfo, "KDFONTOP ioctl failed (will try PIO_FONTX)")) |
| 78 | goto ret; /* success */ |
| 79 | #else |
| 80 | xioctl(fd, KDFONTOP, &cfo); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | #if 0 |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 85 | /* These ones do not honour -C tty (they set font on current tty regardless) |
| 86 | * On x86, this distinction is visible on framebuffer consoles |
| 87 | * (regular character consoles may have only one shared font anyway) |
| 88 | */ |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 89 | #if defined(PIO_FONTX) && !defined(__sparc__) |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 90 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | struct consolefontdesc cfd; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 92 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 93 | cfd.charcount = fontsize; |
| 94 | cfd.charheight = unit; |
| 95 | cfd.chardata = buf; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 96 | |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 97 | if (!ioctl_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)")) |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 98 | goto ret; /* success */ |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 99 | } |
| 100 | #endif |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 101 | xioctl(fd, PIO_FONT, buf); |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 102 | ret: |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 103 | #endif /* 0 */ |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 104 | free(buf); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 107 | static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 108 | { |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 109 | struct unimapinit advice; |
| 110 | struct unimapdesc ud; |
| 111 | struct unipair *up; |
| 112 | int ct = 0, maxct; |
| 113 | int glyph; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 114 | uint16_t unicode; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 116 | maxct = tailsz; /* more than enough */ |
Denis Vlasenko | b95636c | 2006-12-19 23:36:04 +0000 | [diff] [blame] | 117 | up = xmalloc(maxct * sizeof(struct unipair)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 119 | for (glyph = 0; glyph < fontsize; glyph++) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | while (tailsz >= 2) { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 121 | unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 122 | tailsz -= 2; |
| 123 | inbuf += 2; |
| 124 | if (unicode == PSF_SEPARATOR) |
| 125 | break; |
| 126 | up[ct].unicode = unicode; |
| 127 | up[ct].fontpos = glyph; |
| 128 | ct++; |
| 129 | } |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP |
| 133 | this printf did not work on many kernels */ |
| 134 | |
| 135 | advice.advised_hashsize = 0; |
| 136 | advice.advised_hashstep = 0; |
| 137 | advice.advised_hashlevel = 0; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 138 | xioctl(fd, PIO_UNIMAPCLR, &advice); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 139 | ud.entry_ct = ct; |
| 140 | ud.entries = up; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 141 | xioctl(fd, PIO_UNIMAP, &ud); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 144 | static void do_load(int fd, struct psf_header *psfhdr, size_t len) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 145 | { |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 146 | int unit; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 147 | int fontsize; |
| 148 | int hastable; |
| 149 | unsigned head0, head = head; |
| 150 | |
| 151 | /* test for psf first */ |
| 152 | if (len >= sizeof(struct psf_header) && PSF_MAGIC_OK(psfhdr)) { |
| 153 | if (psfhdr->mode > PSF_MAXMODE) |
| 154 | bb_error_msg_and_die("unsupported psf file mode"); |
| 155 | fontsize = ((psfhdr->mode & PSF_MODE512) ? 512 : 256); |
| 156 | #if !defined(PIO_FONTX) || defined(__sparc__) |
| 157 | if (fontsize != 256) |
| 158 | bb_error_msg_and_die("only fontsize 256 supported"); |
| 159 | #endif |
| 160 | hastable = (psfhdr->mode & PSF_MODEHASTAB); |
| 161 | unit = psfhdr->charsize; |
| 162 | head0 = sizeof(struct psf_header); |
| 163 | |
| 164 | head = head0 + fontsize * unit; |
| 165 | if (head > len || (!hastable && head != len)) |
| 166 | bb_error_msg_and_die("input file: bad length"); |
| 167 | } else { |
| 168 | /* file with three code pages? */ |
| 169 | if (len == 9780) { |
| 170 | head0 = 40; |
| 171 | unit = 16; |
| 172 | } else { |
| 173 | /* bare font */ |
| 174 | if (len & 0377) |
| 175 | bb_error_msg_and_die("input file: bad length"); |
| 176 | head0 = 0; |
| 177 | unit = len / 256; |
| 178 | } |
| 179 | fontsize = 256; |
| 180 | hastable = 0; |
| 181 | } |
| 182 | |
| 183 | do_loadfont(fd, (unsigned char *)psfhdr + head0, unit, fontsize); |
| 184 | if (hastable) |
| 185 | do_loadtable(fd, (unsigned char *)psfhdr + head, len - head, fontsize); |
| 186 | } |
| 187 | |
| 188 | #if ENABLE_LOADFONT |
| 189 | int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 190 | int loadfont_main(int argc UNUSED_PARAM, char **argv) |
| 191 | { |
| 192 | size_t len; |
| 193 | struct psf_header *psfhdr; |
| 194 | |
| 195 | // no arguments allowed! |
| 196 | opt_complementary = "=0"; |
| 197 | getopt32(argv, ""); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * We used to look at the length of the input file |
| 201 | * with stat(); now that we accept compressed files, |
| 202 | * just read the entire file. |
| 203 | */ |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 204 | len = 32*1024; // can't be larger |
Denis Vlasenko | fc66892 | 2008-11-06 02:32:31 +0000 | [diff] [blame] | 205 | psfhdr = xmalloc_read(STDIN_FILENO, &len); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 206 | // xmalloc_open_zipped_read_close(filename, &len); |
| 207 | if (!psfhdr) |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 208 | bb_perror_msg_and_die("error reading input font"); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 209 | do_load(get_console_fd_or_die(), psfhdr, len); |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 210 | |
| 211 | return EXIT_SUCCESS; |
| 212 | } |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 213 | #endif |
| 214 | |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 215 | #if ENABLE_SETFONT |
| 216 | |
Denis Vlasenko | 2bc5c03 | 2008-09-13 18:27:32 +0000 | [diff] [blame] | 217 | /* |
| 218 | kbd-1.12: |
| 219 | |
| 220 | setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig] |
| 221 | [-ou umap.orig] [-N] [font.new ...] [-m cmap] [-u umap] [-C console] |
| 222 | [-hNN] [-v] [-V] |
| 223 | |
| 224 | -h NN Override font height |
| 225 | -o file |
| 226 | Save previous font in file |
| 227 | -O file |
| 228 | Save previous font and Unicode map in file |
| 229 | -om file |
| 230 | Store console map in file |
| 231 | -ou file |
| 232 | Save previous Unicode map in file |
| 233 | -m file |
| 234 | Load console map or Unicode console map from file |
| 235 | -u file |
| 236 | Load Unicode table describing the font from file |
| 237 | Example: |
| 238 | # cp866 |
| 239 | 0x00-0x7f idem |
| 240 | # |
| 241 | 0x80 U+0410 # CYRILLIC CAPITAL LETTER A |
| 242 | 0x81 U+0411 # CYRILLIC CAPITAL LETTER BE |
| 243 | 0x82 U+0412 # CYRILLIC CAPITAL LETTER VE |
| 244 | -C console |
| 245 | Set the font for the indicated console |
| 246 | -v Verbose |
| 247 | -V Version |
| 248 | */ |
| 249 | |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 250 | #if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP |
| 251 | static int ctoi(char *s) |
| 252 | { |
| 253 | if (s[0] == '\'' && s[1] != '\0' && s[2] == '\'' && s[3] == '\0') |
| 254 | return s[1]; |
| 255 | // U+ means 0x |
| 256 | if (s[0] == 'U' && s[1] == '+') { |
| 257 | s[0] = '0'; |
| 258 | s[1] = 'x'; |
| 259 | } |
| 260 | if (!isdigit(s[0])) |
| 261 | return -1; |
| 262 | return xstrtoul(s, 0); |
| 263 | } |
| 264 | #endif |
| 265 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 266 | int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 267 | int setfont_main(int argc UNUSED_PARAM, char **argv) |
| 268 | { |
| 269 | size_t len; |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 270 | unsigned opts; |
| 271 | int fd; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 272 | struct psf_header *psfhdr; |
| 273 | char *mapfilename; |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 274 | const char *tty_name = CURRENT_TTY; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 275 | |
| 276 | opt_complementary = "=1"; |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 277 | opts = getopt32(argv, "m:C:", &mapfilename, &tty_name); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 278 | argv += optind; |
| 279 | |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 280 | fd = xopen(tty_name, O_NONBLOCK); |
| 281 | |
| 282 | if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not "" |
Denis Vlasenko | 72fa70a | 2008-09-18 01:01:02 +0000 | [diff] [blame] | 283 | if (*argv[0] != '/') { |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 284 | // goto default fonts location. don't die if doesn't exist |
| 285 | chdir(CONFIG_DEFAULT_SETFONT_DIR "/consolefonts"); |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 288 | // load font |
| 289 | len = 32*1024; // can't be larger |
Denis Vlasenko | fc66892 | 2008-11-06 02:32:31 +0000 | [diff] [blame] | 290 | psfhdr = xmalloc_open_zipped_read_close(*argv, &len); |
Denis Vlasenko | 72fa70a | 2008-09-18 01:01:02 +0000 | [diff] [blame] | 291 | if (!psfhdr) |
| 292 | bb_simple_perror_msg_and_die(*argv); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 293 | do_load(fd, psfhdr, len); |
| 294 | |
| 295 | // load the screen map, if any |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 296 | if (opts & 1) { // -m |
| 297 | unsigned mode = PIO_SCRNMAP; |
| 298 | void *map; |
| 299 | |
| 300 | if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not "" |
Denis Vlasenko | 72fa70a | 2008-09-18 01:01:02 +0000 | [diff] [blame] | 301 | if (mapfilename[0] != '/') { |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 302 | // goto default keymaps location |
| 303 | chdir(CONFIG_DEFAULT_SETFONT_DIR "/consoletrans"); |
| 304 | } |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 305 | } |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 306 | // fetch keymap |
| 307 | map = xmalloc_open_zipped_read_close(mapfilename, &len); |
| 308 | if (!map) |
| 309 | bb_simple_perror_msg_and_die(mapfilename); |
| 310 | // file size is 256 or 512 bytes? -> assume binary map |
| 311 | if (len == E_TABSZ || len == 2*E_TABSZ) { |
| 312 | if (len == 2*E_TABSZ) |
| 313 | mode = PIO_UNISCRNMAP; |
| 314 | } |
| 315 | #if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP |
| 316 | // assume textual Unicode console maps: |
| 317 | // 0x00 U+0000 # NULL (NUL) |
| 318 | // 0x01 U+0001 # START OF HEADING (SOH) |
| 319 | // 0x02 U+0002 # START OF TEXT (STX) |
| 320 | // 0x03 U+0003 # END OF TEXT (ETX) |
| 321 | else { |
| 322 | int i; |
| 323 | char *token[2]; |
| 324 | parser_t *parser; |
| 325 | |
| 326 | if (ENABLE_FEATURE_CLEAN_UP) |
| 327 | free(map); |
| 328 | map = xmalloc(E_TABSZ * sizeof(unsigned short)); |
| 329 | |
| 330 | #define unicodes ((unsigned short *)map) |
| 331 | // fill vanilla map |
| 332 | for (i = 0; i < E_TABSZ; i++) |
| 333 | unicodes[i] = 0xf000 + i; |
| 334 | |
| 335 | parser = config_open(mapfilename); |
| 336 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL | PARSE_MIN_DIE)) { |
| 337 | // parse code/value pair |
| 338 | int a = ctoi(token[0]); |
| 339 | int b = ctoi(token[1]); |
| 340 | if (a < 0 || a >= E_TABSZ |
| 341 | || b < 0 || b > 65535 |
| 342 | ) { |
| 343 | bb_error_msg_and_die("map format"); |
| 344 | } |
| 345 | // patch map |
| 346 | unicodes[a] = b; |
| 347 | // unicode character is met? |
| 348 | if (b > 255) |
| 349 | mode = PIO_UNISCRNMAP; |
| 350 | } |
| 351 | if (ENABLE_FEATURE_CLEAN_UP) |
| 352 | config_close(parser); |
| 353 | |
| 354 | if (mode != PIO_UNISCRNMAP) { |
| 355 | #define asciis ((unsigned char *)map) |
| 356 | for (i = 0; i < E_TABSZ; i++) |
| 357 | asciis[i] = unicodes[i]; |
| 358 | #undef asciis |
| 359 | } |
| 360 | #undef unicodes |
| 361 | } |
| 362 | #endif // ENABLE_FEATURE_SETFONT_TEXTUAL_MAP |
| 363 | |
| 364 | // do set screen map |
| 365 | xioctl(fd, mode, map); |
| 366 | |
| 367 | if (ENABLE_FEATURE_CLEAN_UP) |
| 368 | free(map); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | return EXIT_SUCCESS; |
| 372 | } |
| 373 | #endif |