1#include <sys/consio.h>
2#include <sys/endian.h>
3#include <sys/ioctl.h>
4#include <sys/param.h>
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10
11struct file_header {
12	uint8_t		magic[8];
13	uint8_t		width;
14	uint8_t		height;
15	uint16_t	nglyphs;
16	uint16_t	nmappings_normal;
17	uint16_t	nmappings_bold;
18} __packed;
19
20static vfnt_map_t *
21load_mappingtable(unsigned int nmappings)
22{
23	vfnt_map_t *t;
24	unsigned int i;
25
26	if (nmappings == 0)
27		return (NULL);
28
29	t = malloc(sizeof *t * nmappings);
30
31	if (fread(t, sizeof *t * nmappings, 1, stdin) != 1) {
32		perror("mappings");
33		exit(1);
34	}
35
36	for (i = 0; i < nmappings; i++) {
37		t[i].src = be32toh(t[i].src);
38		t[i].dst = be16toh(t[i].dst);
39		t[i].len = be16toh(t[i].len);
40	}
41
42	return (t);
43}
44
45int
46main(int argc __unused, char *argv[] __unused)
47{
48	struct file_header fh;
49	static vfnt_t vfnt;
50	size_t glyphsize;
51
52	if (fread(&fh, sizeof fh, 1, stdin) != 1) {
53		perror("file_header");
54		return (1);
55	}
56
57	if (memcmp(fh.magic, "VFNT 1.0", 8) != 0) {
58		fprintf(stderr, "Bad magic\n");
59		return (1);
60	}
61
62	vfnt.nnormal = be16toh(fh.nmappings_normal);
63	vfnt.nbold = be16toh(fh.nmappings_bold);
64	vfnt.nglyphs = be16toh(fh.nglyphs);
65	vfnt.width = fh.width;
66	vfnt.height = fh.height;
67
68	glyphsize = howmany(vfnt.width, 8) * vfnt.height * vfnt.nglyphs;
69	vfnt.glyphs = malloc(glyphsize);
70
71	if (fread(vfnt.glyphs, glyphsize, 1, stdin) != 1) {
72		perror("glyphs");
73		return (1);
74	}
75
76	vfnt.normal = load_mappingtable(vfnt.nnormal);
77	vfnt.bold = load_mappingtable(vfnt.nbold);
78
79	if (ioctl(STDOUT_FILENO, PIO_VFONT, &vfnt) == -1) {
80		perror("PIO_VFONT");
81		return (1);
82	}
83
84	return (0);
85}
86