1 /* $OpenBSD: wsfontload.c,v 1.26 2022/12/04 23:50:51 cheloha 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 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30 #include <sys/ioctl.h> 31 #include <sys/stat.h> 32 #include <sys/time.h> 33 #include <sys/types.h> 34 35 #include <err.h> 36 #include <fcntl.h> 37 #include <limits.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 43 #include <dev/wscons/wsconsio.h> 44 45 #define DEFDEV "/dev/ttyCcfg" 46 #define DEFENC WSDISPLAY_FONTENC_ISO 47 #define DEFBITORDER WSDISPLAY_FONTORDER_L2R 48 #define DEFBYTEORDER WSDISPLAY_FONTORDER_L2R 49 50 int main(int, char**); 51 static void usage(void); 52 static int getencoding(char *); 53 54 static void 55 usage(void) 56 { 57 extern char *__progname; 58 59 (void)fprintf(stderr, 60 "usage: %s [-Bbl] [-e encoding] [-f file] [-h height] [-N name]\n" 61 " %*s [-w width] [fontfile]\n", 62 __progname, (int)strlen(__progname), ""); 63 exit(1); 64 } 65 66 static const struct { 67 const char *name; 68 int val; 69 } encodings[] = { 70 {"iso", WSDISPLAY_FONTENC_ISO}, 71 {"ibm", WSDISPLAY_FONTENC_IBM}, 72 }; 73 74 int 75 main(int argc, char *argv[]) 76 { 77 char *wsdev, *infile, *p; 78 struct wsdisplay_font f; 79 struct wsdisplay_screentype screens; 80 int c, res, wsfd, ffd, list, i; 81 int defwidth, defheight; 82 struct stat stat; 83 size_t len; 84 void *buf; 85 const char *errstr; 86 87 wsdev = DEFDEV; 88 memset(&f, 0, sizeof f); 89 f.firstchar = f.numchars = -1; 90 f.encoding = -1; 91 92 list = 0; 93 while ((c = getopt(argc, argv, "bB:e:f:h:lN:w:")) != -1) { 94 switch (c) { 95 case 'f': 96 wsdev = optarg; 97 break; 98 case 'w': 99 f.fontwidth = strtonum(optarg, 1, INT_MAX, &errstr); 100 if (errstr) 101 errx(1, "font width is %s: %s", errstr, optarg); 102 break; 103 case 'h': 104 f.fontheight = strtonum(optarg, 1, INT_MAX, &errstr); 105 if (errstr) 106 errx(1, "font height is %s: %s", 107 errstr, optarg); 108 break; 109 case 'e': 110 f.encoding = getencoding(optarg); 111 break; 112 case 'l': 113 list = 1; 114 break; 115 case 'N': 116 strlcpy(f.name, optarg, WSFONT_NAME_SIZE); 117 break; 118 case 'b': 119 f.bitorder = WSDISPLAY_FONTORDER_R2L; 120 break; 121 case 'B': 122 f.byteorder = WSDISPLAY_FONTORDER_R2L; 123 break; 124 default: 125 usage(); 126 break; 127 } 128 } 129 argc -= optind; 130 argv += optind; 131 132 if (list && argc) 133 usage(); 134 135 if (argc > 1) 136 usage(); 137 138 wsfd = open(wsdev, O_RDWR); 139 if (wsfd == -1) 140 err(2, "open %s", wsdev); 141 142 if (list) { 143 i = 0; 144 p = " # Name Encoding" \ 145 " W H Chars"; 146 do { 147 f.index = i; 148 res = ioctl(wsfd, WSDISPLAYIO_LSFONT, &f); 149 if (res == 0) { 150 if (f.name[0]) { 151 if (p) { 152 puts(p); 153 p = NULL; 154 } 155 printf("%2d %-32s %8s %2d %2d %8d\n", 156 f.index, f.name, 157 encodings[f.encoding].name, 158 f.fontwidth, f.fontheight, 159 f.numchars); 160 } 161 } 162 i++; 163 } while(res == 0); 164 165 close(wsfd); 166 return (0); 167 } 168 169 if (argc > 0) { 170 infile = argv[0]; 171 ffd = open(infile, O_RDONLY); 172 if (ffd == -1) 173 err(4, "open %s", infile); 174 if (!*f.name) 175 strlcpy(f.name, infile, WSFONT_NAME_SIZE); 176 } else { 177 infile = "stdin"; 178 ffd = STDIN_FILENO; 179 } 180 181 memset(&screens, 0, sizeof(screens)); 182 res = ioctl(wsfd, WSDISPLAYIO_GETSCREENTYPE, &screens); 183 if (res == 0) { 184 /* raster frame buffers */ 185 defwidth = screens.fontwidth; 186 defheight = screens.fontheight; 187 } else { 188 /* text-mode VGA */ 189 defwidth = 8; 190 defheight = 16; 191 } 192 193 f.index = -1; 194 if (f.fontwidth == 0) 195 f.fontwidth = defwidth; 196 if (f.fontheight == 0) 197 f.fontheight = defheight; 198 if (f.stride == 0) 199 f.stride = (f.fontwidth + 7) / 8; 200 if (f.encoding < 0) 201 f.encoding = DEFENC; 202 if (f.bitorder == 0) 203 f.bitorder = DEFBITORDER; 204 if (f.byteorder == 0) 205 f.byteorder = DEFBYTEORDER; 206 207 if (f.firstchar < 0) 208 f.firstchar = 0; 209 210 if (f.numchars < 0) { 211 f.numchars = 256; 212 if (argc > 0) { 213 if (fstat(ffd, &stat) == 0) 214 f.numchars = stat.st_size / 215 f.stride / f.fontheight; 216 } 217 } 218 219 len = f.fontheight * f.numchars * f.stride; 220 if (!len) 221 errx(1, "invalid font size"); 222 223 buf = malloc(len); 224 if (!buf) 225 errx(1, "malloc"); 226 res = read(ffd, buf, len); 227 if (res == -1) 228 err(4, "read %s", infile); 229 if (res != len) 230 errx(4, "short read on %s", infile); 231 232 f.data = buf; 233 234 res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f); 235 if (res == -1) 236 err(3, "WSDISPLAYIO_LDFONT"); 237 238 return (0); 239 } 240 241 static int 242 getencoding(char *name) 243 { 244 int i; 245 246 for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++) 247 if (!strcmp(name, encodings[i].name)) 248 return (encodings[i].val); 249 250 if (sscanf(name, "%d", &i) != 1) 251 errx(1, "invalid encoding"); 252 return (i); 253 } 254