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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2, see file LICENSE in this source tree. |
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 |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 16 | # define KDFONTOP 0x4B72 |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 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 | }; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 24 | # define KD_FONT_OP_SET 0 /* Set font */ |
| 25 | # define KD_FONT_OP_GET 1 /* Get font */ |
| 26 | # define KD_FONT_OP_SET_DEFAULT 2 /* Set font to default, data points to name / NULL */ |
| 27 | # define KD_FONT_OP_COPY 3 /* Copy from another console */ |
| 28 | # define KD_FONT_FLAG_OLD 0x80000000 /* Invoked via old interface */ |
| 29 | # define KD_FONT_FLAG_DONT_RECALC 1 /* Don't call adjust_height() */ |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 30 | /* (Used internally for PIO_FONT support) */ |
| 31 | #endif /* KDFONTOP */ |
| 32 | |
| 33 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 34 | enum { |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 35 | PSF1_MAGIC0 = 0x36, |
| 36 | PSF1_MAGIC1 = 0x04, |
| 37 | PSF1_MODE512 = 0x01, |
| 38 | PSF1_MODEHASTAB = 0x02, |
| 39 | PSF1_MODEHASSEQ = 0x04, |
| 40 | PSF1_MAXMODE = 0x05, |
| 41 | PSF1_STARTSEQ = 0xfffe, |
| 42 | PSF1_SEPARATOR = 0xffff, |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 43 | }; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 44 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 45 | struct psf1_header { |
| 46 | unsigned char magic[2]; /* Magic number */ |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 47 | unsigned char mode; /* PSF font mode */ |
| 48 | unsigned char charsize; /* Character size */ |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 51 | #define psf1h(x) ((struct psf1_header*)(x)) |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 52 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 53 | #define PSF1_MAGIC_OK(x) ( \ |
| 54 | (x)->magic[0] == PSF1_MAGIC0 \ |
| 55 | && (x)->magic[1] == PSF1_MAGIC1 \ |
| 56 | ) |
| 57 | |
| 58 | #if ENABLE_FEATURE_LOADFONT_PSF2 |
| 59 | enum { |
| 60 | PSF2_MAGIC0 = 0x72, |
| 61 | PSF2_MAGIC1 = 0xb5, |
| 62 | PSF2_MAGIC2 = 0x4a, |
| 63 | PSF2_MAGIC3 = 0x86, |
| 64 | PSF2_HAS_UNICODE_TABLE = 0x01, |
| 65 | PSF2_MAXVERSION = 0, |
| 66 | PSF2_STARTSEQ = 0xfe, |
| 67 | PSF2_SEPARATOR = 0xff |
| 68 | }; |
| 69 | |
| 70 | struct psf2_header { |
| 71 | unsigned char magic[4]; |
| 72 | unsigned int version; |
| 73 | unsigned int headersize; /* offset of bitmaps in file */ |
| 74 | unsigned int flags; |
| 75 | unsigned int length; /* number of glyphs */ |
| 76 | unsigned int charsize; /* number of bytes for each character */ |
| 77 | unsigned int height; /* max dimensions of glyphs */ |
| 78 | unsigned int width; /* charsize = height * ((width + 7) / 8) */ |
| 79 | }; |
| 80 | |
| 81 | #define psf2h(x) ((struct psf2_header*)(x)) |
| 82 | |
| 83 | #define PSF2_MAGIC_OK(x) ( \ |
| 84 | (x)->magic[0] == PSF2_MAGIC0 \ |
| 85 | && (x)->magic[1] == PSF2_MAGIC1 \ |
| 86 | && (x)->magic[2] == PSF2_MAGIC2 \ |
| 87 | && (x)->magic[3] == PSF2_MAGIC3 \ |
| 88 | ) |
| 89 | #endif /* ENABLE_FEATURE_LOADFONT_PSF2 */ |
| 90 | |
| 91 | |
| 92 | static void do_loadfont(int fd, unsigned char *inbuf, int height, int width, int charsize, int fontsize) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 93 | { |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 94 | unsigned char *buf; |
| 95 | int charwidth = 32 * ((width+7)/8); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 96 | int i; |
| 97 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 98 | if (height < 1 || height > 32 || width < 1 || width > 32) |
| 99 | bb_error_msg_and_die("bad character size %dx%d", height, width); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 100 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 101 | buf = xzalloc(charwidth * ((fontsize < 128) ? 128 : fontsize)); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 102 | for (i = 0; i < fontsize; i++) |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 103 | memcpy(buf + (i*charwidth), inbuf + (i*charsize), charsize); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 104 | |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 105 | { /* KDFONTOP */ |
| 106 | struct console_font_op cfo; |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 107 | cfo.op = KD_FONT_OP_SET; |
| 108 | cfo.flags = 0; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 109 | cfo.width = width; |
| 110 | cfo.height = height; |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 111 | cfo.charcount = fontsize; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 112 | cfo.data = buf; |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 113 | xioctl(fd, KDFONTOP, &cfo); |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 116 | free(buf); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 119 | /* |
| 120 | * Format of the Unicode information: |
| 121 | * |
| 122 | * For each font position <uc>*<seq>*<term> |
| 123 | * where <uc> is a 2-byte little endian Unicode value (PSF1) |
| 124 | * or an UTF-8 coded value (PSF2), |
| 125 | * <seq> = <ss><uc><uc>*, <ss> = psf1 ? 0xFFFE : 0xFE, |
| 126 | * <term> = psf1 ? 0xFFFF : 0xFF. |
| 127 | * and * denotes zero or more occurrences of the preceding item. |
| 128 | * |
| 129 | * Semantics: |
| 130 | * The leading <uc>* part gives Unicode symbols that are all |
| 131 | * represented by this font position. The following sequences |
| 132 | * are sequences of Unicode symbols - probably a symbol |
| 133 | * together with combining accents - also represented by |
| 134 | * this font position. |
| 135 | * |
| 136 | * Example: |
| 137 | * At the font position for a capital A-ring glyph, we |
| 138 | * may have: |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 139 | * 00C5,212B,FFFE,0041,030A,FFFF |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 140 | * Some font positions may be described by sequences only, |
| 141 | * namely when there is no precomposed Unicode value for the glyph. |
| 142 | */ |
| 143 | #if !ENABLE_FEATURE_LOADFONT_PSF2 |
| 144 | #define do_loadtable(fd, inbuf, tailsz, fontsize, psf2) \ |
| 145 | do_loadtable(fd, inbuf, tailsz, fontsize) |
| 146 | #endif |
| 147 | static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize, int psf2) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 148 | { |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 149 | #if !ENABLE_FEATURE_LOADFONT_PSF2 |
| 150 | /* gcc 4.3.1 code size: */ |
| 151 | # define psf2 0 /* +0 bytes */ |
| 152 | // const int psf2 = 0; /* +8 bytes */ |
| 153 | // enum { psf2 = 0 }; /* +13 bytes */ |
| 154 | #endif |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 155 | struct unimapinit advice; |
| 156 | struct unimapdesc ud; |
| 157 | struct unipair *up; |
| 158 | int ct = 0, maxct; |
| 159 | int glyph; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 160 | uint16_t unicode; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 161 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 162 | maxct = tailsz; /* more than enough */ |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 163 | up = xmalloc(maxct * sizeof(*up)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 164 | |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 165 | for (glyph = 0; glyph < fontsize; glyph++) { |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 166 | while (tailsz > 0) { |
| 167 | if (!psf2) { /* PSF1 */ |
| 168 | unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0]; |
| 169 | tailsz -= 2; |
| 170 | inbuf += 2; |
| 171 | if (unicode == PSF1_SEPARATOR) |
| 172 | break; |
| 173 | } else { /* PSF2 */ |
| 174 | #if ENABLE_FEATURE_LOADFONT_PSF2 |
| 175 | --tailsz; |
| 176 | unicode = *inbuf++; |
| 177 | if (unicode == PSF2_SEPARATOR) { |
| 178 | break; |
| 179 | } else if (unicode == PSF2_STARTSEQ) { |
| 180 | bb_error_msg_and_die("unicode sequences not implemented"); |
| 181 | } else if (unicode >= 0xC0) { |
| 182 | if (unicode >= 0xFC) |
| 183 | unicode &= 0x01, maxct = 5; |
| 184 | else if (unicode >= 0xF8) |
| 185 | unicode &= 0x03, maxct = 4; |
| 186 | else if (unicode >= 0xF0) |
| 187 | unicode &= 0x07, maxct = 3; |
| 188 | else if (unicode >= 0xE0) |
| 189 | unicode &= 0x0F, maxct = 2; |
| 190 | else |
| 191 | unicode &= 0x1F, maxct = 1; |
| 192 | do { |
| 193 | if (tailsz <= 0 || *inbuf < 0x80 || *inbuf > 0xBF) |
| 194 | bb_error_msg_and_die("illegal UTF-8 character"); |
| 195 | --tailsz; |
| 196 | unicode = (unicode << 6) + (*inbuf++ & 0x3F); |
| 197 | } while (--maxct > 0); |
| 198 | } else if (unicode >= 0x80) { |
| 199 | bb_error_msg_and_die("illegal UTF-8 character"); |
| 200 | } |
| 201 | #else |
| 202 | return; |
| 203 | #endif |
| 204 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 205 | up[ct].unicode = unicode; |
| 206 | up[ct].fontpos = glyph; |
| 207 | ct++; |
| 208 | } |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP |
| 212 | this printf did not work on many kernels */ |
| 213 | |
| 214 | advice.advised_hashsize = 0; |
| 215 | advice.advised_hashstep = 0; |
| 216 | advice.advised_hashlevel = 0; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 217 | xioctl(fd, PIO_UNIMAPCLR, &advice); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 218 | ud.entry_ct = ct; |
| 219 | ud.entries = up; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 220 | xioctl(fd, PIO_UNIMAP, &ud); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 221 | #undef psf2 |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 224 | static void do_load(int fd, unsigned char *buffer, size_t len) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | { |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 226 | int height; |
| 227 | int width = 8; |
| 228 | int charsize; |
| 229 | int fontsize = 256; |
| 230 | int has_table = 0; |
| 231 | unsigned char *font = buffer; |
| 232 | unsigned char *table; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 233 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 234 | if (len >= sizeof(struct psf1_header) && PSF1_MAGIC_OK(psf1h(buffer))) { |
| 235 | if (psf1h(buffer)->mode > PSF1_MAXMODE) |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 236 | bb_error_msg_and_die("unsupported psf file mode"); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 237 | if (psf1h(buffer)->mode & PSF1_MODE512) |
| 238 | fontsize = 512; |
| 239 | if (psf1h(buffer)->mode & PSF1_MODEHASTAB) |
| 240 | has_table = 1; |
| 241 | height = charsize = psf1h(buffer)->charsize; |
| 242 | font += sizeof(struct psf1_header); |
| 243 | } else |
| 244 | #if ENABLE_FEATURE_LOADFONT_PSF2 |
| 245 | if (len >= sizeof(struct psf2_header) && PSF2_MAGIC_OK(psf2h(buffer))) { |
| 246 | if (psf2h(buffer)->version > PSF2_MAXVERSION) |
| 247 | bb_error_msg_and_die("unsupported psf file version"); |
| 248 | fontsize = psf2h(buffer)->length; |
| 249 | if (psf2h(buffer)->flags & PSF2_HAS_UNICODE_TABLE) |
| 250 | has_table = 2; |
| 251 | charsize = psf2h(buffer)->charsize; |
| 252 | height = psf2h(buffer)->height; |
| 253 | width = psf2h(buffer)->width; |
| 254 | font += psf2h(buffer)->headersize; |
| 255 | } else |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 256 | #endif |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 257 | #if ENABLE_FEATURE_LOADFONT_RAW |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 258 | if (len == 9780) { /* file with three code pages? */ |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 259 | charsize = height = 16; |
| 260 | font += 40; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 261 | } else if ((len & 0377) == 0) { /* bare font */ |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 262 | charsize = height = len / 256; |
| 263 | } else |
| 264 | #endif |
| 265 | { |
| 266 | bb_error_msg_and_die("input file: bad length or unsupported font type"); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 269 | #if !defined(PIO_FONTX) || defined(__sparc__) |
| 270 | if (fontsize != 256) |
| 271 | bb_error_msg_and_die("only fontsize 256 supported"); |
| 272 | #endif |
| 273 | |
| 274 | table = font + fontsize * charsize; |
| 275 | buffer += len; |
| 276 | |
| 277 | if (table > buffer || (!has_table && table != buffer)) |
| 278 | bb_error_msg_and_die("input file: bad length"); |
| 279 | |
| 280 | do_loadfont(fd, font, height, width, charsize, fontsize); |
| 281 | |
| 282 | if (has_table) |
| 283 | do_loadtable(fd, table, buffer - table, fontsize, has_table - 1); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 286 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 287 | #if ENABLE_LOADFONT |
| 288 | int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 289 | int loadfont_main(int argc UNUSED_PARAM, char **argv) |
| 290 | { |
| 291 | size_t len; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 292 | unsigned char *buffer; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 293 | |
| 294 | // no arguments allowed! |
| 295 | opt_complementary = "=0"; |
| 296 | getopt32(argv, ""); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * We used to look at the length of the input file |
| 300 | * with stat(); now that we accept compressed files, |
| 301 | * just read the entire file. |
| 302 | */ |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 303 | len = 32*1024; // can't be larger |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 304 | buffer = xmalloc_read(STDIN_FILENO, &len); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 305 | // xmalloc_open_zipped_read_close(filename, &len); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 306 | if (!buffer) |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 307 | bb_perror_msg_and_die("error reading input font"); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 308 | do_load(get_console_fd_or_die(), buffer, len); |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 309 | |
| 310 | return EXIT_SUCCESS; |
| 311 | } |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 312 | #endif |
| 313 | |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 314 | #if ENABLE_SETFONT |
| 315 | |
Denis Vlasenko | 2bc5c03 | 2008-09-13 18:27:32 +0000 | [diff] [blame] | 316 | /* |
| 317 | kbd-1.12: |
| 318 | |
| 319 | setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig] |
| 320 | [-ou umap.orig] [-N] [font.new ...] [-m cmap] [-u umap] [-C console] |
| 321 | [-hNN] [-v] [-V] |
| 322 | |
| 323 | -h NN Override font height |
| 324 | -o file |
| 325 | Save previous font in file |
| 326 | -O file |
| 327 | Save previous font and Unicode map in file |
| 328 | -om file |
| 329 | Store console map in file |
| 330 | -ou file |
| 331 | Save previous Unicode map in file |
| 332 | -m file |
| 333 | Load console map or Unicode console map from file |
| 334 | -u file |
| 335 | Load Unicode table describing the font from file |
| 336 | Example: |
| 337 | # cp866 |
| 338 | 0x00-0x7f idem |
| 339 | # |
| 340 | 0x80 U+0410 # CYRILLIC CAPITAL LETTER A |
| 341 | 0x81 U+0411 # CYRILLIC CAPITAL LETTER BE |
| 342 | 0x82 U+0412 # CYRILLIC CAPITAL LETTER VE |
| 343 | -C console |
| 344 | Set the font for the indicated console |
| 345 | -v Verbose |
| 346 | -V Version |
| 347 | */ |
| 348 | |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 349 | #if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP |
| 350 | static int ctoi(char *s) |
| 351 | { |
| 352 | if (s[0] == '\'' && s[1] != '\0' && s[2] == '\'' && s[3] == '\0') |
| 353 | return s[1]; |
| 354 | // U+ means 0x |
| 355 | if (s[0] == 'U' && s[1] == '+') { |
| 356 | s[0] = '0'; |
| 357 | s[1] = 'x'; |
| 358 | } |
| 359 | if (!isdigit(s[0])) |
| 360 | return -1; |
| 361 | return xstrtoul(s, 0); |
| 362 | } |
| 363 | #endif |
| 364 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 365 | int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 366 | int setfont_main(int argc UNUSED_PARAM, char **argv) |
| 367 | { |
| 368 | size_t len; |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 369 | unsigned opts; |
| 370 | int fd; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 371 | unsigned char *buffer; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 372 | char *mapfilename; |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 373 | const char *tty_name = CURRENT_TTY; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 374 | |
| 375 | opt_complementary = "=1"; |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 376 | opts = getopt32(argv, "m:C:", &mapfilename, &tty_name); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 377 | argv += optind; |
| 378 | |
Bernhard Reutner-Fischer | a483087 | 2009-10-26 23:27:08 +0100 | [diff] [blame] | 379 | fd = xopen_nonblocking(tty_name); |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 380 | |
| 381 | if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not "" |
Denis Vlasenko | 72fa70a | 2008-09-18 01:01:02 +0000 | [diff] [blame] | 382 | if (*argv[0] != '/') { |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 383 | // goto default fonts location. don't die if doesn't exist |
| 384 | chdir(CONFIG_DEFAULT_SETFONT_DIR "/consolefonts"); |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 387 | // load font |
| 388 | len = 32*1024; // can't be larger |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 389 | buffer = xmalloc_open_zipped_read_close(*argv, &len); |
| 390 | if (!buffer) |
Denis Vlasenko | 72fa70a | 2008-09-18 01:01:02 +0000 | [diff] [blame] | 391 | bb_simple_perror_msg_and_die(*argv); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 392 | do_load(fd, buffer, len); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 393 | |
| 394 | // load the screen map, if any |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 395 | if (opts & 1) { // -m |
| 396 | unsigned mode = PIO_SCRNMAP; |
| 397 | void *map; |
| 398 | |
| 399 | if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not "" |
Denis Vlasenko | 72fa70a | 2008-09-18 01:01:02 +0000 | [diff] [blame] | 400 | if (mapfilename[0] != '/') { |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 401 | // goto default keymaps location |
| 402 | chdir(CONFIG_DEFAULT_SETFONT_DIR "/consoletrans"); |
| 403 | } |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 404 | } |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 405 | // fetch keymap |
| 406 | map = xmalloc_open_zipped_read_close(mapfilename, &len); |
| 407 | if (!map) |
| 408 | bb_simple_perror_msg_and_die(mapfilename); |
| 409 | // file size is 256 or 512 bytes? -> assume binary map |
| 410 | if (len == E_TABSZ || len == 2*E_TABSZ) { |
| 411 | if (len == 2*E_TABSZ) |
| 412 | mode = PIO_UNISCRNMAP; |
| 413 | } |
| 414 | #if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP |
| 415 | // assume textual Unicode console maps: |
| 416 | // 0x00 U+0000 # NULL (NUL) |
| 417 | // 0x01 U+0001 # START OF HEADING (SOH) |
| 418 | // 0x02 U+0002 # START OF TEXT (STX) |
| 419 | // 0x03 U+0003 # END OF TEXT (ETX) |
| 420 | else { |
| 421 | int i; |
| 422 | char *token[2]; |
| 423 | parser_t *parser; |
| 424 | |
| 425 | if (ENABLE_FEATURE_CLEAN_UP) |
| 426 | free(map); |
| 427 | map = xmalloc(E_TABSZ * sizeof(unsigned short)); |
| 428 | |
| 429 | #define unicodes ((unsigned short *)map) |
| 430 | // fill vanilla map |
| 431 | for (i = 0; i < E_TABSZ; i++) |
| 432 | unicodes[i] = 0xf000 + i; |
| 433 | |
| 434 | parser = config_open(mapfilename); |
| 435 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL | PARSE_MIN_DIE)) { |
| 436 | // parse code/value pair |
| 437 | int a = ctoi(token[0]); |
| 438 | int b = ctoi(token[1]); |
| 439 | if (a < 0 || a >= E_TABSZ |
| 440 | || b < 0 || b > 65535 |
| 441 | ) { |
| 442 | bb_error_msg_and_die("map format"); |
| 443 | } |
| 444 | // patch map |
| 445 | unicodes[a] = b; |
| 446 | // unicode character is met? |
| 447 | if (b > 255) |
| 448 | mode = PIO_UNISCRNMAP; |
| 449 | } |
| 450 | if (ENABLE_FEATURE_CLEAN_UP) |
| 451 | config_close(parser); |
| 452 | |
| 453 | if (mode != PIO_UNISCRNMAP) { |
| 454 | #define asciis ((unsigned char *)map) |
| 455 | for (i = 0; i < E_TABSZ; i++) |
| 456 | asciis[i] = unicodes[i]; |
| 457 | #undef asciis |
| 458 | } |
| 459 | #undef unicodes |
| 460 | } |
| 461 | #endif // ENABLE_FEATURE_SETFONT_TEXTUAL_MAP |
| 462 | |
| 463 | // do set screen map |
| 464 | xioctl(fd, mode, map); |
| 465 | |
| 466 | if (ENABLE_FEATURE_CLEAN_UP) |
| 467 | free(map); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | return EXIT_SUCCESS; |
| 471 | } |
| 472 | #endif |