command and file windows are ready
authorLaurent Mazet <mazet@softndesign.org>
Sat, 11 May 2024 22:30:03 +0000 (00:30 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 11 May 2024 22:30:03 +0000 (00:30 +0200)
cmore.c

diff --git a/cmore.c b/cmore.c
index b9617e9241a2ed6a8e61b64d82aff97de1a2ebed..6271d232e750ae493d8ffa8fd08c9d03f98f528b 100644 (file)
--- a/cmore.c
+++ b/cmore.c
@@ -114,12 +114,16 @@ void process (void)
     getmaxyx (wbody, ymax, xmax);
 
     char *buffer = (char *)-1;
+    static int is_stdin = 0;
     if (command) {
         buffer = exec_cmd (command);
     } else if (input) {
         buffer = load_file (input);
     } else if (!lines) {
-        buffer = read_stdin ();
+        if (!is_stdin) {
+            buffer = read_stdin ();
+            is_stdin = 1;
+        }
     }
 
     if (buffer == NULL) {
@@ -135,7 +139,7 @@ void process (void)
 
     if (lines) {
         display (lines, xmax, ymax);
-        if (command || buffer) {
+        if (buffer) {
             free_lines (lines);
             lines = NULL;
         }
@@ -175,11 +179,30 @@ void chcmd (void)
     char *desc[] = {"New command", NULL};
     char *buf[] = {buffer, NULL};
     int c = getstrings (desc, buf, 48);
-    VERBOSE (DEBUG, fprintf (stdout, "return: 0x%04x (%c)\n", c, (c >= 32) ? c : '.'));
+
+    if ((c == '\n') && (strlen (buffer) > 0)) {
+        free (command);
+        free (input);
+        command = strdup (buffer);
+        input = NULL;
+        process ();
+    }
 }
 
 void chfile (void)
 {
+    char buffer[48 + 1] = {0};
+    char *desc[] = {"New file", NULL};
+    char *buf[] = {buffer, NULL};
+    int c = getstrings (desc, buf, 48);
+
+    if ((c == '\n') && (strlen (buffer) > 0)) {
+        free (command);
+        free (input);
+        command = NULL;
+        input = strdup (buffer);
+        process ();
+    }
 }
 
 void contdisp (void)
@@ -257,19 +280,25 @@ int main (int argc, char *argv[])
         switch (c) {
         case 'c':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
-            if ((arg) && (command == NULL)) {
-                command = arg;
+            if (arg) {
+                free (command);
+                free (input);
+                command = strdup (arg);
+                input = NULL;
             } else {
-                VERBOSE (ERROR, fprintf (stderr, "%s: error for command '%s'\n", progname, arg));
+                VERBOSE (ERROR, fprintf (stderr, "%s: error for command\n", progname));
                 return usage (1);
             }
             break;
         case 'f':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
-            if ((arg) && (input == NULL)) {
-                input = arg;
+            if (arg) {
+                free (command);
+                free (input);
+                command = NULL;
+                input = strdup (arg);
             } else {
-                VERBOSE (ERROR, fprintf (stderr, "%s: error for file '%s'\n", progname, arg));
+                VERBOSE (ERROR, fprintf (stderr, "%s: error for file\n", progname));
                 return usage (1);
             }
             break;