Home | History | Annotate | Line # | Download | only in wsfontload
wsfontload.c revision 1.2
      1 /* $NetBSD: wsfontload.c,v 1.2 2000/01/05 18:46:43 ad 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 <unistd.h>
     38 #include <sys/types.h>
     39 #include <sys/ioctl.h>
     40 #include <err.h>
     41 #include <malloc.h>
     42 
     43 #include <dev/wscons/wsconsio.h>
     44 
     45 #define DEFDEV "/dev/ttyEcfg"
     46 #define DEFWIDTH 8
     47 #define DEFHEIGHT 16
     48 #define DEFENC WSDISPLAY_FONTENC_ISO
     49 
     50 int main __P((int, char**));
     51 static void usage __P((void));
     52 static int getencoding __P((char *));
     53 
     54 static void
     55 usage()
     56 {
     57 	extern char *__progname;
     58 
     59 	(void)fprintf(stderr,
     60 		"Usage: %s [-f wsdev] [-w width] [-h height] [-e encoding]"
     61 		" [-N name] [fontfile]\n",
     62 		      __progname);
     63 	exit(1);
     64 }
     65 
     66 int
     67 main(argc, argv)
     68 	int argc;
     69 	char **argv;
     70 {
     71 	char *wsdev;
     72 	struct wsdisplay_font f;
     73 	int c, res, wsfd, ffd;
     74 	size_t len;
     75 	void *buf;
     76 
     77 	wsdev = DEFDEV;
     78 	f.fontwidth = DEFWIDTH;
     79 	f.fontheight = DEFHEIGHT;
     80 	f.firstchar = 0;
     81 	f.numchars = 256;
     82 	f.stride = 0;
     83 	f.encoding = DEFENC;
     84 	f.name = 0;
     85 	f.bitorder = WSDISPLAY_FONTORDER_L2R;
     86 	f.byteorder = WSDISPLAY_FONTORDER_L2R;
     87 
     88 	while ((c = getopt(argc, argv, "f:w:h:e:N:bB")) != -1) {
     89 		switch (c) {
     90 		case 'f':
     91 			wsdev = optarg;
     92 			break;
     93 		case 'w':
     94 			if (sscanf(optarg, "%d", &f.fontwidth) != 1)
     95 				errx(1, "invalid font width");
     96 			break;
     97 		case 'h':
     98 			if (sscanf(optarg, "%d", &f.fontheight) != 1)
     99 				errx(1, "invalid font height");
    100 			break;
    101 		case 'e':
    102 			f.encoding = getencoding(optarg);
    103 			break;
    104 		case 'N':
    105 			f.name = optarg;
    106 			break;
    107 		case 'b':
    108 			f.bitorder = WSDISPLAY_FONTORDER_R2L;
    109 			break;
    110 		case 'B':
    111 			f.byteorder = WSDISPLAY_FONTORDER_R2L;
    112 			break;
    113 		case '?':
    114 		default:
    115 			usage();
    116 			break;
    117 		}
    118 	}
    119 	argc -= optind;
    120 	argv += optind;
    121 
    122 	if (argc > 1)
    123 		usage();
    124 
    125 	wsfd = open(wsdev, O_RDWR, 0);
    126 	if (wsfd < 0)
    127 		err(2, "open ws");
    128 
    129 	if (argc > 0) {
    130 		ffd = open(argv[0], O_RDONLY, 0);
    131 		if (ffd < 0)
    132 			err(4, "open font");
    133 		if (!f.name)
    134 			f.name = argv[0];
    135 	} else
    136 		ffd = 0;
    137 
    138 	if (!f.stride)
    139 		f.stride = (f.fontwidth + 7) / 8;
    140 	len = f.fontheight * f.numchars * f.stride;
    141 	if (!len)
    142 		errx(1, "invalid font size");
    143 
    144 	buf = malloc(len);
    145 	if (!buf)
    146 		errx(1, "malloc");
    147 	res = read(ffd, buf, len);
    148 	if (res < 0)
    149 		err(4, "read font");
    150 	if (res != len)
    151 		errx(4, "short read");
    152 
    153 	f.data = buf;
    154 
    155 	res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
    156 	if (res < 0)
    157 		err(3, "WSDISPLAYIO_LDFONT");
    158 
    159 	return (0);
    160 }
    161 
    162 static struct {
    163 	char *name;
    164 	int val;
    165 } encodings[] = {
    166 	{"iso", WSDISPLAY_FONTENC_ISO},
    167 	{"ibm", WSDISPLAY_FONTENC_IBM},
    168 	{"pcvt", WSDISPLAY_FONTENC_PCVT},
    169 };
    170 
    171 static int
    172 getencoding(name)
    173 	char *name;
    174 {
    175 	int i;
    176 
    177 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
    178 		if (!strcmp(name, encodings[i].name))
    179 			return (encodings[i].val);
    180 
    181 	if (sscanf(name, "%d", &i) != 1)
    182 		errx(1, "invalid encoding");
    183 	return (i);
    184 }
    185