xref: /netbsd-src/usr.sbin/wsfontload/wsfontload.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /* $NetBSD: wsfontload.c,v 1.6 2001/02/19 23:22:50 cgd 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed for the NetBSD Project
18  *	by Matthias Drochner.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include <stdio.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/ioctl.h>
42 #include <err.h>
43 #include <malloc.h>
44 
45 #include <dev/wscons/wsconsio.h>
46 
47 #define DEFDEV		"/dev/ttyEcfg"
48 #define DEFWIDTH	8
49 #define DEFHEIGHT	16
50 #define DEFENC		WSDISPLAY_FONTENC_ISO
51 #define DEFBITORDER	WSDISPLAY_FONTORDER_L2R
52 #define DEFBYTEORDER	WSDISPLAY_FONTORDER_L2R
53 
54 int main __P((int, char**));
55 static void usage __P((void));
56 static int getencoding __P((char *));
57 static char *rgetencoding __P((int));
58 static char *rgetfontorder __P((int));
59 
60 static struct {
61 	char *name;
62 	int val;
63 } fontorders[] = {
64 	{ "known", WSDISPLAY_FONTORDER_KNOWN},
65 	{ "l2r", WSDISPLAY_FONTORDER_L2R},
66 	{ "r2l", WSDISPLAY_FONTORDER_R2L},
67 };
68 
69 static struct {
70 	char *name;
71 	int val;
72 } encodings[] = {
73 	{"iso", WSDISPLAY_FONTENC_ISO},
74 	{"ibm", WSDISPLAY_FONTENC_IBM},
75 	{"pcvt", WSDISPLAY_FONTENC_PCVT},
76 };
77 
78 static void
79 usage()
80 {
81 
82 	(void)fprintf(stderr,
83 		"Usage: %s [-f wsdev] [-w width] [-h height] [-e encoding]"
84 		" [-N name] [-b] [-B] [fontfile]\n",
85 		      getprogname());
86 	exit(1);
87 }
88 
89 /*
90  * map given fontorder to it's string representation
91  */
92 static char *
93 rgetfontorder(fontorder)
94 	int fontorder;
95 {
96 	int i;
97 
98 	for (i = 0; i < sizeof(fontorders) / sizeof(fontorders[0]); i++)
99 		if (fontorders[i].val == fontorder)
100 			return (fontorders[i].name);
101 
102 	return "unknown";
103 }
104 
105 /*
106  * map given encoding to it's string representation
107  */
108 static char *
109 rgetencoding(enc)
110 	int enc;
111 {
112 	int i;
113 
114 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
115 		if (encodings[i].val == enc)
116 			return (encodings[i].name);
117 
118 	return "unknown";
119 }
120 
121 /*
122  * map given encoding string to integer value
123  */
124 static int
125 getencoding(name)
126 	char *name;
127 {
128 	int i;
129 
130 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
131 		if (!strcmp(name, encodings[i].name))
132 			return (encodings[i].val);
133 
134 	if (sscanf(name, "%d", &i) != 1)
135 		errx(1, "invalid encoding");
136 	return (i);
137 }
138 
139 int
140 main(argc, argv)
141 	int argc;
142 	char **argv;
143 {
144 	char *wsdev;
145 	struct wsdisplay_font f;
146 	int c, res, wsfd, ffd, verbose;
147 	size_t len;
148 	void *buf;
149 
150 	wsdev = DEFDEV;
151 	f.fontwidth = DEFWIDTH;
152 	f.fontheight = DEFHEIGHT;
153 	f.firstchar = 0;
154 	f.numchars = 256;
155 	f.stride = 0;
156 	f.encoding = DEFENC;
157 	f.name = 0;
158 	f.bitorder = DEFBITORDER;
159 	f.byteorder = DEFBYTEORDER;
160 
161 	while ((c = getopt(argc, argv, "f:w:h:e:N:bB:v")) != -1) {
162 		switch (c) {
163 		case 'f':
164 			wsdev = optarg;
165 			break;
166 		case 'w':
167 			if (sscanf(optarg, "%d", &f.fontwidth) != 1)
168 				errx(1, "invalid font width");
169 			break;
170 		case 'h':
171 			if (sscanf(optarg, "%d", &f.fontheight) != 1)
172 				errx(1, "invalid font height");
173 			break;
174 		case 'e':
175 			f.encoding = getencoding(optarg);
176 			break;
177 		case 'N':
178 			f.name = optarg;
179 			break;
180 		case 'b':
181 			f.bitorder = WSDISPLAY_FONTORDER_R2L;
182 			break;
183 		case 'B':
184 			f.byteorder = WSDISPLAY_FONTORDER_R2L;
185 			break;
186 		case 'v':
187 			verbose = 1;
188 			break;
189 		case '?':
190 		default:
191 			usage();
192 			break;
193 		}
194 	}
195 	argc -= optind;
196 	argv += optind;
197 
198 	if (argc > 1)
199 		usage();
200 
201 	wsfd = open(wsdev, O_RDWR, 0);
202 	if (wsfd < 0)
203 		err(2, "open ws-device %s", wsdev);
204 
205 	if (argc > 0) {
206 		ffd = open(argv[0], O_RDONLY, 0);
207 		if (ffd < 0)
208 			err(4, "open font %s", argv[0]);
209 		if (!f.name)
210 			f.name = argv[0];
211 	} else
212 		ffd = 0;
213 
214 	if (!f.stride)
215 		f.stride = (f.fontwidth + 7) / 8;
216 	len = f.fontheight * f.numchars * f.stride;
217 	if (!len)
218 		errx(1, "invalid font size");
219 
220 	buf = malloc(len);
221 	if (!buf)
222 		errx(1, "malloc");
223 	res = read(ffd, buf, len);
224 	if (res < 0)
225 		err(4, "read font");
226 	if (res != len)
227 		errx(4, "short read");
228 
229 	f.data = buf;
230 
231 	if (verbose) {
232 		printf("name:       %s\n", f.name);
233 		printf("firstchar:  %d\n", f.firstchar);
234 		printf("numchars:   %d\n", f.numchars);
235 		printf("encoding:   %s (%d)\n",
236 			rgetencoding(f.encoding), f.encoding);
237 		printf("fontwidth:  %d\n", f.fontwidth);
238 		printf("fontheight: %d\n", f.fontheight);
239 		printf("stride:     %d\n", f.stride);
240 		printf("bitorder:   %s (%d)\n",
241 			rgetfontorder(f.bitorder), f.bitorder);
242 		printf("byteorder:  %s (%d)\n",
243 			rgetfontorder(f.byteorder), f.byteorder);
244 	}
245 
246 	res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
247 	if (res < 0)
248 		err(3, "WSDISPLAYIO_LDFONT");
249 
250 	return (0);
251 }
252