diff --git a/Makefile b/Makefile index 498c645..13e282a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=cc -PREFIX=/usr/local +PREFIX= LIBS=-lncurses all: build @@ -13,6 +13,13 @@ ascii: ascii.c build: ascii install: ascii - install -d $(PREFIX)/bin - install -m 755 ascii $(PREFIX)/bin - + @prefix="$(PREFIX)"; \ + if [[ ! $$prefix ]]; then \ + read -p $$'\033[1mWhere do you want to install? (default: /usr/local) \033[0m' prefix; \ + fi; \ + if [[ ! $$prefix ]]; then \ + prefix="/usr/local"; \ + fi; \ + mkdir -pv $$prefix/bin $$prefix/share/man/man1 \ + && cp -v ascii $$prefix/bin/ \ + && cp -v doc/ascii.1 $$prefix/share/man/man1/ diff --git a/README.md b/README.md index 136b826..95077a1 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,13 @@ manually output ASCII sequences. ## Usage -Just run `ascii` to launch the ascii table viewier or `ascii N1 N2...` to print -the ASCII characters listed by number, e.g. `ascii 72 105` will print `Hi`. In -the ASCII table viewer, you can navigate with arrow keys or h/j/k/l. Pressing -`enter` will append to the output buffer and `backspace` will delete from the -output buffer. Pressing `q` or `escape` will exit and print the output buffer -to stdout. Hitting control-c will quit without printing the output buffer. +Just run `ascii` to launch the ascii table viewier. Any text piped in will be +appended to an output buffer, followed by the ASCII values of any numbers +passed as arguments. + +## Controls +In the ASCII table viewer, you can navigate with arrow keys or h/j/k/l. +Pressing `enter` will append to the output buffer and `backspace` will delete +from the output buffer. Pressing `q` or `escape` will exit and print the output +buffer to stdout. Hitting control-c will quit without printing the output +buffer. diff --git a/ascii.c b/ascii.c index 783d37d..0edf804 100644 --- a/ascii.c +++ b/ascii.c @@ -181,12 +181,8 @@ void draw_output(char *start, char *stop) int main(int argc, char *argv[]) { - // If any command line args, treat them as numbers - // and print them as ASCII characters - if (argc >= 2) { - for (int i = 1; i < argc; i++) { - putc(atoi(argv[i]), stdout); - } + if (argc > 1 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) { + printf("ASCII table viewer/editor, Usage: ascii [N...]\n"); return 0; } @@ -237,6 +233,17 @@ int main(int argc, char *argv[]) } } + // If any command line args, treat them as bytes to append to + // the output buffer + for (int i = 1; i < argc; i++) { + long c = strtol(argv[i], NULL, 0); + outbuf[buf_i++] = (char)c; + if (buf_i >= buf_size) { + buf_size *= 2; + outbuf = realloc(outbuf, buf_size); + } + } + int selected = 0; while (1) { redraw(selected); diff --git a/doc/ascii.1 b/doc/ascii.1 new file mode 100644 index 0000000..bfc5131 --- /dev/null +++ b/doc/ascii.1 @@ -0,0 +1,50 @@ +.\" Manpage for ascii. +.\" Contact bruce@bruce-hill.com to correct errors or typos. +.TH man 8 "3 January 2019" "1.0" "ascii man page" +.SH NAME +ascii \- An ASCII table viewer +.SH SYNOPSIS +.B ascii +[ +.I N1 N2... +] +.SH DESCRIPTION +This will display an interactive ASCII table, with the byte numbers and +descriptions. You can also build up raw ASCII output, which will be printed to +stdout when the program closes. +.SH INPUT +.TP +If any input is piped to \fBascii\fR, it will appear in the output buffer. +.SH OPTIONS +.B \--help +Print the command line usage. + +.B N... +If decimal or hex numbers are passed as extra arguments, the corresponding raw +ASCII values will be appended to the output buffer after any piped input. + +.SH CONTROLS +.B Navigating: +In the ASCII table, arrow keys or h/j/k/l can be used to navigate. + +.B Editing: +You can use 'enter' to append to the output buffer and 'backspace' to delete from the output buffer. + +.B Exiting: +\'q' or 'escape' will quit and print the output buffer. Control-c can be used +to quit without printing the output buffer. + +.SH EXAMPLES +.TP +.B +ascii +Open the ASCII table. + +.TP +.B +printf "h" | ascii 105 | base64 +Open the ASCII table with "hi" in the buffer, and pipe the output to base64 +when the program closes. + +.SH AUTHOR +Bruce Hill (bruce@bruce-hill.com)