1 /* $Header: /src/pub/tcsh/sh.hist.c,v 3.27 2000/06/10 22:07:56 kim Exp $ */
3 * sh.hist.c: Shell history expansions and substitutions
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 RCSID("$Id: sh.hist.c,v 3.27 2000/06/10 22:07:56 kim Exp $")
43 extern bool histvalid;
44 extern Char histline[];
47 static bool heq __P((struct wordent *, struct wordent *));
48 static void hfree __P((struct Hist *));
49 static void dohist1 __P((struct Hist *, int *, int));
50 static void phist __P((struct Hist *, int));
52 #define HIST_ONLY 0x01
53 #define HIST_SAVE 0x02
54 #define HIST_LOAD 0x04
56 #define HIST_CLEAR 0x10
57 #define HIST_MERGE 0x20
58 #define HIST_TIME 0x40
69 register struct Hist *hp, *np;
70 register int histlen = 0;
73 /* throw away null lines */
74 if (sp && sp->next->word[0] == '\n')
76 cp = varval(STRhistory);
78 register Char *p = cp;
85 histlen = histlen * 10 + *p++ - '0';
89 (void) enthist(++eventno, sp, 1, mflg);
90 for (hp = &Histlist; (np = hp->Hnext) != NULL;)
91 if (eventno - np->Href >= histlen || histlen == 0)
92 hp->Hnext = np->Hnext, hfree(np);
99 struct wordent *a0, *b0;
101 struct wordent *a = a0->next, *b = b0->next;
104 if (Strcmp(a->word, b->word) != 0)
109 return (b == b0) ? 1 : 0;
117 enthist(event, lp, docopy, mflg)
119 register struct wordent *lp;
124 struct Hist *p = NULL, *pp = &Histlist;
126 register struct Hist *np;
129 if ((dp = varval(STRhistdup)) != STRNULL) {
130 if (eq(dp, STRerase)) {
131 /* masaoki@akebono.tky.hp.com (Kobayashi Masaoki) */
133 for (p = pp; (px = p, p = p->Hnext) != NULL;)
134 if (heq(lp, &(p->Hlex))){
135 px->Hnext = p->Hnext;
136 if (Htime != 0 && p->Htime > Htime)
140 for (p = px->Hnext; p != NULL; p = p->Hnext)
145 else if (eq(dp, STRall)) {
146 for (p = pp; (p = p->Hnext) != NULL;)
147 if (heq(lp, &(p->Hlex))) {
152 else if (eq(dp, STRprev)) {
153 if (pp->Hnext && heq(lp, &(pp->Hnext->Hlex))) {
160 np = p ? p : (struct Hist *) xmalloc((size_t) sizeof(*np));
162 /* Pick up timestamp set by lex() in Htime if reading saved history */
163 if (Htime != (time_t) 0) {
168 (void) time(&(np->Htime));
173 np->Hnum = np->Href = event;
175 copylex(&np->Hlex, lp);
177 np->histline = Strsave(histline);
182 np->Hlex.next = lp->next;
183 lp->next->prev = &np->Hlex;
184 np->Hlex.prev = lp->prev;
185 lp->prev->next = &np->Hlex;
190 while ((p = pp->Hnext) && (p->Htime > np->Htime))
192 while (p && p->Htime == np->Htime)
194 if (heq(&p->Hlex, &np->Hlex))
203 for (p = Histlist.Hnext; p != pp->Hnext; p = p->Hnext)
205 n = p->Hnum; r = p->Href;
206 p->Hnum = np->Hnum; p->Href = np->Href;
207 np->Hnum = n; np->Href = r;
210 np->Hnext = pp->Hnext;
217 register struct Hist *hp;
222 xfree((ptr_t) hp->histline);
236 if (getn(varval(STRhistory)) == 0)
240 (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT));
242 (void) sigrelse(SIGINT);
244 while (*++vp && **vp == '-') {
271 stderror(ERR_HISTUS, "chrSLMT");
276 if (hflg & HIST_CLEAR) {
277 struct Hist *np, *hp;
278 for (hp = &Histlist; (np = hp->Hnext) != NULL;)
279 hp->Hnext = np->Hnext, hfree(np);
282 if (hflg & (HIST_LOAD | HIST_MERGE)) {
283 loadhist(*vp, (hflg & HIST_MERGE) ? 1 : 0);
286 else if (hflg & HIST_SAVE) {
293 n = getn(varval(STRhistory));
295 dohist1(Histlist.Hnext, &n, hflg);
299 dohist1(hp, np, hflg)
303 bool print = (*np) > 0;
305 for (; hp != 0; hp = hp->Hnext) {
307 if ((hflg & HIST_REV) == 0) {
308 dohist1(hp->Hnext, np, hflg);
320 register struct Hist *hp;
323 extern bool output_raw;
324 if (hflg & HIST_ONLY) {
326 * Control characters have to be written as is (output_raw).
327 * This way one can preserve special characters (like tab) in
329 * From: mveksler@vnet.ibm.com (Veksler Michael)
332 if (hflg & HIST_TIME)
334 * Make file entry with history time in format:
335 * "+NNNNNNNNNN" (10 digits, left padded with ascii '0')
338 xprintf("#+%010lu\n", hp->Htime);
340 if (HistLit && hp->histline)
341 xprintf("%S\n", hp->histline);
347 Char *cp = str2short("%h\t%T\t%R\n");
349 struct varent *vp = adrof(STRhistory);
351 if (vp && vp->vec[0] && vp->vec[1])
354 tprintf(FMT_HISTORY, buf, cp, INBUFSIZE, NULL, hp->Htime, (ptr_t) hp);
362 fmthist(fmt, ptr, buf, bufsiz)
368 struct Hist *hp = (struct Hist *) ptr;
371 (void) xsnprintf(buf, bufsiz, "%6d", hp->Hnum);
374 if (HistLit && hp->histline)
375 (void) xsnprintf(buf, bufsiz, "%S", hp->histline);
377 Char ibuf[INBUFSIZE], *ip;
379 (void) sprlex(ibuf, sizeof(ibuf) / sizeof(Char), &hp->Hlex);
380 for (p = buf, ip = ibuf; (*p++ = (CHAR & *ip++)) != '\0'; )
397 int fp, ftmp, oldidfds;
398 struct varent *shist;
399 static Char *dumphist[] = {STRhistory, STRmhT, 0, 0};
401 if (fname == NULL && !ref)
404 * If $savehist is just set, we use the value of $history
405 * else we use the value in $savehist
407 if (((snum = varval(STRsavehist)) == STRNULL) &&
408 ((snum = varval(STRhistory)) == STRNULL))
413 if ((fname = varval(STRhistfile)) == STRNULL)
414 fname = Strspl(varval(STRhome), &STRtildothist[1]);
416 fname = Strsave(fname);
419 fname = globone(fname, G_ERROR);
422 * The 'savehist merge' feature is intended for an environment
423 * with numerous shells beeing in simultaneous use. Imagine
424 * any kind of window system. All these shells 'share' the same
425 * ~/.history file for recording their command line history.
426 * Currently the automatic merge can only succeed when the shells
427 * nicely quit one after another.
429 * Users that like to nuke their environment require here an atomic
430 * loadhist-creat-dohist(dumphist)-close
436 * We need the didfds stuff before loadhist otherwise
437 * exec in a script will fail to print if merge is set.
438 * From: mveksler@iil.intel.com (Veksler Michael)
442 if ((shist = adrof(STRsavehist)) != NULL)
443 if (shist->vec[1] && eq(shist->vec[1], STRmerge))
445 fp = creat(short2str(fname), 0600);
453 dohist(dumphist, NULL);
457 xfree((ptr_t) fname);
462 loadhist(fname, mflg)
466 static Char *loadhist_cmd[] = {STRsource, NULL, NULL, NULL};
467 loadhist_cmd[1] = mflg ? STRmm : STRmh;
470 loadhist_cmd[2] = fname;
471 else if ((fname = varval(STRhistfile)) != STRNULL)
472 loadhist_cmd[2] = fname;
474 loadhist_cmd[2] = STRtildothist;
476 dosource(loadhist_cmd, NULL);