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