more tests
authorLaurent Mazet <mazet@softndesign.org>
Thu, 2 May 2024 21:14:56 +0000 (23:14 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Thu, 2 May 2024 21:47:01 +0000 (23:47 +0200)
cmd.c
cmore.c
tui.c
tui.h

diff --git a/cmd.c b/cmd.c
index 11238c3c6cfc716f63680cf3c4ca132045097c90..fbf0ade08d06c8e38b77ed03a7d2ac4ff060c3bb 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -19,6 +19,12 @@ static char *_read_stream (FILE *sd)
         fread (buffer + size - BUFFERSIZE - 1, 1, BUFFERSIZE, sd);
     } while (!feof (sd));
 
+    /* check size */
+    if (buffer[0] == '\0') {
+        free (buffer);
+        buffer = NULL;
+    }
+
     return buffer;
 }
 
@@ -34,8 +40,7 @@ char *exec_cmd (char *cmd)
         status = pclose(fp);
     }
 
-    if (status == -1) {
-        VERBOSE (ERROR, fprintf (stderr, "can't exec command: %s\n", cmd));
+    if ((status == -1) || (buffer == NULL)) {
         free (buffer);
         buffer = NULL;
     }
@@ -56,7 +61,6 @@ char *load_file (char *name)
     }
 
     if (status == -1) {
-        VERBOSE (ERROR, fprintf (stderr, "can't load file: %s\n", name));
         free (buffer);
         buffer = NULL;
     }
diff --git a/cmore.c b/cmore.c
index 983e636cdea03be4dd802b87c8ae7d8a307712d8..d19de9f4465406db534e2eb9886184a013648403 100644 (file)
--- a/cmore.c
+++ b/cmore.c
@@ -4,6 +4,7 @@
 /* winlnk: cmd.o debug.o tui.o -lpdcurses */
 
 #include <stdlib.h>
+#include <unistd.h>
 
 #include "cmd.h"
 #include "debug.h"
@@ -15,6 +16,7 @@ char *version = "0.1";
 
 char *input = NULL;
 char *command = NULL;
+int second = 0;
 
 void display (char **lines, int xmax, int ymax)
 {
@@ -41,6 +43,10 @@ void display (char **lines, int xmax, int ymax)
             bodymsg (lines[skip + i]);
             eol = (lines[skip + i][xmax - 1 ] == '\0');
         }
+        if (second > 0) {
+            DoExit ();
+            break;
+        }
         int key = waitforkey ();
         switch (key) {
         case KEY_ESC:
@@ -101,9 +107,9 @@ void process (void)
     }
 
     if (buffer == NULL) {
-        char msg[80];
-        sprintf (msg, "Error executing %s", command ? command : input ? input : "stdin");
-        statusmsg (msg);
+        cleanup ();
+        VERBOSE (ERROR, fprintf (stderr, "%s: %s\n", command ? "can't execute:" : input ? "can't load:" : "can't read", command ? command : input ? input : "stdin"));
+        exit (1);
     }
 
     if (!lines) {
@@ -111,10 +117,19 @@ void process (void)
         free (buffer);
     }
 
-    display (lines, xmax, ymax);
-    if (command || buffer) {
-        free_lines (lines);
-        lines = NULL;
+    if (lines) {
+        display (lines, xmax, ymax);
+        if (command || buffer) {
+            free_lines (lines);
+            lines = NULL;
+        }
+    }
+
+    if (second > 0) {
+        VERBOSE (DEBUG, fprintf (stdout, "sleeping for %d''\n", second));
+        sleep (second);
+        cleanup ();
+        exit (0);
     }
 }
 
@@ -152,10 +167,11 @@ menu MainMenu[] =
 int usage (int ret)
 {
     FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s [-c command] [-f file] [-h] [-v]\n", progname);
+    fprintf (fd, "usage: %s [-c command] [-f file] [-h] [-s sec] [-v]\n", progname);
     fprintf (fd, " -c: command\n");
     fprintf (fd, " -i: input file\n");
     fprintf (fd, " -h: help message\n");
+    fprintf (fd, " -s: stoptime\n");
     fprintf (fd, " -v: verbose level (%d)\n", verbose);
 
     return ret;
@@ -201,6 +217,14 @@ int main (int argc, char *argv[])
                 return usage (1);
             }
             break;
+        case 's':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing number of second\n", progname));
+                return usage (1);
+            }
+            second = atoi (arg);
+            break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -223,11 +247,16 @@ int main (int argc, char *argv[])
     return 0;
 }
 
-/* test: cmore.exe -h | grep usage */
 /* test: cmore.exe -c 2>&1 | grep error */
 /* test: cmore.exe -c notAnExistingCommand 2>&1 | grep can\'t */
 /* test: cmore.exe -f 2>&1 | grep error */
 /* test: cmore.exe -f notAnExistingFile 2>&1 | grep can\'t */
+/* test: cmore.exe -h | grep usage */
+/* test: cmore.exe -Z 2>&1 | grep usage */
+/* test: cmore.exe -v 2>&1 | grep missing */
+/* test: cmore.exe -s 2>&1 | grep missing */
+/* test: cmore.exe _ 2>&1 | grep invalid */
+/* test: cat tui.c | cmore.exe -v 5 -s 2*/
 /* test: (sleep 1; echo -n q) | cmore.exe -c 'ip addr' */
 /* test: (sleep 1; echo -ne '\e\e') | cmore.exe -f tui.c */
 /* test: (sleep 1; echo -n kkkkk; sleep 1; echo -n i; sleep 1; echo -n q) | cmore.exe -c 'ip addr' */
diff --git a/tui.c b/tui.c
index f1056af4f9193e449872111091cfed0b40ad93d4..7265218cf507cdd268c8bfc97cc477f5bddaa8b6 100644 (file)
--- a/tui.c
+++ b/tui.c
@@ -344,7 +344,7 @@ void mainmenu(menu *mp)
     wrefresh(wbody);
 }
 
-static void cleanup(void)   /* cleanup curses settings */
+void cleanup(void)   /* cleanup curses settings */
 {
     if (incurses)
     {
diff --git a/tui.h b/tui.h
index f458b9ceee7becdab8436a0a2bf6d72d6b664ea9..4e4ef373b8607676564047ae4e3102c722269f84 100644 (file)
--- a/tui.h
+++ b/tui.h
@@ -52,6 +52,7 @@ void    domenu(menu *mp);
 
 void    mainmenu(menu *mp);
 void    helpmenu(void);
+void    cleanup(void);
 
 int     weditstr(WINDOW *win, char *buf, int field);
 WINDOW *winputbox(WINDOW *win, int nlines, int ncols);