1 /* $OpenBSD: wsfontload.c,v 1.11 2006/08/07 10:43:20 kettenis 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/types.h> 37 #include <sys/time.h> 38 #include <sys/ioctl.h> 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <fcntl.h> 43 #include <unistd.h> 44 #include <string.h> 45 #include <err.h> 46 47 #include <dev/wscons/wsconsio.h> 48 49 #define DEFDEV "/dev/ttyCcfg" 50 #define DEFWIDTH 8 51 #define DEFHEIGHT 16 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 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 }; 82 83 int 84 main(int argc, char *argv[]) 85 { 86 char *wsdev, *p; 87 struct wsdisplay_font f; 88 int c, res, wsfd, ffd, list, i; 89 size_t len; 90 void *buf; 91 92 wsdev = DEFDEV; 93 f.index = -1; 94 f.fontwidth = DEFWIDTH; 95 f.fontheight = DEFHEIGHT; 96 f.firstchar = 0; 97 f.numchars = 256; 98 f.stride = 0; 99 f.encoding = DEFENC; 100 f.name[0] = 0; 101 f.bitorder = DEFBITORDER; 102 f.byteorder = DEFBYTEORDER; 103 104 list = 0; 105 while ((c = getopt(argc, argv, "bB:e:f:h:lN:w:")) != -1) { 106 switch (c) { 107 case 'f': 108 wsdev = optarg; 109 break; 110 case 'w': 111 if (sscanf(optarg, "%d", &f.fontwidth) != 1) 112 errx(1, "invalid font width of %d", 113 f.fontwidth); 114 break; 115 case 'h': 116 if (sscanf(optarg, "%d", &f.fontheight) != 1) 117 errx(1, "invalid font height of %d", 118 f.fontheight); 119 break; 120 case 'e': 121 f.encoding = getencoding(optarg); 122 break; 123 case 'l': 124 list++; 125 break; 126 case 'N': 127 strlcpy(f.name, optarg, WSFONT_NAME_SIZE); 128 break; 129 case 'b': 130 f.bitorder = WSDISPLAY_FONTORDER_R2L; 131 break; 132 case 'B': 133 f.byteorder = WSDISPLAY_FONTORDER_R2L; 134 break; 135 case '?': 136 default: 137 usage(); 138 break; 139 } 140 } 141 argc -= optind; 142 argv += optind; 143 144 if (list && argc) 145 usage(); 146 147 if (argc > 1) 148 usage(); 149 150 wsfd = open(wsdev, O_RDWR, 0); 151 if (wsfd < 0) 152 err(2, "open %s", wsdev); 153 154 if (list) { 155 i = 0; 156 p = " # Name Encoding W H"; 157 do { 158 f.index = i; 159 res = ioctl(wsfd, WSDISPLAYIO_LSFONT, &f); 160 if (res == 0) { 161 if (f.name[0]) { 162 if (p) { 163 puts(p); 164 p = NULL; 165 } 166 printf("%2d %-16s %8s %2d %2d\n", 167 f.index, f.name, 168 encodings[f.encoding].name, 169 f.fontwidth, f.fontheight); 170 } 171 } 172 i++; 173 } while(res == 0); 174 175 return (0); 176 } 177 178 if (argc > 0) { 179 ffd = open(argv[0], O_RDONLY, 0); 180 if (ffd < 0) 181 err(4, "open %s", argv[0]); 182 if (!*f.name) 183 strlcpy(f.name, argv[0], WSFONT_NAME_SIZE); 184 } else 185 ffd = 0; 186 187 if (!f.stride) 188 f.stride = (f.fontwidth + 7) / 8; 189 len = f.fontheight * f.numchars * f.stride; 190 if (!len) 191 errx(1, "invalid font size"); 192 193 buf = malloc(len); 194 if (!buf) 195 errx(1, "malloc"); 196 res = read(ffd, buf, len); 197 if (res < 0) 198 err(4, "read %s", argv[0]); 199 if (res != len) 200 errx(4, "short read on %s", argv[0]); 201 202 f.data = buf; 203 204 res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f); 205 if (res < 0) 206 err(3, "WSDISPLAYIO_LDFONT"); 207 208 return (0); 209 } 210 211 static int 212 getencoding(char *name) 213 { 214 int i; 215 216 for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++) 217 if (!strcmp(name, encodings[i].name)) 218 return (encodings[i].val); 219 220 if (sscanf(name, "%d", &i) != 1) 221 errx(1, "invalid encoding"); 222 return (i); 223 } 224