diff options
author | mp <mp@FreeBSD.org> | 2007-03-11 22:33:41 +0000 |
---|---|---|
committer | mp <mp@FreeBSD.org> | 2007-03-11 22:33:41 +0000 |
commit | a40980339b13e3b506c2317b5b4864127039eb2c (patch) | |
tree | 34aefea92d30b614247ef1f2671f2362f4761785 /contrib/tcsh/sh.hist.c | |
parent | 32837fb336d4709f0a121130a3a78f29be0db5ed (diff) | |
download | FreeBSD-src-a40980339b13e3b506c2317b5b4864127039eb2c.zip FreeBSD-src-a40980339b13e3b506c2317b5b4864127039eb2c.tar.gz |
Import of tcsh-6.15.00
Diffstat (limited to 'contrib/tcsh/sh.hist.c')
-rw-r--r-- | contrib/tcsh/sh.hist.c | 185 |
1 files changed, 80 insertions, 105 deletions
diff --git a/contrib/tcsh/sh.hist.c b/contrib/tcsh/sh.hist.c index a108747..72b376a 100644 --- a/contrib/tcsh/sh.hist.c +++ b/contrib/tcsh/sh.hist.c @@ -1,4 +1,4 @@ -/* $Header: /src/pub/tcsh/sh.hist.c,v 3.33 2004/12/25 21:15:07 christos Exp $ */ +/* $Header: /p/tcsh/cvsroot/tcsh/sh.hist.c,v 3.40 2007/03/01 17:14:51 christos Exp $ */ /* * sh.hist.c: Shell history expansions and substitutions */ @@ -32,18 +32,18 @@ */ #include "sh.h" -RCSID("$Id: sh.hist.c,v 3.33 2004/12/25 21:15:07 christos Exp $") +RCSID("$tcsh: sh.hist.c,v 3.40 2007/03/01 17:14:51 christos Exp $") #include "tc.h" extern int histvalid; -extern Char histline[]; +extern struct Strbuf histline; Char HistLit = 0; -static int heq __P((struct wordent *, struct wordent *)); -static void hfree __P((struct Hist *)); -static void dohist1 __P((struct Hist *, int *, int)); -static void phist __P((struct Hist *, int)); +static int heq (const struct wordent *, const struct wordent *); +static void hfree (struct Hist *); +static void dohist1 (struct Hist *, int *, int); +static void phist (struct Hist *, int); #define HIST_ONLY 0x01 #define HIST_SAVE 0x02 @@ -58,9 +58,7 @@ static void phist __P((struct Hist *, int)); */ void -savehist(sp, mflg) - struct wordent *sp; - int mflg; +savehist(struct wordent *sp, int mflg) { struct Hist *hp, *np; int histlen = 0; @@ -70,16 +68,12 @@ savehist(sp, mflg) if (sp && sp->next->word[0] == '\n') return; cp = varval(STRhistory); - if (*cp) { - Char *p = cp; - - while (*p) { - if (!Isdigit(*p)) { - histlen = 0; - break; - } - histlen = histlen * 10 + *p++ - '0'; + while (*cp) { + if (!Isdigit(*cp)) { + histlen = 0; + break; } + histlen = histlen * 10 + *cp++ - '0'; } if (sp) (void) enthist(++eventno, sp, 1, mflg); @@ -91,10 +85,9 @@ savehist(sp, mflg) } static int -heq(a0, b0) - struct wordent *a0, *b0; +heq(const struct wordent *a0, const struct wordent *b0) { - struct wordent *a = a0->next, *b = b0->next; + const struct wordent *a = a0->next, *b = b0->next; for (;;) { if (Strcmp(a->word, b->word) != 0) @@ -110,16 +103,12 @@ heq(a0, b0) struct Hist * -enthist(event, lp, docopy, mflg) - int event; - struct wordent *lp; - int docopy; - int mflg; +enthist(int event, struct wordent *lp, int docopy, int mflg) { struct Hist *p = NULL, *pp = &Histlist; int n, r; struct Hist *np; - Char *dp; + const Char *dp; if ((dp = varval(STRhistdup)) != STRNULL) { if (eq(dp, STRerase)) { @@ -152,10 +141,10 @@ enthist(event, lp, docopy, mflg) } } - np = p ? p : (struct Hist *) xmalloc((size_t) sizeof(*np)); + np = p ? p : xmalloc(sizeof(*np)); /* Pick up timestamp set by lex() in Htime if reading saved history */ - if (Htime != (time_t) 0) { + if (Htime != 0) { np->Htime = Htime; Htime = 0; } @@ -169,7 +158,7 @@ enthist(event, lp, docopy, mflg) if (docopy) { copylex(&np->Hlex, lp); if (histvalid) - np->histline = Strsave(histline); + np->histline = Strsave(histline.s); else np->histline = NULL; } @@ -208,34 +197,25 @@ enthist(event, lp, docopy, mflg) } static void -hfree(hp) - struct Hist *hp; +hfree(struct Hist *hp) { freelex(&hp->Hlex); if (hp->histline) - xfree((ptr_t) hp->histline); - xfree((ptr_t) hp); + xfree(hp->histline); + xfree(hp); } /*ARGSUSED*/ void -dohist(vp, c) - Char **vp; - struct command *c; +dohist(Char **vp, struct command *c) { int n, hflg = 0; USE(c); if (getn(varval(STRhistory)) == 0) return; - if (setintr) -#ifdef BSDSIGS - (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT)); -#else - (void) sigrelse(SIGINT); -#endif while (*++vp && **vp == '-') { Char *vp2 = *vp; @@ -274,30 +254,32 @@ dohist(vp, c) hp->Hnext = np->Hnext, hfree(np); } - if (hflg & (HIST_LOAD | HIST_MERGE)) { + if (hflg & (HIST_LOAD | HIST_MERGE)) loadhist(*vp, (hflg & HIST_MERGE) ? 1 : 0); - return; - } - else if (hflg & HIST_SAVE) { + else if (hflg & HIST_SAVE) rechist(*vp, 1); - return; - } - if (*vp) - n = getn(*vp); else { - n = getn(varval(STRhistory)); + if (*vp) + n = getn(*vp); + else { + n = getn(varval(STRhistory)); + } + dohist1(Histlist.Hnext, &n, hflg); } - dohist1(Histlist.Hnext, &n, hflg); } static void -dohist1(hp, np, hflg) - struct Hist *hp; - int *np, hflg; +dohist1(struct Hist *hp, int *np, int hflg) { int print = (*np) > 0; for (; hp != 0; hp = hp->Hnext) { + if (setintr) { + int old_pintr_disabled; + + pintr_push_enable(&old_pintr_disabled); + cleanup_until(&old_pintr_disabled); + } (*np)--; if ((hflg & HIST_REV) == 0) { dohist1(hp->Hnext, np, hflg); @@ -311,93 +293,86 @@ dohist1(hp, np, hflg) } static void -phist(hp, hflg) - struct Hist *hp; - int hflg; +phist(struct Hist *hp, int hflg) { if (hflg & HIST_ONLY) { + int old_output_raw; + /* * Control characters have to be written as is (output_raw). * This way one can preserve special characters (like tab) in * the history file. * From: mveksler@vnet.ibm.com (Veksler Michael) */ - output_raw= 1; + old_output_raw = output_raw; + output_raw = 1; + cleanup_push(&old_output_raw, output_raw_restore); if (hflg & HIST_TIME) /* * Make file entry with history time in format: * "+NNNNNNNNNN" (10 digits, left padded with ascii '0') */ - xprintf("#+%010lu\n", hp->Htime); + xprintf("#+%010lu\n", (unsigned long)hp->Htime); if (HistLit && hp->histline) xprintf("%S\n", hp->histline); else prlex(&hp->Hlex); - output_raw= 0; + cleanup_until(&old_output_raw); } else { Char *cp = str2short("%h\t%T\t%R\n"); - Char buf[INBUFSIZE]; + Char *p; struct varent *vp = adrof(STRhistory); if (vp && vp->vec != NULL && vp->vec[0] && vp->vec[1]) cp = vp->vec[1]; - tprintf(FMT_HISTORY, buf, cp, INBUFSIZE, NULL, hp->Htime, (ptr_t) hp); - for (cp = buf; *cp;) + p = tprintf(FMT_HISTORY, cp, NULL, hp->Htime, hp); + cleanup_push(p, xfree); + for (cp = p; *cp;) xputwchar(*cp++); + cleanup_until(p); } } -void -fmthist(fmt, ptr, buf, bufsiz) - int fmt; - ptr_t ptr; - char *buf; - size_t bufsiz; +char * +fmthist(int fmt, ptr_t ptr) { - struct Hist *hp = (struct Hist *) ptr; + struct Hist *hp = ptr; + char *buf; + switch (fmt) { case 'h': - (void) xsnprintf(buf, bufsiz, "%6d", hp->Hnum); - break; + return xasprintf("%6d", hp->Hnum); case 'R': if (HistLit && hp->histline) - (void) xsnprintf(buf, bufsiz, "%S", hp->histline); + return xasprintf("%S", hp->histline); else { - Char ibuf[INBUFSIZE], *ip; + Char *istr, *ip; char *p; - (void) sprlex(ibuf, sizeof(ibuf) / sizeof(Char), &hp->Hlex); - p = buf; - ip = ibuf; - do { - char xbuf[MB_LEN_MAX]; - size_t len; - - len = one_wctomb(xbuf, CHAR & *ip); - if ((size_t)((p - buf) + len) >= bufsiz) - break; - memcpy(p, xbuf, len); - p += len; - } while ((CHAR & *ip++) != 0); - if (p <= buf + bufsiz - 1) - *p = '\0'; + + istr = sprlex(&hp->Hlex); + buf = xmalloc(Strlen(istr) * MB_LEN_MAX + 1); + + for (p = buf, ip = istr; *ip != '\0'; ip++) + p += one_wctomb(p, CHAR & *ip); + + *p = '\0'; + xfree(istr); + return buf; } - break; default: + buf = xmalloc(1); buf[0] = '\0'; - break; + return buf; } - } void -rechist(fname, ref) - Char *fname; - int ref; +rechist(Char *fname, int ref) { Char *snum; int fp, ftmp, oldidfds; @@ -423,10 +398,11 @@ rechist(fname, ref) } else fname = globone(fname, G_ERROR); + cleanup_push(fname, xfree); /* * The 'savehist merge' feature is intended for an environment - * with numerous shells beeing in simultaneous use. Imagine + * with numerous shells being in simultaneous use. Imagine * any kind of window system. All these shells 'share' the same * ~/.history file for recording their command line history. * Currently the automatic merge can only succeed when the shells @@ -448,26 +424,25 @@ rechist(fname, ref) if ((shist = adrof(STRsavehist)) != NULL && shist->vec != NULL) if (shist->vec[1] && eq(shist->vec[1], STRmerge)) loadhist(fname, 1); - fp = creat(short2str(fname), 0600); + fp = xcreat(short2str(fname), 0600); if (fp == -1) { didfds = oldidfds; + cleanup_until(fname); return; } ftmp = SHOUT; SHOUT = fp; dumphist[2] = snum; dohist(dumphist, NULL); - (void) close(fp); + xclose(fp); SHOUT = ftmp; didfds = oldidfds; - xfree((ptr_t) fname); + cleanup_until(fname); } void -loadhist(fname, mflg) - Char *fname; - int mflg; +loadhist(Char *fname, int mflg) { static Char *loadhist_cmd[] = {STRsource, NULL, NULL, NULL}; loadhist_cmd[1] = mflg ? STRmm : STRmh; |