draft of main function
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Fri, 26 Apr 2024 16:19:43 +0000 (18:19 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Fri, 26 Apr 2024 16:19:43 +0000 (18:19 +0200)
cmd.c
cmore.c
debug.c [new file with mode: 0644]
debug.h [new file with mode: 0644]

diff --git a/cmd.c b/cmd.c
index 66cedd9e4e32e89d34ee88d9a0c26dcf07919175..dfbeb3dd22556b927098bb5a567fa9edbcb16984 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "debug.h"
+
 #include "cmd.h"
 
 #define BUFFERSIZE 4096
diff --git a/cmore.c b/cmore.c
index aa1245ef816f4bb89612ebb59ad85d66dbe1cccc..43ae48f5a66602d94b1f53ac4f242048942680b5 100644 (file)
--- 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 <stdlib.h>
 
 #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 (file)
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 (file)
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: */