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 Copyright (C) 1981-2004 Kern Sibbald and John Walker
13 Yes, that is 1981 no error.
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with this program; if not, write to the Free
27 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 * If the top bit of a UTF-8 string is 0 (8 bits), then it
35 * is a normal ASCII character.
36 * If the top two bits are 11 (i.e. (c & 0xC0) == 0xC0 then
37 * it is the start of a series of chars (up to 5)
38 * Each subsequent character starts with 10 (i.e. (c & 0xC0) == 0x80)
51 /* We are in Bacula */
58 extern "C" int tgetent(void *, const char *);
59 extern "C" int tgetnum(const char *);
60 extern "C" char *tgetstr (const char*, char**);
61 extern "C" char *tgoto (const char *, int, int);
68 /* From termios library */
72 /* Forward referenced functions */
74 static void sigintcatcher(int);
77 static void add_smap(char *str, int func);
80 /* Global variables */
82 static const char *t_up = "\n"; /* scroll up character */
83 static const char *t_honk = "\007"; /* sound beep */
84 static char *t_il; /* insert line */
85 static char *t_dl; /* delete line */
86 static char *t_cs; /* clear screen */
87 static char *t_cl; /* clear line */
88 static int t_width = 79; /* terminal width */
89 static int t_height = 24; /* terminal height */
90 static int linsdel_ok = 0; /* set if term has line insert & delete fncs */
92 static char *t_cm; /* cursor positioning */
93 static char *t_ti; /* init sequence */
94 static char *t_te; /* end sequence */
95 static char *t_do; /* down one line */
96 static char *t_sf; /* scroll screen one line up */
98 /* Keypad and Function Keys */
99 static char *kl; /* left key */
100 static char *kr; /* right */
101 static char *ku; /* up */
102 static char *kd; /* down */
103 static char *kh; /* home */
104 static char *kb; /* backspace */
105 static char *kD; /* delete key */
106 static char *kI; /* insert */
107 static char *kN; /* next page */
108 static char *kP; /* previous page */
109 static char *kH; /* home */
110 static char *kE; /* end */
113 #define EOS '\0' /* end of string terminator */
119 * Stab entry. Input chars (str), the length, and the desired
122 typedef struct s_stab {
131 static stab_t **stab = NULL; /* array of stabs by length */
132 static int num_stab; /* size of stab array */
134 static bool old_term_params_set = false;
135 static struct termios old_term_params;
137 /* Maintain lines in a doubly linked circular pool of lines. Each line is
138 preceded by a header defined by the lstr structure */
141 struct lstr { /* line pool structure */
142 struct lstr *prevl; /* link to previous line */
143 struct lstr *nextl; /* link to next line */
144 long len; /* length of line+header */
145 char used; /* set if line valid */
146 char line; /* line is actually varying length */
150 #define POOLEN 128000 /* bytes in line pool */
152 #define POOLEN 500 /* bytes in line pool */
154 char pool[POOLEN]; /* line pool */
155 #define PHDRL ((int)sizeof(struct lstr)) /* length of line header */
157 static struct lstr *lptr; /* current line pointer */
158 static struct lstr *slptr; /* store line pointer */
160 static char *getnext(), *getprev();
161 static int first = 1;
162 static int mode_insert = 0;
163 static int mode_wspace = 1; /* words separated by spaces */
166 static short char_map[600]= {
167 0, F_SOL, /* ^a Line start */
168 F_PRVWRD, /* ^b Previous word */ F_BREAK, /* ^C break */
169 F_DELCHR, /* ^D Delete character */ F_EOL, /* ^e End of line */
170 F_CSRRGT, /* ^f Right */ F_TABBAK, /* ^G Back tab */
171 F_CSRLFT, /* ^H Left */ F_TAB, /* ^I Tab */
172 F_CSRDWN, /* ^J Down */ F_DELEOL, /* ^K kill to eol */
173 F_CLRSCRN,/* ^L clear screen */ F_RETURN, /* ^M Carriage return */
174 F_RETURN, /* ^N enter line */ F_CONCAT, /* ^O Concatenate lines */
175 F_CSRUP, /* ^P cursor up */ F_TINS, /* ^Q Insert character mode */
176 F_PAGUP, /* ^R Page up */ F_CENTER, /* ^S Center text */
177 F_PAGDWN, /* ^T Page down */ F_DELSOL, /* ^U delete to start of line */
178 F_DELWRD, /* ^V Delete word */ F_PRVWRD, /* ^W Previous word */
179 F_NXTMCH, /* ^X Next match */ F_DELEOL, /* ^Y Delete to end of line */
180 F_BACKGND,/* ^Z Background */ 0x1B, /* ^[=ESC escape */
181 F_TENTRY, /* ^\ Entry mode */ F_PASTECB,/* ^]=paste clipboard */
182 F_HOME, /* ^^ Home */ F_ERSLIN, /* ^_ Erase line */
184 ' ','!','"','#','$','%','&','\047',
185 '(',')','*','+','\054','-','.','/',
186 '0','1','2','3','4','5','6','7',
187 '8','9',':',';','<','=','>','?',
188 '@','A','B','C','D','E','F','G',
189 'H','I','J','K','L','M','N','O',
190 'P','Q','R','S','T','U','V','W',
191 'X','Y','Z','[','\\',']','^','_',
192 '\140','a','b','c','d','e','f','g',
193 'h','i','j','k','l','m','n','o',
194 'p','q','r','s','t','u','v','w',
195 'x','y','z','{','|','}','\176',F_ERSCHR /* erase character */
200 /* Local variables */
202 #define CR '\r' /* carriage return */
205 /* Function Prototypes */
207 static unsigned int input_char(void);
208 static unsigned int t_gnc(void);
209 static void insert_space(char *curline, int line_len);
210 static void insert_hole(char *curline, int line_len);
211 static void forward(char *str, int str_len);
212 static void backup(char *curline);
213 static void delchr(int cnt, char *curline, int line_len);
214 static int iswordc(char c);
215 static int next_word(char *ldb_buf);
216 static int prev_word(char *ldb_buf);
217 static void prtcur(char *str);
218 static void poolinit(void);
219 static char * getnext(void);
220 static char * getprev(void);
221 static void putline(char *newl, int newlen);
222 static void t_honk_horn(void);
223 static void t_insert_line(void);
224 static void t_delete_line(void);
225 static void t_clrline(int pos, int width);
226 void t_sendl(const char *msg, int len);
227 void t_send(const char *msg);
229 static void asclrs();
230 static void ascurs(int y, int x);
232 static void rawmode(FILE *input);
233 static void normode(void);
234 static unsigned t_getch();
235 static void asclrl(int pos, int width);
236 static void asinsl();
237 static void asdell();
239 int input_line(char *string, int length);
247 void con_init(FILE *input)
257 void con_set_zed_keys(void)
259 char_map[1] = F_NXTWRD; /* ^A Next Word */
260 char_map[2] = F_SPLIT; /* ^B Split line */
261 char_map[3] = F_EOI; /* ^C Quit */
262 char_map[4] = F_DELCHR; /* ^D Delete character */
263 char_map[5] = F_EOF; /* ^E End of file */
264 char_map[6] = F_INSCHR; /* ^F Insert character */
265 char_map[7] = F_TABBAK; /* ^G Back tab */
266 char_map[8] = F_CSRLFT; /* ^H Left */
267 char_map[9] = F_TAB; /* ^I Tab */
268 char_map[10] = F_CSRDWN; /* ^J Down */
269 char_map[11] = F_CSRUP; /* ^K Up */
270 char_map[12] = F_CSRRGT; /* ^L Right */
271 char_map[13] = F_RETURN; /* ^M Carriage return */
272 char_map[14] = F_EOL; /* ^N End of line */
273 char_map[15] = F_CONCAT; /* ^O Concatenate lines */
274 char_map[16] = F_MARK; /* ^P Set marker */
275 char_map[17] = F_TINS; /* ^Q Insert character mode */
276 char_map[18] = F_PAGUP; /* ^R Page up */
277 char_map[19] = F_CENTER; /* ^S Center text */
278 char_map[20] = F_PAGDWN; /* ^T Page down */
279 char_map[21] = F_SOL; /* ^U Line start */
280 char_map[22] = F_DELWRD; /* ^V Delete word */
281 char_map[23] = F_PRVWRD; /* ^W Previous word */
282 char_map[24] = F_NXTMCH; /* ^X Next match */
283 char_map[25] = F_DELEOL; /* ^Y Delete to end of line */
284 char_map[26] = F_DELLIN; /* ^Z Delete line */
286 char_map[28] = F_TENTRY; /* ^\ Entry mode */
287 char_map[29] = F_PASTECB;/* ^]=paste clipboard */
288 char_map[30] = F_HOME; /* ^^ Home */
289 char_map[31] = F_ERSLIN; /* ^_ Erase line */
300 * Guarantee that the string is properly terminated */
301 char *bstrncpy(char *dest, const char *src, int maxlen)
303 strncpy(dest, src, maxlen-1);
311 * New style string mapping to function code
313 static unsigned do_smap(unsigned c)
333 for (i=len-1; i<MAX_STAB; i++) {
334 for (tstab=stab[i]; tstab; tstab=tstab->next) {
335 if (strncmp(str, tstab->str, len) == 0) {
336 if (len == tstab->len) {
340 break; /* found possibility continue searching */
347 /* found partial match, so get next character and retry */
348 str[len++] = t_gnc();
354 static void dump_stab()
360 for (i=0; i<MAX_STAB; i++) {
361 for (tstab=stab[i]; tstab; tstab=tstab->next) {
362 for (j=0; j<tstab->len; j++) {
364 if (c < 0x20 || c > 0x7F) {
365 sprintf(buf, " 0x%x ", c);
373 sprintf(buf, " func=%d len=%d\n\r", tstab->func, tstab->len);
381 * New routine. Add string to string->func mapping table.
383 static void add_smap(char *str, int func)
393 /* errmsg("String for func %d is zero length\n", func); */
396 tstab = (stab_t *)malloc(sizeof(stab_t));
397 memset(tstab, 0, sizeof(stab_t));
399 tstab->str = (char *)malloc(tstab->len + 1);
400 bstrncpy(tstab->str, str, tstab->len + 1);
402 if (tstab->len > num_stab) {
403 printf("stab string too long %d. Max is %d\n", tstab->len, num_stab);
406 tstab->next = stab[tstab->len-1];
407 stab[tstab->len-1] = tstab;
408 /* printf("Add_smap tstab=%x len=%d func=%d tstab->next=%x\n\r", tstab, len,
409 func, tstab->next); */
414 /* Get the next character from the terminal - performs table lookup on
415 the character to do the desired translation */
421 if ((c=t_gnc()) <= 599) { /* IBM generates codes up to 260 */
423 } else if (c > 1000) { /* stuffed function */
424 c -= 1000; /* convert back to function code */
429 /* if we got a screen size escape sequence, read height, width */
432 y = t_gnc() - 0x20; /* y */
433 x = t_gnc() - 0x20; /* x */
440 /* Get a complete input line */
443 input_line(char *string, int length)
445 char curline[2000]; /* edit buffer */
452 poolinit(); /* build line pool */
455 noline = 1; /* no line fetched yet */
456 for (cl=cp=0; cl<length && cl<(int)sizeof(curline); ) {
461 switch (c=input_char()) {
462 case F_RETURN: /* CR */
463 t_sendl("\r\n", 2); /* yes, print it and */
464 goto done; /* get out */
465 case F_CLRSCRN: /* clear screen */
467 t_sendl(curline, cl);
471 if (noline) { /* no line fetched yet */
472 getnext(); /* getnext so getprev gets current */
473 noline = 0; /* we now have line */
475 bstrncpy(curline, getprev(), sizeof(curline));
479 noline = 0; /* mark line fetched */
480 bstrncpy(curline, getnext(), sizeof(curline));
484 insert_space(curline, sizeof(curline));
487 delchr(1, curline, sizeof(curline)); /* delete one character */
489 case F_CSRLFT: /* Backspace */
493 forward(curline, sizeof(curline));
495 case F_ERSCHR: /* Rubout */
497 delchr(1, curline, sizeof(curline));
504 t_clrline(0, t_width);
509 i = next_word(curline);
511 forward(curline, sizeof(curline));
515 i = prev_word(curline);
521 delchr(next_word(curline), curline, sizeof(curline)); /* delete word */
523 case F_NXTMCH: /* Ctl-X */
525 *string = EOS; /* terminate string */
526 return(c); /* give it to him */
528 /* Note fall through */
532 backup(curline); /* backup to beginning of line */
534 t_clrline(0, t_width); /* erase line */
536 cl = 0; /* reset cursor counter */
547 forward(curline, sizeof(curline));
553 case F_TINS: /* toggle insert mode */
554 mode_insert = !mode_insert; /* flip bit */
557 if (c > 255) { /* function key hit */
558 if (cl==0) { /* if first character then */
559 *string = EOS; /* terminate string */
560 return c; /* return it */
562 t_honk_horn(); /* complain */
564 if ((c & 0xC0) == 0xC0) {
565 if ((c & 0xFC) == 0xFC) {
567 } else if ((c & 0xF8) == 0xF8) {
569 } else if ((c & 0xF0) == 0xF0) {
571 } else if ((c & 0xE0) == 0xE0) {
580 insert_space(curline, sizeof(curline));
582 curline[cp++] = c; /* store character in line being built */
583 t_char(c); /* echo character to terminal */
586 insert_hole(curline, sizeof(curline));
587 curline[cp++] = c; /* store character in line being built */
588 t_char(c); /* echo character to terminal */
591 cl = cp; /* keep current length */
598 /* If we fall through here rather than goto done, the line is too long
599 simply return what we have now. */
601 curline[cl++] = EOS; /* terminate */
602 bstrncpy(string,curline,length); /* return line to caller */
603 /* Note, put line zaps curline */
604 putline(curline,cl); /* save line for posterity */
605 return 0; /* give it to him/her */
608 /* Insert a space at the current cursor position */
610 insert_space(char *curline, int curline_len)
614 if (cp > cl || cl+1 > curline_len) {
617 /* Note! source and destination overlap */
618 memmove(&curline[cp+1],&curline[cp],i=cl-cp);
623 forward(curline, curline_len);
633 insert_hole(char *curline, int curline_len)
637 if (cp > cl || cl+1 > curline_len) {
640 /* Note! source and destination overlap */
641 memmove(&curline[cp+1], &curline[cp], i=cl-cp);
647 /* Move cursor forward keeping characters under it */
649 forward(char *str, int str_len)
660 if ((str[cp] & 0xC0) == 0xC0) {
662 while ((str[cp] & 0xC0) == 0x80) {
672 /* How many characters under the cursor */
674 char_count(int cptr, char *str)
680 if ((str[cptr] & 0xC0) == 0xC0) {
682 while ((str[cptr] & 0xC0) == 0x80) {
690 /* Backup cursor keeping characters under it */
697 while ((str[cp] & 0xC0) == 0x80) {
704 /* Delete the character under the cursor */
706 delchr(int del, char *curline, int line_len)
710 if (cp > cl || del == 0) {
713 while (del-- && cp > 0) {
714 cnt = char_count(cp, curline);
715 if ((i=cl-cp-cnt) > 0) {
716 memcpy(&curline[cp], &curline[cp+cnt], i);
720 t_clrline(0, t_width);
723 forward(curline, line_len);
732 /* Determine if character is part of a word */
738 if (c >= '0' && c <= '9')
740 if (c == '$' || c == '%')
745 /* Return number of characters to get to next word */
747 next_word(char *ldb_buf)
754 for ( ; ncp<cl && iswordc(*(ldb_buf+ncp)); ncp++) ;
755 for ( ; ncp<cl && !iswordc(*(ldb_buf+ncp)); ncp++) ;
759 /* Return number of characters to get to previous word */
761 prev_word(char *ldb_buf)
765 if (cp == 0) /* if at begin of line stop now */
767 if (cp > cl) /* if past eol start at eol */
771 /* backup to end of previous word - i.e. skip special chars */
772 for (i=ncp-1; i && !iswordc(*(ldb_buf+i)); i--) ;
773 if (i == 0) { /* at beginning of line? */
774 return cp; /* backup to beginning */
776 /* now move back through word to beginning of word */
777 for ( ; i && iswordc(*(ldb_buf+i)); i--) ;
778 ncp = i+1; /* position to first char of word */
779 if (i==0 && iswordc(*ldb_buf)) /* check for beginning of line */
781 return cp-ncp; /* return count */
784 /* Display new current line */
791 t_clrline(0,t_width);
792 cp = cl = strlen(str);
797 /* Initialize line pool. Split pool into two pieces. */
801 slptr = lptr = (struct lstr *)pool;
810 /* Return pointer to next line in the pool and advance current line pointer */
814 do { /* find next used line */
816 } while (!lptr->used);
817 return (char *)&lptr->line;
820 /* Return pointer to previous line in the pool */
824 do { /* find previous used line */
826 } while (!lptr->used);
827 return (char *)&lptr->line;
831 putline(char *newl, int newlen)
833 struct lstr *nptr; /* points to next line */
836 lptr = slptr; /* get ptr to last line stored */
837 lptr = lptr->nextl; /* advance pointer */
838 if ((char *)lptr-pool+newlen+PHDRL > POOLEN) { /* not enough room */
839 lptr->used = 0; /* delete line */
840 lptr = (struct lstr *)pool; /* start at beginning of buffer */
842 while (lptr->len < newlen+PHDRL) { /* concatenate buffers */
843 nptr = lptr->nextl; /* point to next line */
844 lptr->nextl = nptr->nextl; /* unlink it from list */
845 nptr->nextl->prevl = lptr;
846 lptr->len += nptr->len;
848 if (lptr->len > newlen + 2 * PHDRL) { /* split buffer */
849 nptr = (struct lstr *)((char *)lptr + newlen + PHDRL);
850 /* Appropriate byte alignment - normally 2 byte, but on
851 sparc we need 4 byte alignment, so we always do 4 */
852 if (((long unsigned)nptr & 3) != 0) { /* test four byte alignment */
854 nptr = (struct lstr *)((((long unsigned) p) & ~3) + 4);
856 nptr->len = lptr->len - ((char *)nptr - (char *)lptr);
857 lptr->len -= nptr->len;
858 nptr->nextl = lptr->nextl; /* link in new buffer */
859 lptr->nextl->prevl = nptr;
864 memcpy(&lptr->line,newl,newlen);
865 lptr->used = 1; /* mark line used */
866 slptr = lptr; /* save as stored line */
871 dump(struct lstr *ptr, char *msg)
873 printf("%s buf=%x nextl=%x prevl=%x len=%d used=%d\n",
874 msg,ptr,ptr->nextl,ptr->prevl,ptr->len,ptr->used);
876 printf("line=%s\n",&ptr->line);
878 #endif /* DEBUGOUT */
881 /* Honk horn on terminal */
888 /* Insert line on terminal */
895 /* Delete line from terminal */
902 /* clear line from pos to width */
904 t_clrline(int pos, int width)
906 asclrl(pos, width); /* clear to end of line */
909 /* Helper function to add string preceded by
910 * ESC to smap table */
911 static void add_esc_smap(const char *str, int func)
914 buf[0] = 0x1B; /* esc */
915 bstrncpy(buf+1, str, sizeof(buf)-1);
919 /* Set raw mode on terminal file. Basically, get the terminal into a
920 mode in which all characters can be read as they are entered. CBREAK
921 mode is not sufficient.
923 static void rawmode(FILE *input)
926 static char term_buf[2048];
927 static char *term_buffer = term_buf;
928 char *termtype = (char *)getenv("TERM");
930 /* Make sure we are dealing with a terminal */
931 if (!isatty(fileno(input))) {
934 if (tcgetattr(0, &old_term_params) != 0) {
935 printf("conio: Cannot tcgetattr()\n");
938 old_term_params_set = true;
940 t.c_cc[VMIN] = 1; /* satisfy read after 1 char */
942 t.c_iflag &= ~(BRKINT | IGNPAR | PARMRK | INPCK |
943 ISTRIP | ICRNL | IXON | IXOFF | INLCR | IGNCR);
946 t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON |
948 tcflush(0, TCIFLUSH);
949 if (tcsetattr(0, TCSANOW, &t) == -1) {
950 printf("Cannot tcsetattr()\n");
953 /* Defaults, the main program can override these */
954 signal(SIGQUIT, SIG_IGN);
955 signal(SIGHUP, SIG_IGN);
956 // signal(SIGSTOP, SIG_IGN);
957 signal(SIGINT, sigintcatcher);
958 signal(SIGWINCH, SIG_IGN);
959 signal(SIGQUIT, SIG_IGN);
960 signal(SIGCHLD, SIG_IGN);
961 // signal(SIGTSTP, SIG_IGN);
964 printf("Cannot get terminal type.\n");
968 if (tgetent(term_buffer, termtype) < 0) {
969 printf("Cannot get terminal termcap entry.\n");
973 t_width = t_height = -1;
974 t_width = tgetnum("co") - 1;
975 t_height = tgetnum("li");
978 t_cm = (char *)tgetstr("cm", &term_buffer);
979 t_cs = (char *)tgetstr("cl", &term_buffer); /* clear screen */
980 t_cl = (char *)tgetstr("ce", &term_buffer); /* clear line */
981 t_dl = (char *)tgetstr("dl", &term_buffer); /* delete line */
982 t_il = (char *)tgetstr("al", &term_buffer); /* insert line */
983 t_honk = (char *)tgetstr("bl", &term_buffer); /* beep */
984 t_ti = (char *)tgetstr("ti", &term_buffer);
985 t_te = (char *)tgetstr("te", &term_buffer);
986 t_up = (char *)tgetstr("up", &term_buffer);
987 t_do = (char *)tgetstr("do", &term_buffer);
988 t_sf = (char *)tgetstr("sf", &term_buffer);
990 num_stab = MAX_STAB; /* get default stab size */
991 stab = (stab_t **)malloc(sizeof(stab_t *) * num_stab);
992 memset(stab, 0, sizeof(stab_t *) * num_stab);
995 kl = (char *)tgetstr("kl", &term_buffer);
996 kr = (char *)tgetstr("kr", &term_buffer);
997 ku = (char *)tgetstr("ku", &term_buffer);
998 kd = (char *)tgetstr("kd", &term_buffer);
999 kh = (char *)tgetstr("kh", &term_buffer);
1000 kb = (char *)tgetstr("kb", &term_buffer);
1001 kD = (char *)tgetstr("kD", &term_buffer);
1002 kI = (char *)tgetstr("kI", &term_buffer);
1003 kN = (char *)tgetstr("kN", &term_buffer);
1004 kP = (char *)tgetstr("kP", &term_buffer);
1005 kH = (char *)tgetstr("kH", &term_buffer);
1006 kE = (char *)tgetstr("kE", &term_buffer);
1008 add_smap(kl, F_CSRLFT);
1009 add_smap(kr, F_CSRRGT);
1010 add_smap(ku, F_CSRUP);
1011 add_smap(kd, F_CSRDWN);
1012 add_smap(kI, F_TINS);
1013 add_smap(kN, F_PAGDWN);
1014 add_smap(kP, F_PAGUP);
1015 add_smap(kH, F_HOME);
1016 add_smap(kE, F_EOF);
1019 add_esc_smap("[A", F_CSRUP);
1020 add_esc_smap("[B", F_CSRDWN);
1021 add_esc_smap("[C", F_CSRRGT);
1022 add_esc_smap("[D", F_CSRLFT);
1023 add_esc_smap("[1~", F_HOME);
1024 add_esc_smap("[2~", F_TINS);
1025 add_esc_smap("[3~", F_DELCHR);
1026 add_esc_smap("[4~", F_EOF);
1027 add_esc_smap("f", F_NXTWRD);
1028 add_esc_smap("b", F_PRVWRD);
1032 /* Restore tty mode */
1033 static void normode()
1035 if (old_term_params_set) {
1036 tcsetattr(0, TCSANOW, &old_term_params);
1037 old_term_params_set = false;
1041 /* Get next character from terminal/script file/unget buffer */
1049 /* Get next character from OS */
1050 static unsigned t_getch(void)
1054 if (read(0, &c, 1) != 1) {
1060 /* Send message to terminal - primitive routine */
1062 t_sendl(const char *msg, int len)
1068 t_send(const char *msg)
1073 t_sendl(msg, strlen(msg)); /* faster than one char at time */
1076 /* Send single character to terminal - primitive routine - */
1084 static int brkflg = 0; /* set on user break */
1086 /* Routine to return true if user types break */
1092 /* Clear break flag */
1099 /* Interrupt caught here */
1100 static void sigintcatcher(int sig)
1107 signal(SIGINT, sigintcatcher);
1114 signal(SIGINT, sigintcatcher);
1118 /* ASCLRL() -- Clear to end of line from current position */
1119 static void asclrl(int pos, int width)
1124 t_send(t_cl); /* use clear to eol function */
1127 if (pos==1 && linsdel_ok) {
1128 t_delete_line(); /* delete line */
1129 t_insert_line(); /* reinsert it */
1132 for (i=1; i<=width-pos+1; i++)
1133 t_char(' '); /* last resort, blank it out */
1134 for (i=1; i<=width-pos+1; i++) /* backspace to original position */
1141 /* ASCURS -- Set cursor position */
1142 static void ascurs(int y, int x)
1144 t_send((char *)tgoto(t_cm, x, y));
1148 /* ASCLRS -- Clear whole screen */
1149 static void asclrs()
1157 /* ASINSL -- insert new line after cursor */
1158 static void asinsl()
1160 t_clrline(0, t_width);
1161 t_send(t_il); /* insert before */
1164 /* ASDELL -- Delete line at cursor */
1165 static void asdell()