12243Sarnold /* 234677Sbostic * Copyright (c) 1981 Regents of the University of California. 334677Sbostic * All rights reserved. 434677Sbostic * 534677Sbostic * Redistribution and use in source and binary forms are permitted 6*34924Sbostic * provided that the above copyright notice and this paragraph are 7*34924Sbostic * duplicated in all such forms and that any documentation, 8*34924Sbostic * advertising materials, and other materials related to such 9*34924Sbostic * distribution and use acknowledge that the software was developed 10*34924Sbostic * by the University of California, Berkeley. The name of the 11*34924Sbostic * University may not be used to endorse or promote products derived 12*34924Sbostic * from this software without specific prior written permission. 13*34924Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34924Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34924Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1622656Sdist */ 1722656Sdist 1822656Sdist #ifndef lint 19*34924Sbostic static char sccsid[] = "@(#)setterm.c 5.4 (Berkeley) 06/30/88"; 2034677Sbostic #endif /* not lint */ 2122656Sdist 2222656Sdist /* 232243Sarnold * Terminal initialization routines. 242243Sarnold * 252243Sarnold */ 262243Sarnold 272243Sarnold # include "curses.ext" 282243Sarnold 2919875Sbloom static bool *sflags[] = { 3019875Sbloom &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI, 3121040Sbloom &MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS, 3221040Sbloom &XX 332243Sarnold }; 342243Sarnold 3519875Sbloom static char *_PC, 3619875Sbloom **sstrs[] = { 3719875Sbloom &AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS, 3819875Sbloom &DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2, 3919875Sbloom &K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC, 4019875Sbloom &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU, 4119875Sbloom &LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF, 4219875Sbloom &SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US, 4319875Sbloom &VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM, 4419875Sbloom &DOWN_PARM, &LEFT_PARM, &RIGHT_PARM, 452243Sarnold }, 462465Sarnold *tgoto(); 472243Sarnold 4819875Sbloom char _tspace[2048]; /* Space for capability strings */ 492243Sarnold 5019875Sbloom static char *aoftspace; /* Address of _tspace for relocation */ 5119875Sbloom 522243Sarnold static int destcol, destline; 532243Sarnold 542243Sarnold /* 552243Sarnold * This routine does terminal type initialization routines, and 562243Sarnold * calculation of flags at entry. It is almost entirely stolen from 572243Sarnold * Bill Joy's ex version 2.6. 582243Sarnold */ 592243Sarnold short ospeed = -1; 602243Sarnold 612243Sarnold gettmode() { 622243Sarnold 632243Sarnold if (gtty(_tty_ch, &_tty) < 0) 642243Sarnold return; 652243Sarnold savetty(); 662243Sarnold if (stty(_tty_ch, &_tty) < 0) 672243Sarnold _tty.sg_flags = _res_flg; 682243Sarnold ospeed = _tty.sg_ospeed; 692243Sarnold _res_flg = _tty.sg_flags; 702243Sarnold UPPERCASE = (_tty.sg_flags & LCASE) != 0; 712243Sarnold GT = ((_tty.sg_flags & XTABS) == 0); 722243Sarnold NONL = ((_tty.sg_flags & CRMOD) == 0); 733631Sarnold _tty.sg_flags &= ~XTABS; 744612Sarnold stty(_tty_ch, &_tty); 752243Sarnold # ifdef DEBUG 762243Sarnold fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE"); 772243Sarnold fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE"); 782243Sarnold fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE"); 792243Sarnold fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed); 802243Sarnold # endif 812243Sarnold } 822243Sarnold 832243Sarnold setterm(type) 842243Sarnold reg char *type; { 852243Sarnold 8611736Sarnold reg int unknown; 8711736Sarnold static char genbuf[1024]; 8819875Sbloom # ifdef TIOCGWINSZ 8918032Sbloom struct winsize win; 9019875Sbloom # endif 912243Sarnold 922243Sarnold # ifdef DEBUG 932243Sarnold fprintf(outf, "SETTERM(\"%s\")\n", type); 942243Sarnold fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS); 952243Sarnold # endif 962243Sarnold if (type[0] == '\0') 972243Sarnold type = "xx"; 982243Sarnold unknown = FALSE; 992243Sarnold if (tgetent(genbuf, type) != 1) { 1002243Sarnold unknown++; 1012243Sarnold strcpy(genbuf, "xx|dumb:"); 1022243Sarnold } 1032243Sarnold # ifdef DEBUG 1042243Sarnold fprintf(outf, "SETTERM: tty = %s\n", type); 1052243Sarnold # endif 10619875Sbloom # ifdef TIOCGWINSZ 10718032Sbloom if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) { 10818032Sbloom if (LINES == 0) 10918032Sbloom LINES = win.ws_row; 11018032Sbloom if (COLS == 0) 11118032Sbloom COLS = win.ws_col; 11218032Sbloom } 11319875Sbloom # endif 11418032Sbloom 1152243Sarnold if (LINES == 0) 1162243Sarnold LINES = tgetnum("li"); 1172243Sarnold if (LINES <= 5) 1182243Sarnold LINES = 24; 1192243Sarnold 1202243Sarnold if (COLS == 0) 1212243Sarnold COLS = tgetnum("co"); 1222243Sarnold if (COLS <= 4) 1232243Sarnold COLS = 80; 12412570Sarnold 1252243Sarnold # ifdef DEBUG 1262243Sarnold fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS); 1272243Sarnold # endif 12819875Sbloom aoftspace = _tspace; 1292243Sarnold zap(); /* get terminal description */ 13019875Sbloom 13119875Sbloom /* 13219875Sbloom * Handle funny termcap capabilities 13319875Sbloom */ 13419875Sbloom if (CS && SC && RC) AL=DL=""; 13519875Sbloom if (AL_PARM && AL==NULL) AL=""; 13619875Sbloom if (DL_PARM && DL==NULL) DL=""; 13719875Sbloom if (IC && IM==NULL) IM=""; 13819875Sbloom if (IC && EI==NULL) EI=""; 13919875Sbloom if (!GT) BT=NULL; /* If we can't tab, we can't backtab either */ 14019875Sbloom 1412243Sarnold if (tgoto(CM, destcol, destline)[0] == 'O') 1422243Sarnold CA = FALSE, CM = 0; 1432243Sarnold else 1442243Sarnold CA = TRUE; 14519875Sbloom 14619875Sbloom PC = _PC ? _PC[0] : FALSE; 14719875Sbloom aoftspace = _tspace; 14825427Sbloom strncpy(ttytype, longname(genbuf, type), sizeof(ttytype) - 1); 14925427Sbloom ttytype[sizeof(ttytype) - 1] = '\0'; 1502243Sarnold if (unknown) 1512243Sarnold return ERR; 1522243Sarnold return OK; 1532243Sarnold } 15419875Sbloom 1552243Sarnold /* 1564616Sarnold * This routine gets all the terminal flags from the termcap database 1572243Sarnold */ 1582243Sarnold 15919875Sbloom zap() 16019875Sbloom { 16119875Sbloom register char *namp; 16219875Sbloom register bool **fp; 16319875Sbloom register char ***sp; 16419875Sbloom #ifdef DEBUG 16519875Sbloom register char *cp; 16619875Sbloom #endif 1672243Sarnold extern char *tgetstr(); 1682243Sarnold 16921040Sbloom namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx"; 1702243Sarnold fp = sflags; 1712243Sarnold do { 1722243Sarnold *(*fp++) = tgetflag(namp); 17319875Sbloom #ifdef DEBUG 17419875Sbloom fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE"); 17519875Sbloom #endif 1762243Sarnold namp += 2; 1772243Sarnold } while (*namp); 17819875Sbloom namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI"; 1792243Sarnold sp = sstrs; 1802243Sarnold do { 1812243Sarnold *(*sp++) = tgetstr(namp, &aoftspace); 18219875Sbloom #ifdef DEBUG 18319875Sbloom fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\""); 18419875Sbloom if (*sp[-1] != NULL) { 18519875Sbloom for (cp = *sp[-1]; *cp; cp++) 18619875Sbloom fprintf(outf, "%s", unctrl(*cp)); 18719875Sbloom fprintf(outf, "\"\n"); 18819875Sbloom } 18919875Sbloom #endif 1902243Sarnold namp += 2; 1912243Sarnold } while (*namp); 19221040Sbloom if (XS) 19321040Sbloom SO = SE = NULL; 19421040Sbloom else { 19521040Sbloom if (tgetnum("sg") > 0) 19621040Sbloom SO = NULL; 19721040Sbloom if (tgetnum("ug") > 0) 19821040Sbloom US = NULL; 19921040Sbloom if (!SO && US) { 20021040Sbloom SO = US; 20121040Sbloom SE = UE; 20221040Sbloom } 2032243Sarnold } 2042243Sarnold } 2053786Sarnold 2063786Sarnold /* 2073786Sarnold * return a capability from termcap 2083786Sarnold */ 2093786Sarnold char * 2103786Sarnold getcap(name) 2113786Sarnold char *name; 2123786Sarnold { 2134616Sarnold char *tgetstr(); 2144615Sarnold 2154616Sarnold return tgetstr(name, &aoftspace); 2163786Sarnold } 217