1 /* 2 * Copyright (c) 1981, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)setterm.c 8.4 (Berkeley) 05/04/94"; 10 #endif /* not lint */ 11 12 #include <stdlib.h> 13 #include <string.h> 14 #include <termios.h> 15 #include <unistd.h> 16 17 #include "curses.h" 18 19 static void zap __P((void)); 20 21 static char *sflags[] = { 22 /* am bs da eo hc in mi ms */ 23 &AM, &BS, &DA, &EO, &HC, &IN, &MI, &MS, 24 /* nc ns os ul xb xn xt xs xx */ 25 &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS, &XX 26 }; 27 28 static char *_PC, 29 **sstrs[] = { 30 /* AL bc bt cd ce cl cm cr cs */ 31 &AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS, 32 /* dc DL dm do ed ei k0 k1 k2 */ 33 &DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2, 34 /* k3 k4 k5 k6 k7 k8 k9 ho ic */ 35 &K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC, 36 /* im ip kd ke kh kl kr ks ku */ 37 &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU, 38 /* ll ma nd nl pc rc sc se SF */ 39 &LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF, 40 /* so SR ta te ti uc ue up us */ 41 &SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US, 42 /* vb vs ve al dl sf sr AL */ 43 &VB, &VS, &VE, &al, &dl, &sf, &sr, &AL_PARM, 44 /* DL UP DO LE */ 45 &DL_PARM, &UP_PARM, &DOWN_PARM, &LEFT_PARM, 46 /* RI */ 47 &RIGHT_PARM, 48 }; 49 50 static char *aoftspace; /* Address of _tspace for relocation */ 51 static char tspace[2048]; /* Space for capability strings */ 52 53 char *ttytype; 54 55 int 56 setterm(type) 57 register char *type; 58 { 59 static char genbuf[1024]; 60 static char __ttytype[1024]; 61 register int unknown; 62 struct winsize win; 63 char *p; 64 65 #ifdef DEBUG 66 __CTRACE("setterm: (\"%s\")\nLINES = %d, COLS = %d\n", 67 type, LINES, COLS); 68 #endif 69 if (type[0] == '\0') 70 type = "xx"; 71 unknown = 0; 72 if (tgetent(genbuf, type) != 1) { 73 unknown++; 74 strcpy(genbuf, "xx|dumb:"); 75 } 76 #ifdef DEBUG 77 __CTRACE("setterm: tty = %s\n", type); 78 #endif 79 80 /* Try TIOCGWINSZ, and, if it fails, the termcap entry. */ 81 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1 && 82 win.ws_row != 0 && win.ws_col != 0) { 83 LINES = win.ws_row; 84 COLS = win.ws_col; 85 } else { 86 LINES = tgetnum("li"); 87 COLS = tgetnum("co"); 88 } 89 90 /* POSIX 1003.2 requires that the environment override. */ 91 if ((p = getenv("LINES")) != NULL) 92 LINES = strtol(p, NULL, 10); 93 if ((p = getenv("COLUMNS")) != NULL) 94 COLS = strtol(p, NULL, 10); 95 96 /* 97 * Want cols > 4, otherwise things will fail. 98 */ 99 if (COLS <= 4) 100 return (ERR); 101 102 #ifdef DEBUG 103 __CTRACE("setterm: LINES = %d, COLS = %d\n", LINES, COLS); 104 #endif 105 aoftspace = tspace; 106 zap(); /* Get terminal description. */ 107 108 /* If we can't tab, we can't backtab, either. */ 109 if (!GT) 110 BT = NULL; 111 112 /* 113 * Test for cursor motion capbility. 114 * 115 * XXX 116 * This is truly stupid -- tgoto returns "OOPS" if it can't 117 * do cursor motions. 118 */ 119 if (tgoto(CM, 0, 0)[0] == 'O') { 120 CA = 0; 121 CM = 0; 122 } else 123 CA = 1; 124 125 PC = _PC ? _PC[0] : 0; 126 aoftspace = tspace; 127 ttytype = longname(genbuf, __ttytype); 128 129 if ((!AL && !al) || (!DL && !dl)) 130 __noqch = 1; 131 132 return (unknown ? ERR : OK); 133 } 134 135 /* 136 * zap -- 137 * Gets all the terminal flags from the termcap database. 138 */ 139 static void 140 zap() 141 { 142 register char *namp, ***sp; 143 register char **fp; 144 char tmp[3]; 145 #ifdef DEBUG 146 register char *cp; 147 #endif 148 tmp[2] = '\0'; 149 150 namp = "ambsdaeohcinmimsncnsosulxbxnxtxsxx"; 151 fp = sflags; 152 do { 153 *tmp = *namp; 154 *(tmp + 1) = *(namp + 1); 155 *(*fp++) = tgetflag(tmp); 156 #ifdef DEBUG 157 __CTRACE("2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE"); 158 #endif 159 namp += 2; 160 161 } while (*namp); 162 namp = "ALbcbtcdceclcmcrcsdcDLdmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscseSFsoSRtatetiucueupusvbvsvealdlsfsrALDLUPDOLERI"; 163 sp = sstrs; 164 do { 165 *tmp = *namp; 166 *(tmp + 1) = *(namp + 1); 167 *(*sp++) = tgetstr(tmp, &aoftspace); 168 #ifdef DEBUG 169 __CTRACE("2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\""); 170 if (*sp[-1] != NULL) { 171 for (cp = *sp[-1]; *cp; cp++) 172 __CTRACE("%s", unctrl(*cp)); 173 __CTRACE("\"\n"); 174 } 175 #endif 176 namp += 2; 177 } while (*namp); 178 if (XS) 179 SO = SE = NULL; 180 else { 181 if (tgetnum("sg") > 0) 182 SO = NULL; 183 if (tgetnum("ug") > 0) 184 US = NULL; 185 if (!SO && US) { 186 SO = US; 187 SE = UE; 188 } 189 } 190 } 191 192 /* 193 * getcap -- 194 * Return a capability from termcap. 195 */ 196 char * 197 getcap(name) 198 char *name; 199 { 200 return (tgetstr(name, &aoftspace)); 201 } 202