bdfload.c revision 43d302b1
143d302b1Smacallan/* $NetBSD: bdfload.c,v 1.15 2022/09/27 10:57:23 macallan Exp $ */ 2d5f9d0dbSmacallan 3d5f9d0dbSmacallan/* 4d5f9d0dbSmacallan * Copyright (c) 2018 Michael Lorenz 5d5f9d0dbSmacallan * All rights reserved. 6d5f9d0dbSmacallan * 7d5f9d0dbSmacallan * Redistribution and use in source and binary forms, with or without 8d5f9d0dbSmacallan * modification, are permitted provided that the following conditions 9d5f9d0dbSmacallan * are met: 10d5f9d0dbSmacallan * 1. Redistributions of source code must retain the above copyright 11d5f9d0dbSmacallan * notice, this list of conditions and the following disclaimer. 12d5f9d0dbSmacallan * 2. Redistributions in binary form must reproduce the above copyright 13d5f9d0dbSmacallan * notice, this list of conditions and the following disclaimer in the 14d5f9d0dbSmacallan * documentation and/or other materials provided with the distribution. 15d5f9d0dbSmacallan * 16d5f9d0dbSmacallan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17d5f9d0dbSmacallan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18d5f9d0dbSmacallan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19d5f9d0dbSmacallan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20d5f9d0dbSmacallan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21d5f9d0dbSmacallan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22d5f9d0dbSmacallan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23d5f9d0dbSmacallan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24d5f9d0dbSmacallan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25d5f9d0dbSmacallan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26d5f9d0dbSmacallan */ 27d5f9d0dbSmacallan 28d5f9d0dbSmacallan/* 29d5f9d0dbSmacallan * a crude BDF loader for wsdisplay 30d5f9d0dbSmacallan */ 31d5f9d0dbSmacallan 32d5f9d0dbSmacallan#include <stdlib.h> 33d5f9d0dbSmacallan#include <stdio.h> 34d5f9d0dbSmacallan#include <fcntl.h> 35d5f9d0dbSmacallan#include <unistd.h> 36d5f9d0dbSmacallan#include <string.h> 37d5f9d0dbSmacallan#include <errno.h> 38d5f9d0dbSmacallan#include <ctype.h> 39d5f9d0dbSmacallan#include <sys/ioctl.h> 40d5f9d0dbSmacallan#include <err.h> 41d5f9d0dbSmacallan 42d5f9d0dbSmacallan#include <dev/wscons/wsconsio.h> 43d5f9d0dbSmacallan 442b154882Suwe/* 452b154882Suwe * wsdisplay_font but with strings embedded and integer fields in 462b154882Suwe * little endian 472b154882Suwe */ 482b154882Suwestruct wsfthdr { 492b154882Suwe char magic[4]; /* "WSFT" */ 502b154882Suwe char name[64]; 512b154882Suwe uint32_t firstchar; 522b154882Suwe uint32_t numchars; 532b154882Suwe uint32_t encoding; 542b154882Suwe uint32_t fontwidth; 552b154882Suwe uint32_t fontheight; 562b154882Suwe uint32_t stride; 572b154882Suwe uint32_t bitorder; 582b154882Suwe uint32_t byteorder; 592b154882Suwe}; 602b154882Suwe 612b154882Suwe 622b154882Suweconst struct encmap { 632b154882Suwe const char *name; 642b154882Suwe int encoding; 652b154882Suwe} encmap[] = { 662b154882Suwe { "cp437", WSDISPLAY_FONTENC_IBM }, 672b154882Suwe { "ibm", WSDISPLAY_FONTENC_IBM }, 682b154882Suwe { "iso", WSDISPLAY_FONTENC_ISO }, 69a5a7ac3fSmacallan { "iso8859", WSDISPLAY_FONTENC_ISO }, 70a5a7ac3fSmacallan { "iso10646", WSDISPLAY_FONTENC_ISO }, 712b154882Suwe { "iso-8859-1", WSDISPLAY_FONTENC_ISO }, 722b154882Suwe { "iso-8859-2", WSDISPLAY_FONTENC_ISO2 }, 732b154882Suwe { "iso-8859-7", WSDISPLAY_FONTENC_ISO7 }, 742b154882Suwe { "iso2", WSDISPLAY_FONTENC_ISO2 }, 752b154882Suwe { "iso7", WSDISPLAY_FONTENC_ISO7 }, 762b154882Suwe { "iso8859-1", WSDISPLAY_FONTENC_ISO }, 772b154882Suwe { "iso8859-2", WSDISPLAY_FONTENC_ISO2 }, 782b154882Suwe { "iso8859-7", WSDISPLAY_FONTENC_ISO7 }, 792b154882Suwe { "koi8-r", WSDISPLAY_FONTENC_KOI8_R }, 802b154882Suwe { "koi8r", WSDISPLAY_FONTENC_KOI8_R }, 812b154882Suwe { "latin-1", WSDISPLAY_FONTENC_ISO }, 822b154882Suwe { "latin-2", WSDISPLAY_FONTENC_ISO2 }, 832b154882Suwe { "latin1", WSDISPLAY_FONTENC_ISO }, 842b154882Suwe { "latin2", WSDISPLAY_FONTENC_ISO2 }, 852b154882Suwe { "pcvt", WSDISPLAY_FONTENC_PCVT }, 862b154882Suwe { NULL, -1 } 872b154882Suwe}; 882b154882Suwe 892b154882Suweconst char * const encname[] = { 902b154882Suwe#define _ENC(_e) [_e] = #_e 912b154882Suwe _ENC(WSDISPLAY_FONTENC_ISO), 922b154882Suwe _ENC(WSDISPLAY_FONTENC_IBM), 932b154882Suwe _ENC(WSDISPLAY_FONTENC_PCVT), 942b154882Suwe _ENC(WSDISPLAY_FONTENC_ISO7), 952b154882Suwe _ENC(WSDISPLAY_FONTENC_ISO2), 962b154882Suwe _ENC(WSDISPLAY_FONTENC_KOI8_R), 972b154882Suwe}; 982b154882Suwe 992b154882Suwe 1002b154882Suweconst char *ofile = NULL; 1012b154882Suweint encoding = -1; 10261348eb3Smacallanint verbose = 0; 103554a5d78Smacallanint dump = 0; 1042d60533cSmacallanint header = 0; 105a5a7ac3fSmacallanint force = 0; 1067d6aaba4Smacallanchar commentbuf[2048] = ""; 1077d6aaba4Smacallanint commentptr = 0; 10851bbcd86Smacallanchar fontname[64] = ""; 1092b154882Suwe 110554a5d78Smacallanvoid 111554a5d78Smacallandump_line(char *gptr, int stride) 112554a5d78Smacallan{ 113554a5d78Smacallan int i, j, msk, c; 114554a5d78Smacallan 115554a5d78Smacallan for (i = 0; i < stride; i++) { 116554a5d78Smacallan c = gptr[i]; 117554a5d78Smacallan msk = 0x80; 118554a5d78Smacallan for (j = 0; j < 8; j++) { 119554a5d78Smacallan putchar((c & msk) != 0 ? '#' : ' '); 120554a5d78Smacallan msk = msk >> 1; 121554a5d78Smacallan } 122554a5d78Smacallan } 123ec211365Smacallan printf("\n"); 124554a5d78Smacallan} 125554a5d78Smacallan 1262d60533cSmacallanvoid 1272d60533cSmacallanwrite_wsf(const char *oname, struct wsdisplay_font *f, char *buffer, int buflen) 1282d60533cSmacallan{ 1292d60533cSmacallan struct wsfthdr h; 1302d60533cSmacallan 1312d60533cSmacallan memset(&h, 0, sizeof(h)); 1322d60533cSmacallan strncpy(h.magic, "WSFT", sizeof(h.magic)); 1332d60533cSmacallan strncpy(h.name, f->name, sizeof(h.name)); 1342d60533cSmacallan h.firstchar = htole32(f->firstchar); 1352d60533cSmacallan h.numchars = htole32(f->numchars); 1362d60533cSmacallan h.encoding = htole32(f->encoding); 1372d60533cSmacallan h.fontwidth = htole32(f->fontwidth); 1382d60533cSmacallan h.fontheight = htole32(f->fontheight); 1392d60533cSmacallan h.stride = htole32(f->stride); 1402d60533cSmacallan h.bitorder = htole32(f->bitorder); 1412d60533cSmacallan h.byteorder = htole32(f->byteorder); 1422d60533cSmacallan 1432d60533cSmacallan int wsfd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0644); 1442d60533cSmacallan if (wsfd < 0) 1452d60533cSmacallan err(EXIT_FAILURE, "%s", ofile); 1462d60533cSmacallan 1472d60533cSmacallan ssize_t nwritten; 1482d60533cSmacallan nwritten = write(wsfd, &h, sizeof(h)); 1492d60533cSmacallan if (nwritten < 0) 1502d60533cSmacallan err(EXIT_FAILURE, "%s", ofile); 1512d60533cSmacallan if (nwritten != sizeof(h)) 1522d60533cSmacallan errx(EXIT_FAILURE, "%s: partial write", ofile); 1532d60533cSmacallan 1542d60533cSmacallan nwritten = write(wsfd, buffer, buflen); 1552d60533cSmacallan if (nwritten < 0) 1562d60533cSmacallan err(EXIT_FAILURE, "%s", ofile); 1572d60533cSmacallan if (nwritten != buflen) 1582d60533cSmacallan errx(EXIT_FAILURE, "%s: partial write", ofile); 1592d60533cSmacallan close(wsfd); 1602d60533cSmacallan} 1612d60533cSmacallan 1622d60533cSmacallanint 16351bbcd86Smacallanwrite_header(const char *filename, struct wsdisplay_font *f, 1642d60533cSmacallan char *buffer, int buflen) 1652d60533cSmacallan{ 1662d60533cSmacallan FILE *output; 1677dd7205fSmacallan int i, j, x, y, idx, pxls, left; 16843d302b1Smacallan char name[64], c, msk; 1692d60533cSmacallan 1702d60533cSmacallan /* now output as a header file */ 17143d302b1Smacallan snprintf(name, sizeof(name), "%s_%dx%d", f->name, 1722d60533cSmacallan f->fontwidth, f->fontheight); 17343d302b1Smacallan for (i = 0; i < strlen(name); i++) { 17443d302b1Smacallan if (isblank((int)name[i])) 17543d302b1Smacallan name[i]='_'; 1762d60533cSmacallan } 1772d60533cSmacallan if ((output = fopen(filename, "w")) == NULL) { 1782d60533cSmacallan fprintf(stderr, "Can't open output file %s\n", filename); 1792d60533cSmacallan return -1; 1802d60533cSmacallan } 1817d6aaba4Smacallan if (commentptr > 0) { 1827d6aaba4Smacallan fprintf(output, "/*\n"); 1837d6aaba4Smacallan fputs(commentbuf, output); 1847d6aaba4Smacallan fprintf(output, "*/\n\n"); 1857d6aaba4Smacallan } 1867d6aaba4Smacallan 18743d302b1Smacallan fprintf(output, "static u_char %s_data[];\n", name); 1882d60533cSmacallan fprintf(output, "\n"); 18943d302b1Smacallan fprintf(output, "static struct wsdisplay_font %s = {\n", name); 19051bbcd86Smacallan fprintf(output, "\t\"%s\",\t\t\t/* typeface name */\n", f->name); 1912d60533cSmacallan fprintf(output, "\t%d,\t\t\t\t/* firstchar */\n", f->firstchar); 1922d60533cSmacallan fprintf(output, "\t%d,\t\t\t\t/* numchars */\n", f->numchars); 1932d60533cSmacallan fprintf(output, "\t%d,\t\t\t\t/* encoding */\n", f->encoding); 1942d60533cSmacallan fprintf(output, "\t%d,\t\t\t\t/* fontwidth */\n", f->fontwidth); 1952d60533cSmacallan fprintf(output, "\t%d,\t\t\t\t/* fontheight */\n", f->fontheight); 1962d60533cSmacallan fprintf(output, "\t%d,\t\t\t\t/* stride */\n", f->stride); 1972d60533cSmacallan fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* bit order */\n"); 1982d60533cSmacallan fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* byte order */\n"); 19943d302b1Smacallan fprintf(output, "\t%s_data\t\t/* data */\n", name); 2002d60533cSmacallan fprintf(output, "};\n\n"); 20143d302b1Smacallan fprintf(output, "static u_char %s_data[] = {\n", name); 2022d60533cSmacallan for (i = f->firstchar; i < f->firstchar + f->numchars; i++) { 2032d60533cSmacallan fprintf(output, "\t/* %d */\n", i); 2042d60533cSmacallan idx = i * f->stride * f->fontheight; 2052d60533cSmacallan for (y = 0; y < f->fontheight; y++) { 2062d60533cSmacallan for (x = 0; x < f->stride; x++) { 2072d60533cSmacallan fprintf(output, "0x%02x, ",buffer[idx + x]); 2082d60533cSmacallan } 2092d60533cSmacallan fprintf(output, "/* "); 2107dd7205fSmacallan pxls = f->fontwidth; 2112d60533cSmacallan for (x = 0; x < f->stride; x++) { 2122d60533cSmacallan c = buffer[idx + x]; 2132d60533cSmacallan msk = 0x80; 2147dd7205fSmacallan left = pxls > 8 ? 8 : pxls; 2157dd7205fSmacallan for (j = 0; j < left; j++) { 216426ebb7fSmacallan fprintf(output, "%s", 217426ebb7fSmacallan (c & msk) != 0 ? "[]" : ". "); 2182d60533cSmacallan msk = msk >> 1; 2192d60533cSmacallan } 2207dd7205fSmacallan pxls -= 8; 2212d60533cSmacallan } 2222d60533cSmacallan fprintf(output, " */\n"); 2232d60533cSmacallan 2242d60533cSmacallan idx += f->stride; 2252d60533cSmacallan } 2262d60533cSmacallan } 2272d60533cSmacallan fprintf(output, "};\n"); 2282d60533cSmacallan fclose(output); 2292d60533cSmacallan return 0; 2302d60533cSmacallan} 2312d60533cSmacallan 232d5f9d0dbSmacallanvoid 233d5f9d0dbSmacallaninterpret(FILE *foo) 234d5f9d0dbSmacallan{ 23551bbcd86Smacallan char line[128], *arg, name[64] = "foo", *buffer; 2362b154882Suwe int buflen = -1; 237d5f9d0dbSmacallan int len, in_char = 0, current = -1, stride = 0, charsize = 0; 238d5f9d0dbSmacallan int width, height, x, y, num; 239d5f9d0dbSmacallan int first = 255, last = 0; 240d5f9d0dbSmacallan int left, top, lines; 241d5f9d0dbSmacallan int bl = 255, bt = 255, br = -1, bb = -1; 242d5f9d0dbSmacallan struct wsdisplay_font f; 2432b154882Suwe int status; 244d5f9d0dbSmacallan 245d5f9d0dbSmacallan while (fgets(line, sizeof(line), foo) != NULL) { 246d5f9d0dbSmacallan int i = 0; 247d5f9d0dbSmacallan /* separate keyword from parameters */ 248d5f9d0dbSmacallan len = strlen(line); 249d5f9d0dbSmacallan while (!isspace(line[i]) && (i < len)) i++; 250d5f9d0dbSmacallan line[i] = 0; 251d5f9d0dbSmacallan arg = &line[i + 1]; 252d5f9d0dbSmacallan i = 0; 253d5f9d0dbSmacallan len = strlen(arg); 254d5f9d0dbSmacallan /* get rid of garbage */ 255d5f9d0dbSmacallan while ((!iscntrl(arg[i])) && (arg[i] != 0)) { 256d5f9d0dbSmacallan i++; 257d5f9d0dbSmacallan } 258d5f9d0dbSmacallan arg[i] = 0; 259d5f9d0dbSmacallan if (strcmp(line, "FAMILY_NAME") == 0) { 260d5f9d0dbSmacallan /* cut off quotation marks */ 261d5f9d0dbSmacallan strncpy(name, arg + 1, 64); 262d5f9d0dbSmacallan name[strlen(name) - 1] = 0; 26361348eb3Smacallan if (verbose) printf("name: %s\n", name); 2647d6aaba4Smacallan } else if (strcmp(line, "COMMENT") == 0) { 2657d6aaba4Smacallan commentptr += snprintf(&commentbuf[commentptr], 2667d6aaba4Smacallan 2048 - commentptr, 2677d6aaba4Smacallan "%s\n", arg); 268a5a7ac3fSmacallan } else if (strcmp(line, "SPACING") == 0) { 269a5a7ac3fSmacallan char spc[16]; 270a5a7ac3fSmacallan int res; 271a5a7ac3fSmacallan res = sscanf(arg, "%s", spc); 272a5a7ac3fSmacallan if (res > 0) { 273a5a7ac3fSmacallan if (verbose) printf("spacing %s\n", spc); 274a5a7ac3fSmacallan if ((spc[1] == 'P') && (force == 0)) { 275a5a7ac3fSmacallan fprintf(stderr, "This is a proportional font, results are probably not suitable for console use.\n"); 276a5a7ac3fSmacallan fprintf(stderr, "Use -f to override if you want to try it anyway.\n"); 277a5a7ac3fSmacallan exit(1); 278a5a7ac3fSmacallan } 279a5a7ac3fSmacallan } 280d5f9d0dbSmacallan } else if (strcmp(line, "FONTBOUNDINGBOX") == 0) { 281d5f9d0dbSmacallan int res; 282d5f9d0dbSmacallan res = sscanf(arg, "%d %d %d %d", 283d5f9d0dbSmacallan &width, &height, &x, &y); 284d5f9d0dbSmacallan stride = (width + 7) >> 3; 28561348eb3Smacallan if (verbose) printf("box %d x %d\n", width, height); 286d5f9d0dbSmacallan if (stride > 2) { 28761348eb3Smacallan err(EXIT_FAILURE, 28861348eb3Smacallan "no fonts wider than 16 work for now\n"); 289d5f9d0dbSmacallan } 290d5f9d0dbSmacallan charsize = height * stride; 291d5f9d0dbSmacallan buflen = 256 * charsize; 2922b154882Suwe buffer = calloc(1, buflen); 293d5f9d0dbSmacallan if (buffer == NULL) { 29461348eb3Smacallan err(EXIT_FAILURE, 29561348eb3Smacallan "failed to allocate %dKB for glyphs\n", 296d5f9d0dbSmacallan buflen); 297ce18d260Suwe } 298d5f9d0dbSmacallan } else if (strcmp(line, "CHARS") == 0) { 299d5f9d0dbSmacallan if (sscanf(arg, "%d", &num) == 1) 30061348eb3Smacallan if (verbose) 30161348eb3Smacallan printf("number of characters: %d\n", num); 302d5f9d0dbSmacallan } else if (strcmp(line, "STARTCHAR") == 0) { 303d5f9d0dbSmacallan in_char = 1; 304d5f9d0dbSmacallan } else if (strcmp(line, "ENDCHAR") == 0) { 305d5f9d0dbSmacallan in_char = 0; 306d5f9d0dbSmacallan current = -1; 307d5f9d0dbSmacallan } else if (strcmp(line, "ENCODING") == 0) { 308d5f9d0dbSmacallan if (sscanf(arg, "%d", ¤t) == 1) { 309d5f9d0dbSmacallan if (current >= 0 && current < 256) { 310d5f9d0dbSmacallan if (current < first) first = current; 311d5f9d0dbSmacallan if (current > last) last = current; 312554a5d78Smacallan if (dump) printf("glyph %d\n", current); 313d5f9d0dbSmacallan } 314d5f9d0dbSmacallan } 315d5f9d0dbSmacallan } else if (strcmp(line, "BBX") == 0) { 316d5f9d0dbSmacallan int cx, cy, cwi, che; 317d5f9d0dbSmacallan if (sscanf(arg, "%d %d %d %d", &cwi, &che, &cx, &cy) 318d5f9d0dbSmacallan == 4) { 319d5f9d0dbSmacallan left = cx; 320d5f9d0dbSmacallan lines = che; 321d5f9d0dbSmacallan top = height + y - che - cy; 322d5f9d0dbSmacallan if (left < bl) bl = left; 323d5f9d0dbSmacallan if (top < bt) bt = top; 324d5f9d0dbSmacallan if ((left + cwi) > br) br = left + cwi; 325d5f9d0dbSmacallan if ((top + che) > bb) bb = top + che; 326554a5d78Smacallan if(dump && verbose) printf("top %d left %d\n", top, left); 327d5f9d0dbSmacallan } 328d5f9d0dbSmacallan } else if (strcmp(line, "BITMAP") == 0) { 329d5f9d0dbSmacallan int i, j, k, l; 330d5f9d0dbSmacallan char num[32]; 331d5f9d0dbSmacallan char *gptr = &buffer[charsize * current]; 332d5f9d0dbSmacallan char *bptr = gptr + top; 333d5f9d0dbSmacallan uint16_t *bptr16 = (uint16_t *)gptr; 334d5f9d0dbSmacallan bptr16 += top; 335d5f9d0dbSmacallan /* see if the character is in range */ 336d5f9d0dbSmacallan if ((current < 0) || (current > 255)) continue; 337d5f9d0dbSmacallan /* now we read & render the character */ 338d5f9d0dbSmacallan for (i = 0; i < lines; i++) { 339d5f9d0dbSmacallan fgets(num, 32, foo); 340d5f9d0dbSmacallan sscanf(num, "%x", &l); 341d5f9d0dbSmacallan if ((stride) == 2 && (strlen(num) < 4)) 342d5f9d0dbSmacallan l = l << 8; 343d5f9d0dbSmacallan l = l >> left; 344d5f9d0dbSmacallan if (stride == 1) { 345b9e0dc8cSmacallan *bptr = l; 346b9e0dc8cSmacallan bptr++; 347d5f9d0dbSmacallan } else { 348d5f9d0dbSmacallan *bptr16 = htobe16(l); 349d5f9d0dbSmacallan bptr16++; 350ce18d260Suwe } 351d5f9d0dbSmacallan } 352554a5d78Smacallan if (dump) { 353554a5d78Smacallan gptr = &buffer[charsize * current]; 354554a5d78Smacallan for (i = 0; i < height; i++) { 355554a5d78Smacallan dump_line(gptr, stride); 356554a5d78Smacallan gptr += stride; 357554a5d78Smacallan } 358554a5d78Smacallan } 359d5f9d0dbSmacallan } 360d5f9d0dbSmacallan } 36161348eb3Smacallan if (verbose) { 36261348eb3Smacallan printf("range %d to %d\n", first, last); 36361348eb3Smacallan printf("encoding: %s\n", encname[encoding]); 36461348eb3Smacallan printf("actual box: %d %d %d %d\n", bl, bt, br, bb); 36561348eb3Smacallan } 3662b154882Suwe 367d5f9d0dbSmacallan /* now stuff it into a something wsfont understands */ 368d5f9d0dbSmacallan f.fontwidth = width /*(width + 3) & ~3*/; 369d5f9d0dbSmacallan f.fontheight = height; 370d5f9d0dbSmacallan f.firstchar = first; 3712b154882Suwe f.numchars = last - first + 1; 372d5f9d0dbSmacallan f.stride = stride; 3732b154882Suwe f.encoding = encoding; 37451bbcd86Smacallan if (fontname[0] == 0) { 37551bbcd86Smacallan f.name = name; 37651bbcd86Smacallan } else f.name = fontname; 377d5f9d0dbSmacallan f.bitorder = WSDISPLAY_FONTORDER_L2R; 378d5f9d0dbSmacallan f.byteorder = WSDISPLAY_FONTORDER_L2R; 379d5f9d0dbSmacallan f.data = &buffer[first * charsize]; 380d5f9d0dbSmacallan 3812b154882Suwe if (ofile == NULL) { 3822b154882Suwe int fdev = open("/dev/wsfont", O_RDWR, 0); 3832b154882Suwe if (fdev < 0) 3842b154882Suwe err(EXIT_FAILURE, "/dev/wsfont"); 3852b154882Suwe status = ioctl(fdev, WSDISPLAYIO_LDFONT, &f); 3862b154882Suwe if (status != 0) 3872b154882Suwe err(EXIT_FAILURE, "WSDISPLAYIO_LDFONT"); 3882b154882Suwe close(fdev); 3892b154882Suwe } 3902b154882Suwe else { 3912d60533cSmacallan if (header == 0) { 3922d60533cSmacallan write_wsf(ofile, &f, buffer, buflen); 3932d60533cSmacallan } else 39451bbcd86Smacallan write_header(ofile, &f, buffer, buflen); 3952b154882Suwe } 396d5f9d0dbSmacallan} 397d5f9d0dbSmacallan 3982b154882Suwe__dead void 3992b154882Suweusage() 4002b154882Suwe{ 401a5a7ac3fSmacallan fprintf(stderr, "usage: bdfload [-vdhf] [-e encoding] [-N name] [-o ofile.wsf] font.bdf\n"); 4022b154882Suwe exit(EXIT_FAILURE); 4032b154882Suwe} 4042b154882Suwe 4052b154882Suweint 406d5f9d0dbSmacallanmain(int argc, char *argv[]) 407d5f9d0dbSmacallan{ 408d5f9d0dbSmacallan FILE *foo; 4092b154882Suwe const char *encname = NULL; 4102b154882Suwe 4112b154882Suwe int c; 412a5a7ac3fSmacallan while ((c = getopt(argc, argv, "e:o:N:vdhf")) != -1) { 4132b154882Suwe switch (c) { 4142b154882Suwe 4152b154882Suwe /* font encoding */ 4162b154882Suwe case 'e': 4172b154882Suwe if (encname != NULL) 4182b154882Suwe usage(); 4192b154882Suwe encname = optarg; 4202b154882Suwe break; 4212b154882Suwe 4222b154882Suwe /* output file name */ 4232b154882Suwe case 'o': 4242b154882Suwe if (ofile != NULL) 4252b154882Suwe usage(); 4262b154882Suwe ofile = optarg; 4272b154882Suwe break; 4282b154882Suwe 42961348eb3Smacallan case 'v': 43061348eb3Smacallan verbose = 1; 43161348eb3Smacallan break; 43261348eb3Smacallan 433554a5d78Smacallan case 'd': 434554a5d78Smacallan dump = 1; 435554a5d78Smacallan break; 436554a5d78Smacallan 4372d60533cSmacallan case 'h': 4382d60533cSmacallan header = 1; 4392d60533cSmacallan break; 440a5a7ac3fSmacallan case 'f': 441a5a7ac3fSmacallan force = 1; 442a5a7ac3fSmacallan break; 44351bbcd86Smacallan case 'N': 44451bbcd86Smacallan strncpy(fontname, optarg, 64); 44551bbcd86Smacallan break; 4462b154882Suwe case '?': /* FALLTHROUGH */ 4472b154882Suwe default: 4482b154882Suwe usage(); 449d5f9d0dbSmacallan } 450d5f9d0dbSmacallan } 4512b154882Suwe 4522b154882Suwe argc -= optind; 4532b154882Suwe argv += optind; 4542b154882Suwe 4552b154882Suwe if (encname == NULL) { 4562b154882Suwe encoding = WSDISPLAY_FONTENC_ISO; 4572b154882Suwe } 4582b154882Suwe else { 4592b154882Suwe for (const struct encmap *e = encmap; e->name; ++e) { 4602b154882Suwe if (strcmp(e->name, encname) == 0) { 4612b154882Suwe encoding = e->encoding; 4622b154882Suwe break; 4632b154882Suwe } 4642b154882Suwe } 4652b154882Suwe } 4662b154882Suwe 4672b154882Suwe /* get encoding from the bdf file? */ 4682b154882Suwe if (encoding == -1) 4692b154882Suwe encoding = WSDISPLAY_FONTENC_ISO; 4702b154882Suwe 4712b154882Suwe if (argc == 0) 4722b154882Suwe usage(); 4732b154882Suwe 4742b154882Suwe const char *bdfname = argv[0]; 4752b154882Suwe foo = fopen(bdfname, "r"); 4762b154882Suwe if (foo == NULL) 4772b154882Suwe err(EXIT_FAILURE, "%s", bdfname); 4782b154882Suwe 4792b154882Suwe interpret(foo); 4802b154882Suwe return EXIT_SUCCESS; 481d5f9d0dbSmacallan} 482