import / small alignment of many arm includes
[minix3.git] / bin / ksh / history.c
blobe29aade44c6fc42c4b1588348be4bf08ed75fdbb
1 /* $NetBSD: history.c,v 1.11 2011/08/31 16:24:54 plunky Exp $ */
3 /*
4 * command history
6 * only implements in-memory history.
7 */
9 /*
10 * This file contains
11 * a) the original in-memory history mechanism
12 * b) a simple file saving history mechanism done by sjg@zen
13 * define EASY_HISTORY to get this
14 * c) a more complicated mechanism done by pc@hillside.co.uk
15 * that more closely follows the real ksh way of doing
16 * things. You need to have the mmap system call for this
17 * to work on your system
19 #include <sys/cdefs.h>
21 #ifndef lint
22 __RCSID("$NetBSD: history.c,v 1.11 2011/08/31 16:24:54 plunky Exp $");
23 #endif
26 #include "sh.h"
27 #include "ksh_stat.h"
29 #ifdef HISTORY
30 # ifdef EASY_HISTORY
32 # ifndef HISTFILE
33 # ifdef OS2
34 # define HISTFILE "history.ksh"
35 # else /* OS2 */
36 # define HISTFILE ".pdksh_history"
37 # endif /* OS2 */
38 # endif
40 # else
41 /* Defines and includes for the complicated case */
43 # include <sys/file.h>
44 # include <sys/mman.h>
47 * variables for handling the data file
49 static int histfd;
50 static int hsize;
52 static int hist_count_lines ARGS((unsigned char *, int));
53 static int hist_shrink ARGS((unsigned char *, int));
54 static unsigned char *hist_skip_back ARGS((unsigned char *,int *,int));
55 static void histload ARGS((Source *, unsigned char *, int));
56 static void histinsert ARGS((Source *, int, unsigned char *));
57 static void writehistfile ARGS((int, char *));
58 static int sprinkle ARGS((int));
60 # ifdef MAP_FILE
61 # define MAP_FLAGS (MAP_FILE|MAP_PRIVATE)
62 # else
63 # define MAP_FLAGS MAP_PRIVATE
64 # endif
66 # endif /* of EASY_HISTORY */
68 static int hist_execute ARGS((char *));
69 static int hist_replace ARGS((char **, const char *, const char *, int));
70 static char **hist_get ARGS((const char *, int, int));
71 static char **hist_get_newest ARGS((int));
72 static char **hist_get_oldest ARGS((void));
73 static void histbackup ARGS((void));
75 static char **current; /* current position in history[] */
76 static int curpos; /* current index in history[] */
77 static char *hname; /* current name of history file */
78 static int hstarted; /* set after hist_init() called */
79 static Source *hist_source;
82 int
83 c_fc(wp)
84 char **wp;
86 struct shf *shf;
87 struct temp UNINITIALIZED(*tf);
88 char *p, *editor = (char *) 0;
89 int gflag = 0, lflag = 0, nflag = 0, sflag = 0, rflag = 0;
90 int optc;
91 char *first = (char *) 0, *last = (char *) 0;
92 char **hfirst, **hlast, **hp;
94 if (hist_source == NULL) {
95 bi_errorf("not interactive");
96 return 1;
99 while ((optc = ksh_getopt(wp, &builtin_opt, "e:glnrs0,1,2,3,4,5,6,7,8,9,")) != EOF)
100 switch (optc) {
101 case 'e':
102 p = builtin_opt.optarg;
103 if (strcmp(p, "-") == 0)
104 sflag++;
105 else {
106 size_t len = strlen(p) + 4;
107 editor = str_nsave(p, len, ATEMP);
108 strlcat(editor, " $_", len);
110 break;
111 case 'g': /* non-at&t ksh */
112 gflag++;
113 break;
114 case 'l':
115 lflag++;
116 break;
117 case 'n':
118 nflag++;
119 break;
120 case 'r':
121 rflag++;
122 break;
123 case 's': /* posix version of -e - */
124 sflag++;
125 break;
126 /* kludge city - accept -num as -- -num (kind of) */
127 case '0': case '1': case '2': case '3': case '4':
128 case '5': case '6': case '7': case '8': case '9':
129 p = shf_smprintf("-%c%s",
130 optc, builtin_opt.optarg);
131 if (!first)
132 first = p;
133 else if (!last)
134 last = p;
135 else {
136 bi_errorf("too many arguments");
137 return 1;
139 break;
140 case '?':
141 return 1;
143 wp += builtin_opt.optind;
145 /* Substitute and execute command */
146 if (sflag) {
147 char *pat = (char *) 0, *rep = (char *) 0;
149 if (editor || lflag || nflag || rflag) {
150 bi_errorf("can't use -e, -l, -n, -r with -s (-e -)");
151 return 1;
154 /* Check for pattern replacement argument */
155 if (*wp && **wp && (p = strchr(*wp + 1, '='))) {
156 pat = str_save(*wp, ATEMP);
157 p = pat + (p - *wp);
158 *p++ = '\0';
159 rep = p;
160 wp++;
162 /* Check for search prefix */
163 if (!first && (first = *wp))
164 wp++;
165 if (last || *wp) {
166 bi_errorf("too many arguments");
167 return 1;
170 hp = first ? hist_get(first, FALSE, FALSE)
171 : hist_get_newest(FALSE);
172 if (!hp)
173 return 1;
174 return hist_replace(hp, pat, rep, gflag);
177 if (editor && (lflag || nflag)) {
178 bi_errorf("can't use -l, -n with -e");
179 return 1;
182 if (!first && (first = *wp))
183 wp++;
184 if (!last && (last = *wp))
185 wp++;
186 if (*wp) {
187 bi_errorf("too many arguments");
188 return 1;
190 if (!first) {
191 hfirst = lflag ? hist_get("-16", TRUE, TRUE)
192 : hist_get_newest(FALSE);
193 if (!hfirst)
194 return 1;
195 /* can't fail if hfirst didn't fail */
196 hlast = hist_get_newest(FALSE);
197 } else {
198 /* POSIX says not an error if first/last out of bounds
199 * when range is specified; at&t ksh and pdksh allow out of
200 * bounds for -l as well.
202 hfirst = hist_get(first, (lflag || last) ? TRUE : FALSE,
203 lflag ? TRUE : FALSE);
204 if (!hfirst)
205 return 1;
206 hlast = last ? hist_get(last, TRUE, lflag ? TRUE : FALSE)
207 : (lflag ? hist_get_newest(FALSE) : hfirst);
208 if (!hlast)
209 return 1;
211 if (hfirst > hlast) {
212 char **temp;
214 temp = hfirst; hfirst = hlast; hlast = temp;
215 rflag = !rflag; /* POSIX */
218 /* List history */
219 if (lflag) {
220 char *s, *t;
221 const char *nfmt = nflag ? "\t" : "%d\t";
223 for (hp = rflag ? hlast : hfirst;
224 hp >= hfirst && hp <= hlast; hp += rflag ? -1 : 1)
226 shf_fprintf(shl_stdout, nfmt,
227 hist_source->line - (int) (histptr - hp));
228 /* print multi-line commands correctly */
229 for (s = *hp; (t = strchr(s, '\n')); s = t)
230 shf_fprintf(shl_stdout, "%.*s\t", ++t - s, s);
231 shf_fprintf(shl_stdout, "%s\n", s);
233 shf_flush(shl_stdout);
234 return 0;
237 /* Run editor on selected lines, then run resulting commands */
239 tf = maketemp(ATEMP, TT_HIST_EDIT, &e->temps);
240 if (!(shf = tf->shf)) {
241 bi_errorf("cannot create temp file %s - %s",
242 tf->name, strerror(errno));
243 return 1;
245 for (hp = rflag ? hlast : hfirst;
246 hp >= hfirst && hp <= hlast; hp += rflag ? -1 : 1)
247 shf_fprintf(shf, "%s\n", *hp);
248 if (shf_close(shf) == EOF) {
249 bi_errorf("error writing temporary file - %s", strerror(errno));
250 return 1;
253 /* Ignore setstr errors here (arbitrary) */
254 setstr(local("_", FALSE), tf->name, KSH_RETURN_ERROR);
256 /* XXX: source should not get trashed by this.. */
258 Source *sold = source;
259 int ret;
261 ret = command(editor ? editor : "${FCEDIT:-/bin/ed} $_");
262 source = sold;
263 if (ret)
264 return ret;
268 struct stat statb;
269 XString xs;
270 char *xp;
271 int n;
273 if (!(shf = shf_open(tf->name, O_RDONLY, 0, 0))) {
274 bi_errorf("cannot open temp file %s", tf->name);
275 return 1;
278 n = fstat(shf_fileno(shf), &statb) < 0 ? 128
279 : statb.st_size + 1;
280 Xinit(xs, xp, n, hist_source->areap);
281 while ((n = shf_read(xp, Xnleft(xs, xp), shf)) > 0) {
282 xp += n;
283 if (Xnleft(xs, xp) <= 0)
284 XcheckN(xs, xp, Xlength(xs, xp));
286 if (n < 0) {
287 bi_errorf("error reading temp file %s - %s",
288 tf->name, strerror(shf_errno(shf)));
289 shf_close(shf);
290 return 1;
292 shf_close(shf);
293 *xp = '\0';
294 strip_nuls(Xstring(xs, xp), Xlength(xs, xp));
295 return hist_execute(Xstring(xs, xp));
299 /* Save cmd in history, execute cmd (cmd gets trashed) */
300 static int
301 hist_execute(cmd)
302 char *cmd;
304 Source *sold;
305 int ret;
306 char *p, *q;
308 histbackup();
310 for (p = cmd; p; p = q) {
311 if ((q = strchr(p, '\n'))) {
312 *q++ = '\0'; /* kill the newline */
313 if (!*q) /* ignore trailing newline */
314 q = (char *) 0;
316 #ifdef EASY_HISTORY
317 if (p != cmd)
318 histappend(p, TRUE);
319 else
320 #endif /* EASY_HISTORY */
321 histsave(++(hist_source->line), p, 1);
323 shellf("%s\n", p); /* POSIX doesn't say this is done... */
324 if ((p = q)) /* restore \n (trailing \n not restored) */
325 q[-1] = '\n';
328 /* Commands are executed here instead of pushing them onto the
329 * input 'cause posix says the redirection and variable assignments
330 * in
331 * X=y fc -e - 42 2> /dev/null
332 * are to effect the repeated commands environment.
334 /* XXX: source should not get trashed by this.. */
335 sold = source;
336 ret = command(cmd);
337 source = sold;
338 return ret;
341 static int
342 hist_replace(hp, pat, rep, globalv)
343 char **hp;
344 const char *pat;
345 const char *rep;
346 int globalv;
348 char *line;
350 if (!pat)
351 line = str_save(*hp, ATEMP);
352 else {
353 char *s, *s1;
354 int pat_len = strlen(pat);
355 int rep_len = strlen(rep);
356 int len;
357 XString xs;
358 char *xp;
359 int any_subst = 0;
361 Xinit(xs, xp, 128, ATEMP);
362 for (s = *hp; (s1 = strstr(s, pat))
363 && (!any_subst || globalv) ; s = s1 + pat_len)
365 any_subst = 1;
366 len = s1 - s;
367 XcheckN(xs, xp, len + rep_len);
368 memcpy(xp, s, len); /* first part */
369 xp += len;
370 memcpy(xp, rep, rep_len); /* replacement */
371 xp += rep_len;
373 if (!any_subst) {
374 bi_errorf("substitution failed");
375 return 1;
377 len = strlen(s) + 1;
378 XcheckN(xs, xp, len);
379 memcpy(xp, s, len);
380 xp += len;
381 line = Xclose(xs, xp);
383 return hist_execute(line);
387 * get pointer to history given pattern
388 * pattern is a number or string
390 static char **
391 hist_get(str, approx, allow_cur)
392 const char *str;
393 int approx;
394 int allow_cur;
396 char **hp = (char **) 0;
397 int n;
399 if (getn(str, &n)) {
400 hp = histptr + (n < 0 ? n : (n - hist_source->line));
401 if (hp < histlist) {
402 if (approx)
403 hp = hist_get_oldest();
404 else {
405 bi_errorf("%s: not in history", str);
406 hp = (char **) 0;
408 } else if (hp > histptr) {
409 if (approx)
410 hp = hist_get_newest(allow_cur);
411 else {
412 bi_errorf("%s: not in history", str);
413 hp = (char **) 0;
415 } else if (!allow_cur && hp == histptr) {
416 bi_errorf("%s: invalid range", str);
417 hp = (char **) 0;
419 } else {
420 int anchored = *str == '?' ? (++str, 0) : 1;
422 /* the -1 is to avoid the current fc command */
423 n = findhist(histptr - histlist - 1, 0, str, anchored);
424 if (n < 0) {
425 bi_errorf("%s: not in history", str);
426 hp = (char **) 0;
427 } else
428 hp = &histlist[n];
430 return hp;
433 /* Return a pointer to the newest command in the history */
434 static char **
435 hist_get_newest(allow_cur)
436 int allow_cur;
438 if (histptr < histlist || (!allow_cur && histptr == histlist)) {
439 bi_errorf("no history (yet)");
440 return (char **) 0;
442 if (allow_cur)
443 return histptr;
444 return histptr - 1;
447 /* Return a pointer to the newest command in the history */
448 static char **
449 hist_get_oldest()
451 if (histptr <= histlist) {
452 bi_errorf("no history (yet)");
453 return (char **) 0;
455 return histlist;
458 /******************************/
459 /* Back up over last histsave */
460 /******************************/
461 static void
462 histbackup()
464 static int last_line = -1;
466 if (histptr >= histlist && last_line != hist_source->line) {
467 hist_source->line--;
468 afree((void*)*histptr, APERM);
469 histptr--;
470 last_line = hist_source->line;
475 * Return the current position.
477 char **
478 histpos()
480 return current;
484 histN()
486 return curpos;
490 histnum(n)
491 int n;
493 int last = histptr - histlist;
495 if (n < 0 || n >= last) {
496 current = histptr;
497 curpos = last;
498 return last;
499 } else {
500 current = &histlist[n];
501 curpos = n;
502 return n;
507 * This will become unnecessary if hist_get is modified to allow
508 * searching from positions other than the end, and in either
509 * direction.
512 findhist(start, fwd, str, anchored)
513 int start;
514 int fwd;
515 const char *str;
516 int anchored;
518 char **hp;
519 int maxhist = histptr - histlist;
520 int incr = fwd ? 1 : -1;
521 int len = strlen(str);
523 if (start < 0 || start >= maxhist)
524 start = maxhist;
526 hp = &histlist[start];
527 for (; hp >= histlist && hp <= histptr; hp += incr)
528 if ((anchored && strncmp(*hp, str, len) == 0)
529 || (!anchored && strstr(*hp, str)))
530 return hp - histlist;
532 return -1;
536 * set history
537 * this means reallocating the dataspace
539 void
540 sethistsize(n)
541 int n;
543 if (n > 0 && n != histsize) {
544 int cursize = histptr - histlist;
546 /* save most recent history */
547 if (n < cursize) {
548 memmove(histlist, histptr - n, n * sizeof(char *));
549 cursize = n;
552 histlist = (char **)aresize(histlist, n*sizeof(char *), APERM);
554 histsize = n;
555 histptr = histlist + cursize;
560 * set history file
561 * This can mean reloading/resetting/starting history file
562 * maintenance
564 void
565 sethistfile(name)
566 const char *name;
568 /* if not started then nothing to do */
569 if (hstarted == 0)
570 return;
572 /* if the name is the same as the name we have */
573 if (hname && strcmp(hname, name) == 0)
574 return;
577 * its a new name - possibly
579 # ifdef EASY_HISTORY
580 if (hname) {
581 afree(hname, APERM);
582 hname = NULL;
584 # else
585 if (histfd) {
586 /* yes the file is open */
587 (void) close(histfd);
588 histfd = 0;
589 hsize = 0;
590 afree(hname, APERM);
591 hname = NULL;
592 /* let's reset the history */
593 histptr = histlist - 1;
594 hist_source->line = 0;
596 # endif
598 hist_init(hist_source);
602 * initialise the history vector
604 void
605 init_histvec()
607 if (histlist == NULL) {
608 histsize = HISTORYSIZE;
609 histlist = (char **)alloc(histsize*sizeof (char *), APERM);
610 histptr = histlist - 1;
614 # ifdef EASY_HISTORY
616 * save command in history
618 void
619 histsave(lno, cmd, dowrite)
620 int lno; /* ignored (compatibility with COMPLEX_HISTORY) */
621 const char *cmd;
622 int dowrite; /* ignored (compatibility with COMPLEX_HISTORY) */
624 register char **hp = histptr;
625 char *cp;
627 if (++hp >= histlist + histsize) { /* remove oldest command */
628 afree((void*)histlist[0], APERM);
629 memmove(histlist, histlist + 1,
630 sizeof(histlist[0]) * (histsize - 1));
631 hp = &histlist[histsize - 1];
633 *hp = str_save(cmd, APERM);
634 /* trash trailing newline but allow imbedded newlines */
635 cp = *hp + strlen(*hp);
636 if (cp > *hp && cp[-1] == '\n')
637 cp[-1] = '\0';
638 histptr = hp;
642 * Append an entry to the last saved command. Used for multiline
643 * commands
645 void
646 histappend(cmd, nl_separate)
647 const char *cmd;
648 int nl_separate;
650 int hlen, clen;
651 char *p;
653 hlen = strlen(*histptr);
654 clen = strlen(cmd);
655 if (clen > 0 && cmd[clen-1] == '\n')
656 clen--;
657 p = *histptr = (char *) aresize(*histptr, hlen + clen + 2, APERM);
658 p += hlen;
659 if (nl_separate)
660 *p++ = '\n';
661 memcpy(p, cmd, clen);
662 p[clen] = '\0';
666 * 92-04-25 <sjg@zen>
667 * A simple history file implementation.
668 * At present we only save the history when we exit.
669 * This can cause problems when there are multiple shells are
670 * running under the same user-id. The last shell to exit gets
671 * to save its history.
673 void
674 hist_init(s)
675 Source *s;
677 char *f;
678 FILE *fh;
680 if (Flag(FTALKING) == 0)
681 return;
683 hstarted = 1;
685 hist_source = s;
687 if ((f = str_val(global("HISTFILE"))) == NULL || *f == '\0') {
688 # if 1 /* Don't use history file unless the user asks for it */
689 hname = NULL;
690 return;
691 # else
692 char *home = str_val(global("HOME"));
693 int len;
695 if (home == NULL)
696 home = null;
697 f = HISTFILE;
698 hname = alloc(len = strlen(home) + strlen(f) + 2, APERM);
699 shf_snprintf(hname, len, "%s/%s", home, f);
700 # endif
701 } else
702 hname = str_save(f, APERM);
704 if ((fh = fopen(hname, "r"))) {
705 int pos = 0, nread = 0;
706 int contin = 0; /* continuation of previous command */
707 char *end;
708 char hline[LINE + 1];
710 while (1) {
711 if (pos >= nread) {
712 pos = 0;
713 nread = fread(hline, 1, LINE, fh);
714 if (nread <= 0)
715 break;
716 hline[nread] = '\0';
718 end = strchr(hline + pos, 0); /* will always succeed */
719 if (contin)
720 histappend(hline + pos, 0);
721 else {
722 hist_source->line++;
723 histsave(0, hline + pos, 0);
725 pos = end - hline + 1;
726 contin = end == &hline[nread];
728 fclose(fh);
733 * save our history.
734 * We check that we do not have more than we are allowed.
735 * If the history file is read-only we do nothing.
736 * Handy for having all shells start with a useful history set.
739 void
740 hist_finish()
742 static int once;
743 int fd;
744 #if !defined(O_EXLOCK) && defined(LOCK_EX)
745 int rc;
746 #endif
747 FILE *fh;
748 register int i;
749 register char **hp;
751 if (once++)
752 return;
753 if (hname == NULL || hname[0] == 0)
754 return;
756 /* check how many we have */
757 i = histptr - histlist;
758 if (i >= histsize)
759 hp = &histptr[-histsize];
760 else
761 hp = histlist;
763 fd = open(hname, O_WRONLY | O_CREAT | O_TRUNC
764 #ifdef O_EXLOCK
765 | O_EXLOCK
766 #endif
767 , 0777);
769 #if !defined(O_EXLOCK) && defined(LOCK_EX)
770 do {
771 rc = flock(fd, LOCK_EX);
772 if (rc == -1 && errno != EINTR)
773 return;
774 } while (rc == -1 && errno == EINTR); /* if interrupted, try again. */
775 #endif
777 /* Remove anything written before we got the lock */
778 ftruncate(fd, 0);
779 if (fd >= 0 && (fh = fdopen(fd, "w"))) {
780 for (i = 0; hp + i <= histptr && hp[i]; i++)
781 fprintf(fh, "%s%c", hp[i], '\0');
782 #if !defined(O_EXLOCK) && defined(LOCK_EX)
783 flock(fd, LOCK_UN);
784 #endif
785 fclose(fh);
789 # else /* EASY_HISTORY */
792 * Routines added by Peter Collinson BSDI(Europe)/Hillside Systems to
793 * a) permit HISTSIZE to control number of lines of history stored
794 * b) maintain a physical history file
796 * It turns out that there is a lot of ghastly hackery here
801 * save command in history
803 void
804 histsave(lno, cmd, dowrite)
805 int lno;
806 const char *cmd;
807 int dowrite;
809 register char **hp;
810 char *c, *cp;
812 c = str_save(cmd, APERM);
813 if ((cp = strchr(c, '\n')) != NULL)
814 *cp = '\0';
816 if (histfd && dowrite)
817 writehistfile(lno, c);
819 hp = histptr;
821 if (++hp >= histlist + histsize) { /* remove oldest command */
822 afree((void*)*histlist, APERM);
823 for (hp = histlist; hp < histlist + histsize - 1; hp++)
824 hp[0] = hp[1];
826 *hp = c;
827 histptr = hp;
831 * Write history data to a file nominated by HISTFILE
832 * if HISTFILE is unset then history still happens, but
833 * the data is not written to a file
834 * All copies of ksh looking at the file will maintain the
835 * same history. This is ksh behaviour.
837 * This stuff uses mmap()
838 * if your system ain't got it - then you'll have to undef HISTORYFILE
842 * Open a history file
843 * Format is:
844 * Bytes 1, 2: HMAGIC - just to check that we are dealing with
845 * the correct object
846 * Then follows a number of stored commands
847 * Each command is
848 * <command byte><command number(4 bytes)><bytes><null>
850 # define HMAGIC1 0xab
851 # define HMAGIC2 0xcd
852 # define COMMAND 0xff
854 void
855 hist_init(s)
856 Source *s;
858 unsigned char *base;
859 int lines;
860 int fd;
862 if (Flag(FTALKING) == 0)
863 return;
865 hstarted = 1;
867 hist_source = s;
869 hname = str_val(global("HISTFILE"));
870 if (hname == NULL)
871 return;
872 hname = str_save(hname, APERM);
874 retry:
875 /* we have a file and are interactive */
876 if ((fd = open(hname, O_RDWR|O_CREAT|O_APPEND, 0600)) < 0)
877 return;
879 histfd = savefd(fd, 0);
881 (void) flock(histfd, LOCK_EX);
883 hsize = lseek(histfd, 0L, SEEK_END);
885 if (hsize == 0) {
886 /* add magic */
887 if (sprinkle(histfd)) {
888 hist_finish();
889 return;
892 else if (hsize > 0) {
894 * we have some data
896 base = (unsigned char *)mmap(0, hsize, PROT_READ, MAP_FLAGS, histfd, 0);
898 * check on its validity
900 if (base == MAP_FAILED || *base != HMAGIC1 || base[1] != HMAGIC2) {
901 if (base != MAP_FAILED)
902 munmap((caddr_t)base, hsize);
903 hist_finish();
904 unlink(hname);
905 goto retry;
907 if (hsize > 2) {
908 lines = hist_count_lines(base+2, hsize-2);
909 if (lines > histsize) {
910 /* we need to make the file smaller */
911 if (hist_shrink(base, hsize))
912 unlink(hname);
913 munmap((caddr_t)base, hsize);
914 hist_finish();
915 goto retry;
918 histload(hist_source, base+2, hsize-2);
919 munmap((caddr_t)base, hsize);
921 (void) flock(histfd, LOCK_UN);
922 hsize = lseek(histfd, 0L, SEEK_END);
925 typedef enum state {
926 shdr, /* expecting a header */
927 sline, /* looking for a null byte to end the line */
928 sn1, /* bytes 1 to 4 of a line no */
929 sn2, sn3, sn4
930 } State;
932 static int
933 hist_count_lines(base, bytes)
934 register unsigned char *base;
935 register int bytes;
937 State state = shdr;
938 int lines = 0;
940 while (bytes--) {
941 switch (state)
943 case shdr:
944 if (*base == COMMAND)
945 state = sn1;
946 break;
947 case sn1:
948 state = sn2; break;
949 case sn2:
950 state = sn3; break;
951 case sn3:
952 state = sn4; break;
953 case sn4:
954 state = sline; break;
955 case sline:
956 if (*base == '\0')
957 lines++, state = shdr;
959 base++;
961 return lines;
965 * Shrink the history file to histsize lines
967 static int
968 hist_shrink(oldbase, oldbytes)
969 unsigned char *oldbase;
970 int oldbytes;
972 int fd;
973 char nfile[1024];
974 struct stat statb;
975 unsigned char *nbase = oldbase;
976 int nbytes = oldbytes;
978 nbase = hist_skip_back(nbase, &nbytes, histsize);
979 if (nbase == NULL)
980 return 1;
981 if (nbase == oldbase)
982 return 0;
985 * create temp file
987 (void) shf_snprintf(nfile, sizeof(nfile), "%s.%d", hname, procpid);
988 if ((fd = creat(nfile, 0600)) < 0)
989 return 1;
991 if (sprinkle(fd)) {
992 close(fd);
993 unlink(nfile);
994 return 1;
996 if (write(fd, nbase, nbytes) != nbytes) {
997 close(fd);
998 unlink(nfile);
999 return 1;
1002 * worry about who owns this file
1004 if (fstat(histfd, &statb) >= 0)
1005 fchown(fd, statb.st_uid, statb.st_gid);
1006 close(fd);
1009 * rename
1011 if (rename(nfile, hname) < 0)
1012 return 1;
1013 return 0;
1018 * find a pointer to the data `no' back from the end of the file
1019 * return the pointer and the number of bytes left
1021 static unsigned char *
1022 hist_skip_back(base, bytes, no)
1023 unsigned char *base;
1024 int *bytes;
1025 int no;
1027 register int lines = 0;
1028 register unsigned char *ep;
1030 for (ep = base + *bytes; --ep > base; ) {
1031 /* this doesn't really work: the 4 byte line number that is
1032 * encoded after the COMMAND byte can itself contain the
1033 * COMMAND byte....
1035 for (; ep > base && *ep != COMMAND; ep--)
1037 if (ep == base)
1038 break;
1039 if (++lines == no) {
1040 *bytes = *bytes - ((char *)ep - (char *)base);
1041 return ep;
1044 return NULL;
1048 * load the history structure from the stored data
1050 static void
1051 histload(s, base, bytes)
1052 Source *s;
1053 register unsigned char *base;
1054 register int bytes;
1056 State state;
1057 int lno = 0;
1058 unsigned char *line = NULL;
1060 for (state = shdr; bytes-- > 0; base++) {
1061 switch (state) {
1062 case shdr:
1063 if (*base == COMMAND)
1064 state = sn1;
1065 break;
1066 case sn1:
1067 lno = (((*base)&0xff)<<24);
1068 state = sn2;
1069 break;
1070 case sn2:
1071 lno |= (((*base)&0xff)<<16);
1072 state = sn3;
1073 break;
1074 case sn3:
1075 lno |= (((*base)&0xff)<<8);
1076 state = sn4;
1077 break;
1078 case sn4:
1079 lno |= (*base)&0xff;
1080 line = base+1;
1081 state = sline;
1082 break;
1083 case sline:
1084 if (*base == '\0') {
1085 /* worry about line numbers */
1086 if (histptr >= histlist && lno-1 != s->line) {
1087 /* a replacement ? */
1088 histinsert(s, lno, line);
1090 else {
1091 s->line = lno;
1092 histsave(lno, (char *)line, 0);
1094 state = shdr;
1101 * Insert a line into the history at a specified number
1103 static void
1104 histinsert(s, lno, line)
1105 Source *s;
1106 int lno;
1107 unsigned char *line;
1109 register char **hp;
1111 if (lno >= s->line-(histptr-histlist) && lno <= s->line) {
1112 hp = &histptr[lno-s->line];
1113 if (*hp)
1114 afree((void*)*hp, APERM);
1115 *hp = str_save((char *)line, APERM);
1120 * write a command to the end of the history file
1121 * This *MAY* seem easy but it's also necessary to check
1122 * that the history file has not changed in size.
1123 * If it has - then some other shell has written to it
1124 * and we should read those commands to update our history
1126 static void
1127 writehistfile(lno, cmd)
1128 int lno;
1129 char *cmd;
1131 int sizenow;
1132 unsigned char *base;
1133 unsigned char *new;
1134 int bytes;
1135 unsigned char hdr[5];
1137 (void) flock(histfd, LOCK_EX);
1138 sizenow = lseek(histfd, 0L, SEEK_END);
1139 if (sizenow != hsize) {
1141 * Things have changed
1143 if (sizenow > hsize) {
1144 /* someone has added some lines */
1145 bytes = sizenow - hsize;
1146 base = (unsigned char *)mmap(0, sizenow, PROT_READ, MAP_FLAGS, histfd, 0);
1147 if (base == MAP_FAILED)
1148 goto bad;
1149 new = base + hsize;
1150 if (*new != COMMAND) {
1151 munmap((caddr_t)base, sizenow);
1152 goto bad;
1154 hist_source->line--;
1155 histload(hist_source, new, bytes);
1156 hist_source->line++;
1157 lno = hist_source->line;
1158 munmap((caddr_t)base, sizenow);
1159 hsize = sizenow;
1160 } else {
1161 /* it has shrunk */
1162 /* but to what? */
1163 /* we'll give up for now */
1164 goto bad;
1168 * we can write our bit now
1170 hdr[0] = COMMAND;
1171 hdr[1] = (lno>>24)&0xff;
1172 hdr[2] = (lno>>16)&0xff;
1173 hdr[3] = (lno>>8)&0xff;
1174 hdr[4] = lno&0xff;
1175 (void) write(histfd, hdr, 5);
1176 (void) write(histfd, cmd, strlen(cmd)+1);
1177 hsize = lseek(histfd, 0L, SEEK_END);
1178 (void) flock(histfd, LOCK_UN);
1179 return;
1180 bad:
1181 hist_finish();
1184 void
1185 hist_finish()
1187 (void) flock(histfd, LOCK_UN);
1188 (void) close(histfd);
1189 histfd = 0;
1193 * add magic to the history file
1195 static int
1196 sprinkle(fd)
1197 int fd;
1199 static unsigned char mag[] = { HMAGIC1, HMAGIC2 };
1201 return(write(fd, mag, 2) != 2);
1204 # endif
1205 #else /* HISTORY */
1207 /* No history to be compiled in: dummy routines to avoid lots more ifdefs */
1208 void
1209 init_histvec()
1212 void
1213 hist_init(s)
1214 Source *s;
1217 void
1218 hist_finish()
1221 void
1222 histsave(lno, cmd, dowrite)
1223 int lno;
1224 const char *cmd;
1225 int dowrite;
1227 errorf("history not enabled");
1229 #endif /* HISTORY */