wsfontload.c revision 1.17 1 /* $NetBSD: wsfontload.c,v 1.17 2011/08/31 13:32:42 joerg 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 <err.h>
37 #include <malloc.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 int c, res, wsfd, ffd, verbose = 0;
139 size_t len;
140 void *buf;
141
142 wsdev = DEFDEV;
143 f.fontwidth = DEFWIDTH;
144 f.fontheight = DEFHEIGHT;
145 f.firstchar = 0;
146 f.numchars = 256;
147 f.stride = 0;
148 f.encoding = DEFENC;
149 f.name = 0;
150 f.bitorder = DEFBITORDER;
151 f.byteorder = DEFBYTEORDER;
152
153 while ((c = getopt(argc, argv, "f:w:h:e:N:bBv")) != -1) {
154 switch (c) {
155 case 'f':
156 wsdev = optarg;
157 break;
158 case 'w':
159 if (sscanf(optarg, "%d", &f.fontwidth) != 1)
160 errx(1, "invalid font width");
161 break;
162 case 'h':
163 if (sscanf(optarg, "%d", &f.fontheight) != 1)
164 errx(1, "invalid font height");
165 break;
166 case 'e':
167 f.encoding = getencoding(optarg);
168 break;
169 case 'N':
170 f.name = optarg;
171 break;
172 case 'b':
173 f.bitorder = WSDISPLAY_FONTORDER_R2L;
174 break;
175 case 'B':
176 f.byteorder = WSDISPLAY_FONTORDER_R2L;
177 break;
178 case 'v':
179 verbose = 1;
180 break;
181 case '?':
182 default:
183 usage();
184 break;
185 }
186 }
187 argc -= optind;
188 argv += optind;
189
190 if (argc > 1)
191 usage();
192
193 wsfd = open(wsdev, O_RDWR, 0);
194 if (wsfd < 0)
195 err(2, "open ws-device %s", wsdev);
196
197 if (argc > 0) {
198 ffd = open(argv[0], O_RDONLY, 0);
199 if (ffd < 0)
200 err(4, "open font %s", argv[0]);
201 if (!f.name)
202 f.name = argv[0];
203 } else
204 ffd = 0;
205
206 if (!f.stride)
207 f.stride = (f.fontwidth + 7) / 8;
208 len = f.fontheight * f.numchars * f.stride;
209 if (!len)
210 errx(1, "invalid font size");
211
212 buf = malloc(len);
213 if (!buf)
214 errx(1, "malloc");
215 res = read(ffd, buf, len);
216 if (res < 0)
217 err(4, "read font");
218 if ((size_t)res != len)
219 errx(4, "short read");
220
221 f.data = buf;
222
223 if (verbose) {
224 printf("name: %s\n", f.name);
225 printf("firstchar: %d\n", f.firstchar);
226 printf("numchars: %d\n", f.numchars);
227 printf("encoding: %s (%d)\n",
228 rgetencoding(f.encoding), f.encoding);
229 printf("fontwidth: %d\n", f.fontwidth);
230 printf("fontheight: %d\n", f.fontheight);
231 printf("stride: %d\n", f.stride);
232 printf("bitorder: %s (%d)\n",
233 rgetfontorder(f.bitorder), f.bitorder);
234 printf("byteorder: %s (%d)\n",
235 rgetfontorder(f.byteorder), f.byteorder);
236 }
237
238 res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
239 if (res < 0)
240 err(3, "WSDISPLAYIO_LDFONT");
241
242 return (0);
243 }
244