setfont: support -m and -C, support -m TEXTUAL_MAP (by Vladimir)
authorDenis Vlasenko <vda.linux@googlemail.com>
Tue, 16 Sep 2008 19:35:42 +0000 (19:35 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Tue, 16 Sep 2008 19:35:42 +0000 (19:35 -0000)
build system: fixlet for echo applet config

function                                             old     new   delta
setfont_main                                         157     387    +230
ctoi                                                   -      75     +75
packed_usage                                       24921   24950     +29

console-tools/Config.in
console-tools/loadfont.c
coreutils/Config.in
include/usage.h

index 254e2f7..994140b 100644 (file)
@@ -101,6 +101,21 @@ config SETFONT
        help
          Allows to load console screen map. Useful for i18n.
 
+config FEATURE_SETFONT_TEXTUAL_MAP
+       bool "Support reading textual screen maps"
+       default n
+       depends on SETFONT
+       help
+         Support reading textual screen maps.
+
+config DEFAULT_SETFONT_DIR
+       string "Default directory for console-tools files"
+       default ""
+       depends on SETFONT
+       help
+         Directory to use if setfont's params are simple filenames
+         (not /path/to/file or ./file). Default is "" (no default directory).
+
 config SETKEYCODES
        bool "setkeycodes"
        default n
index ce4734c..38dad3f 100644 (file)
@@ -17,7 +17,7 @@ enum {
        PSF_MODE512 = 0x01,
        PSF_MODEHASTAB = 0x02,
        PSF_MAXMODE = 0x03,
-       PSF_SEPARATOR = 0xFFFF
+       PSF_SEPARATOR = 0xffff
 };
 
 struct psf_header {
@@ -165,6 +165,8 @@ int loadfont_main(int argc UNUSED_PARAM, char **argv)
 }
 #endif
 
+#if ENABLE_SETFONT
+
 /*
 kbd-1.12:
 
@@ -198,36 +200,125 @@ setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig]
 -V     Version
 */
 
-#if ENABLE_SETFONT
+#if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
+static int ctoi(char *s)
+{
+       if (s[0] == '\'' && s[1] != '\0' && s[2] == '\'' && s[3] == '\0')
+               return s[1];
+       // U+ means 0x
+       if (s[0] == 'U' && s[1] == '+') {
+               s[0] = '0';
+               s[1] = 'x';
+       }
+       if (!isdigit(s[0]))
+               return -1;
+       return xstrtoul(s, 0);
+}
+#endif
+
 int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int setfont_main(int argc UNUSED_PARAM, char **argv)
 {
        size_t len;
+       unsigned opts;
+       int fd;
        struct psf_header *psfhdr;
        char *mapfilename;
-       int fd;
+       const char *tty_name = CURRENT_TTY;
 
        opt_complementary = "=1";
-       getopt32(argv, "m:", &mapfilename);
+       opts = getopt32(argv, "m:C:", &mapfilename, &tty_name);
        argv += optind;
 
+       fd = xopen(tty_name, O_NONBLOCK);
+
+       if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not ""
+               if (strchr(*argv, '/') != NULL) {
+                       // goto default fonts location. don't die if doesn't exist
+                       chdir(CONFIG_DEFAULT_SETFONT_DIR "/consolefonts");
+                       // buglet: we don't return to current dir...
+                       // affects "setfont FONT -m ./MAP" case
+               }
+       }
        // load font
        len = 32*1024; // can't be larger
        psfhdr = (struct psf_header *) xmalloc_open_zipped_read_close(*argv, &len);
-       fd = get_console_fd_or_die();
        do_load(fd, psfhdr, len);
 
        // load the screen map, if any
-       if (option_mask32 & 1) { // -m
-               void *map = xmalloc_open_zipped_read_close(mapfilename, &len);
+       if (opts & 1) { // -m
+               unsigned mode = PIO_SCRNMAP;
+               void *map;
+
+               if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not ""
+                       if (strchr(mapfilename, '/') != NULL) {
+                               // goto default keymaps location
+                               chdir(CONFIG_DEFAULT_SETFONT_DIR "/consoletrans");
+                       }
+               }
+               // fetch keymap
+               map = xmalloc_open_zipped_read_close(mapfilename, &len);
+               if (!map)
+                       bb_simple_perror_msg_and_die(mapfilename);
+               // file size is 256 or 512 bytes? -> assume binary map
                if (len == E_TABSZ || len == 2*E_TABSZ) {
-                       //TODO: support textual Unicode console maps:
-                       // 0x00 U+0000  #  NULL (NUL)
-                       // 0x01 U+0001  #  START OF HEADING (SOH)
-                       // 0x02 U+0002  #  START OF TEXT (STX)
-                       // 0x03 U+0003  #  END OF TEXT (ETX)
-                       xioctl(fd, (len == 2*E_TABSZ) ? PIO_UNISCRNMAP : PIO_SCRNMAP, map);
+                       if (len == 2*E_TABSZ)
+                               mode = PIO_UNISCRNMAP;
                }
+#if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
+               // assume textual Unicode console maps:
+               // 0x00 U+0000  #  NULL (NUL)
+               // 0x01 U+0001  #  START OF HEADING (SOH)
+               // 0x02 U+0002  #  START OF TEXT (STX)
+               // 0x03 U+0003  #  END OF TEXT (ETX)
+               else {
+                       int i;
+                       char *token[2];
+                       parser_t *parser;
+
+                       if (ENABLE_FEATURE_CLEAN_UP)
+                               free(map);
+                       map = xmalloc(E_TABSZ * sizeof(unsigned short));
+
+#define unicodes ((unsigned short *)map)
+                       // fill vanilla map
+                       for (i = 0; i < E_TABSZ; i++)
+                               unicodes[i] = 0xf000 + i;
+
+                       parser = config_open(mapfilename);
+                       while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL | PARSE_MIN_DIE)) {
+                               // parse code/value pair
+                               int a = ctoi(token[0]);
+                               int b = ctoi(token[1]);
+                               if (a < 0 || a >= E_TABSZ
+                                || b < 0 || b > 65535
+                               ) {
+                                       bb_error_msg_and_die("map format");
+                               }
+                               // patch map
+                               unicodes[a] = b;
+                               // unicode character is met?
+                               if (b > 255)
+                                       mode = PIO_UNISCRNMAP;
+                       }
+                       if (ENABLE_FEATURE_CLEAN_UP)
+                               config_close(parser);
+
+                       if (mode != PIO_UNISCRNMAP) {
+#define asciis ((unsigned char *)map)
+                               for (i = 0; i < E_TABSZ; i++)
+                                       asciis[i] = unicodes[i];
+#undef asciis
+                       }
+#undef unicodes
+               }
+#endif // ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
+
+               // do set screen map
+               xioctl(fd, mode, map);
+
+               if (ENABLE_FEATURE_CLEAN_UP)
+                       free(map);
        }
 
        return EXIT_SUCCESS;
index 80aca30..8cbc92f 100644 (file)
@@ -649,7 +649,7 @@ config TEST
 config FEATURE_TEST_64
        bool "Extend test to 64 bit"
        default n
-       depends on TEST || FEATURE_TEST_64
+       depends on TEST || ASH_BUILTIN_TEST
        help
          Enable 64-bit support in test.
 
index 71e9f42..b1c4d55 100644 (file)
      "\n       -W      Display warnings about entries that had no matching files" \
 
 #define setfont_trivial_usage \
-       "[-m mapfile] font"
+       "FONT [-m MAPFILE] [-C TTY]"
 #define setfont_full_usage "\n\n" \
        "Load a console font\n" \
      "\nOptions:" \
-     "\n       -m mapfile      Load console screen map from mapfile"
+     "\n       -m MAPFILE      Load console screen map" \
+     "\n       -C TTY          Affect TTY instead of /dev/tty" \
+
 #define setfont_example_usage \
        "$ setfont -m koi8-r /etc/i18n/fontname\n"