first functional code
authorMazet Laurent <mazet@softndesign.org>
Mon, 29 Apr 2024 05:30:01 +0000 (07:30 +0200)
committerMazet Laurent <mazet@softndesign.org>
Mon, 29 Apr 2024 05:30:01 +0000 (07:30 +0200)
cmore.c

diff --git a/cmore.c b/cmore.c
index d439217c13e0e4a0e480ee3dc8adea5802818276..c9bc89eb1bbe071cb19af2a93052f3f50e476647 100644 (file)
--- a/cmore.c
+++ b/cmore.c
 char *progname = NULL;
 char *version = "0.1";
 
-int display (char **lines, int xmax, int ymax)
+char *input = NULL;
+char *command = NULL;
+
+void display (char **lines, int xmax, int ymax)
 {
     int stop = 0;
 
+#ifdef _PDCURSES
     statusmsg ("Press 'q' or escape to quit");
-    
+#else
+    statusmsg ("Press 'q' or double-escape to quit");
+#endif
+
     int skip = 0;
     while (!stop) {
         clsbody ();
@@ -35,9 +42,23 @@ int display (char **lines, int xmax, int ymax)
         }
         int key = waitforkey ();
         switch (key) {
-        case 'q':
         case KEY_ESC:
-            exit (0);
+#ifndef _PDCURSES
+            key = waitforkey ();
+            switch (key) {
+            case KEY_ESC:
+                DoExit ();
+                /* fallthrough */
+            case 'f':
+            case 'h':
+                stop = 1;
+                break;
+            }
+            break;
+#endif
+        case 'q':
+            DoExit ();
+            stop = 1;
             break;
         case 'i':
         case KEY_UP:
@@ -47,58 +68,58 @@ int display (char **lines, int xmax, int ymax)
         case KEY_DOWN:
             skip = (lines[skip + 1] != NULL) ? skip + 1 : skip;
             break;
-       case BUTTON_ALT+'f': //ALT_F:
-       case BUTTON_ALT+'h': //ALT_H:
-            stop = 2;
+#ifdef _PDCURSES
+        case BUTTON_ALT+'F': //ALT_F:
+        case BUTTON_ALT+'H': //ALT_H:
+            stop = 1;
             break;
+#endif
+        default:
+            printf ("key pressed: %c (0x%04x)\n", (char)key, key);
         }
     }
 
     rmstatus();
-
-    return (stop == 1);
 }
 
-int view (char *cmd)
+void process (void)
 {
-    char *buffer;
+    static char **lines = NULL;
 
     int xmax, ymax;
     WINDOW *wbody = bodywin ();
     getmaxyx (wbody, ymax, xmax);
 
-    buffer = exec_cmd (cmd);
+    char *buffer = (char *)-1;
+    if (command) {
+        buffer = exec_cmd (command);
+    } else if (input) {
+        buffer = load_file (input);
+    } else if (!lines) {
+        buffer = read_stdin ();
+    }
+
     if (buffer == NULL) {
         char msg[80];
-        sprintf (msg, "Error executing %s", cmd);
+        sprintf (msg, "Error executing %s", command ? command : input ? input : "stdin");
         statusmsg (msg);
-        return 1;
     }
 
-    char **lines = split_lines (buffer, xmax);
-    free (buffer);
-
-    int rc = display (lines, xmax, ymax);
-    free_lines (lines);
-
-    return rc;
-}
-
-void exec_ipconfig (void)
-{
-    static int index = 0;
+    if (!lines) {
+        lines = split_lines (buffer, xmax);
+        free (buffer);
+    }
 
-    switch (index) {
-        case 0: view ("ifconfig"); break;
-        case 1: view ("ps aux"); break;
-        case 2: view ("ls"); break;
+    display (lines, xmax, ymax);
+    if (command || buffer) {
+        free_lines (lines);
+        lines = NULL;
     }
-    index = (index + 1) % 3;
 }
 
 menu SubMenu0[] =
 {
-    { "Refresh", exec_ipconfig, "Various shell commands" },
+    { "Refresh", process, "Refresh action"},
     { "Exit", DoExit, "Terminate program" },
     { "", (FUNC)0, "" }
 };
@@ -126,11 +147,6 @@ menu MainMenu[] =
     { "", (FUNC)0, "" }   /* always add this as the last item! */
 };
 
-void init (void)
-{
-    view ("ifconfig");
-}
-
 /* help message */
 int usage (int ret)
 {
@@ -147,8 +163,6 @@ int usage (int ret)
 /* main function */
 int main (int argc, char *argv[])
 {
-    char *input = NULL;
-    char *command = NULL;
 
     /* get basename */
     char *pt = progname = argv[0];
@@ -201,11 +215,9 @@ int main (int argc, char *argv[])
     }
 
     char titre[256] = {0};
-    sprintf (titre, "Application: %s", (argc > 1) ? argv[1] : "demonstration program");
-
-    //setlocale (LC_ALL, "");
+    sprintf (titre, "Application: %s", command ? command : input ? input : "stdin");
 
-    startmenu (MainMenu, titre, init);
+    startmenu (MainMenu, titre, process);
 
     return 0;
 }