| 1 | /*- |
| 2 | * Copyright (c) 1980, 1991 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #ifndef lint |
| 35 | static char sccsid[] = "@(#)hist.c 5.9 (Berkeley) 6/8/91"; |
| 36 | #endif /* not lint */ |
| 37 | |
| 38 | #include <sys/types.h> |
| 39 | #include <stdlib.h> |
| 40 | #if __STDC__ |
| 41 | # include <stdarg.h> |
| 42 | #else |
| 43 | # include <varargs.h> |
| 44 | #endif |
| 45 | |
| 46 | #include "csh.h" |
| 47 | #include "extern.h" |
| 48 | |
| 49 | static void hfree __P((struct Hist *)); |
| 50 | static void dohist1 __P((struct Hist *, int *, int, int, int)); |
| 51 | static void phist __P((struct Hist *, int, int)); |
| 52 | |
| 53 | void |
| 54 | savehist(sp) |
| 55 | struct wordent *sp; |
| 56 | { |
| 57 | register struct Hist *hp, *np; |
| 58 | register int histlen = 0; |
| 59 | Char *cp; |
| 60 | |
| 61 | /* throw away null lines */ |
| 62 | if (sp->next->word[0] == '\n') |
| 63 | return; |
| 64 | cp = value(STRhistory); |
| 65 | if (*cp) { |
| 66 | register Char *p = cp; |
| 67 | |
| 68 | while (*p) { |
| 69 | if (!Isdigit(*p)) { |
| 70 | histlen = 0; |
| 71 | break; |
| 72 | } |
| 73 | histlen = histlen * 10 + *p++ - '0'; |
| 74 | } |
| 75 | } |
| 76 | for (hp = &Histlist; np = hp->Hnext;) |
| 77 | if (eventno - np->Href >= histlen || histlen == 0) |
| 78 | hp->Hnext = np->Hnext, hfree(np); |
| 79 | else |
| 80 | hp = np; |
| 81 | (void) enthist(++eventno, sp, 1); |
| 82 | } |
| 83 | |
| 84 | struct Hist * |
| 85 | enthist(event, lp, docopy) |
| 86 | int event; |
| 87 | register struct wordent *lp; |
| 88 | bool docopy; |
| 89 | { |
| 90 | register struct Hist *np; |
| 91 | |
| 92 | np = (struct Hist *) xmalloc((size_t) sizeof(*np)); |
| 93 | (void) time(&(np->Htime)); |
| 94 | np->Hnum = np->Href = event; |
| 95 | if (docopy) { |
| 96 | copylex(&np->Hlex, lp); |
| 97 | } |
| 98 | else { |
| 99 | np->Hlex.next = lp->next; |
| 100 | lp->next->prev = &np->Hlex; |
| 101 | np->Hlex.prev = lp->prev; |
| 102 | lp->prev->next = &np->Hlex; |
| 103 | } |
| 104 | np->Hnext = Histlist.Hnext; |
| 105 | Histlist.Hnext = np; |
| 106 | return (np); |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | hfree(hp) |
| 111 | register struct Hist *hp; |
| 112 | { |
| 113 | |
| 114 | freelex(&hp->Hlex); |
| 115 | xfree((ptr_t) hp); |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | dohist(vp) |
| 120 | Char **vp; |
| 121 | { |
| 122 | int n, rflg = 0, hflg = 0, tflg = 0; |
| 123 | |
| 124 | if (getn(value(STRhistory)) == 0) |
| 125 | return; |
| 126 | if (setintr) |
| 127 | (void) sigsetmask(sigblock((sigset_t) 0) & ~sigmask(SIGINT)); |
| 128 | while (*++vp && **vp == '-') { |
| 129 | Char *vp2 = *vp; |
| 130 | |
| 131 | while (*++vp2) |
| 132 | switch (*vp2) { |
| 133 | case 'h': |
| 134 | hflg++; |
| 135 | break; |
| 136 | case 'r': |
| 137 | rflg++; |
| 138 | break; |
| 139 | case 't': |
| 140 | tflg++; |
| 141 | break; |
| 142 | case '-': /* ignore multiple '-'s */ |
| 143 | break; |
| 144 | default: |
| 145 | stderror(ERR_HISTUS); |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | if (*vp) |
| 150 | n = getn(*vp); |
| 151 | else { |
| 152 | n = getn(value(STRhistory)); |
| 153 | } |
| 154 | dohist1(Histlist.Hnext, &n, rflg, hflg, tflg); |
| 155 | } |
| 156 | |
| 157 | static void |
| 158 | dohist1(hp, np, rflg, hflg, tflg) |
| 159 | struct Hist *hp; |
| 160 | int *np, rflg, hflg, tflg; |
| 161 | { |
| 162 | bool print = (*np) > 0; |
| 163 | |
| 164 | for (; hp != 0; hp = hp->Hnext) { |
| 165 | (*np)--; |
| 166 | hp->Href++; |
| 167 | if (rflg == 0) { |
| 168 | dohist1(hp->Hnext, np, rflg, hflg, tflg); |
| 169 | if (print) |
| 170 | phist(hp, hflg, tflg); |
| 171 | return; |
| 172 | } |
| 173 | if (*np >= 0) |
| 174 | phist(hp, hflg, tflg); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static void |
| 179 | phist(hp, hflg, tflg) |
| 180 | register struct Hist *hp; |
| 181 | int hflg, tflg; |
| 182 | { |
| 183 | struct tm *t; |
| 184 | char ampm = 'a'; |
| 185 | |
| 186 | if (hflg == 0) { |
| 187 | xprintf("%6d\t", hp->Hnum); |
| 188 | if (tflg == 0) { |
| 189 | t = localtime(&hp->Htime); |
| 190 | if (adrof(STRampm)) { /* addition by Hans J. Albertsson */ |
| 191 | if (t->tm_hour >= 12) { |
| 192 | if (t->tm_hour > 12) |
| 193 | t->tm_hour -= 12; |
| 194 | ampm = 'p'; |
| 195 | } |
| 196 | else if (t->tm_hour == 0) |
| 197 | t->tm_hour = 12; |
| 198 | xprintf("%2d:%02d%cm\t", t->tm_hour, t->tm_min, ampm); |
| 199 | } |
| 200 | else { |
| 201 | xprintf("%2d:%02d\t", t->tm_hour, t->tm_min); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | prlex(&hp->Hlex); |
| 206 | } |