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