From 2bb9bfda46fd49e3610c92cc63268c56648a44b3 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Mon, 9 Mar 2020 19:39:15 -0400 Subject: Add HISTFILE history saving --- bin/1sh/1sh.1 | 5 +++++ bin/1sh/histedit.c | 38 ++++++++++++++++++++++++++++++++++++-- bin/1sh/main.c | 4 ++++ bin/1sh/myhistedit.h | 1 + bin/1sh/var.c | 3 +++ bin/1sh/var.h | 2 ++ 6 files changed, 51 insertions(+), 2 deletions(-) diff --git a/bin/1sh/1sh.1 b/bin/1sh/1sh.1 index d72a87c7..aa906bca 100644 --- a/bin/1sh/1sh.1 +++ b/bin/1sh/1sh.1 @@ -1351,6 +1351,11 @@ If not set, the default editor is The default editor used with the .Ic fc built-in. +.It Va HISTFILE +The path to the file in which command history is saved. +History is loaded when +.Va HISTFILE +is set and saved on exit. .It Va HISTSIZE The number of previous commands that are accessible. .It Va HOME diff --git a/bin/1sh/histedit.c b/bin/1sh/histedit.c index 520b4554..9784b1f1 100644 --- a/bin/1sh/histedit.c +++ b/bin/1sh/histedit.c @@ -69,6 +69,9 @@ EditLine *el; /* editline cookie */ int displayhist; static FILE *el_in, *el_out, *el_err; +static void history_load(const char *hf); +static void history_save(const char *hf); + static char *fc_replace(const char *, char *, char *); static int not_fcnumber(const char *); static int str_to_event(const char *, int); @@ -92,9 +95,10 @@ histedit(void) hist = history_init(); INTON; - if (hist != NULL) + if (hist != NULL) { sethistsize(histsizeval()); - else + sethistfile(histfileval()); + } else out2fmt_flush("sh: can't initialize history\n"); } if (editing && !el && isatty(0)) { /* && isatty(2) ??? */ @@ -152,6 +156,8 @@ bad: el = NULL; } if (hist) { + if (*histfileval() != '\0') + history_save(histfileval()); history_end(hist); hist = NULL; } @@ -160,6 +166,34 @@ bad: } +void +sethistfile(const char *hf) +{ + if (hist != NULL && hf != NULL && *hf != '\0') + history_load(hf); +} + + +static void +history_load(const char *hf) +{ + HistEvent he; + + if (history(hist, &he, H_LOAD, hf) == -1) + warning("%s: %s", he.str, hf); +} + + +static void +history_save(const char *hf) +{ + HistEvent he; + + if (history(hist, &he, H_SAVE, hf) == -1) + warning("%s: %s", he.str, hf); +} + + void sethistsize(const char *hs) { diff --git a/bin/1sh/main.c b/bin/1sh/main.c index 14b7af01..86b6e981 100644 --- a/bin/1sh/main.c +++ b/bin/1sh/main.c @@ -179,6 +179,8 @@ state4: if (sflag || minusc == NULL) { cmdloop(1); } + iflag = 0; + optschanged(); exitshell(exitstatus); /*NOTREACHED*/ return 0; @@ -345,6 +347,8 @@ exitcmd(int argc, char **argv) { if (stoppedjobs()) return 0; + iflag = 0; + optschanged(); if (argc > 1) exitshell(number(argv[1])); else diff --git a/bin/1sh/myhistedit.h b/bin/1sh/myhistedit.h index 3f1b4d64..e9a2be2c 100644 --- a/bin/1sh/myhistedit.h +++ b/bin/1sh/myhistedit.h @@ -39,6 +39,7 @@ extern EditLine *el; extern int displayhist; void histedit(void); +void sethistfile(const char *); void sethistsize(const char *); void setterm(const char *); diff --git a/bin/1sh/var.c b/bin/1sh/var.c index b501ef4a..d7467cc7 100644 --- a/bin/1sh/var.c +++ b/bin/1sh/var.c @@ -85,6 +85,7 @@ struct varinit { #ifndef NO_HISTORY +struct var vhistfile; struct var vhistsize; struct var vterm; #endif @@ -106,6 +107,8 @@ int forcelocal; static const struct varinit varinit[] = { #ifndef NO_HISTORY + { &vhistfile, VUNSET, "HISTFILE=", + sethistfile }, { &vhistsize, VUNSET, "HISTSIZE=", sethistsize }, #endif diff --git a/bin/1sh/var.h b/bin/1sh/var.h index 1bdf4afd..d06d8688 100644 --- a/bin/1sh/var.h +++ b/bin/1sh/var.h @@ -85,6 +85,7 @@ extern struct var vrps1; extern struct var vrps2; extern struct var vdisvfork; #ifndef NO_HISTORY +extern struct var vhistfile; extern struct var vhistsize; extern struct var vterm; #endif @@ -112,6 +113,7 @@ extern int initial_localeisutf8; #define rps2val() (vrps2.text + 5) #define optindval() (voptind.text + 7) #ifndef NO_HISTORY +#define histfileval() (vhistfile.text + 9) #define histsizeval() (vhistsize.text + 9) #define termval() (vterm.text + 5) #endif -- cgit 1.4.1