From: Laurent Mazet Date: Thu, 2 May 2024 21:14:56 +0000 (+0200) Subject: more tests X-Git-Tag: cmore-1.0~26 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=53cd5b255867cb8b63fb4e66e1aef0e1db3b37d4;p=cmore.git more tests --- diff --git a/cmd.c b/cmd.c index 11238c3..fbf0ade 100644 --- 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 983e636..d19de9f 100644 --- a/cmore.c +++ b/cmore.c @@ -4,6 +4,7 @@ /* winlnk: cmd.o debug.o tui.o -lpdcurses */ #include +#include #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 f1056af..7265218 100644 --- 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 f458b9c..4e4ef37 100644 --- 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);