Unhook gperf, it was only used by gcc2.
[dragonfly.git] / usr.sbin / pcvt / loadfont / loadfont.c
1 /*
2  * Copyright (c) 1992, 1995 Hellmuth Michaelis
3  *
4  * Copyright (c) 1992, 1994 Brian Dunford-Shore
5  *
6  * 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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by
19  *      Hellmuth Michaelis and Brian Dunford-Shore
20  * 4. The name authors may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $FreeBSD: src/usr.sbin/pcvt/loadfont/loadfont.c,v 1.6.6.1 2001/05/12 10:11:42 kris Exp $
35  * $DragonFly: src/usr.sbin/pcvt/loadfont/Attic/loadfont.c,v 1.3 2004/03/24 17:46:23 cpressey Exp $
36  */
37
38 static char *id =
39         "@(#)loadfont.c, 3.20, Last Edit-Date: [Fri Apr  7 10:13:16 1995]";
40
41 /*---------------------------------------------------------------------------*
42  *
43  *      load a font into vga character font memory
44  *
45  *      -hm     removing explicit HGC support (same as MDA ..)
46  *      -hm     new pcvt_ioctl.h SIZ_xxROWS
47  *      -hm     add -d option
48  *
49  *---------------------------------------------------------------------------*/
50
51 #include <stdio.h>
52 #include <err.h>
53 #include <fcntl.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <machine/pcvt_ioctl.h>
57
58 #define FONT8X8         2048    /* filesize for 8x8 font              */
59 #define HEIGHT8X8       8       /* 8 scan lines char cell height      */
60 #define SSCAN8X8        143     /* 400 scan lines on screen - 256 - 1 */
61
62 #define FONT8X10        2560    /* filesize for 8x10 font             */
63 #define HEIGHT8X10      10      /* 10 scan lines char cell height     */
64 #define SSCAN8X10       143     /* 400 scan lines on screen - 256 - 1 */
65
66 #define FONT8X14        3584    /* filesize for 8x14 font             */
67 #define HEIGHT8X14      14      /* 14 scan lines char cell height     */
68 #define SSCAN8X14       135     /* 392 scan lines on screen - 256 - 1 */
69
70 #define FONT8X16        4096    /* filesize for 8x16 font             */
71 #define HEIGHT8X16      16      /* 16 scan lines char cell height     */
72 #define SSCAN8X16       143     /* 400 scan lines on screen - 256 - 1 */
73
74 struct screeninfo screeninfo;
75
76 #define DEFAULTFD 0
77 int fd;
78
79 static void setfont(int, int, int, int, int);
80 static void loadfont(int, int, const unsigned char *);
81 static void printvgafontattr(int);
82 static void printheader(void);
83 static void usage(void);
84
85 int
86 main(int argc, char **argv)
87 {
88         extern int optind;
89         extern int opterr;
90         extern char *optarg;
91
92         FILE *in;
93         struct stat sbuf, *sbp;
94         unsigned char *fonttab;
95         int ret;
96         int chr_height;
97         int scr_scan;
98         int scr_rows;
99         int c;
100         int chr_set = -1;
101         char *filename;
102         int fflag = -1;
103         int info = -1;
104         int dflag = 0;
105         char *device;
106
107         while( (c = getopt(argc, argv, "c:d:f:i")) != -1)
108         {
109                 switch(c)
110                 {
111                         case 'c':
112                                 chr_set = atoi(optarg);
113                                 break;
114
115                         case 'd':
116                                 device = optarg;
117                                 dflag = 1;
118                                 break;
119
120                         case 'f':
121                                 filename = optarg;
122                                 fflag = 1;
123                                 break;
124
125                         case 'i':
126                                 info = 1;
127                                 break;
128
129                         case '?':
130                         default:
131                                 usage();
132                                 break;
133                 }
134         }
135
136         if(chr_set == -1 || fflag == -1)
137                 info = 1;
138
139         if(dflag)
140         {
141                 if((fd = open(device, O_RDWR)) == -1)
142                         err(1, "ERROR opening %s", device);
143         }
144         else
145         {
146                 fd = DEFAULTFD;
147         }
148
149         if(info == 1)
150         {
151                 int i;
152
153                 if(ioctl(fd, VGAGETSCREEN, &screeninfo) == -1)
154                         err(1, "ioctl VGAGETSCREEN failed");
155
156                 switch(screeninfo.adaptor_type)
157                 {
158                   case UNKNOWN_ADAPTOR:
159                   case MDA_ADAPTOR:
160                   case CGA_ADAPTOR:
161                     printf("Adaptor does not support Downloadable Fonts!\n");
162                     break;
163                   case EGA_ADAPTOR:
164                     printheader();
165                     for(i = 0;i < 4;i++)
166                     {
167                         printvgafontattr(i);
168                     }
169                     break;
170                   case VGA_ADAPTOR:
171                     printheader();
172                     for(i = 0;i < 8;i++)
173                     {
174                         printvgafontattr(i);
175                     }
176                 }
177                 printf("\n");
178                 exit(0);
179         }
180
181         if(chr_set < 0 || chr_set > 7)
182                 usage();
183
184         sbp = &sbuf;
185
186         if((in = fopen(filename, "r")) == NULL)
187                 err(1, "cannot open file %s for reading", filename);
188
189         if((fstat(fileno(in), sbp)) != 0)
190                 err(1, "cannot fstat file %s", filename);
191
192         switch(sbp->st_size)
193         {
194                 case FONT8X8:
195                         chr_height = HEIGHT8X8;
196                         scr_scan = SSCAN8X8;
197                         scr_rows = SIZ_50ROWS;
198                         break;
199
200                 case FONT8X10:
201                         chr_height = HEIGHT8X10;
202                         scr_scan = SSCAN8X10;
203                         scr_rows = SIZ_40ROWS;
204                         break;
205
206                 case FONT8X14:
207                         chr_height = HEIGHT8X14;
208                         scr_scan = SSCAN8X14;
209                         scr_rows = SIZ_28ROWS;
210                         break;
211
212                 case FONT8X16:
213                         chr_height = HEIGHT8X16;
214                         scr_scan = SSCAN8X16;
215                         scr_rows = SIZ_25ROWS;
216                         break;
217
218                 default:
219                         errx(1, "error, file %s is no valid font file, size=%d", argv[1], sbp->st_size);
220         }
221
222         if((fonttab = (unsigned char *)malloc((size_t)sbp->st_size)) == NULL)
223                 errx(1, "error, malloc failed");
224
225         if((ret = fread(fonttab, sizeof(*fonttab), sbp->st_size, in)) != sbp->st_size)
226                 errx(1, "error reading file %s, size = %d, read = is no valid font file, size=%d",
227                          argv[1], sbp->st_size, ret);
228
229         loadfont(chr_set, chr_height, fonttab);
230         setfont(chr_set, 1, chr_height - 1, scr_scan, scr_rows);
231
232         exit(0);
233 }
234
235 void
236 setfont(int charset, int fontloaded, int charscan, int scrscan, int scrrow)
237 {
238         struct vgafontattr vfattr;
239
240         vfattr.character_set = charset;
241         vfattr.font_loaded = fontloaded;
242         vfattr.character_scanlines = charscan;
243         vfattr.screen_scanlines = scrscan;
244         vfattr.screen_size = scrrow;
245
246         if(ioctl(fd, VGASETFONTATTR, &vfattr) == -1)
247                 err(1, "ioctl VGASETFONTATTR failed, error");
248 }
249
250
251 void
252 loadfont(int fontset, int charscanlines, const unsigned char *font_table)
253 {
254         int i, j;
255         struct vgaloadchar vlc;
256
257         vlc.character_set = fontset;
258         vlc.character_scanlines = charscanlines;
259
260         for(i = 0; i < 256; i++)
261         {
262                 vlc.character = i;
263                 for (j = 0; j < charscanlines; j++)
264                 {
265                         vlc.char_table[j] = font_table[j];
266                 }
267                 font_table += charscanlines;
268                 if(ioctl(fd, VGALOADCHAR, &vlc) == -1)
269                         err(1, "ioctl VGALOADCHAR failed, error");
270         }
271 }
272
273 void
274 printvgafontattr(int charset)
275 {
276         struct vgafontattr vfattr;
277         static int sizetab[] = { 25, 28, 35, 40, 43, 50 };
278
279         vfattr.character_set = charset;
280
281         if(ioctl(fd, VGAGETFONTATTR, &vfattr) == -1)
282                 err(1, "ioctl VGAGETFONTATTR failed, error");
283         printf(" %d  ",charset);
284         if(vfattr.font_loaded)
285         {
286
287                 printf("Loaded ");
288                 printf(" %2.2d       ", sizetab[vfattr.screen_size]);
289                 printf(" %2.2d           ",
290                        (((int)vfattr.character_scanlines) & 0x1f) + 1);
291                 printf(" %3.3d",
292                        ((int)vfattr.screen_scanlines+0x101));
293         }
294         else
295         {
296                 printf("Empty");
297         }
298         printf("\n");
299 }
300
301 void
302 printheader(void)
303 {
304         printf("\nEGA/VGA Charactersets Status Info:\n\n");
305         printf("Set Status Lines CharScanLines ScreenScanLines\n");
306         printf("--- ------ ----- ------------- ---------------\n");
307 }
308
309 void
310 usage(void)
311 {
312         fprintf(stderr,"\nloadfont - load font into ega/vga font ram for pcvt video driver\n");
313         fprintf(stderr,"usage: loadfont -c <cset> -d <dev> -f <name> -i\n");
314         fprintf(stderr,"       -c <cset> characterset to load (ega 0..3, vga 0..7)\n");
315         fprintf(stderr,"       -d <dev>  specify device\n");
316         fprintf(stderr,"       -f <name> filename containing binary font data\n");
317         fprintf(stderr,"       -i        print status and types of loaded fonts (default)\n");
318         exit(1);
319 }