forked from wryun/es-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.c
134 lines (105 loc) · 2.38 KB
/
history.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* history.c -- control the history file ($Revision: 1.1.1.1 $) */
#include "es.h"
#include "gc.h"
#include "input.h"
/*
* constants
*/
#define BUFSIZE ((size_t) 4096) /* buffer size to fill reads into */
/*
* globals
*/
static Buffer *histbuffer = NULL;
#if HAVE_READLINE
#include <readline/history.h>
Boolean reloadhistory = FALSE;
static char *history;
#if 0
/* These split history file entries by timestamp, which allows readline to pick up
* multi-line commands correctly across process boundaries. Disabled by default,
* because it leaves the history file itself kind of ugly. */
static int history_write_timestamps = 1;
static char history_comment_char = '#';
#endif
#endif
/*
* histbuffer -- build the history line during input and dump it as a gc-string
*/
extern void newhistbuffer() {
assert(histbuffer == NULL);
histbuffer = openbuffer(BUFSIZE);
}
extern void addhistbuffer(char c) {
if (histbuffer == NULL)
return;
histbuffer = bufputc(histbuffer, c);
}
extern char *dumphistbuffer() {
char *s;
size_t len;
assert(histbuffer != NULL);
s = sealcountedbuffer(histbuffer);
histbuffer = NULL;
len = strlen(s);
if (len > 0 && s[len - 1] == '\n')
s[len - 1] = '\0';
return s;
}
/*
* history file
*/
#if HAVE_READLINE
extern void setmaxhistorylength(int len) {
static int currenthistlen = -1; /* unlimited */
if (len != currenthistlen) {
switch (len) {
case -1:
unstifle_history();
break;
case 0:
clear_history();
FALLTHROUGH;
default:
stifle_history(len);
}
currenthistlen = len;
}
}
extern void loghistory(char *cmd) {
int err;
if (cmd == NULL)
return;
add_history(cmd);
if (history == NULL)
return;
if ((err = append_history(1, history))) {
eprint("history(%s): %s\n", history, esstrerror(err));
vardef("history", NULL, NULL);
}
}
static void reload_history(void) {
/* Attempt to populate readline history with new history file. */
if (history != NULL)
read_history(history);
using_history();
reloadhistory = FALSE;
}
extern void sethistory(char *file) {
if (reloadhistory)
reload_history();
reloadhistory = TRUE;
history = file;
}
extern void checkreloadhistory(void) {
if (reloadhistory)
reload_history();
}
/*
* initialization
*/
/* inithistory -- called at dawn of time from main() */
extern void inithistory(void) {
/* declare the global roots */
globalroot(&history); /* history file */
}
#endif