1262861Sjhb#include <sys/cdefs.h>
2262861Sjhb__FBSDID("$FreeBSD: releng/10.3/tools/tools/vt/setfont/setfont.c 262861 2014-03-06 18:30:56Z jhb $");
3262861Sjhb
4219888Sed#include <sys/consio.h>
5219888Sed#include <sys/endian.h>
6219888Sed#include <sys/ioctl.h>
7219888Sed#include <sys/param.h>
8219888Sed
9219888Sed#include <stdio.h>
10219888Sed#include <stdlib.h>
11219888Sed#include <string.h>
12219888Sed#include <unistd.h>
13219888Sed
14219888Sedstruct file_header {
15219888Sed	uint8_t		magic[8];
16219888Sed	uint8_t		width;
17219888Sed	uint8_t		height;
18262861Sjhb	uint16_t	pad;
19262861Sjhb	uint32_t	glyph_count;
20262861Sjhb	uint32_t	map_count[4];
21219888Sed} __packed;
22219888Sed
23219888Sedstatic vfnt_map_t *
24219888Sedload_mappingtable(unsigned int nmappings)
25219888Sed{
26219888Sed	vfnt_map_t *t;
27219888Sed	unsigned int i;
28219888Sed
29219888Sed	if (nmappings == 0)
30219888Sed		return (NULL);
31219888Sed
32219888Sed	t = malloc(sizeof *t * nmappings);
33219888Sed
34219888Sed	if (fread(t, sizeof *t * nmappings, 1, stdin) != 1) {
35219888Sed		perror("mappings");
36219888Sed		exit(1);
37219888Sed	}
38219888Sed
39219888Sed	for (i = 0; i < nmappings; i++) {
40219888Sed		t[i].src = be32toh(t[i].src);
41219888Sed		t[i].dst = be16toh(t[i].dst);
42219888Sed		t[i].len = be16toh(t[i].len);
43219888Sed	}
44219888Sed
45219888Sed	return (t);
46219888Sed}
47219888Sed
48219888Sedint
49219888Sedmain(int argc __unused, char *argv[] __unused)
50219888Sed{
51219888Sed	struct file_header fh;
52219888Sed	static vfnt_t vfnt;
53219888Sed	size_t glyphsize;
54262861Sjhb	unsigned int i;
55219888Sed
56219888Sed	if (fread(&fh, sizeof fh, 1, stdin) != 1) {
57219888Sed		perror("file_header");
58219888Sed		return (1);
59219888Sed	}
60219888Sed
61262861Sjhb	if (memcmp(fh.magic, "VFNT0002", 8) != 0) {
62219888Sed		fprintf(stderr, "Bad magic\n");
63219888Sed		return (1);
64219888Sed	}
65219888Sed
66262861Sjhb	for (i = 0; i < VFNT_MAPS; i++)
67262861Sjhb		vfnt.map_count[i] = be32toh(fh.map_count[i]);
68262861Sjhb	vfnt.glyph_count = be32toh(fh.glyph_count);
69219888Sed	vfnt.width = fh.width;
70219888Sed	vfnt.height = fh.height;
71219888Sed
72262861Sjhb	glyphsize = howmany(vfnt.width, 8) * vfnt.height * vfnt.glyph_count;
73219888Sed	vfnt.glyphs = malloc(glyphsize);
74219888Sed
75219888Sed	if (fread(vfnt.glyphs, glyphsize, 1, stdin) != 1) {
76219888Sed		perror("glyphs");
77219888Sed		return (1);
78219888Sed	}
79219888Sed
80262861Sjhb	for (i = 0; i < VFNT_MAPS; i++)
81262861Sjhb		vfnt.map[i] = load_mappingtable(vfnt.map_count[i]);
82219888Sed
83219888Sed	if (ioctl(STDOUT_FILENO, PIO_VFONT, &vfnt) == -1) {
84219888Sed		perror("PIO_VFONT");
85219888Sed		return (1);
86219888Sed	}
87219888Sed
88219888Sed	return (0);
89219888Sed}
90