From: Laurent Mazet Date: Mon, 19 Dec 2022 10:09:25 +0000 (+0100) Subject: new program to display ascii table X-Git-Url: https://secure.softndesign.org/git/?p=ascii.git;a=commitdiff_plain;h=93b149370129c76fe7dc0b06c823fc4140dd0a8b new program to display ascii table --- diff --git a/ascii.c b/ascii.c new file mode 100644 index 0000000..5eeac21 --- /dev/null +++ b/ascii.c @@ -0,0 +1,112 @@ +/* depend: */ +/* cflags: */ +/* linker: */ + +#include +#include +#include +#include +#include + +/* macros */ + +#define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0) + +/* gobal variables */ + +char *progname = NULL; +int verbose = 0; +int nbcols = 4; + +char tablechars[256][4] = { + "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "DEL", + "DS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", + "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", + "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US", + "SP", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "DEL"}; + +/* help function */ + +void usage (int ret) +{ + FILE *fd = ret ? stderr : stdout; + fprintf (fd, "usage: %s\n", progname); + fprintf (fd, " -c : number of columns (%d)\n", nbcols); + fprintf (fd, " -h : help message\n"); + fprintf (fd, " -v : verbose level (%d)\n", verbose); + + exit (ret); +} + +/* main function */ + +int main (int argc, char *argv[]) +{ + int i; + + progname = argv[0]; + + int c; + while ((c = getopt(argc, argv, "c:hv:")) != EOF) { + switch (c) { + case 'c': + nbcols = atoi (optarg); + break; + case 'v': + verbose = atoi (optarg); + break; + case 'h': + default: + usage (c != 'h'); + } + } + if (argc - optind != 0) { + fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]); + usage (1); + } + + for (i = 0; i < 256; i++) { + char line[] = " : : x"; + + if (i > 99) line[0] = '0' + i / 100; + if (i > 9) line[1] = '0' + (i / 10) % 10; + line[2] = '0' + i % 10; + line[4] = '0' + i / 16; + if (line[4] > '9') line[4] += 'a' - '0' - 10; + line[5] = '0' + i % 16; + if (line[5] > '9') line[5] += 'a' - '0' - 10; + + if (tablechars[i][0] != '\0') { + int j = 0; + while (tablechars[i][j] != '\0') { + line[8 + j] = tablechars[i][j]; + j++; + } + } else { + line[8] = i; + } + line[11] = (((i + 1) % nbcols == 0) || (i == 255)) ? '\n' : ' '; + + write (STDOUT_FILENO, line, 12); + } + + return 0; +} + +// test: ascii.exe -h +// test: ascii.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' +// test: ascii.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }' +// test: ascii.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }' + +/* vim: set ts=4 sw=4 et: */ diff --git a/makefile b/makefile index 40cfde7..c98ce70 100644 --- a/makefile +++ b/makefile @@ -18,6 +18,7 @@ LDFLAGS += -g ALLEXE = ALLEXE += calc +ALLEXE += ascii ALLEXE += skel SHELL = bash