we've only got one tty discipline now.
[unix-history] / usr / src / bin / csh / hist.c
index 10437c2..1eb195d 100644 (file)
@@ -1,4 +1,12 @@
-static char *sccsid = "@(#)hist.c 4.8 %G%";
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley Software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char *sccsid = "@(#)hist.c      5.3 (Berkeley) %G%";
+#endif
 
 #include "sh.h"
 
 
 #include "sh.h"
 
@@ -10,29 +18,30 @@ savehist(sp)
        struct wordent *sp;
 {
        register struct Hist *hp, *np;
        struct wordent *sp;
 {
        register struct Hist *hp, *np;
-       int histlen;
-       register char *cp;
+       register int histlen = 0;
+       char *cp;
 
 
-       cp = value("history");
-       if (*cp == 0)
-               histlen = 0;
-       else {
-               while (*cp && digit(*cp))
-                       cp++;
-               /* avoid a looping snafu */
-               if (*cp)
-                       set("history", "10");
-               histlen = getn(value("history"));
-       }
        /* throw away null lines */
        if (sp->next->word[0] == '\n')
                return;
        /* throw away null lines */
        if (sp->next->word[0] == '\n')
                return;
+       cp = value("history");
+       if (*cp) {
+               register char *p = cp;
+
+               while (*p) {
+                       if (!digit(*p)) {
+                               histlen = 0;
+                               break;
+                       }
+                       histlen = histlen * 10 + *p++ - '0';
+               }
+       }
        for (hp = &Histlist; np = hp->Hnext;)
                if (eventno - np->Href >= histlen || histlen == 0)
                        hp->Hnext = np->Hnext, hfree(np);
                else
                        hp = np;
        for (hp = &Histlist; np = hp->Hnext;)
                if (eventno - np->Href >= histlen || histlen == 0)
                        hp->Hnext = np->Hnext, hfree(np);
                else
                        hp = np;
-       enthist(++eventno, sp, 1);
+       (void) enthist(++eventno, sp, 1);
 }
 
 struct Hist *
 }
 
 struct Hist *
@@ -43,7 +52,7 @@ enthist(event, lp, docopy)
 {
        register struct Hist *np;
 
 {
        register struct Hist *np;
 
-       np = (struct Hist *) calloc(1, sizeof *np);
+       np = (struct Hist *) xalloc(sizeof *np);
        np->Hnum = np->Href = event;
        if (docopy)
                copylex(&np->Hlex, lp);
        np->Hnum = np->Href = event;
        if (docopy)
                copylex(&np->Hlex, lp);
@@ -73,14 +82,24 @@ dohist(vp)
        if (getn(value("history")) == 0)
                return;
        if (setintr)
        if (getn(value("history")) == 0)
                return;
        if (setintr)
-               sigsetmask(sigblock(0) & ~sigmask(SIGINT));
-       vp++;
-       while (*vp && *vp[0] == '-') {
-               if (*vp && eq(*vp, "-h"))
-                       hflg++;
-               else if (*vp && eq(*vp, "-r"))
-                       rflg++;
-               vp++;
+               (void) sigsetmask(sigblock(0L) & ~sigmask(SIGINT));
+       while (*++vp && **vp == '-') {
+               char *vp2 = *vp;
+               while (*++vp2)
+                       switch (*vp2) {
+                       case 'h':
+                               hflg++;
+                               break;
+                       case 'r':
+                               rflg++;
+                               break;
+                       case '-':       /* ignore multiple '-'s */
+                               break;
+                       default:
+                               printf("Unknown flag: -%c\n", *vp2);
+                               error("Usage: history [-rh] [# number of events]");
+                       }
        }
        if (*vp)
                n = getn(*vp);
        }
        if (*vp)
                n = getn(*vp);