1
/* $NetBSD: hist.c,v 1.15 2003/11/01 23:36:39 christos Exp $ */
1
/* $NetBSD: hist.c,v 1.20 2011/07/29 15:16:33 christos Exp $ */
4
4
* Copyright (c) 1992, 1993
56
56
el->el_history.fun = NULL;
57
57
el->el_history.ref = NULL;
58
el->el_history.buf = (char *) el_malloc(EL_BUFSIZ);
58
el->el_history.buf = el_malloc(EL_BUFSIZ * sizeof(*el->el_history.buf));
59
59
el->el_history.sz = EL_BUFSIZ;
60
60
if (el->el_history.buf == NULL)
62
62
el->el_history.last = el->el_history.buf;
80
80
* Set new history interface
83
hist_set(EditLine *el, hist_fun_t fun, ptr_t ptr)
83
hist_set(EditLine *el, hist_fun_t fun, void *ptr)
86
86
el->el_history.ref = ptr;
87
87
el->el_history.fun = fun;
96
96
protected el_action_t
97
97
hist_get(EditLine *el)
102
102
if (el->el_history.eventno == 0) { /* if really the current line */
103
(void) strncpy(el->el_line.buffer, el->el_history.buf,
103
(void) Strncpy(el->el_line.buffer, el->el_history.buf,
104
104
el->el_history.sz);
105
105
el->el_line.lastchar = el->el_line.buffer +
106
106
(el->el_history.last - el->el_history.buf);
112
112
#endif /* KSHVI */
113
113
el->el_line.cursor = el->el_line.lastchar;
117
117
if (el->el_history.ref == NULL)
120
120
hp = HIST_FIRST(el);
125
125
for (h = 1; h < el->el_history.eventno; h++)
126
126
if ((hp = HIST_NEXT(el)) == NULL) {
127
127
el->el_history.eventno = h;
130
(void) strlcpy(el->el_line.buffer, hp,
130
(void) Strncpy(el->el_line.buffer, hp,
131
131
(size_t)(el->el_line.limit - el->el_line.buffer));
132
el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer);
132
el->el_line.buffer[el->el_line.limit - el->el_line.buffer - 1] = '\0';
133
el->el_line.lastchar = el->el_line.buffer + Strlen(el->el_line.buffer);
134
135
if (el->el_line.lastchar > el->el_line.buffer
135
136
&& el->el_line.lastchar[-1] == '\n')
152
153
* process a history command
155
hist_command(EditLine *el, int argc, const char **argv)
156
hist_command(EditLine *el, int argc, const Char **argv)
161
162
if (el->el_history.ref == NULL)
164
if (argc == 1 || strcmp(argv[1], "list") == 0) {
165
if (argc == 1 || Strcmp(argv[1], STR("list")) == 0) {
165
166
/* List history entries */
167
168
for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
168
169
(void) fprintf(el->el_outfile, "%d %s",
169
el->el_history.ev.num, str);
170
el->el_history.ev.num, ct_encode_string(str, &el->el_scratch));
176
num = (int)strtol(argv[2], NULL, 0);
178
if (strcmp(argv[1], "size") == 0)
179
return history(el->el_history.ref, &ev, H_SETSIZE, num);
181
if (strcmp(argv[1], "unique") == 0)
182
return history(el->el_history.ref, &ev, H_SETUNIQUE, num);
177
num = (int)Strtol(argv[2], NULL, 0);
179
if (Strcmp(argv[1], STR("size")) == 0)
180
return FUNW(history)(el->el_history.ref, &ev, H_SETSIZE, num);
182
if (Strcmp(argv[1], STR("unique")) == 0)
183
return FUNW(history)(el->el_history.ref, &ev, H_SETUNIQUE, num);
193
194
hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
197
newbuf = realloc(el->el_history.buf, newsz);
198
newbuf = el_realloc(el->el_history.buf, newsz * sizeof(*newbuf));
201
(void) memset(&newbuf[oldsz], '\0', newsz - oldsz);
202
(void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf));
203
204
el->el_history.last = newbuf +
204
205
(el->el_history.last - el->el_history.buf);
214
hist_convert(EditLine *el, int fn, void *arg)
217
if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1)
219
return ct_decode_string((const char *)(const void *)ev.str,