From 8ffb744cc199922280caae15cfaeab541f594a78 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 12 May 2024 00:30:03 +0200 Subject: [PATCH] command and file windows are ready --- cmore.c | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/cmore.c b/cmore.c index b9617e9..6271d23 100644 --- 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; -- 2.30.2