From 8994b9d28ebe5b6c3c90330a32d8317cd7299d9b Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Mon, 16 Dec 2024 06:22:56 -0500 Subject: add options to --help message. --- Makefile | 5 ++++- mkusage.pl | 18 ++++++++++++++++++ usage.c | 16 ++++++++++++++++ uxd.1 | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- uxd.c | 54 +++++++++++++++++++++++++++++++++++++----------------- uxd.rst | 32 ++++++++++++++++++++++++++++++-- 6 files changed, 159 insertions(+), 22 deletions(-) create mode 100644 mkusage.pl create mode 100644 usage.c diff --git a/Makefile b/Makefile index 115ff0c..128e6e7 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,10 @@ CFLAGS=$(DEFINES) $(WARNFLAGS) $(MYCFLAGS) all: uxd man -uxd: uxd.c getopt.c +uxd: uxd.c getopt.c usage.c + +usage.c: uxd.rst mkusage.pl + perl mkusage.pl uxd.rst > usage.c install: all mkdir -p $(DESTDIR)$(BINDIR) $(DESTDIR)$(MAN1DIR) diff --git a/mkusage.pl b/mkusage.pl new file mode 100644 index 0000000..f8fe485 --- /dev/null +++ b/mkusage.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl -w + +print "char *usage_opts[] = {\n"; + +while(<>) { + chomp; + if(/^-[a-zA-Z]/) { + $opt = $_; + next; + } + if($opt && (/^\.\. (.*)$/)) { + print "\t\" $opt: $1\",\n"; + undef $opt; + } +} + +# don't use NULL, it's not defined. +print "\t(char *)0\n};\n"; diff --git a/usage.c b/usage.c new file mode 100644 index 0000000..6664916 --- /dev/null +++ b/usage.c @@ -0,0 +1,16 @@ +char *usage_opts[] = { + " -b: bold color output.", + " -c nnnn: colors (2 to 4 digits, 0 to 7).", + " -h, --help: print this help message.", + " -i: print number of bytes/chars/ascii/multibyte/bad sequences.", + " -l length: stop dumping after bytes (not characters).", + " -m: monochrome mode.", + " -n: ignore UXD_OPTS environment variable.", + " -o offset: added to hex offsets (decimal, 0x hex, 0 octal).", + " -r: highlight multi-byte chars in reverse video.", + " -s pos: seek in input before dumping (-pos = seek back from EOF).", + " -S pos: like -s, but also sets -o so addresses start at 0.", + " -u: uppercase hex digits.", + " -v, --version: print version of uxd.", + (char *)0 +}; diff --git a/uxd.1 b/uxd.1 index 914e9e7..6e7ac58 100644 --- a/uxd.1 +++ b/uxd.1 @@ -59,11 +59,17 @@ over the environment. Options can be bundled: \fB\-ubc1234\fP is the same as \fB\-u\fP \fB\-n\fP \fB\-c 1234\fP\&. The one exception is the \fB\-n\fP option, which should appear by itself. +.\" the comments are turned into the --help message by mkusage.pl. +. .INDENT 0.0 .TP .B \-b Bold output. This may be more or less readable, depending on your terminal and its color settings. Ignored if \fB\-m\fP given. +.UNINDENT +.\" bold color output. +. +.INDENT 0.0 .TP .BI \-c \ nnnn Set the colors to use. Must be 2 to 4 digits, from 0 to 7. These are @@ -72,38 +78,70 @@ normal characters, the 3rd digit (optional) is the color for non\-printable and space characters, and the 4th (optional) is for invalid UTF\-8 sequences. Default: \fB2351\fP\&. This option also disables a prior \fB\-m\fP option. +.UNINDENT +.\" colors (2 to 4 digits, 0 to 7). +. +.INDENT 0.0 .TP .B \-h\fP,\fB \-\-help Print built\-in usage message and exit. +.UNINDENT +.\" print this help message. +. +.INDENT 0.0 .TP .B \-i After dumping, print information about the input: number of bytes, characters, ASCII (one\-byte) characters, multi\-byte characters, and bad sequences. +.UNINDENT +.\" print number of bytes/chars/ascii/multibyte/bad sequences. +. +.INDENT 0.0 .TP .BI \-l \ length Stop dumping after \fIlength\fP bytes (not characters). If the limit is reached in the middle of a multibyte character, the entire character will be dumped. +.UNINDENT +.\" stop dumping after bytes (not characters). +. +.INDENT 0.0 .TP .B \-m Monochrome mode. Uses underline, bold, reverse video instead of color. Use this if you have trouble distinguishing the colors, or if they look too much like angry fruit salad. Disables prior \fB\-b\fP, \fB\-c\fP options. +.UNINDENT +.\" monochrome mode. +. +.INDENT 0.0 .TP .B \-n Ignore \fBUXD_OPTS\fP environment variable. This option should not be bundled with other options (e.g. use \fB\-n \-u\fP, not \fB\-nu\fP). +.UNINDENT +.\" ignore UXD_OPTS environment variable. +. +.INDENT 0.0 .TP .BI \-o \ offset Add this amount to the hex offsets (left column). May be negative, if you can think of a reason to want it to be. Can be given in decimal, hex (with \fI0x\fP prefix), or octal (with \fI0\fP prefix). +.UNINDENT +.\" added to hex offsets (decimal, 0x hex, 0 octal). +. +.INDENT 0.0 .TP .B \-r Highlight multi\-byte sequences in reverse video, in the hex output. Ignored if \fB\-m\fP given. +.UNINDENT +.\" highlight multi-byte chars in reverse video. +. +.INDENT 0.0 .TP .BI \-s \ pos Seek in input before starting to dump. \fIpos\fP is bytes, not @@ -111,18 +149,32 @@ characters. Positive \fIpos\fP means seek from the start of the input. Negative \fIpos\fP only works on files (not standard input); it means seek backward from EOF. Can be given in decimal, hex (with \fI0x\fP prefix), or octal (with \fI0\fP prefix). +.UNINDENT +.\" seek in input before dumping (-pos = seek back from EOF). +. +.INDENT 0.0 .TP .BI \-S \ pos Same as \fB\-s\fP, but file offsets start at 0 rather than the position after seeking. \fB\-S 100\fP is the same as \fB\-s 100 \-o \-100\fP\&. Works with negative \fIpos\fP, too. +.UNINDENT +.\" like -s, but also sets -o so addresses start at 0. +. +.INDENT 0.0 .TP .B \-u Use uppercase hex digits \fIA\-F\fP\&. Default is lowercase. +.UNINDENT +.\" uppercase hex digits. +. +.INDENT 0.0 .TP .B \-v\fP,\fB \-\-version Print version number and exit. .UNINDENT +.\" print version of uxd. +. .SH OUTPUT FORMAT .sp The output is designed to fit in an 80\-column terminal. @@ -231,8 +283,8 @@ The author\(aqs testing is done primarily with \fBurxvt\fP(1). Other terminals aren\(aqt tested as often. Some terminals may need UTF\-8 enabled, if it\(aqs not on by default (e.g. xterm). .sp -Known to work: urxvt, xterm, st, xfce4\-terminal, gnome\-terminal, kitty, the Linux console (but -see \fBFONTS\fP, below). +Known to work: urxvt, xterm, st, xfce4\-terminal, gnome\-terminal, +kitty, konsole, the Linux console (but see \fBFONTS\fP, below). .sp Known \fBnot\fP to work: rxvt (doesn\(aqt support Unicode at all), and its derivatives such as aterm. diff --git a/uxd.c b/uxd.c index 22a25f3..3aea465 100644 --- a/uxd.c +++ b/uxd.c @@ -60,30 +60,38 @@ extern int optind; #define CYAN 6 #define WHITE 7 /* don't use (could be the background color) */ -#define SPECIAL PURPLE +/* default colors */ +int normal_colors[] = { GREEN, YELLOW }; +int bad_color = RED; +int special_color = PURPLE; -#define BAD_FG BLACK -#define BAD_BG bad_color +/* toggles between 0 and 1 for each normal character */ +int cur_normal_hilite = 1; +/* highlight types */ #define HL_NORMAL 0 #define HL_NORM_INV 1 #define HL_SPECIAL 2 #define HL_BAD 3 -int normal_colors[] = { GREEN, YELLOW }; -int cur_normal_hilite = 1; -int bad_color = RED; -int special_color = SPECIAL; - +/* name (read from argv[0]), for error/warning messages. */ const char *self; + +/* the input file, either stdin or a file we open for reading. */ FILE *input; /* these buffers are bigger than they need to be really. */ +/* offset and hex bytes: */ char left_buf[4096]; + +/* printable form: */ char right_buf[4096]; +/* dump_column ranges 0..(MAX_DUMP_COLS-1) */ #define MAX_DUMP_COLS 16 int dump_column = 0; + +/* where we're at in the input. */ int filepos = 0; /* Unicode control character printable equivalents. For 0, use @@ -93,20 +101,23 @@ int filepos = 0; normal font sizes, but it's still better than using a dot for everything like xxd does. */ char * const special_symbols[] = { + /* 0-0x0f: */ "∅", "␁", "␂", "␃", "␄", "␅", "␆", "␇", "␈", "⇥", "↵", "␋", "␌", "␍", "␎", "␏", + /* 0x10-0x1f: */ "␐", "␑", "␒", "␓", "␔", "␕", "␖", "␗", "␘", "␙", "␚", "⎋", "␜", "␝", "␞", "␟", + /* 0x20 (space): */ "␣", }; /* options */ -int print_info = 0; /* -i */ -int bold = 0; /* -b */ +int print_info = 0; /* -i */ +int bold = 0; /* -b */ int hilite_multi = 0; /* -r */ -int mono = 0; /* -m */ +int mono = 0; /* -m */ long display_offset = 0; /* -o */ -long seekpos = 0; /* -s, -S */ +long seekpos = 0; /* -s, -S */ int seek_offset_zero = 0; /* -S */ -long limit; /* -l */ +long limit; /* -l */ const char *hex_byte_fmt = "%02x"; /* -u */ const char *hex_word_fmt = "%04x: "; /* " */ @@ -118,9 +129,18 @@ long bad_count = 0; long char_count = 0; void usage(void) { - printf("uxd (Utf-8 heX Dump) v" VERSION " by B. Watson. WTFPL.\n"); - printf("Usage: %s []\n", self); - printf(" With no , or with -, read standard input.\n"); + extern char *usage_opts[]; + char **opt; + + puts("uxd (Utf-8 heX Dump) v" VERSION " by B. Watson. WTFPL."); + printf("Usage: %s -[options] []\n", self); + puts(" With no , or with -, read standard input."); + puts("Options:"); + + for(opt = usage_opts; *opt; opt++) { + puts(*opt); + } + exit(0); } @@ -332,7 +352,7 @@ void append_color(char *buf, int hl_type) { break; default: case HL_BAD: - fgcolor = BAD_FG; + fgcolor = 0; bgcolor = bad_color; break; } diff --git a/uxd.rst b/uxd.rst index b0645e8..3e5e702 100644 --- a/uxd.rst +++ b/uxd.rst @@ -50,10 +50,14 @@ Options can be bundled: **-ubc1234** is the same as **-u** **-n** **-c 1234**. The one exception is the **-n** option, which should appear by itself. +.. the comments are turned into the --help message by mkusage.pl. + -b Bold output. This may be more or less readable, depending on your terminal and its color settings. Ignored if **-m** given. +.. bold color output. + -c nnnn Set the colors to use. Must be 2 to 4 digits, from 0 to 7. These are standard ANSI colors. The first 2 are the alternating colors for @@ -62,38 +66,54 @@ by itself. sequences. Default: **2351**. This option also disables a prior **-m** option. +.. colors (2 to 4 digits, 0 to 7). + -h, --help Print built-in usage message and exit. +.. print this help message. + -i After dumping, print information about the input: number of bytes, characters, ASCII (one-byte) characters, multi-byte characters, and bad sequences. +.. print number of bytes/chars/ascii/multibyte/bad sequences. + -l length Stop dumping after *length* bytes (not characters). If the limit is reached in the middle of a multibyte character, the entire character will be dumped. +.. stop dumping after bytes (not characters). + -m Monochrome mode. Uses underline, bold, reverse video instead of color. Use this if you have trouble distinguishing the colors, or if they look too much like angry fruit salad. Disables prior **-b**, **-c** options. +.. monochrome mode. + -n Ignore **UXD_OPTS** environment variable. This option should not be bundled with other options (e.g. use **-n -u**, not **-nu**). +.. ignore UXD_OPTS environment variable. + -o offset Add this amount to the hex offsets (left column). May be negative, if you can think of a reason to want it to be. Can be given in decimal, hex (with *0x* prefix), or octal (with *0* prefix). +.. added to hex offsets (decimal, 0x hex, 0 octal). + -r Highlight multi-byte sequences in reverse video, in the hex output. Ignored if **-m** given. +.. highlight multi-byte chars in reverse video. + -s pos Seek in input before starting to dump. *pos* is bytes, not characters. Positive *pos* means seek from the start of the @@ -101,17 +121,25 @@ by itself. it means seek backward from EOF. Can be given in decimal, hex (with *0x* prefix), or octal (with *0* prefix). +.. seek in input before dumping (-pos = seek back from EOF). + -S pos Same as **-s**, but file offsets start at 0 rather than the position after seeking. **-S 100** is the same as **-s 100 -o -100**. Works with negative *pos*, too. +.. like -s, but also sets -o so addresses start at 0. + -u Use uppercase hex digits *A-F*. Default is lowercase. +.. uppercase hex digits. + -v, --version Print version number and exit. +.. print version of uxd. + OUTPUT FORMAT ============= @@ -211,8 +239,8 @@ The author's testing is done primarily with **urxvt**\(1). Other terminals aren't tested as often. Some terminals may need UTF-8 enabled, if it's not on by default (e.g. xterm). -Known to work: urxvt, xterm, st, xfce4-terminal, gnome-terminal, kitty, the Linux console (but -see **FONTS**, below). +Known to work: urxvt, xterm, st, xfce4-terminal, gnome-terminal, +kitty, konsole, the Linux console (but see **FONTS**, below). Known **not** to work: rxvt (doesn't support Unicode at all), and its derivatives such as aterm. -- cgit v1.2.3