wsfontload.c revision 1.19 1 /* $NetBSD: wsfontload.c,v 1.19 2017/06/23 02:16:39 macallan Exp $ */
2
3 /*
4 * Copyright (c) 1999
5 * Matthias Drochner. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/stat.h>
37 #include <err.h>
38
39 #include <dev/wscons/wsconsio.h>
40
41 #define DEFDEV "/dev/wsfont"
42 #define DEFWIDTH 8
43 #define DEFHEIGHT 16
44 #define DEFENC WSDISPLAY_FONTENC_ISO
45 #define DEFBITORDER WSDISPLAY_FONTORDER_L2R
46 #define DEFBYTEORDER WSDISPLAY_FONTORDER_L2R
47
48 __dead static void usage(void);
49 static int getencoding(char *);
50 static const char *rgetencoding(int);
51 static const char *rgetfontorder(int);
52
53 static struct {
54 const char *name;
55 int val;
56 } fontorders[] = {
57 { "known", WSDISPLAY_FONTORDER_KNOWN},
58 { "l2r", WSDISPLAY_FONTORDER_L2R},
59 { "r2l", WSDISPLAY_FONTORDER_R2L},
60 };
61
62 static struct {
63 const char *name;
64 int val;
65 } encodings[] = {
66 {"iso", WSDISPLAY_FONTENC_ISO},
67 {"ibm", WSDISPLAY_FONTENC_IBM},
68 {"pcvt", WSDISPLAY_FONTENC_PCVT},
69 {"iso7", WSDISPLAY_FONTENC_ISO7},
70 {"iso2", WSDISPLAY_FONTENC_ISO2},
71 {"koi8r", WSDISPLAY_FONTENC_KOI8_R},
72 };
73
74 static void
75 usage(void)
76 {
77
78 (void)fprintf(stderr,
79 "usage: %s [-Bbv] [-e encoding] [-f wsdev] [-h height]"
80 " [-N name] [-w width] [fontfile]\n",
81 getprogname());
82 exit(1);
83 }
84
85 /*
86 * map given fontorder to its string representation
87 */
88 static const char *
89 rgetfontorder(int fontorder)
90 {
91 size_t i;
92
93 for (i = 0; i < sizeof(fontorders) / sizeof(fontorders[0]); i++)
94 if (fontorders[i].val == fontorder)
95 return (fontorders[i].name);
96
97 return "unknown";
98 }
99
100 /*
101 * map given encoding to its string representation
102 */
103 static const char *
104 rgetencoding(int enc)
105 {
106 size_t i;
107
108 for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
109 if (encodings[i].val == enc)
110 return (encodings[i].name);
111
112 return "unknown";
113 }
114
115 /*
116 * map given encoding string to integer value
117 */
118 static int
119 getencoding(char *name)
120 {
121 size_t i;
122 int j;
123
124 for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
125 if (!strcmp(name, encodings[i].name))
126 return (encodings[i].val);
127
128 if (sscanf(name, "%d", &j) != 1)
129 errx(1, "invalid encoding");
130 return (j);
131 }
132
133 int
134 main(int argc, char **argv)
135 {
136 const char *wsdev;
137 struct wsdisplay_font f;
138 struct stat st;
139 int c, res, wsfd, ffd, verbose = 0;
140 size_t len;
141 void *buf;
142 char nbuf[65];
143
144 wsdev = DEFDEV;
145 f.fontwidth = DEFWIDTH;
146 f.fontheight = DEFHEIGHT;
147 f.firstchar = 0;
148 f.numchars = 256;
149 f.stride = 0;
150 f.encoding = DEFENC;
151 f.name = 0;
152 f.bitorder = DEFBITORDER;
153 f.byteorder = DEFBYTEORDER;
154
155 while ((c = getopt(argc, argv, "f:w:h:e:N:bBv")) != -1) {
156 switch (c) {
157 case 'f':
158 wsdev = optarg;
159 break;
160 case 'w':
161 if (sscanf(optarg, "%d", &f.fontwidth) != 1)
162 errx(1, "invalid font width");
163 break;
164 case 'h':
165 if (sscanf(optarg, "%d", &f.fontheight) != 1)
166 errx(1, "invalid font height");
167 break;
168 case 'e':
169 f.encoding = getencoding(optarg);
170 break;
171 case 'N':
172 f.name = optarg;
173 break;
174 case 'b':
175 f.bitorder = WSDISPLAY_FONTORDER_R2L;
176 break;
177 case 'B':
178 f.byteorder = WSDISPLAY_FONTORDER_R2L;
179 break;
180 case 'v':
181 verbose = 1;
182 break;
183 case '?':
184 default:
185 usage();
186 break;
187 }
188 }
189 argc -= optind;
190 argv += optind;
191
192 if (argc > 1)
193 usage();
194
195 wsfd = open(wsdev, O_RDWR, 0);
196 if (wsfd < 0)
197 err(2, "open ws-device %s", wsdev);
198
199 if (argc > 0) {
200 ffd = open(argv[0], O_RDONLY, 0);
201 if (ffd < 0)
202 err(4, "open font %s", argv[0]);
203 if (!f.name)
204 f.name = argv[0];
205 } else
206 ffd = 0;
207
208 if (!f.stride)
209 f.stride = (f.fontwidth + 7) / 8;
210 len = f.fontheight * f.numchars * f.stride;
211 if (fstat(ffd, &st) == 0) {
212 if (len != st.st_size) {
213 uint32_t foo = 0;
214 char b[65];
215 len = st.st_size;
216 /* read header */
217 read(ffd, b, 4);
218 if (strncmp(b, "WSFT", 4) != 0)
219 errx(1, "invalid wsf file ");
220 read(ffd, b, 64);
221 b[64] = 0;
222 strcpy(nbuf, b);
223 f.name = nbuf;
224 read(ffd, &foo, 4);
225 f.firstchar = le32toh(foo);
226 read(ffd, &foo, 4);
227 f.numchars = le32toh(foo);
228 read(ffd, &foo, 4);
229 f.encoding = le32toh(foo);
230 read(ffd, &foo, 4);
231 f.fontwidth = le32toh(foo);
232 read(ffd, &foo, 4);
233 f.fontheight = le32toh(foo);
234 read(ffd, &foo, 4);
235 f.stride = le32toh(foo);
236 read(ffd, &foo, 4);
237 f.bitorder = le32toh(foo);
238 read(ffd, &foo, 4);
239 f.byteorder = le32toh(foo);
240 len = f.numchars * f.fontheight * f.stride;
241 }
242 }
243
244 if (!len)
245 errx(1, "invalid font size");
246
247 buf = malloc(len);
248 if (!buf)
249 errx(1, "malloc");
250 res = read(ffd, buf, len);
251 if (res < 0)
252 err(4, "read font");
253 if ((size_t)res != len)
254 errx(4, "short read");
255
256 f.data = buf;
257
258 if (verbose) {
259 printf("name: %s\n", f.name);
260 printf("firstchar: %d\n", f.firstchar);
261 printf("numchars: %d\n", f.numchars);
262 printf("encoding: %s (%d)\n",
263 rgetencoding(f.encoding), f.encoding);
264 printf("fontwidth: %d\n", f.fontwidth);
265 printf("fontheight: %d\n", f.fontheight);
266 printf("stride: %d\n", f.stride);
267 printf("bitorder: %s (%d)\n",
268 rgetfontorder(f.bitorder), f.bitorder);
269 printf("byteorder: %s (%d)\n",
270 rgetfontorder(f.byteorder), f.byteorder);
271 }
272
273 res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
274 if (res < 0)
275 err(3, "WSDISPLAYIO_LDFONT");
276
277 return (0);
278 }
279