hist.c revision 1.1.1.2
11.1Scgd/*-
21.1.1.2Smycroft * Copyright (c) 1980, 1991, 1993
31.1.1.2Smycroft *	The Regents of the University of California.  All rights reserved.
41.1Scgd *
51.1Scgd * Redistribution and use in source and binary forms, with or without
61.1Scgd * modification, are permitted provided that the following conditions
71.1Scgd * are met:
81.1Scgd * 1. Redistributions of source code must retain the above copyright
91.1Scgd *    notice, this list of conditions and the following disclaimer.
101.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
111.1Scgd *    notice, this list of conditions and the following disclaimer in the
121.1Scgd *    documentation and/or other materials provided with the distribution.
131.1Scgd * 3. All advertising materials mentioning features or use of this software
141.1Scgd *    must display the following acknowledgement:
151.1Scgd *	This product includes software developed by the University of
161.1Scgd *	California, Berkeley and its contributors.
171.1Scgd * 4. Neither the name of the University nor the names of its contributors
181.1Scgd *    may be used to endorse or promote products derived from this software
191.1Scgd *    without specific prior written permission.
201.1Scgd *
211.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311.1Scgd * SUCH DAMAGE.
321.1Scgd */
331.1Scgd
341.1Scgd#ifndef lint
351.1.1.2Smycroftstatic char sccsid[] = "@(#)hist.c	8.1 (Berkeley) 5/31/93";
361.1Scgd#endif /* not lint */
371.1Scgd
381.1Scgd#include <sys/types.h>
391.1Scgd#include <stdlib.h>
401.1Scgd#if __STDC__
411.1Scgd# include <stdarg.h>
421.1Scgd#else
431.1Scgd# include <varargs.h>
441.1Scgd#endif
451.1Scgd
461.1Scgd#include "csh.h"
471.1Scgd#include "extern.h"
481.1Scgd
491.1Scgdstatic void	hfree __P((struct Hist *));
501.1.1.2Smycroftstatic void	dohist1 __P((struct Hist *, int *, int, int));
511.1.1.2Smycroftstatic void	phist __P((struct Hist *, int));
521.1Scgd
531.1Scgdvoid
541.1Scgdsavehist(sp)
551.1Scgd    struct wordent *sp;
561.1Scgd{
571.1Scgd    register struct Hist *hp, *np;
581.1Scgd    register int histlen = 0;
591.1Scgd    Char   *cp;
601.1Scgd
611.1Scgd    /* throw away null lines */
621.1Scgd    if (sp->next->word[0] == '\n')
631.1Scgd	return;
641.1Scgd    cp = value(STRhistory);
651.1Scgd    if (*cp) {
661.1Scgd	register Char *p = cp;
671.1Scgd
681.1Scgd	while (*p) {
691.1Scgd	    if (!Isdigit(*p)) {
701.1Scgd		histlen = 0;
711.1Scgd		break;
721.1Scgd	    }
731.1Scgd	    histlen = histlen * 10 + *p++ - '0';
741.1Scgd	}
751.1Scgd    }
761.1.1.2Smycroft    for (hp = &Histlist; (np = hp->Hnext) != NULL;)
771.1Scgd	if (eventno - np->Href >= histlen || histlen == 0)
781.1Scgd	    hp->Hnext = np->Hnext, hfree(np);
791.1Scgd	else
801.1Scgd	    hp = np;
811.1Scgd    (void) enthist(++eventno, sp, 1);
821.1Scgd}
831.1Scgd
841.1Scgdstruct Hist *
851.1Scgdenthist(event, lp, docopy)
861.1Scgd    int     event;
871.1Scgd    register struct wordent *lp;
881.1Scgd    bool    docopy;
891.1Scgd{
901.1Scgd    register struct Hist *np;
911.1Scgd
921.1Scgd    np = (struct Hist *) xmalloc((size_t) sizeof(*np));
931.1Scgd    np->Hnum = np->Href = event;
941.1Scgd    if (docopy) {
951.1Scgd	copylex(&np->Hlex, lp);
961.1Scgd    }
971.1Scgd    else {
981.1Scgd	np->Hlex.next = lp->next;
991.1Scgd	lp->next->prev = &np->Hlex;
1001.1Scgd	np->Hlex.prev = lp->prev;
1011.1Scgd	lp->prev->next = &np->Hlex;
1021.1Scgd    }
1031.1Scgd    np->Hnext = Histlist.Hnext;
1041.1Scgd    Histlist.Hnext = np;
1051.1Scgd    return (np);
1061.1Scgd}
1071.1Scgd
1081.1Scgdstatic void
1091.1Scgdhfree(hp)
1101.1Scgd    register struct Hist *hp;
1111.1Scgd{
1121.1Scgd
1131.1Scgd    freelex(&hp->Hlex);
1141.1Scgd    xfree((ptr_t) hp);
1151.1Scgd}
1161.1Scgd
1171.1Scgdvoid
1181.1.1.2Smycroft/*ARGSUSED*/
1191.1.1.2Smycroftdohist(v, t)
1201.1.1.2Smycroft    Char **v;
1211.1.1.2Smycroft    struct command *t;
1221.1Scgd{
1231.1.1.2Smycroft    int     n, rflg = 0, hflg = 0;
1241.1Scgd
1251.1Scgd    if (getn(value(STRhistory)) == 0)
1261.1Scgd	return;
1271.1Scgd    if (setintr)
1281.1Scgd	(void) sigsetmask(sigblock((sigset_t) 0) & ~sigmask(SIGINT));
1291.1.1.2Smycroft    while (*++v && **v == '-') {
1301.1.1.2Smycroft	Char   *vp = *v;
1311.1Scgd
1321.1.1.2Smycroft	while (*++vp)
1331.1.1.2Smycroft	    switch (*vp) {
1341.1Scgd	    case 'h':
1351.1Scgd		hflg++;
1361.1Scgd		break;
1371.1Scgd	    case 'r':
1381.1Scgd		rflg++;
1391.1Scgd		break;
1401.1Scgd	    case '-':		/* ignore multiple '-'s */
1411.1Scgd		break;
1421.1Scgd	    default:
1431.1Scgd		stderror(ERR_HISTUS);
1441.1Scgd		break;
1451.1Scgd	    }
1461.1Scgd    }
1471.1.1.2Smycroft    if (*v)
1481.1.1.2Smycroft	n = getn(*v);
1491.1Scgd    else {
1501.1Scgd	n = getn(value(STRhistory));
1511.1Scgd    }
1521.1.1.2Smycroft    dohist1(Histlist.Hnext, &n, rflg, hflg);
1531.1Scgd}
1541.1Scgd
1551.1Scgdstatic void
1561.1.1.2Smycroftdohist1(hp, np, rflg, hflg)
1571.1Scgd    struct Hist *hp;
1581.1.1.2Smycroft    int    *np, rflg, hflg;
1591.1Scgd{
1601.1Scgd    bool    print = (*np) > 0;
1611.1Scgd
1621.1Scgd    for (; hp != 0; hp = hp->Hnext) {
1631.1Scgd	(*np)--;
1641.1Scgd	hp->Href++;
1651.1Scgd	if (rflg == 0) {
1661.1.1.2Smycroft	    dohist1(hp->Hnext, np, rflg, hflg);
1671.1Scgd	    if (print)
1681.1.1.2Smycroft		phist(hp, hflg);
1691.1Scgd	    return;
1701.1Scgd	}
1711.1Scgd	if (*np >= 0)
1721.1.1.2Smycroft	    phist(hp, hflg);
1731.1Scgd    }
1741.1Scgd}
1751.1Scgd
1761.1Scgdstatic void
1771.1.1.2Smycroftphist(hp, hflg)
1781.1Scgd    register struct Hist *hp;
1791.1.1.2Smycroft    int     hflg;
1801.1Scgd{
1811.1.1.2Smycroft    if (hflg == 0)
1821.1.1.2Smycroft	(void) fprintf(cshout, "%6d\t", hp->Hnum);
1831.1.1.2Smycroft    prlex(cshout, &hp->Hlex);
1841.1Scgd}
185