From 4bbdea75311fdb53261cdbd1073b0b0d289abe01 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Fri, 26 Apr 2024 18:19:43 +0200 Subject: [PATCH] draft of main function --- cmd.c | 2 ++ cmore.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- debug.c | 5 ++++ debug.h | 21 +++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 debug.c create mode 100644 debug.h diff --git a/cmd.c b/cmd.c index 66cedd9..dfbeb3d 100644 --- a/cmd.c +++ b/cmd.c @@ -2,6 +2,8 @@ #include #include +#include "debug.h" + #include "cmd.h" #define BUFFERSIZE 4096 diff --git a/cmore.c b/cmore.c index aa1245e..43ae48f 100644 --- a/cmore.c +++ b/cmore.c @@ -1,10 +1,11 @@ /* depend: */ /* cflags: */ -/* linker: cmd.o tui.o -lpdcurses */ +/* linker: cmd.o debug.o tui.o -lpdcurses */ #include #include "cmd.h" +#include "debug.h" #include "tui.h" int display (char **lines, int xmax, int ymax) @@ -129,10 +130,78 @@ void init (void) } } +/* help message */ +int usage (int ret) +{ + FILE *fd = ret ? stderr : stdout; + fprintf (fd, "usage: %s [-c command] [-f file] [-h] [-v]\n", progname); + fprintf (fd, " -c: command\n"); + fprintf (fd, " -i: input file\n"); + fprintf (fd, " -h: help message\n"); + fprintf (fd, " -v: verbose level (%d)\n", verbose); + + return ret; +} + +/* main function */ int main (int argc, char *argv[]) { - char titre[256] = {0}; + int rc = 0; + char *input = NULL; + char *command = NULL; + char c; + + /* get basename */ + char *pt = progname = argv[0]; + while (*pt) { + if ((*pt == '/') || (*pt == '\\')) { + progname = pt + 1; + } + pt++; + } + /* process argument */ + while (argc-- > 1) { + char *arg = *(++argv); + if (arg[0] != '-') { + VERBOSE (ERROR, fprintf (stderr, "%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) && (command == NULL)) { + command = arg; + } else { + VERBOSE (ERROR, fprintf (stderr, "%s: error for command '%s'\n", progname, arg)); + return usage (1); + } + break; + case 'f': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if ((arg) && (file == NULL)) { + file = arg; + } else { + VERBOSE (ERROR, fprintf (stderr, "%s: error for file '%s'\n", progname, arg)); + return usage (1); + } + break; + case 'v': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname)); + return usage (1); + } + verbose = atoi (arg); + break; + case 'h': + default: + return usage (c != 'h'); + } + } + + char titre[256] = {0}; sprintf (titre, "Application: %s", (argc > 1) ? argv[1] : "demonstration program"); //setlocale (LC_ALL, ""); diff --git a/debug.c b/debug.c new file mode 100644 index 0000000..151e615 --- /dev/null +++ b/debug.c @@ -0,0 +1,5 @@ +#include "debug.h" + +int verbose = 1; + +/* vim: set ts=4 sw=4 et: */ diff --git a/debug.h b/debug.h new file mode 100644 index 0000000..32dc0ca --- /dev/null +++ b/debug.h @@ -0,0 +1,21 @@ +#ifndef __DEBUG_H__ +#define __DEBUG_H__ + +/* constants */ + +#define DEBUG 3 +#define INFO 2 +#define WARNING 1 +#define ERROR 0 + +/* macros */ + +#define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0) + +/* gobal variables */ + +extern int verbose; + +#endif /* __DEBUG_H__ */ + +/* vim: set ts=4 sw=4 et: */ -- 2.30.2