-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.c
2842 lines (2775 loc) · 83.1 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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2012 by Martin C. Shepherd.
*
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons
* to whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
* OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Except as contained in this notice, the name of a copyright holder
* shall not be used in advertising or otherwise to promote the sale, use
* or other dealings in this Software without prior written authorization
* of the copyright holder.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include "ioutil.h"
#include "history.h"
#include "freelist.h"
#include "errmsg.h"
/*
* History lines are split into sub-strings of GLH_SEG_SIZE
* characters. To avoid wasting space in the GlhLineSeg structure,
* this should be a multiple of the size of a pointer.
*/
#define GLH_SEG_SIZE 16
/*
* GlhLineSeg structures contain fixed sized segments of a larger
* string. These are linked into lists to record strings, with all but
* the last segment having GLH_SEG_SIZE characters. The last segment
* of a string is terminated within the GLH_SEG_SIZE characters with a
* '\0'.
*/
typedef struct GlhLineSeg GlhLineSeg;
struct GlhLineSeg {
GlhLineSeg *next; /* The next sub-string of the history line */
char s[GLH_SEG_SIZE]; /* The sub-string. Beware that only the final */
/* substring of a line, as indicated by 'next' */
/* being NULL, is '\0' terminated. */
};
/*
* History lines are recorded in a hash table, such that repeated
* lines are stored just once.
*
* Start by defining the size of the hash table. This should be a
* prime number.
*/
#define GLH_HASH_SIZE 113
typedef struct GlhHashBucket GlhHashBucket;
/*
* Each history line will be represented in the hash table by a
* structure of the following type.
*/
typedef struct GlhHashNode GlhHashNode;
struct GlhHashNode {
GlhHashBucket *bucket; /* The parent hash-table bucket of this node */
GlhHashNode *next; /* The next in the list of nodes within the */
/* parent hash-table bucket. */
GlhLineSeg *head; /* The list of sub-strings which make up a line */
int len; /* The length of the line, excluding any '\0' */
int used; /* The number of times this string is pointed to by */
/* the time-ordered list of history lines. */
int reported; /* A flag that is used when searching to ensure that */
/* a line isn't reported redundantly. */
};
/*
* How many new GlhHashNode elements should be allocated at a time?
*/
#define GLH_HASH_INCR 50
static int _glh_is_line(GlhHashNode *hash, const char *line, size_t n);
static int _glh_line_matches_prefix(GlhHashNode *line, GlhHashNode *prefix);
static void _glh_return_line(GlhHashNode *hash, char *line, size_t dim);
/*
* All history lines which hash to a given bucket in the hash table, are
* recorded in a structure of the following type.
*/
struct GlhHashBucket {
GlhHashNode *lines; /* The list of history lines which fall in this bucket */
};
static GlhHashBucket *glh_find_bucket(GlHistory *glh, const char *line,
size_t n);
static GlhHashNode *glh_find_hash_node(GlhHashBucket *bucket, const char *line,
size_t n);
typedef struct {
FreeList *node_mem; /* A free-list of GlhHashNode structures */
GlhHashBucket bucket[GLH_HASH_SIZE]; /* The buckets of the hash table */
} GlhLineHash;
/*
* GlhLineNode's are used to record history lines in time order.
*/
typedef struct GlhLineNode GlhLineNode;
struct GlhLineNode {
long id; /* The unique identifier of this history line */
time_t timestamp; /* The time at which the line was archived */
unsigned group; /* The identifier of the history group to which the */
/* the line belongs. */
GlhLineNode *next; /* The next youngest line in the list */
GlhLineNode *prev; /* The next oldest line in the list */
GlhHashNode *line; /* The hash-table entry of the history line */
};
/*
* The number of GlhLineNode elements per freelist block.
*/
#define GLH_LINE_INCR 100
/*
* Encapsulate the time-ordered list of historical lines.
*/
typedef struct {
FreeList *node_mem; /* A freelist of GlhLineNode objects */
GlhLineNode *head; /* The oldest line in the list */
GlhLineNode *tail; /* The newest line in the list */
} GlhLineList;
/*
* The _glh_lookup_history() returns copies of history lines in a
* dynamically allocated array. This array is initially allocated
* GLH_LOOKUP_SIZE bytes. If subsequently this size turns out to be
* too small, realloc() is used to increase its size to the required
* size plus GLH_LOOKUP_MARGIN. The idea of the later parameter is to
* reduce the number of realloc() operations needed.
*/
#define GLH_LBUF_SIZE 300
#define GLH_LBUF_MARGIN 100
/*
* Encapsulate all of the resources needed to store historical input lines.
*/
struct GlHistory {
ErrMsg *err; /* The error-reporting buffer */
GlhLineSeg *buffer; /* An array of sub-line nodes to be partitioned */
/* into lists of sub-strings recording input lines. */
int nbuff; /* The allocated dimension of buffer[] */
GlhLineSeg *unused; /* The list of free nodes in buffer[] */
GlhLineList list; /* A time ordered list of history lines */
GlhLineNode *recall; /* The last line recalled, or NULL if no recall */
/* session is currently active. */
GlhLineNode *id_node;/* The node at which the last ID search terminated */
GlhLineHash hash; /* A hash-table of reference-counted history lines */
GlhHashNode *prefix; /* A pointer to a line containing the prefix that */
/* is being searched for. Note that if prefix==NULL */
/* and prefix_len>0, this means that no line in */
/* the buffer starts with the requested prefix. */
int prefix_len; /* The length of the prefix being searched for. */
char *lbuf; /* The array in which _glh_lookup_history() returns */
/* history lines */
int lbuf_dim; /* The allocated size of lbuf[] */
int nbusy; /* The number of line segments in buffer[] that are */
/* currently being used to record sub-lines */
int nfree; /* The number of line segments in buffer that are */
/* not currently being used to record sub-lines */
unsigned long seq; /* The next ID to assign to a line node */
unsigned group; /* The identifier of the current history group */
int nline; /* The number of lines currently in the history list */
int max_lines; /* Either -1 or a ceiling on the number of lines */
int enable; /* If false, ignore history additions and lookups */
};
#ifndef WITHOUT_FILE_SYSTEM
static int _glh_cant_load_history(GlHistory *glh, const char *filename,
int lineno, const char *message, FILE *fp);
static int _glh_cant_save_history(GlHistory *glh, const char *message,
const char *filename, FILE *fp);
static int _glh_write_timestamp(FILE *fp, time_t timestamp);
static int _glh_decode_timestamp(char *string, char **endp, time_t *timestamp);
#endif
static void _glh_discard_line(GlHistory *glh, GlhLineNode *node);
static GlhLineNode *_glh_find_id(GlHistory *glh, GlhLineID id);
static GlhHashNode *_glh_acquire_copy(GlHistory *glh, const char *line,
size_t n);
static GlhHashNode *_glh_discard_copy(GlHistory *glh, GlhHashNode *hnode);
static int _glh_prepare_for_recall(GlHistory *glh, char *line);
/*
* The following structure and functions are used to iterate through
* the characters of a segmented history line.
*/
typedef struct {
GlhLineSeg *seg; /* The line segment that the next character will */
/* be returned from. */
int posn; /* The index in the above line segment, containing */
/* the next unread character. */
char c; /* The current character in the input line */
} GlhLineStream;
static void glh_init_stream(GlhLineStream *str, GlhHashNode *line);
static void glh_step_stream(GlhLineStream *str);
/*
* See if search prefix contains any globbing characters.
*/
static int glh_contains_glob(GlhHashNode *prefix);
/*
* Match a line against a search pattern.
*/
static int glh_line_matches_glob(GlhLineStream *lstr, GlhLineStream *pstr);
static int glh_matches_range(char c, GlhLineStream *pstr);
/*.......................................................................
* Create a line history maintenance object.
*
* Input:
* buflen size_t The number of bytes to allocate to the
* buffer that is used to record all of the
* most recent lines of user input that will fit.
* If buflen==0, no buffer will be allocated.
* Output:
* return GlHistory * The new object, or NULL on error.
*/
GlHistory *_new_GlHistory(size_t buflen)
{
GlHistory *glh; /* The object to be returned */
int i;
/*
* Allocate the container.
*/
glh = (GlHistory *) malloc(sizeof(GlHistory));
if(!glh) {
errno = ENOMEM;
return NULL;
};
/*
* Before attempting any operation that might fail, initialize the
* container at least up to the point at which it can safely be passed
* to _del_GlHistory().
*/
glh->err = NULL;
glh->buffer = NULL;
glh->nbuff = (buflen+GLH_SEG_SIZE-1) / GLH_SEG_SIZE;
glh->unused = NULL;
glh->list.node_mem = NULL;
glh->list.head = glh->list.tail = NULL;
glh->recall = NULL;
glh->id_node = NULL;
glh->hash.node_mem = NULL;
for(i=0; i<GLH_HASH_SIZE; i++)
glh->hash.bucket[i].lines = NULL;
glh->prefix = NULL;
glh->lbuf = NULL;
glh->lbuf_dim = 0;
glh->nbusy = 0;
glh->nfree = glh->nbuff;
glh->seq = 0;
glh->group = 0;
glh->nline = 0;
glh->max_lines = -1;
glh->enable = 1;
/*
* Allocate a place to record error messages.
*/
glh->err = _new_ErrMsg();
if(!glh->err)
return _del_GlHistory(glh);
/*
* Allocate the buffer, if required.
*/
if(glh->nbuff > 0) {
glh->nbuff = glh->nfree;
glh->buffer = (GlhLineSeg *) malloc(sizeof(GlhLineSeg) * glh->nbuff);
if(!glh->buffer) {
errno = ENOMEM;
return _del_GlHistory(glh);
};
/*
* All nodes of the buffer are currently unused, so link them all into
* a list and make glh->unused point to the head of this list.
*/
glh->unused = glh->buffer;
for(i=0; i<glh->nbuff-1; i++) {
GlhLineSeg *seg = glh->unused + i;
seg->next = seg + 1;
};
glh->unused[i].next = NULL;
};
/*
* Allocate the GlhLineNode freelist.
*/
glh->list.node_mem = _new_FreeList(sizeof(GlhLineNode), GLH_LINE_INCR);
if(!glh->list.node_mem)
return _del_GlHistory(glh);
/*
* Allocate the GlhHashNode freelist.
*/
glh->hash.node_mem = _new_FreeList(sizeof(GlhLineNode), GLH_HASH_INCR);
if(!glh->hash.node_mem)
return _del_GlHistory(glh);
/*
* Allocate the array that _glh_lookup_history() uses to return a
* copy of a given history line. This will be resized when necessary.
*/
glh->lbuf_dim = GLH_LBUF_SIZE;
glh->lbuf = (char *) malloc(glh->lbuf_dim);
if(!glh->lbuf) {
errno = ENOMEM;
return _del_GlHistory(glh);
};
return glh;
}
/*.......................................................................
* Delete a GlHistory object.
*
* Input:
* glh GlHistory * The object to be deleted.
* Output:
* return GlHistory * The deleted object (always NULL).
*/
GlHistory *_del_GlHistory(GlHistory *glh)
{
if(glh) {
/*
* Delete the error-message buffer.
*/
glh->err = _del_ErrMsg(glh->err);
/*
* Delete the buffer.
*/
if(glh->buffer) {
free(glh->buffer);
glh->buffer = NULL;
glh->unused = NULL;
};
/*
* Delete the freelist of GlhLineNode's.
*/
glh->list.node_mem = _del_FreeList(glh->list.node_mem, 1);
/*
* The contents of the list were deleted by deleting the freelist.
*/
glh->list.head = NULL;
glh->list.tail = NULL;
/*
* Delete the freelist of GlhHashNode's.
*/
glh->hash.node_mem = _del_FreeList(glh->hash.node_mem, 1);
/*
* Delete the lookup buffer.
*/
if(glh->lbuf)
free(glh->lbuf);
/*
* Delete the container.
*/
free(glh);
};
return NULL;
}
/*.......................................................................
* Append a new line to the history list, deleting old lines to make
* room, if needed.
*
* Input:
* glh GlHistory * The input-line history maintenance object.
* line char * The line to be archived.
* force int Unless this flag is non-zero, empty lines aren't
* archived. This flag requests that the line be
* archived regardless.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int _glh_add_history(GlHistory *glh, const char *line, int force)
{
int slen; /* The length of the line to be recorded (minus the '\0') */
int empty; /* True if the string is empty */
const char *nlptr; /* A pointer to a newline character in line[] */
GlhHashNode *hnode; /* The hash-table node of the line */
GlhLineNode *lnode; /* A node in the time-ordered list of lines */
int i;
/*
* Check the arguments.
*/
if(!glh || !line) {
errno = EINVAL;
return 1;
};
/*
* Is history enabled?
*/
if(!glh->enable || !glh->buffer || glh->max_lines == 0)
return 0;
/*
* Cancel any ongoing search.
*/
if(_glh_cancel_search(glh))
return 1;
/*
* How long is the string to be recorded, being careful not to include
* any terminating '\n' character.
*/
nlptr = strchr(line, '\n');
if(nlptr)
slen = (nlptr - line);
else
slen = strlen(line);
/*
* Is the line empty?
*/
empty = 1;
for(i=0; i<slen && empty; i++)
empty = isspace((int)(unsigned char) line[i]);
/*
* If the line is empty, don't add it to the buffer unless explicitly
* told to.
*/
if(empty && !force)
return 0;
/*
* Has an upper limit to the number of lines in the history list been
* specified?
*/
if(glh->max_lines >= 0) {
/*
* If necessary, remove old lines until there is room to add one new
* line without exceeding the specified line limit.
*/
while(glh->nline > 0 && glh->nline >= glh->max_lines)
_glh_discard_line(glh, glh->list.head);
/*
* We can't archive the line if the maximum number of lines allowed is
* zero.
*/
if(glh->max_lines == 0)
return 0;
};
/*
* Unless already stored, store a copy of the line in the history buffer,
* then return a reference-counted hash-node pointer to this copy.
*/
hnode = _glh_acquire_copy(glh, line, slen);
if(!hnode) {
_err_record_msg(glh->err, "No room to store history line", END_ERR_MSG);
errno = ENOMEM;
return 1;
};
/*
* Allocate a new node in the time-ordered list of lines.
*/
lnode = (GlhLineNode *) _new_FreeListNode(glh->list.node_mem);
/*
* If a new line-node couldn't be allocated, discard our copy of the
* stored line before reporting the error.
*/
if(!lnode) {
hnode = _glh_discard_copy(glh, hnode);
_err_record_msg(glh->err, "No room to store history line", END_ERR_MSG);
errno = ENOMEM;
return 1;
};
/*
* Record a pointer to the hash-table record of the line in the new
* list node.
*/
lnode->id = glh->seq++;
lnode->timestamp = time(NULL);
lnode->group = glh->group;
lnode->line = hnode;
/*
* Append the new node to the end of the time-ordered list.
*/
if(glh->list.head)
glh->list.tail->next = lnode;
else
glh->list.head = lnode;
lnode->next = NULL;
lnode->prev = glh->list.tail;
glh->list.tail = lnode;
/*
* Record the addition of a line to the list.
*/
glh->nline++;
return 0;
}
/*.......................................................................
* Recall the next oldest line that has the search prefix last recorded
* by _glh_search_prefix().
*
* Input:
* glh GlHistory * The input-line history maintenance object.
* line char * The input line buffer. On input this should contain
* the current input line, and on output, if anything
* was found, its contents will have been replaced
* with the matching line.
* dim size_t The allocated dimension of the line buffer.
* Output:
* return char * A pointer to line[0], or NULL if not found.
*/
char *_glh_find_backwards(GlHistory *glh, char *line, size_t dim)
{
GlhLineNode *node; /* The line location node being checked */
GlhHashNode *old_line; /* The previous recalled line */
/*
* Check the arguments.
*/
if(!glh || !line) {
if(glh)
_err_record_msg(glh->err, "NULL argument(s)", END_ERR_MSG);
errno = EINVAL;
return NULL;
};
/*
* Is history enabled?
*/
if(!glh->enable || !glh->buffer || glh->max_lines == 0)
return NULL;
/*
* Check the line dimensions.
*/
if(dim < strlen(line) + 1) {
_err_record_msg(glh->err, "'dim' argument inconsistent with strlen(line)",
END_ERR_MSG);
errno = EINVAL;
return NULL;
};
/*
* Preserve the input line if needed.
*/
if(_glh_prepare_for_recall(glh, line))
return NULL;
/*
* From where should we start the search?
*/
if(glh->recall) {
node = glh->recall->prev;
old_line = glh->recall->line;
} else {
node = glh->list.tail;
old_line = NULL;
};
/*
* Search backwards through the list for the first match with the
* prefix string that differs from the last line that was recalled.
*/
while(node && (node->group != glh->group || node->line == old_line ||
!_glh_line_matches_prefix(node->line, glh->prefix)))
node = node->prev;
/*
* Was a matching line found?
*/
if(node) {
/*
* Recall the found node as the starting point for subsequent
* searches.
*/
glh->recall = node;
/*
* Copy the matching line into the provided line buffer.
*/
_glh_return_line(node->line, line, dim);
/*
* Return it.
*/
return line;
};
/*
* No match was found.
*/
return NULL;
}
/*.......................................................................
* Recall the next newest line that has the search prefix last recorded
* by _glh_search_prefix().
*
* Input:
* glh GlHistory * The input-line history maintenance object.
* line char * The input line buffer. On input this should contain
* the current input line, and on output, if anything
* was found, its contents will have been replaced
* with the matching line.
* dim size_t The allocated dimensions of the line buffer.
* Output:
* return char * The line requested, or NULL if no matching line
* was found.
*/
char *_glh_find_forwards(GlHistory *glh, char *line, size_t dim)
{
GlhLineNode *node; /* The line location node being checked */
GlhHashNode *old_line; /* The previous recalled line */
/*
* Check the arguments.
*/
if(!glh || !line) {
if(glh)
_err_record_msg(glh->err, "NULL argument(s)", END_ERR_MSG);
errno = EINVAL;
return NULL;
};
/*
* Is history enabled?
*/
if(!glh->enable || !glh->buffer || glh->max_lines == 0)
return NULL;
/*
* Check the line dimensions.
*/
if(dim < strlen(line) + 1) {
_err_record_msg(glh->err, "'dim' argument inconsistent with strlen(line)",
END_ERR_MSG);
errno = EINVAL;
return NULL;
};
/*
* From where should we start the search?
*/
if(glh->recall) {
node = glh->recall->next;
old_line = glh->recall->line;
} else {
return NULL;
};
/*
* Search forwards through the list for the first match with the
* prefix string.
*/
while(node && (node->group != glh->group || node->line == old_line ||
!_glh_line_matches_prefix(node->line, glh->prefix)))
node = node->next;
/*
* Was a matching line found?
*/
if(node) {
/*
* Copy the matching line into the provided line buffer.
*/
_glh_return_line(node->line, line, dim);
/*
* Record the starting point of the next search.
*/
glh->recall = node;
/*
* If we just returned the line that was being entered when the search
* session first started, cancel the search.
*/
if(node == glh->list.tail)
_glh_cancel_search(glh);
/*
* Return the matching line to the user.
*/
return line;
};
/*
* No match was found.
*/
return NULL;
}
/*.......................................................................
* If a search is in progress, cancel it.
*
* This involves discarding the line that was temporarily saved by
* _glh_find_backwards() when the search was originally started,
* and reseting the search iteration pointer to NULL.
*
* Input:
* glh GlHistory * The input-line history maintenance object.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int _glh_cancel_search(GlHistory *glh)
{
/*
* Check the arguments.
*/
if(!glh) {
errno = EINVAL;
return 1;
};
/*
* If there wasn't a search in progress, do nothing.
*/
if(!glh->recall)
return 0;
/*
* Reset the search pointers. Note that it is essential to set
* glh->recall to NULL before calling _glh_discard_line(), to avoid an
* infinite recursion.
*/
glh->recall = NULL;
/*
* Delete the node of the preserved line.
*/
_glh_discard_line(glh, glh->list.tail);
return 0;
}
/*.......................................................................
* Set the prefix of subsequent history searches.
*
* Input:
* glh GlHistory * The input-line history maintenance object.
* line const char * The command line who's prefix is to be used.
* prefix_len int The length of the prefix.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int _glh_search_prefix(GlHistory *glh, const char *line, int prefix_len)
{
/*
* Check the arguments.
*/
if(!glh) {
errno = EINVAL;
return 1;
};
/*
* Is history enabled?
*/
if(!glh->enable || !glh->buffer || glh->max_lines == 0)
return 0;
/*
* Discard any existing prefix.
*/
glh->prefix = _glh_discard_copy(glh, glh->prefix);
/*
* Only store a copy of the prefix string if it isn't a zero-length string.
*/
if(prefix_len > 0) {
/*
* Get a reference-counted copy of the prefix from the history cache buffer.
*/
glh->prefix = _glh_acquire_copy(glh, line, prefix_len);
/*
* Was there insufficient buffer space?
*/
if(!glh->prefix) {
_err_record_msg(glh->err, "The search prefix is too long to store",
END_ERR_MSG);
errno = ENOMEM;
return 1;
};
};
return 0;
}
/*.......................................................................
* Recall the oldest recorded line.
*
* Input:
* glh GlHistory * The input-line history maintenance object.
* line char * The input line buffer. On input this should contain
* the current input line, and on output, its contents
* will have been replaced with the oldest line.
* dim size_t The allocated dimensions of the line buffer.
* Output:
* return char * A pointer to line[0], or NULL if not found.
*/
char *_glh_oldest_line(GlHistory *glh, char *line, size_t dim)
{
GlhLineNode *node; /* The line location node being checked */
/*
* Check the arguments.
*/
if(!glh || !line) {
if(glh)
_err_record_msg(glh->err, "NULL argument(s)", END_ERR_MSG);
errno = EINVAL;
return NULL;
};
/*
* Is history enabled?
*/
if(!glh->enable || !glh->buffer || glh->max_lines == 0)
return NULL;
/*
* Check the line dimensions.
*/
if(dim < strlen(line) + 1) {
_err_record_msg(glh->err, "'dim' argument inconsistent with strlen(line)",
END_ERR_MSG);
errno = EINVAL;
return NULL;
};
/*
* Preserve the input line if needed.
*/
if(_glh_prepare_for_recall(glh, line))
return NULL;
/*
* Locate the oldest line that belongs to the current group.
*/
for(node=glh->list.head; node && node->group != glh->group;
node = node->next)
;
/*
* No line found?
*/
if(!node)
return NULL;
/*
* Record the above node as the starting point for subsequent
* searches.
*/
glh->recall = node;
/*
* Copy the recalled line into the provided line buffer.
*/
_glh_return_line(node->line, line, dim);
/*
* If we just returned the line that was being entered when the search
* session first started, cancel the search.
*/
if(node == glh->list.tail)
_glh_cancel_search(glh);
return line;
}
/*.......................................................................
* Recall the line that was being entered when the search started.
*
* Input:
* glh GlHistory * The input-line history maintenance object.
* line char * The input line buffer. On input this should contain
* the current input line, and on output, its contents
* will have been replaced with the line that was
* being entered when the search was started.
* dim size_t The allocated dimensions of the line buffer.
* Output:
* return char * A pointer to line[0], or NULL if not found.
*/
char *_glh_current_line(GlHistory *glh, char *line, size_t dim)
{
/*
* Check the arguments.
*/
if(!glh || !line) {
if(glh)
_err_record_msg(glh->err, "NULL argument(s)", END_ERR_MSG);
errno = EINVAL;
return NULL;
};
/*
* If history isn't enabled, or no history search has yet been started,
* ignore the call.
*/
if(!glh->enable || !glh->buffer || glh->max_lines == 0 || !glh->recall)
return NULL;
/*
* Check the line dimensions.
*/
if(dim < strlen(line) + 1) {
_err_record_msg(glh->err, "'dim' argument inconsistent with strlen(line)",
END_ERR_MSG);
errno = EINVAL;
return NULL;
};
/*
* Copy the recalled line into the provided line buffer.
*/
_glh_return_line(glh->list.tail->line, line, dim);
/*
* Since we have returned to the starting point of the search, cancel it.
*/
_glh_cancel_search(glh);
return line;
}
/*.......................................................................
* Query the id of a history line offset by a given number of lines from
* the one that is currently being recalled. If a recall session isn't
* in progress, or the offset points outside the history list, 0 is
* returned.
*
* Input:
* glh GlHistory * The input-line history maintenance object.
* offset int The line offset (0 for the current line, < 0
* for an older line, > 0 for a newer line.
* Output:
* return GlhLineID The identifier of the line that is currently
* being recalled, or 0 if no recall session is
* currently in progress.
*/
GlhLineID _glh_line_id(GlHistory *glh, int offset)
{
GlhLineNode *node; /* The line location node being checked */
/*
* Is history enabled?
*/
if(!glh->enable || !glh->buffer || glh->max_lines == 0)
return 0;
/*
* Search forward 'offset' lines to find the required line.
*/
if(offset >= 0) {
for(node=glh->recall; node && offset != 0; node=node->next) {
if(node->group == glh->group)
offset--;
};
} else {
for(node=glh->recall; node && offset != 0; node=node->prev) {
if(node->group == glh->group)
offset++;
};
};
return node ? node->id : 0;
}
/*.......................................................................
* Recall a line by its history buffer ID. If the line is no longer
* in the buffer, or the id is zero, NULL is returned.
*
* Input:
* glh GlHistory * The input-line history maintenance object.
* id GlhLineID The ID of the line to be returned.
* line char * The input line buffer. On input this should contain
* the current input line, and on output, its contents
* will have been replaced with the saved line.
* dim size_t The allocated dimensions of the line buffer.
* Output:
* return char * A pointer to line[0], or NULL if not found.
*/
char *_glh_recall_line(GlHistory *glh, GlhLineID id, char *line, size_t dim)
{
GlhLineNode *node; /* The line location node being checked */
/*
* Is history enabled?
*/
if(!glh->enable || !glh->buffer || glh->max_lines == 0)
return NULL;
/*
* Preserve the input line if needed.
*/
if(_glh_prepare_for_recall(glh, line))
return NULL;
/*
* Search for the specified line.
*/
node = _glh_find_id(glh, id);
/*
* Not found?
*/
if(!node || node->group != glh->group)
return NULL;
/*
* Record the node of the matching line as the starting point
* for subsequent searches.
*/
glh->recall = node;
/*
* Copy the recalled line into the provided line buffer.
*/
_glh_return_line(node->line, line, dim);
return line;
}
/*.......................................................................
* Save the current history in a specified file.
*
* Input:
* glh GlHistory * The input-line history maintenance object.
* filename const char * The name of the new file to record the
* history in.
* comment const char * Extra information such as timestamps will
* be recorded on a line started with this
* string, the idea being that the file can
* double as a command file. Specify "" if
* you don't care.
* max_lines int The maximum number of lines to save, or -1
* to save all of the lines in the history
* list.
* Output:
* return int 0 - OK.
* 1 - Error.