clean calc
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 20 Dec 2022 17:29:09 +0000 (18:29 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 20 Dec 2022 17:29:09 +0000 (18:29 +0100)
ascii.c [deleted file]
calc.c
fdprintf.c
fdprintf.h
makefile

diff --git a/ascii.c b/ascii.c
deleted file mode 100644 (file)
index c61554b..0000000
--- a/ascii.c
+++ /dev/null
@@ -1,146 +0,0 @@
-/* depend: */
-/* cflags: */
-/* linker: atoi.o fdprintf.o */
-
-#include <stddef.h>
-
-#include "atoi.h"
-#include "fdprintf.h"
-
-/* macros */
-
-#define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
-#define VERSION "0.9"
-
-/* 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 */
-
-int usage (int ret)
-{
-    int fd = ret ? stdfderr : stdfdout;
-    fdprintf (fd, "usage: %s\n", progname);
-    fdprintf (fd, " -c : number of columns (%d)\n", nbcols);
-    fdprintf (fd, " -h : help message\n");
-    fdprintf (fd, " -v : show version\n");
-
-    return ret;
-}
-
-/* main function */
-
-int main (int argc, char *argv[]) 
-{
-    int i = 0;
-
-    /* programm name */
-
-    progname = argv[0];
-    while (progname[i] != '\0') {
-        if ((progname[i] == '/') || (progname[i] == '\\')) {
-            progname += i + 1;
-            i = 0;
-        } else {
-            i++;
-        }
-    }
-
-    /* argument processing */
-
-    while (argc-- > 1) {
-        char *arg = *(++argv);
-        if (arg[0] != '-') {
-            PRINTERR ("%s: invalid option -- %s\n", progname, arg);
-            return usage (1);
-        }
-        char c = arg[1];
-        switch (c) {
-        case 'c':
-            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
-            if (arg == NULL) {
-                PRINTERR ("%s: missing number of columns\n", progname);
-                return usage (1);
-            }
-            nbcols = atoi (arg);
-            break;
-        case 'v':
-            PRINTOUT ("%s: version %s\n", progname, VERSION);
-            return 0;
-            break;
-        case 'h':
-        default:
-            return usage (c != 'h');
-        }
-    }
-
-    /* main */
-
-    int n = (256 + nbcols - 1) / nbcols;
-    for (i = 0; i < n * nbcols; i++) {
-        char line[] = "   [  ]    x";
-        int j = i / nbcols + (i % nbcols) * n;
-
-        if (j > 255) {
-            PRINTOUT ("\n");
-            continue;
-        }
-
-        if (j > 99) line[0] = '0' + j / 100;
-        if (j > 9) line[1] = '0' + (j / 10) % 10;
-        line[2] = '0' + j % 10;
-        line[4] = '0' + j / 16;
-        if (line[4] > '9') line[4] += 'a' - '0' - 10;
-        line[5] = '0' + j % 16;
-        if (line[5] > '9') line[5] += 'a' - '0' - 10;
-        
-        if (tablechars[j][0] != '\0') {
-            int k = 0;
-            while (tablechars[j][k] != '\0') {
-                line[8 + k] = tablechars[j][k];
-                k++;
-            }
-        } else {
-            line[8] = j;
-        }
-        line[11] = ((i + 1) % nbcols == 0) ? '\n' : ' ';
-
-        PRINTOUT ("%s", line);
-    }
-
-    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) }'
-
-// test: ascii.exe | awk '/ 64\[40\] @/ { rc=1 } END { exit (1-rc) }'
-// test: ascii.exe | awk '/127\[7f\] DEL/ { rc=1 } END { exit (1-rc) }'
-// test: ascii.exe -c 4 | tail -1 | awk '/63/ { rc=1 } END { exit (1-rc) }'
-// test: ascii.exe -c 5 | tail -1 | awk '/51/ { rc=1 } END { exit (1-rc) }'
-// test: ascii.exe -c 6 | tail -1 | awk '/42/ { rc=1 } END { exit (1-rc) }'
-
-/* vim: set ts=4 sw=4 et: */
diff --git a/calc.c b/calc.c
index f5d9cfdb4399d65771f3223a5a13e2c248bfb4dc..6414c2df28f98b7b5b8ffdf1574a6abbda7d3960 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,14 +1,15 @@
 /* depend: */
 /* cflags: */
-/* linker: */
+/* linker: atoi.o fdprintf.o */
 
-#include <assert.h>
-#include <getopt.h>
-#include <malloc.h>
+//#include <malloc.h>
+#include <stddef.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <unistd.h>
 
+#include "atoi.h"
+#include "fdprintf.h"
+
 /* constants */
 
 //#define BUFFER_SIZE 4096
 /* gobal variables */
 
 char *progname = NULL;
-int verbose = 0;
+int verbose = 2;
 
 /* help function */
 
-void usage (int ret)
+int usage (int ret)
 {
-    FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s\n", progname);
-    fprintf (fd, " -h : help message\n");
-    fprintf (fd, " -v : verbose level (%d)\n", verbose);
+    int fd = ret ? stdfderr : stdfdout;
+    fdprintf (fd, "usage: %s\n", progname);
+    fdprintf (fd, " -h : help message\n");
+    fdprintf (fd, " -v : verbose level (%d)\n", verbose);
 
-    exit (ret);
+    return ret;
 }
 
 /* main function */
@@ -52,38 +53,57 @@ int main (int argc, char *argv[])
 {
     char buffer[BUFFER_SIZE + 1] = {0};
     char *pt = buffer;
-    int i, j = 0, n;
+    int i = 0, j = 0, n;
+
+    /* program name */
 
     progname = argv[0];
+    while (progname[i] != '\0') {
+        if ((progname[i] == '/') || (progname[i] == '\\')) {
+            progname += i + 1;
+            i = 0;
+        } else {
+            i++;
+        }
+    }
+
+    /* argument processing */
 
-    int c;
-    while ((c = getopt(argc, argv, "hv:")) != EOF) {
+     while (argc-- > 1) {
+        char *arg = *(++argv);
+        if (arg[0] != '-') {
+            PRINTERR ("%s: invalid option -- %s\n", progname, arg);
+            return usage (1);
+        }
+        char c = arg[1];
         switch (c) {
         case 'v':
-            verbose = atoi (optarg);
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                PRINTERR ("%s: missing verbose level\n", progname);
+                return usage (1);
+            }
+            verbose = atoi (arg);
             break;
         case 'h':
-            VERBOSE (INFO, usage (0));
-            break;
         default:
-            VERBOSE (ERROR, usage (1));
+            return usage (c != 'h');
         }
     }
-    if (argc - optind != 0) {
-        VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]));
-        VERBOSE (ERROR, usage (1));
-    }
 
     /* read from input stream */
-    while ((n = read (STDIN_FILENO, pt, BUFFER_SIZE - (pt - buffer))) != 0) {
-        VERBOSE (DEBUG, fprintf (stdout, "read %d bytes\n", n));
+
+    while ((n = read (stdfdin, pt, BUFFER_SIZE - (pt - buffer))) != 0) {
+        VERBOSE (DEBUG, PRINTOUT ("read %d bytes\n", n));
         n += (pt - buffer);
 
         /* look for end of line */
         for (i = 0, j = 0; i < n; i++) {
             if (buffer[i] == '\n') {
                 buffer[i] = 0;
-                VERBOSE (DEBUG, fprintf (stdout, "line(%d): %s\n", j, buffer + j));
+                VERBOSE (DEBUG, PRINTOUT ("line(%d): %s\n", j, buffer + j));
+                //fsync (stdfdout);
+                fflush (stdout);
                 j = i + 1;
             }
         }
@@ -100,11 +120,6 @@ int main (int argc, char *argv[])
         }
     }
 
-    /* check that nothing is left behind */
-
-    VERBOSE (DEBUG, fprintf (stdout, "last\n"));
-    VERBOSE (DEBUG, fprintf (stdout, "line(%d): %s\n", j, buffer + j));
-
     return 0;
 }
 
index 238878dce42b2877ba90c719b9a62e46bfb4b945..58bf89d8449c0a8f3f5e02c71834afd648b6f2e1 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "fdprintf.h"
 
+int stdfdin = STDIN_FILENO;
 int stdfdout = STDOUT_FILENO;
 int stdfderr = STDERR_FILENO;
 
index 3094ef7ae44b3f8cad3ab39868175245176179ee..31369f7c4b178a87fa548c989442ecf967d50be9 100644 (file)
@@ -3,6 +3,7 @@
 
 int fdprintf (int fd, const char *fmt, ...);
 
+extern int stdfdin;
 extern int stdfdout;
 extern int stdfderr;
 
index aa9b1e2185cc30da79d2b81f7010f0b5ac49c3dc..772aca4a9a1888fff18b1e8b20152f6ded4e6d99 100644 (file)
--- a/makefile
+++ b/makefile
@@ -18,7 +18,6 @@ LDFLAGS += -g
 
 ALLEXE  =
 ALLEXE += calc
-ALLEXE += ascii
 ALLEXE += skel
 
 SHELL = bash