1 /* $OpenBSD: wsfontload.c,v 1.4 2001/03/14 02:51:36 mickey Exp $ */ 2 /* $NetBSD: wsfontload.c,v 1.2 2000/01/05 18:46:43 ad Exp $ */ 3 4 /* 5 * Copyright (c) 1999 6 * Matthias Drochner. 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 for the NetBSD Project 19 * by Matthias Drochner. 20 * 4. The name of the author 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 AUTHOR ``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 AUTHOR 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 */ 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <fcntl.h> 39 #include <unistd.h> 40 #include <sys/types.h> 41 #include <sys/ioctl.h> 42 #include <string.h> 43 #include <err.h> 44 45 #include <dev/wscons/wsconsio.h> 46 47 #define DEFDEV "/dev/ttyCcfg" 48 #define DEFWIDTH 8 49 #define DEFHEIGHT 16 50 #define DEFENC WSDISPLAY_FONTENC_ISO 51 #define DEFBITORDER WSDISPLAY_FONTORDER_L2R 52 #define DEFBYTEORDER WSDISPLAY_FONTORDER_L2R 53 54 int main __P((int, char**)); 55 static void usage __P((void)); 56 static int getencoding __P((char *)); 57 58 static void 59 usage() 60 { 61 extern char *__progname; 62 63 (void)fprintf(stderr, 64 "Usage: %s [-f wsdev] -l\n" 65 " %s [-f wsdev] -d name\n" 66 " %s [-w width] [-h height] [-e encoding] " 67 "[-N name] [-b] [-B] [fontfile]\n", 68 __progname, __progname, __progname); 69 exit(1); 70 } 71 72 static const 73 struct { 74 const char *name; 75 int val; 76 } encodings[] = { 77 {"iso", WSDISPLAY_FONTENC_ISO}, 78 {"ibm", WSDISPLAY_FONTENC_IBM}, 79 {"pcvt", WSDISPLAY_FONTENC_PCVT}, 80 {"iso7", WSDISPLAY_FONTENC_ISO7}, 81 {"sony", WSDISPLAY_FONTENC_SONY}, 82 }; 83 84 int 85 main(argc, argv) 86 int argc; 87 char **argv; 88 { 89 char *wsdev, *p; 90 struct wsdisplay_font f; 91 int c, res, wsfd, ffd, list, i; 92 size_t len; 93 void *buf; 94 95 wsdev = DEFDEV; 96 f.index = -1; 97 f.fontwidth = DEFWIDTH; 98 f.fontheight = DEFHEIGHT; 99 f.firstchar = 0; 100 f.numchars = 256; 101 f.stride = 0; 102 f.encoding = DEFENC; 103 f.name[0] = 0; 104 f.bitorder = DEFBITORDER; 105 f.byteorder = DEFBYTEORDER; 106 107 list = 0; 108 while ((c = getopt(argc, argv, "bB:e:f:h:lN:w:")) != -1) { 109 switch (c) { 110 case 'f': 111 wsdev = optarg; 112 break; 113 case 'w': 114 if (sscanf(optarg, "%d", &f.fontwidth) != 1) 115 errx(1, "invalid font width of %d", 116 f.fontwidth); 117 break; 118 case 'h': 119 if (sscanf(optarg, "%d", &f.fontheight) != 1) 120 errx(1, "invalid font height of %d", 121 f.fontheight); 122 break; 123 case 'e': 124 f.encoding = getencoding(optarg); 125 break; 126 case 'l': 127 list++; 128 break; 129 case 'N': 130 strlcpy(f.name, optarg, WSFONT_NAME_SIZE); 131 break; 132 case 'b': 133 f.bitorder = WSDISPLAY_FONTORDER_R2L; 134 break; 135 case 'B': 136 f.byteorder = WSDISPLAY_FONTORDER_R2L; 137 break; 138 case '?': 139 default: 140 usage(); 141 break; 142 } 143 } 144 argc -= optind; 145 argv += optind; 146 147 if (list && argc) 148 usage(); 149 150 if (argc > 1) 151 usage(); 152 153 wsfd = open(wsdev, O_RDWR, 0); 154 if (wsfd < 0) 155 err(2, "open %s", wsdev); 156 157 if (list) { 158 i = 0; 159 p = " # Name Encoding W H"; 160 do { 161 f.index = i; 162 res = ioctl(wsfd, WSDISPLAYIO_LSFONT, &f); 163 if (res == 0) { 164 if (f.name[0]) { 165 if (p) { 166 puts(p); 167 p = NULL; 168 } 169 printf("%2d %-16s %8s %2d %2d\n", 170 f.index, f.name, 171 encodings[f.encoding].name, 172 f.fontwidth, f.fontheight); 173 } 174 } 175 i++; 176 } while(res == 0); 177 178 return (0); 179 } 180 181 if (argc > 0) { 182 ffd = open(argv[0], O_RDONLY, 0); 183 if (ffd < 0) 184 err(4, "open %s", argv[0]); 185 if (!*f.name) 186 strlcpy(f.name, argv[0], WSFONT_NAME_SIZE); 187 } else 188 ffd = 0; 189 190 if (!f.stride) 191 f.stride = (f.fontwidth + 7) / 8; 192 len = f.fontheight * f.numchars * f.stride; 193 if (!len) 194 errx(1, "invalid font size"); 195 196 buf = malloc(len); 197 if (!buf) 198 errx(1, "malloc"); 199 res = read(ffd, buf, len); 200 if (res < 0) 201 err(4, "read %s", argv[0]); 202 if (res != len) 203 errx(4, "short read on %s", argv[0]); 204 205 f.data = buf; 206 207 res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f); 208 if (res < 0) 209 err(3, "WSDISPLAYIO_LDFONT"); 210 211 return (0); 212 } 213 214 static int 215 getencoding(name) 216 char *name; 217 { 218 int i; 219 220 for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++) 221 if (!strcmp(name, encodings[i].name)) 222 return (encodings[i].val); 223 224 if (sscanf(name, "%d", &i) != 1) 225 errx(1, "invalid encoding"); 226 return (i); 227 } 228