From 02c05a86636172f9c6750ec54eb05231ed199e30 Mon Sep 17 00:00:00 2001 From: Alexey Gladkov Date: Wed, 23 Oct 2024 15:36:35 +0200 Subject: [PATCH] Add help message Many utilities have options and arguments, but no help message. Adding help message and standard options makes the utilities more user friendly. Signed-off-by: Alexey Gladkov --- src/Makefile.am | 1 + src/clrunimap.c | 54 +++++++++++++++++++++++++-- src/outpsfheader.c | 90 +++++++++++++++++++++++++++++++-------------- src/readpsfheader.c | 45 ++++++++++++++++++++--- src/screendump.c | 63 ++++++++++++++++++++++++++----- src/setlogcons.c | 77 ++++++++++++++++++++++++++++---------- src/setmetamode.c | 4 +- src/setpalette.c | 75 +++++++++++++++++++++++++++++-------- 8 files changed, 327 insertions(+), 82 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 54c67220..8635299d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -61,6 +61,7 @@ setvtrgb_LDADD = $(COMMON_LIBS) showkey_LDADD = $(COMMON_LIBS) spawn_console_LDADD = $(COMMON_LIBS) spawn_login_LDADD = $(COMMON_LIBS) +outpsfheader_LDADD = $(COMMON_LIBS) resizecons_LDADD = libkbdfile/libkbdfile.la $(COMMON_LIBS) diff --git a/src/clrunimap.c b/src/clrunimap.c index 3e5d4da8..98a8d46e 100644 --- a/src/clrunimap.c +++ b/src/clrunimap.c @@ -9,21 +9,64 @@ #include #include #include +#include +#include #include #include #include "libcommon.h" +static void KBD_ATTR_NORETURN +usage(int rc, const struct kbd_help *options) +{ + fprintf(stderr, "Usage: %s [option...]\n", program_invocation_short_name); + + print_options(options); + print_report_bugs(); + + exit(rc); +} + int main(int argc, char *argv[]) { - int fd; + int c, fd; char *console = NULL; + 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 } + }; + setuplocale(); - if (argc >= 3 && !strcmp(argv[1], "-C")) - console = argv[2]; + 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 ((fd = getfd(console)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console.")); @@ -34,5 +77,8 @@ int main(int argc, char *argv[]) if ((ret = kfont_init(program_invocation_short_name, &ctx)) < 0) return -ret; - return kfont_put_unicodemap(ctx, fd, NULL, NULL); + if (kfont_put_unicodemap(ctx, fd, NULL, NULL) < 0) + return EXIT_FAILURE; + + return EXIT_SUCCESS; } diff --git a/src/outpsfheader.c b/src/outpsfheader.c index 38a9c50d..a9d2a812 100644 --- a/src/outpsfheader.c +++ b/src/outpsfheader.c @@ -5,51 +5,85 @@ #include #include /* exit */ #include +#include +#include #include #include "libcommon.h" static void KBD_ATTR_NORETURN -usage(void) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, "call: outpsfheader psftype fontsize charsize hastable\n"); - exit(1); + fprintf(stderr, "Usage: %s [option...] \n", + program_invocation_short_name); + + print_options(options); + print_report_bugs(); + + exit(rc); } int main(int argc, char **argv) { - int psftype, hastable; + int c, psftype, hastable; unsigned int fontsize, charsize; - if (argc != 5) - usage(); - - psftype = atoi(argv[1]); - fontsize = (unsigned int) atoi(argv[2]); - charsize = (unsigned int) atoi(argv[3]); - hastable = atoi(argv[4]); + const char *short_opts = "hV"; + const struct option long_opts[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + const struct kbd_help opthelp[] = { + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; - if (charsize > UCHAR_MAX) { - fprintf(stderr, "charsize is too large\n"); - exit(1); + 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 ((argc - optind) != 4) + usage(EX_USAGE, opthelp); + + psftype = atoi(argv[optind++]); + fontsize = (unsigned int) atoi(argv[optind++]); + charsize = (unsigned int) atoi(argv[optind++]); + hastable = atoi(argv[optind++]); + + if (charsize > UCHAR_MAX) + kbd_error(EXIT_FAILURE, 0, "charsize is too large"); + if (psftype == 1) { struct psf1_header h1; - if (fontsize != 256 && fontsize != 512) - usage(); + if (fontsize != 256 && fontsize != 512) { + kbd_warning(0, "fontsize can be 256 or 512"); + usage(EX_USAGE, opthelp); + } + h1.magic[0] = PSF1_MAGIC0; h1.magic[1] = PSF1_MAGIC1; h1.mode = (fontsize == 256) ? 0 : PSF1_MODE512; if (hastable) h1.mode |= PSF1_MODEHASTAB; h1.charsize = (unsigned char) charsize; - if (fwrite(&h1, sizeof(h1), 1, stdout) != 1) { - fprintf(stderr, "write error\n"); - exit(1); - } + + if (fwrite(&h1, sizeof(h1), 1, stdout) != 1) + kbd_error(EXIT_FAILURE, errno, "fwrite"); + } else if (psftype == 2) { struct psf2_header h2; @@ -64,11 +98,13 @@ int main(int argc, char **argv) h2.charsize = charsize; h2.width = 8; h2.height = charsize; - if (fwrite(&h2, sizeof(h2), 1, stdout) != 1) { - fprintf(stderr, "write error\n"); - exit(1); - } - } else - usage(); - return 0; + + if (fwrite(&h2, sizeof(h2), 1, stdout) != 1) + kbd_error(EXIT_FAILURE, errno, "fwrite"); + + } else { + usage(EX_USAGE, opthelp); + } + + return EXIT_SUCCESS; } diff --git a/src/readpsfheader.c b/src/readpsfheader.c index 9a159bc1..2d87e591 100644 --- a/src/readpsfheader.c +++ b/src/readpsfheader.c @@ -3,32 +3,65 @@ #include #include #include /* exit */ +#include #include #include "libcommon.h" static void KBD_ATTR_NORETURN -usage(void) +usage(int rc, const struct kbd_help *options) { - fprintf(stderr, "usage: readpsfheader font.psf\n"); - exit(EX_USAGE); + fprintf(stderr, "Usage: %s [option...] \n", + program_invocation_short_name); + + print_options(options); + print_report_bugs(); + + exit(rc); } int main(int argc, char **argv) { + int c; int psftype, fontlen, charsize, hastable, notable; int width = 8, bytewidth, height; char *inbuf, *fontbuf; int inbuflth, fontbuflth; struct unicode_list *uclistheads = NULL; + const char *short_opts = "hV"; + const struct option long_opts[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + const struct kbd_help opthelp[] = { + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; + setuplocale(); - if (argc != 2) - usage(); + 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 (optind == argc) + usage(EX_USAGE, opthelp); - FILE *f = fopen(argv[1], "rb"); + FILE *f = fopen(argv[optind], "rb"); if (!f) { perror("fopen"); return EX_NOINPUT; diff --git a/src/screendump.c b/src/screendump.c index 02340111..a7b08337 100644 --- a/src/screendump.c +++ b/src/screendump.c @@ -18,37 +18,82 @@ */ #include "config.h" +#include #include #include #include #include #include #include -#include +#include +#include +#include #include "libcommon.h" +static void KBD_ATTR_NORETURN +usage(int rc, const struct kbd_help *options) +{ + fprintf(stderr, "Usage: %s [option...] [N]\n", + program_invocation_short_name); + + print_options(options); + print_report_bugs(); + + exit(rc); +} + int main(int argc, char **argv) { int cons = 0; char infile[20]; unsigned char header[4]; unsigned int rows, cols; - int fd; + int c, fd; unsigned int i, j; char *inbuf, *outbuf, *p, *q; - setuplocale(); + const char *short_opts = "hV"; + const struct option long_opts[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + const struct kbd_help opthelp[] = { + { "-V, --version", _("print version number.") }, + { "-h, --help", _("print this usage message.") }, + { NULL, NULL } + }; - if (argc == 2 && !strcmp(argv[1], "-V")) - print_version_and_exit(); + setuplocale(); - if (argc > 2) { - fprintf(stderr, _("usage: screendump [n]\n")); - exit(EXIT_FAILURE); + 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; + } } - cons = (argc == 2) ? atoi(argv[1]) : 0; + cons = 0; + + if (optind < argc) { + cons = atoi(argv[optind]); + + if (cons < 0) + kbd_error(EX_DATAERR, 0, + "Argument must be positive: %s", argv[optind]); + + if (cons > CHAR_MAX) + kbd_error(EX_DATAERR, 0, + "Argument is too big: %s", argv[optind]); + } sprintf(infile, "/dev/vcsa%d", cons); fd = open(infile, O_RDONLY); diff --git a/src/setlogcons.c b/src/setlogcons.c index 464f8f81..c202b874 100644 --- a/src/setlogcons.c +++ b/src/setlogcons.c @@ -18,42 +18,81 @@ #include #include #include +#include #include "libcommon.h" +static void KBD_ATTR_NORETURN +usage(int rc, const struct kbd_help *options) +{ + fprintf(stderr, "Usage: %s [option...] [N]\n", program_invocation_short_name); + fprintf(stderr, "\n"); + fprintf(stderr, "Send kernel messages to the current console or to console N\n"); + + print_options(options); + print_report_bugs(); + + exit(rc); +} + int main(int argc, char **argv) { - int fd, cons; + int c, fd, cons; struct { char fn, subarg; } arg; + char *console = NULL; setuplocale(); - cons = 0; /* current console */ - - if (argc == 2) { - if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { - fprintf(stderr, "Usage: %s [N]\n", program_invocation_short_name); - fprintf(stderr, "Send kernel messages to the current console or to console N\n"); - return EX_USAGE; + 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 (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) - print_version_and_exit(); + cons = 0; /* current console */ - cons = atoi(argv[1]); - } + if (optind < argc) { + cons = atoi(argv[optind]); - if (cons < 0) - kbd_error(EX_DATAERR, 0, "Argument must be positive: %s", - (argc == 2) ? argv[1] : "0"); + if (cons < 0) + kbd_error(EX_DATAERR, 0, + "Argument must be positive: %s", argv[optind]); - if (cons > CHAR_MAX) - kbd_error(EX_DATAERR, 0, "Argument is too big: %s", - (argc == 2) ? argv[1] : "0"); + if (cons > CHAR_MAX) + kbd_error(EX_DATAERR, 0, + "Argument is too big: %s", argv[optind]); + } - 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.")); arg.fn = TIOCL_SETKMSGREDIRECT; /* redirect kernel messages */ diff --git a/src/setmetamode.c b/src/setmetamode.c index 57d83edc..ed74b1af 100644 --- a/src/setmetamode.c +++ b/src/setmetamode.c @@ -126,12 +126,12 @@ int main(int argc, char **argv) nmeta = 0; /* make gcc happy */ for (mp = metas; (unsigned)(mp - metas) < SIZE(metas); mp++) { - if (!strcmp(argv[1], mp->name)) { + if (!strcmp(argv[optind], mp->name)) { nmeta = mp->val; goto end; } } - fprintf(stderr, _("Unrecognized argument: %s"), argv[1]); + fprintf(stderr, _("Unrecognized argument: %s"), argv[optind]); fprintf(stderr, "\n\n"); usage(EXIT_FAILURE, opthelp); diff --git a/src/setpalette.c b/src/setpalette.c index 040c5cfe..912225e5 100644 --- a/src/setpalette.c +++ b/src/setpalette.c @@ -1,37 +1,82 @@ #include "config.h" +#include +#include + #include #include +#include +#include #include -#include -#include #include "libcommon.h" +static void KBD_ATTR_NORETURN +usage(int rc, const struct kbd_help *options) +{ + fprintf(stderr, "Usage: %s [option...] \n", + program_invocation_short_name); + + print_options(options); + print_report_bugs(); + + exit(rc); +} + int main(int argc, char **argv) { - int fd, indx, red, green, blue; + int c, fd, indx, red, green, blue; unsigned char cmap[48]; + char *console = NULL; + + 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 } + }; setuplocale(); - if (argc != 5) { - fprintf(stderr, "usage: %s index red green blue\n", program_invocation_short_name); - exit(EXIT_FAILURE); + 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; + } } - indx = atoi(argv[1]); - red = atoi(argv[2]); - green = atoi(argv[3]); - blue = atoi(argv[4]); + if ((argc - optind) != 4) + usage(EX_USAGE, opthelp); + + indx = atoi(argv[optind++]); + red = atoi(argv[optind++]); + green = atoi(argv[optind++]); + blue = atoi(argv[optind++]); if (indx < 0 || red < 0 || green < 0 || blue < 0 || - indx > 15 || red > 255 || green > 255 || blue > 255) { - fprintf(stderr, "indx must be in 0..15, colors in 0..255\n"); - exit(EXIT_FAILURE); - } + indx > 15 || red > 255 || green > 255 || blue > 255) + kbd_error(EXIT_FAILURE, 0, "indx must be in 0..15, colors in 0..255"); - 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 (ioctl(fd, GIO_CMAP, cmap))