Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/console-tools/loadfont.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.815  
changed lines
  Added in v.816