date and time created 92/06/03 17:58:45 by marc
authorMarc Teitelbaum <marc@ucbvax.Berkeley.EDU>
Thu, 4 Jun 1992 08:58:45 +0000 (00:58 -0800)
committerMarc Teitelbaum <marc@ucbvax.Berkeley.EDU>
Thu, 4 Jun 1992 08:58:45 +0000 (00:58 -0800)
SCCS-vsn: bin/sh/histedit.c 1.1

usr/src/bin/sh/histedit.c [new file with mode: 0644]

diff --git a/usr/src/bin/sh/histedit.c b/usr/src/bin/sh/histedit.c
new file mode 100644 (file)
index 0000000..0640e43
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * Editline and history functions (and glue).
+ */
+#include <stdio.h>
+#include "parser.h"
+#include "options.h"
+#include "error.h"
+
+#include "histedit.h"
+
+History *hist; /* history cookie */
+EditLine *el;  /* editline cookie */
+static FILE *el_in, *el_out;
+
+extern int is_interactive;
+
+/*
+ * Set history and editing status.  Called whenever the status may
+ * have changed (figures out what to do).
+ */
+histedit() {
+
+       if (is_interactive) {
+               if (!hist) {
+                       /*
+                        * turn history on
+                        */
+                       INTOFF;
+                       hist = history_init();
+                       INTON;
+
+                       if (hist != NULL)
+                               sethistsize();
+                       else
+                               out2str("sh: can't initialize history\n");
+               }
+               if (Eflag && !el && isatty(0)) { /* && isatty(2) ??? */
+                       /*
+                        * turn editing on
+                        */
+                       INTOFF;
+                       if (el_in == NULL)
+                               el_in = fdopen(0, "r");
+                       if (el_out == NULL)
+                               el_out = fdopen(2, "w");
+                       if (el_in == NULL || el_out == NULL)
+                               goto bad;
+                       el = el_init(arg0, el_in, el_out);
+                       if (el != NULL) {
+                               if (hist)
+                                       el_set(el, EL_HIST, history_set, hist);
+                               el_set(el, EL_PROMPT, getprompt);
+                       } else {
+bad:
+                               out2str("sh: can't initialize editing\n");
+                       }
+                       INTON;
+                       if (el) /* XXX - -o vi etc... */
+                               el_set(el, EL_EDITOR, "vi");
+               } else if (!Eflag && el) {
+                       INTOFF;
+                       el_end(el);
+                       el = NULL;
+                       INTON;
+               }
+       } else {
+               INTOFF;
+               if (el) {       /* no editing if not interactive */
+                       el_end(el);
+                       el = NULL;
+               }
+               if (hist) {
+                       history_end(hist);
+                       hist = NULL;
+               }
+               INTON;
+       }
+}
+
+sethistsize() {
+       char *cp;
+       int histsize;
+
+       if (hist != NULL) {
+               cp = lookupvar("HISTSIZE");
+               if (cp == NULL || *cp == '\0' || 
+                  (histsize = atoi(cp)) < 0)
+                       histsize = 100;
+               history_set(hist, H_EVENT, histsize);
+       }
+}