Skip to content

Commit

Permalink
Add help message
Browse files Browse the repository at this point in the history
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 <legion@kernel.org>
  • Loading branch information
legionus committed Oct 24, 2024
1 parent bd073cc commit 02c05a8
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
54 changes: 50 additions & 4 deletions src/clrunimap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,64 @@
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <getopt.h>
#include <sysexits.h>
#include <linux/kd.h>

#include <kfont.h>

#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."));
Expand All @@ -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;
}
90 changes: 63 additions & 27 deletions src/outpsfheader.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,85 @@
#include <stdio.h>
#include <stdlib.h> /* exit */
#include <limits.h>
#include <getopt.h>
#include <sysexits.h>

#include <kfont.h>

#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...] <psftype> <fontsize> <charsize> <hastable>\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;

Expand All @@ -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;
}
45 changes: 39 additions & 6 deletions src/readpsfheader.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,65 @@
#include <sysexits.h>
#include <stdio.h>
#include <stdlib.h> /* exit */
#include <getopt.h>

#include <kfont.h>

#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...] <psffile>\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;
Expand Down
63 changes: 54 additions & 9 deletions src/screendump.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,82 @@
*/
#include "config.h"

#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <limits.h>
#include <getopt.h>
#include <sysexits.h>

#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);
Expand Down
Loading

0 comments on commit 02c05a8

Please sign in to comment.