FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
gnuplot/src/history.c at master · gnuplot/gnuplot · GitHub
gnuplot
gnuplot
Repository navigation
Code
Pull requests
2
(2)
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
gnuplot
/
src
/
history.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
556 lines (458 loc) · 12.2 KB
Breadcrumbs
gnuplot
/
src
/
history.c
Copy path
File metadata and controls
556 lines (458 loc) · 12.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* GNUPLOT - history.c */
/*[
* Copyright 1986 - 1993, 1999, 2004 Thomas Williams, Colin Kelley
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose with or without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*
* Permission to modify the software is granted, but not the right to
* distribute the complete modified source code. Modifications are to
* be distributed as patches to the released version. Permission to
* distribute binaries produced by compiling modified sources is granted,
* provided you
* 1. distribute the corresponding source modifications from the
* released version in the form of a patch file along with the binaries,
* 2. add special version identification to distinguish your version
* in addition to the base release version number,
* 3. provide your name and address as the primary contact for the
* support of your modified version, and
* 4. retain our contact information in regard to use of the base
* software.
* Permission to distribute the released version of the source code along
* with corresponding source modifications in the form of a patch file is
* granted with same provisions 2 through 4 for binary distributions.
*
* This software is provided "as is" without express or implied warranty
* to the extent permitted by applicable law.
]*/
#include
<stdio.h>
#include
"gp_hist.h"
#include
"alloc.h"
#include
"plot.h"
#include
"util.h"
/* Public variables */
int
gnuplot_history_size
=
HISTORY_SIZE
;
TBOOLEAN
history_quiet
=
FALSE;
TBOOLEAN
history_full
=
FALSE;
#if
defined(
READLINE
)
/* Built-in readline */
struct
hist
*
history
=
NULL
;
/* last entry in the history list, no history yet */
struct
hist
*
cur_entry
=
NULL
;
int
history_length
=
0
;
/* number of entries in history list */
int
history_base
=
1
;
/* add line to the history */
void
add_history
(
char
*
line
)
{
struct
hist
*
entry
;
entry
=
(
struct
hist
*
)
gp_alloc
(
sizeof
(
struct
hist
),
"history"
);
entry
->
line
=
gp_strdup
(
line
);
entry
->
data
=
NULL
;
entry
->
prev
=
history
;
entry
->
next
=
NULL
;
if
(
history
!=
NULL
)
history
->
next
=
entry
;
else
cur_entry
=
entry
;
history
=
entry
;
history_length
++
;
}
/* write history to a file
*/
int
write_history
(
char
*
filename
)
{
write_history_n
(
0
,
filename
,
"w"
);
return
0
;
}
/* routine to read history entries from a file
*/
int
read_history
(
char
*
filename
)
{
return
gp_read_history
(
filename
);
}
void
using_history
(
void
)
{
/* Nothing to do. */
}
void
clear_history
(
void
)
{
HIST_ENTRY
*
entry
=
history
;
while
(
entry
!=
NULL
) {
HIST_ENTRY
*
prev
=
entry
->
prev
;
free
(
entry
->
line
);
free
(
entry
);
entry
=
prev
;
}
history_length
=
0
;
cur_entry
=
NULL
;
history
=
NULL
;
}
int
where_history
(
void
)
{
struct
hist
*
entry
=
history
;
/* last_entry */
int
hist_index
=
history_length
;
if
(
entry
==
NULL
)
return
0
;
/* no history yet */
if
(
cur_entry
==
NULL
)
return
history_length
;
/* find the current history entry and count backwards */
while
((
entry
->
prev
!=
NULL
)
&&
(
entry
!=
cur_entry
)) {
entry
=
entry
->
prev
;
hist_index
--
;
}
if
(
hist_index
>
0
)
hist_index
--
;
return
hist_index
;
}
int
history_set_pos
(
int
offset
)
{
struct
hist
*
entry
=
history
;
/* last_entry */
int
hist_index
=
history_length
-
1
;
if
((
offset
<
0
)
||
(
offset
>
history_length
)
||
(
history
==
NULL
))
return
0
;
if
(
offset
==
history_length
) {
cur_entry
=
NULL
;
return
1
;
}
/* seek backwards */
while
(
entry
!=
NULL
) {
if
(
hist_index
==
offset
) {
cur_entry
=
entry
;
return
1
;
}
entry
=
entry
->
prev
;
hist_index
--
;
}
return
0
;
}
HIST_ENTRY
*
history_get
(
int
offset
)
{
struct
hist
*
entry
=
history
;
/* last_entry */
int
hist_index
=
history_length
-
1
;
int
hist_ofs
=
offset
-
history_base
;
if
((
hist_ofs
<
0
)
||
(
hist_ofs
>=
history_length
)
||
(
history
==
NULL
))
return
NULL
;
/* find the current history entry and count backwards */
/* seek backwards */
while
(
entry
!=
NULL
) {
if
(
hist_index
==
hist_ofs
)
return
entry
;
entry
=
entry
->
prev
;
hist_index
--
;
}
return
NULL
;
}
HIST_ENTRY
*
current_history
(
void
)
{
return
cur_entry
;
}
HIST_ENTRY
*
previous_history
(
void
)
{
if
(
cur_entry
==
NULL
)
return
(
cur_entry
=
history
);
if
((
cur_entry
!=
NULL
)
&&
(
cur_entry
->
prev
!=
NULL
))
return
(
cur_entry
=
cur_entry
->
prev
);
else
return
NULL
;
}
HIST_ENTRY
*
next_history
(
void
)
{
if
(
cur_entry
!=
NULL
)
return
(
cur_entry
=
cur_entry
->
next
);
else
return
NULL
;
}
HIST_ENTRY
*
replace_history_entry
(
int
which
,
const
char
*
line
,
histdata_t
data
)
{
HIST_ENTRY
*
entry
=
history_get
(
which
+
1
);
HIST_ENTRY
*
prev_entry
;
if
(
entry
==
NULL
)
return
NULL
;
/* save contents: allocate new entry */
prev_entry
=
(
HIST_ENTRY
*
)
malloc
(
sizeof
(
HIST_ENTRY
));
if
(
entry
!=
NULL
) {
memset
(
prev_entry
,
0
,
sizeof
(
HIST_ENTRY
));
prev_entry
->
line
=
entry
->
line
;
prev_entry
->
data
=
entry
->
data
;
}
/* set new value */
entry
->
line
=
gp_strdup
(
line
);
entry
->
data
=
data
;
return
prev_entry
;
}
HIST_ENTRY
*
remove_history
(
int
which
)
{
HIST_ENTRY
*
entry
;
entry
=
history_get
(
which
+
history_base
);
if
(
entry
==
NULL
)
return
NULL
;
/* remove entry from chain */
if
(
entry
->
prev
!=
NULL
)
entry
->
prev
->
next
=
entry
->
next
;
if
(
entry
->
next
!=
NULL
)
entry
->
next
->
prev
=
entry
->
prev
;
else
history
=
entry
->
prev
;
/* last entry */
if
(
cur_entry
==
entry
)
cur_entry
=
entry
->
prev
;
/* adjust length */
history_length
--
;
return
entry
;
}
#endif
#if
defined(
READLINE
)
||
defined(
HAVE_LIBEDITLINE
)
histdata_t
free_history_entry
(
HIST_ENTRY
*
histent
)
{
histdata_t
data
;
if
(
histent
==
NULL
)
return
NULL
;
data
=
histent
->
data
;
free
((
void
*
)(
histent
->
line
));
free
(
histent
);
return
data
;
}
#endif
#if
defined(
READLINE
)
||
defined(
HAVE_WINEDITLINE
)
int
history_search
(
const
char
*
string
,
int
direction
)
{
int
start
;
HIST_ENTRY
*
entry
;
/* Work-around for WinEditLine: */
int
once
=
1
;
/* ensure that we try seeking at least one position */
char
*
pos
;
start
=
where_history
();
entry
=
current_history
();
while
(((
entry
!=
NULL
)
&&
entry
->
line
!=
NULL
)
||
once
) {
if
((
entry
!=
NULL
)
&&
(
entry
->
line
!=
NULL
)
&&
((
pos
=
strstr
(
entry
->
line
,
string
))
!=
NULL
))
return
(
pos
-
entry
->
line
);
if
(
direction
<
0
)
entry
=
previous_history
();
else
entry
=
next_history
();
once
=
0
;
}
/* not found */
history_set_pos
(
start
);
return
-1
;
}
int
history_search_prefix
(
const
char
*
string
,
int
direction
)
{
int
start
;
HIST_ENTRY
*
entry
;
/* Work-around for WinEditLine: */
int
once
=
1
;
/* ensure that we try seeking at least one position */
size_t
len
=
strlen
(
string
);
start
=
where_history
();
entry
=
current_history
();
while
(((
entry
!=
NULL
)
&&
entry
->
line
!=
NULL
)
||
once
) {
if
((
entry
!=
NULL
)
&&
(
entry
->
line
!=
NULL
)
&&
(
strncmp
(
entry
->
line
,
string
,
len
)
==
0
))
return
0
;
if
(
direction
<
0
)
entry
=
previous_history
();
else
entry
=
next_history
();
once
=
0
;
}
/* not found */
history_set_pos
(
start
);
return
-1
;
}
#endif
#ifdef
GNUPLOT_HISTORY
/* routine to read history entries from a file,
* this complements write_history and is necessary for
* saving of history when we are not using libreadline
*/
int
gp_read_history
(
const
char
*
filename
)
{
FILE
*
hist_file
;
if
((
hist_file
=
fopen
(
filename
,
"r"
))) {
while
(!
feof
(
hist_file
)) {
char
line
[
MAX_LINE_LEN
+
1
];
char
*
pline
;
pline
=
fgets
(
line
,
MAX_LINE_LEN
,
hist_file
);
if
(
pline
!=
NULL
) {
/* remove trailing linefeed */
if
((
pline
=
strrchr
(
line
,
'\n'
)))
*
pline
=
'\0'
;
if
((
pline
=
strrchr
(
line
,
'\r'
)))
*
pline
=
'\0'
;
/* skip leading whitespace */
pline
=
line
;
while
(
isspace
((
unsigned
char
)
*
pline
))
pline
++
;
/* avoid adding empty lines */
if
(
*
pline
)
add_history
(
pline
);
}
}
fclose
(
hist_file
);
return
0
;
}
else
{
return
errno
;
}
}
#endif
#ifdef
USE_READLINE
/* Save history to file, or write to stdout or pipe.
* For pipes, only "|" works, pipes starting with ">" get a strange
* filename like in the non-readline version.
*
* Peter Weilbacher, 28Jun2004
*/
void
write_history_list
(
const
int
num
,
const
char
*
const
filename
,
const
char
*
mode
)
{
const
HIST_ENTRY
*
list_entry
;
FILE
*
out
=
stdout
;
int
is_pipe
=
0
;
int
is_file
=
0
;
int
is_quiet
=
0
;
int
i
,
istart
;
if
(
filename
&&
filename
[
0
]) {
/* good filename given and not quiet */
#ifdef
PIPES
if
(
filename
[
0
]
==
'|'
) {
restrict_popen
();
out
=
popen
(
filename
+
1
,
"w"
);
is_pipe
=
1
;
}
else
#endif
{
if
(!(
out
=
fopen
(
filename
,
mode
))) {
int_warn
(
NO_CARET
,
"Cannot open file to save history, using standard output.\n"
);
out
=
stdout
;
}
else
{
is_file
=
1
;
}
}
}
else
if
(
filename
&&
!
filename
[
0
]) {
is_quiet
=
1
;
}
/* Determine starting point and output in loop. */
if
(
num
>
0
)
istart
=
history_length
-
num
-
1
;
else
istart
=
0
;
if
(
istart
<
0
||
istart
>
history_length
)
istart
=
0
;
for
(
i
=
istart
; (
list_entry
=
history_get
(
i
+
history_base
));
i
++
) {
/* don't add line numbers when writing to file to make file loadable */
if
(!
is_file
&&
!
is_quiet
)
fprintf
(
out
,
"%5i %s\n"
,
i
+
history_base
,
list_entry
->
line
);
else
fprintf
(
out
,
"%s\n"
,
list_entry
->
line
);
}
#ifdef
PIPES
if
(
is_pipe
)
pclose
(
out
);
#endif
if
(
is_file
)
fclose
(
out
);
}
/* This is the function getting called in command.c */
void
write_history_n
(
const
int
n
,
const
char
*
filename
,
const
char
*
mode
)
{
write_history_list
(
n
,
filename
,
mode
);
}
#endif
#ifdef
USE_READLINE
/* finds and returns a command from the history list by number */
const
char
*
history_find_by_number
(
int
n
)
{
if
(
0
<
n
&&
n
<
history_length
)
return
history_get
(
n
)
->
line
;
else
return
NULL
;
}
/* finds and returns a command from the history which starts with <cmd>
* Returns NULL if nothing found
*
* Peter Weilbacher, 28Jun2004
*/
const
char
*
history_find
(
char
*
cmd
)
{
if
(!
*
cmd
)
return
NULL
;
/* Start at latest entry */
#if
!defined(
HAVE_LIBEDITLINE
)
history_set_pos
(
history_length
);
#else
while
(
previous_history
());
#endif
/* Anchored backward search for prefix */
if
(
history_search_prefix
(
cmd
,
-1
)
==
0
)
return
current_history
()
->
line
;
return
NULL
;
}
/* finds and print all occurencies of commands from the history which
* start with <cmd>
* Returns the number of found entries on success,
* and 0 if no such entry exists
*
* Peter Weilbacher 28Jun2004
*/
int
history_find_all
(
char
*
cmd
)
{
int
found
;
int
ret
;
int
number
=
0
;
/* each entry found increases this */
if
(!
*
cmd
)
return
0
;
/* Output matching history entries in chronological order (not backwards
* so we have to start at the beginning of the history list.
*/
#if
!defined(
HAVE_LIBEDITLINE
)
ret
=
history_set_pos
(
0
);
if
(
ret
==
0
) {
fprintf
(
stderr
,
"ERROR (history_find_all): could not rewind history\n"
);
return
0
;
}
#else
/* HAVE_LIBEDITLINE */
/* libedit's history_set_pos() does not work properly,
so we manually go to the oldest entry. Note that directions
are reversed. */
while
(
next_history
());
#endif
do
{
found
=
history_search_prefix
(
cmd
,
1
);
/* Anchored backward search for prefix */
if
(
found
==
0
) {
number
++
;
#if
!defined(
HAVE_LIBEDITLINE
)
printf
(
"%5i %s\n"
,
where_history
()
+
history_base
,
current_history
()
->
line
);
/* Advance one step or we find always the same entry. */
if
(
next_history
()
==
NULL
)
break
;
/* finished if stepping didn't work */
#else
/* HAVE_LIBEDITLINE */
/* libedit's history indices are reversed wrt GNU readline */
printf
(
"%5i %s\n"
,
history_length
-
where_history
()
+
history_base
,
current_history
()
->
line
);
/* Advance one step or we find always the same entry. */
if
(!
previous_history
())
break
;
/* finished if stepping didn't work */
#endif
}
/* (found == 0) */
}
while
(
found
>
-1
);
return
number
;
}
#endif
/* READLINE */
Back
|
FazBrowse Home
|
New Git URL