new program to display ascii table
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Mon, 19 Dec 2022 10:09:25 +0000 (11:09 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Mon, 19 Dec 2022 10:09:25 +0000 (11:09 +0100)
ascii.c [new file with mode: 0644]
makefile

diff --git a/ascii.c b/ascii.c
new file mode 100644 (file)
index 0000000..5eeac21
--- /dev/null
+++ b/ascii.c
@@ -0,0 +1,112 @@
+/* depend: */
+/* cflags: */
+/* linker: */
+
+#include <assert.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/* 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: */
index 40cfde7c33adb4477999621f99273f4e539a26f2..c98ce70f99931118b7f99c8e1aa8da1749d3df7b 100644 (file)
--- a/makefile
+++ b/makefile
@@ -18,6 +18,7 @@ LDFLAGS += -g
 
 ALLEXE  =
 ALLEXE += calc
+ALLEXE += ascii
 ALLEXE += skel
 
 SHELL = bash