summaryrefslogtreecommitdiff
path: root/console-tools/loadfont.c
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
committerErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
commite49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch)
treec90bda10731ad9333ce3b404f993354c9fc104b8 /console-tools/loadfont.c
parentc0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff)
downloadbusybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'console-tools/loadfont.c')
-rw-r--r--console-tools/loadfont.c196
1 files changed, 98 insertions, 98 deletions
diff --git a/console-tools/loadfont.c b/console-tools/loadfont.c
index e44525d9c..64b725610 100644
--- a/console-tools/loadfont.c
+++ b/console-tools/loadfont.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * loadfont.c - Eugene Crosser & Andries Brouwer 3 * loadfont.c - Eugene Crosser & Andries Brouwer
3 * 4 *
@@ -30,72 +31,69 @@
30#define PSF_SEPARATOR 0xFFFF 31#define PSF_SEPARATOR 0xFFFF
31 32
32static const char loadfont_usage[] = "loadfont\n" 33static const char loadfont_usage[] = "loadfont\n"
33"\n" 34 "\n" "\tLoad a console font from standard input.\n" "\n";
34"\tLoad a console font from standard input.\n"
35"\n";
36 35
37struct psf_header 36struct psf_header {
38{ 37 unsigned char magic1, magic2; /* Magic number */
39 unsigned char magic1, magic2; /* Magic number */ 38 unsigned char mode; /* PSF font mode */
40 unsigned char mode; /* PSF font mode */ 39 unsigned char charsize; /* Character size */
41 unsigned char charsize; /* Character size */
42}; 40};
43 41
44#define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2) 42#define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
45 43
46static void loadnewfont(int fd); 44static void loadnewfont(int fd);
47 45
48extern int 46extern int loadfont_main(int argc, char **argv)
49loadfont_main(int argc, char **argv)
50{ 47{
51 int fd; 48 int fd;
52 49
53 fd = open("/dev/tty0", O_RDWR); 50 fd = open("/dev/tty0", O_RDWR);
54 if (fd < 0) { 51 if (fd < 0) {
55 fprintf(stderr, "Error opening /dev/tty0: %s\n", strerror(errno)); 52 fprintf(stderr, "Error opening /dev/tty0: %s\n", strerror(errno));
56 return 1; 53 return 1;
57 } 54 }
58 loadnewfont(fd); 55 loadnewfont(fd);
59 56
60 return 0; 57 return 0;
61} 58}
62 59
63static void 60static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
64do_loadfont(int fd, char *inbuf, int unit, int fontsize) { 61{
65 char buf[16384]; 62 char buf[16384];
66 int i; 63 int i;
67 64
68 memset(buf,0,sizeof(buf)); 65 memset(buf, 0, sizeof(buf));
69 66
70 if (unit < 1 || unit > 32) { 67 if (unit < 1 || unit > 32) {
71 fprintf(stderr, "Bad character size %d\n", unit); 68 fprintf(stderr, "Bad character size %d\n", unit);
72 exit(1); 69 exit(1);
73 } 70 }
74 71
75 for (i = 0; i < fontsize; i++) 72 for (i = 0; i < fontsize; i++)
76 memcpy(buf+(32*i), inbuf+(unit*i), unit); 73 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
77 74
78#if defined( PIO_FONTX ) && !defined( __sparc__ ) 75#if defined( PIO_FONTX ) && !defined( __sparc__ )
79 { 76 {
80 struct consolefontdesc cfd; 77 struct consolefontdesc cfd;
81 78
82 cfd.charcount = fontsize; 79 cfd.charcount = fontsize;
83 cfd.charheight = unit; 80 cfd.charheight = unit;
84 cfd.chardata = buf; 81 cfd.chardata = buf;
85 82
86 if (ioctl(fd, PIO_FONTX, &cfd) == 0) 83 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
87 return; /* success */ 84 return; /* success */
88 perror("PIO_FONTX ioctl error (trying PIO_FONT)"); 85 perror("PIO_FONTX ioctl error (trying PIO_FONT)");
89 } 86 }
90#endif 87#endif
91 if (ioctl(fd, PIO_FONT, buf)) { 88 if (ioctl(fd, PIO_FONT, buf)) {
92 perror("PIO_FONT ioctl error"); 89 perror("PIO_FONT ioctl error");
93 exit(1); 90 exit(1);
94 } 91 }
95} 92}
96 93
97static void 94static void
98do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize) { 95do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
96{
99 struct unimapinit advice; 97 struct unimapinit advice;
100 struct unimapdesc ud; 98 struct unimapdesc ud;
101 struct unipair *up; 99 struct unipair *up;
@@ -103,23 +101,24 @@ do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize) {
103 int glyph; 101 int glyph;
104 u_short unicode; 102 u_short unicode;
105 103
106 maxct = tailsz; /* more than enough */ 104 maxct = tailsz; /* more than enough */
107 up = (struct unipair *) malloc(maxct * sizeof(struct unipair)); 105 up = (struct unipair *) malloc(maxct * sizeof(struct unipair));
106
108 if (!up) { 107 if (!up) {
109 fprintf(stderr, "Out of memory?\n"); 108 fprintf(stderr, "Out of memory?\n");
110 exit(1); 109 exit(1);
111 } 110 }
112 for (glyph = 0; glyph < fontsize; glyph++) { 111 for (glyph = 0; glyph < fontsize; glyph++) {
113 while (tailsz >= 2) { 112 while (tailsz >= 2) {
114 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0]; 113 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
115 tailsz -= 2; 114 tailsz -= 2;
116 inbuf += 2; 115 inbuf += 2;
117 if (unicode == PSF_SEPARATOR) 116 if (unicode == PSF_SEPARATOR)
118 break; 117 break;
119 up[ct].unicode = unicode; 118 up[ct].unicode = unicode;
120 up[ct].fontpos = glyph; 119 up[ct].fontpos = glyph;
121 ct++; 120 ct++;
122 } 121 }
123 } 122 }
124 123
125 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP 124 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
@@ -128,33 +127,33 @@ do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize) {
128 advice.advised_hashsize = 0; 127 advice.advised_hashsize = 0;
129 advice.advised_hashstep = 0; 128 advice.advised_hashstep = 0;
130 advice.advised_hashlevel = 0; 129 advice.advised_hashlevel = 0;
131 if(ioctl(fd, PIO_UNIMAPCLR, &advice)) { 130 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
132#ifdef ENOIOCTLCMD 131#ifdef ENOIOCTLCMD
133 if (errno == ENOIOCTLCMD) { 132 if (errno == ENOIOCTLCMD) {
134 fprintf(stderr, "It seems this kernel is older than 1.1.92\n"); 133 fprintf(stderr, "It seems this kernel is older than 1.1.92\n");
135 fprintf(stderr, "No Unicode mapping table loaded.\n"); 134 fprintf(stderr, "No Unicode mapping table loaded.\n");
136 } else 135 } else
137#endif 136#endif
138 perror("PIO_UNIMAPCLR"); 137 perror("PIO_UNIMAPCLR");
139 exit(1); 138 exit(1);
140 } 139 }
141 ud.entry_ct = ct; 140 ud.entry_ct = ct;
142 ud.entries = up; 141 ud.entries = up;
143 if(ioctl(fd, PIO_UNIMAP, &ud)) { 142 if (ioctl(fd, PIO_UNIMAP, &ud)) {
144#if 0 143#if 0
145 if (errno == ENOMEM) { 144 if (errno == ENOMEM) {
146 /* change advice parameters */ 145 /* change advice parameters */
147 } 146 }
148#endif 147#endif
149 perror("PIO_UNIMAP"); 148 perror("PIO_UNIMAP");
150 exit(1); 149 exit(1);
151 } 150 }
152} 151}
153 152
154static void 153static void loadnewfont(int fd)
155loadnewfont(int fd) { 154{
156 int unit; 155 int unit;
157 char inbuf[32768]; /* primitive */ 156 char inbuf[32768]; /* primitive */
158 int inputlth, offset; 157 int inputlth, offset;
159 158
160 /* 159 /*
@@ -178,57 +177,58 @@ loadnewfont(int fd) {
178 177
179 /* test for psf first */ 178 /* test for psf first */
180 { 179 {
181 struct psf_header psfhdr; 180 struct psf_header psfhdr;
182 int fontsize; 181 int fontsize;
183 int hastable; 182 int hastable;
184 int head0, head; 183 int head0, head;
185 184
186 if (inputlth < sizeof(struct psf_header)) 185 if (inputlth < sizeof(struct psf_header))
187 goto no_psf; 186 goto no_psf;
188 187
189 psfhdr = * (struct psf_header *) &inbuf[0]; 188 psfhdr = *(struct psf_header *) &inbuf[0];
190 189
191 if (!PSF_MAGIC_OK(psfhdr)) 190 if (!PSF_MAGIC_OK(psfhdr))
192 goto no_psf; 191 goto no_psf;
193 192
194 if (psfhdr.mode > PSF_MAXMODE) { 193 if (psfhdr.mode > PSF_MAXMODE) {
195 fprintf(stderr, "Unsupported psf file mode\n"); 194 fprintf(stderr, "Unsupported psf file mode\n");
196 exit(1); 195 exit(1);
197 } 196 }
198 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256); 197 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
199#if !defined( PIO_FONTX ) || defined( __sparc__ ) 198#if !defined( PIO_FONTX ) || defined( __sparc__ )
200 if (fontsize != 256) { 199 if (fontsize != 256) {
201 fprintf(stderr, "Only fontsize 256 supported\n"); 200 fprintf(stderr, "Only fontsize 256 supported\n");
202 exit(1); 201 exit(1);
203 } 202 }
204#endif 203#endif
205 hastable = (psfhdr.mode & PSF_MODEHASTAB); 204 hastable = (psfhdr.mode & PSF_MODEHASTAB);
206 unit = psfhdr.charsize; 205 unit = psfhdr.charsize;
207 head0 = sizeof(struct psf_header); 206 head0 = sizeof(struct psf_header);
208 head = head0 + fontsize*unit; 207
209 if (head > inputlth || (!hastable && head != inputlth)) { 208 head = head0 + fontsize * unit;
210 fprintf(stderr, "Input file: bad length\n"); 209 if (head > inputlth || (!hastable && head != inputlth)) {
211 exit(1); 210 fprintf(stderr, "Input file: bad length\n");
212 } 211 exit(1);
213 do_loadfont(fd, inbuf + head0, unit, fontsize); 212 }
214 if (hastable) 213 do_loadfont(fd, inbuf + head0, unit, fontsize);
215 do_loadtable(fd, inbuf + head, inputlth-head, fontsize); 214 if (hastable)
216 return; 215 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
216 return;
217 } 217 }
218 no_psf: 218 no_psf:
219 219
220 /* file with three code pages? */ 220 /* file with three code pages? */
221 if (inputlth == 9780) { 221 if (inputlth == 9780) {
222 offset = 40; 222 offset = 40;
223 unit = 16; 223 unit = 16;
224 } else { 224 } else {
225 /* bare font */ 225 /* bare font */
226 if (inputlth & 0377) { 226 if (inputlth & 0377) {
227 fprintf(stderr, "Bad input file size\n"); 227 fprintf(stderr, "Bad input file size\n");
228 exit(1); 228 exit(1);
229 } 229 }
230 offset = 0; 230 offset = 0;
231 unit = inputlth/256; 231 unit = inputlth / 256;
232 } 232 }
233 do_loadfont(fd, inbuf+offset, unit, 256); 233 do_loadfont(fd, inbuf + offset, unit, 256);
234} 234}