1 /* $NetBSD: wsfontload.c,v 1.21 2017/06/23 18:40:03 macallan 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29 #include <stdio.h> 30 #include <fcntl.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <unistd.h> 34 #include <sys/types.h> 35 #include <sys/ioctl.h> 36 #include <sys/stat.h> 37 #include <err.h> 38 39 #include <dev/wscons/wsconsio.h> 40 41 #define DEFDEV "/dev/wsfont" 42 #define DEFWIDTH 8 43 #define DEFHEIGHT 16 44 #define DEFENC WSDISPLAY_FONTENC_ISO 45 #define DEFBITORDER WSDISPLAY_FONTORDER_L2R 46 #define DEFBYTEORDER WSDISPLAY_FONTORDER_L2R 47 48 __dead static void usage(void); 49 static int getencoding(char *); 50 static const char *rgetencoding(int); 51 static const char *rgetfontorder(int); 52 53 static struct { 54 const char *name; 55 int val; 56 } fontorders[] = { 57 { "known", WSDISPLAY_FONTORDER_KNOWN}, 58 { "l2r", WSDISPLAY_FONTORDER_L2R}, 59 { "r2l", WSDISPLAY_FONTORDER_R2L}, 60 }; 61 62 static struct { 63 const char *name; 64 int val; 65 } encodings[] = { 66 {"iso", WSDISPLAY_FONTENC_ISO}, 67 {"ibm", WSDISPLAY_FONTENC_IBM}, 68 {"pcvt", WSDISPLAY_FONTENC_PCVT}, 69 {"iso7", WSDISPLAY_FONTENC_ISO7}, 70 {"iso2", WSDISPLAY_FONTENC_ISO2}, 71 {"koi8r", WSDISPLAY_FONTENC_KOI8_R}, 72 }; 73 74 static void 75 usage(void) 76 { 77 78 (void)fprintf(stderr, 79 "usage: %s [-Bbv] [-e encoding] [-f wsdev] [-h height]" 80 " [-N name] [-w width] [fontfile]\n", 81 getprogname()); 82 exit(1); 83 } 84 85 /* 86 * map given fontorder to its string representation 87 */ 88 static const char * 89 rgetfontorder(int fontorder) 90 { 91 size_t i; 92 93 for (i = 0; i < sizeof(fontorders) / sizeof(fontorders[0]); i++) 94 if (fontorders[i].val == fontorder) 95 return (fontorders[i].name); 96 97 return "unknown"; 98 } 99 100 /* 101 * map given encoding to its string representation 102 */ 103 static const char * 104 rgetencoding(int enc) 105 { 106 size_t i; 107 108 for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++) 109 if (encodings[i].val == enc) 110 return (encodings[i].name); 111 112 return "unknown"; 113 } 114 115 /* 116 * map given encoding string to integer value 117 */ 118 static int 119 getencoding(char *name) 120 { 121 size_t i; 122 int j; 123 124 for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++) 125 if (!strcmp(name, encodings[i].name)) 126 return (encodings[i].val); 127 128 if (sscanf(name, "%d", &j) != 1) 129 errx(1, "invalid encoding"); 130 return (j); 131 } 132 133 int 134 main(int argc, char **argv) 135 { 136 const char *wsdev; 137 struct wsdisplay_font f; 138 struct stat st; 139 int c, res, wsfd, ffd, verbose = 0; 140 size_t len; 141 int use_embedded_name = 1; 142 void *buf; 143 char nbuf[65]; 144 145 wsdev = DEFDEV; 146 f.fontwidth = DEFWIDTH; 147 f.fontheight = DEFHEIGHT; 148 f.firstchar = 0; 149 f.numchars = 256; 150 f.stride = 0; 151 f.encoding = DEFENC; 152 f.name = 0; 153 f.bitorder = DEFBITORDER; 154 f.byteorder = DEFBYTEORDER; 155 156 while ((c = getopt(argc, argv, "f:w:h:e:N:bBv")) != -1) { 157 switch (c) { 158 case 'f': 159 wsdev = optarg; 160 break; 161 case 'w': 162 if (sscanf(optarg, "%d", &f.fontwidth) != 1) 163 errx(1, "invalid font width"); 164 break; 165 case 'h': 166 if (sscanf(optarg, "%d", &f.fontheight) != 1) 167 errx(1, "invalid font height"); 168 break; 169 case 'e': 170 f.encoding = getencoding(optarg); 171 break; 172 case 'N': 173 f.name = optarg; 174 use_embedded_name = 0; 175 break; 176 case 'b': 177 f.bitorder = WSDISPLAY_FONTORDER_R2L; 178 break; 179 case 'B': 180 f.byteorder = WSDISPLAY_FONTORDER_R2L; 181 break; 182 case 'v': 183 verbose = 1; 184 break; 185 case '?': 186 default: 187 usage(); 188 break; 189 } 190 } 191 argc -= optind; 192 argv += optind; 193 194 if (argc > 1) 195 usage(); 196 197 wsfd = open(wsdev, O_RDWR, 0); 198 if (wsfd < 0) 199 err(2, "open ws-device %s", wsdev); 200 201 if (argc > 0) { 202 ffd = open(argv[0], O_RDONLY, 0); 203 if (ffd < 0) 204 err(4, "open font %s", argv[0]); 205 if (!f.name) 206 f.name = argv[0]; 207 } else 208 ffd = 0; 209 210 if (!f.stride) 211 f.stride = (f.fontwidth + 7) / 8; 212 len = f.fontheight * f.numchars * f.stride; 213 if ((ffd != 0) && (fstat(ffd, &st) == 0)) { 214 if ((off_t)len != st.st_size) { 215 uint32_t foo = 0; 216 char b[65]; 217 len = st.st_size; 218 /* read header */ 219 read(ffd, b, 4); 220 if (strncmp(b, "WSFT", 4) != 0) 221 errx(1, "invalid wsf file "); 222 read(ffd, b, 64); 223 if (use_embedded_name) { 224 b[64] = 0; 225 strcpy(nbuf, b); 226 f.name = nbuf; 227 } 228 read(ffd, &foo, 4); 229 f.firstchar = le32toh(foo); 230 read(ffd, &foo, 4); 231 f.numchars = le32toh(foo); 232 read(ffd, &foo, 4); 233 f.encoding = le32toh(foo); 234 read(ffd, &foo, 4); 235 f.fontwidth = le32toh(foo); 236 read(ffd, &foo, 4); 237 f.fontheight = le32toh(foo); 238 read(ffd, &foo, 4); 239 f.stride = le32toh(foo); 240 read(ffd, &foo, 4); 241 f.bitorder = le32toh(foo); 242 read(ffd, &foo, 4); 243 f.byteorder = le32toh(foo); 244 len = f.numchars * f.fontheight * f.stride; 245 } 246 } 247 248 if (!len) 249 errx(1, "invalid font size"); 250 251 buf = malloc(len); 252 if (!buf) 253 errx(1, "malloc"); 254 res = read(ffd, buf, len); 255 if (res < 0) 256 err(4, "read font"); 257 if ((size_t)res != len) 258 errx(4, "short read"); 259 260 f.data = buf; 261 262 if (verbose) { 263 printf("name: %s\n", f.name); 264 printf("firstchar: %d\n", f.firstchar); 265 printf("numchars: %d\n", f.numchars); 266 printf("encoding: %s (%d)\n", 267 rgetencoding(f.encoding), f.encoding); 268 printf("fontwidth: %d\n", f.fontwidth); 269 printf("fontheight: %d\n", f.fontheight); 270 printf("stride: %d\n", f.stride); 271 printf("bitorder: %s (%d)\n", 272 rgetfontorder(f.bitorder), f.bitorder); 273 printf("byteorder: %s (%d)\n", 274 rgetfontorder(f.byteorder), f.byteorder); 275 } 276 277 res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f); 278 if (res < 0) 279 err(3, "WSDISPLAYIO_LDFONT"); 280 281 return (0); 282 } 283