xref: /openbsd-src/usr.sbin/wsfontload/wsfontload.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: wsfontload.c,v 1.13 2013/10/20 16:09:25 miod Exp $ */
2 /* $NetBSD: wsfontload.c,v 1.2 2000/01/05 18:46:43 ad Exp $ */
3 
4 /*
5  * Copyright (c) 1999
6  *	Matthias Drochner.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed for the NetBSD Project
19  *	by Matthias Drochner.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/ioctl.h>
39 #include <sys/stat.h>
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <err.h>
47 
48 #include <dev/wscons/wsconsio.h>
49 
50 #define DEFDEV		"/dev/ttyCcfg"
51 #define DEFENC		WSDISPLAY_FONTENC_ISO
52 #define DEFBITORDER	WSDISPLAY_FONTORDER_L2R
53 #define DEFBYTEORDER	WSDISPLAY_FONTORDER_L2R
54 
55 int main(int, char**);
56 static void usage(void);
57 static int getencoding(char *);
58 
59 static void
60 usage(void)
61 {
62 	extern char *__progname;
63 
64 	(void)fprintf(stderr,
65 	    "usage: %s [-Bbl] [-e encoding] [-f file] [-h height] [-N name]\n"
66 	    "       %*s [-w width] [fontfile]\n",
67 	    __progname, (int)strlen(__progname), "");
68 	exit(1);
69 }
70 
71 static const struct {
72 	const char *name;
73 	int val;
74 } encodings[] = {
75 	{"iso",  WSDISPLAY_FONTENC_ISO},
76 	{"ibm",  WSDISPLAY_FONTENC_IBM},
77 #if 0
78 	{"pcvt", WSDISPLAY_FONTENC_PCVT},
79 	{"iso7", WSDISPLAY_FONTENC_ISO7},
80 #endif
81 };
82 
83 int
84 main(int argc, char *argv[])
85 {
86 	char *wsdev, *infile, *p;
87 	struct wsdisplay_font f;
88 	int c, res, wsfd, ffd, type, list, i;
89 	int defwidth, defheight;
90 	struct stat stat;
91 	size_t len;
92 	void *buf;
93 
94 	wsdev = DEFDEV;
95 	memset(&f, 0, sizeof f);
96 	f.firstchar = f.numchars = -1;
97 	f.encoding = -1;
98 
99 	list = 0;
100 	while ((c = getopt(argc, argv, "bB:e:f:h:lN:w:")) != -1) {
101 		switch (c) {
102 		case 'f':
103 			wsdev = optarg;
104 			break;
105 		case 'w':
106 			if (sscanf(optarg, "%d", &f.fontwidth) != 1)
107 				errx(1, "invalid font width of %d",
108 				    f.fontwidth);
109 			break;
110 		case 'h':
111 			if (sscanf(optarg, "%d", &f.fontheight) != 1)
112 				errx(1, "invalid font height of %d",
113 				    f.fontheight);
114 			break;
115 		case 'e':
116 			f.encoding = getencoding(optarg);
117 			break;
118 		case 'l':
119 			list++;
120 			break;
121 		case 'N':
122 			strlcpy(f.name, optarg, WSFONT_NAME_SIZE);
123 			break;
124 		case 'b':
125 			f.bitorder = WSDISPLAY_FONTORDER_R2L;
126 			break;
127 		case 'B':
128 			f.byteorder = WSDISPLAY_FONTORDER_R2L;
129 			break;
130 		case '?':
131 		default:
132 			usage();
133 			break;
134 		}
135 	}
136 	argc -= optind;
137 	argv += optind;
138 
139 	if (list && argc)
140 		usage();
141 
142 	if (argc > 1)
143 		usage();
144 
145 	wsfd = open(wsdev, O_RDWR, 0);
146 	if (wsfd < 0)
147 		err(2, "open %s", wsdev);
148 
149 	if (list) {
150 		i = 0;
151 		p = " # Name             Encoding  W  H";
152 		do {
153 			f.index = i;
154 			res = ioctl(wsfd, WSDISPLAYIO_LSFONT, &f);
155 			if (res == 0) {
156 				if (f.name[0]) {
157 					if (p) {
158 						puts(p);
159 						p = NULL;
160 					}
161 					printf("%2d %-16s %8s %2d %2d\n",
162 					    f.index, f.name,
163 					    encodings[f.encoding].name,
164 					    f.fontwidth, f.fontheight);
165 				}
166 			}
167 			i++;
168 		} while(res == 0);
169 
170 		return (0);
171 	}
172 
173 	if (argc > 0) {
174 		infile = argv[0];
175 		ffd = open(infile, O_RDONLY, 0);
176 		if (ffd < 0)
177 			err(4, "open %s", infile);
178 		if (!*f.name)
179 			strlcpy(f.name, infile, WSFONT_NAME_SIZE);
180 	} else {
181 		infile = "stdin";
182 		ffd = STDIN_FILENO;
183 	}
184 
185 	res = ioctl(wsfd, WSDISPLAYIO_GTYPE, &type);
186 	if (res != 0)
187 		type = WSDISPLAY_TYPE_UNKNOWN;
188 
189 	switch (type) {
190 	/* text-mode VGA */
191 	case WSDISPLAY_TYPE_ISAVGA:
192 	case WSDISPLAY_TYPE_PCIVGA:
193 		defwidth = 8;
194 		defheight = 16;
195 		break;
196 	/* raster frame buffers */
197 	default:
198 		/* XXX ought to be computed from the frame buffer resolution */
199 		defwidth = 12;
200 		defheight = 22;
201 		break;
202 	}
203 
204 	f.index = -1;
205 	if (f.fontwidth == 0)
206 		f.fontwidth = defwidth;
207 	if (f.fontheight == 0)
208 		f.fontheight = defheight;
209 	if (f.stride == 0)
210 		f.stride = (f.fontwidth + 7) / 8;
211 	if (f.encoding < 0)
212 		f.encoding = DEFENC;
213 	if (f.bitorder == 0)
214 		f.bitorder = DEFBITORDER;
215 	if (f.byteorder == 0)
216 		f.byteorder = DEFBYTEORDER;
217 
218 	if (f.firstchar < 0)
219 		f.firstchar = 0;
220 
221 	if (f.numchars < 0) {
222 		f.numchars = 256;
223 		if (argc > 0) {
224 			if (fstat(ffd, &stat) == 0)
225 				f.numchars = stat.st_size /
226 				    f.stride / f.fontheight;
227 		}
228 	}
229 
230 	len = f.fontheight * f.numchars * f.stride;
231 	if (!len)
232 		errx(1, "invalid font size");
233 
234 	buf = malloc(len);
235 	if (!buf)
236 		errx(1, "malloc");
237 	res = read(ffd, buf, len);
238 	if (res < 0)
239 		err(4, "read %s", infile);
240 	if (res != len)
241 		errx(4, "short read on %s", infile);
242 
243 	f.data = buf;
244 
245 	res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
246 	if (res < 0)
247 		err(3, "WSDISPLAYIO_LDFONT");
248 
249 	return (0);
250 }
251 
252 static int
253 getencoding(char *name)
254 {
255 	int i;
256 
257 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
258 		if (!strcmp(name, encodings[i].name))
259 			return (encodings[i].val);
260 
261 	if (sscanf(name, "%d", &i) != 1)
262 		errx(1, "invalid encoding");
263 	return (i);
264 }
265