-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibtecla.h
1834 lines (1715 loc) · 81.9 KB
/
libtecla.h
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
#ifndef libtecla_h
#define libtecla_h
/*
* 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.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h> /* FILE * */
#include <stdlib.h> /* size_t */
#include <time.h> /* time_t */
#include <signal.h> /* struct sigaction */
/*
* The following are the three components of the libtecla version number.
* Note that it is better to use the libtecla_version() function than these
* macros since the macros only tell you which version of the library your
* code was compiled against, whereas the libtecla_version() function
* tells you which version of the shared tecla library your program is
* actually linked to.
*/
#define TECLA_MAJOR_VER 1
#define TECLA_MINOR_VER 6
#define TECLA_MICRO_VER 3
/*.......................................................................
* Query the version number of the tecla library.
*
* Input:
* major int * The major version number of the library
* will be assigned to *major. This number is
* only incremented when a change to the library is
* made that breaks binary (shared library) and/or
* compilation backwards compatibility.
* minor int * The minor version number of the library
* will be assigned to *minor. This number is
* incremented whenever new functions are added to
* the public API.
* micro int * The micro version number of the library will be
* assigned to *micro. This number is incremented
* whenever internal changes are made that don't
* change the public API, such as bug fixes and
* performance enhancements.
*/
void libtecla_version(int *major, int *minor, int *micro);
/*-----------------------------------------------------------------------
* The getline module provides interactive command-line input, recall
* and editing by users at terminals. See the gl_getline(3) man page for
* more details.
*-----------------------------------------------------------------------*/
/*
* Provide an opaque handle for the resource object that is defined in
* getline.h.
*/
typedef struct GetLine GetLine;
/*
* The following two functions are used to create and delete the
* resource objects that are used by the gl_getline() function.
*/
GetLine *new_GetLine(size_t linelen, size_t histlen);
GetLine *del_GetLine(GetLine *gl);
/*
* Read a line into an internal buffer of gl.
*/
char *gl_get_line(GetLine *gl, const char *prompt, const char *start_line,
int start_pos);
/*.......................................................................
* Prompt the user for a single-character reply.
*
* Input:
* gl GetLine * A resource object returned by new_GetLine().
* prompt char * The prompt to prefix the query with, or NULL
* to reuse the previous prompt.
* defchar char The character to substitute if the
* user simply hits return, or '\n' if you don't
* need to substitute anything.
* Output:
* return int The character that was read, or EOF if the read
* had to be aborted (in which case you can call
* gl_return_status() to find out why).
*/
int gl_query_char(GetLine *gl, const char *prompt, char defchar);
/*.......................................................................
* Read a single uninterpretted character from the user, without
* displaying anything.
*
* Input:
* gl GetLine * A resource object previously returned by
* new_GetLine().
* Output:
* return int The character that was read, or EOF if the read
* had to be aborted (in which case you can call
* gl_return_status() to find out why).
*/
int gl_read_char(GetLine *gl);
/*
* Configure the application specific and/or user-specific behavior of
* gl_get_line().
*/
int gl_configure_getline(GetLine *gl, const char *app_string,
const char *app_file, const char *user_file);
/*
* The following enumerators specify the origin of a key binding, and
* are listed in order of decreasing priority, such that user-specified
* key-bindings take precedence over application default bindings.
*/
typedef enum {
GL_USER_KEY, /* A key-binding specified by the user */
GL_APP_KEY /* A key-binding specified by the application */
} GlKeyOrigin;
/*
* Bind a key sequence to a given action. If action==NULL, unbind the
* key-sequence.
*/
int gl_bind_keyseq(GetLine *gl, GlKeyOrigin origin, const char *keyseq,
const char *action);
/*-----------------------------------------------------------------------
* The file-expansion module provides facilities for expanding ~user/ and
* $envvar expressions, and for expanding glob-style wildcards.
* See the ef_expand_file(3) man page for more details.
*-----------------------------------------------------------------------*/
/*
* ExpandFile objects contain the resources needed to expand pathnames.
*/
typedef struct ExpandFile ExpandFile;
/*
* The following functions are used to create and delete the resource
* objects that are used by the ef_expand_file() function.
*/
ExpandFile *new_ExpandFile(void);
ExpandFile *del_ExpandFile(ExpandFile *ef);
/*
* A container of the following type is returned by ef_expand_file().
*/
typedef struct {
int exists; /* True if the files in files[] currently exist. */
/* This only time that this may not be true is if */
/* the input filename didn't contain any wildcards */
/* and thus wasn't matched against existing files. */
/* In this case the single entry in 'nfile' may not */
/* refer to an existing file. */
int nfile; /* The number of files in files[] */
char **files; /* An array of 'nfile' filenames. */
} FileExpansion;
/*
* The ef_expand_file() function expands a specified pathname, converting
* ~user/ and ~/ patterns at the start of the pathname to the
* corresponding home directories, replacing $envvar with the value of
* the corresponding environment variable, and then, if there are any
* wildcards, matching these against existing filenames.
*
* If no errors occur, a container is returned containing the array of
* files that resulted from the expansion. If there were no wildcards
* in the input pathname, this will contain just the original pathname
* after expansion of ~ and $ expressions. If there were any wildcards,
* then the array will contain the files that matched them. Note that
* if there were any wildcards but no existing files match them, this
* is counted as an error and NULL is returned.
*
* The supported wildcards and their meanings are:
* * - Match any sequence of zero or more characters.
* ? - Match any single character.
* [chars] - Match any single character that appears in 'chars'.
* If 'chars' contains an expression of the form a-b,
* then any character between a and b, including a and b,
* matches. The '-' character looses its special meaning
* as a range specifier when it appears at the start
* of the sequence of characters.
* [^chars] - The same as [chars] except that it matches any single
* character that doesn't appear in 'chars'.
*
* Wildcard expressions are applied to individual filename components.
* They don't match across directory separators. A '.' character at
* the beginning of a filename component must also be matched
* explicitly by a '.' character in the input pathname, since these
* are UNIX's hidden files.
*
* Input:
* fe ExpandFile * The pathname expansion resource object.
* path const char * The path name to be expanded.
* pathlen int The length of the suffix of path[] that
* constitutes the filename to be expanded,
* or -1 to specify that the whole of the
* path string should be used.
* Output:
* return FileExpansion * A pointer to a results container within the
* given ExpandFile object. This contains an
* array of the pathnames that resulted from
* expanding ~ and $ expressions and from
* matching any wildcards, sorted into lexical
* order.
*
* This container and its contents will be
* recycled on subsequent calls, so if you need
* to keep the results of two successive runs,
* you will either have to allocate a private
* copy of the array, or use two ExpandFile
* objects.
*
* On error, NULL is returned. A description
* of the error can be acquired by calling the
* ef_last_error() function.
*/
FileExpansion *ef_expand_file(ExpandFile *ef, const char *path, int pathlen);
/*.......................................................................
* Print out an array of matching files.
*
* Input:
* result FileExpansion * The container of the sorted array of
* expansions.
* fp FILE * The output stream to write to.
* term_width int The width of the terminal.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int ef_list_expansions(FileExpansion *result, FILE *fp, int term_width);
/*
* The ef_last_error() function returns a description of the last error
* that occurred in a call ef_expand_file(). Note that this message is
* contained in an array which is allocated as part of *ef, and its
* contents thus potentially change on every call to ef_expand_file().
*/
const char *ef_last_error(ExpandFile *ef);
/*-----------------------------------------------------------------------
* The WordCompletion module is used for completing incomplete words, such
* as filenames. Programs can use functions within this module to register
* their own customized completion functions.
*-----------------------------------------------------------------------*/
/*
* Ambiguous completion matches are recorded in objects of the
* following type.
*/
typedef struct WordCompletion WordCompletion;
/*
* Create a new completion object.
*/
WordCompletion *new_WordCompletion(void);
/*
* Delete a redundant completion object.
*/
WordCompletion *del_WordCompletion(WordCompletion *cpl);
/*.......................................................................
* Callback functions declared and prototyped using the following macro
* are called upon to return an array of possible completion suffixes
* for the token that precedes a specified location in the given
* input line. It is up to this function to figure out where the token
* starts, and to call cpl_add_completion() to register each possible
* completion before returning.
*
* Input:
* cpl WordCompletion * An opaque pointer to the object that will
* contain the matches. This should be filled
* via zero or more calls to cpl_add_completion().
* data void * The anonymous 'data' argument that was
* passed to cpl_complete_word() or
* gl_customize_completion()).
* line const char * The current input line.
* word_end int The index of the character in line[] which
* follows the end of the token that is being
* completed.
* Output
* return int 0 - OK.
* 1 - Error.
*/
#define CPL_MATCH_FN(fn) int (fn)(WordCompletion *cpl, void *data, \
const char *line, int word_end)
typedef CPL_MATCH_FN(CplMatchFn);
/*.......................................................................
* Optional callback functions declared and prototyped using the
* following macro are called upon to return non-zero if a given
* file, specified by its pathname, is to be included in a list of
* completions.
*
* Input:
* data void * The application specified pointer which
* was specified when this callback function
* was registered. This can be used to have
* anything you like passed to your callback.
* pathname const char * The pathname of the file to be checked to
* see if it should be included in the list
* of completions.
* Output
* return int 0 - Ignore this file.
* 1 - Do include this file in the list
* of completions.
*/
#define CPL_CHECK_FN(fn) int (fn)(void *data, const char *pathname)
typedef CPL_CHECK_FN(CplCheckFn);
/*
* You can use the following CplCheckFn callback function to only
* have executables included in a list of completions.
*/
CPL_CHECK_FN(cpl_check_exe);
/*
* cpl_file_completions() is the builtin filename completion callback
* function. This can also be called by your own custom CPL_MATCH_FN()
* callback functions. To do this pass on all of the arguments of your
* custom callback function to cpl_file_completions(), with the exception
* of the (void *data) argument. The data argument should either be passed
* NULL to request the default behaviour of the file-completion function,
* or be passed a pointer to a CplFileConf structure (see below). In the
* latter case the contents of the structure modify the behavior of the
* file-completer.
*/
CPL_MATCH_FN(cpl_file_completions);
/*
* Objects of the following type can be used to change the default
* behavior of the cpl_file_completions() callback function.
*/
typedef struct CplFileConf CplFileConf;
/*
* If you want to change the behavior of the cpl_file_completions()
* callback function, call the following function to allocate a
* configuration object, then call one or more of the subsequent
* functions to change any of the default configuration parameters
* that you don't want. This function returns NULL when there is
* insufficient memory.
*/
CplFileConf *new_CplFileConf(void);
/*
* If backslashes in the prefix being passed to cpl_file_completions()
* should be treated as literal characters, call the following function
* with literal=1. Otherwise the default is to treat them as escape
* characters which remove the special meanings of spaces etc..
*/
void cfc_literal_escapes(CplFileConf *cfc, int literal);
/*
* Before calling cpl_file_completions(), call this function if you
* know the index at which the filename prefix starts in the input line.
* Otherwise by default, or if you specify start_index to be -1, the
* filename is taken to start after the first unescaped space preceding
* the cursor, or the start of the line, which ever comes first.
*/
void cfc_file_start(CplFileConf *cfc, int start_index);
/*
* If you only want certain types of files to be included in the
* list of completions, use the following function to specify a
* callback function which will be called to ask whether a given file
* should be included. The chk_data argument is will be passed to the
* callback function whenever it is called and can be anything you want.
*/
void cfc_set_check_fn(CplFileConf *cfc, CplCheckFn *chk_fn, void *chk_data);
/*
* The following function deletes a CplFileConf objects previously
* returned by new_CplFileConf(). It always returns NULL.
*/
CplFileConf *del_CplFileConf(CplFileConf *cfc);
/*
* The following configuration structure is deprecated. Do not change
* its contents, since this will break any programs that still use it,
* and don't use it in new programs. Instead use opaque CplFileConf
* objects as described above. cpl_file_completions() figures out
* what type of structure you pass it, by virtue of a magic int code
* placed at the start of CplFileConf object by new_CplFileConf().
*/
typedef struct {
int escaped; /* Opposite to the argument of cfc_literal_escapes() */
int file_start; /* Equivalent to the argument of cfc_file_start() */
} CplFileArgs;
/*
* This initializes the deprecated CplFileArgs structures.
*/
void cpl_init_FileArgs(CplFileArgs *cfa);
/*.......................................................................
* When an error occurs while performing a completion, custom completion
* callback functions should register a terse description of the error
* by calling cpl_record_error(). This message will then be returned on
* the next call to cpl_last_error() and used by getline to display an
* error message to the user.
*
* Input:
* cpl WordCompletion * The string-completion resource object that was
* originally passed to the callback.
* errmsg const char * The description of the error.
*/
void cpl_record_error(WordCompletion *cpl, const char *errmsg);
/*.......................................................................
* This function can be used to replace the builtin filename-completion
* function with one of the user's choice. The user's completion function
* has the option of calling the builtin filename-completion function
* if it believes that the token that it has been presented with is a
* filename (see cpl_file_completions() above).
*
* Input:
* gl GetLine * The resource object of the command-line input
* module.
* data void * This is passed to match_fn() whenever it is
* called. It could, for example, point to a
* symbol table that match_fn() would look up
* matches in.
* match_fn CplMatchFn * The function that will identify the prefix
* to be completed from the input line, and
* report matching symbols.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int gl_customize_completion(GetLine *gl, void *data, CplMatchFn *match_fn);
/*.......................................................................
* This function allows you to install alternate completion action
* functions or completion listing functions, or to change the
* completion function of an existing action of the same type. This
* should preferably be called before the first call to gl_get_line()
* so that the name of the action becomes defined before the user's
* configuration file is read.
*
* Input:
* gl GetLine * The resource object of the command-line input
* module.
* data void * This is passed to match_fn() whenever it is
* called. It could, for example, point to a
* symbol table that match_fn() would look up
* matches in.
* match_fn CplMatchFn * The function that will identify the prefix
* to be completed from the input line, and
* report matching symbols.
* list_only int If non-zero, install an action that only lists
* possible completions, rather than attempting
* to perform the completion.
* name const char * The name with which users can refer to the
* binding in tecla configuration files.
* keyseq const char * The key sequence with which to invoke
* the binding. This should be specified in the
* same manner as key-sequences in tecla
* configuration files (eg. "M-^I").
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int gl_completion_action(GetLine *gl, void *data, CplMatchFn *match_fn,
int list_only, const char *name, const char *keyseq);
/*.......................................................................
* Change the terminal (or stream) that getline interacts with.
*
* Input:
* gl GetLine * The resource object of the command-line input
* module.
* input_fp FILE * The stdio stream to read from.
* output_fp FILE * The stdio stream to write to.
* term const char * The terminal type. This can be NULL if
* either or both of input_fp and output_fp don't
* refer to a terminal. Otherwise it should refer
* to an entry in the terminal information database.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int gl_change_terminal(GetLine *gl, FILE *input_fp, FILE *output_fp,
const char *term);
/*.......................................................................
* The following functions can be used to save and restore the contents
* of the history buffer.
*
* Input:
* gl GetLine * The resource object of the command-line input
* module.
* filename const char * The name of the new file to write to.
* 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. Be sure to specify the
* same string to both functions.
* 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.
*/
int gl_save_history(GetLine *gl, const char *filename, const char *comment,
int max_lines);
int gl_load_history(GetLine *gl, const char *filename, const char *comment);
/*
* Enumerate file-descriptor events that can be waited for.
*/
typedef enum {
GLFD_READ, /* Watch for data waiting to be read from a file descriptor */
GLFD_WRITE, /* Watch for ability to write to a file descriptor */
GLFD_URGENT /* Watch for urgent out-of-band data on the file descriptor */
} GlFdEvent;
/*
* The following enumeration is used for the return status of file
* descriptor event callbacks.
*/
typedef enum {
GLFD_ABORT, /* Cause gl_get_line() to abort with an error */
GLFD_REFRESH, /* Redraw the input line and continue waiting for input */
GLFD_CONTINUE /* Continue to wait for input, without redrawing the line */
} GlFdStatus;
/*.......................................................................
* On systems that have the select() system call, while gl_get_line()
* is waiting for terminal input, it can also be asked to listen for
* activity on arbitrary file descriptors. Callback functions of the
* following type can be registered to be called when activity is
* seen. If your callback needs to write to the terminal or use
* signals, please see the gl_get_line(3) man page.
*
* Input:
* gl GetLine * The gl_get_line() resource object. You can use
* this safely to call gl_watch_fd() or
* gl_inactivity_timeout(). The effect of calling other
* functions that take a gl argument is undefined,
* and must be avoided.
* data void * A pointer to arbitrary callback data, as originally
* registered with gl_watch_fd().
* fd int The file descriptor that has activity.
* event GlFdEvent The activity seen on the file descriptor. The
* inclusion of this argument allows the same
* callback to be registered for multiple events.
* Output:
* return GlFdStatus GLFD_ABORT - Cause gl_get_line() to abort with
* an error (set errno if you need it).
* GLFD_REFRESH - Redraw the input line and continue
* waiting for input. Use this if you
* wrote something to the terminal.
* GLFD_CONTINUE - Continue to wait for input, without
* redrawing the line.
*/
#define GL_FD_EVENT_FN(fn) GlFdStatus (fn)(GetLine *gl, void *data, int fd, \
GlFdEvent event)
typedef GL_FD_EVENT_FN(GlFdEventFn);
/*.......................................................................
* Where possible, register a function and associated data to be called
* whenever a specified event is seen on a file descriptor.
*
* Input:
* gl GetLine * The resource object of the command-line input
* module.
* fd int The file descriptor to watch.
* event GlFdEvent The type of activity to watch for.
* callback GlFdEventFn * The function to call when the specified
* event occurs. Setting this to 0 removes
* any existing callback.
* data void * A pointer to arbitrary data to pass to the
* callback function.
* Output:
* return int 0 - OK.
* 1 - Either gl==NULL, or this facility isn't
* available on the the host system
* (ie. select() isn't available). No
* error message is generated in the latter
* case.
*/
int gl_watch_fd(GetLine *gl, int fd, GlFdEvent event,
GlFdEventFn *callback, void *data);
/*
* Enumerators from the following list are returned by activity
* timeout callbacks registered by gl_inactivity_timeout(). They tell
* gl_get_line() whether and how to procede.
*/
typedef enum {
GLTO_ABORT, /* Cause gl_get_line() to abort with an error */
GLTO_REFRESH, /* Redraw the input line and continue waiting for input */
GLTO_CONTINUE /* Continue to wait for input, without redrawing the line */
} GlAfterTimeout;
/*.......................................................................
* On systems that have the select() system call, the application has
* the option of providing a callback function of the following type,
* which is called whenever no terminal input or other I/O activity is
* seen for the timeout duration specified in the last call to
* gl_inactivity_timeout().
*
* Input:
* gl GetLine * The gl_get_line() resource object. You can use
* this safely to call gl_watch_fd() or
* gl_inactivity_timeout(). The effect of calling other
* functions that take a gl argument is undefined,
* and must be avoided.
* data void * A pointer to arbitrary callback data, as
* originally registered with gl_inactivity_timeout().
* Output:
* return GlAfterTimeout GLTO_ABORT - Cause gl_get_line() to
* abort with an error (set
* errno if you need it).
* GLTO_REFRESH - Redraw the input line and
* continue waiting for
* input. Use this if you
* wrote something to the
* terminal.
* GLTO_CONTINUE - Continue to wait for
* input, without redrawing
* the line.
*/
#define GL_TIMEOUT_FN(fn) GlAfterTimeout (fn)(GetLine *gl, void *data)
typedef GL_TIMEOUT_FN(GlTimeoutFn);
/*.......................................................................
* On systems with the select() system call, the gl_inactivity_timeout()
* function provides the option of setting (or cancelling) an
* inactivity timeout. Inactivity, in this case, refers both to
* terminal input received from the user, and to I/O on any file
* descriptors registered by calls to gl_watch_fd(). If at any time,
* no activity is seen for the requested time period, the specified
* timeout callback function is called. On returning, this callback
* returns a code which tells gl_get_line() what to do next. Note that
* each call to gl_inactivity_timeout() replaces any previously installed
* timeout callback, and that specifying a callback of 0, turns off
* inactivity timing.
*
* Beware that although the timeout argument includes a nano-second
* component, few computer clocks presently have resolutions finer
* than a few milliseconds, so asking for less than a few milliseconds
* is equivalent to zero on a lot of systems.
*
* Input:
* gl GetLine * The resource object of the command-line input
* module.
* callback GlTimeoutFn * The function to call when the inactivity
* timeout is exceeded. To turn off
* inactivity timeouts altogether, send 0.
* data void * A pointer to arbitrary data to pass to the
* callback function.
* sec unsigned long The number of whole seconds in the timeout.
* nsec unsigned long The fractional number of seconds in the
* timeout, expressed in nano-seconds (see
* the caveat above).
* Output:
* return int 0 - OK.
* 1 - Either gl==NULL, or this facility isn't
* available on the the host system
* (ie. select() isn't available). No
* error message is generated in the latter
* case.
*/
int gl_inactivity_timeout(GetLine *gl, GlTimeoutFn *timeout_fn, void *data,
unsigned long sec, unsigned long nsec);
/*.......................................................................
* Switch history streams. History streams represent separate history
* lists recorded within a single history buffer. Different streams
* are distinguished by integer identifiers chosen by the calling
* appplicaton. Initially new_GetLine() sets the stream identifier to
* 0. Whenever a new line is appended to the history list, the current
* stream identifier is recorded with it, and history lookups only
* consider lines marked with the current stream identifier.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* id unsigned The new history stream identifier.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int gl_group_history(GetLine *gl, unsigned id);
/*.......................................................................
* Display the contents of the history list.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* fp FILE * The stdio output stream to write to.
* fmt const char * A format string. This containing characters to be
* written verbatim, plus any of the following
* format directives:
* %D - The date, formatted like 2001-11-20
* %T - The time of day, formatted like 23:59:59
* %N - The sequential entry number of the
* line in the history buffer.
* %G - The number of the history group that
* the line belongs to.
* %% - A literal % character.
* %H - The history line itself.
* Note that a '\n' newline character is not
* appended by default.
* all_groups int If true, display history lines from all
* history groups. Otherwise only display
* those of the current history group.
* max_lines int If max_lines is < 0, all available lines
* are displayed. Otherwise only the most
* recent max_lines lines will be displayed.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int gl_show_history(GetLine *gl, FILE *fp, const char *fmt, int all_groups,
int max_lines);
/*.......................................................................
* Resize or delete the history buffer.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* bufsize size_t The number of bytes in the history buffer, or 0
* to delete the buffer completely.
* Output:
* return int 0 - OK.
* 1 - Insufficient memory (the previous buffer
* will have been retained). No error message
* will be displayed.
*/
int gl_resize_history(GetLine *gl, size_t bufsize);
/*.......................................................................
* Set an upper limit to the number of lines that can be recorded in the
* history list, or remove a previously specified limit.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* max_lines int The maximum number of lines to allow, or -1 to
* cancel a previous limit and allow as many lines
* as will fit in the current history buffer size.
*/
void gl_limit_history(GetLine *gl, int max_lines);
/*.......................................................................
* Discard either all historical lines, or just those associated with the
* current history group.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* all_groups int If true, clear all of the history. If false,
* clear only the stored lines associated with the
* currently selected history group.
*/
void gl_clear_history(GetLine *gl, int all_groups);
/*.......................................................................
* Temporarily enable or disable the gl_get_line() history mechanism.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* enable int If true, turn on the history mechanism. If
* false, disable it.
*/
void gl_toggle_history(GetLine *gl, int enable);
/*
* Objects of the following type are returned by gl_terminal_size().
*/
typedef struct {
int nline; /* The terminal has nline lines */
int ncolumn; /* The terminal has ncolumn columns */
} GlTerminalSize;
/*.......................................................................
* Update if necessary, and return the current size of the terminal.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* def_ncolumn int If the number of columns in the terminal
* can't be determined, substitute this number.
* def_nline int If the number of lines in the terminal can't
* be determined, substitute this number.
* Output:
* return GlTerminalSize The current terminal size.
*/
GlTerminalSize gl_terminal_size(GetLine *gl, int def_ncolumn, int def_nline);
/*.......................................................................
* Tell gl_get_line() the current terminal size. Note that this is only
* necessary on systems where changes in terminal size aren't reported
* via SIGWINCH.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* ncolumn int The number of columns in the terminal.
* nline int The number of rows in the terminal.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int gl_set_term_size(GetLine *gl, int ncolumn, int nline);
/*
* The gl_lookup_history() function returns information in an
* argument of the following type.
*/
typedef struct {
const char *line; /* The requested history line */
unsigned group; /* The history group to which the */
/* line belongs. */
time_t timestamp; /* The date and time at which the */
/* line was originally entered. */
} GlHistoryLine;
/*.......................................................................
* Lookup a history line by its sequential number of entry in the
* history buffer.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* id unsigned long The identification number of the line to
* be returned, where 0 denotes the first line
* that was entered in the history list, and
* each subsequently added line has a number
* one greater than the previous one. For
* the range of lines currently in the list,
* see the gl_range_of_history() function.
* Input/Output:
* line GlHistoryLine * A pointer to the variable in which to
* return the details of the line.
* Output:
* return int 0 - The line is no longer in the history
* list, and *line has not been changed.
* 1 - The requested line can be found in
* *line. Note that the string in
* line->line is part of the history
* buffer and will change, so a private
* copy should be made if you wish to
* use it after subsequent calls to any
* functions that take gl as an argument.
*/
int gl_lookup_history(GetLine *gl, unsigned long id, GlHistoryLine *line);
/*
* The gl_state_of_history() function returns information in an argument
* of the following type.
*/
typedef struct {
int enabled; /* True if history is enabled */
unsigned group; /* The current history group */
int max_lines; /* The current upper limit on the number of lines */
/* in the history list, or -1 if unlimited. */
} GlHistoryState;
/*.......................................................................
* Query the state of the history list. Note that any of the input/output
* pointers can be specified as NULL.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* Input/Output:
* state GlHistoryState * A pointer to the variable in which to record
* the return values.
*/
void gl_state_of_history(GetLine *gl, GlHistoryState *state);
/*
* The gl_range_of_history() function returns information in an argument
* of the following type.
*/
typedef struct {
unsigned long oldest; /* The sequential entry number of the oldest */
/* line in the history list. */
unsigned long newest; /* The sequential entry number of the newest */
/* line in the history list. */
int nlines; /* The number of lines in the history list */
} GlHistoryRange;
/*.......................................................................
* Query the number and range of lines in the history buffer.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* range GlHistoryRange * A pointer to the variable in which to record
* the return values. If range->nline=0, the
* range of lines will be given as 0-0.
*/
void gl_range_of_history(GetLine *gl, GlHistoryRange *range);
/*
* The gl_size_of_history() function returns information in an argument
* of the following type.
*/
typedef struct {
size_t size; /* The size of the history buffer (bytes) */
size_t used; /* The number of bytes of the history buffer */
/* that are currently occupied. */
} GlHistorySize;
/*.......................................................................
* Return the size of the history buffer and the amount of the
* buffer that is currently in use.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* Input/Output:
* GlHistorySize size * A pointer to the variable in which to return
* the results.
*/
void gl_size_of_history(GetLine *gl, GlHistorySize *size);
/*.......................................................................
* Enable or disable the automatic addition of newly entered lines to the
* history list.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* enable int If true, subsequently entered lines will
* automatically be added to the history list
* before they are returned to the caller of
* gl_get_line(). If 0, the choice of how and
* when to archive lines in the history list,
* is left up to the calling application, which
* can do so via calls to gl_append_history().
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int gl_automatic_history(GetLine *gl, int enable);
/*.......................................................................
* Append a specified line to the history list.
*
* Input:
* gl GetLine * The resource object of gl_get_line().
* line const char * The line to be added.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int gl_append_history(GetLine *gl, const char *line);
/*.......................................................................
* Specify whether text that users type should be displayed or hidden.
* In the latter case, only the prompt is displayed, and the final
* input line is not archived in the history list.
*
* Input:
* gl GetLine * The input-line history maintenance object.
* enable int 0 - Disable echoing.
* 1 - Enable echoing.
* -1 - Just query the mode without changing it.
* Output:
* return int The echoing disposition that was in effect
* before this function was called:
* 0 - Echoing was disabled.
* 1 - Echoing was enabled.
*/
int gl_echo_mode(GetLine *gl, int enable);
/*.......................................................................
* This function can be called from gl_get_line() callbacks to have
* the prompt changed when they return. It has no effect if gl_get_line()
* is not currently being invoked.
*