GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/wsfontload/wsfontload.c Lines: 0 114 0.0 %
Date: 2017-11-13 Branches: 0 77 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: wsfontload.c,v 1.20 2017/08/23 09:15:33 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
	int c, res, wsfd, ffd, type, list, i;
80
	int defwidth, defheight;
81
	struct stat stat;
82
	size_t len;
83
	void *buf;
84
	const char *errstr;
85
86
	wsdev = DEFDEV;
87
	memset(&f, 0, sizeof f);
88
	f.firstchar = f.numchars = -1;
89
	f.encoding = -1;
90
91
	list = 0;
92
	while ((c = getopt(argc, argv, "bB:e:f:h:lN:w:")) != -1) {
93
		switch (c) {
94
		case 'f':
95
			wsdev = optarg;
96
			break;
97
		case 'w':
98
			f.fontwidth = strtonum(optarg, 1, INT_MAX, &errstr);
99
			if (errstr)
100
				errx(1, "font width is %s: %s", errstr, optarg);
101
			break;
102
		case 'h':
103
			f.fontheight = strtonum(optarg, 1, INT_MAX, &errstr);
104
			if (errstr)
105
				errx(1, "font height is %s: %s",
106
				    errstr, optarg);
107
			break;
108
		case 'e':
109
			f.encoding = getencoding(optarg);
110
			break;
111
		case 'l':
112
			list = 1;
113
			break;
114
		case 'N':
115
			strlcpy(f.name, optarg, WSFONT_NAME_SIZE);
116
			break;
117
		case 'b':
118
			f.bitorder = WSDISPLAY_FONTORDER_R2L;
119
			break;
120
		case 'B':
121
			f.byteorder = WSDISPLAY_FONTORDER_R2L;
122
			break;
123
		case '?':
124
		default:
125
			usage();
126
			break;
127
		}
128
	}
129
	argc -= optind;
130
	argv += optind;
131
132
	if (list && argc)
133
		usage();
134
135
	if (argc > 1)
136
		usage();
137
138
	wsfd = open(wsdev, O_RDWR, 0);
139
	if (wsfd < 0)
140
		err(2, "open %s", wsdev);
141
142
	if (list) {
143
		i = 0;
144
		p = " # Name                             Encoding  W  H";
145
		do {
146
			f.index = i;
147
			res = ioctl(wsfd, WSDISPLAYIO_LSFONT, &f);
148
			if (res == 0) {
149
				if (f.name[0]) {
150
					if (p) {
151
						puts(p);
152
						p = NULL;
153
					}
154
					printf("%2d %-32s %8s %2d %2d\n",
155
					    f.index, f.name,
156
					    encodings[f.encoding].name,
157
					    f.fontwidth, f.fontheight);
158
				}
159
			}
160
			i++;
161
		} while(res == 0);
162
163
		close(wsfd);
164
		return (0);
165
	}
166
167
	if (argc > 0) {
168
		infile = argv[0];
169
		ffd = open(infile, O_RDONLY, 0);
170
		if (ffd < 0)
171
			err(4, "open %s", infile);
172
		if (!*f.name)
173
			strlcpy(f.name, infile, WSFONT_NAME_SIZE);
174
	} else {
175
		infile = "stdin";
176
		ffd = STDIN_FILENO;
177
	}
178
179
	res = ioctl(wsfd, WSDISPLAYIO_GTYPE, &type);
180
	if (res != 0)
181
		type = WSDISPLAY_TYPE_UNKNOWN;
182
183
	switch (type) {
184
	/* text-mode VGA */
185
	case WSDISPLAY_TYPE_ISAVGA:
186
	case WSDISPLAY_TYPE_PCIVGA:
187
		defwidth = 8;
188
		defheight = 16;
189
		break;
190
	/* raster frame buffers */
191
	default:
192
		/* XXX ought to be computed from the frame buffer resolution */
193
		defwidth = 12;
194
		defheight = 22;
195
		break;
196
	}
197
198
	f.index = -1;
199
	if (f.fontwidth == 0)
200
		f.fontwidth = defwidth;
201
	if (f.fontheight == 0)
202
		f.fontheight = defheight;
203
	if (f.stride == 0)
204
		f.stride = (f.fontwidth + 7) / 8;
205
	if (f.encoding < 0)
206
		f.encoding = DEFENC;
207
	if (f.bitorder == 0)
208
		f.bitorder = DEFBITORDER;
209
	if (f.byteorder == 0)
210
		f.byteorder = DEFBYTEORDER;
211
212
	if (f.firstchar < 0)
213
		f.firstchar = 0;
214
215
	if (f.numchars < 0) {
216
		f.numchars = 256;
217
		if (argc > 0) {
218
			if (fstat(ffd, &stat) == 0)
219
				f.numchars = stat.st_size /
220
				    f.stride / f.fontheight;
221
		}
222
	}
223
224
	len = f.fontheight * f.numchars * f.stride;
225
	if (!len)
226
		errx(1, "invalid font size");
227
228
	buf = malloc(len);
229
	if (!buf)
230
		errx(1, "malloc");
231
	res = read(ffd, buf, len);
232
	if (res < 0)
233
		err(4, "read %s", infile);
234
	if (res != len)
235
		errx(4, "short read on %s", infile);
236
237
	f.data = buf;
238
239
	res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
240
	if (res < 0)
241
		err(3, "WSDISPLAYIO_LDFONT");
242
243
	return (0);
244
}
245
246
static int
247
getencoding(char *name)
248
{
249
	int i;
250
251
	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
252
		if (!strcmp(name, encodings[i].name))
253
			return (encodings[i].val);
254
255
	if (sscanf(name, "%d", &i) != 1)
256
		errx(1, "invalid encoding");
257
	return (i);
258
}