-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetline.c
12849 lines (12436 loc) · 391 KB
/
getline.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.
*/
/*
* Standard headers.
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <setjmp.h>
#include <stdarg.h>
/*
* UNIX headers.
*/
#include <sys/ioctl.h>
#ifdef HAVE_SELECT
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <sys/time.h>
#include <sys/types.h>
#endif
/*
* Handle the different sources of terminal control string and size
* information. Note that if no terminal information database is available,
* ANSI VT100 control sequences are used.
*/
#if defined(USE_TERMINFO) || defined(USE_TERMCAP)
/*
* Include curses.h or ncurses/curses.h depending on which is available.
*/
#ifdef HAVE_CURSES_H
#include <curses.h>
#elif defined(HAVE_NCURSES_CURSES_H)
#include <ncurses/curses.h>
#endif
/*
* Include term.h where available.
*/
#if defined(HAVE_TERM_H)
#include <term.h>
#elif defined(HAVE_NCURSES_TERM_H)
#include <ncurses/term.h>
#endif
/*
* When using termcap, include termcap.h on systems that have it.
* Otherwise assume that all prototypes are provided by curses.h.
*/
#if defined(USE_TERMCAP) && defined(HAVE_TERMCAP_H)
#include <termcap.h>
#endif
/*
* Under Solaris default Curses the output function that tputs takes is
* declared to have a char argument. On all other systems and on Solaris
* X/Open Curses (Issue 4, Version 2) it expects an int argument (using
* c89 or options -I /usr/xpg4/include -L /usr/xpg4/lib -R /usr/xpg4/lib
* selects XPG4v2 Curses on Solaris 2.6 and later).
*
* Similarly, under Mac OS X, the return value of the tputs output
* function is declared as void, whereas it is declared as int on
* other systems.
*/
#if defined __sun && defined __SVR4 && !defined _XOPEN_CURSES
typedef int TputsRetType;
typedef char TputsArgType; /* int tputs(char c, FILE *fp) */
#define TPUTS_RETURNS_VALUE 1
#elif defined(__APPLE__) && defined(__MACH__)
typedef void TputsRetType;
typedef int TputsArgType; /* void tputs(int c, FILE *fp) */
#define TPUTS_RETURNS_VALUE 0
#else
typedef int TputsRetType;
typedef int TputsArgType; /* int tputs(int c, FILE *fp) */
#define TPUTS_RETURNS_VALUE 1
#endif
/*
* Use the above specifications to prototype our tputs callback function.
*/
static TputsRetType gl_tputs_putchar(TputsArgType c);
#endif /* defined(USE_TERMINFO) || defined(USE_TERMCAP) */
/*
* If the library is being compiled without filesystem access facilities,
* ensure that none of the action functions that normally do access the
* filesystem are bound by default, and that it they do get bound, that
* they don't do anything.
*/
#if WITHOUT_FILE_SYSTEM
#define HIDE_FILE_SYSTEM
#endif
/*
* POSIX headers.
*/
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
/*
* Provide typedefs for standard POSIX structures.
*/
typedef struct sigaction SigAction;
typedef struct termios Termios;
/*
* Which flag is used to select non-blocking I/O with fcntl()?
*/
#undef NON_BLOCKING_FLAG
#if defined(O_NONBLOCK)
#define NON_BLOCKING_FLAG (O_NONBLOCK)
#elif defined(O_NDELAY)
#define NON_BLOCKING_FLAG (O_NDELAY)
#endif
/*
* What value should we give errno if I/O blocks when it shouldn't.
*/
#undef BLOCKED_ERRNO
#if defined(EAGAIN)
#define BLOCKED_ERRNO (EAGAIN)
#elif defined(EWOULDBLOCK)
#define BLOCKED_ERRNO (EWOULDBLOCK)
#elif defined(EIO)
#define BLOCKED_ERRNO (EIO)
#else
#define BLOCKED_ERRNO 0
#endif
/*
* Local headers.
*/
#ifndef WITHOUT_FILE_SYSTEM
#include "pathutil.h"
#endif
#include "libtecla.h"
#include "keytab.h"
#include "getline.h"
#include "ioutil.h"
#include "history.h"
#include "freelist.h"
#include "stringrp.h"
#include "chrqueue.h"
#include "cplmatch.h"
#ifndef WITHOUT_FILE_SYSTEM
#include "expand.h"
#endif
#include "errmsg.h"
/*
* Enumerate the available editing styles.
*/
typedef enum {
GL_EMACS_MODE, /* Emacs style editing */
GL_VI_MODE, /* Vi style editing */
GL_NO_EDITOR /* Fall back to the basic OS-provided editing */
} GlEditor;
/*
* Set the largest key-sequence that can be handled.
*/
#define GL_KEY_MAX 64
/*
* In vi mode, the following datatype is used to implement the
* undo command. It records a copy of the input line from before
* the command-mode action which edited the input line.
*/
typedef struct {
char *line; /* A historical copy of the input line */
int buff_curpos; /* The historical location of the cursor in */
/* line[] when the line was modified. */
int ntotal; /* The number of characters in line[] */
int saved; /* True once a line has been saved after the */
/* last call to gl_interpret_char(). */
} ViUndo;
/*
* In vi mode, the following datatype is used to record information
* needed by the vi-repeat-change command.
*/
typedef struct {
KtAction action; /* The last action function that made a */
/* change to the line. */
int count; /* The repeat count that was passed to the */
/* above command. */
int input_curpos; /* Whenever vi command mode is entered, the */
/* the position at which it was first left */
/* is recorded here. */
int command_curpos; /* Whenever vi command mode is entered, the */
/* the location of the cursor is recorded */
/* here. */
char input_char; /* Commands that call gl_read_terminal() */
/* record the character here, so that it can */
/* used on repeating the function. */
int saved; /* True if a function has been saved since the */
/* last call to gl_interpret_char(). */
int active; /* True while a function is being repeated. */
} ViRepeat;
/*
* The following datatype is used to encapsulate information specific
* to vi mode.
*/
typedef struct {
ViUndo undo; /* Information needed to implement the vi */
/* undo command. */
ViRepeat repeat; /* Information needed to implement the vi */
/* repeat command. */
int command; /* True in vi command-mode */
int find_forward; /* True if the last character search was in the */
/* forward direction. */
int find_onto; /* True if the last character search left the */
/* on top of the located character, as opposed */
/* to just before or after it. */
char find_char; /* The last character sought, or '\0' if no */
/* searches have been performed yet. */
} ViMode;
#ifdef HAVE_SELECT
/*
* Define a type for recording a file-descriptor callback and its associated
* data.
*/
typedef struct {
GlFdEventFn *fn; /* The callback function */
void *data; /* Anonymous data to pass to the callback function */
} GlFdHandler;
/*
* A list of nodes of the following type is used to record file-activity
* event handlers, but only on systems that have the select() system call.
*/
typedef struct GlFdNode GlFdNode;
struct GlFdNode {
GlFdNode *next; /* The next in the list of nodes */
int fd; /* The file descriptor being watched */
GlFdHandler rd; /* The callback to call when fd is readable */
GlFdHandler wr; /* The callback to call when fd is writable */
GlFdHandler ur; /* The callback to call when fd has urgent data */
};
/*
* Set the number of the above structures to allocate every time that
* the freelist of GlFdNode's becomes exhausted.
*/
#define GLFD_FREELIST_BLOCKING 10
static int gl_call_fd_handler(GetLine *gl, GlFdHandler *gfh, int fd,
GlFdEvent event);
static int gl_call_timeout_handler(GetLine *gl);
#endif
/*
* Each signal that gl_get_line() traps is described by a list node
* of the following type.
*/
typedef struct GlSignalNode GlSignalNode;
struct GlSignalNode {
GlSignalNode *next; /* The next signal in the list */
int signo; /* The number of the signal */
sigset_t proc_mask; /* A process mask which only includes signo */
SigAction original; /* The signal disposition of the calling program */
/* for this signal. */
unsigned flags; /* A bitwise union of GlSignalFlags enumerators */
GlAfterSignal after; /* What to do after the signal has been handled */
int errno_value; /* What to set errno to */
};
/*
* Set the number of the above structures to allocate every time that
* the freelist of GlSignalNode's becomes exhausted.
*/
#define GLS_FREELIST_BLOCKING 30
/*
* Completion handlers and their callback data are recorded in
* nodes of the following type.
*/
typedef struct GlCplCallback GlCplCallback;
struct GlCplCallback {
CplMatchFn *fn; /* The completion callback function */
void *data; /* Arbitrary callback data */
};
/*
* The following function is used as the default completion handler when
* the filesystem is to be hidden. It simply reports no completions.
*/
#ifdef HIDE_FILE_SYSTEM
static CPL_MATCH_FN(gl_no_completions);
#endif
/*
* Specify how many GlCplCallback nodes are added to the GlCplCallback freelist
* whenever it becomes exhausted.
*/
#define GL_CPL_FREELIST_BLOCKING 10
/*
* External action functions and their callback data are recorded in
* nodes of the following type.
*/
typedef struct GlExternalAction GlExternalAction;
struct GlExternalAction {
GlActionFn *fn; /* The function which implements the action */
void *data; /* Arbitrary callback data */
};
/*
* Specify how many GlExternalAction nodes are added to the
* GlExternalAction freelist whenever it becomes exhausted.
*/
#define GL_EXT_ACT_FREELIST_BLOCKING 10
/*
* Define the contents of the GetLine object.
* Note that the typedef for this object can be found in libtecla.h.
*/
struct GetLine {
ErrMsg *err; /* The error-reporting buffer */
GlHistory *glh; /* The line-history buffer */
WordCompletion *cpl; /* String completion resource object */
GlCplCallback cplfn; /* The completion callback */
#ifndef WITHOUT_FILE_SYSTEM
ExpandFile *ef; /* ~user/, $envvar and wildcard expansion */
/* resource object. */
#endif
StringGroup *capmem; /* Memory for recording terminal capability */
/* strings. */
GlCharQueue *cq; /* The terminal output character queue */
int input_fd; /* The file descriptor to read on */
int output_fd; /* The file descriptor to write to */
FILE *input_fp; /* A stream wrapper around input_fd */
FILE *output_fp; /* A stream wrapper around output_fd */
FILE *file_fp; /* When input is being temporarily taken from */
/* a file, this is its file-pointer. Otherwise */
/* it is NULL. */
char *term; /* The terminal type specified on the last call */
/* to gl_change_terminal(). */
int is_term; /* True if stdin is a terminal */
GlWriteFn *flush_fn; /* The function to call to write to the terminal */
GlIOMode io_mode; /* The I/O mode established by gl_io_mode() */
int raw_mode; /* True while the terminal is in raw mode */
GlPendingIO pending_io; /* The type of I/O that is currently pending */
GlReturnStatus rtn_status; /* The reason why gl_get_line() returned */
int rtn_errno; /* THe value of errno associated with rtn_status */
size_t linelen; /* The max number of characters per line */
char *line; /* A line-input buffer of allocated size */
/* linelen+2. The extra 2 characters are */
/* reserved for "\n\0". */
char *cutbuf; /* A cut-buffer of the same size as line[] */
char *prompt; /* The current prompt string */
int prompt_len; /* The length of the prompt string */
int prompt_changed; /* True after a callback changes the prompt */
int prompt_style; /* How the prompt string is displayed */
FreeList *cpl_mem; /* Memory for GlCplCallback objects */
FreeList *ext_act_mem; /* Memory for GlExternalAction objects */
FreeList *sig_mem; /* Memory for nodes of the signal list */
GlSignalNode *sigs; /* The head of the list of signals */
int signals_masked; /* True between calls to gl_mask_signals() and */
/* gl_unmask_signals() */
int signals_overriden; /* True between calls to gl_override_signals() */
/* and gl_restore_signals() */
sigset_t all_signal_set; /* The set of all signals that we are trapping */
sigset_t old_signal_set; /* The set of blocked signals on entry to */
/* gl_get_line(). */
sigset_t use_signal_set; /* The subset of all_signal_set to unblock */
/* while waiting for key-strokes */
Termios oldattr; /* Saved terminal attributes. */
KeyTab *bindings; /* A table of key-bindings */
int ntotal; /* The number of characters in gl->line[] */
int buff_curpos; /* The cursor position within gl->line[] */
int term_curpos; /* The cursor position on the terminal */
int term_len; /* The number of terminal characters used to */
/* display the current input line. */
int buff_mark; /* A marker location in the buffer */
int insert_curpos; /* The cursor position at start of insert */
int insert; /* True in insert mode */
int number; /* If >= 0, a numeric argument is being read */
int endline; /* True to tell gl_get_input_line() to return */
/* the current contents of gl->line[] */
int displayed; /* True if an input line is currently displayed */
int redisplay; /* If true, the input line will be redrawn */
/* either after the current action function */
/* returns, or when gl_get_input_line() */
/* is next called. */
int postpone; /* _gl_normal_io() sets this flag, to */
/* postpone any redisplays until */
/* is next called, to resume line editing. */
char keybuf[GL_KEY_MAX+1]; /* A buffer of currently unprocessed key presses */
int nbuf; /* The number of characters in keybuf[] */
int nread; /* The number of characters read from keybuf[] */
KtAction current_action; /* The action function that is being invoked */
int current_count; /* The repeat count passed to */
/* current_acction.fn() */
GlhLineID preload_id; /* When not zero, this should be the ID of a */
/* line in the history buffer for potential */
/* recall. */
int preload_history; /* If true, preload the above history line when */
/* gl_get_input_line() is next called. */
long keyseq_count; /* The number of key sequences entered by the */
/* the user since new_GetLine() was called. */
long last_search; /* The value of keyseq_count during the last */
/* history search operation. */
GlEditor editor; /* The style of editing, (eg. vi or emacs) */
int silence_bell; /* True if gl_ring_bell() should do nothing. */
int automatic_history; /* True to automatically archive entered lines */
/* in the history list. */
ViMode vi; /* Parameters used when editing in vi mode */
const char *left; /* The string that moves the cursor 1 character */
/* left. */
const char *right; /* The string that moves the cursor 1 character */
/* right. */
const char *up; /* The string that moves the cursor 1 character */
/* up. */
const char *down; /* The string that moves the cursor 1 character */
/* down. */
const char *home; /* The string that moves the cursor home */
const char *bol; /* Move cursor to beginning of line */
const char *clear_eol; /* The string that clears from the cursor to */
/* the end of the line. */
const char *clear_eod; /* The string that clears from the cursor to */
/* the end of the display. */
const char *u_arrow; /* The string returned by the up-arrow key */
const char *d_arrow; /* The string returned by the down-arrow key */
const char *l_arrow; /* The string returned by the left-arrow key */
const char *r_arrow; /* The string returned by the right-arrow key */
const char *sound_bell; /* The string needed to ring the terminal bell */
const char *bold; /* Switch to the bold font */
const char *underline; /* Underline subsequent characters */
const char *standout; /* Turn on standout mode */
const char *dim; /* Switch to a dim font */
const char *reverse; /* Turn on reverse video */
const char *blink; /* Switch to a blinking font */
const char *text_attr_off; /* Turn off all text attributes */
int nline; /* The height of the terminal in lines */
int ncolumn; /* The width of the terminal in columns */
#ifdef USE_TERMCAP
char *tgetent_buf; /* The buffer that is used by tgetent() to */
/* store a terminal description. */
char *tgetstr_buf; /* The buffer that is used by tgetstr() to */
/* store terminal capabilities. */
#endif
#ifdef USE_TERMINFO
const char *left_n; /* The parameter string that moves the cursor */
/* n characters left. */
const char *right_n; /* The parameter string that moves the cursor */
/* n characters right. */
#endif
char *app_file; /* The pathname of the application-specific */
/* .teclarc configuration file, or NULL. */
char *user_file; /* The pathname of the user-specific */
/* .teclarc configuration file, or NULL. */
int configured; /* True as soon as any teclarc configuration */
/* file has been read. */
int echo; /* True to display the line as it is being */
/* entered. If 0, only the prompt will be */
/* displayed, and the line will not be */
/* archived in the history list. */
int last_signal; /* The last signal that was caught by */
/* the last call to gl_get_line(), or -1 */
/* if no signal has been caught yet. */
#ifdef HAVE_SELECT
FreeList *fd_node_mem; /* A freelist of GlFdNode structures */
GlFdNode *fd_nodes; /* The list of fd event descriptions */
fd_set rfds; /* The set of fds to watch for readability */
fd_set wfds; /* The set of fds to watch for writability */
fd_set ufds; /* The set of fds to watch for urgent data */
int max_fd; /* The maximum file-descriptor being watched */
struct { /* Inactivity timeout related data */
struct timeval dt; /* The inactivity timeout when timer.fn() */
/* isn't 0 */
GlTimeoutFn *fn; /* The application callback to call when */
/* the inactivity timer expires, or 0 if */
/* timeouts are not required. */
void *data; /* Application provided data to be passed to */
/* timer.fn(). */
} timer;
#endif
};
/*
* Define the max amount of space needed to store a termcap terminal
* description. Unfortunately this has to be done by guesswork, so
* there is the potential for buffer overflows if we guess too small.
* Fortunately termcap has been replaced by terminfo on most
* platforms, and with terminfo this isn't an issue. The value that I
* am using here is the conventional value, as recommended by certain
* web references.
*/
#ifdef USE_TERMCAP
#define TERMCAP_BUF_SIZE 2048
#endif
/*
* Set the size of the string segments used to store terminal capability
* strings.
*/
#define CAPMEM_SEGMENT_SIZE 512
/*
* If no terminal size information is available, substitute the
* following vt100 default sizes.
*/
#define GL_DEF_NLINE 24
#define GL_DEF_NCOLUMN 80
/*
* Enumerate the attributes needed to classify different types of
* signals. These attributes reflect the standard default
* characteristics of these signals (according to Richard Steven's
* Advanced Programming in the UNIX Environment). Note that these values
* are all powers of 2, so that they can be combined in a bitwise union.
*/
typedef enum {
GLSA_TERM=1, /* A signal that terminates processes */
GLSA_SUSP=2, /* A signal that suspends processes */
GLSA_CONT=4, /* A signal that is sent when suspended processes resume */
GLSA_IGN=8, /* A signal that is ignored */
GLSA_CORE=16, /* A signal that generates a core dump */
GLSA_HARD=32, /* A signal generated by a hardware exception */
GLSA_SIZE=64 /* A signal indicating terminal size changes */
} GlSigAttr;
/*
* List the signals that we need to catch. In general these are
* those that by default terminate or suspend the process, since
* in such cases we need to restore terminal settings.
*/
static const struct GlDefSignal {
int signo; /* The number of the signal */
unsigned flags; /* A bitwise union of GlSignalFlags enumerators */
GlAfterSignal after; /* What to do after the signal has been delivered */
int attr; /* The default attributes of this signal, expressed */
/* as a bitwise union of GlSigAttr enumerators */
int errno_value; /* What to set errno to */
} gl_signal_list[] = {
{SIGABRT, GLS_SUSPEND_INPUT, GLS_ABORT, GLSA_TERM|GLSA_CORE, EINTR},
{SIGALRM, GLS_RESTORE_ENV, GLS_CONTINUE, GLSA_TERM, 0},
{SIGCONT, GLS_RESTORE_ENV, GLS_CONTINUE, GLSA_CONT|GLSA_IGN, 0},
#if defined(SIGHUP)
#ifdef ENOTTY
{SIGHUP, GLS_SUSPEND_INPUT, GLS_ABORT, GLSA_TERM, ENOTTY},
#else
{SIGHUP, GLS_SUSPEND_INPUT, GLS_ABORT, GLSA_TERM, EINTR},
#endif
#endif
{SIGINT, GLS_SUSPEND_INPUT, GLS_ABORT, GLSA_TERM, EINTR},
#if defined(SIGPIPE)
#ifdef EPIPE
{SIGPIPE, GLS_SUSPEND_INPUT, GLS_ABORT, GLSA_TERM, EPIPE},
#else
{SIGPIPE, GLS_SUSPEND_INPUT, GLS_ABORT, GLSA_TERM, EINTR},
#endif
#endif
#ifdef SIGPOLL
{SIGPOLL, GLS_SUSPEND_INPUT, GLS_ABORT, GLSA_TERM, EINTR},
#endif
#ifdef SIGPWR
{SIGPWR, GLS_RESTORE_ENV, GLS_CONTINUE, GLSA_IGN, 0},
#endif
#ifdef SIGQUIT
{SIGQUIT, GLS_SUSPEND_INPUT, GLS_ABORT, GLSA_TERM|GLSA_CORE, EINTR},
#endif
{SIGTERM, GLS_SUSPEND_INPUT, GLS_ABORT, GLSA_TERM, EINTR},
#ifdef SIGTSTP
{SIGTSTP, GLS_SUSPEND_INPUT, GLS_CONTINUE, GLSA_SUSP, 0},
#endif
#ifdef SIGTTIN
{SIGTTIN, GLS_SUSPEND_INPUT, GLS_CONTINUE, GLSA_SUSP, 0},
#endif
#ifdef SIGTTOU
{SIGTTOU, GLS_SUSPEND_INPUT, GLS_CONTINUE, GLSA_SUSP, 0},
#endif
#ifdef SIGUSR1
{SIGUSR1, GLS_RESTORE_ENV, GLS_CONTINUE, GLSA_TERM, 0},
#endif
#ifdef SIGUSR2
{SIGUSR2, GLS_RESTORE_ENV, GLS_CONTINUE, GLSA_TERM, 0},
#endif
#ifdef SIGVTALRM
{SIGVTALRM, GLS_RESTORE_ENV, GLS_CONTINUE, GLSA_TERM, 0},
#endif
#ifdef SIGWINCH
{SIGWINCH, GLS_RESTORE_ENV, GLS_CONTINUE, GLSA_SIZE|GLSA_IGN, 0},
#endif
#ifdef SIGXCPU
{SIGXCPU, GLS_RESTORE_ENV, GLS_CONTINUE, GLSA_TERM|GLSA_CORE, 0},
#endif
#ifdef SIGXFSZ
{SIGXFSZ, GLS_RESTORE_ENV, GLS_CONTINUE, GLSA_TERM|GLSA_CORE, 0},
#endif
};
/*
* Define file-scope variables for use in signal handlers.
*/
static volatile sig_atomic_t gl_pending_signal = -1;
static sigjmp_buf gl_setjmp_buffer;
static void gl_signal_handler(int signo);
static int gl_check_caught_signal(GetLine *gl);
/*
* Respond to an externally caught process suspension or
* termination signal.
*/
static void gl_suspend_process(int signo, GetLine *gl, int ngl);
/* Return the default attributes of a given signal */
static int gl_classify_signal(int signo);
/*
* Unfortunately both terminfo and termcap require one to use the tputs()
* function to output terminal control characters, and this function
* doesn't allow one to specify a file stream. As a result, the following
* file-scope variable is used to pass the current output file stream.
* This is bad, but there doesn't seem to be any alternative.
*/
static GetLine *tputs_gl = NULL;
/*
* Define a tab to be a string of 8 spaces.
*/
#define TAB_WIDTH 8
/*
* Lookup the current size of the terminal.
*/
static void gl_query_size(GetLine *gl, int *ncolumn, int *nline);
/*
* Getline calls this to temporarily override certain signal handlers
* of the calling program.
*/
static int gl_override_signal_handlers(GetLine *gl);
/*
* Getline calls this to restore the signal handlers of the calling
* program.
*/
static int gl_restore_signal_handlers(GetLine *gl);
/*
* Temporarily block the delivery of all signals that gl_get_line()
* is currently configured to trap.
*/
static int gl_mask_signals(GetLine *gl, sigset_t *oldset);
/*
* Restore the process signal mask that was overriden by a previous
* call to gl_mask_signals().
*/
static int gl_unmask_signals(GetLine *gl, sigset_t *oldset);
/*
* Unblock the signals that gl_get_line() has been configured to catch.
*/
static int gl_catch_signals(GetLine *gl);
/*
* Return the set of all trappable signals.
*/
static void gl_list_trappable_signals(sigset_t *signals);
/*
* Put the terminal into raw input mode, after saving the original
* terminal attributes in gl->oldattr.
*/
static int gl_raw_terminal_mode(GetLine *gl);
/*
* Restore the terminal attributes from gl->oldattr.
*/
static int gl_restore_terminal_attributes(GetLine *gl);
/*
* Switch to non-blocking I/O if possible.
*/
static int gl_nonblocking_io(GetLine *gl, int fd);
/*
* Switch to blocking I/O if possible.
*/
static int gl_blocking_io(GetLine *gl, int fd);
/*
* Read a line from the user in raw mode.
*/
static int gl_get_input_line(GetLine *gl, const char *prompt,
const char *start_line, int start_pos);
/*
* Query the user for a single character.
*/
static int gl_get_query_char(GetLine *gl, const char *prompt, int defchar);
/*
* Read input from a non-interactive input stream.
*/
static int gl_read_stream_line(GetLine *gl);
/*
* Read a single character from a non-interactive input stream.
*/
static int gl_read_stream_char(GetLine *gl);
/*
* Prepare to edit a new line.
*/
static int gl_present_line(GetLine *gl, const char *prompt,
const char *start_line, int start_pos);
/*
* Reset all line-editing parameters for a new input line.
*/
static void gl_reset_editor(GetLine *gl);
/*
* Handle the receipt of the potential start of a new key-sequence from
* the user.
*/
static int gl_interpret_char(GetLine *gl, char c);
/*
* Bind a single control or meta character to an action.
*/
static int gl_bind_control_char(GetLine *gl, KtBinder binder,
char c, const char *action);
/*
* Set up terminal-specific key bindings.
*/
static int gl_bind_terminal_keys(GetLine *gl);
/*
* Lookup terminal control string and size information.
*/
static int gl_control_strings(GetLine *gl, const char *term);
/*
* Wrappers around the terminfo and termcap functions that lookup
* strings in the terminal information databases.
*/
#ifdef USE_TERMINFO
static const char *gl_tigetstr(GetLine *gl, const char *name);
#elif defined(USE_TERMCAP)
static const char *gl_tgetstr(GetLine *gl, const char *name, char **bufptr);
#endif
/*
* Output a binary string directly to the terminal.
*/
static int gl_print_raw_string(GetLine *gl, int buffered,
const char *string, int n);
/*
* Print an informational message, starting and finishing on new lines.
* After the list of strings to be printed, the last argument MUST be
* GL_END_INFO.
*/
static int gl_print_info(GetLine *gl, ...);
#define GL_END_INFO ((const char *)0)
/*
* Start a newline and place the cursor at its start.
*/
static int gl_start_newline(GetLine *gl, int buffered);
/*
* Output a terminal control sequence.
*/
static int gl_print_control_sequence(GetLine *gl, int nline,
const char *string);
/*
* Output a character or string to the terminal after converting tabs
* to spaces and control characters to a caret followed by the modified
* character.
*/
static int gl_print_char(GetLine *gl, char c, char pad);
static int gl_print_string(GetLine *gl, const char *string, char pad);
/*
* Delete nc characters starting from the one under the cursor.
* Optionally copy the deleted characters to the cut buffer.
*/
static int gl_delete_chars(GetLine *gl, int nc, int cut);
/*
* Add a character to the line buffer at the current cursor position,
* inserting or overwriting according the current mode.
*/
static int gl_add_char_to_line(GetLine *gl, char c);
/*
* Insert/append a string to the line buffer and terminal at the current
* cursor position.
*/
static int gl_add_string_to_line(GetLine *gl, const char *s);
/*
* Record a new character in the input-line buffer.
*/
static int gl_buffer_char(GetLine *gl, char c, int bufpos);
/*
* Record a string in the input-line buffer.
*/
static int gl_buffer_string(GetLine *gl, const char *s, int n, int bufpos);
/*
* Make way to insert a string in the input-line buffer.
*/
static int gl_make_gap_in_buffer(GetLine *gl, int start, int n);
/*
* Remove characters from the input-line buffer, and move any characters
* that followed them to the start of the vacated space.
*/
static void gl_remove_from_buffer(GetLine *gl, int start, int n);
/*
* Terminate the input-line buffer after a specified number of characters.
*/
static int gl_truncate_buffer(GetLine *gl, int n);
/*
* Delete the displayed part of the input line that follows the current
* terminal cursor position.
*/
static int gl_truncate_display(GetLine *gl);
/*
* Accomodate changes to the contents of the input line buffer
* that weren't made by the above gl_*buffer functions.
*/
static void gl_update_buffer(GetLine *gl);
/*
* Read a single character from the terminal.
*/
static int gl_read_terminal(GetLine *gl, int keep, char *c);
/*
* Discard processed characters from the key-press lookahead buffer.
*/
static void gl_discard_chars(GetLine *gl, int nused);
/*
* Move the terminal cursor n positions to the left or right.
*/
static int gl_terminal_move_cursor(GetLine *gl, int n);
/*
* Move the terminal cursor to a given position.
*/
static int gl_set_term_curpos(GetLine *gl, int term_curpos);
/*
* Set the position of the cursor both in the line input buffer and on the
* terminal.
*/
static int gl_place_cursor(GetLine *gl, int buff_curpos);
/*
* How many characters are needed to write a number as an octal string?
*/
static int gl_octal_width(unsigned num);
/*
* Return the number of spaces needed to display a tab character at
* a given location of the terminal.
*/
static int gl_displayed_tab_width(GetLine *gl, int term_curpos);
/*
* Return the number of terminal characters needed to display a
* given raw character.
*/
static int gl_displayed_char_width(GetLine *gl, char c, int term_curpos);
/*
* Return the number of terminal characters needed to display a
* given substring.
*/
static int gl_displayed_string_width(GetLine *gl, const char *string, int nc,
int term_curpos);
/*
* Return non-zero if 'c' is to be considered part of a word.
*/
static int gl_is_word_char(int c);
/*
* Read a tecla configuration file.
*/
static int _gl_read_config_file(GetLine *gl, const char *filename, KtBinder who);
/*
* Read a tecla configuration string.
*/
static int _gl_read_config_string(GetLine *gl, const char *buffer, KtBinder who);
/*
* Define the callback function used by _gl_parse_config_line() to
* read the next character of a configuration stream.
*/
#define GLC_GETC_FN(fn) int (fn)(void *stream)
typedef GLC_GETC_FN(GlcGetcFn);
static GLC_GETC_FN(glc_file_getc); /* Read from a file */
static GLC_GETC_FN(glc_buff_getc); /* Read from a string */
/*
* Parse a single configuration command line.
*/
static int _gl_parse_config_line(GetLine *gl, void *stream, GlcGetcFn *getc_fn,
const char *origin, KtBinder who, int *lineno);
static int gl_report_config_error(GetLine *gl, const char *origin, int lineno,
const char *errmsg);
/*
* Bind the actual arrow key bindings to match those of the symbolic
* arrow-key bindings.
*/
static int _gl_bind_arrow_keys(GetLine *gl);
/*
* Copy the binding of the specified symbolic arrow-key binding to
* the terminal specific, and default arrow-key key-sequences.
*/
static int _gl_rebind_arrow_key(GetLine *gl, const char *name,
const char *term_seq,
const char *def_seq1,
const char *def_seq2);
/*
* After the gl_read_from_file() action has been used to tell gl_get_line()
* to temporarily read input from a file, gl_revert_input() arranges
* for input to be reverted to the input stream last registered with
* gl_change_terminal().
*/
static void gl_revert_input(GetLine *gl);
/*
* Flush unwritten characters to the terminal.
*/
static int gl_flush_output(GetLine *gl);
/*
* The callback through which all terminal output is routed.
* This simply appends characters to a queue buffer, which is
* subsequently flushed to the output channel by gl_flush_output().
*/
static GL_WRITE_FN(gl_write_fn);
/*
* The callback function which the output character queue object