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);
70 /* From termios library */
79 /* Forward referenced functions */
81 static void sigintcatcher(int);
84 static void add_smap(char *str, int func);
87 /* Global variables */
89 static const char *t_up = "\n"; /* scroll up character */
90 static const char *t_honk = "\007"; /* sound beep */
91 static char *t_il; /* insert line */
92 static char *t_dl; /* delete line */
93 static char *t_cs; /* clear screen */
94 static char *t_cl; /* clear line */
95 static int t_width = 79; /* terminal width */
96 static int t_height = 24; /* terminal height */
97 static int linsdel_ok = 0; /* set if term has line insert & delete fncs */
99 static char *t_cm; /* cursor positioning */
100 static char *t_ti; /* init sequence */
101 static char *t_te; /* end sequence */
102 static char *t_do; /* down one line */
103 static char *t_sf; /* scroll screen one line up */
105 /* Keypad and Function Keys */
106 static char *kl; /* left key */
107 static char *kr; /* right */
108 static char *ku; /* up */
109 static char *kd; /* down */
110 static char *kh; /* home */
111 static char *kb; /* backspace */
112 static char *kD; /* delete key */
113 static char *kI; /* insert */
114 static char *kN; /* next page */
115 static char *kP; /* previous page */
116 static char *kH; /* home */
117 static char *kE; /* end */
120 #define EOS '\0' /* end of string terminator */
126 * Stab entry. Input chars (str), the length, and the desired
129 typedef struct s_stab {
138 static stab_t **stab = NULL; /* array of stabs by length */
139 static int num_stab; /* size of stab array */
141 static bool old_term_params_set = false;
142 static struct termios old_term_params;
144 /* Maintain lines in a doubly linked circular pool of lines. Each line is
145 preceded by a header defined by the lstr structure */
148 struct lstr { /* line pool structure */
149 struct lstr *prevl; /* link to previous line */
150 struct lstr *nextl; /* link to next line */
151 long len; /* length of line+header */
152 char used; /* set if line valid */
153 char line; /* line is actually varying length */
157 #define POOLEN 128000 /* bytes in line pool */
159 #define POOLEN 500 /* bytes in line pool */
161 char pool[POOLEN]; /* line pool */
162 #define PHDRL ((int)sizeof(struct lstr)) /* length of line header */
164 static struct lstr *lptr; /* current line pointer */
165 static struct lstr *slptr; /* store line pointer */
167 static char *getnext(), *getprev();
168 static int first = 1;
169 static int mode_insert = 0;
170 static int mode_wspace = 1; /* words separated by spaces */
173 static short char_map[600]= {
174 0, F_SOL, /* ^a Line start */
175 F_PRVWRD, /* ^b Previous word */ F_BREAK, /* ^C break */
176 F_DELCHR, /* ^D Delete character */ F_EOL, /* ^e End of line */
177 F_CSRRGT, /* ^f Right */ F_TABBAK, /* ^G Back tab */
178 F_CSRLFT, /* ^H Left */ F_TAB, /* ^I Tab */
179 F_CSRDWN, /* ^J Down */ F_DELEOL, /* ^K kill to eol */
180 F_CLRSCRN,/* ^L clear screen */ F_RETURN, /* ^M Carriage return */
181 F_RETURN, /* ^N enter line */ F_CONCAT, /* ^O Concatenate lines */
182 F_CSRUP, /* ^P cursor up */ F_TINS, /* ^Q Insert character mode */
183 F_PAGUP, /* ^R Page up */ F_CENTER, /* ^S Center text */
184 F_PAGDWN, /* ^T Page down */ F_DELSOL, /* ^U delete to start of line */
185 F_DELWRD, /* ^V Delete word */ F_PRVWRD, /* ^W Previous word */
186 F_NXTMCH, /* ^X Next match */ F_DELEOL, /* ^Y Delete to end of line */
187 F_BACKGND,/* ^Z Background */ 0x1B, /* ^[=ESC escape */
188 F_TENTRY, /* ^\ Entry mode */ F_PASTECB,/* ^]=paste clipboard */
189 F_HOME, /* ^^ Home */ F_ERSLIN, /* ^_ Erase line */
191 ' ','!','"','#','$','%','&','\047',
192 '(',')','*','+','\054','-','.','/',
193 '0','1','2','3','4','5','6','7',
194 '8','9',':',';','<','=','>','?',
195 '@','A','B','C','D','E','F','G',
196 'H','I','J','K','L','M','N','O',
197 'P','Q','R','S','T','U','V','W',
198 'X','Y','Z','[','\\',']','^','_',
199 '\140','a','b','c','d','e','f','g',
200 'h','i','j','k','l','m','n','o',
201 'p','q','r','s','t','u','v','w',
202 'x','y','z','{','|','}','\176',F_ERSCHR /* erase character */
207 /* Local variables */
209 #define CR '\r' /* carriage return */
212 /* Function Prototypes */
214 static unsigned int input_char(void);
215 static unsigned int t_gnc(void);
216 static void insert_space(char *curline, int line_len);
217 static void insert_hole(char *curline, int line_len);
218 static void forward(char *str, int str_len);
219 static void backup(char *curline);
220 static void delchr(int cnt, char *curline, int line_len);
221 static int iswordc(char c);
222 static int next_word(char *ldb_buf);
223 static int prev_word(char *ldb_buf);
224 static void prtcur(char *str);
225 static void poolinit(void);
226 static char * getnext(void);
227 static char * getprev(void);
228 static void putline(char *newl, int newlen);
229 static void t_honk_horn(void);
230 static void t_insert_line(void);
231 static void t_delete_line(void);
232 static void t_clrline(int pos, int width);
233 void t_sendl(const char *msg, int len);
234 void t_send(const char *msg);
236 static void asclrs();
237 static void ascurs(int y, int x);
239 static void rawmode(FILE *input);
240 static void normode(void);
241 static unsigned t_getch();
242 static void asclrl(int pos, int width);
243 static void asinsl();
244 static void asdell();
246 int input_line(char *string, int length);
254 void con_init(FILE *input)
264 void con_set_zed_keys(void)
266 char_map[1] = F_NXTWRD; /* ^A Next Word */
267 char_map[2] = F_SPLIT; /* ^B Split line */
268 char_map[3] = F_EOI; /* ^C Quit */
269 char_map[4] = F_DELCHR; /* ^D Delete character */
270 char_map[5] = F_EOF; /* ^E End of file */
271 char_map[6] = F_INSCHR; /* ^F Insert character */
272 char_map[7] = F_TABBAK; /* ^G Back tab */
273 char_map[8] = F_CSRLFT; /* ^H Left */
274 char_map[9] = F_TAB; /* ^I Tab */
275 char_map[10] = F_CSRDWN; /* ^J Down */
276 char_map[11] = F_CSRUP; /* ^K Up */
277 char_map[12] = F_CSRRGT; /* ^L Right */
278 char_map[13] = F_RETURN; /* ^M Carriage return */
279 char_map[14] = F_EOL; /* ^N End of line */
280 char_map[15] = F_CONCAT; /* ^O Concatenate lines */
281 char_map[16] = F_MARK; /* ^P Set marker */
282 char_map[17] = F_TINS; /* ^Q Insert character mode */
283 char_map[18] = F_PAGUP; /* ^R Page up */
284 char_map[19] = F_CENTER; /* ^S Center text */
285 char_map[20] = F_PAGDWN; /* ^T Page down */
286 char_map[21] = F_SOL; /* ^U Line start */
287 char_map[22] = F_DELWRD; /* ^V Delete word */
288 char_map[23] = F_PRVWRD; /* ^W Previous word */
289 char_map[24] = F_NXTMCH; /* ^X Next match */
290 char_map[25] = F_DELEOL; /* ^Y Delete to end of line */
291 char_map[26] = F_DELLIN; /* ^Z Delete line */
293 char_map[28] = F_TENTRY; /* ^\ Entry mode */
294 char_map[29] = F_PASTECB;/* ^]=paste clipboard */
295 char_map[30] = F_HOME; /* ^^ Home */
296 char_map[31] = F_ERSLIN; /* ^_ Erase line */
307 * Guarantee that the string is properly terminated */
308 char *bstrncpy(char *dest, const char *src, int maxlen)
310 strncpy(dest, src, maxlen-1);
318 * New style string mapping to function code
320 static unsigned do_smap(unsigned c)
340 for (i=len-1; i<MAX_STAB; i++) {
341 for (tstab=stab[i]; tstab; tstab=tstab->next) {
342 if (strncmp(str, tstab->str, len) == 0) {
343 if (len == tstab->len) {
347 break; /* found possibility continue searching */
354 /* found partial match, so get next character and retry */
355 str[len++] = t_gnc();
361 static void dump_stab()
367 for (i=0; i<MAX_STAB; i++) {
368 for (tstab=stab[i]; tstab; tstab=tstab->next) {
369 for (j=0; j<tstab->len; j++) {
371 if (c < 0x20 || c > 0x7F) {
372 sprintf(buf, " 0x%x ", c);
380 sprintf(buf, " func=%d len=%d\n\r", tstab->func, tstab->len);
388 * New routine. Add string to string->func mapping table.
390 static void add_smap(char *str, int func)
400 /* errmsg("String for func %d is zero length\n", func); */
403 tstab = (stab_t *)malloc(sizeof(stab_t));
404 memset(tstab, 0, sizeof(stab_t));
406 tstab->str = (char *)malloc(tstab->len + 1);
407 bstrncpy(tstab->str, str, tstab->len + 1);
409 if (tstab->len > num_stab) {
410 printf("stab string too long %d. Max is %d\n", tstab->len, num_stab);
413 tstab->next = stab[tstab->len-1];
414 stab[tstab->len-1] = tstab;
415 /* printf("Add_smap tstab=%x len=%d func=%d tstab->next=%x\n\r", tstab, len,
416 func, tstab->next); */
421 /* Get the next character from the terminal - performs table lookup on
422 the character to do the desired translation */
428 if ((c=t_gnc()) <= 599) { /* IBM generates codes up to 260 */
430 } else if (c > 1000) { /* stuffed function */
431 c -= 1000; /* convert back to function code */
436 /* if we got a screen size escape sequence, read height, width */
439 y = t_gnc() - 0x20; /* y */
440 x = t_gnc() - 0x20; /* x */
447 /* Get a complete input line */
450 input_line(char *string, int length)
452 char curline[2000]; /* edit buffer */
459 poolinit(); /* build line pool */
462 noline = 1; /* no line fetched yet */
463 for (cl=cp=0; cl<length && cl<(int)sizeof(curline); ) {
468 switch (c=input_char()) {
469 case F_RETURN: /* CR */
470 t_sendl("\r\n", 2); /* yes, print it and */
471 goto done; /* get out */
472 case F_CLRSCRN: /* clear screen */
474 t_sendl(curline, cl);
478 if (noline) { /* no line fetched yet */
479 getnext(); /* getnext so getprev gets current */
480 noline = 0; /* we now have line */
482 bstrncpy(curline, getprev(), sizeof(curline));
486 noline = 0; /* mark line fetched */
487 bstrncpy(curline, getnext(), sizeof(curline));
491 insert_space(curline, sizeof(curline));
494 delchr(1, curline, sizeof(curline)); /* delete one character */
496 case F_CSRLFT: /* Backspace */
500 forward(curline, sizeof(curline));
502 case F_ERSCHR: /* Rubout */
504 delchr(1, curline, sizeof(curline));
511 t_clrline(0, t_width);
516 i = next_word(curline);
518 forward(curline, sizeof(curline));
522 i = prev_word(curline);
528 delchr(next_word(curline), curline, sizeof(curline)); /* delete word */
530 case F_NXTMCH: /* Ctl-X */
532 *string = EOS; /* terminate string */
533 return(c); /* give it to him */
535 /* Note fall through */
539 backup(curline); /* backup to beginning of line */
541 t_clrline(0, t_width); /* erase line */
543 cl = 0; /* reset cursor counter */
554 forward(curline, sizeof(curline));
560 case F_TINS: /* toggle insert mode */
561 mode_insert = !mode_insert; /* flip bit */
564 if (c > 255) { /* function key hit */
565 if (cl==0) { /* if first character then */
566 *string = EOS; /* terminate string */
567 return c; /* return it */
569 t_honk_horn(); /* complain */
571 if ((c & 0xC0) == 0xC0) {
572 if ((c & 0xFC) == 0xFC) {
574 } else if ((c & 0xF8) == 0xF8) {
576 } else if ((c & 0xF0) == 0xF0) {
578 } else if ((c & 0xE0) == 0xE0) {
587 insert_space(curline, sizeof(curline));
589 curline[cp++] = c; /* store character in line being built */
590 t_char(c); /* echo character to terminal */
593 insert_hole(curline, sizeof(curline));
594 curline[cp++] = c; /* store character in line being built */
595 t_char(c); /* echo character to terminal */
598 cl = cp; /* keep current length */
605 /* If we fall through here rather than goto done, the line is too long
606 simply return what we have now. */
608 curline[cl++] = EOS; /* terminate */
609 bstrncpy(string,curline,length); /* return line to caller */
610 /* Note, put line zaps curline */
611 putline(curline,cl); /* save line for posterity */
612 return 0; /* give it to him/her */
615 /* Insert a space at the current cursor position */
617 insert_space(char *curline, int curline_len)
621 if (cp > cl || cl+1 > curline_len) {
624 /* Note! source and destination overlap */
625 memmove(&curline[cp+1],&curline[cp],i=cl-cp);
630 forward(curline, curline_len);
640 insert_hole(char *curline, int curline_len)
644 if (cp > cl || cl+1 > curline_len) {
647 /* Note! source and destination overlap */
648 memmove(&curline[cp+1], &curline[cp], i=cl-cp);
654 /* Move cursor forward keeping characters under it */
656 forward(char *str, int str_len)
667 if ((str[cp] & 0xC0) == 0xC0) {
669 while ((str[cp] & 0xC0) == 0x80) {
679 /* How many characters under the cursor */
681 char_count(int cptr, char *str)
687 if ((str[cptr] & 0xC0) == 0xC0) {
689 while ((str[cptr] & 0xC0) == 0x80) {
697 /* Backup cursor keeping characters under it */
704 while ((str[cp] & 0xC0) == 0x80) {
711 /* Delete the character under the cursor */
713 delchr(int del, char *curline, int line_len)
717 if (cp > cl || del == 0) {
720 while (del-- && cp > 0) {
721 cnt = char_count(cp, curline);
722 if ((i=cl-cp-cnt) > 0) {
723 memcpy(&curline[cp], &curline[cp+cnt], i);
727 t_clrline(0, t_width);
730 forward(curline, line_len);
739 /* Determine if character is part of a word */
745 if (c >= '0' && c <= '9')
747 if (c == '$' || c == '%')
752 /* Return number of characters to get to next word */
754 next_word(char *ldb_buf)
761 for ( ; ncp<cl && iswordc(*(ldb_buf+ncp)); ncp++) ;
762 for ( ; ncp<cl && !iswordc(*(ldb_buf+ncp)); ncp++) ;
766 /* Return number of characters to get to previous word */
768 prev_word(char *ldb_buf)
772 if (cp == 0) /* if at begin of line stop now */
774 if (cp > cl) /* if past eol start at eol */
778 /* backup to end of previous word - i.e. skip special chars */
779 for (i=ncp-1; i && !iswordc(*(ldb_buf+i)); i--) ;
780 if (i == 0) { /* at beginning of line? */
781 return cp; /* backup to beginning */
783 /* now move back through word to beginning of word */
784 for ( ; i && iswordc(*(ldb_buf+i)); i--) ;
785 ncp = i+1; /* position to first char of word */
786 if (i==0 && iswordc(*ldb_buf)) /* check for beginning of line */
788 return cp-ncp; /* return count */
791 /* Display new current line */
798 t_clrline(0,t_width);
799 cp = cl = strlen(str);
804 /* Initialize line pool. Split pool into two pieces. */
808 slptr = lptr = (struct lstr *)pool;
817 /* Return pointer to next line in the pool and advance current line pointer */
821 do { /* find next used line */
823 } while (!lptr->used);
824 return (char *)&lptr->line;
827 /* Return pointer to previous line in the pool */
831 do { /* find previous used line */
833 } while (!lptr->used);
834 return (char *)&lptr->line;
838 putline(char *newl, int newlen)
840 struct lstr *nptr; /* points to next line */
843 lptr = slptr; /* get ptr to last line stored */
844 lptr = lptr->nextl; /* advance pointer */
845 if ((char *)lptr-pool+newlen+PHDRL > POOLEN) { /* not enough room */
846 lptr->used = 0; /* delete line */
847 lptr = (struct lstr *)pool; /* start at beginning of buffer */
849 while (lptr->len < newlen+PHDRL) { /* concatenate buffers */
850 nptr = lptr->nextl; /* point to next line */
851 lptr->nextl = nptr->nextl; /* unlink it from list */
852 nptr->nextl->prevl = lptr;
853 lptr->len += nptr->len;
855 if (lptr->len > newlen + 2 * PHDRL) { /* split buffer */
856 nptr = (struct lstr *)((char *)lptr + newlen + PHDRL);
857 /* Appropriate byte alignment - normally 2 byte, but on
858 sparc we need 4 byte alignment, so we always do 4 */
859 if (((long unsigned)nptr & 3) != 0) { /* test four byte alignment */
861 nptr = (struct lstr *)((((long unsigned) p) & ~3) + 4);
863 nptr->len = lptr->len - ((char *)nptr - (char *)lptr);
864 lptr->len -= nptr->len;
865 nptr->nextl = lptr->nextl; /* link in new buffer */
866 lptr->nextl->prevl = nptr;
871 memcpy(&lptr->line,newl,newlen);
872 lptr->used = 1; /* mark line used */
873 slptr = lptr; /* save as stored line */
878 dump(struct lstr *ptr, char *msg)
880 printf("%s buf=%x nextl=%x prevl=%x len=%d used=%d\n",
881 msg,ptr,ptr->nextl,ptr->prevl,ptr->len,ptr->used);
883 printf("line=%s\n",&ptr->line);
885 #endif /* DEBUGOUT */
888 /* Honk horn on terminal */
895 /* Insert line on terminal */
902 /* Delete line from terminal */
909 /* clear line from pos to width */
911 t_clrline(int pos, int width)
913 asclrl(pos, width); /* clear to end of line */
916 /* Helper function to add string preceded by
917 * ESC to smap table */
918 static void add_esc_smap(const char *str, int func)
921 buf[0] = 0x1B; /* esc */
922 bstrncpy(buf+1, str, sizeof(buf)-1);
926 /* Set raw mode on terminal file. Basically, get the terminal into a
927 mode in which all characters can be read as they are entered. CBREAK
928 mode is not sufficient.
930 static void rawmode(FILE *input)
933 static char term_buf[2048];
934 static char *term_buffer = term_buf;
935 char *termtype = (char *)getenv("TERM");
937 /* Make sure we are dealing with a terminal */
938 if (!isatty(fileno(input))) {
941 if (tcgetattr(0, &old_term_params) != 0) {
942 printf("conio: Cannot tcgetattr()\n");
945 old_term_params_set = true;
947 t.c_cc[VMIN] = 1; /* satisfy read after 1 char */
949 t.c_iflag &= ~(BRKINT | IGNPAR | PARMRK | INPCK |
950 ISTRIP | ICRNL | IXON | IXOFF | INLCR | IGNCR);
953 t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON |
955 tcflush(0, TCIFLUSH);
956 if (tcsetattr(0, TCSANOW, &t) == -1) {
957 printf("Cannot tcsetattr()\n");
960 /* Defaults, the main program can override these */
961 signal(SIGQUIT, SIG_IGN);
962 signal(SIGHUP, SIG_IGN);
963 // signal(SIGSTOP, SIG_IGN);
964 signal(SIGINT, sigintcatcher);
965 signal(SIGWINCH, SIG_IGN);
966 signal(SIGQUIT, SIG_IGN);
967 signal(SIGCHLD, SIG_IGN);
968 // signal(SIGTSTP, SIG_IGN);
971 printf("Cannot get terminal type.\n");
975 if (tgetent(term_buffer, termtype) < 0) {
976 printf("Cannot get terminal termcap entry.\n");
980 t_width = t_height = -1;
981 t_width = tgetnum("co") - 1;
982 t_height = tgetnum("li");
985 t_cm = (char *)tgetstr("cm", &term_buffer);
986 t_cs = (char *)tgetstr("cl", &term_buffer); /* clear screen */
987 t_cl = (char *)tgetstr("ce", &term_buffer); /* clear line */
988 t_dl = (char *)tgetstr("dl", &term_buffer); /* delete line */
989 t_il = (char *)tgetstr("al", &term_buffer); /* insert line */
990 t_honk = (char *)tgetstr("bl", &term_buffer); /* beep */
991 t_ti = (char *)tgetstr("ti", &term_buffer);
992 t_te = (char *)tgetstr("te", &term_buffer);
993 t_up = (char *)tgetstr("up", &term_buffer);
994 t_do = (char *)tgetstr("do", &term_buffer);
995 t_sf = (char *)tgetstr("sf", &term_buffer);
997 num_stab = MAX_STAB; /* get default stab size */
998 stab = (stab_t **)malloc(sizeof(stab_t *) * num_stab);
999 memset(stab, 0, sizeof(stab_t *) * num_stab);
1002 kl = (char *)tgetstr("kl", &term_buffer);
1003 kr = (char *)tgetstr("kr", &term_buffer);
1004 ku = (char *)tgetstr("ku", &term_buffer);
1005 kd = (char *)tgetstr("kd", &term_buffer);
1006 kh = (char *)tgetstr("kh", &term_buffer);
1007 kb = (char *)tgetstr("kb", &term_buffer);
1008 kD = (char *)tgetstr("kD", &term_buffer);
1009 kI = (char *)tgetstr("kI", &term_buffer);
1010 kN = (char *)tgetstr("kN", &term_buffer);
1011 kP = (char *)tgetstr("kP", &term_buffer);
1012 kH = (char *)tgetstr("kH", &term_buffer);
1013 kE = (char *)tgetstr("kE", &term_buffer);
1015 add_smap(kl, F_CSRLFT);
1016 add_smap(kr, F_CSRRGT);
1017 add_smap(ku, F_CSRUP);
1018 add_smap(kd, F_CSRDWN);
1019 add_smap(kI, F_TINS);
1020 add_smap(kN, F_PAGDWN);
1021 add_smap(kP, F_PAGUP);
1022 add_smap(kH, F_HOME);
1023 add_smap(kE, F_EOF);
1026 add_esc_smap("[A", F_CSRUP);
1027 add_esc_smap("[B", F_CSRDWN);
1028 add_esc_smap("[C", F_CSRRGT);
1029 add_esc_smap("[D", F_CSRLFT);
1030 add_esc_smap("[1~", F_HOME);
1031 add_esc_smap("[2~", F_TINS);
1032 add_esc_smap("[3~", F_DELCHR);
1033 add_esc_smap("[4~", F_EOF);
1034 add_esc_smap("f", F_NXTWRD);
1035 add_esc_smap("b", F_PRVWRD);
1039 /* Restore tty mode */
1040 static void normode()
1042 if (old_term_params_set) {
1043 tcsetattr(0, TCSANOW, &old_term_params);
1044 old_term_params_set = false;
1048 /* Get next character from terminal/script file/unget buffer */
1056 /* Get next character from OS */
1057 static unsigned t_getch(void)
1061 if (read(0, &c, 1) != 1) {
1067 /* Send message to terminal - primitive routine */
1069 t_sendl(const char *msg, int len)
1075 t_send(const char *msg)
1080 t_sendl(msg, strlen(msg)); /* faster than one char at time */
1083 /* Send single character to terminal - primitive routine - */
1091 static int brkflg = 0; /* set on user break */
1093 /* Routine to return true if user types break */
1099 /* Clear break flag */
1106 /* Interrupt caught here */
1107 static void sigintcatcher(int sig)
1114 signal(SIGINT, sigintcatcher);
1121 signal(SIGINT, sigintcatcher);
1125 /* ASCLRL() -- Clear to end of line from current position */
1126 static void asclrl(int pos, int width)
1131 t_send(t_cl); /* use clear to eol function */
1134 if (pos==1 && linsdel_ok) {
1135 t_delete_line(); /* delete line */
1136 t_insert_line(); /* reinsert it */
1139 for (i=1; i<=width-pos+1; i++)
1140 t_char(' '); /* last resort, blank it out */
1141 for (i=1; i<=width-pos+1; i++) /* backspace to original position */
1148 /* ASCURS -- Set cursor position */
1149 static void ascurs(int y, int x)
1151 t_send((char *)tgoto(t_cm, x, y));
1155 /* ASCLRS -- Clear whole screen */
1156 static void asclrs()
1164 /* ASINSL -- insert new line after cursor */
1165 static void asinsl()
1167 t_clrline(0, t_width);
1168 t_send(t_il); /* insert before */
1171 /* ASDELL -- Delete line at cursor */
1172 static void asdell()