Extract usage information into a separate file.
[oweals/busybox.git] / console-tools / loadfont.c
1 /* vi: set sw=4 ts=4: */
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.)
9  */
10 #include "internal.h"
11 #include <stdio.h>
12 #include <string.h>
13 #include <fcntl.h>
14 #include <memory.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <dirent.h>
20 #include <errno.h>
21 #include <sys/ioctl.h>
22 #include <sys/kd.h>
23 #include <endian.h>
24
25 #define PSF_MAGIC1      0x36
26 #define PSF_MAGIC2      0x04
27
28 #define PSF_MODE512    0x01
29 #define PSF_MODEHASTAB 0x02
30 #define PSF_MAXMODE    0x03
31 #define PSF_SEPARATOR  0xFFFF
32
33 struct psf_header {
34         unsigned char magic1, magic2;   /* Magic number */
35         unsigned char mode;                     /* PSF font mode */
36         unsigned char charsize;         /* Character size */
37 };
38
39 #define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
40
41 static void loadnewfont(int fd);
42
43 extern int loadfont_main(int argc, char **argv)
44 {
45         int fd;
46
47         if (argc>=2 && *argv[1]=='-') {
48                 usage(loadfont_usage);
49         }
50
51         fd = open("/dev/tty0", O_RDWR);
52         if (fd < 0) {
53                 errorMsg("Error opening /dev/tty0: %s\n", strerror(errno));
54                 return( FALSE);
55         }
56         loadnewfont(fd);
57
58         return( TRUE);
59 }
60
61 static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
62 {
63         char buf[16384];
64         int i;
65
66         memset(buf, 0, sizeof(buf));
67
68         if (unit < 1 || unit > 32) {
69                 errorMsg("Bad character size %d\n", unit);
70                 exit(1);
71         }
72
73         for (i = 0; i < fontsize; i++)
74                 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
75
76 #if defined( PIO_FONTX ) && !defined( __sparc__ )
77         {
78                 struct consolefontdesc cfd;
79
80                 cfd.charcount = fontsize;
81                 cfd.charheight = unit;
82                 cfd.chardata = buf;
83
84                 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
85                         return;                         /* success */
86                 perror("PIO_FONTX ioctl error (trying PIO_FONT)");
87         }
88 #endif
89         if (ioctl(fd, PIO_FONT, buf)) {
90                 perror("PIO_FONT ioctl error");
91                 exit(1);
92         }
93 }
94
95 static void
96 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
97 {
98         struct unimapinit advice;
99         struct unimapdesc ud;
100         struct unipair *up;
101         int ct = 0, maxct;
102         int glyph;
103         u_short unicode;
104
105         maxct = tailsz;                         /* more than enough */
106         up = (struct unipair *) malloc(maxct * sizeof(struct unipair));
107
108         if (!up) {
109                 errorMsg("Out of memory?\n");
110                 exit(1);
111         }
112         for (glyph = 0; glyph < fontsize; glyph++) {
113                 while (tailsz >= 2) {
114                         unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
115                         tailsz -= 2;
116                         inbuf += 2;
117                         if (unicode == PSF_SEPARATOR)
118                                 break;
119                         up[ct].unicode = unicode;
120                         up[ct].fontpos = glyph;
121                         ct++;
122                 }
123         }
124
125         /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
126            this printf did not work on many kernels */
127
128         advice.advised_hashsize = 0;
129         advice.advised_hashstep = 0;
130         advice.advised_hashlevel = 0;
131         if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
132 #ifdef ENOIOCTLCMD
133                 if (errno == ENOIOCTLCMD) {
134                         errorMsg("It seems this kernel is older than 1.1.92\n");
135                         errorMsg("No Unicode mapping table loaded.\n");
136                 } else
137 #endif
138                         perror("PIO_UNIMAPCLR");
139                 exit(1);
140         }
141         ud.entry_ct = ct;
142         ud.entries = up;
143         if (ioctl(fd, PIO_UNIMAP, &ud)) {
144 #if 0
145                 if (errno == ENOMEM) {
146                         /* change advice parameters */
147                 }
148 #endif
149                 perror("PIO_UNIMAP");
150                 exit(1);
151         }
152 }
153
154 static void loadnewfont(int fd)
155 {
156         int unit;
157         char inbuf[32768];                      /* primitive */
158         unsigned int inputlth, offset;
159
160         /*
161          * We used to look at the length of the input file
162          * with stat(); now that we accept compressed files,
163          * just read the entire file.
164          */
165         inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
166         if (ferror(stdin)) {
167                 perror("Error reading input font");
168                 exit(1);
169         }
170         /* use malloc/realloc in case of giant files;
171            maybe these do not occur: 16kB for the font,
172            and 16kB for the map leaves 32 unicode values
173            for each font position */
174         if (!feof(stdin)) {
175                 perror("Font too large");
176                 exit(1);
177         }
178
179         /* test for psf first */
180         {
181                 struct psf_header psfhdr;
182                 int fontsize;
183                 int hastable;
184                 unsigned int head0, head;
185
186                 if (inputlth < sizeof(struct psf_header))
187                         goto no_psf;
188
189                 psfhdr = *(struct psf_header *) &inbuf[0];
190
191                 if (!PSF_MAGIC_OK(psfhdr))
192                         goto no_psf;
193
194                 if (psfhdr.mode > PSF_MAXMODE) {
195                         errorMsg("Unsupported psf file mode\n");
196                         exit(1);
197                 }
198                 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
199 #if !defined( PIO_FONTX ) || defined( __sparc__ )
200                 if (fontsize != 256) {
201                         errorMsg("Only fontsize 256 supported\n");
202                         exit(1);
203                 }
204 #endif
205                 hastable = (psfhdr.mode & PSF_MODEHASTAB);
206                 unit = psfhdr.charsize;
207                 head0 = sizeof(struct psf_header);
208
209                 head = head0 + fontsize * unit;
210                 if (head > inputlth || (!hastable && head != inputlth)) {
211                         errorMsg("Input file: bad length\n");
212                         exit(1);
213                 }
214                 do_loadfont(fd, inbuf + head0, unit, fontsize);
215                 if (hastable)
216                         do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
217                 return;
218         }
219   no_psf:
220
221         /* file with three code pages? */
222         if (inputlth == 9780) {
223                 offset = 40;
224                 unit = 16;
225         } else {
226                 /* bare font */
227                 if (inputlth & 0377) {
228                         errorMsg("Bad input file size\n");
229                         exit(1);
230                 }
231                 offset = 0;
232                 unit = inputlth / 256;
233         }
234         do_loadfont(fd, inbuf + offset, unit, 256);
235 }