BSD 4_4_Lite2 release
[unix-history] / usr / src / bin / sh / histedit.c
index dd20472..5601852 100644 (file)
@@ -1,16 +1,63 @@
-/*
- * Editline and history functions (and glue).
+/*-
+ * Copyright (c) 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Kenneth Almquist.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
  */
+
+#ifndef lint
+static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
+#endif /* not lint */
+
 #include <sys/param.h>
 #include <paths.h>
 #include <stdio.h>
 #include <sys/param.h>
 #include <paths.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+/*
+ * Editline and history functions (and glue).
+ */
 #include "shell.h"
 #include "parser.h"
 #include "var.h"
 #include "options.h"
 #include "shell.h"
 #include "parser.h"
 #include "var.h"
 #include "options.h"
+#include "main.h"
+#include "output.h"
 #include "mystring.h"
 #include "mystring.h"
+#ifndef NO_HISTORY
+#include "myhistedit.h"
+#endif
 #include "error.h"
 #include "error.h"
-#include "histedit.h"
+#include "eval.h"
 #include "memalloc.h"
 
 #define MAXHISTLOOPS   4       /* max recursions through fc */
 #include "memalloc.h"
 
 #define MAXHISTLOOPS   4       /* max recursions through fc */
@@ -27,7 +74,9 @@ STATIC char *fc_replace __P((const char *, char *, char *));
  * Set history and editing status.  Called whenever the status may
  * have changed (figures out what to do).
  */
  * Set history and editing status.  Called whenever the status may
  * have changed (figures out what to do).
  */
-histedit() {
+void
+histedit() 
+{
 
 #define editing (Eflag || Vflag)
 
 
 #define editing (Eflag || Vflag)
 
@@ -92,7 +141,10 @@ bad:
        }
 }
 
        }
 }
 
-sethistsize() {
+
+void
+sethistsize()
+{
        char *cp;
        int histsize;
 
        char *cp;
        int histsize;
 
@@ -109,8 +161,10 @@ sethistsize() {
  *  This command is provided since POSIX decided to standardize
  *  the Korn shell fc command.  Oh well...
  */
  *  This command is provided since POSIX decided to standardize
  *  the Korn shell fc command.  Oh well...
  */
+int
 histcmd(argc, argv)
 histcmd(argc, argv)
-       char *argv[];
+       int argc;
+       char **argv;
 {
        extern char *optarg;
        extern int optind, optopt, optreset;
 {
        extern char *optarg;
        extern int optind, optopt, optreset;
@@ -127,9 +181,27 @@ histcmd(argc, argv)
        struct jmploc *volatile savehandler;
        char editfile[MAXPATHLEN + 1];
        FILE *efp;
        struct jmploc *volatile savehandler;
        char editfile[MAXPATHLEN + 1];
        FILE *efp;
+#ifdef __GNUC__
+       /* Avoid longjmp clobbering */
+       (void) &editor;
+       (void) &lflg;
+       (void) &nflg;
+       (void) &rflg;
+       (void) &sflg;
+       (void) &firststr;
+       (void) &laststr;
+       (void) &pat;
+       (void) &repl;
+       (void) &efp;
+       (void) &argc;
+       (void) &argv;
+#endif
 
        if (hist == NULL)
                error("history not active");
 
        if (hist == NULL)
                error("history not active");
+       
+       if (argc == 1)
+               error("missing history argument");
 
        optreset = 1; optind = 1; /* initialize getopt */
        while (not_fcnumber(argv[optind]) &&
 
        optreset = 1; optind = 1; /* initialize getopt */
        while (not_fcnumber(argv[optind]) &&
@@ -315,6 +387,7 @@ histcmd(argc, argv)
                --active;
        if (displayhist)
                displayhist = 0;
                --active;
        if (displayhist)
                displayhist = 0;
+       return 0;
 }
 
 STATIC char *
 }
 
 STATIC char *
@@ -341,14 +414,18 @@ fc_replace(s, p, r)
        return (dest);
 }
 
        return (dest);
 }
 
+int
 not_fcnumber(s)
         char *s;
 {
 not_fcnumber(s)
         char *s;
 {
+       if (s == NULL)
+               return 0;
         if (*s == '-')
                 s++;
        return (!is_number(s));
 }
 
         if (*s == '-')
                 s++;
        return (!is_number(s));
 }
 
+int
 str_to_event(str, last)
        char *str;
        int last;
 str_to_event(str, last)
        char *str;
        int last;
@@ -356,7 +433,7 @@ str_to_event(str, last)
        const HistEvent *he;
        char *s = str;
        int relative = 0;
        const HistEvent *he;
        char *s = str;
        int relative = 0;
-       int i, j;
+       int i;
 
        he = history(hist, H_FIRST);
        switch (*s) {
 
        he = history(hist, H_FIRST);
        switch (*s) {
@@ -391,7 +468,7 @@ str_to_event(str, last)
                /*
                 * pattern 
                 */
                /*
                 * pattern 
                 */
-               he = history(hist, H_NEXT_STR, str);
+               he = history(hist, H_PREV_STR, str);
                if (he == NULL)
                        error("history pattern not found: %s", str);
        }
                if (he == NULL)
                        error("history pattern not found: %s", str);
        }