2 Generalized console input/output handler
3 A maintanable replacement for readline()
5 Updated for Bacula, Kern Sibbald, December MMIII
7 This code is in part derived from code that I wrote in
8 1981, so some of it is a bit old and could use a cleanup.
12 Bacula® - The Network Backup Solution
14 Copyright (C) 1981-2006 Free Software Foundation Europe e.V.
15 Yes, that is 1981 no error.
17 The main author of Bacula is Kern Sibbald, with contributions from
18 many others, a complete list can be found in the file AUTHORS.
19 This program is Free Software; you can redistribute it and/or
20 modify it under the terms of version two of the GNU General Public
21 License as published by the Free Software Foundation and included
24 This program is distributed in the hope that it will be useful, but
25 WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 Bacula® is a registered trademark of John Walker.
35 The licensor of Bacula is the Free Software Foundation Europe
36 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
37 Switzerland, email:ftf@fsfeurope.org.
42 * If the top bit of a UTF-8 string is 0 (8 bits), then it
43 * is a normal ASCII character.
44 * If the top two bits are 11 (i.e. (c & 0xC0) == 0xC0 then
45 * it is the start of a series of chars (up to 5)
46 * Each subsequent character starts with 10 (i.e. (c & 0xC0) == 0x80)
59 /* We are in Bacula */
66 extern "C" int tgetent(void *, const char *);
67 extern "C" int tgetnum(const char *);
68 extern "C" char *tgetstr (const char*, char**);
69 extern "C" char *tgoto (const char *, int, int);
72 #elif defined (__digital__) && defined (__unix__)
73 extern "C" int tgetent(void *, const char *);
74 extern "C" int tgetnum(const char *);
75 extern "C" char *tgetstr (const char*, char**);
76 extern "C" char *tgoto (const char *, int, int);
84 /* From termios library */
93 /* Forward referenced functions */
95 static void sigintcatcher(int);
98 static void add_smap(char *str, int func);
101 /* Global variables */
103 static const char *t_up = "\n"; /* scroll up character */
104 static const char *t_honk = "\007"; /* sound beep */
105 static char *t_il; /* insert line */
106 static char *t_dl; /* delete line */
107 static char *t_cs; /* clear screen */
108 static char *t_cl; /* clear line */
109 static int t_width = 79; /* terminal width */
110 static int t_height = 24; /* terminal height */
111 static int linsdel_ok = 0; /* set if term has line insert & delete fncs */
113 static char *t_cm; /* cursor positioning */
114 static char *t_ti; /* init sequence */
115 static char *t_te; /* end sequence */
116 static char *t_do; /* down one line */
117 static char *t_sf; /* scroll screen one line up */
119 /* Keypad and Function Keys */
120 static char *kl; /* left key */
121 static char *kr; /* right */
122 static char *ku; /* up */
123 static char *kd; /* down */
124 static char *kh; /* home */
125 static char *kb; /* backspace */
126 static char *kD; /* delete key */
127 static char *kI; /* insert */
128 static char *kN; /* next page */
129 static char *kP; /* previous page */
130 static char *kH; /* home */
131 static char *kE; /* end */
134 #define EOS '\0' /* end of string terminator */
140 * Stab entry. Input chars (str), the length, and the desired
143 typedef struct s_stab {
152 static stab_t **stab = NULL; /* array of stabs by length */
153 static int num_stab; /* size of stab array */
155 static bool old_term_params_set = false;
156 static struct termios old_term_params;
158 /* Maintain lines in a doubly linked circular pool of lines. Each line is
159 preceded by a header defined by the lstr structure */
162 struct lstr { /* line pool structure */
163 struct lstr *prevl; /* link to previous line */
164 struct lstr *nextl; /* link to next line */
165 long len; /* length of line+header */
166 char used; /* set if line valid */
167 char line; /* line is actually varying length */
171 #define POOLEN 128000 /* bytes in line pool */
173 #define POOLEN 500 /* bytes in line pool */
175 char pool[POOLEN]; /* line pool */
176 #define PHDRL ((int)sizeof(struct lstr)) /* length of line header */
178 static struct lstr *lptr; /* current line pointer */
179 static struct lstr *slptr; /* store line pointer */
181 static char *getnext(), *getprev();
182 static int first = 1;
183 static int mode_insert = 0;
184 static int mode_wspace = 1; /* words separated by spaces */
187 static short char_map[600]= {
188 0, F_SOL, /* ^a Line start */
189 F_PRVWRD, /* ^b Previous word */ F_BREAK, /* ^C break */
190 F_DELCHR, /* ^D Delete character */ F_EOL, /* ^e End of line */
191 F_CSRRGT, /* ^f Right */ F_TABBAK, /* ^G Back tab */
192 F_CSRLFT, /* ^H Left */ F_TAB, /* ^I Tab */
193 F_CSRDWN, /* ^J Down */ F_DELEOL, /* ^K kill to eol */
194 F_CLRSCRN,/* ^L clear screen */ F_RETURN, /* ^M Carriage return */
195 F_RETURN, /* ^N enter line */ F_CONCAT, /* ^O Concatenate lines */
196 F_CSRUP, /* ^P cursor up */ F_TINS, /* ^Q Insert character mode */
197 F_PAGUP, /* ^R Page up */ F_CENTER, /* ^S Center text */
198 F_PAGDWN, /* ^T Page down */ F_DELSOL, /* ^U delete to start of line */
199 F_DELWRD, /* ^V Delete word */ F_PRVWRD, /* ^W Previous word */
200 F_NXTMCH, /* ^X Next match */ F_DELEOL, /* ^Y Delete to end of line */
201 F_BACKGND,/* ^Z Background */ 0x1B, /* ^[=ESC escape */
202 F_TENTRY, /* ^\ Entry mode */ F_PASTECB,/* ^]=paste clipboard */
203 F_HOME, /* ^^ Home */ F_ERSLIN, /* ^_ Erase line */
205 ' ','!','"','#','$','%','&','\047',
206 '(',')','*','+','\054','-','.','/',
207 '0','1','2','3','4','5','6','7',
208 '8','9',':',';','<','=','>','?',
209 '@','A','B','C','D','E','F','G',
210 'H','I','J','K','L','M','N','O',
211 'P','Q','R','S','T','U','V','W',
212 'X','Y','Z','[','\\',']','^','_',
213 '\140','a','b','c','d','e','f','g',
214 'h','i','j','k','l','m','n','o',
215 'p','q','r','s','t','u','v','w',
216 'x','y','z','{','|','}','\176',F_ERSCHR /* erase character */
221 /* Local variables */
223 #define CR '\r' /* carriage return */
226 /* Function Prototypes */
228 static unsigned int input_char(void);
229 static unsigned int t_gnc(void);
230 static void insert_space(char *curline, int line_len);
231 static void insert_hole(char *curline, int line_len);
232 static void forward(char *str, int str_len);
233 static void backup(char *curline);
234 static void delchr(int cnt, char *curline, int line_len);
235 static int iswordc(char c);
236 static int next_word(char *ldb_buf);
237 static int prev_word(char *ldb_buf);
238 static void prtcur(char *str);
239 static void poolinit(void);
240 static char * getnext(void);
241 static char * getprev(void);
242 static void putline(char *newl, int newlen);
243 static void t_honk_horn(void);
244 static void t_insert_line(void);
245 static void t_delete_line(void);
246 static void t_clrline(int pos, int width);
247 void t_sendl(const char *msg, int len);
248 void t_send(const char *msg);
250 static void asclrs();
251 static void ascurs(int y, int x);
253 static void rawmode(FILE *input);
254 static void normode(void);
255 static unsigned t_getch();
256 static void asclrl(int pos, int width);
257 static void asinsl();
258 static void asdell();
260 int input_line(char *string, int length);
268 void con_init(FILE *input)
278 void con_set_zed_keys(void)
280 char_map[1] = F_NXTWRD; /* ^A Next Word */
281 char_map[2] = F_SPLIT; /* ^B Split line */
282 char_map[3] = F_EOI; /* ^C Quit */
283 char_map[4] = F_DELCHR; /* ^D Delete character */
284 char_map[5] = F_EOF; /* ^E End of file */
285 char_map[6] = F_INSCHR; /* ^F Insert character */
286 char_map[7] = F_TABBAK; /* ^G Back tab */
287 char_map[8] = F_CSRLFT; /* ^H Left */
288 char_map[9] = F_TAB; /* ^I Tab */
289 char_map[10] = F_CSRDWN; /* ^J Down */
290 char_map[11] = F_CSRUP; /* ^K Up */
291 char_map[12] = F_CSRRGT; /* ^L Right */
292 char_map[13] = F_RETURN; /* ^M Carriage return */
293 char_map[14] = F_EOL; /* ^N End of line */
294 char_map[15] = F_CONCAT; /* ^O Concatenate lines */
295 char_map[16] = F_MARK; /* ^P Set marker */
296 char_map[17] = F_TINS; /* ^Q Insert character mode */
297 char_map[18] = F_PAGUP; /* ^R Page up */
298 char_map[19] = F_CENTER; /* ^S Center text */
299 char_map[20] = F_PAGDWN; /* ^T Page down */
300 char_map[21] = F_SOL; /* ^U Line start */
301 char_map[22] = F_DELWRD; /* ^V Delete word */
302 char_map[23] = F_PRVWRD; /* ^W Previous word */
303 char_map[24] = F_NXTMCH; /* ^X Next match */
304 char_map[25] = F_DELEOL; /* ^Y Delete to end of line */
305 char_map[26] = F_DELLIN; /* ^Z Delete line */
307 char_map[28] = F_TENTRY; /* ^\ Entry mode */
308 char_map[29] = F_PASTECB;/* ^]=paste clipboard */
309 char_map[30] = F_HOME; /* ^^ Home */
310 char_map[31] = F_ERSLIN; /* ^_ Erase line */
321 * Guarantee that the string is properly terminated */
322 char *bstrncpy(char *dest, const char *src, int maxlen)
324 strncpy(dest, src, maxlen-1);
332 * New style string mapping to function code
334 static unsigned do_smap(unsigned c)
354 for (i=len-1; i<MAX_STAB; i++) {
355 for (tstab=stab[i]; tstab; tstab=tstab->next) {
356 if (strncmp(str, tstab->str, len) == 0) {
357 if (len == tstab->len) {
361 break; /* found possibility continue searching */
368 /* found partial match, so get next character and retry */
369 str[len++] = t_gnc();
375 static void dump_stab()
381 for (i=0; i<MAX_STAB; i++) {
382 for (tstab=stab[i]; tstab; tstab=tstab->next) {
383 for (j=0; j<tstab->len; j++) {
385 if (c < 0x20 || c > 0x7F) {
386 sprintf(buf, " 0x%x ", c);
394 sprintf(buf, " func=%d len=%d\n\r", tstab->func, tstab->len);
402 * New routine. Add string to string->func mapping table.
404 static void add_smap(char *str, int func)
414 /* errmsg("String for func %d is zero length\n", func); */
417 tstab = (stab_t *)malloc(sizeof(stab_t));
418 memset(tstab, 0, sizeof(stab_t));
420 tstab->str = (char *)malloc(tstab->len + 1);
421 bstrncpy(tstab->str, str, tstab->len + 1);
423 if (tstab->len > num_stab) {
424 printf("stab string too long %d. Max is %d\n", tstab->len, num_stab);
427 tstab->next = stab[tstab->len-1];
428 stab[tstab->len-1] = tstab;
429 /* printf("Add_smap tstab=%x len=%d func=%d tstab->next=%x\n\r", tstab, len,
430 func, tstab->next); */
435 /* Get the next character from the terminal - performs table lookup on
436 the character to do the desired translation */
442 if ((c=t_gnc()) <= 599) { /* IBM generates codes up to 260 */
444 } else if (c > 1000) { /* stuffed function */
445 c -= 1000; /* convert back to function code */
450 /* if we got a screen size escape sequence, read height, width */
453 y = t_gnc() - 0x20; /* y */
454 x = t_gnc() - 0x20; /* x */
461 /* Get a complete input line */
464 input_line(char *string, int length)
466 char curline[2000]; /* edit buffer */
473 poolinit(); /* build line pool */
476 noline = 1; /* no line fetched yet */
477 for (cl=cp=0; cl<length && cl<(int)sizeof(curline); ) {
482 switch (c=input_char()) {
483 case F_RETURN: /* CR */
484 t_sendl("\r\n", 2); /* yes, print it and */
485 goto done; /* get out */
486 case F_CLRSCRN: /* clear screen */
488 t_sendl(curline, cl);
492 if (noline) { /* no line fetched yet */
493 getnext(); /* getnext so getprev gets current */
494 noline = 0; /* we now have line */
496 bstrncpy(curline, getprev(), sizeof(curline));
500 noline = 0; /* mark line fetched */
501 bstrncpy(curline, getnext(), sizeof(curline));
505 insert_space(curline, sizeof(curline));
508 delchr(1, curline, sizeof(curline)); /* delete one character */
510 case F_CSRLFT: /* Backspace */
514 forward(curline, sizeof(curline));
516 case F_ERSCHR: /* Rubout */
518 delchr(1, curline, sizeof(curline));
525 t_clrline(0, t_width);
530 i = next_word(curline);
532 forward(curline, sizeof(curline));
536 i = prev_word(curline);
542 delchr(next_word(curline), curline, sizeof(curline)); /* delete word */
544 case F_NXTMCH: /* Ctl-X */
546 *string = EOS; /* terminate string */
547 return(c); /* give it to him */
549 /* Note fall through */
553 backup(curline); /* backup to beginning of line */
555 t_clrline(0, t_width); /* erase line */
557 cl = 0; /* reset cursor counter */
568 forward(curline, sizeof(curline));
574 case F_TINS: /* toggle insert mode */
575 mode_insert = !mode_insert; /* flip bit */
578 if (c > 255) { /* function key hit */
579 if (cl==0) { /* if first character then */
580 *string = EOS; /* terminate string */
581 return c; /* return it */
583 t_honk_horn(); /* complain */
585 if ((c & 0xC0) == 0xC0) {
586 if ((c & 0xFC) == 0xFC) {
588 } else if ((c & 0xF8) == 0xF8) {
590 } else if ((c & 0xF0) == 0xF0) {
592 } else if ((c & 0xE0) == 0xE0) {
601 insert_space(curline, sizeof(curline));
603 curline[cp++] = c; /* store character in line being built */
604 t_char(c); /* echo character to terminal */
607 insert_hole(curline, sizeof(curline));
608 curline[cp++] = c; /* store character in line being built */
609 t_char(c); /* echo character to terminal */
612 cl = cp; /* keep current length */
619 /* If we fall through here rather than goto done, the line is too long
620 simply return what we have now. */
622 curline[cl++] = EOS; /* terminate */
623 bstrncpy(string,curline,length); /* return line to caller */
624 /* Note, put line zaps curline */
625 putline(curline,cl); /* save line for posterity */
626 return 0; /* give it to him/her */
629 /* Insert a space at the current cursor position */
631 insert_space(char *curline, int curline_len)
635 if (cp > cl || cl+1 > curline_len) {
638 /* Note! source and destination overlap */
639 memmove(&curline[cp+1],&curline[cp],i=cl-cp);
644 forward(curline, curline_len);
654 insert_hole(char *curline, int curline_len)
658 if (cp > cl || cl+1 > curline_len) {
661 /* Note! source and destination overlap */
662 memmove(&curline[cp+1], &curline[cp], i=cl-cp);
668 /* Move cursor forward keeping characters under it */
670 forward(char *str, int str_len)
681 if ((str[cp] & 0xC0) == 0xC0) {
683 while ((str[cp] & 0xC0) == 0x80) {
693 /* How many characters under the cursor */
695 char_count(int cptr, char *str)
701 if ((str[cptr] & 0xC0) == 0xC0) {
703 while ((str[cptr] & 0xC0) == 0x80) {
711 /* Backup cursor keeping characters under it */
718 while ((str[cp] & 0xC0) == 0x80) {
725 /* Delete the character under the cursor */
727 delchr(int del, char *curline, int line_len)
731 if (cp > cl || del == 0) {
734 while (del-- && cp > 0) {
735 cnt = char_count(cp, curline);
736 if ((i=cl-cp-cnt) > 0) {
737 memcpy(&curline[cp], &curline[cp+cnt], i);
741 t_clrline(0, t_width);
744 forward(curline, line_len);
753 /* Determine if character is part of a word */
759 if (c >= '0' && c <= '9')
761 if (c == '$' || c == '%')
766 /* Return number of characters to get to next word */
768 next_word(char *ldb_buf)
775 for ( ; ncp<cl && iswordc(*(ldb_buf+ncp)); ncp++) ;
776 for ( ; ncp<cl && !iswordc(*(ldb_buf+ncp)); ncp++) ;
780 /* Return number of characters to get to previous word */
782 prev_word(char *ldb_buf)
786 if (cp == 0) /* if at begin of line stop now */
788 if (cp > cl) /* if past eol start at eol */
792 /* backup to end of previous word - i.e. skip special chars */
793 for (i=ncp-1; i && !iswordc(*(ldb_buf+i)); i--) ;
794 if (i == 0) { /* at beginning of line? */
795 return cp; /* backup to beginning */
797 /* now move back through word to beginning of word */
798 for ( ; i && iswordc(*(ldb_buf+i)); i--) ;
799 ncp = i+1; /* position to first char of word */
800 if (i==0 && iswordc(*ldb_buf)) /* check for beginning of line */
802 return cp-ncp; /* return count */
805 /* Display new current line */
812 t_clrline(0,t_width);
813 cp = cl = strlen(str);
818 /* Initialize line pool. Split pool into two pieces. */
822 slptr = lptr = (struct lstr *)pool;
831 /* Return pointer to next line in the pool and advance current line pointer */
835 do { /* find next used line */
837 } while (!lptr->used);
838 return (char *)&lptr->line;
841 /* Return pointer to previous line in the pool */
845 do { /* find previous used line */
847 } while (!lptr->used);
848 return (char *)&lptr->line;
852 putline(char *newl, int newlen)
854 struct lstr *nptr; /* points to next line */
857 lptr = slptr; /* get ptr to last line stored */
858 lptr = lptr->nextl; /* advance pointer */
859 if ((char *)lptr-pool+newlen+PHDRL > POOLEN) { /* not enough room */
860 lptr->used = 0; /* delete line */
861 lptr = (struct lstr *)pool; /* start at beginning of buffer */
863 while (lptr->len < newlen+PHDRL) { /* concatenate buffers */
864 nptr = lptr->nextl; /* point to next line */
865 lptr->nextl = nptr->nextl; /* unlink it from list */
866 nptr->nextl->prevl = lptr;
867 lptr->len += nptr->len;
869 if (lptr->len > newlen + 2 * PHDRL) { /* split buffer */
870 nptr = (struct lstr *)((char *)lptr + newlen + PHDRL);
871 /* Appropriate byte alignment - normally 2 byte, but on
872 sparc we need 4 byte alignment, so we always do 4 */
873 if (((long unsigned)nptr & 3) != 0) { /* test four byte alignment */
875 nptr = (struct lstr *)((((long unsigned) p) & ~3) + 4);
877 nptr->len = lptr->len - ((char *)nptr - (char *)lptr);
878 lptr->len -= nptr->len;
879 nptr->nextl = lptr->nextl; /* link in new buffer */
880 lptr->nextl->prevl = nptr;
885 memcpy(&lptr->line,newl,newlen);
886 lptr->used = 1; /* mark line used */
887 slptr = lptr; /* save as stored line */
892 dump(struct lstr *ptr, char *msg)
894 printf("%s buf=%x nextl=%x prevl=%x len=%d used=%d\n",
895 msg,ptr,ptr->nextl,ptr->prevl,ptr->len,ptr->used);
897 printf("line=%s\n",&ptr->line);
899 #endif /* DEBUGOUT */
902 /* Honk horn on terminal */
909 /* Insert line on terminal */
916 /* Delete line from terminal */
923 /* clear line from pos to width */
925 t_clrline(int pos, int width)
927 asclrl(pos, width); /* clear to end of line */
930 /* Helper function to add string preceded by
931 * ESC to smap table */
932 static void add_esc_smap(const char *str, int func)
935 buf[0] = 0x1B; /* esc */
936 bstrncpy(buf+1, str, sizeof(buf)-1);
940 /* Set raw mode on terminal file. Basically, get the terminal into a
941 mode in which all characters can be read as they are entered. CBREAK
942 mode is not sufficient.
944 static void rawmode(FILE *input)
947 static char term_buf[2048];
948 static char *term_buffer = term_buf;
949 char *termtype = (char *)getenv("TERM");
951 /* Make sure we are dealing with a terminal */
952 if (!isatty(fileno(input))) {
955 if (tcgetattr(0, &old_term_params) != 0) {
956 printf("conio: Cannot tcgetattr()\n");
959 old_term_params_set = true;
961 t.c_cc[VMIN] = 1; /* satisfy read after 1 char */
963 t.c_iflag &= ~(BRKINT | IGNPAR | PARMRK | INPCK |
964 ISTRIP | ICRNL | IXON | IXOFF | INLCR | IGNCR);
967 t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON |
969 tcflush(0, TCIFLUSH);
970 if (tcsetattr(0, TCSANOW, &t) == -1) {
971 printf("Cannot tcsetattr()\n");
974 /* Defaults, the main program can override these */
975 signal(SIGQUIT, SIG_IGN);
976 signal(SIGHUP, SIG_IGN);
977 signal(SIGINT, sigintcatcher);
978 signal(SIGWINCH, SIG_IGN);
981 printf("Cannot get terminal type.\n");
985 if (tgetent(term_buffer, termtype) < 0) {
986 printf("Cannot get terminal termcap entry.\n");
990 t_width = t_height = -1;
991 t_width = tgetnum("co") - 1;
992 t_height = tgetnum("li");
995 t_cm = (char *)tgetstr("cm", &term_buffer);
996 t_cs = (char *)tgetstr("cl", &term_buffer); /* clear screen */
997 t_cl = (char *)tgetstr("ce", &term_buffer); /* clear line */
998 t_dl = (char *)tgetstr("dl", &term_buffer); /* delete line */
999 t_il = (char *)tgetstr("al", &term_buffer); /* insert line */
1000 t_honk = (char *)tgetstr("bl", &term_buffer); /* beep */
1001 t_ti = (char *)tgetstr("ti", &term_buffer);
1002 t_te = (char *)tgetstr("te", &term_buffer);
1003 t_up = (char *)tgetstr("up", &term_buffer);
1004 t_do = (char *)tgetstr("do", &term_buffer);
1005 t_sf = (char *)tgetstr("sf", &term_buffer);
1007 num_stab = MAX_STAB; /* get default stab size */
1008 stab = (stab_t **)malloc(sizeof(stab_t *) * num_stab);
1009 memset(stab, 0, sizeof(stab_t *) * num_stab);
1012 kl = (char *)tgetstr("kl", &term_buffer);
1013 kr = (char *)tgetstr("kr", &term_buffer);
1014 ku = (char *)tgetstr("ku", &term_buffer);
1015 kd = (char *)tgetstr("kd", &term_buffer);
1016 kh = (char *)tgetstr("kh", &term_buffer);
1017 kb = (char *)tgetstr("kb", &term_buffer);
1018 kD = (char *)tgetstr("kD", &term_buffer);
1019 kI = (char *)tgetstr("kI", &term_buffer);
1020 kN = (char *)tgetstr("kN", &term_buffer);
1021 kP = (char *)tgetstr("kP", &term_buffer);
1022 kH = (char *)tgetstr("kH", &term_buffer);
1023 kE = (char *)tgetstr("kE", &term_buffer);
1025 add_smap(kl, F_CSRLFT);
1026 add_smap(kr, F_CSRRGT);
1027 add_smap(ku, F_CSRUP);
1028 add_smap(kd, F_CSRDWN);
1029 add_smap(kI, F_TINS);
1030 add_smap(kN, F_PAGDWN);
1031 add_smap(kP, F_PAGUP);
1032 add_smap(kH, F_HOME);
1033 add_smap(kE, F_EOF);
1036 add_esc_smap("[A", F_CSRUP);
1037 add_esc_smap("[B", F_CSRDWN);
1038 add_esc_smap("[C", F_CSRRGT);
1039 add_esc_smap("[D", F_CSRLFT);
1040 add_esc_smap("[1~", F_HOME);
1041 add_esc_smap("[2~", F_TINS);
1042 add_esc_smap("[3~", F_DELCHR);
1043 add_esc_smap("[4~", F_EOF);
1044 add_esc_smap("f", F_NXTWRD);
1045 add_esc_smap("b", F_PRVWRD);
1049 /* Restore tty mode */
1050 static void normode()
1052 if (old_term_params_set) {
1053 tcsetattr(0, TCSANOW, &old_term_params);
1054 old_term_params_set = false;
1058 /* Get next character from terminal/script file/unget buffer */
1066 /* Get next character from OS */
1067 static unsigned t_getch(void)
1071 if (read(0, &c, 1) != 1) {
1077 /* Send message to terminal - primitive routine */
1079 t_sendl(const char *msg, int len)
1085 t_send(const char *msg)
1090 t_sendl(msg, strlen(msg)); /* faster than one char at time */
1093 /* Send single character to terminal - primitive routine - */
1097 (void)write(1, &c, 1);
1101 static int brkflg = 0; /* set on user break */
1103 /* Routine to return true if user types break */
1109 /* Clear break flag */
1116 /* Interrupt caught here */
1117 static void sigintcatcher(int sig)
1124 signal(SIGINT, sigintcatcher);
1131 signal(SIGINT, sigintcatcher);
1135 /* ASCLRL() -- Clear to end of line from current position */
1136 static void asclrl(int pos, int width)
1141 t_send(t_cl); /* use clear to eol function */
1144 if (pos==1 && linsdel_ok) {
1145 t_delete_line(); /* delete line */
1146 t_insert_line(); /* reinsert it */
1149 for (i=1; i<=width-pos+1; i++)
1150 t_char(' '); /* last resort, blank it out */
1151 for (i=1; i<=width-pos+1; i++) /* backspace to original position */
1158 /* ASCURS -- Set cursor position */
1159 static void ascurs(int y, int x)
1161 t_send((char *)tgoto(t_cm, x, y));
1165 /* ASCLRS -- Clear whole screen */
1166 static void asclrs()
1174 /* ASINSL -- insert new line after cursor */
1175 static void asinsl()
1177 t_clrline(0, t_width);
1178 t_send(t_il); /* insert before */
1181 /* ASDELL -- Delete line at cursor */
1182 static void asdell()