2 Generalized console input/output handler
3 A maintanable replacement for readline()
5 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));
500 t_clrline(0, t_width);
505 i = next_word(curline);
507 forward(curline, sizeof(curline));
511 i = prev_word(curline);
517 delchr(next_word(curline), curline, sizeof(curline)); /* delete word */
519 case F_NXTMCH: /* Ctl-X */
521 *string = EOS; /* terminate string */
522 return(c); /* give it to him */
524 /* Note fall through */
528 backup(curline); /* backup to beginning of line */
530 t_clrline(0,t_width); /* erase line */
532 cl = 0; /* reset cursor counter */
541 forward(curline, sizeof(curline));
547 case F_TINS: /* toggle insert mode */
548 mode_insert = !mode_insert; /* flip bit */
551 if (c > 255) { /* function key hit */
552 if (cl==0) { /* if first character then */
553 *string = EOS; /* terminate string */
554 return c; /* return it */
556 t_honk_horn(); /* complain */
558 if ((c & 0xC0) == 0xC0) {
559 if ((c & 0xFC) == 0xFC) {
561 } else if ((c & 0xF8) == 0xF8) {
563 } else if ((c & 0xF0) == 0xF0) {
565 } else if ((c & 0xE0) == 0xE0) {
574 insert_space(curline, sizeof(curline));
576 curline[cp++] = c; /* store character in line being built */
577 t_char(c); /* echo character to terminal */
580 insert_hole(curline, sizeof(curline));
581 curline[cp++] = c; /* store character in line being built */
582 t_char(c); /* echo character to terminal */
585 cl = cp; /* keep current length */
592 /* If we fall through here rather than goto done, the line is too long
593 simply return what we have now. */
595 curline[cl++] = EOS; /* terminate */
596 bstrncpy(string,curline,length); /* return line to caller */
597 /* Note, put line zaps curline */
598 putline(curline,cl); /* save line for posterity */
599 return 0; /* give it to him/her */
602 /* Insert a space at the current cursor position */
604 insert_space(char *curline, int curline_len)
608 if (cp > cl || cl+1 > curline_len) {
611 /* Note! source and destination overlap */
612 memmove(&curline[cp+1],&curline[cp],i=cl-cp);
617 forward(curline, curline_len);
627 insert_hole(char *curline, int curline_len)
631 if (cp > cl || cl+1 > curline_len) {
634 /* Note! source and destination overlap */
635 memmove(&curline[cp+1], &curline[cp], i=cl-cp);
641 /* Move cursor forward keeping characters under it */
643 forward(char *str, int str_len)
654 if ((str[cp] & 0xC0) == 0xC0) {
656 while ((str[cp] & 0xC0) == 0x80) {
666 /* How many characters under the cursor */
668 char_count(int cptr, char *str)
674 if ((str[cptr] & 0xC0) == 0xC0) {
676 while ((str[cptr] & 0xC0) == 0x80) {
684 /* Backup cursor keeping characters under it */
691 while ((str[cp] & 0xC0) == 0x80) {
698 /* Delete the character under the cursor */
700 delchr(int del, char *curline, int line_len)
704 if (cp > cl || del == 0) {
707 while (del-- && cp > 0) {
708 cnt = char_count(cp, curline);
709 if ((i=cl-cp-cnt) > 0) {
710 memcpy(&curline[cp], &curline[cp+cnt], i);
714 t_clrline(0, t_width);
717 forward(curline, line_len);
726 /* Determine if character is part of a word */
732 if (c >= '0' && c <= '9')
734 if (c == '$' || c == '%')
739 /* Return number of characters to get to next word */
741 next_word(char *ldb_buf)
748 for ( ; ncp<cl && iswordc(*(ldb_buf+ncp)); ncp++) ;
749 for ( ; ncp<cl && !iswordc(*(ldb_buf+ncp)); ncp++) ;
753 /* Return number of characters to get to previous word */
755 prev_word(char *ldb_buf)
759 if (cp == 0) /* if at begin of line stop now */
761 if (cp > cl) /* if past eol start at eol */
765 /* backup to end of previous word - i.e. skip special chars */
766 for (i=ncp-1; i && !iswordc(*(ldb_buf+i)); i--) ;
767 if (i == 0) { /* at beginning of line? */
768 return cp; /* backup to beginning */
770 /* now move back through word to beginning of word */
771 for ( ; i && iswordc(*(ldb_buf+i)); i--) ;
772 ncp = i+1; /* position to first char of word */
773 if (i==0 && iswordc(*ldb_buf)) /* check for beginning of line */
775 return cp-ncp; /* return count */
778 /* Display new current line */
785 t_clrline(0,t_width);
786 cp = cl = strlen(str);
791 /* Initialize line pool. Split pool into two pieces. */
795 slptr = lptr = (struct lstr *)pool;
804 /* Return pointer to next line in the pool and advance current line pointer */
808 do { /* find next used line */
810 } while (!lptr->used);
811 return (char *)&lptr->line;
814 /* Return pointer to previous line in the pool */
818 do { /* find previous used line */
820 } while (!lptr->used);
821 return (char *)&lptr->line;
825 putline(char *newl, int newlen)
827 struct lstr *nptr; /* points to next line */
830 lptr = slptr; /* get ptr to last line stored */
831 lptr = lptr->nextl; /* advance pointer */
832 if ((char *)lptr-pool+newlen+PHDRL > POOLEN) { /* not enough room */
833 lptr->used = 0; /* delete line */
834 lptr = (struct lstr *)pool; /* start at beginning of buffer */
836 while (lptr->len < newlen+PHDRL) { /* concatenate buffers */
837 nptr = lptr->nextl; /* point to next line */
838 lptr->nextl = nptr->nextl; /* unlink it from list */
839 nptr->nextl->prevl = lptr;
840 lptr->len += nptr->len;
842 if (lptr->len > newlen + 2 * PHDRL) { /* split buffer */
843 nptr = (struct lstr *)((char *)lptr + newlen + PHDRL);
844 /* Appropriate byte alignment - normally 2 byte, but on
845 sparc we need 4 byte alignment, so we always do 4 */
846 if (((long unsigned)nptr & 3) != 0) { /* test four byte alignment */
848 nptr = (struct lstr *)((((long unsigned) p) & ~3) + 4);
850 nptr->len = lptr->len - ((char *)nptr - (char *)lptr);
851 lptr->len -= nptr->len;
852 nptr->nextl = lptr->nextl; /* link in new buffer */
853 lptr->nextl->prevl = nptr;
858 memcpy(&lptr->line,newl,newlen);
859 lptr->used = 1; /* mark line used */
860 slptr = lptr; /* save as stored line */
865 dump(struct lstr *ptr, char *msg)
867 printf("%s buf=%x nextl=%x prevl=%x len=%d used=%d\n",
868 msg,ptr,ptr->nextl,ptr->prevl,ptr->len,ptr->used);
870 printf("line=%s\n",&ptr->line);
872 #endif /* DEBUGOUT */
875 /* Honk horn on terminal */
882 /* Insert line on terminal */
889 /* Delete line from terminal */
896 /* clear line from pos to width */
898 t_clrline(int pos, int width)
900 asclrl(pos, width); /* clear to end of line */
903 /* Helper function to add string preceded by
904 * ESC to smap table */
905 static void add_esc_smap(const char *str, int func)
908 buf[0] = 0x1B; /* esc */
909 bstrncpy(buf+1, str, sizeof(buf)-1);
913 /* Set raw mode on terminal file. Basically, get the terminal into a
914 mode in which all characters can be read as they are entered. CBREAK
915 mode is not sufficient.
917 static void rawmode(FILE *input)
920 static char term_buf[2048];
921 static char *term_buffer = term_buf;
922 char *termtype = (char *)getenv("TERM");
924 /* Make sure we are dealing with a terminal */
925 if (!isatty(fileno(input))) {
928 if (tcgetattr(0, &old_term_params) != 0) {
929 printf("conio: Cannot tcgetattr()\n");
932 old_term_params_set = true;
934 t.c_cc[VMIN] = 1; /* satisfy read after 1 char */
936 t.c_iflag &= ~(BRKINT | IGNPAR | PARMRK | INPCK |
937 ISTRIP | ICRNL | IXON | IXOFF | INLCR | IGNCR);
940 t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON |
942 tcflush(0, TCIFLUSH);
943 if (tcsetattr(0, TCSANOW, &t) == -1) {
944 printf("Cannot tcsetattr()\n");
947 /* Defaults, the main program can override these */
948 signal(SIGQUIT, SIG_IGN);
949 signal(SIGHUP, SIG_IGN);
950 // signal(SIGSTOP, SIG_IGN);
951 signal(SIGINT, sigintcatcher);
952 signal(SIGWINCH, SIG_IGN);
953 signal(SIGQUIT, SIG_IGN);
954 signal(SIGCHLD, SIG_IGN);
955 // signal(SIGTSTP, SIG_IGN);
958 printf("Cannot get terminal type.\n");
962 if (tgetent(term_buffer, termtype) < 0) {
963 printf("Cannot get terminal termcap entry.\n");
967 t_width = t_height = -1;
968 t_width = tgetnum("co") - 1;
969 t_height = tgetnum("li");
972 t_cm = (char *)tgetstr("cm", &term_buffer);
973 t_cs = (char *)tgetstr("cl", &term_buffer); /* clear screen */
974 t_cl = (char *)tgetstr("ce", &term_buffer); /* clear line */
975 t_dl = (char *)tgetstr("dl", &term_buffer); /* delete line */
976 t_il = (char *)tgetstr("al", &term_buffer); /* insert line */
977 t_honk = (char *)tgetstr("bl", &term_buffer); /* beep */
978 t_ti = (char *)tgetstr("ti", &term_buffer);
979 t_te = (char *)tgetstr("te", &term_buffer);
980 t_up = (char *)tgetstr("up", &term_buffer);
981 t_do = (char *)tgetstr("do", &term_buffer);
982 t_sf = (char *)tgetstr("sf", &term_buffer);
984 num_stab = MAX_STAB; /* get default stab size */
985 stab = (stab_t **)malloc(sizeof(stab_t *) * num_stab);
986 memset(stab, 0, sizeof(stab_t *) * num_stab);
989 kl = (char *)tgetstr("kl", &term_buffer);
990 kr = (char *)tgetstr("kr", &term_buffer);
991 ku = (char *)tgetstr("ku", &term_buffer);
992 kd = (char *)tgetstr("kd", &term_buffer);
993 kh = (char *)tgetstr("kh", &term_buffer);
994 kb = (char *)tgetstr("kb", &term_buffer);
995 kD = (char *)tgetstr("kD", &term_buffer);
996 kI = (char *)tgetstr("kI", &term_buffer);
997 kN = (char *)tgetstr("kN", &term_buffer);
998 kP = (char *)tgetstr("kP", &term_buffer);
999 kH = (char *)tgetstr("kH", &term_buffer);
1000 kE = (char *)tgetstr("kE", &term_buffer);
1002 add_smap(kl, F_CSRLFT);
1003 add_smap(kr, F_CSRRGT);
1004 add_smap(ku, F_CSRUP);
1005 add_smap(kd, F_CSRDWN);
1006 add_smap(kI, F_TINS);
1007 add_smap(kN, F_PAGDWN);
1008 add_smap(kP, F_PAGUP);
1009 add_smap(kH, F_HOME);
1010 add_smap(kE, F_EOF);
1013 add_esc_smap("[A", F_CSRUP);
1014 add_esc_smap("[B", F_CSRDWN);
1015 add_esc_smap("[C", F_CSRRGT);
1016 add_esc_smap("[D", F_CSRLFT);
1017 add_esc_smap("[1~", F_HOME);
1018 add_esc_smap("[2~", F_TINS);
1019 add_esc_smap("[3~", F_DELCHR);
1020 add_esc_smap("[4~", F_EOF);
1021 add_esc_smap("f", F_NXTWRD);
1022 add_esc_smap("b", F_PRVWRD);
1026 /* Restore tty mode */
1027 static void normode()
1029 if (old_term_params_set) {
1030 tcsetattr(0, TCSANOW, &old_term_params);
1031 old_term_params_set = false;
1035 /* Get next character from terminal/script file/unget buffer */
1043 /* Get next character from OS */
1044 static unsigned t_getch(void)
1048 if (read(0, &c, 1) != 1) {
1054 /* Send message to terminal - primitive routine */
1056 t_sendl(const char *msg, int len)
1062 t_send(const char *msg)
1067 t_sendl(msg, strlen(msg)); /* faster than one char at time */
1070 /* Send single character to terminal - primitive routine - */
1078 static int brkflg = 0; /* set on user break */
1080 /* Routine to return true if user types break */
1086 /* Clear break flag */
1093 /* Interrupt caught here */
1094 static void sigintcatcher(int sig)
1101 signal(SIGINT, sigintcatcher);
1108 signal(SIGINT, sigintcatcher);
1112 /* ASCLRL() -- Clear to end of line from current position */
1113 static void asclrl(int pos, int width)
1118 t_send(t_cl); /* use clear to eol function */
1121 if (pos==1 && linsdel_ok) {
1122 t_delete_line(); /* delete line */
1123 t_insert_line(); /* reinsert it */
1126 for (i=1; i<=width-pos+1; i++)
1127 t_char(' '); /* last resort, blank it out */
1128 for (i=1; i<=width-pos+1; i++) /* backspace to original position */
1135 /* ASCURS -- Set cursor position */
1136 static void ascurs(int y, int x)
1138 t_send((char *)tgoto(t_cm, x, y));
1142 /* ASCLRS -- Clear whole screen */
1143 static void asclrs()
1151 /* ASINSL -- insert new line after cursor */
1152 static void asinsl()
1154 t_clrline(0, t_width);
1155 t_send(t_il); /* insert before */
1158 /* ASDELL -- Delete line at cursor */
1159 static void asdell()