tty.c (3840B)
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 2 /* tty.c - version 1.0.3 */ 3 /* With thanks to the people who sent code for SYSV - hpscdi!jon, 4 arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others. */ 5 6 #include <stdio.h> 7 #include "hack.h" 8 9 static char erase_char, kill_char; 10 11 /* 12 * Get initial state of terminal, set ospeed (for termcap routines) 13 * and switch off tab expansion if necessary. 14 * Called by startup() in termcap.c and after returning from ! or ^Z 15 */ 16 gettty(){ 17 erase_char = '\b'; 18 kill_char = 21; /* cntl-U */ 19 flags.cbreak = TRUE; 20 #ifdef DGK 21 disable_ctrlP(); /* turn off ^P processing */ 22 #endif 23 } 24 25 /* reset terminal to original state */ 26 settty(s) char *s; { 27 end_screen(); 28 if(s) printf(s); 29 (void) fflush(stdout); 30 #ifdef DGK 31 enable_ctrlP(); /* turn on ^P processing */ 32 #endif 33 } 34 35 36 /* fatal error */ 37 /*VARARGS1*/ 38 error(s,x,y) char *s; { 39 end_screen(); 40 putchar('\n'); 41 printf(s,x,y); 42 putchar('\n'); 43 exit(1); 44 } 45 46 /* 47 * Read a line closed with '\n' into the array char bufp[BUFSZ]. 48 * (The '\n' is not stored. The string is closed with a '\0'.) 49 * Reading can be interrupted by an escape ('\033') - now the 50 * resulting string is "\033". 51 */ 52 getlin(bufp) 53 register char *bufp; 54 { 55 register char *obufp = bufp; 56 register int c; 57 58 flags.toplin = 2; /* nonempty, no --More-- required */ 59 for(;;) { 60 (void) fflush(stdout); 61 if((c = getchar()) == EOF) { 62 *bufp = 0; 63 return; 64 } 65 if(c == '\033') { 66 *obufp = c; 67 obufp[1] = 0; 68 return; 69 } 70 if(c == erase_char || c == '\b') { 71 if(bufp != obufp) { 72 bufp--; 73 putstr("\b \b"); /* putsym converts \b */ 74 } else bell(); 75 } else if(c == '\n') { 76 *bufp = 0; 77 return; 78 } else if(' ' <= c && c < '\177') { 79 /* avoid isprint() - some people don't have it 80 ' ' is not always a printing char */ 81 *bufp = c; 82 bufp[1] = 0; 83 putstr(bufp); 84 if(bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO) 85 bufp++; 86 } else if(c == kill_char || c == '\177') { /* Robert Viduya */ 87 /* this test last - @ might be the kill_char */ 88 while(bufp != obufp) { 89 bufp--; 90 putstr("\b \b"); 91 } 92 } else 93 bell(); 94 } 95 } 96 97 getret() { 98 cgetret(""); 99 } 100 101 cgetret(s) 102 register char *s; 103 { 104 putsym('\n'); 105 if(flags.standout) 106 standoutbeg(); 107 putstr("Hit "); 108 putstr(flags.cbreak ? "space" : "return"); 109 putstr(" to continue: "); 110 if(flags.standout) 111 standoutend(); 112 xwaitforspace(s); 113 } 114 115 char morc; /* tell the outside world what char he used */ 116 117 xwaitforspace(s) 118 register char *s; /* chars allowed besides space or return */ 119 { 120 register int c; 121 122 morc = 0; 123 while((c = readchar()) != '\n') { 124 if(flags.cbreak) { 125 if(c == ' ') break; 126 if(s && index(s,c)) { 127 morc = c; 128 break; 129 } 130 bell(); 131 } 132 } 133 } 134 135 static int last_multi; 136 137 char * 138 parse() 139 { 140 static char inline[COLNO]; 141 register foo; 142 143 flags.move = 1; 144 if(!Invisible) curs_on_u(); else home(); 145 multi = 0; 146 #ifdef DGK 147 while((foo = readchar()) >= '0' && foo <= '9') { 148 multi = 10*multi+foo-'0'; 149 if (multi < 0 || multi > LARGEST_INT) 150 multi = LARGEST_INT; 151 if (multi > 9) { 152 remember_topl(); 153 home(); 154 cl_end(); 155 printf("Count: %d", multi); 156 } 157 last_multi = multi; 158 } 159 # ifdef REDO 160 if (foo == DOAGAIN || in_doagain) 161 multi = last_multi; 162 else { 163 savech(0); /* reset input queue */ 164 savech(foo); 165 } 166 # endif 167 168 #else /* DGK */ 169 170 while((foo = readchar()) >= '0' && foo <= '9') 171 multi = 10*multi+foo-'0'; 172 173 #endif /* DGK */ 174 175 if(multi) { 176 multi--; 177 save_cm = inline; 178 } 179 inline[0] = foo; 180 inline[1] = 0; 181 if(foo == 'g' || foo == 'G'){ 182 inline[1] = getchar(); 183 savech(inline[1]); 184 inline[2] = 0; 185 } 186 if(foo == 'm' || foo == 'M'){ 187 inline[1] = getchar(); 188 savech(inline[1]); 189 inline[2] = 0; 190 } 191 clrlin(); 192 return(inline); 193 } 194 195 char 196 readchar() { 197 register int sym; 198 199 (void) fflush(stdout); 200 sym = getchar(); 201 if(flags.toplin == 1) 202 flags.toplin = 2; 203 return((char) sym); 204 }