1: /* 2: * Copyright (c) 1980 Regents of the University of California. 3: * All rights reserved. The Berkeley software License Agreement 4: * specifies the terms and conditions for redistribution. 5: */ 6: 7: #if !defined(lint) && !defined(NOSCCS) 8: static char sccsid[] = "@(#)cr_tty.c 5.2.1 (2.11BSD GTE) 12/9/94"; 9: #endif 10: 11: /* 12: * Terminal initialization routines. 13: * 14: */ 15: 16: # include "curses.ext" 17: 18: static bool *sflags[] = { 19: &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI, 20: &MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS, 21: &XX 22: }; 23: 24: static char *_PC, 25: **sstrs[] = { 26: &AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS, 27: &DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2, 28: &K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC, 29: &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU, 30: &LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF, 31: &SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US, 32: &VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM, 33: &DOWN_PARM, &LEFT_PARM, &RIGHT_PARM, 34: }, 35: *tgoto(); 36: 37: char _tspace[2048]; /* Space for capability strings */ 38: 39: static char *aoftspace; /* Address of _tspace for relocation */ 40: 41: static int destcol, destline; 42: 43: /* 44: * This routine does terminal type initialization routines, and 45: * calculation of flags at entry. It is almost entirely stolen from 46: * Bill Joy's ex version 2.6. 47: */ 48: short ospeed = -1; 49: 50: gettmode() { 51: 52: if (gtty(_tty_ch, &_tty) < 0) 53: return; 54: savetty(); 55: if (stty(_tty_ch, &_tty) < 0) 56: _tty.sg_flags = _res_flg; 57: ospeed = _tty.sg_ospeed; 58: _res_flg = _tty.sg_flags; 59: GT = ((_tty.sg_flags & XTABS) == 0); 60: NONL = ((_tty.sg_flags & CRMOD) == 0); 61: _tty.sg_flags &= ~XTABS; 62: stty(_tty_ch, &_tty); 63: # ifdef DEBUG 64: fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE"); 65: fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE"); 66: fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed); 67: # endif 68: } 69: 70: setterm(type) 71: reg char *type; { 72: 73: reg int unknown; 74: static char genbuf[1024]; 75: # ifdef TIOCGWINSZ 76: struct winsize win; 77: # endif 78: 79: # ifdef DEBUG 80: fprintf(outf, "SETTERM(\"%s\")\n", type); 81: fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS); 82: # endif 83: if (type[0] == '\0') 84: type = "xx"; 85: unknown = FALSE; 86: if (tgetent(genbuf, type) != 1) { 87: unknown++; 88: strcpy(genbuf, "xx|dumb:"); 89: } 90: # ifdef DEBUG 91: fprintf(outf, "SETTERM: tty = %s\n", type); 92: # endif 93: # ifdef TIOCGWINSZ 94: if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) { 95: if (LINES == 0) 96: LINES = win.ws_row; 97: if (COLS == 0) 98: COLS = win.ws_col; 99: } 100: # endif 101: 102: if (LINES == 0) 103: LINES = tgetnum("li"); 104: if (LINES <= 5) 105: LINES = 24; 106: 107: if (COLS == 0) 108: COLS = tgetnum("co"); 109: if (COLS <= 4) 110: COLS = 80; 111: 112: # ifdef DEBUG 113: fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS); 114: # endif 115: aoftspace = _tspace; 116: zap(); /* get terminal description */ 117: 118: /* 119: * Handle funny termcap capabilities 120: */ 121: if (CS && SC && RC) AL=DL=""; 122: if (AL_PARM && AL==NULL) AL=""; 123: if (DL_PARM && DL==NULL) DL=""; 124: if (IC && IM==NULL) IM=""; 125: if (IC && EI==NULL) EI=""; 126: if (!GT) BT=NULL; /* If we can't tab, we can't backtab either */ 127: 128: if (tgoto(CM, destcol, destline)[0] == 'O') 129: CA = FALSE, CM = 0; 130: else 131: CA = TRUE; 132: 133: PC = _PC ? _PC[0] : FALSE; 134: aoftspace = _tspace; 135: strncpy(ttytype, longname(genbuf, type), sizeof(ttytype) - 1); 136: ttytype[sizeof(ttytype) - 1] = '\0'; 137: if (unknown) 138: return ERR; 139: return OK; 140: } 141: 142: /* 143: * This routine gets all the terminal flags from the termcap database 144: */ 145: 146: zap() 147: { 148: register char *namp; 149: register bool **fp; 150: register char ***sp; 151: #ifdef DEBUG 152: register char *cp; 153: #endif 154: extern char *tgetstr(); 155: 156: namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx"; 157: fp = sflags; 158: do { 159: *(*fp++) = tgetflag(namp); 160: #ifdef DEBUG 161: fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE"); 162: #endif 163: namp += 2; 164: } while (*namp); 165: namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI"; 166: sp = sstrs; 167: do { 168: *(*sp++) = tgetstr(namp, &aoftspace); 169: #ifdef DEBUG 170: fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\""); 171: if (*sp[-1] != NULL) { 172: for (cp = *sp[-1]; *cp; cp++) 173: fprintf(outf, "%s", unctrl(*cp)); 174: fprintf(outf, "\"\n"); 175: } 176: #endif 177: namp += 2; 178: } while (*namp); 179: if (XS) 180: SO = SE = NULL; 181: else { 182: if (tgetnum("sg") > 0) 183: SO = NULL; 184: if (tgetnum("ug") > 0) 185: US = NULL; 186: if (!SO && US) { 187: SO = US; 188: SE = UE; 189: } 190: } 191: } 192: 193: /* 194: * return a capability from termcap 195: */ 196: char * 197: getcap(name) 198: char *name; 199: { 200: char *tgetstr(); 201: 202: return tgetstr(name, &aoftspace); 203: }