1 /* $OpenBSD: wsfontload.c,v 1.16 2015/09/06 20:07:46 tobias 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 <sys/ioctl.h> 37 #include <sys/stat.h> 38 #include <sys/time.h> 39 #include <sys/types.h> 40 41 #include <err.h> 42 #include <fcntl.h> 43 #include <limits.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #include <dev/wscons/wsconsio.h> 50 51 #define DEFDEV "/dev/ttyCcfg" 52 #define DEFENC WSDISPLAY_FONTENC_ISO 53 #define DEFBITORDER WSDISPLAY_FONTORDER_L2R 54 #define DEFBYTEORDER WSDISPLAY_FONTORDER_L2R 55 56 int main(int, char**); 57 static void usage(void); 58 static int getencoding(char *); 59 60 static void 61 usage(void) 62 { 63 extern char *__progname; 64 65 (void)fprintf(stderr, 66 "usage: %s [-Bbl] [-e encoding] [-f file] [-h height] [-N name]\n" 67 " %*s [-w width] [fontfile]\n", 68 __progname, (int)strlen(__progname), ""); 69 exit(1); 70 } 71 72 static const struct { 73 const char *name; 74 int val; 75 } encodings[] = { 76 {"iso", WSDISPLAY_FONTENC_ISO}, 77 {"ibm", WSDISPLAY_FONTENC_IBM}, 78 #if 0 79 {"pcvt", WSDISPLAY_FONTENC_PCVT}, 80 {"iso7", WSDISPLAY_FONTENC_ISO7}, 81 #endif 82 }; 83 84 int 85 main(int argc, char *argv[]) 86 { 87 char *wsdev, *infile, *p; 88 struct wsdisplay_font f; 89 int c, res, wsfd, ffd, type, list, i; 90 int defwidth, defheight; 91 struct stat stat; 92 size_t len; 93 void *buf; 94 const char *errstr; 95 96 wsdev = DEFDEV; 97 memset(&f, 0, sizeof f); 98 f.firstchar = f.numchars = -1; 99 f.encoding = -1; 100 101 list = 0; 102 while ((c = getopt(argc, argv, "bB:e:f:h:lN:w:")) != -1) { 103 switch (c) { 104 case 'f': 105 wsdev = optarg; 106 break; 107 case 'w': 108 f.fontwidth = strtonum(optarg, 1, INT_MAX, &errstr); 109 if (errstr) 110 errx(1, "font width is %s: %s", errstr, optarg); 111 break; 112 case 'h': 113 f.fontheight = strtonum(optarg, 1, INT_MAX, &errstr); 114 if (errstr) 115 errx(1, "font height is %s: %s", 116 errstr, optarg); 117 break; 118 case 'e': 119 f.encoding = getencoding(optarg); 120 break; 121 case 'l': 122 list = 1; 123 break; 124 case 'N': 125 strlcpy(f.name, optarg, WSFONT_NAME_SIZE); 126 break; 127 case 'b': 128 f.bitorder = WSDISPLAY_FONTORDER_R2L; 129 break; 130 case 'B': 131 f.byteorder = WSDISPLAY_FONTORDER_R2L; 132 break; 133 case '?': 134 default: 135 usage(); 136 break; 137 } 138 } 139 argc -= optind; 140 argv += optind; 141 142 if (list && argc) 143 usage(); 144 145 if (argc > 1) 146 usage(); 147 148 wsfd = open(wsdev, O_RDWR, 0); 149 if (wsfd < 0) 150 err(2, "open %s", wsdev); 151 152 if (list) { 153 i = 0; 154 p = " # Name Encoding W H"; 155 do { 156 f.index = i; 157 res = ioctl(wsfd, WSDISPLAYIO_LSFONT, &f); 158 if (res == 0) { 159 if (f.name[0]) { 160 if (p) { 161 puts(p); 162 p = NULL; 163 } 164 printf("%2d %-16s %8s %2d %2d\n", 165 f.index, f.name, 166 encodings[f.encoding].name, 167 f.fontwidth, f.fontheight); 168 } 169 } 170 i++; 171 } while(res == 0); 172 173 return (0); 174 } 175 176 if (argc > 0) { 177 infile = argv[0]; 178 ffd = open(infile, O_RDONLY, 0); 179 if (ffd < 0) 180 err(4, "open %s", infile); 181 if (!*f.name) 182 strlcpy(f.name, infile, WSFONT_NAME_SIZE); 183 } else { 184 infile = "stdin"; 185 ffd = STDIN_FILENO; 186 } 187 188 res = ioctl(wsfd, WSDISPLAYIO_GTYPE, &type); 189 if (res != 0) 190 type = WSDISPLAY_TYPE_UNKNOWN; 191 192 switch (type) { 193 /* text-mode VGA */ 194 case WSDISPLAY_TYPE_ISAVGA: 195 case WSDISPLAY_TYPE_PCIVGA: 196 defwidth = 8; 197 defheight = 16; 198 break; 199 /* raster frame buffers */ 200 default: 201 /* XXX ought to be computed from the frame buffer resolution */ 202 defwidth = 12; 203 defheight = 22; 204 break; 205 } 206 207 f.index = -1; 208 if (f.fontwidth == 0) 209 f.fontwidth = defwidth; 210 if (f.fontheight == 0) 211 f.fontheight = defheight; 212 if (f.stride == 0) 213 f.stride = (f.fontwidth + 7) / 8; 214 if (f.encoding < 0) 215 f.encoding = DEFENC; 216 if (f.bitorder == 0) 217 f.bitorder = DEFBITORDER; 218 if (f.byteorder == 0) 219 f.byteorder = DEFBYTEORDER; 220 221 if (f.firstchar < 0) 222 f.firstchar = 0; 223 224 if (f.numchars < 0) { 225 f.numchars = 256; 226 if (argc > 0) { 227 if (fstat(ffd, &stat) == 0) 228 f.numchars = stat.st_size / 229 f.stride / f.fontheight; 230 } 231 } 232 233 len = f.fontheight * f.numchars * f.stride; 234 if (!len) 235 errx(1, "invalid font size"); 236 237 buf = malloc(len); 238 if (!buf) 239 errx(1, "malloc"); 240 res = read(ffd, buf, len); 241 if (res < 0) 242 err(4, "read %s", infile); 243 if (res != len) 244 errx(4, "short read on %s", infile); 245 246 f.data = buf; 247 248 res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f); 249 if (res < 0) 250 err(3, "WSDISPLAYIO_LDFONT"); 251 252 return (0); 253 } 254 255 static int 256 getencoding(char *name) 257 { 258 int i; 259 260 for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++) 261 if (!strcmp(name, encodings[i].name)) 262 return (encodings[i].val); 263 264 if (sscanf(name, "%d", &i) != 1) 265 errx(1, "invalid encoding"); 266 return (i); 267 } 268