1: /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
   2: /* hack.tty.c - version 1.0.4 (2.11BSD) 1997/3/28 */
   3: 
   4: #include    "hack.h"
   5: #include    <stdio.h>
   6: 
   7: /*
   8:  * Some systems may have getchar() return EOF for various reasons, and
   9:  * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
  10:  * Also see the comment in config.h on `VERSION7'.
  11:  */
  12: #define NR_OF_EOFS  20
  13: 
  14: #include    <sgtty.h>
  15: #define termstruct  sgttyb
  16: #define kill_sym    sg_kill
  17: #define erase_sym   sg_erase
  18: #define tabflgs     sg_flags
  19: #define echoflgs    sg_flags
  20: #define cbrkflgs    sg_flags
  21: #define CBRKMASK    CBREAK
  22: #define CBRKON      /* empty */
  23: #define OSPEED(x)   (x).sg_ospeed
  24: #define GTTY(x)     (gtty(0, x))
  25: #define STTY(x)     (stty(0, x))
  26: 
  27: extern short ospeed;
  28: static char erase_char, kill_char;
  29: static boolean settty_needed = FALSE;
  30: struct termstruct inittyb, curttyb;
  31: 
  32: /*
  33:  * Get initial state of terminal, set ospeed (for termcap routines)
  34:  * and switch off tab expansion if necessary.
  35:  * Called by startup() in termcap.c and after returning from ! or ^Z
  36:  */
  37: gettty(){
  38:     if(GTTY(&inittyb) < 0)
  39:         perror("Hack (gettty)");
  40:     curttyb = inittyb;
  41:     ospeed = OSPEED(inittyb);
  42:     erase_char = inittyb.erase_sym;
  43:     kill_char = inittyb.kill_sym;
  44:     getioctls();
  45: 
  46:     /* do not expand tabs - they might be needed inside a cm sequence */
  47:     if(curttyb.tabflgs & XTABS) {
  48:         curttyb.tabflgs &= ~XTABS;
  49:         setctty();
  50:     }
  51:     settty_needed = TRUE;
  52: }
  53: 
  54: /* reset terminal to original state */
  55: settty(s) char *s; {
  56:     clear_screen();
  57:     end_screen();
  58:     if(s) printf(s);
  59:     (void) fflush(stdout);
  60:     if(STTY(&inittyb) < 0)
  61:         perror("Hack (settty)");
  62:     flags.echo = (inittyb.echoflgs & ECHO) ? ON : OFF;
  63:     flags.cbreak = (CBRKON(inittyb.cbrkflgs & CBRKMASK)) ? ON : OFF;
  64:     setioctls();
  65: }
  66: 
  67: setctty(){
  68:     if(STTY(&curttyb) < 0)
  69:         perror("Hack (setctty)");
  70: }
  71: 
  72: 
  73: setftty(){
  74: register int ef = 0;            /* desired value of flags & ECHO */
  75: register int cf = CBRKON(CBRKMASK); /* desired value of flags & CBREAK */
  76: register int change = 0;
  77:     flags.cbreak = ON;
  78:     flags.echo = OFF;
  79:     /* Should use (ECHO|CRMOD) here instead of ECHO */
  80:     if((curttyb.echoflgs & ECHO) != ef){
  81:         curttyb.echoflgs &= ~ECHO;
  82: /*		curttyb.echoflgs |= ef;					*/
  83:         change++;
  84:     }
  85:     if((curttyb.cbrkflgs & CBRKMASK) != cf){
  86:         curttyb.cbrkflgs &= ~CBRKMASK;
  87:         curttyb.cbrkflgs |= cf;
  88:         change++;
  89:     }
  90:     if(change){
  91:         setctty();
  92:     }
  93:     start_screen();
  94: }
  95: 
  96: 
  97: /* fatal error */
  98: /*VARARGS1*/
  99: error(s,x,y) char *s; {
 100:     if(settty_needed)
 101:         settty((char *) 0);
 102:     printf(s,x,y);
 103:     putchar('\n');
 104:     exit(1);
 105: }
 106: 
 107: /*
 108:  * Read a line closed with '\n' into the array char bufp[BUFSZ].
 109:  * (The '\n' is not stored. The string is closed with a '\0'.)
 110:  * Reading can be interrupted by an escape ('\033') - now the
 111:  * resulting string is "\033".
 112:  */
 113: getlin(bufp)
 114: register char *bufp;
 115: {
 116:     register char *obufp = bufp;
 117:     register int c;
 118: 
 119:     flags.toplin = 2;       /* nonempty, no --More-- required */
 120:     for(;;) {
 121: /*		(void) fflush(stdout);	/* readchar already does this */
 122:         if((c = readchar()) == EOF) {
 123:             *bufp = 0;
 124:             return;
 125:         }
 126:         if(c == '\033') {
 127:             *obufp = c;
 128:             obufp[1] = 0;
 129:             return;
 130:         }
 131:         if(c == erase_char || c == '\b') {
 132:             if(bufp != obufp) {
 133:                 bufp--;
 134:                 putstr("\b \b"); /* putsym converts \b */
 135:             } else  bell();
 136:         } else if(c == '\n') {
 137:             *bufp = 0;
 138:             return;
 139:         } else if(' ' <= c && c < '\177') {
 140:                 /* avoid isprint() - some people don't have it
 141: 				   ' ' is not always a printing char */
 142:             *bufp = c;
 143:             bufp[1] = 0;
 144:             putstr(bufp);
 145:             if(bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)
 146:                 bufp++;
 147:         } else if(c == kill_char || c == '\177') { /* Robert Viduya */
 148:                 /* this test last - @ might be the kill_char */
 149:             while(bufp != obufp) {
 150:                 bufp--;
 151:                 putstr("\b \b");
 152:             }
 153:         } else
 154:             bell();
 155:     }
 156: }
 157: 
 158: getret() {
 159:     cgetret("");
 160: }
 161: 
 162: cgetret(s)
 163: register char *s;
 164: {
 165:     putsym('\n');
 166:     if(flags.standout)
 167:         standoutbeg();
 168:     putstr("Hit ");
 169:     putstr(flags.cbreak ? "space" : "return");
 170:     putstr(" to continue: ");
 171:     if(flags.standout)
 172:         standoutend();
 173:     xwaitforspace(s);
 174: }
 175: 
 176: char morc;  /* tell the outside world what char he used */
 177: 
 178: xwaitforspace(s)
 179: register char *s;   /* chars allowed besides space or return */
 180: {
 181: register int c;
 182: 
 183:     morc = 0;
 184: 
 185:     while((c = readchar()) != '\n') {
 186:         if(flags.cbreak) {
 187:         if(c == ' ') break;
 188:         if(s && index(s,c)) {
 189:             morc = c;
 190:             break;
 191:         }
 192:         bell();
 193:         }
 194:     }
 195: }
 196: 
 197: char *
 198: parse()
 199: {
 200:     static char inline[COLNO];
 201:     register foo;
 202: 
 203:     flags.move = 1;
 204:     if(!Invisible) curs_on_u(); else home();
 205:     while((foo = readchar()) >= '0' && foo <= '9')
 206:         multi = 10*multi+foo-'0';
 207:     if(multi) {
 208:         multi--;
 209:         save_cm = inline;
 210:     }
 211:     inline[0] = foo;
 212:     inline[1] = 0;
 213:     if(foo == 'f' || foo == 'F'){
 214:         inline[1] = readchar();
 215: #ifdef QUEST
 216:         if(inline[1] == foo) inline[2] = readchar(); else
 217: #endif QUEST
 218:         inline[2] = 0;
 219:     }
 220:     if(foo == 'm' || foo == 'M'){
 221:         inline[1] = readchar();
 222:         inline[2] = 0;
 223:     }
 224:     clrlin();
 225:     return(inline);
 226: }
 227: 
 228: char
 229: readchar() {
 230:     register int sym;
 231: 
 232:     (void) fflush(stdout);
 233:     if((sym = getchar()) == EOF)
 234: #ifdef NR_OF_EOFS
 235:     { /*
 236: 	   * VERSION7 and some SYSV systems seem to return EOFs for various
 237: 	   * reasons (?like when one hits break or for interrupted
 238: 	   * systemcalls?), and we must see several before we quit.
 239: 	   */
 240:         register int cnt = NR_OF_EOFS;
 241:         while (cnt--) {
 242:             clearerr(stdin);    /* omit if clearerr is undefined */
 243:             if((sym = getchar()) != EOF) goto noteof;
 244:         }
 245:         end_of_input();
 246:          noteof:    ;
 247:     }
 248: #else
 249:         end_of_input();
 250: #endif NR_OF_EOFS
 251:     if(flags.toplin == 1)
 252:         flags.toplin = 2;
 253:     return((char) sym);
 254: }
 255: 
 256: end_of_input()
 257: {
 258:     settty("End of input?\n");
 259:     clearlocks();
 260:     exit(0);
 261: }

Defined functions

cgetret defined in line 162; used 2 times
end_of_input defined in line 256; used 4 times
parse defined in line 197; used 2 times
setctty defined in line 67; used 2 times
xwaitforspace defined in line 178; used 2 times

Defined variables

curttyb defined in line 30; used 9 times
erase_char defined in line 28; used 2 times
inittyb defined in line 30; used 8 times
kill_char defined in line 28; used 2 times
morc defined in line 176; used 5 times
settty_needed defined in line 29; used 2 times

Defined macros

CBRKMASK defined in line 21; used 4 times
CBRKON defined in line 22; used 2 times
GTTY defined in line 24; used 1 times
  • in line 38
NR_OF_EOFS defined in line 12; used 2 times
OSPEED defined in line 23; used 1 times
  • in line 41
STTY defined in line 25; used 2 times
cbrkflgs defined in line 20; used 4 times
echoflgs defined in line 19; used 3 times
erase_sym defined in line 17; used 1 times
  • in line 42
kill_sym defined in line 16; used 1 times
  • in line 43
tabflgs defined in line 18; used 2 times
termstruct defined in line 15; never used
Last modified: 1997-03-28
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3895
Valid CSS Valid XHTML 1.0 Strict