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