xref: /openbsd/usr.sbin/wsfontload/wsfontload.c (revision 097a140d)
1 /* $OpenBSD: wsfontload.c,v 1.23 2020/08/05 13:56:06 fcambus 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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 
35 #include <err.h>
36 #include <fcntl.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include <dev/wscons/wsconsio.h>
44 
45 #define DEFDEV		"/dev/ttyCcfg"
46 #define DEFENC		WSDISPLAY_FONTENC_ISO
47 #define DEFBITORDER	WSDISPLAY_FONTORDER_L2R
48 #define DEFBYTEORDER	WSDISPLAY_FONTORDER_L2R
49 
50 int main(int, char**);
51 static void usage(void);
52 static int getencoding(char *);
53 
54 static void
55 usage(void)
56 {
57 	extern char *__progname;
58 
59 	(void)fprintf(stderr,
60 	    "usage: %s [-Bbl] [-e encoding] [-f file] [-h height] [-N name]\n"
61 	    "       %*s [-w width] [fontfile]\n",
62 	    __progname, (int)strlen(__progname), "");
63 	exit(1);
64 }
65 
66 static const struct {
67 	const char *name;
68 	int val;
69 } encodings[] = {
70 	{"iso",  WSDISPLAY_FONTENC_ISO},
71 	{"ibm",  WSDISPLAY_FONTENC_IBM},
72 };
73 
74 int
75 main(int argc, char *argv[])
76 {
77 	char *wsdev, *infile, *p;
78 	struct wsdisplay_font f;
79 	struct wsdisplay_screentype screens;
80 	int c, res, wsfd, ffd, type, list, i;
81 	int defwidth, defheight;
82 	struct stat stat;
83 	size_t len;
84 	void *buf;
85 	const char *errstr;
86 
87 	wsdev = DEFDEV;
88 	memset(&f, 0, sizeof f);
89 	f.firstchar = f.numchars = -1;
90 	f.encoding = -1;
91 
92 	list = 0;
93 	while ((c = getopt(argc, argv, "bB:e:f:h:lN:w:")) != -1) {
94 		switch (c) {
95 		case 'f':
96 			wsdev = optarg;
97 			break;
98 		case 'w':
99 			f.fontwidth = strtonum(optarg, 1, INT_MAX, &errstr);
100 			if (errstr)
101 				errx(1, "font width is %s: %s", errstr, optarg);
102 			break;
103 		case 'h':
104 			f.fontheight = strtonum(optarg, 1, INT_MAX, &errstr);
105 			if (errstr)
106 				errx(1, "font height is %s: %s",
107 				    errstr, optarg);
108 			break;
109 		case 'e':
110 			f.encoding = getencoding(optarg);
111 			break;
112 		case 'l':
113 			list = 1;
114 			break;
115 		case 'N':
116 			strlcpy(f.name, optarg, WSFONT_NAME_SIZE);
117 			break;
118 		case 'b':
119 			f.bitorder = WSDISPLAY_FONTORDER_R2L;
120 			break;
121 		case 'B':
122 			f.byteorder = WSDISPLAY_FONTORDER_R2L;
123 			break;
124 		case '?':
125 		default:
126 			usage();
127 			break;
128 		}
129 	}
130 	argc -= optind;
131 	argv += optind;
132 
133 	if (list && argc)
134 		usage();
135 
136 	if (argc > 1)
137 		usage();
138 
139 	wsfd = open(wsdev, O_RDWR, 0);
140 	if (wsfd == -1)
141 		err(2, "open %s", wsdev);
142 
143 	if (list) {
144 		i = 0;
145 		p = " # Name                             Encoding" \
146 		    "  W  H    Chars";
147 		do {
148 			f.index = i;
149 			res = ioctl(wsfd, WSDISPLAYIO_LSFONT, &f);
150 			if (res == 0) {
151 				if (f.name[0]) {
152 					if (p) {
153 						puts(p);
154 						p = NULL;
155 					}
156 					printf("%2d %-32s %8s %2d %2d %8d\n",
157 					    f.index, f.name,
158 					    encodings[f.encoding].name,
159 					    f.fontwidth, f.fontheight,
160 					    f.numchars);
161 				}
162 			}
163 			i++;
164 		} while(res == 0);
165 
166 		close(wsfd);
167 		return (0);
168 	}
169 
170 	if (argc > 0) {
171 		infile = argv[0];
172 		ffd = open(infile, O_RDONLY, 0);
173 		if (ffd == -1)
174 			err(4, "open %s", infile);
175 		if (!*f.name)
176 			strlcpy(f.name, infile, WSFONT_NAME_SIZE);
177 	} else {
178 		infile = "stdin";
179 		ffd = STDIN_FILENO;
180 	}
181 
182 	memset(&screens, 0, sizeof(screens));
183 	res = ioctl(wsfd, WSDISPLAYIO_GETSCREENTYPE, &screens);
184 	if (res == 0) {
185 		/* raster frame buffers */
186 		defwidth = screens.fontwidth;
187 		defheight = screens.fontheight;
188 	} else {
189 		/* text-mode VGA */
190 		defwidth = 8;
191 		defheight = 16;
192 	}
193 
194 	f.index = -1;
195 	if (f.fontwidth == 0)
196 		f.fontwidth = defwidth;
197 	if (f.fontheight == 0)
198 		f.fontheight = defheight;
199 	if (f.stride == 0)
200 		f.stride = (f.fontwidth + 7) / 8;
201 	if (f.encoding < 0)
202 		f.encoding = DEFENC;
203 	if (f.bitorder == 0)
204 		f.bitorder = DEFBITORDER;
205 	if (f.byteorder == 0)
206 		f.byteorder = DEFBYTEORDER;
207 
208 	if (f.firstchar < 0)
209 		f.firstchar = 0;
210 
211 	if (f.numchars < 0) {
212 		f.numchars = 256;
213 		if (argc > 0) {
214 			if (fstat(ffd, &stat) == 0)
215 				f.numchars = stat.st_size /
216 				    f.stride / f.fontheight;
217 		}
218 	}
219 
220 	len = f.fontheight * f.numchars * f.stride;
221 	if (!len)
222 		errx(1, "invalid font size");
223 
224 	buf = malloc(len);
225 	if (!buf)
226 		errx(1, "malloc");
227 	res = read(ffd, buf, len);
228 	if (res == -1)
229 		err(4, "read %s", infile);
230 	if (res != len)
231 		errx(4, "short read on %s", infile);
232 
233 	f.data = buf;
234 
235 	res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
236 	if (res == -1)
237 		err(3, "WSDISPLAYIO_LDFONT");
238 
239 	return (0);
240 }
241 
242 static int
243 getencoding(char *name)
244 {
245 	int i;
246 
247 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
248 		if (!strcmp(name, encodings[i].name))
249 			return (encodings[i].val);
250 
251 	if (sscanf(name, "%d", &i) != 1)
252 		errx(1, "invalid encoding");
253 	return (i);
254 }
255