diff options
author | Alexey Gladkov <gladkov.alexey@gmail.com> | 2020-05-12 12:35:37 +0200 |
---|---|---|
committer | Alexey Gladkov <gladkov.alexey@gmail.com> | 2020-05-19 11:59:53 +0200 |
commit | a963bbabe48808ef7b4f3373fea8a86e87fa5dbf (patch) | |
tree | a98d41a784bd5868c7d047660905705127916e75 | |
parent | bc603778b15c6703c9e432843d4d5c5f991373ca (diff) | |
download | kbd-a963bbabe48808ef7b4f3373fea8a86e87fa5dbf.tar.gz |
Add long options, help messages
Most utilities have added short and long options. Some error messages
have been modified to simplify the translation.
Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
-rw-r--r-- | src/chvt.c | 44 | ||||
-rw-r--r-- | src/deallocvt.c | 56 | ||||
-rw-r--r-- | src/dumpkeys.c | 135 | ||||
-rw-r--r-- | src/fgconsole.c | 68 | ||||
-rw-r--r-- | src/getkeycodes.c | 65 | ||||
-rw-r--r-- | src/getunimap.c | 63 | ||||
-rw-r--r-- | src/kbd_mode.c | 83 | ||||
-rw-r--r-- | src/kbdinfo.c | 74 | ||||
-rw-r--r-- | src/kbdrate.c | 62 | ||||
-rw-r--r-- | src/libcommon/getfd.c | 6 | ||||
-rw-r--r-- | src/libcommon/libcommon.h | 5 | ||||
-rw-r--r-- | src/libkeymap/ksyms.c | 12 | ||||
-rw-r--r-- | src/libkeymap/loadkeys.c | 2 | ||||
-rw-r--r-- | src/loadkeys.c | 119 | ||||
-rw-r--r-- | src/loadunimap.c | 75 | ||||
-rw-r--r-- | src/mapscrn.c | 106 | ||||
-rw-r--r-- | src/openvt.c | 72 | ||||
-rw-r--r-- | src/psfxtable.c | 16 | ||||
-rw-r--r-- | src/screendump.c | 4 | ||||
-rw-r--r-- | src/setkeycodes.c | 118 | ||||
-rw-r--r-- | src/setleds.c | 8 | ||||
-rw-r--r-- | src/setmetamode.c | 86 | ||||
-rw-r--r-- | src/setvtrgb.c | 78 | ||||
-rw-r--r-- | src/showconsolefont.c | 85 | ||||
-rw-r--r-- | src/showkey.c | 53 | ||||
-rw-r--r-- | src/totextmode.c | 2 |
26 files changed, 1067 insertions, 430 deletions
@@ -19,14 +19,32 @@ #include "libcommon.h" static void __attribute__((noreturn)) -usage(int rc) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, _("Usage: %s [option...] N\n" - "\n" - "Options:\n" - " -h, --help print this usage message;\n" - " -V, --version print version number.\n"), - get_progname()); + const struct kbd_help *h; + fprintf(stderr, _("Usage: %s [option...] N\n"), get_progname()); + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + exit(rc); } @@ -56,23 +74,29 @@ int main(int argc, char *argv[]) set_progname(argv[0]); setuplocale(); + const struct kbd_help opthelp[] = { + { "-h, --help", _("print this usage message.") }, + { "-V, --version", _("print version number.") }, + { NULL, NULL } + }; + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { case 'V': print_version_and_exit(); break; case 'h': - usage(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); break; case '?': - usage(EX_USAGE); + usage(EX_USAGE, opthelp); break; } } if (argc == optind) { fprintf(stderr, _("Argument required\n")); - usage(EX_USAGE); + usage(EX_USAGE, opthelp); } if ((fd = getfd(NULL)) < 0) diff --git a/src/deallocvt.c b/src/deallocvt.c index de46347f..e1a24f0a 100644 --- a/src/deallocvt.c +++ b/src/deallocvt.c @@ -19,15 +19,32 @@ #include "libcommon.h" static void __attribute__((noreturn)) -usage(int rc) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, _("Usage: %s [option...] [N ...]\n" - "\n" - "Options:\n" - "\n" - " -h, --help print this usage message;\n" - " -V, --version print version number.\n"), - get_progname()); + const struct kbd_help *h; + fprintf(stderr, _("Usage: %s [option...] [N ...]\n"), get_progname()); + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + exit(rc); } @@ -35,6 +52,12 @@ int main(int argc, char *argv[]) { int fd, num, i; + if (argc < 1) /* unlikely */ + return EXIT_FAILURE; + + set_progname(argv[0]); + setuplocale(); + const char *const short_opts = "hV"; const struct option long_opts[] = { { "help", no_argument, NULL, 'h' }, @@ -42,10 +65,11 @@ int main(int argc, char *argv[]) { NULL, 0, NULL, 0 } }; - if (argc < 1) /* unlikely */ - return EXIT_FAILURE; - set_progname(argv[0]); - setuplocale(); + const struct kbd_help opthelp[] = { + { "-h, --help", _("print this usage message.") }, + { "-V, --version", _("print version number.") }, + { NULL, NULL } + }; while ((i = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (i) { @@ -53,9 +77,9 @@ int main(int argc, char *argv[]) print_version_and_exit(); break; case 'h': - usage(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); case '?': - usage(EX_USAGE); + usage(EX_USAGE, opthelp); } } @@ -78,9 +102,9 @@ int main(int argc, char *argv[]) for (i = 1; i < argc; i++) { num = atoi(argv[i]); if (num == 0) { - kbd_error(EXIT_FAILURE, 0, _("0: illegal VT number\n")); + kbd_error(EXIT_FAILURE, 0, _("0: illegal VT number")); } else if (num == 1) { - kbd_error(EXIT_FAILURE, 0, _("VT 1 is the console and cannot be deallocated\n")); + kbd_error(EXIT_FAILURE, 0, _("VT 1 is the console and cannot be deallocated")); } else if (ioctl(fd, VT_DISALLOCATE, num)) { kbd_error(EXIT_FAILURE, errno, _("could not deallocate console %d: " "ioctl VT_DISALLOCATE"), diff --git a/src/dumpkeys.c b/src/dumpkeys.c index 184b11c9..9bcc5d15 100644 --- a/src/dumpkeys.c +++ b/src/dumpkeys.c @@ -25,37 +25,68 @@ static int fd; static void __attribute__((noreturn)) -usage(int rc) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, _("dumpkeys version %s"), PACKAGE_VERSION); - fprintf(stderr, _("\n" - "usage: dumpkeys [options...]\n" - "\n" - "Options:\n" - " -i, --short-info display information about keyboard driver;\n" - " -l, -s, --long-info display above and symbols known to loadkeys;\n" - " -n, --numeric display keytable in hexadecimal notation;\n" - " -f, --full-table don't use short-hand notations, one row per keycode;\n" - " -1, --separate-lines one line per (modifier,keycode) pair;\n" - " -S, --shape=\n" - " -t, --funcs-only display only the function key strings;\n" - " -k, --keys-only display only key bindings;\n" - " -d, --compose-only display only compose key combinations;\n" - " -c, --charset=")); + const struct kbd_help *h; + + fprintf(stderr, _("Usage: %s [option...]\n"), get_progname()); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Available charsets: ")); lk_list_charsets(stderr); - fprintf(stderr, _( - " interpret character action codes to be from the\n" - " specified character set;\n")); - fprintf(stderr, _( - " -v, --verbose explain what is being done;\n" - " -h, --help print this usage message;\n" - " -V, --version print version number.\n")); + fprintf(stderr, "\n"); + + fprintf(stderr, _("Available shapes:\n" + " 2 - default output;\n" + " 4 - one line for each keycode;\n" + " 8 - one line for each (modifier,keycode) pair;\n" + " 16 - one line for each keycode until 1st hole.\n" + )); + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + exit(rc); } int main(int argc, char *argv[]) { - const char *short_opts = "hilvsnf1tkdS:c:V"; + int c, rc; + int kbd_mode; + + char long_info = 0; + char short_info = 0; + char numeric = 0; + lk_table_shape table = LK_SHAPE_DEFAULT; + char funcs_only = 0; + char keys_only = 0; + char diac_only = 0; + char *console = NULL; + + struct lk_ctx *ctx; + + set_progname(argv[0]); + setuplocale(); + + const char *short_opts = "hilvsnf1tkdS:c:C:V"; const struct option long_opts[] = { { "help", no_argument, NULL, 'h' }, { "short-info", no_argument, NULL, 'i' }, @@ -68,33 +99,35 @@ int main(int argc, char *argv[]) { "keys-only", no_argument, NULL, 'k' }, { "compose-only", no_argument, NULL, 'd' }, { "charset", required_argument, NULL, 'c' }, + { "console", required_argument, NULL, 'C' }, { "verbose", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; - int c, rc; - int kbd_mode; - - char long_info = 0; - char short_info = 0; - char numeric = 0; - lk_table_shape table = LK_SHAPE_DEFAULT; - char funcs_only = 0; - char keys_only = 0; - char diac_only = 0; - - struct lk_ctx *ctx; - - set_progname(argv[0]); - setuplocale(); + const struct kbd_help opthelp[] = { + { "-i, --short-info", _("display information about keyboard driver.") }, + { "-l, -s, --long-info", _("display above and symbols known to loadkeys.") }, + { "-n, --numeric", _("display keytable in hexadecimal notation.") }, + { "-f, --full-table", _("don't use short-hand notations, one row per keycode.") }, + { "-1, --separate-lines", _("one line per (modifier,keycode) pair.") }, + { "-S, --shape={2|4|8|16}", _("") }, + { "-t, --funcs-only", _("display only the function key strings.") }, + { "-k, --keys-only", _("display only key bindings.") }, + { "-d, --compose-only", _("display only compose key combinations.") }, + { "-c, --charset=CHARSET", _("interpret character action codes to be from the specified character set.") }, + { "-C, --console=DEV", _("the console device to be used.") }, + { "-v, --verbose", _("be more verbose.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; ctx = lk_init(); if (!ctx) { exit(EXIT_FAILURE); } - while ((c = getopt_long(argc, argv, - short_opts, long_opts, NULL)) != -1) { + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { case 'i': short_info = 1; @@ -131,34 +164,34 @@ int main(int argc, char *argv[]) if ((lk_set_charset(ctx, optarg)) != 0) { fprintf(stderr, _("unknown charset %s - ignoring charset request\n"), optarg); - usage(EX_USAGE); + usage(EX_USAGE, opthelp); } printf("charset \"%s\"\n", optarg); break; + case 'C': + console = optarg; + break; case 'V': print_version_and_exit(); break; case 'h': - usage(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); break; case '?': - usage(EX_USAGE); + usage(EX_USAGE, opthelp); break; } } if (optind < argc) - usage(EX_USAGE); + usage(EX_USAGE, opthelp); - if ((fd = getfd(NULL)) < 0) + if ((fd = getfd(console)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); /* check whether the keyboard is in Unicode mode */ - if (ioctl(fd, KDGKBMODE, &kbd_mode)) { - fprintf(stderr, _("%s: error reading keyboard mode: %m\n"), - get_progname()); - exit(EXIT_FAILURE); - } + if (ioctl(fd, KDGKBMODE, &kbd_mode)) + kbd_error(EXIT_FAILURE, errno, _("error reading keyboard mode")); if (kbd_mode == K_UNICODE) { lk_set_parser_flags(ctx, LK_FLAG_PREFER_UNICODE); diff --git a/src/fgconsole.c b/src/fgconsole.c index 01e4743d..ce50e9ed 100644 --- a/src/fgconsole.c +++ b/src/fgconsole.c @@ -5,6 +5,7 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <errno.h> #include <sys/ioctl.h> #include <getopt.h> @@ -15,20 +16,34 @@ #include "libcommon.h" static void __attribute__((noreturn)) -usage(int rc) +usage(int rc, const struct kbd_help *options) { - const char *progname = get_progname(); - fprintf(stderr, _( - "%s version %s\n" - "\n" - "Usage: %s [options]\n" - "\n" - "Options:\n" - "\n" - " -n, --next-available print number of next unallocated VT\n" - " -h, --help print this usage message;\n" - " -V, --version print version number.\n"), - progname, PACKAGE_VERSION, progname); + const struct kbd_help *h; + + fprintf(stderr, _("Usage: %s [option...]\n"), get_progname()); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + exit(rc); } @@ -37,18 +52,31 @@ int main(int argc, char **argv) struct vt_stat vtstat; int fd, vtno = -1, c, show_vt = 0; struct serial_struct sr; + char *console = NULL; + + set_progname(argv[0]); + setuplocale(); + const struct option long_opts[] = { + { "console", required_argument, NULL, 'C' }, { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, 'V' }, { "next-available", no_argument, NULL, 'n' }, { NULL, 0, NULL, 0 } }; + const struct kbd_help opthelp[] = { + { "-C, --console=DEV", _("the console device to be used.") }, + { "-n, --next-available", _("print number of next unallocated VT.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; - set_progname(argv[0]); - setuplocale(); - - while ((c = getopt_long(argc, argv, "Vhn", long_opts, NULL)) != EOF) { + while ((c = getopt_long(argc, argv, "C:Vhn", long_opts, NULL)) != EOF) { switch (c) { + case 'C': + console = optarg; + break; case 'n': show_vt = 1; break; @@ -56,15 +84,15 @@ int main(int argc, char **argv) print_version_and_exit(); break; case 'h': - usage(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); break; case '?': - usage(EX_USAGE); + usage(EX_USAGE, opthelp); break; } } - if ((fd = getfd(NULL)) < 0) + if ((fd = getfd(console)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); if (show_vt) { diff --git a/src/getkeycodes.c b/src/getkeycodes.c index b189acb4..6cbf5262 100644 --- a/src/getkeycodes.c +++ b/src/getkeycodes.c @@ -9,6 +9,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <getopt.h> #include <fcntl.h> #include <sysexits.h> #include <sys/ioctl.h> @@ -17,26 +18,70 @@ #include "libcommon.h" static void __attribute__((noreturn)) -usage(int rc) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, _("usage: getkeycodes\n")); + const struct kbd_help *h; + fprintf(stderr, _("Usage: %s [option...]\n"), get_progname()); + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + exit(rc); } int main(int argc, char **argv) { - int fd; + int fd, c; unsigned int sc, sc0; struct kbkeycode a; set_progname(argv[0]); setuplocale(); - if (argc == 2 && !strcmp(argv[1], "-V")) - print_version_and_exit(); + const char *const short_opts = "hV"; + const struct option long_opts[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; - if (argc != 1) - usage(EX_USAGE); + const struct kbd_help opthelp[] = { + { "-h, --help", _("print this usage message.") }, + { "-V, --version", _("print version number.") }, + { NULL, NULL } + }; + + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { + switch (c) { + case 'V': + print_version_and_exit(); + break; + case 'h': + usage(EXIT_SUCCESS, opthelp); + break; + case '?': + usage(EX_USAGE, opthelp); + break; + } + } if ((fd = getfd(NULL)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); @@ -65,8 +110,10 @@ int main(int argc, char **argv) } for (sc = (sc0 & ~7U); sc < 256; sc++) { - if (sc == 128) - printf(_("\n\nEscaped scancodes e0 xx (hex)\n")); + if (sc == 128) { + printf("\n\n"); + printf(_("Escaped scancodes e0 xx (hex)\n")); + } if (sc % 8 == 0) { if (sc < 128) printf("\n 0x%02x: ", sc); diff --git a/src/getunimap.c b/src/getunimap.c index 765672ca..3812ccd8 100644 --- a/src/getunimap.c +++ b/src/getunimap.c @@ -3,6 +3,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <getopt.h> #include <errno.h> #include <ctype.h> #include <unistd.h> @@ -28,9 +29,32 @@ ud_compar(const void *u1, const void *u2) } static void __attribute__((noreturn)) -usage(int rc) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, _("Usage:\n\t%s [-s] [-C console]\n"), get_progname()); + const struct kbd_help *h; + fprintf(stderr, _("Usage: %s [option...]\n"), get_progname()); + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + exit(rc); } @@ -46,11 +70,23 @@ int main(int argc, char **argv) set_progname(argv[0]); setuplocale(); - if (argc == 2 && - (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) - print_version_and_exit(); - - while ((c = getopt(argc, argv, "sC:")) != EOF) { + const char *const short_opts = "hVsC:"; + const struct option long_opts[] = { + { "sort", no_argument, NULL, 's' }, + { "console", required_argument, NULL, 'C' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + const struct kbd_help opthelp[] = { + { "-s, --sort", _("sort and merge elements.") }, + { "-C, --console=DEV", _("the console device to be used.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; + + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { case 's': sortflag = 1; @@ -58,13 +94,20 @@ int main(int argc, char **argv) case 'C': console = optarg; break; - default: - usage(EX_USAGE); + case 'V': + print_version_and_exit(); + break; + case 'h': + usage(EXIT_SUCCESS, opthelp); + break; + case '?': + usage(EX_USAGE, opthelp); + break; } } if (optind < argc) - usage(EX_USAGE); + usage(EX_USAGE, opthelp); if ((fd = getfd(console)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); diff --git a/src/kbd_mode.c b/src/kbd_mode.c index 0de0f711..90a2f15c 100644 --- a/src/kbd_mode.c +++ b/src/kbd_mode.c @@ -10,6 +10,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <getopt.h> #include <errno.h> #include <unistd.h> #include <sysexits.h> @@ -19,10 +20,36 @@ #include "libcommon.h" static void __attribute__((noreturn)) -usage(void) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, _("usage: kbd_mode [-a|-u|-k|-s] [-f] [-C device]\n")); - exit(EX_USAGE); + const struct kbd_help *h; + fprintf(stderr, _("Usage: %s [option...]\n"), get_progname()); + fprintf(stderr, "\n"); + fprintf(stderr, _("This utility reports or sets the keyboard mode.\n")); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + + exit(rc); } static void @@ -54,32 +81,53 @@ int main(int argc, char *argv[]) set_progname(argv[0]); setuplocale(); - if (argc == 2 && !strcmp(argv[1], "-V")) - print_version_and_exit(); + const char *short_opts = "auskfC:hV"; + const struct option long_opts[] = { + { "ascii", no_argument, NULL, 'a' }, + { "keycode", no_argument, NULL, 'k' }, + { "scancode", no_argument, NULL, 's' }, + { "unicode", no_argument, NULL, 'u' }, + { "force", no_argument, NULL, 'f' }, + { "console", required_argument, NULL, 'C' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + const struct kbd_help opthelp[] = { + { "-a, --ascii", _("set ASCII mode.") }, + { "-k, --keycode", _("set keycode mode.") }, + { "-s, --scancode", _("set scancode mode.") }, + { "-u, --unicode", _("set UTF-8 mode.") }, + { "-f, --force", _("switch the mode even if it makes the keyboard unusable.") }, + { "-C, --console=DEV", _("the console device to be used.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; - while ((c = getopt(argc, argv, "auskfC:")) != EOF) { + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { case 'a': if (n > 0) - usage(); + usage(EX_USAGE, opthelp); mode = K_XLATE; n++; break; case 'u': if (n > 0) - usage(); + usage(EX_USAGE, opthelp); mode = K_UNICODE; n++; break; case 's': if (n > 0) - usage(); + usage(EX_USAGE, opthelp); mode = K_RAW; n++; break; case 'k': if (n > 0) - usage(); + usage(EX_USAGE, opthelp); mode = K_MEDIUMRAW; n++; break; @@ -87,12 +135,19 @@ int main(int argc, char *argv[]) force = 1; break; case 'C': - if (!optarg || !optarg[0]) - usage(); + if (optarg == NULL || optarg[0] == '\0') + usage(EX_USAGE, opthelp); console = optarg; break; - default: - usage(); + case 'V': + print_version_and_exit(); + break; + case 'h': + usage(EXIT_SUCCESS, opthelp); + break; + case '?': + usage(EX_USAGE, opthelp); + break; } } diff --git a/src/kbdinfo.c b/src/kbdinfo.c index e24cc5c6..4dbbf90e 100644 --- a/src/kbdinfo.c +++ b/src/kbdinfo.c @@ -16,24 +16,44 @@ static const char *action = NULL; static const char *value = NULL; static void __attribute__((noreturn)) -usage(int code) +usage(int rc, const struct kbd_help *options) { + const struct kbd_help *h; + fprintf(stderr, - _("Usage: %1$s [options] getmode [text|graphics]\n" - " or: %1$s [options] gkbmode [raw|xlate|mediumraw|unicode]\n" - " or: %1$s [options] gkbmeta [metabit|escprefix]\n" - " or: %1$s [options] gkbled [scrolllock|numlock|capslock]\n" - "\n" - "The utility allows to read and check various parameters\n" - "of the keyboard and virtual console.\n" - "\n" - "Options:\n" - " -C, --console=DEV the console device to be used;\n" - " -h, --help print this usage message;\n" - " -V, --version print version number.\n" - ), - get_progname()); - exit(code); + _("Usage: %1$s [option...] getmode [text|graphics]\n" + " or: %1$s [option...] gkbmode [raw|xlate|mediumraw|unicode]\n" + " or: %1$s [option...] gkbmeta [metabit|escprefix]\n" + " or: %1$s [option...] gkbled [scrolllock|numlock|capslock]\n"), + get_progname()); + fprintf(stderr, "\n"); + fprintf(stderr, _( + "The utility allows to read and check various parameters\n" + "of the keyboard and virtual console.\n")); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + + exit(rc); } static int @@ -53,6 +73,9 @@ int main(int argc, char **argv) char flags; const char *console = NULL; + set_progname(argv[0]); + setuplocale(); + const char *short_opts = "C:hV"; const struct option long_opts[] = { { "console", required_argument, NULL, 'C' }, @@ -60,31 +83,34 @@ int main(int argc, char **argv) { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; - - set_progname(argv[0]); - setuplocale(); + const struct kbd_help opthelp[] = { + { "-C, --console=DEV", _("the console device to be used.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { case 'C': if (optarg == NULL || optarg[0] == '\0') - usage(EX_USAGE); + usage(EX_USAGE, opthelp); console = optarg; break; case 'V': print_version_and_exit(); break; case 'h': - usage(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); break; case '?': - usage(EX_USAGE); + usage(EX_USAGE, opthelp); break; } } if (optind == argc) { - kbd_error(EXIT_FAILURE, 0, _("Error: Not enough arguments.\n")); + kbd_error(EXIT_FAILURE, 0, _("Not enough arguments.")); } action = argv[optind++]; @@ -159,7 +185,7 @@ int main(int argc, char **argv) } } else { - kbd_warning(0, _("Error: Unrecognized action: %s\n"), action); + kbd_warning(0, _("Unrecognized action: %s"), action); } close(fd); diff --git a/src/kbdrate.c b/src/kbdrate.c index 4622e436..fd392ad6 100644 --- a/src/kbdrate.c +++ b/src/kbdrate.c @@ -316,21 +316,36 @@ int delay = 250; /* Default delay */ #endif static void __attribute__((noreturn)) -usage(int rc) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, _( - "Usage: kbdrate [options...]\n" - "\n" - "The prorgam sets the keyboard repeat rate and delay in user mode.\n" - "\n" - "Options:\n" - " -r, --rate=NUM set the rate in characters per second (default %.1f);\n" - " -d, --delay=NUM set the amount of time the key must remain\n" - " depressed before it will start to repeat (default %d);\n" - " -s, --silent suppress all normal output;\n" - " -h, --help print this usage message;\n" - " -V, --version print version number.\n" - "\n"), rate, delay); + const struct kbd_help *h; + + fprintf(stderr, _("Usage: %s [option...]\n"), get_progname()); + fprintf(stderr, "\n"); + fprintf(stderr, _("The prorgam sets the keyboard repeat rate and delay in user mode.\n")); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + exit(rc); } @@ -339,6 +354,9 @@ int main(int argc, char **argv) int silent = 0; int c; + set_progname(argv[0]); + setuplocale(); + const char *short_opts = "r:d:pshV"; const struct option long_opts[] = { { "rate", required_argument, NULL, 'r' }, @@ -349,9 +367,15 @@ int main(int argc, char **argv) { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; - - set_progname(argv[0]); - setuplocale(); + const struct kbd_help opthelp[] = { + { "-r, --rate=NUM", _("set the rate in characters per second.") }, + { "-d, --delay=NUM", _("set the amount of time the key must remain depressed before it will start to repeat.") }, + { "-p, --print", _("do not set new values, but only display the current ones.") }, + { "-s, --silent", _("suppress all normal output.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { @@ -371,9 +395,9 @@ int main(int argc, char **argv) print_version_and_exit(); break; case 'h': - usage(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); case '?': - usage(EX_USAGE); + usage(EX_USAGE, opthelp); } } diff --git a/src/libcommon/getfd.c b/src/libcommon/getfd.c index 65ef6a3f..b65d927a 100644 --- a/src/libcommon/getfd.c +++ b/src/libcommon/getfd.c @@ -67,7 +67,8 @@ getfd(const char *fnam) return fd; close(fd); } - fprintf(stderr, _("Couldn't open %s\n"), fnam); + fprintf(stderr, _("Couldn't open %s"), fnam); + fprintf(stderr, "\n"); exit(1); } @@ -84,7 +85,8 @@ getfd(const char *fnam) return fd; fprintf(stderr, - _("Couldn't get a file descriptor referring to the console\n")); + _("Couldn't get a file descriptor referring to the console")); + fprintf(stderr, "\n"); /* total failure */ exit(1); diff --git a/src/libcommon/libcommon.h b/src/libcommon/libcommon.h index 2250e571..56653582 100644 --- a/src/libcommon/libcommon.h +++ b/src/libcommon/libcommon.h @@ -34,6 +34,11 @@ textdomain(PACKAGE); \ } while (0) +struct kbd_help { + const char *opts; + const char *desc; +}; + // getfd.c int getfd(const char *fnam); diff --git a/src/libkeymap/ksyms.c b/src/libkeymap/ksyms.c index a632f0a3..46e30b78 100644 --- a/src/libkeymap/ksyms.c +++ b/src/libkeymap/ksyms.c @@ -337,36 +337,36 @@ int ksymtocode(struct lk_ctx *ctx, const char *s, int direction) for (i = 0; i < 256 - 160; i++) if (!strcmp(s, latin1_syms[i].name)) { - INFO(ctx, _("assuming iso-8859-1 %s"), s); + INFO(ctx, _("assuming %s %s"), "iso-8859-1", s); return K(KT_LATIN, 160 + i); } for (i = 0; i < 256 - 160; i++) if (!strcmp(s, iso8859_15_syms[i].name)) { - INFO(ctx, _("assuming iso-8859-15 %s"), s); + INFO(ctx, _("assuming %s %s"), "iso-8859-15", s); return K(KT_LATIN, 160 + i); } for (i = 0; i < 256 - 160; i++) if (!strcmp(s, latin2_syms[i].name)) { - INFO(ctx, _("assuming iso-8859-2 %s"), s); + INFO(ctx, _("assuming %s %s"), "iso-8859-2", s); return K(KT_LATIN, 160 + i); } for (i = 0; i < 256 - 160; i++) if (!strcmp(s, latin3_syms[i].name)) { - INFO(ctx, _("assuming iso-8859-3 %s"), s); + INFO(ctx, _("assuming %s %s"), "iso-8859-3", s); return K(KT_LATIN, 160 + i); } for (i = 0; i < 256 - 160; i++) if (!strcmp(s, latin4_syms[i].name)) { - INFO(ctx, _("assuming iso-8859-4 %s"), s); + INFO(ctx, _("assuming %s %s"), "iso-8859-4", s); return K(KT_LATIN, 160 + i); } } - ERR(ctx, _("unknown keysym '%s'\n"), s); + ERR(ctx, _("unknown keysym '%s'"), s); return CODE_FOR_UNKNOWN_KSYM; } diff --git a/src/libkeymap/loadkeys.c b/src/libkeymap/loadkeys.c index 829c5c1c..4e663921 100644 --- a/src/libkeymap/loadkeys.c +++ b/src/libkeymap/loadkeys.c @@ -235,7 +235,7 @@ defdiacs(struct lk_ctx *ctx, int fd) if (ptr->diacr > UCHAR_MAX || ptr->base > UCHAR_MAX || ptr->result > UCHAR_MAX) { - ERR(ctx, "unable to load compose definitions because some of them are too large"); + ERR(ctx, _("unable to load compose definitions because some of them are too large")); return -1; } diff --git a/src/loadkeys.c b/src/loadkeys.c index f119cfe3..82f1f055 100644 --- a/src/loadkeys.c +++ b/src/loadkeys.c @@ -16,6 +16,7 @@ #include <string.h> #include <getopt.h> #include <unistd.h> +#include <errno.h> #include <sysexits.h> #include <sys/ioctl.h> @@ -38,34 +39,62 @@ static const char *const suffixes[] = { }; static void __attribute__((noreturn)) -usage(int rc) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, _( - "loadkeys version %s\n" - "\n" - "Usage: %s [option...] [mapfile...]\n" - "\n" - "Options:\n" - " -a, --ascii force conversion to ASCII;\n" - " -b, --bkeymap output a binary keymap to stdout;\n" - " -c, --clearcompose clear kernel compose table;\n" - " -C, --console=file the console device to be used;\n" - " -d, --default load \"%s\";\n" - " -m, --mktable output a \"defkeymap.c\" to stdout;\n" - " -p, --parse search and parse keymap without action;\n" - " -s, --clearstrings clear kernel string table;\n" - " -u, --unicode force conversion to Unicode;\n" - " -q, --quiet suppress all normal output;\n" - " -v, --verbose explain what is being done;\n" - " -h, --help print this usage message;\n" - " -V, --version print version number.\n"), - PACKAGE_VERSION, get_progname(), DEFMAP); + const struct kbd_help *h; + + fprintf(stderr, _("Usage: %s [option...] [mapfile...]\n"), get_progname()); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Default keymap: %s\n"), DEFMAP); + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + exit(rc); } int main(int argc, char *argv[]) { - const char *const short_opts = "abcC:dhmpsuqvV"; + int options = 0; + + const char *const *dirpath; + const char *dirpath2[] = { NULL, NULL }; + + struct lk_ctx *ctx; + lk_flags flags = 0; + + int c, i, rc = -1; + int fd = -1; + int kbd_mode; + int kd_mode; + char *console = NULL; + char *ev; + struct kbdfile_ctx *fctx; + struct kbdfile *fp = NULL; + + set_progname(argv[0]); + setuplocale(); + + const char *const short_opts = "abcC:dhmpsuqvV"; const struct option long_opts[] = { { "console", required_argument, NULL, 'C' }, { "ascii", no_argument, NULL, 'a' }, @@ -82,6 +111,22 @@ int main(int argc, char *argv[]) { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; + const struct kbd_help opthelp[] = { + { "-C, --console=DEV", _("the console device to be used.") }, + { "-a, --ascii", _("force conversion to ASCII.") }, + { "-b, --bkeymap", _("output a binary keymap to stdout.") }, + { "-c, --clearcompose", _("clear kernel compose table.") }, + { "-d, --default", _("load default.") }, + { "-m, --mktable", _("output a 'defkeymap.c' to stdout.") }, + { "-p, --parse", _("search and parse keymap without action.") }, + { "-s, --clearstrings", _("clear kernel string table.") }, + { "-u, --unicode", _("force conversion to Unicode.") }, + { "-q, --quiet", _("suppress all normal output.") }, + { "-v, --verbose", _("be more verbose.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; enum options { OPT_A = (1 << 1), @@ -91,25 +136,6 @@ int main(int argc, char *argv[]) OPT_U = (1 << 5), OPT_P = (1 << 6) }; - int options = 0; - - const char *const *dirpath; - const char *dirpath2[] = { NULL, NULL }; - - struct lk_ctx *ctx; - lk_flags flags = 0; - - int c, i, rc = -1; - int fd = -1; - int kbd_mode; - int kd_mode; - char *console = NULL; - char *ev; - struct kbdfile_ctx *fctx; - struct kbdfile *fp = NULL; - - set_progname(argv[0]); - setuplocale(); ctx = lk_init(); if (!ctx) { @@ -163,10 +189,10 @@ int main(int argc, char *argv[]) print_version_and_exit(); break; case 'h': - usage(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); break; case '?': - usage(EX_USAGE); + usage(EX_USAGE, opthelp); break; } } @@ -185,11 +211,8 @@ int main(int argc, char *argv[]) /* check whether the keyboard is in Unicode mode */ if (ioctl(fd, KDGKBMODE, &kbd_mode) || - ioctl(fd, KDGETMODE, &kd_mode)) { - fprintf(stderr, _("%s: error reading keyboard mode: %m\n"), - get_progname()); - exit(EXIT_FAILURE); - } + ioctl(fd, KDGETMODE, &kd_mode)) + kbd_error(EXIT_FAILURE, errno, _("error reading keyboard mode")); if (kbd_mode == K_UNICODE) { if (options & OPT_A) { diff --git a/src/loadunimap.c b/src/loadunimap.c index f372c5f8..ad3bc122 100644 --- a/src/loadunimap.c +++ b/src/loadunimap.c @@ -8,6 +8,7 @@ #include <errno.h> #include <stdio.h> #include <stdlib.h> +#include <getopt.h> #include <sysexits.h> #include <string.h> #include <ctype.h> @@ -19,14 +20,37 @@ #include "libcommon.h" #include "kfont.h" -extern char *progname; - static void __attribute__((noreturn)) -usage(void) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, - _("Usage:\n\t%s [-C console] [-o map.orig]\n"), progname); - exit(1); + const struct kbd_help *h; + fprintf(stderr, _("Usage: %s [option...]\n"), get_progname()); + fprintf(stderr, "\n"); + fprintf(stderr, _("This utility reports or sets the keyboard mode.\n")); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + + exit(rc); } int main(int argc, char *argv[]) @@ -39,25 +63,46 @@ int main(int argc, char *argv[]) set_progname(argv[0]); setuplocale(); - if (argc == 2 && - (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) - print_version_and_exit(); + const char *short_opts = "o:C:hV"; + const struct option long_opts[] = { + { "output", required_argument, NULL, 'o' }, + { "console", required_argument, NULL, 'C' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + const struct kbd_help opthelp[] = { + { "-o, --output=FILE", _("save the old map to the FILE.") }, + { "-C, --console=DEV", _("the console device to be used.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; - while ((c = getopt(argc, argv, "C:o:")) != EOF) { + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { + case 'o': + outfnam = optarg; + break; case 'C': + if (optarg == NULL || optarg[0] == '\0') + usage(EX_USAGE, opthelp); console = optarg; break; - case 'o': - outfnam = optarg; + case 'V': + print_version_and_exit(); + break; + case 'h': + usage(EXIT_SUCCESS, opthelp); + break; + case '?': + usage(EX_USAGE, opthelp); break; - default: - usage(); } } if (argc > optind + 1 || (argc == optind && !outfnam)) - usage(); + usage(EX_USAGE, opthelp); if ((fd = getfd(console)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); diff --git a/src/mapscrn.c b/src/mapscrn.c index 3298152f..4fde680d 100644 --- a/src/mapscrn.c +++ b/src/mapscrn.c @@ -5,6 +5,7 @@ #include <stdio.h> #include <stdlib.h> +#include <getopt.h> #include <memory.h> #include <string.h> #include <fcntl.h> @@ -17,46 +18,103 @@ #include "libcommon.h" #include "kfont.h" +static void __attribute__((noreturn)) +usage(int rc, const struct kbd_help *options) +{ + const struct kbd_help *h; + fprintf(stderr, _("Usage: %s [option...] [map-file]\n"), get_progname()); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + + exit(rc); +} + int main(int argc, char *argv[]) { - int fd, ret; + int fd, c, ret; + char *console = NULL; + char *outfnam = NULL; struct kfont_context *kfont; set_progname(argv[0]); setuplocale(); + const char *short_opts = "o:C:hV"; + const struct option long_opts[] = { + { "output", required_argument, NULL, 'o' }, + { "console", required_argument, NULL, 'C' }, + { "verbose", no_argument, NULL, 'v' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + const struct kbd_help opthelp[] = { + { "-o, --output=FILE", _("save the old map to the FILE.") }, + { "-C, --console=DEV", _("the console device to be used.") }, + { "-v, --verbose", _("be more verbose.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; + if ((ret = kfont_init(get_progname(), &kfont)) < 0) return -ret; - if (argc == 2 && !strcmp(argv[1], "-V")) - print_version_and_exit(); - - if (argc > 1 && !strcmp(argv[1], "-v")) { - kfont_inc_verbosity(kfont); - argc--; - argv++; + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { + switch (c) { + case 'o': + outfnam = optarg; + break; + case 'C': + if (optarg == NULL || optarg[0] == '\0') + usage(EX_USAGE, opthelp); + console = optarg; + break; + case 'v': + kfont_inc_verbosity(kfont); + break; + case 'V': + print_version_and_exit(); + break; + case 'h': + usage(EXIT_SUCCESS, opthelp); + break; + case '?': + usage(EX_USAGE, opthelp); + break; + } } - if ((fd = getfd(NULL)) < 0) + if ((fd = getfd(console)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); - if (argc >= 3 && !strcmp(argv[1], "-o")) { - if ((ret = kfont_save_consolemap(kfont, fd, argv[2])) < 0) - return -ret; - - argc -= 2; - argv += 2; + if (outfnam && (ret = kfont_save_consolemap(kfont, fd, outfnam)) < 0) + return -ret; - if (argc == 1) - return EX_OK; - } + if (optind == argc) + return EX_OK; - if (argc != 2) { - fprintf(stderr, _("usage: %s [-V] [-v] [-o map.orig] map-file\n"), - get_progname()); - return EX_USAGE; - } - if ((ret = kfont_load_consolemap(kfont, fd, argv[1])) < 0) + if ((ret = kfont_load_consolemap(kfont, fd, argv[optind])) < 0) return -ret; return EX_OK; diff --git a/src/openvt.c b/src/openvt.c index 7519ceb0..c260bb28 100644 --- a/src/openvt.c +++ b/src/openvt.c @@ -44,28 +44,38 @@ #error vt device name must be defined #endif -static void - __attribute__((noreturn)) - print_help(int ret) +static void __attribute__((noreturn)) +usage(int rc, const struct kbd_help *options) { - printf(_("Usage: %s [OPTIONS] -- command\n" - "\n" - "This utility helps you to start a program on a new virtual terminal (VT).\n" - "\n" - "Options:\n" - " -c, --console=NUM use the given VT number;\n" - " -e, --exec execute the command, without forking;\n" - " -f, --force force opening a VT without checking;\n" - " -l, --login make the command a login shell;\n" - " -u, --user figure out the owner of the current VT;\n" - " -s, --switch switch to the new VT;\n" - " -w, --wait wait for command to complete;\n" - " -v, --verbose explain what is being done;\n" - " -h, --help print this usage message;\n" - " -V, --version print version number.\n" - "\n"), - get_progname()); - exit(ret); + const struct kbd_help *h; + + fprintf(stderr, _("Usage: %s [option...] -- command\n"), get_progname()); + fprintf(stderr, "\n"); + fprintf(stderr, _("This utility helps you to start a program on a new virtual terminal (VT).\n")); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + + exit(rc); } /* @@ -252,6 +262,9 @@ int main(int argc, char *argv[]) char vtname[PATH_MAX+1]; char *cmd = NULL, *def_cmd = NULL, *username = NULL; + set_progname(argv[0]); + setuplocale(); + struct option long_options[] = { { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'V' }, @@ -266,8 +279,19 @@ int main(int argc, char *argv[]) { 0, 0, 0, 0 } }; - set_progname(argv[0]); - setuplocale(); + const struct kbd_help opthelp[] = { + { "-C, --console=DEV", _("the console device to be used.") }, + { "-e, --exec", _("execute the command, without forking.") }, + { "-f, --force", _("force opening a VT without checking.") }, + { "-l, --login", _("make the command a login shell.") }, + { "-u, --user", _("figure out the owner of the current VT.") }, + { "-s, --switch", _("switch to the new VT.") }, + { "-w, --wait", _("wait for command to complete") }, + { "-v, --verbose", _("be more verbose.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; while ((opt = getopt_long(argc, argv, "c:lsfuewhvV", long_options, NULL)) != -1) { switch (opt) { @@ -312,7 +336,7 @@ int main(int argc, char *argv[]) break; default: case 'h': - print_help(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); break; } } diff --git a/src/psfxtable.c b/src/psfxtable.c index 476e719a..d8dff10e 100644 --- a/src/psfxtable.c +++ b/src/psfxtable.c @@ -63,7 +63,7 @@ int main(int argc, char **argv) if (!strcmp(get_progname(), "psfaddtable")) { /* Do not send binary data to stdout without explicit "-" */ if (argc != 4) { - const char *u = _("Usage:\n\t%s infont intable outfont\n"); + const char *u = _("Usage: %s infont intable outfont\n"); fprintf(stderr, u, get_progname()); return EX_USAGE; } @@ -72,7 +72,7 @@ int main(int argc, char **argv) ofname = argv[3]; } else if (!strcmp(get_progname(), "psfgettable")) { if (argc < 2 || argc > 3) { - const char *u = _("Usage:\n\t%s infont [outtable]\n"); + const char *u = _("Usage: %s infont [outtable]\n"); fprintf(stderr, u, get_progname()); return EX_USAGE; } @@ -81,7 +81,7 @@ int main(int argc, char **argv) } else if (!strcmp(get_progname(), "psfstriptable")) { /* Do not send binary data to stdout without explicit "-" */ if (argc != 3) { - const char *u = _("Usage:\n\t%s infont outfont\n"); + const char *u = _("Usage: %s infont outfont\n"); fprintf(stderr, u, get_progname()); return EX_USAGE; } @@ -104,7 +104,7 @@ int main(int argc, char **argv) break; } if (i < argc || argc <= 1) { - const char *u = _("Usage:\n\t%s [-i infont] [-o outfont] " + const char *u = _("Usage: %s [-i infont] [-o outfont] " "[-it intable] [-ot outtable] [-nt]\n"); fprintf(stderr, u, get_progname()); return EX_USAGE; @@ -117,14 +117,14 @@ int main(int argc, char **argv) if (!strcmp(ifname, "-")) ifil = stdin; else if (!(ifil = fopen(ifname, "r"))) - kbd_error(EX_NOINPUT, errno, "Unable to open: %s", ifname); + kbd_error(EX_NOINPUT, errno, _("Unable to open: %s"), ifname); if (!itname) /* nothing */; else if (!strcmp(itname, "-")) itab = stdin; else if (!(itab = fopen(itname, "r"))) - kbd_error(EX_NOINPUT, errno, "Unable to open: %s", itname); + kbd_error(EX_NOINPUT, errno, _("Unable to open: %s"), itname); /* Refuse ifil == itab == stdin ? Perhaps not. */ @@ -133,14 +133,14 @@ int main(int argc, char **argv) else if (!strcmp(ofname, "-")) ofil = stdout; else if (!(ofil = fopen(ofname, "w"))) - kbd_error(EX_CANTCREAT, errno, "Unable to open: %s", ofname); + kbd_error(EX_CANTCREAT, errno, _("Unable to open: %s"), ofname); if (!otname) /* nothing */; else if (!strcmp(otname, "-")) otab = stdout; else if (!(otab = fopen(otname, "w"))) - kbd_error(EX_CANTCREAT, errno, "Unable to open: %s", otname); + kbd_error(EX_CANTCREAT, errno, _("Unable to open: %s"), otname); if (kfont_read_psffont(kfont, ifil, &inbuf, &inbuflth, &fontbuf, &fontbuflth, &width, &fontlen, 0, diff --git a/src/screendump.c b/src/screendump.c index 227cf055..8e24b801 100644 --- a/src/screendump.c +++ b/src/screendump.c @@ -133,7 +133,7 @@ try_ioctl : { /* we tried this just to be sure, but TIOCLINUX function 0 has been disabled since 1.1.92 Do not mention `ioctl dump' in error msg */ - kbd_warning(0, _("couldn't read %s\n"), infile); + kbd_warning(0, _("couldn't read %s"), infile); #endif return EXIT_FAILURE; } @@ -142,7 +142,7 @@ try_ioctl : { cols = screenbuf[1]; if (rows != win.ws_row || cols != win.ws_col) { kbd_error(EXIT_FAILURE, 0, - _("Strange ... screen is both %dx%d and %dx%d ??\n"), + _("Strange ... screen is both %dx%d and %dx%d ?"), win.ws_col, win.ws_row, cols, rows); } diff --git a/src/setkeycodes.c b/src/setkeycodes.c index 0b52b2b3..74c9caf5 100644 --- a/src/setkeycodes.c +++ b/src/setkeycodes.c @@ -10,6 +10,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <getopt.h> #include <errno.h> #include <fcntl.h> #include <limits.h> @@ -20,17 +21,41 @@ #include "libcommon.h" static void __attribute__((noreturn)) -usage(const char *s) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, "setkeycode: %s\n", s); - fprintf(stderr, _( - "usage: setkeycode scancode keycode ...\n" - " (where scancode is either xx or e0xx, given in hexadecimal,\n" - " and keycode is given in decimal)\n")); - exit(EX_USAGE); + const struct kbd_help *h; + + fprintf(stderr, _("Usage: %s [option...] scancode keycode ...\n"), get_progname()); + fprintf(stderr, "\n"); + fprintf(stderr, _("(where scancode is either xx or e0xx, given in hexadecimal,\n" + "and keycode is given in decimal)\n")); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + + exit(rc); } -static void +static int str_to_uint(const char *str, int base, unsigned int *res) { char *ep; @@ -39,41 +64,86 @@ str_to_uint(const char *str, int base, unsigned int *res) errno = 0; v = strtol(str, &ep, base); - if (*ep) - usage(_("error reading scancode")); + if (*ep) { + kbd_warning(0, _("error reading scancode")); + return -1; + } - if (errno == ERANGE) - kbd_error(EX_DATAERR, 0, "Argument out of range: %s", str); + if (errno == ERANGE) { + kbd_warning(0, _("Argument out of range: %s"), str); + return -1; + } - if (v < 0) - kbd_error(EX_DATAERR, 0, "Argument must be positive: %s", str); + if (v < 0) { + kbd_warning(0, _("Argument must be positive: %s"), str); + return -1; + } - if (v > UINT_MAX) - kbd_error(EX_DATAERR, 0, "Argument is too big: %s", str); + if (v > UINT_MAX) { + kbd_warning(0, "Argument is too big: %s", str); + return -1; + } *res = (unsigned int) v; + return 0; } int main(int argc, char **argv) { - int fd; + int fd, c; struct kbkeycode a; + char *console = NULL; set_progname(argv[0]); setuplocale(); - if (argc == 2 && !strcmp(argv[1], "-V")) - print_version_and_exit(); + const char *short_opts = "C:hV"; + const struct option long_opts[] = { + { "console", required_argument, NULL, 'C' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + const struct kbd_help opthelp[] = { + { "-C, --console=DEV", _("the console device to be used.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; + + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { + switch (c) { + case 'C': + if (optarg == NULL || optarg[0] == '\0') + usage(EX_USAGE, opthelp); + console = optarg; + break; + case 'V': + print_version_and_exit(); + break; + case 'h': + usage(EXIT_SUCCESS, opthelp); + break; + case '?': + usage(EX_USAGE, opthelp); + break; + } + } - if (argc % 2 != 1) - usage(_("even number of arguments expected")); + if (optind == argc) { + kbd_warning(0, _("Not enough arguments.")); + usage(EX_USAGE, opthelp); + } - if ((fd = getfd(NULL)) < 0) + if ((fd = getfd(console)) < 0) kbd_error(EX_OSERR, 0, _("Couldn't get a file descriptor referring to the console")); while (argc > 2) { - str_to_uint(argv[1], 16, &a.scancode); - str_to_uint(argv[2], 0, &a.keycode); + if (str_to_uint(argv[1], 16, &a.scancode) < 0) + return EX_DATAERR; + + if (str_to_uint(argv[2], 0, &a.keycode) < 0) + return EX_DATAERR; if (a.scancode >= 0xe000) { a.scancode -= 0xe000; diff --git a/src/setleds.c b/src/setleds.c index 6f07f9de..c29ef5e9 100644 --- a/src/setleds.c +++ b/src/setleds.c @@ -138,10 +138,10 @@ sungetleds(arg_state unsigned char *cur_leds) #ifdef KIOCGLED if (ioctl(sunkbdfd, KIOCGLED, cur_leds)) { kbd_error(EXIT_FAILURE, errno, _("Error reading current led setting from /dev/kbd: " - "ioctl KIOCGLED")); + "ioctl %s"), "KIOCGLED"); } #else - kbd_error(EXIT_FAILURE, 0, _("KIOCGLED unavailable?\n")); + kbd_error(EXIT_FAILURE, 0, _("ioctl %s unavailable?\n"), "KIOCGLED"); #endif } @@ -157,10 +157,10 @@ sunsetleds(arg_state unsigned char *cur_leds) #ifdef KIOCSLED if (ioctl(sunkbdfd, KIOCSLED, cur_leds)) { kbd_error(EXIT_FAILURE, errno, _("Error reading current led setting from /dev/kbd: " - "ioctl KIOCSLED")); + "ioctl %s"), "KIOCSLED"); } #else - kbd_error(EXIT_FAILURE, 0, _("KIOCSLED unavailable?\n")); + kbd_error(EXIT_FAILURE, 0, _("ioctl %s unavailable?\n"), "KIOCSLED"); #endif } diff --git a/src/setmetamode.c b/src/setmetamode.c index 8e690ec9..9ef2acf9 100644 --- a/src/setmetamode.c +++ b/src/setmetamode.c @@ -20,26 +20,42 @@ #include "libcommon.h" static void __attribute__((noreturn)) -usage(int rc) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, _("Usage: %1$s [option...] [argument]\n" - "\n" - "Each vt has his own copy of this bit. Use\n" - " %1$s [argument] < /dev/ttyn\n" - "to change the settings of another vt.\n" - "The setting before and after the change are reported.\n" - "\n" - "Arguments:\n" - " metabit the keysym marked with the high bit set.\n" - " escprefix specifies if pressing the meta (alt) key\n" - " generates an ESC (\\033) prefix followed by\n" - " the keysym.\n" - "\n" - "Options:\n" - " -C, --console=DEV the console device to be used;\n" - " -h, --help print this usage message;\n" - " -V, --version print version number\n" - "\n"), get_progname()); + const struct kbd_help *h; + + fprintf(stderr, _("Usage: %s [option...] [argument]\n"), get_progname()); + fprintf(stderr, "\n"); + fprintf(stderr, _( + "Arguments:\n" + " metabit the keysym marked with the high bit set.\n" + " escprefix specifies if pressing the meta (alt) key\n" + " generates an ESC (\\033) prefix followed by\n" + " the keysym.\n" + )); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + exit(rc); } @@ -50,15 +66,15 @@ report(unsigned int meta) switch (meta) { case K_METABIT: - s = _("Meta key sets high order bit\n"); + s = _("Meta key sets high order bit"); break; case K_ESCPREFIX: - s = _("Meta key gives Esc prefix\n"); + s = _("Meta key gives Esc prefix"); break; default: - s = _("Strange mode for Meta key?\n"); + s = _("Strange mode for Meta key?"); } - printf("%s", s); + printf("%s\n", s); } struct meta { @@ -83,6 +99,9 @@ int main(int argc, char **argv) int fd = 0; char *console = NULL; + set_progname(argv[0]); + setuplocale(); + const char *short_opts = "C:hV"; const struct option long_opts[] = { { "console", required_argument, NULL, 'C' }, @@ -90,25 +109,28 @@ int main(int argc, char **argv) { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; - - set_progname(argv[0]); - setuplocale(); + const struct kbd_help opthelp[] = { + { "-C, --console=DEV", _("the console device to be used.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { case 'C': if (optarg == NULL || optarg[0] == '\0') - usage(EX_USAGE); + usage(EX_USAGE, opthelp); console = optarg; break; case 'V': print_version_and_exit(); break; case 'h': - usage(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); break; case '?': - usage(EX_USAGE); + usage(EX_USAGE, opthelp); break; } } @@ -130,13 +152,13 @@ int main(int argc, char **argv) for (mp = metas; (unsigned)(mp - metas) < SIZE(metas); mp++) { if (!strcmp(argv[1], mp->name)) { nmeta = mp->val; - goto fnd; + goto end; } } fprintf(stderr, _("unrecognized argument: _%s_\n\n"), argv[1]); - usage(EXIT_FAILURE); + usage(EXIT_FAILURE, opthelp); -fnd: +end: printf(_("old state: ")); report(ometa); if (ioctl(fd, KDSKBMETA, nmeta)) { diff --git a/src/setvtrgb.c b/src/setvtrgb.c index 6f0f9dd0..b3ceda0e 100644 --- a/src/setvtrgb.c +++ b/src/setvtrgb.c @@ -37,27 +37,45 @@ unsigned char vga_colors[] = { }; static void __attribute__((noreturn)) -usage(int code) +usage(int rc, const struct kbd_help *options) { - const char *progname = get_progname(); - fprintf(stderr, - _("Usage: %s [options] [vga|FILE|-]\n" - "\n" - "If you use the FILE parameter, FILE should be exactly 3 lines of\n" - "comma-separated decimal values for RED, GREEN, and BLUE.\n" - "\n" - "To seed a valid FILE:\n" - " cat /sys/module/vt/parameters/default_{red,grn,blu} > FILE\n" - "\n" - "and then edit the values in FILE.\n" - "\n" - "Options:\n" - " -C, --console=DEV the console device to be used;\n" - " -h, --help print this usage message;\n" - " -V, --version print version number.\n" - "\n"), - progname); - exit(code); + const struct kbd_help *h; + + fprintf(stderr, _("Usage: %s [option...] [vga|FILE|-]\n"), get_progname()); + fprintf(stderr, "\n"); + fprintf(stderr, _( + "If you use the FILE parameter, FILE should be exactly 3 lines of\n" + "comma-separated decimal values for RED, GREEN, and BLUE.\n" + "\n" + "To seed a valid FILE:\n" + " cat /sys/module/vt/parameters/default_{red,grn,blu} > FILE\n" + "\n" + "and then edit the values in FILE.\n" + )); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + + exit(rc); } static void @@ -101,6 +119,9 @@ int main(int argc, char **argv) FILE *f; const char *console = NULL; + set_progname(argv[0]); + setuplocale(); + const char *short_opts = "C:hV"; const struct option long_opts[] = { { "console", required_argument, NULL, 'C' }, @@ -108,31 +129,34 @@ int main(int argc, char **argv) { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; - - set_progname(argv[0]); - setuplocale(); + const struct kbd_help opthelp[] = { + { "-C, --console=DEV", _("the console device to be used.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { case 'C': if (optarg == NULL || optarg[0] == '\0') - usage(EX_USAGE); + usage(EX_USAGE, opthelp); console = optarg; break; case 'V': print_version_and_exit(); break; case 'h': - usage(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); break; case '?': - usage(EX_USAGE); + usage(EX_USAGE, opthelp); break; } } if (optind == argc) - usage(EX_USAGE); + usage(EX_USAGE, opthelp); file = argv[optind]; diff --git a/src/showconsolefont.c b/src/showconsolefont.c index 26a5b9af..9c7a637c 100644 --- a/src/showconsolefont.c +++ b/src/showconsolefont.c @@ -6,6 +6,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <getopt.h> #include <errno.h> #include <unistd.h> #include <sysexits.h> @@ -95,21 +96,36 @@ setnewunicodemap(struct kfont_context *ctx, unsigned int *list, int cnt) } static void __attribute__((noreturn)) -usage(void) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, - _("usage: showconsolefont -V|--version\n" - " showconsolefont [-C tty] [-v] [-i]\n" - "(probably after loading a font with `setfont font')\n" - "\n" - "Options:\n" - " -C tty device to read the font from. Default: current tty;\n" - " -v be more verbose;\n" - " -i don't print out the font table, just show;\n" - " ROWSxCOLSxCOUNT and exit;\n" - " -V, --version print version number.\n" - )); - exit(EX_USAGE); + const struct kbd_help *h; + + fprintf(stderr, _("Usage: %s [option...]\n"), get_progname()); + fprintf(stderr, _("(probably after loading a font with `setfont font')\n")); + + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + + exit(rc); } int main(int argc, char **argv) @@ -125,33 +141,54 @@ int main(int argc, char **argv) set_progname(argv[0]); setuplocale(); + const char *const short_opts = "C:ivVh"; + const struct option long_opts[] = { + { "console", required_argument, NULL, 'C' }, + { "info", no_argument, NULL, 'i' }, + { "verbose", no_argument, NULL, 'v' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + const struct kbd_help opthelp[] = { + { "-C, --console=DEV", _("the console device to be used.") }, + { "-i, --info", _("don't print out the font table, just show: ROWSxCOLSxCOUNT and exit.") }, + { "-v, --verbose", _("be more verbose.") }, + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; + struct kfont_context *kfont; if ((ret = kfont_init(get_progname(), &kfont)) < 0) return -ret; - if (argc == 2 && - (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) - print_version_and_exit(); - - while ((c = getopt(argc, argv, "ivC:")) != EOF) { + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { + case 'C': + console = optarg; + break; case 'i': info = 1; break; case 'v': kfont_inc_verbosity(kfont); break; - case 'C': - console = optarg; + case 'V': + print_version_and_exit(); + break; + case 'h': + usage(EXIT_SUCCESS, opthelp); + break; + case '?': + usage(EX_USAGE, opthelp); break; - default: - usage(); } } if (optind != argc) - usage(); + usage(EX_USAGE, opthelp); if ((fd = getfd(console)) < 0) kbd_error(EX_OSERR, 0, _("Couldn't get a file descriptor referring to the console")); diff --git a/src/showkey.c b/src/showkey.c index 27f56268..7ab44f8b 100644 --- a/src/showkey.c +++ b/src/showkey.c @@ -7,6 +7,7 @@ #include <getopt.h> #include <fcntl.h> #include <signal.h> +#include <string.h> #include <termios.h> #include <sysexits.h> #include <sys/ioctl.h> @@ -85,19 +86,32 @@ watch_dog(int x __attribute__((unused))) } static void __attribute__((noreturn)) -usage(int rc) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, _( - "showkey version %s\n\n" - "usage: showkey [options...]\n" - "\n" - "Options:\n" - " -a, --ascii display the decimal/octal/hex values of the keys;\n" - " -s, --scancodes display only the raw scan-codes;\n" - " -k, --keycodes display only the interpreted keycodes (default);\n" - " -h, --help print this usage message;\n" - " -V, --version print version number.\n"), - PACKAGE_VERSION); + const struct kbd_help *h; + fprintf(stderr, _("Usage: %s [option...]\n"), get_progname()); + if (options) { + int max = 0; + + fprintf(stderr, "\n"); + fprintf(stderr, _("Options:")); + fprintf(stderr, "\n"); + + for (h = options; h && h->opts; h++) { + int len = (int) strlen(h->opts); + if (max < len) + max = len; + } + max += 2; + + for (h = options; h && h->opts; h++) + fprintf(stderr, " %-*s %s\n", max, h->opts, h->desc); + } + + fprintf(stderr, "\n"); + fprintf(stderr, _("Report bugs to authors.\n")); + fprintf(stderr, "\n"); + exit(rc); } @@ -124,6 +138,15 @@ int main(int argc, char *argv[]) set_progname(argv[0]); setuplocale(); + const struct kbd_help opthelp[] = { + { "-a, --ascii", _("display the decimal/octal/hex values of the keys.") }, + { "-s, --scancodes", _("display only the raw scan-codes.") }, + { "-k, --keycodes", _("display only the interpreted keycodes (default).") }, + { "-h, --help", _("print this usage message.") }, + { "-V, --version", _("print version number.") }, + { NULL, NULL } + }; + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { case 's': @@ -139,16 +162,16 @@ int main(int argc, char *argv[]) print_version_and_exit(); break; case 'h': - usage(EXIT_SUCCESS); + usage(EXIT_SUCCESS, opthelp); break; case '?': - usage(EX_USAGE); + usage(EX_USAGE, opthelp); break; } } if (optind < argc) - usage(EX_USAGE); + usage(EX_USAGE, opthelp); if (print_ascii) { /* no mode and signal and timer stuff - just read stdin */ diff --git a/src/totextmode.c b/src/totextmode.c index b5035c9b..520a4a65 100644 --- a/src/totextmode.c +++ b/src/totextmode.c @@ -25,7 +25,7 @@ int main(int argc, char *argv[]) print_version_and_exit(); if (argc != 2) { - kbd_error(EXIT_FAILURE, 0, _("usage: totextmode\n")); + kbd_error(EXIT_FAILURE, 0, _("Usage: %s [option...]\n"), get_progname()); } if ((fd = getfd(NULL)) < 0) |