Add readline support to s51
[fw/altos] / s51 / s51-main.c
index 27ed571aca68eb360f5267d8aaff33d27ee55720..46b97b0c8ca7039c25867f9caa297cc0d72f121f 100644 (file)
@@ -31,6 +31,7 @@ static double freq = 11059200;
 char *s51_prompt = "> ";
 struct ccdbg *s51_dbg;
 int s51_interrupted = 0;
+int s51_monitor = 0;
 
 static FILE *s51_input;
 static FILE *s51_output;
@@ -54,7 +55,7 @@ main(int argc, char **argv)
        char *endptr;
        struct sigvec vec, ovec;
 
-       while ((opt = getopt(argc, argv, "PVvHht:X:c:r:Z:s:S:p:")) != -1) {
+       while ((opt = getopt(argc, argv, "PVvHhmt:X:c:r:Z:s:S:p:")) != -1) {
                switch (opt) {
                case 't':
                        cpu = optarg;
@@ -100,6 +101,9 @@ main(int argc, char **argv)
                case 'h':
                        usage ();
                        break;
+               case 'm':
+                       s51_monitor = 1;
+                       break;
                }
        }
        if (s51_port) {
@@ -146,7 +150,7 @@ main(int argc, char **argv)
                                perror("fdopen");
                                exit(1);
                        }
-                       vec.sv_handler = s51_sigint;
+                       vec.sv_handler = SIG_IGN;
                        vec.sv_mask = 0;
                        vec.sv_flags = 0;
                        sigvec(SIGINT, &vec, &ovec);
@@ -174,10 +178,8 @@ s51_printf(char *format, ...)
 
        va_start(ap, format);
        vfprintf(s51_output, format, ap);
-#if 1
-       if (s51_port)
+       if (s51_monitor)
                vfprintf(stdout, format, ap);
-#endif
        va_end(ap);
 }
 
@@ -187,21 +189,34 @@ s51_putc(int c)
        putc(c, s51_output);
 }
 
+#include <readline/readline.h>
+#include <readline/history.h>
+
 int
 s51_read_line(char *line, int len)
 {
        int ret;
-       if (s51_prompt)
-               s51_printf("%s", s51_prompt);
-       else
-               s51_putc('\0');
-       fflush(s51_output);
-       ret = fgets(line, len, s51_input) != NULL;
-#if 1
-       if (s51_port)
-               printf("> %s", line);
-#endif
-       fflush(stdout);
+       if (s51_output == stdout && s51_input == stdin && s51_prompt) {
+               char *r;
+
+               r = readline(s51_prompt);
+               if (r == NULL)
+                       return 0;
+               strncpy (line, r, len);
+               line[len-1] = '\0';
+               add_history(r);
+               return 1;
+       } else {
+               if (s51_prompt)
+                       s51_printf("%s", s51_prompt);
+               else
+                       s51_putc('\0');
+               fflush(s51_output);
+               ret = fgets(line, len, s51_input) != NULL;
+               if (s51_monitor)
+                       printf("> %s", line);
+               fflush(stdout);
+       }
        return ret;
 }