From 01f02c415322b4de051a616b58de5d69a08a3a7f Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 20 Dec 2022 18:29:09 +0100 Subject: [PATCH] clean calc --- ascii.c | 146 ----------------------------------------------------- calc.c | 77 ++++++++++++++++------------ fdprintf.c | 1 + fdprintf.h | 1 + makefile | 1 - 5 files changed, 48 insertions(+), 178 deletions(-) delete mode 100644 ascii.c diff --git a/ascii.c b/ascii.c deleted file mode 100644 index c61554b..0000000 --- a/ascii.c +++ /dev/null @@ -1,146 +0,0 @@ -/* depend: */ -/* cflags: */ -/* linker: atoi.o fdprintf.o */ - -#include - -#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 f5d9cfd..6414c2d 100644 --- a/calc.c +++ b/calc.c @@ -1,14 +1,15 @@ /* depend: */ /* cflags: */ -/* linker: */ +/* linker: atoi.o fdprintf.o */ -#include -#include -#include +//#include +#include #include -#include #include +#include "atoi.h" +#include "fdprintf.h" + /* constants */ //#define BUFFER_SIZE 4096 @@ -32,18 +33,18 @@ /* 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; } diff --git a/fdprintf.c b/fdprintf.c index 238878d..58bf89d 100644 --- a/fdprintf.c +++ b/fdprintf.c @@ -17,6 +17,7 @@ #include "fdprintf.h" +int stdfdin = STDIN_FILENO; int stdfdout = STDOUT_FILENO; int stdfderr = STDERR_FILENO; diff --git a/fdprintf.h b/fdprintf.h index 3094ef7..31369f7 100644 --- a/fdprintf.h +++ b/fdprintf.h @@ -3,6 +3,7 @@ int fdprintf (int fd, const char *fmt, ...); +extern int stdfdin; extern int stdfdout; extern int stdfderr; diff --git a/makefile b/makefile index aa9b1e2..772aca4 100644 --- a/makefile +++ b/makefile @@ -18,7 +18,6 @@ LDFLAGS += -g ALLEXE = ALLEXE += calc -ALLEXE += ascii ALLEXE += skel SHELL = bash -- 2.30.2