xref: /minix3/minix/commands/loadfont/loadfont.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc /* loadfont.c - Load custom font into EGA, VGA video card
2*433d6423SLionel Sambuc  *
3*433d6423SLionel Sambuc  * Author: Hrvoje Stipetic (hs@hck.hr) Jun-1995.
4*433d6423SLionel Sambuc  *
5*433d6423SLionel Sambuc  */
6*433d6423SLionel Sambuc 
7*433d6423SLionel Sambuc #include <sys/types.h>
8*433d6423SLionel Sambuc #include <fcntl.h>
9*433d6423SLionel Sambuc #include <unistd.h>
10*433d6423SLionel Sambuc #include <stdlib.h>
11*433d6423SLionel Sambuc #include <string.h>
12*433d6423SLionel Sambuc #include <errno.h>
13*433d6423SLionel Sambuc #include <termios.h>
14*433d6423SLionel Sambuc #include <sys/ioctl.h>
15*433d6423SLionel Sambuc 
tell(char * s)16*433d6423SLionel Sambuc void tell(char *s)
17*433d6423SLionel Sambuc {
18*433d6423SLionel Sambuc   write(2, s, strlen(s));
19*433d6423SLionel Sambuc }
20*433d6423SLionel Sambuc 
itoa(unsigned i)21*433d6423SLionel Sambuc char *itoa(unsigned i)
22*433d6423SLionel Sambuc {
23*433d6423SLionel Sambuc   static char a[3*sizeof(int)];
24*433d6423SLionel Sambuc   char *p = a+sizeof(a)-1;
25*433d6423SLionel Sambuc 
26*433d6423SLionel Sambuc   do {
27*433d6423SLionel Sambuc 	*--p = '0' + i%10;
28*433d6423SLionel Sambuc   } while ((i /= 10) > 0);
29*433d6423SLionel Sambuc 
30*433d6423SLionel Sambuc   return p;
31*433d6423SLionel Sambuc }
32*433d6423SLionel Sambuc 
report(char * say)33*433d6423SLionel Sambuc void report(char *say)
34*433d6423SLionel Sambuc {
35*433d6423SLionel Sambuc   int err = errno;
36*433d6423SLionel Sambuc   tell("loadfont: ");
37*433d6423SLionel Sambuc   if (say != NULL) {
38*433d6423SLionel Sambuc 	tell(say);
39*433d6423SLionel Sambuc 	tell(": ");
40*433d6423SLionel Sambuc   }
41*433d6423SLionel Sambuc   tell(strerror(err));
42*433d6423SLionel Sambuc   tell("\n");
43*433d6423SLionel Sambuc }
44*433d6423SLionel Sambuc 
usage(void)45*433d6423SLionel Sambuc void usage(void)
46*433d6423SLionel Sambuc {
47*433d6423SLionel Sambuc   tell("Usage: loadfont fontfile\n");
48*433d6423SLionel Sambuc   exit(1);
49*433d6423SLionel Sambuc }
50*433d6423SLionel Sambuc 
51*433d6423SLionel Sambuc 
main(int argc,char * argv[])52*433d6423SLionel Sambuc int main(int argc, char *argv[])
53*433d6423SLionel Sambuc {
54*433d6423SLionel Sambuc   static u8_t font[256][32];
55*433d6423SLionel Sambuc   static u8_t font_file[256 * (16+14+8) + 1];
56*433d6423SLionel Sambuc   u8_t *ff;
57*433d6423SLionel Sambuc   int fd, size, tsize, ch, ln;
58*433d6423SLionel Sambuc   struct winsize ws;
59*433d6423SLionel Sambuc 
60*433d6423SLionel Sambuc 
61*433d6423SLionel Sambuc   if (argc != 2)
62*433d6423SLionel Sambuc 	usage();
63*433d6423SLionel Sambuc 
64*433d6423SLionel Sambuc   if ((fd = open(argv[1], O_RDONLY)) < 0) {
65*433d6423SLionel Sambuc 	report(argv[1]);
66*433d6423SLionel Sambuc 	exit(1);
67*433d6423SLionel Sambuc   }
68*433d6423SLionel Sambuc 
69*433d6423SLionel Sambuc   switch (read(fd, font_file, sizeof(font_file))) {
70*433d6423SLionel Sambuc   case 256 * 8:
71*433d6423SLionel Sambuc   	size = 8;
72*433d6423SLionel Sambuc   	break;
73*433d6423SLionel Sambuc   case 256 * 14:
74*433d6423SLionel Sambuc   	size = 14;
75*433d6423SLionel Sambuc   	break;
76*433d6423SLionel Sambuc   case 256 * 16:
77*433d6423SLionel Sambuc   	size = 16;
78*433d6423SLionel Sambuc   	break;
79*433d6423SLionel Sambuc   case 256 * (16+14+8):
80*433d6423SLionel Sambuc   	size = 0;
81*433d6423SLionel Sambuc   	break;
82*433d6423SLionel Sambuc   case -1:
83*433d6423SLionel Sambuc 	report(argv[1]);
84*433d6423SLionel Sambuc 	exit(1);
85*433d6423SLionel Sambuc   default:
86*433d6423SLionel Sambuc 	tell("loadfont: ");
87*433d6423SLionel Sambuc 	tell(argv[1]);
88*433d6423SLionel Sambuc 	tell(": fontfile is not an 8x8, 8x14, 8x16, or compound font\n");
89*433d6423SLionel Sambuc 	exit(1);
90*433d6423SLionel Sambuc   }
91*433d6423SLionel Sambuc   close(fd);
92*433d6423SLionel Sambuc 
93*433d6423SLionel Sambuc   if (ioctl(0, TIOCGWINSZ, &ws) < 0 || (errno= ENOTTY, ws.ws_row == 0)) {
94*433d6423SLionel Sambuc 	report(NULL);
95*433d6423SLionel Sambuc 	exit(1);
96*433d6423SLionel Sambuc   }
97*433d6423SLionel Sambuc   tsize = ws.ws_ypixel / ws.ws_row;
98*433d6423SLionel Sambuc 
99*433d6423SLionel Sambuc   if (size == 0) {
100*433d6423SLionel Sambuc 	if (tsize >= 16) {
101*433d6423SLionel Sambuc 		ff = font_file + 256 * (0);
102*433d6423SLionel Sambuc 	} else
103*433d6423SLionel Sambuc 	if (tsize >= 14) {
104*433d6423SLionel Sambuc 		ff = font_file + 256 * (16);
105*433d6423SLionel Sambuc 	} else {
106*433d6423SLionel Sambuc 		ff = font_file + 256 * (16 + 14);
107*433d6423SLionel Sambuc 	}
108*433d6423SLionel Sambuc 	size = tsize;
109*433d6423SLionel Sambuc   } else {
110*433d6423SLionel Sambuc 	ff = font_file;
111*433d6423SLionel Sambuc   }
112*433d6423SLionel Sambuc 
113*433d6423SLionel Sambuc   for (ch = 0; ch < 256; ch++) {
114*433d6423SLionel Sambuc 	for (ln = 0; ln < size; ln++) font[ch][ln] = ff[ch * size + ln];
115*433d6423SLionel Sambuc   }
116*433d6423SLionel Sambuc 
117*433d6423SLionel Sambuc   if (ioctl(0, TIOCSFON, font) < 0) {
118*433d6423SLionel Sambuc 	report(NULL);
119*433d6423SLionel Sambuc 	exit(1);
120*433d6423SLionel Sambuc   }
121*433d6423SLionel Sambuc   exit(0);
122*433d6423SLionel Sambuc }
123