aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2024-12-14 03:55:37 -0500
committerB. Watson <urchlay@slackware.uk>2024-12-14 03:55:37 -0500
commit56fd406b636f29e4178c888ac62337b9c792f3dd (patch)
treec912566d97ec06f9f2fe0860578f518ebf5e0da1
parente11de8814424c2abb61d91a050e1096d17765787 (diff)
downloaduxd-56fd406b636f29e4178c888ac62337b9c792f3dd.tar.gz
add option parsing, implement -r option.
-rw-r--r--Makefile2
-rw-r--r--uxd.c81
2 files changed, 67 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index c926d46..115ff0c 100644
--- a/Makefile
+++ b/Makefile
@@ -30,6 +30,8 @@ CFLAGS=$(DEFINES) $(WARNFLAGS) $(MYCFLAGS)
all: uxd man
+uxd: uxd.c getopt.c
+
install: all
mkdir -p $(DESTDIR)$(BINDIR) $(DESTDIR)$(MAN1DIR)
$(INSTALL_DATA) uxd.1 $(DESTDIR)$(MAN1DIR)
diff --git a/uxd.c b/uxd.c
index 9aaf83e..f58c24e 100644
--- a/uxd.c
+++ b/uxd.c
@@ -3,6 +3,10 @@
#include <string.h>
#include <unistd.h>
+extern int getopt(int, char **, char *);
+extern char *optarg;
+extern int optind;
+
/* output looks like:
0 1 2 3 4 5 6 7 8 9 A B C D E F
@@ -75,35 +79,76 @@ char right_buf[4096];
int dump_column = 0;
int filepos = 0;
+/* options */
+int hilite_multi = 0; /* -r */
+int mono = 0; /* -m */
+int display_offset = 0; /* -o */
+int seekpos = 0; /* -s */
+int uppercase = 0; /* -u */
+
void usage(void) {
- printf("uxd (Utf-8 heX Dump) v"VERSION" by B. Watson. WTFPL.\n");
+ printf("uxd (Utf-8 heX Dump) v" VERSION " by B. Watson. WTFPL.\n");
printf("Usage: %s [<file>]\n", self);
printf(" With no <file>, or with -, read standard input.\n");
exit(0);
}
-void open_input(const int argc, const char *argv1) {
- if(argc == 1) {
+void version(void) {
+ printf("%s\n", VERSION);
+ exit(0);
+}
+
+void open_input(const char *arg) {
+ if(!arg || (strcmp(arg, "-") == 0)) {
input = stdin;
- return;
+ } else {
+ input = fopen(arg, "rb");
+ if(!input) {
+ fprintf(stderr, "%s: ", self);
+ perror(arg);
+ exit(1);
+ }
}
+}
- if(argv1[0] == '-' && argv1[1] != '\0') {
- usage();
+void parse_options(int argc, char **argv) {
+ int opt;
+
+ if(argc > 1) {
+ if(strcmp(argv[1], "--help") == 0)
+ usage();
+ if(strcmp(argv[1], "--version") == 0)
+ version();
}
- if(argc == 2) {
- if(strcmp(argv1, "-") == 0)
- input = stdin;
- else {
- input = fopen(argv1, "rb");
- if(!input) {
- fprintf(stderr, "%s: ", self);
- perror(argv1);
+ while((opt = getopt(argc, argv, "rmo:s:uhv")) != -1) {
+ switch(opt) {
+ case 'r':
+ hilite_multi = 1; break;
+ case 'm':
+ mono = 1; break;
+ case 'o':
+ display_offset = atoi(optarg);
+ break;
+ case 's':
+ seekpos = atoi(optarg);
+ break;
+ case 'u':
+ uppercase = 1; break;
+ case 'h':
+ usage(); break;
+ case 'v':
+ version(); break;
+ default:
exit(1);
- }
}
}
+
+ /* filename (if present) must come after all -options, and
+ there can only be one filename. */
+ if(optind < (argc - 1)) usage();
+
+ open_input(argv[optind]);
}
/* Unicode control character printable equivalents. For 0, use
@@ -323,6 +368,10 @@ int dump_utf8_char(void) {
append_right(printable);
append_color_off(right_buf);
+ if(hilite_multi && cont_count) {
+ c = bg; bg = fg; fg = c;
+ }
+
for(i = 0; i <= cont_count; i++) {
append_left(bytes[i], (i != cont_count), fg, bg);
}
@@ -341,7 +390,7 @@ void dump_file(void) {
int main(int argc, char **argv) {
set_self(argv[0]);
- open_input(argc, argv[1]);
+ parse_options(argc, argv);
dump_file();
fclose(input);
return 0;