Home | History | Annotate | Line # | Download | only in wsfontload
wsfontload.c revision 1.4
      1 /* $NetBSD: wsfontload.c,v 1.4 2000/07/04 20:27:40 matt Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999
      5  *	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Matthias Drochner.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <stdio.h>
     36 #include <fcntl.h>
     37 #include <string.h>
     38 #include <unistd.h>
     39 #include <sys/types.h>
     40 #include <sys/ioctl.h>
     41 #include <err.h>
     42 #include <malloc.h>
     43 
     44 #include <dev/wscons/wsconsio.h>
     45 
     46 #define DEFDEV		"/dev/ttyEcfg"
     47 #define DEFWIDTH	8
     48 #define DEFHEIGHT	16
     49 #define DEFENC		WSDISPLAY_FONTENC_ISO
     50 #define DEFBITORDER	WSDISPLAY_FONTORDER_L2R
     51 #define DEFBYTEORDER	WSDISPLAY_FONTORDER_L2R
     52 
     53 int main __P((int, char**));
     54 static void usage __P((void));
     55 static int getencoding __P((char *));
     56 
     57 static void
     58 usage()
     59 {
     60 	extern char *__progname;
     61 
     62 	(void)fprintf(stderr,
     63 		"Usage: %s [-f wsdev] [-w width] [-h height] [-e encoding]"
     64 		" [-N name] [-b] [-B] [fontfile]\n",
     65 		      __progname);
     66 	exit(1);
     67 }
     68 
     69 int
     70 main(argc, argv)
     71 	int argc;
     72 	char **argv;
     73 {
     74 	char *wsdev;
     75 	struct wsdisplay_font f;
     76 	int c, res, wsfd, ffd;
     77 	size_t len;
     78 	void *buf;
     79 
     80 	wsdev = DEFDEV;
     81 	f.fontwidth = DEFWIDTH;
     82 	f.fontheight = DEFHEIGHT;
     83 	f.firstchar = 0;
     84 	f.numchars = 256;
     85 	f.stride = 0;
     86 	f.encoding = DEFENC;
     87 	f.name = 0;
     88 	f.bitorder = DEFBITORDER;
     89 	f.byteorder = DEFBYTEORDER;
     90 
     91 	while ((c = getopt(argc, argv, "f:w:h:e:N:bB")) != -1) {
     92 		switch (c) {
     93 		case 'f':
     94 			wsdev = optarg;
     95 			break;
     96 		case 'w':
     97 			if (sscanf(optarg, "%d", &f.fontwidth) != 1)
     98 				errx(1, "invalid font width");
     99 			break;
    100 		case 'h':
    101 			if (sscanf(optarg, "%d", &f.fontheight) != 1)
    102 				errx(1, "invalid font height");
    103 			break;
    104 		case 'e':
    105 			f.encoding = getencoding(optarg);
    106 			break;
    107 		case 'N':
    108 			f.name = optarg;
    109 			break;
    110 		case 'b':
    111 			f.bitorder = WSDISPLAY_FONTORDER_R2L;
    112 			break;
    113 		case 'B':
    114 			f.byteorder = WSDISPLAY_FONTORDER_R2L;
    115 			break;
    116 		case '?':
    117 		default:
    118 			usage();
    119 			break;
    120 		}
    121 	}
    122 	argc -= optind;
    123 	argv += optind;
    124 
    125 	if (argc > 1)
    126 		usage();
    127 
    128 	wsfd = open(wsdev, O_RDWR, 0);
    129 	if (wsfd < 0)
    130 		err(2, "open ws");
    131 
    132 	if (argc > 0) {
    133 		ffd = open(argv[0], O_RDONLY, 0);
    134 		if (ffd < 0)
    135 			err(4, "open font");
    136 		if (!f.name)
    137 			f.name = argv[0];
    138 	} else
    139 		ffd = 0;
    140 
    141 	if (!f.stride)
    142 		f.stride = (f.fontwidth + 7) / 8;
    143 	len = f.fontheight * f.numchars * f.stride;
    144 	if (!len)
    145 		errx(1, "invalid font size");
    146 
    147 	buf = malloc(len);
    148 	if (!buf)
    149 		errx(1, "malloc");
    150 	res = read(ffd, buf, len);
    151 	if (res < 0)
    152 		err(4, "read font");
    153 	if (res != len)
    154 		errx(4, "short read");
    155 
    156 	f.data = buf;
    157 
    158 	res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
    159 	if (res < 0)
    160 		err(3, "WSDISPLAYIO_LDFONT");
    161 
    162 	return (0);
    163 }
    164 
    165 static struct {
    166 	char *name;
    167 	int val;
    168 } encodings[] = {
    169 	{"iso", WSDISPLAY_FONTENC_ISO},
    170 	{"ibm", WSDISPLAY_FONTENC_IBM},
    171 	{"pcvt", WSDISPLAY_FONTENC_PCVT},
    172 };
    173 
    174 static int
    175 getencoding(name)
    176 	char *name;
    177 {
    178 	int i;
    179 
    180 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
    181 		if (!strcmp(name, encodings[i].name))
    182 			return (encodings[i].val);
    183 
    184 	if (sscanf(name, "%d", &i) != 1)
    185 		errx(1, "invalid encoding");
    186 	return (i);
    187 }
    188