blob: 29f723ec00424f2671b8c63a55d2e587e0a10650 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3e0fbae1999-10-19 06:02:44 +00002/*
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 Vlasenkodb12d1d2008-12-07 00:52:58 +00009 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersen3e0fbae1999-10-19 06:02:44 +000011 */
Denys Vlasenko6d932992016-11-23 10:39:27 +010012//config:config LOADFONT
13//config: bool "loadfont"
14//config: default y
15//config: select PLATFORM_LINUX
16//config: help
17//config: This program loads a console font from standard input.
18//config:
19//config:config SETFONT
20//config: bool "setfont"
21//config: default y
22//config: select PLATFORM_LINUX
23//config: help
24//config: Allows to load console screen map. Useful for i18n.
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
31//config: Support reading textual screen maps.
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
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).
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 Vlasenkof5604222017-01-10 14:58:54 +010045//config: bool "Support PSF2 console fonts"
Denys Vlasenko6d932992016-11-23 10:39:27 +010046//config: default y
47//config: depends on LOADFONT || SETFONT
Denys Vlasenko6d932992016-11-23 10:39:27 +010048//config:
49//config:config FEATURE_LOADFONT_RAW
Denys Vlasenkof5604222017-01-10 14:58:54 +010050//config: bool "Support old (raw) console fonts"
Denys Vlasenko6d932992016-11-23 10:39:27 +010051//config: default y
52//config: depends on LOADFONT || SETFONT
Denys Vlasenko6d932992016-11-23 10:39:27 +010053
54//applet:IF_LOADFONT(APPLET(loadfont, BB_DIR_USR_SBIN, BB_SUID_DROP))
55//applet:IF_SETFONT(APPLET(setfont, BB_DIR_USR_SBIN, BB_SUID_DROP))
56
57//kbuild:lib-$(CONFIG_LOADFONT) += loadfont.o
58//kbuild:lib-$(CONFIG_SETFONT) += loadfont.o
Pere Orga55068c42011-03-27 23:42:28 +020059
60//usage:#define loadfont_trivial_usage
61//usage: "< font"
62//usage:#define loadfont_full_usage "\n\n"
63//usage: "Load a console font from stdin"
64/* //usage: "\n -C TTY Affect TTY instead of /dev/tty" */
65//usage:
66//usage:#define loadfont_example_usage
67//usage: "$ loadfont < /etc/i18n/fontname\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020068//usage:
69//usage:#define setfont_trivial_usage
70//usage: "FONT [-m MAPFILE] [-C TTY]"
71//usage:#define setfont_full_usage "\n\n"
72//usage: "Load a console font\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020073//usage: "\n -m MAPFILE Load console screen map"
74//usage: "\n -C TTY Affect TTY instead of /dev/tty"
75//usage:
76//usage:#define setfont_example_usage
77//usage: "$ setfont -m koi8-r /etc/i18n/fontname\n"
Pere Orga55068c42011-03-27 23:42:28 +020078
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000079#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000080#include <sys/kd.h>
Eric Andersen3e0fbae1999-10-19 06:02:44 +000081
Denis Vlasenkoeef60772008-09-20 18:14:13 +000082#ifndef KDFONTOP
Harald Becker8ce1dc02010-02-21 13:10:26 +010083# define KDFONTOP 0x4B72
Denis Vlasenkoeef60772008-09-20 18:14:13 +000084struct console_font_op {
85 unsigned op; /* KD_FONT_OP_* */
86 unsigned flags; /* KD_FONT_FLAG_* */
87 unsigned width, height;
88 unsigned charcount;
89 unsigned char *data; /* font data with height fixed to 32 */
90};
Harald Becker8ce1dc02010-02-21 13:10:26 +010091# define KD_FONT_OP_SET 0 /* Set font */
92# define KD_FONT_OP_GET 1 /* Get font */
93# define KD_FONT_OP_SET_DEFAULT 2 /* Set font to default, data points to name / NULL */
94# define KD_FONT_OP_COPY 3 /* Copy from another console */
95# define KD_FONT_FLAG_OLD 0x80000000 /* Invoked via old interface */
96# define KD_FONT_FLAG_DONT_RECALC 1 /* Don't call adjust_height() */
Denis Vlasenkoeef60772008-09-20 18:14:13 +000097 /* (Used internally for PIO_FONT support) */
98#endif /* KDFONTOP */
99
100
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000101enum {
Harald Becker8ce1dc02010-02-21 13:10:26 +0100102 PSF1_MAGIC0 = 0x36,
103 PSF1_MAGIC1 = 0x04,
104 PSF1_MODE512 = 0x01,
105 PSF1_MODEHASTAB = 0x02,
106 PSF1_MODEHASSEQ = 0x04,
107 PSF1_MAXMODE = 0x05,
108 PSF1_STARTSEQ = 0xfffe,
109 PSF1_SEPARATOR = 0xffff,
Rob Landleybc68cd12006-03-10 19:22:06 +0000110};
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000111
Harald Becker8ce1dc02010-02-21 13:10:26 +0100112struct psf1_header {
113 unsigned char magic[2]; /* Magic number */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000114 unsigned char mode; /* PSF font mode */
115 unsigned char charsize; /* Character size */
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000116};
117
Harald Becker8ce1dc02010-02-21 13:10:26 +0100118#define psf1h(x) ((struct psf1_header*)(x))
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000119
Harald Becker8ce1dc02010-02-21 13:10:26 +0100120#define PSF1_MAGIC_OK(x) ( \
121 (x)->magic[0] == PSF1_MAGIC0 \
122 && (x)->magic[1] == PSF1_MAGIC1 \
123)
124
125#if ENABLE_FEATURE_LOADFONT_PSF2
126enum {
127 PSF2_MAGIC0 = 0x72,
128 PSF2_MAGIC1 = 0xb5,
129 PSF2_MAGIC2 = 0x4a,
130 PSF2_MAGIC3 = 0x86,
131 PSF2_HAS_UNICODE_TABLE = 0x01,
132 PSF2_MAXVERSION = 0,
133 PSF2_STARTSEQ = 0xfe,
134 PSF2_SEPARATOR = 0xff
135};
136
137struct psf2_header {
138 unsigned char magic[4];
139 unsigned int version;
140 unsigned int headersize; /* offset of bitmaps in file */
141 unsigned int flags;
142 unsigned int length; /* number of glyphs */
143 unsigned int charsize; /* number of bytes for each character */
144 unsigned int height; /* max dimensions of glyphs */
145 unsigned int width; /* charsize = height * ((width + 7) / 8) */
146};
147
148#define psf2h(x) ((struct psf2_header*)(x))
149
150#define PSF2_MAGIC_OK(x) ( \
151 (x)->magic[0] == PSF2_MAGIC0 \
152 && (x)->magic[1] == PSF2_MAGIC1 \
153 && (x)->magic[2] == PSF2_MAGIC2 \
154 && (x)->magic[3] == PSF2_MAGIC3 \
155)
156#endif /* ENABLE_FEATURE_LOADFONT_PSF2 */
157
158
159static void do_loadfont(int fd, unsigned char *inbuf, int height, int width, int charsize, int fontsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160{
Harald Becker8ce1dc02010-02-21 13:10:26 +0100161 unsigned char *buf;
162 int charwidth = 32 * ((width+7)/8);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000163 int i;
164
Harald Becker8ce1dc02010-02-21 13:10:26 +0100165 if (height < 1 || height > 32 || width < 1 || width > 32)
166 bb_error_msg_and_die("bad character size %dx%d", height, width);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000167
Harald Becker8ce1dc02010-02-21 13:10:26 +0100168 buf = xzalloc(charwidth * ((fontsize < 128) ? 128 : fontsize));
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000169 for (i = 0; i < fontsize; i++)
Harald Becker8ce1dc02010-02-21 13:10:26 +0100170 memcpy(buf + (i*charwidth), inbuf + (i*charsize), charsize);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000171
Denis Vlasenkoeef60772008-09-20 18:14:13 +0000172 { /* KDFONTOP */
173 struct console_font_op cfo;
Denis Vlasenkoeef60772008-09-20 18:14:13 +0000174 cfo.op = KD_FONT_OP_SET;
175 cfo.flags = 0;
Harald Becker8ce1dc02010-02-21 13:10:26 +0100176 cfo.width = width;
177 cfo.height = height;
Denis Vlasenkoeef60772008-09-20 18:14:13 +0000178 cfo.charcount = fontsize;
Harald Becker8ce1dc02010-02-21 13:10:26 +0100179 cfo.data = buf;
Denis Vlasenkoeef60772008-09-20 18:14:13 +0000180 xioctl(fd, KDFONTOP, &cfo);
Denis Vlasenkoeef60772008-09-20 18:14:13 +0000181 }
182
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000183 free(buf);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000184}
185
Harald Becker8ce1dc02010-02-21 13:10:26 +0100186/*
187 * Format of the Unicode information:
188 *
189 * For each font position <uc>*<seq>*<term>
190 * where <uc> is a 2-byte little endian Unicode value (PSF1)
191 * or an UTF-8 coded value (PSF2),
192 * <seq> = <ss><uc><uc>*, <ss> = psf1 ? 0xFFFE : 0xFE,
193 * <term> = psf1 ? 0xFFFF : 0xFF.
194 * and * denotes zero or more occurrences of the preceding item.
195 *
196 * Semantics:
197 * The leading <uc>* part gives Unicode symbols that are all
198 * represented by this font position. The following sequences
199 * are sequences of Unicode symbols - probably a symbol
200 * together with combining accents - also represented by
201 * this font position.
202 *
203 * Example:
204 * At the font position for a capital A-ring glyph, we
205 * may have:
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200206 * 00C5,212B,FFFE,0041,030A,FFFF
Harald Becker8ce1dc02010-02-21 13:10:26 +0100207 * Some font positions may be described by sequences only,
208 * namely when there is no precomposed Unicode value for the glyph.
209 */
210#if !ENABLE_FEATURE_LOADFONT_PSF2
211#define do_loadtable(fd, inbuf, tailsz, fontsize, psf2) \
212 do_loadtable(fd, inbuf, tailsz, fontsize)
213#endif
214static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize, int psf2)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215{
Harald Becker8ce1dc02010-02-21 13:10:26 +0100216#if !ENABLE_FEATURE_LOADFONT_PSF2
217/* gcc 4.3.1 code size: */
218# define psf2 0 /* +0 bytes */
219// const int psf2 = 0; /* +8 bytes */
220// enum { psf2 = 0 }; /* +13 bytes */
221#endif
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000222 struct unimapinit advice;
223 struct unimapdesc ud;
224 struct unipair *up;
225 int ct = 0, maxct;
226 int glyph;
Denis Vlasenko28703012006-12-19 20:32:02 +0000227 uint16_t unicode;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000228
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200229 maxct = tailsz; /* more than enough */
Harald Becker8ce1dc02010-02-21 13:10:26 +0100230 up = xmalloc(maxct * sizeof(*up));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000232 for (glyph = 0; glyph < fontsize; glyph++) {
Harald Becker8ce1dc02010-02-21 13:10:26 +0100233 while (tailsz > 0) {
234 if (!psf2) { /* PSF1 */
235 unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
236 tailsz -= 2;
237 inbuf += 2;
238 if (unicode == PSF1_SEPARATOR)
239 break;
240 } else { /* PSF2 */
241#if ENABLE_FEATURE_LOADFONT_PSF2
242 --tailsz;
243 unicode = *inbuf++;
244 if (unicode == PSF2_SEPARATOR) {
245 break;
246 } else if (unicode == PSF2_STARTSEQ) {
247 bb_error_msg_and_die("unicode sequences not implemented");
248 } else if (unicode >= 0xC0) {
249 if (unicode >= 0xFC)
250 unicode &= 0x01, maxct = 5;
251 else if (unicode >= 0xF8)
252 unicode &= 0x03, maxct = 4;
253 else if (unicode >= 0xF0)
254 unicode &= 0x07, maxct = 3;
255 else if (unicode >= 0xE0)
256 unicode &= 0x0F, maxct = 2;
257 else
258 unicode &= 0x1F, maxct = 1;
259 do {
260 if (tailsz <= 0 || *inbuf < 0x80 || *inbuf > 0xBF)
261 bb_error_msg_and_die("illegal UTF-8 character");
262 --tailsz;
263 unicode = (unicode << 6) + (*inbuf++ & 0x3F);
264 } while (--maxct > 0);
265 } else if (unicode >= 0x80) {
266 bb_error_msg_and_die("illegal UTF-8 character");
267 }
268#else
269 return;
270#endif
271 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 up[ct].unicode = unicode;
273 up[ct].fontpos = glyph;
274 ct++;
275 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000276 }
277
278 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100279 * this printf did not work on many kernels */
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000280
281 advice.advised_hashsize = 0;
282 advice.advised_hashstep = 0;
283 advice.advised_hashlevel = 0;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000284 xioctl(fd, PIO_UNIMAPCLR, &advice);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000285 ud.entry_ct = ct;
286 ud.entries = up;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000287 xioctl(fd, PIO_UNIMAP, &ud);
Harald Becker8ce1dc02010-02-21 13:10:26 +0100288#undef psf2
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000289}
290
Harald Becker8ce1dc02010-02-21 13:10:26 +0100291static void do_load(int fd, unsigned char *buffer, size_t len)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292{
Harald Becker8ce1dc02010-02-21 13:10:26 +0100293 int height;
294 int width = 8;
295 int charsize;
296 int fontsize = 256;
297 int has_table = 0;
298 unsigned char *font = buffer;
299 unsigned char *table;
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000300
Harald Becker8ce1dc02010-02-21 13:10:26 +0100301 if (len >= sizeof(struct psf1_header) && PSF1_MAGIC_OK(psf1h(buffer))) {
302 if (psf1h(buffer)->mode > PSF1_MAXMODE)
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000303 bb_error_msg_and_die("unsupported psf file mode");
Harald Becker8ce1dc02010-02-21 13:10:26 +0100304 if (psf1h(buffer)->mode & PSF1_MODE512)
305 fontsize = 512;
306 if (psf1h(buffer)->mode & PSF1_MODEHASTAB)
307 has_table = 1;
308 height = charsize = psf1h(buffer)->charsize;
309 font += sizeof(struct psf1_header);
310 } else
311#if ENABLE_FEATURE_LOADFONT_PSF2
312 if (len >= sizeof(struct psf2_header) && PSF2_MAGIC_OK(psf2h(buffer))) {
313 if (psf2h(buffer)->version > PSF2_MAXVERSION)
314 bb_error_msg_and_die("unsupported psf file version");
315 fontsize = psf2h(buffer)->length;
316 if (psf2h(buffer)->flags & PSF2_HAS_UNICODE_TABLE)
317 has_table = 2;
318 charsize = psf2h(buffer)->charsize;
319 height = psf2h(buffer)->height;
320 width = psf2h(buffer)->width;
321 font += psf2h(buffer)->headersize;
322 } else
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000323#endif
Harald Becker8ce1dc02010-02-21 13:10:26 +0100324#if ENABLE_FEATURE_LOADFONT_RAW
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200325 if (len == 9780) { /* file with three code pages? */
Harald Becker8ce1dc02010-02-21 13:10:26 +0100326 charsize = height = 16;
327 font += 40;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200328 } else if ((len & 0377) == 0) { /* bare font */
Harald Becker8ce1dc02010-02-21 13:10:26 +0100329 charsize = height = len / 256;
330 } else
331#endif
332 {
333 bb_error_msg_and_die("input file: bad length or unsupported font type");
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000334 }
335
Harald Becker8ce1dc02010-02-21 13:10:26 +0100336#if !defined(PIO_FONTX) || defined(__sparc__)
337 if (fontsize != 256)
338 bb_error_msg_and_die("only fontsize 256 supported");
339#endif
340
341 table = font + fontsize * charsize;
342 buffer += len;
343
344 if (table > buffer || (!has_table && table != buffer))
345 bb_error_msg_and_die("input file: bad length");
346
347 do_loadfont(fd, font, height, width, charsize, fontsize);
348
349 if (has_table)
350 do_loadtable(fd, table, buffer - table, fontsize, has_table - 1);
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000351}
352
Harald Becker8ce1dc02010-02-21 13:10:26 +0100353
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000354#if ENABLE_LOADFONT
355int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
356int loadfont_main(int argc UNUSED_PARAM, char **argv)
357{
358 size_t len;
Harald Becker8ce1dc02010-02-21 13:10:26 +0100359 unsigned char *buffer;
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000360
361 // no arguments allowed!
362 opt_complementary = "=0";
363 getopt32(argv, "");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000364
365 /*
366 * We used to look at the length of the input file
367 * with stat(); now that we accept compressed files,
368 * just read the entire file.
Denys Vlasenko2b288232016-11-03 20:57:37 +0100369 * Len was 32k, but latarcyrheb-sun32.psfu is 34377 bytes
370 * (it has largish Unicode map).
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000371 */
Denys Vlasenko2b288232016-11-03 20:57:37 +0100372 len = 128*1024;
Harald Becker8ce1dc02010-02-21 13:10:26 +0100373 buffer = xmalloc_read(STDIN_FILENO, &len);
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000374 // xmalloc_open_zipped_read_close(filename, &len);
Harald Becker8ce1dc02010-02-21 13:10:26 +0100375 if (!buffer)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000376 bb_perror_msg_and_die("error reading input font");
Harald Becker8ce1dc02010-02-21 13:10:26 +0100377 do_load(get_console_fd_or_die(), buffer, len);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000378
379 return EXIT_SUCCESS;
380}
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000381#endif
382
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000383#if ENABLE_SETFONT
384
Denis Vlasenko2bc5c032008-09-13 18:27:32 +0000385/*
386kbd-1.12:
387
388setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig]
389[-ou umap.orig] [-N] [font.new ...] [-m cmap] [-u umap] [-C console]
390[-hNN] [-v] [-V]
391
392-h NN Override font height
393-o file
394 Save previous font in file
395-O file
396 Save previous font and Unicode map in file
397-om file
398 Store console map in file
399-ou file
400 Save previous Unicode map in file
401-m file
402 Load console map or Unicode console map from file
403-u file
404 Load Unicode table describing the font from file
405 Example:
406 # cp866
407 0x00-0x7f idem
408 #
409 0x80 U+0410 # CYRILLIC CAPITAL LETTER A
410 0x81 U+0411 # CYRILLIC CAPITAL LETTER BE
411 0x82 U+0412 # CYRILLIC CAPITAL LETTER VE
412-C console
413 Set the font for the indicated console
414-v Verbose
415-V Version
416*/
417
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000418#if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
419static int ctoi(char *s)
420{
421 if (s[0] == '\'' && s[1] != '\0' && s[2] == '\'' && s[3] == '\0')
422 return s[1];
423 // U+ means 0x
424 if (s[0] == 'U' && s[1] == '+') {
425 s[0] = '0';
426 s[1] = 'x';
427 }
428 if (!isdigit(s[0]))
429 return -1;
430 return xstrtoul(s, 0);
431}
432#endif
433
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000434int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
435int setfont_main(int argc UNUSED_PARAM, char **argv)
436{
437 size_t len;
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000438 unsigned opts;
439 int fd;
Harald Becker8ce1dc02010-02-21 13:10:26 +0100440 unsigned char *buffer;
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000441 char *mapfilename;
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000442 const char *tty_name = CURRENT_TTY;
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000443
444 opt_complementary = "=1";
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000445 opts = getopt32(argv, "m:C:", &mapfilename, &tty_name);
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000446 argv += optind;
447
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +0100448 fd = xopen_nonblocking(tty_name);
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000449
450 if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not ""
Denis Vlasenko72fa70a2008-09-18 01:01:02 +0000451 if (*argv[0] != '/') {
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000452 // goto default fonts location. don't die if doesn't exist
453 chdir(CONFIG_DEFAULT_SETFONT_DIR "/consolefonts");
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000454 }
455 }
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000456 // load font
Denys Vlasenko2b288232016-11-03 20:57:37 +0100457 len = 128*1024;
Harald Becker8ce1dc02010-02-21 13:10:26 +0100458 buffer = xmalloc_open_zipped_read_close(*argv, &len);
459 if (!buffer)
Denis Vlasenko72fa70a2008-09-18 01:01:02 +0000460 bb_simple_perror_msg_and_die(*argv);
Harald Becker8ce1dc02010-02-21 13:10:26 +0100461 do_load(fd, buffer, len);
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000462
463 // load the screen map, if any
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000464 if (opts & 1) { // -m
465 unsigned mode = PIO_SCRNMAP;
466 void *map;
467
468 if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not ""
Denis Vlasenko72fa70a2008-09-18 01:01:02 +0000469 if (mapfilename[0] != '/') {
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000470 // goto default keymaps location
471 chdir(CONFIG_DEFAULT_SETFONT_DIR "/consoletrans");
472 }
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000473 }
Denis Vlasenko53f219e2008-09-16 19:35:42 +0000474 // fetch keymap
475 map = xmalloc_open_zipped_read_close(mapfilename, &len);
476 if (!map)
477 bb_simple_perror_msg_and_die(mapfilename);
478 // file size is 256 or 512 bytes? -> assume binary map
479 if (len == E_TABSZ || len == 2*E_TABSZ) {
480 if (len == 2*E_TABSZ)
481 mode = PIO_UNISCRNMAP;
482 }
483#if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
484 // assume textual Unicode console maps:
485 // 0x00 U+0000 # NULL (NUL)
486 // 0x01 U+0001 # START OF HEADING (SOH)
487 // 0x02 U+0002 # START OF TEXT (STX)
488 // 0x03 U+0003 # END OF TEXT (ETX)
489 else {
490 int i;
491 char *token[2];
492 parser_t *parser;
493
494 if (ENABLE_FEATURE_CLEAN_UP)
495 free(map);
496 map = xmalloc(E_TABSZ * sizeof(unsigned short));
497
498#define unicodes ((unsigned short *)map)
499 // fill vanilla map
500 for (i = 0; i < E_TABSZ; i++)
501 unicodes[i] = 0xf000 + i;
502
503 parser = config_open(mapfilename);
504 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL | PARSE_MIN_DIE)) {
505 // parse code/value pair
506 int a = ctoi(token[0]);
507 int b = ctoi(token[1]);
508 if (a < 0 || a >= E_TABSZ
509 || b < 0 || b > 65535
510 ) {
511 bb_error_msg_and_die("map format");
512 }
513 // patch map
514 unicodes[a] = b;
515 // unicode character is met?
516 if (b > 255)
517 mode = PIO_UNISCRNMAP;
518 }
519 if (ENABLE_FEATURE_CLEAN_UP)
520 config_close(parser);
521
522 if (mode != PIO_UNISCRNMAP) {
523#define asciis ((unsigned char *)map)
524 for (i = 0; i < E_TABSZ; i++)
525 asciis[i] = unicodes[i];
526#undef asciis
527 }
528#undef unicodes
529 }
530#endif // ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
531
532 // do set screen map
533 xioctl(fd, mode, map);
534
535 if (ENABLE_FEATURE_CLEAN_UP)
536 free(map);
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000537 }
538
539 return EXIT_SUCCESS;
540}
541#endif