ash: in tryexec(), ensure we don't try to run embedded scripts as applets
[oweals/busybox.git] / console-tools / loadfont.c
index e51142c8e9c9743275b9a7dfc999a84eda3041e4..81d0c3db46c2db31dd0f27591b48aff52d60fb1f 100644 (file)
@@ -7,8 +7,56 @@
  * Loads the console font, and possibly the corresponding screen map(s).
  * (Adapted for busybox by Matej Vela.)
  *
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
+//config:config LOADFONT
+//config:      bool "loadfont (5.4 kb)"
+//config:      default y
+//config:      select PLATFORM_LINUX
+//config:      help
+//config:      This program loads a console font from standard input.
+//config:
+//config:config SETFONT
+//config:      bool "setfont (26 kb)"
+//config:      default y
+//config:      select PLATFORM_LINUX
+//config:      help
+//config:      Allows to load console screen map. Useful for i18n.
+//config:
+//config:config FEATURE_SETFONT_TEXTUAL_MAP
+//config:      bool "Support reading textual screen maps"
+//config:      default y
+//config:      depends on SETFONT
+//config:      help
+//config:      Support reading textual screen maps.
+//config:
+//config:config DEFAULT_SETFONT_DIR
+//config:      string "Default directory for console-tools files"
+//config:      default ""
+//config:      depends on SETFONT
+//config:      help
+//config:      Directory to use if setfont's params are simple filenames
+//config:      (not /path/to/file or ./file). Default is "" (no default directory).
+//config:
+//config:comment "Common options for loadfont and setfont"
+//config:      depends on LOADFONT || SETFONT
+//config:
+//config:config FEATURE_LOADFONT_PSF2
+//config:      bool "Support PSF2 console fonts"
+//config:      default y
+//config:      depends on LOADFONT || SETFONT
+//config:
+//config:config FEATURE_LOADFONT_RAW
+//config:      bool "Support old (raw) console fonts"
+//config:      default y
+//config:      depends on LOADFONT || SETFONT
+
+//applet:IF_LOADFONT(APPLET_NOEXEC(loadfont, loadfont, BB_DIR_USR_SBIN, BB_SUID_DROP, loadfont))
+//applet:IF_SETFONT(APPLET_NOEXEC(setfont, setfont, BB_DIR_USR_SBIN, BB_SUID_DROP, setfont))
+
+//kbuild:lib-$(CONFIG_LOADFONT) += loadfont.o
+//kbuild:lib-$(CONFIG_SETFONT) += loadfont.o
+
 #include "libbb.h"
 #include <sys/kd.h>
 
@@ -136,7 +184,7 @@ static void do_loadfont(int fd, unsigned char *inbuf, int height, int width, int
  * Example:
  * At the font position for a capital A-ring glyph, we
  * may have:
- *     00C5,212B,FFFE,0041,030A,FFFF
+ *   00C5,212B,FFFE,0041,030A,FFFF
  * Some font positions may be described by sequences only,
  * namely when there is no precomposed Unicode value for the glyph.
  */
@@ -159,7 +207,7 @@ static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize,
        int glyph;
        uint16_t unicode;
 
-       maxct = tailsz; /* more than enough */
+       maxct = tailsz; /* more than enough */
        up = xmalloc(maxct * sizeof(*up));
 
        for (glyph = 0; glyph < fontsize; glyph++) {
@@ -209,7 +257,7 @@ static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize,
        }
 
        /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
-          this printf did not work on many kernels */
+        * this printf did not work on many kernels */
 
        advice.advised_hashsize = 0;
        advice.advised_hashstep = 0;
@@ -255,10 +303,10 @@ static void do_load(int fd, unsigned char *buffer, size_t len)
        } else
 #endif
 #if ENABLE_FEATURE_LOADFONT_RAW
-       if (len == 9780) {      /* file with three code pages? */
+       if (len == 9780) {  /* file with three code pages? */
                charsize = height = 16;
                font += 40;
-       } else if ((len & 0377) == 0) {         /* bare font */
+       } else if ((len & 0377) == 0) {  /* bare font */
                charsize = height = len / 256;
        } else
 #endif
@@ -285,6 +333,14 @@ static void do_load(int fd, unsigned char *buffer, size_t len)
 
 
 #if ENABLE_LOADFONT
+//usage:#define loadfont_trivial_usage
+//usage:       "< font"
+//usage:#define loadfont_full_usage "\n\n"
+//usage:       "Load a console font from stdin"
+/* //usage:     "\n    -C TTY  Affect TTY instead of /dev/tty" */
+//usage:
+//usage:#define loadfont_example_usage
+//usage:       "$ loadfont < /etc/i18n/fontname\n"
 int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int loadfont_main(int argc UNUSED_PARAM, char **argv)
 {
@@ -292,15 +348,16 @@ int loadfont_main(int argc UNUSED_PARAM, char **argv)
        unsigned char *buffer;
 
        // no arguments allowed!
-       opt_complementary = "=0";
-       getopt32(argv, "");
+       getopt32(argv, "^" "" "\0" "=0");
 
        /*
         * We used to look at the length of the input file
         * with stat(); now that we accept compressed files,
         * just read the entire file.
+        * Len was 32k, but latarcyrheb-sun32.psfu is 34377 bytes
+        * (it has largish Unicode map).
         */
-       len = 32*1024; // can't be larger
+       len = 128*1024;
        buffer = xmalloc_read(STDIN_FILENO, &len);
        // xmalloc_open_zipped_read_close(filename, &len);
        if (!buffer)
@@ -311,11 +368,9 @@ int loadfont_main(int argc UNUSED_PARAM, char **argv)
 }
 #endif
 
-#if ENABLE_SETFONT
-
-/*
-kbd-1.12:
 
+#if ENABLE_SETFONT
+/* kbd-1.12:
 setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig]
 [-ou umap.orig] [-N] [font.new ...] [-m cmap] [-u umap] [-C console]
 [-hNN] [-v] [-V]
@@ -345,8 +400,17 @@ setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig]
 -v     Verbose
 -V     Version
 */
-
-#if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
+//usage:#define setfont_trivial_usage
+//usage:       "FONT [-m MAPFILE] [-C TTY]"
+//usage:#define setfont_full_usage "\n\n"
+//usage:       "Load a console font\n"
+//usage:     "\n       -m MAPFILE      Load console screen map"
+//usage:     "\n       -C TTY          Affect TTY instead of /dev/tty"
+//usage:
+//usage:#define setfont_example_usage
+//usage:       "$ setfont -m koi8-r /etc/i18n/fontname\n"
+
+# if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
 static int ctoi(char *s)
 {
        if (s[0] == '\'' && s[1] != '\0' && s[2] == '\'' && s[3] == '\0')
@@ -360,7 +424,7 @@ static int ctoi(char *s)
                return -1;
        return xstrtoul(s, 0);
 }
-#endif
+# endif
 
 int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int setfont_main(int argc UNUSED_PARAM, char **argv)
@@ -372,8 +436,7 @@ int setfont_main(int argc UNUSED_PARAM, char **argv)
        char *mapfilename;
        const char *tty_name = CURRENT_TTY;
 
-       opt_complementary = "=1";
-       opts = getopt32(argv, "m:C:", &mapfilename, &tty_name);
+       opts = getopt32(argv, "^" "m:C:" "\0" "=1", &mapfilename, &tty_name);
        argv += optind;
 
        fd = xopen_nonblocking(tty_name);
@@ -385,7 +448,7 @@ int setfont_main(int argc UNUSED_PARAM, char **argv)
                }
        }
        // load font
-       len = 32*1024; // can't be larger
+       len = 128*1024;
        buffer = xmalloc_open_zipped_read_close(*argv, &len);
        if (!buffer)
                bb_simple_perror_msg_and_die(*argv);
@@ -411,7 +474,7 @@ int setfont_main(int argc UNUSED_PARAM, char **argv)
                        if (len == 2*E_TABSZ)
                                mode = PIO_UNISCRNMAP;
                }
-#if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
+# if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
                // assume textual Unicode console maps:
                // 0x00 U+0000  #  NULL (NUL)
                // 0x01 U+0001  #  START OF HEADING (SOH)
@@ -458,7 +521,7 @@ int setfont_main(int argc UNUSED_PARAM, char **argv)
                        }
 #undef unicodes
                }
-#endif // ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
+# endif // ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
 
                // do set screen map
                xioctl(fd, mode, map);