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