xref: /netbsd-src/usr.sbin/wsfontload/wsfontload.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: wsfontload.c,v 1.14 2009/04/19 02:25:48 lukem 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 	size_t 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 	size_t 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 	size_t i;
121 	int j;
122 
123 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
124 		if (!strcmp(name, encodings[i].name))
125 			return (encodings[i].val);
126 
127 	if (sscanf(name, "%d", &j) != 1)
128 		errx(1, "invalid encoding");
129 	return (j);
130 }
131 
132 int
133 main(int argc, char **argv)
134 {
135 	const char *wsdev;
136 	struct wsdisplay_font f;
137 	int c, res, wsfd, ffd, verbose = 0;
138 	size_t len;
139 	void *buf;
140 
141 	wsdev = DEFDEV;
142 	f.fontwidth = DEFWIDTH;
143 	f.fontheight = DEFHEIGHT;
144 	f.firstchar = 0;
145 	f.numchars = 256;
146 	f.stride = 0;
147 	f.encoding = DEFENC;
148 	f.name = 0;
149 	f.bitorder = DEFBITORDER;
150 	f.byteorder = DEFBYTEORDER;
151 
152 	while ((c = getopt(argc, argv, "f:w:h:e:N:bBv")) != -1) {
153 		switch (c) {
154 		case 'f':
155 			wsdev = optarg;
156 			break;
157 		case 'w':
158 			if (sscanf(optarg, "%d", &f.fontwidth) != 1)
159 				errx(1, "invalid font width");
160 			break;
161 		case 'h':
162 			if (sscanf(optarg, "%d", &f.fontheight) != 1)
163 				errx(1, "invalid font height");
164 			break;
165 		case 'e':
166 			f.encoding = getencoding(optarg);
167 			break;
168 		case 'N':
169 			f.name = optarg;
170 			break;
171 		case 'b':
172 			f.bitorder = WSDISPLAY_FONTORDER_R2L;
173 			break;
174 		case 'B':
175 			f.byteorder = WSDISPLAY_FONTORDER_R2L;
176 			break;
177 		case 'v':
178 			verbose = 1;
179 			break;
180 		case '?':
181 		default:
182 			usage();
183 			break;
184 		}
185 	}
186 	argc -= optind;
187 	argv += optind;
188 
189 	if (argc > 1)
190 		usage();
191 
192 	wsfd = open(wsdev, O_RDWR, 0);
193 	if (wsfd < 0)
194 		err(2, "open ws-device %s", wsdev);
195 
196 	if (argc > 0) {
197 		ffd = open(argv[0], O_RDONLY, 0);
198 		if (ffd < 0)
199 			err(4, "open font %s", argv[0]);
200 		if (!f.name)
201 			f.name = argv[0];
202 	} else
203 		ffd = 0;
204 
205 	if (!f.stride)
206 		f.stride = (f.fontwidth + 7) / 8;
207 	len = f.fontheight * f.numchars * f.stride;
208 	if (!len)
209 		errx(1, "invalid font size");
210 
211 	buf = malloc(len);
212 	if (!buf)
213 		errx(1, "malloc");
214 	res = read(ffd, buf, len);
215 	if (res < 0)
216 		err(4, "read font");
217 	if ((size_t)res != len)
218 		errx(4, "short read");
219 
220 	f.data = buf;
221 
222 	if (verbose) {
223 		printf("name:       %s\n", f.name);
224 		printf("firstchar:  %d\n", f.firstchar);
225 		printf("numchars:   %d\n", f.numchars);
226 		printf("encoding:   %s (%d)\n",
227 			rgetencoding(f.encoding), f.encoding);
228 		printf("fontwidth:  %d\n", f.fontwidth);
229 		printf("fontheight: %d\n", f.fontheight);
230 		printf("stride:     %d\n", f.stride);
231 		printf("bitorder:   %s (%d)\n",
232 			rgetfontorder(f.bitorder), f.bitorder);
233 		printf("byteorder:  %s (%d)\n",
234 			rgetfontorder(f.byteorder), f.byteorder);
235 	}
236 
237 	res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
238 	if (res < 0)
239 		err(3, "WSDISPLAYIO_LDFONT");
240 
241 	return (0);
242 }
243