xref: /netbsd-src/usr.sbin/wsfontload/wsfontload.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /* $NetBSD: wsfontload.c,v 1.13 2008/05/26 12:15:42 drochner 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 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 };
72 
73 static void
74 usage(void)
75 {
76 
77 	(void)fprintf(stderr,
78 		"usage: %s [-f wsdev] [-w width] [-h height] [-e encoding]"
79 		" [-N name] [-b] [-B] [fontfile]\n",
80 		      getprogname());
81 	exit(1);
82 }
83 
84 /*
85  * map given fontorder to its string representation
86  */
87 static const char *
88 rgetfontorder(int fontorder)
89 {
90 	int i;
91 
92 	for (i = 0; i < sizeof(fontorders) / sizeof(fontorders[0]); i++)
93 		if (fontorders[i].val == fontorder)
94 			return (fontorders[i].name);
95 
96 	return "unknown";
97 }
98 
99 /*
100  * map given encoding to its string representation
101  */
102 static const char *
103 rgetencoding(int enc)
104 {
105 	int i;
106 
107 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
108 		if (encodings[i].val == enc)
109 			return (encodings[i].name);
110 
111 	return "unknown";
112 }
113 
114 /*
115  * map given encoding string to integer value
116  */
117 static int
118 getencoding(char *name)
119 {
120 	int i;
121 
122 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
123 		if (!strcmp(name, encodings[i].name))
124 			return (encodings[i].val);
125 
126 	if (sscanf(name, "%d", &i) != 1)
127 		errx(1, "invalid encoding");
128 	return (i);
129 }
130 
131 int
132 main(int argc, char **argv)
133 {
134 	const char *wsdev;
135 	struct wsdisplay_font f;
136 	int c, res, wsfd, ffd, verbose = 0;
137 	size_t len;
138 	void *buf;
139 
140 	wsdev = DEFDEV;
141 	f.fontwidth = DEFWIDTH;
142 	f.fontheight = DEFHEIGHT;
143 	f.firstchar = 0;
144 	f.numchars = 256;
145 	f.stride = 0;
146 	f.encoding = DEFENC;
147 	f.name = 0;
148 	f.bitorder = DEFBITORDER;
149 	f.byteorder = DEFBYTEORDER;
150 
151 	while ((c = getopt(argc, argv, "f:w:h:e:N:bBv")) != -1) {
152 		switch (c) {
153 		case 'f':
154 			wsdev = optarg;
155 			break;
156 		case 'w':
157 			if (sscanf(optarg, "%d", &f.fontwidth) != 1)
158 				errx(1, "invalid font width");
159 			break;
160 		case 'h':
161 			if (sscanf(optarg, "%d", &f.fontheight) != 1)
162 				errx(1, "invalid font height");
163 			break;
164 		case 'e':
165 			f.encoding = getencoding(optarg);
166 			break;
167 		case 'N':
168 			f.name = optarg;
169 			break;
170 		case 'b':
171 			f.bitorder = WSDISPLAY_FONTORDER_R2L;
172 			break;
173 		case 'B':
174 			f.byteorder = WSDISPLAY_FONTORDER_R2L;
175 			break;
176 		case 'v':
177 			verbose = 1;
178 			break;
179 		case '?':
180 		default:
181 			usage();
182 			break;
183 		}
184 	}
185 	argc -= optind;
186 	argv += optind;
187 
188 	if (argc > 1)
189 		usage();
190 
191 	wsfd = open(wsdev, O_RDWR, 0);
192 	if (wsfd < 0)
193 		err(2, "open ws-device %s", wsdev);
194 
195 	if (argc > 0) {
196 		ffd = open(argv[0], O_RDONLY, 0);
197 		if (ffd < 0)
198 			err(4, "open font %s", argv[0]);
199 		if (!f.name)
200 			f.name = argv[0];
201 	} else
202 		ffd = 0;
203 
204 	if (!f.stride)
205 		f.stride = (f.fontwidth + 7) / 8;
206 	len = f.fontheight * f.numchars * f.stride;
207 	if (!len)
208 		errx(1, "invalid font size");
209 
210 	buf = malloc(len);
211 	if (!buf)
212 		errx(1, "malloc");
213 	res = read(ffd, buf, len);
214 	if (res < 0)
215 		err(4, "read font");
216 	if (res != len)
217 		errx(4, "short read");
218 
219 	f.data = buf;
220 
221 	if (verbose) {
222 		printf("name:       %s\n", f.name);
223 		printf("firstchar:  %d\n", f.firstchar);
224 		printf("numchars:   %d\n", f.numchars);
225 		printf("encoding:   %s (%d)\n",
226 			rgetencoding(f.encoding), f.encoding);
227 		printf("fontwidth:  %d\n", f.fontwidth);
228 		printf("fontheight: %d\n", f.fontheight);
229 		printf("stride:     %d\n", f.stride);
230 		printf("bitorder:   %s (%d)\n",
231 			rgetfontorder(f.bitorder), f.bitorder);
232 		printf("byteorder:  %s (%d)\n",
233 			rgetfontorder(f.byteorder), f.byteorder);
234 	}
235 
236 	res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
237 	if (res < 0)
238 		err(3, "WSDISPLAYIO_LDFONT");
239 
240 	return (0);
241 }
242