-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcligen_history.c
411 lines (369 loc) · 10.6 KB
/
cligen_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Code in this file is based on:
*
* Copyright (C) 1991, 1992, 1993 by Chris Thewalt ([email protected])
*
* Permission to use, copy, modify, and distribute this software
* for any purpose and without fee is hereby granted, provided
* that the above copyright notices appear in all copies and that both the
* copyright notice and this permission notice appear in supporting
* documentation. This software is provided "as is" without express or
* implied warranty.
*
* Thanks to the following people who have provided enhancements and fixes:
* Ron Ueberschaer, Christoph Keller, Scott Schwartz, Steven List,
* DaviD W. Sanderson, Goran Bostrom, Michael Gleason, Glenn Kasten,
* Edin Hodzic, Eric J Bivona, Kai Uwe Rommel, Danny Quah, Ulrich Betzler
* Modifications are under:
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2001-2022 Olof Hagsand
This file is part of CLIgen.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL"),
in which case the provisions of the GPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of the GPL, and not to allow others to
use your version of this file under the terms of Apache License version 2, indicate
your decision by deleting the provisions above and replace them with the
notice and other provisions required by the GPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the Apache License version 2 or the GPL.
***** END LICENSE BLOCK *****
*/
#include "cligen_config.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include "cligen_buf.h"
#include "cligen_cv.h"
#include "cligen_cvec.h"
#include "cligen_parsetree.h"
#include "cligen_pt_head.h"
#include "cligen_callback.h"
#include "cligen_object.h"
#include "cligen_io.h"
#include "cligen_handle.h"
#include "cligen_getline.h"
#include "cligen_handle_internal.h"
#include "cligen_history_internal.h"
#include "cligen_history.h"
/*! Makes a copy of the string
*
* @param[in] p String input
* @retval str Malloced copied string output
* @retval NULL Error
*/
static char *
hist_save(char *p)
{
char *s = NULL;
int len = strlen(p)+1;
char *nl = strchr(p, '\n'); /* newline */
if (nl) {
if ((s = malloc(len)) == NULL)
goto done;
strcpy(s, p);
s[len-1] = 0;
}
else {
if ((s = strdup(p)) == NULL)
goto done;
}
done:
return s;
}
/*! Add a line to the CLIgen history
*
* @param[in] h CLIgen handle
* @param[in] buf String to add to history
* @retval 0 OK
* @retval -1 Error
*/
int
hist_add(cligen_handle h,
char *buf)
{
struct cligen_handle *ch = handle(h);
int retval = -1;
char *p = buf;
int len;
if (strlen(buf) >= cligen_buf_size(h))
if (cligen_buf_increase(h, strlen(buf)) < 0)
goto done;
while (*p == ' ' || *p == '\t' || *p == '\n')
p++;
if (*p) {
len = strlen(buf);
if (strchr(p, '\n')) /* previous line already has NL stripped */
len--;
if (ch->ch_hist_pre == 0 || strlen(ch->ch_hist_pre) != len ||
strncmp(ch->ch_hist_pre, buf, len) != 0) {
if ((ch->ch_hist_buf[ch->ch_hist_last] = hist_save(buf)) == NULL)
goto done;
ch->ch_hist_pre = ch->ch_hist_buf[ch->ch_hist_last];
ch->ch_hist_last = (ch->ch_hist_last + 1) % ch->ch_hist_size;
if (ch->ch_hist_buf[ch->ch_hist_last] && *ch->ch_hist_buf[ch->ch_hist_last]) {
free(ch->ch_hist_buf[ch->ch_hist_last]);
}
ch->ch_hist_buf[ch->ch_hist_last] = ""; /* NB not-malloced, check in hist_free */
}
}
ch->ch_hist_cur = ch->ch_hist_last;
retval = 0;
done:
return retval;
}
/*! Clear the history and deallocate all memory of the history
*
* @param[in] h CLIgen handle
*/
int
hist_exit(cligen_handle h)
{
struct cligen_handle *ch = handle(h);
int i;
for (i=0; i < ch->ch_hist_size; i++)
if (ch->ch_hist_buf[i] && strlen(ch->ch_hist_buf[i])){
free(ch->ch_hist_buf[i]);
ch->ch_hist_buf[i] = NULL;
}
free(ch->ch_hist_buf);
ch->ch_hist_buf = NULL;
// done:
return 0;
}
/*! Loads previous hist entry into input buffer, sticks on first
*
* @param[in] h CLIgen handle
*/
char *
hist_prev(cligen_handle h)
{
struct cligen_handle *ch = handle(h);
char *p = 0;
int next = (ch->ch_hist_cur - 1 + ch->ch_hist_size) % ch->ch_hist_size;
if (ch->ch_hist_buf[ch->ch_hist_cur] != 0 && next != ch->ch_hist_last) {
ch->ch_hist_cur = next;
p = ch->ch_hist_buf[ch->ch_hist_cur];
}
if (p == 0) {
p = "";
gl_putc('\007');
}
return p;
}
/* Loads next hist entry into input buffer, clears on last
*
* @param[in] h CLIgen handle
*/
char *
hist_next(cligen_handle h)
{
struct cligen_handle *ch = handle(h);
char *p = 0;
if (ch->ch_hist_cur != ch->ch_hist_last) {
ch->ch_hist_cur = (ch->ch_hist_cur+1) % ch->ch_hist_size;
p = ch->ch_hist_buf[ch->ch_hist_cur];
}
if (p == 0) {
p = "";
gl_putc('\007');
}
return p;
}
/*!
* @param[in] h CLIgen handle
*/
int
hist_pos_set(cligen_handle h,
int pos)
{
struct cligen_handle *ch = handle(h);
ch->ch_hist_cur = pos;
return 0;
}
/*!
* @param[in] h CLIgen handle
*/
int
hist_pos(cligen_handle h)
{
struct cligen_handle *ch = handle(h);
return ch->ch_hist_cur;
}
/*!
* @param[in] h CLIgen handle
*/
int
hist_last_get(cligen_handle h)
{
struct cligen_handle *ch = handle(h);
return ch->ch_hist_last;
}
/*! Copy history line/pos to cligen buffer
*
* @param[in] h CLIgen handle
* @param[in] pos Line number to copy from history to cligen main buffer
*/
int
hist_copy(cligen_handle h,
char *ptr)
{
strncpy(cligen_buf(h), ptr, cligen_buf_size(h));
return 0;
}
/*!
* @param[in] h CLIgen handle
*/
int
hist_copy_prev(cligen_handle h)
{
char *ptr = hist_prev(h);
strncpy(cligen_buf(h), ptr, cligen_buf_size(h));
return 0;
}
/*!
* @param[in] h CLIgen handle
*/
int
hist_copy_pos(cligen_handle h)
{
struct cligen_handle *ch = handle(h);
int pos;
pos = hist_pos(h);
strncpy(cligen_buf(h), ch->ch_hist_buf[pos], cligen_buf_size(h));
return 0;
}
/*!
* @param[in] h CLIgen handle
*/
int
hist_copy_next(cligen_handle h)
{
char *ptr = hist_next(h);
strncpy(cligen_buf(h), ptr, cligen_buf_size(h));
return 0;
}
/*---------------------------- Public API -----------------------------*/
/*! Initialize CLIgen history.
*
* Getline assumes there is a history, so you need to call this at time of
* init. But you can also call it later to resize, but that will erase all
* existing entries.
* @param[in] h CLIgen handle
* @param[in] lines Number of lines in history
*/
int
cligen_hist_init(cligen_handle h,
int lines)
{
struct cligen_handle *ch = handle(h);
int retval = -1;
int i;
int old_size;
if (lines < 1){
errno = EINVAL;
goto done;
}
old_size = ch->ch_hist_size;
ch->ch_hist_size = lines+1; /* circular buffer needs an extra element */
for (i=0; i < old_size; i++)
if (ch->ch_hist_buf[i]){
if (strlen(ch->ch_hist_buf[i]))
free(ch->ch_hist_buf[i]);
ch->ch_hist_buf[i] = NULL;
}
if ((ch->ch_hist_buf = (char**)realloc(ch->ch_hist_buf, ch->ch_hist_size*sizeof(char*))) == NULL)
goto done;
ch->ch_hist_cur = 0;
ch->ch_hist_last = 0;
ch->ch_hist_pre = 0;
ch->ch_hist_buf[0] = ""; /* NB not-malloced, check in hist_free */
for (i=1; i < ch->ch_hist_size; i++) /* reset all entries */
ch->ch_hist_buf[i] = (char *)0;
retval = 0;
done:
return retval;
}
/*! Read history entries from file
*
* @param[in] h CLIgen handle
* @param[in] f Open file for read
* @see cligen_hist_init must be called before
* @note open file f instead of filename so that caller can have better error
* control if file not found or lacking permissions
*/
int
cligen_hist_file_load(cligen_handle h,
FILE *f)
{
int retval = -1;
int ret;
unsigned char ch;
cbuf *cb = NULL;
if ((cb = cbuf_new()) == NULL)
goto done;
while (1){
ret = fgetc(f);
if (ret == EOF)
break; /* eof or error */
ch = (unsigned char)ret;
if (ch == '\n'){
if (hist_add(h, cbuf_get(cb)) < 0)
goto done;
cbuf_reset(cb);
}
else
if (cbuf_append(cb, ch) < 0)
goto done;
}
retval = 0;
done:
if (cb)
cbuf_free(cb);
return retval;
}
/*! Write history entries back to file
*
* @param[in] h CLIgen handle
* @param[in] filename Name of history file (or NULL if no history file)
* @see cligen_exit Call before this function before cligen_exit
*/
int
cligen_hist_file_save(cligen_handle h,
FILE *f)
{
int retval = -1;
struct cligen_handle *ch = handle(h);
int i;
char *line;
int i1;
/* rewind to last */
i = (ch->ch_hist_last+1)%ch->ch_hist_size;
while(ch->ch_hist_buf[i] == NULL)
i = (i+1)%ch->ch_hist_size;
i1 = ch->ch_hist_last;
while(i != i1){
if ((line = ch->ch_hist_buf[i]) == NULL) /* shouldnt happen */
break;
fprintf(f, "%s\n", line);
i = (i+1)%ch->ch_hist_size;
}
retval = 0;
return retval;
}