termcap.c revision 84214
1169691Skan/* A portion of this file is from ncurses: */ 2169691Skan/*************************************************************************** 3169691Skan* COPYRIGHT NOTICE * 4169691Skan**************************************************************************** 5169691Skan* ncurses is copyright (C) 1992-1995 * 6169691Skan* Zeyd M. Ben-Halim * 7169691Skan* zmbenhal@netcom.com * 8169691Skan* Eric S. Raymond * 9169691Skan* esr@snark.thyrsus.com * 10169691Skan* * 11169691Skan* Permission is hereby granted to reproduce and distribute ncurses * 12169691Skan* by any means and for any fee, whether alone or as part of a * 13169691Skan* larger distribution, in source or in binary form, PROVIDED * 14169691Skan* this notice is included with any such distribution, and is not * 15169691Skan* removed from any of its header files. Mention of ncurses in any * 16169691Skan* applications linked with it is highly appreciated. * 17169691Skan* * 18169691Skan* ncurses comes AS IS with no warranty, implied or expressed. * 19169691Skan* * 20169691Skan***************************************************************************/ 21169691Skan 22169691Skan#include <curses.priv.h> 23169691Skan 24169691Skan#include <string.h> 25169691Skan#include <term.h> 26169691Skan#include <tic.h> 27169691Skan#include <term_entry.h> 28169691Skan 29169691Skan/* The rest is from BSD */ 30169691Skan/* 31169691Skan * Copyright (c) 1980, 1993 32169691Skan * The Regents of the University of California. All rights reserved. 33169691Skan * 34169691Skan * Redistribution and use in source and binary forms, with or without 35169691Skan * modification, are permitted provided that the following conditions 36169691Skan * are met: 37169691Skan * 1. Redistributions of source code must retain the above copyright 38169691Skan * notice, this list of conditions and the following disclaimer. 39169691Skan * 2. Redistributions in binary form must reproduce the above copyright 40169691Skan * notice, this list of conditions and the following disclaimer in the 41169691Skan * documentation and/or other materials provided with the distribution. 42169691Skan * 3. All advertising materials mentioning features or use of this software 43169691Skan * must display the following acknowledgement: 44169691Skan * This product includes software developed by the University of 45169691Skan * California, Berkeley and its contributors. 46169691Skan * 4. Neither the name of the University nor the names of its contributors 47169691Skan * may be used to endorse or promote products derived from this software 48169691Skan * without specific prior written permission. 49169691Skan * 50169691Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51169691Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52169691Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53169691Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54169691Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55169691Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56169691Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57169691Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58169691Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59169691Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60169691Skan * SUCH DAMAGE. 61169691Skan */ 62169691Skan 63169691Skan#include <sys/cdefs.h> 64169691Skan__FBSDID("$FreeBSD: head/lib/ncurses/ncurses/termcap.c 84214 2001-09-30 22:02:43Z dillon $"); 65169691Skan 66169691Skan#ifndef lint 67169691Skanstatic const char sccsid[] = "@(#)termcap.c 8.1 (Berkeley) 6/4/93"; 68169691Skan#endif /* not lint */ 69169691Skan 70169691Skan#include <stdio.h> 71169691Skan#include <ctype.h> 72169691Skan#include <stdlib.h> 73169691Skan#include <string.h> 74169691Skan#include <unistd.h> 75169691Skan#include <sys/param.h> 76169691Skan#include "pathnames.h" 77169691Skan 78169691Skan#define PBUFSIZ MAXPATHLEN /* max length of filename path */ 79169691Skan#define PVECSIZ 32 /* max number of names in path */ 80169691Skan#define TBUFSIZ 1024 /* max length of _nc_tgetent buffer */ 81169691Skan 82169691Skanchar _nc_termcap[TBUFSIZ + 1]; /* Last getcap, provided to tgetent() emul */ 83169691Skan 84169691Skan/* 85169691Skan * termcap - routines for dealing with the terminal capability data base 86169691Skan * 87169691Skan * BUG: Should use a "last" pointer in tbuf, so that searching 88169691Skan * for capabilities alphabetically would not be a n**2/2 89169691Skan * process when large numbers of capabilities are given. 90169691Skan * Note: If we add a last pointer now we will screw up the 91169691Skan * tc capability. We really should compile termcap. 92169691Skan * 93169691Skan * Essentially all the work here is scanning and decoding escapes 94169691Skan * in string capabilities. We don't use stdio because the editor 95169691Skan * doesn't, and because living w/o it is not hard. 96169691Skan */ 97169691Skan 98169691Skan/* 99169691Skan * Get an entry for terminal name in buffer _nc_termcap from the termcap 100169691Skan * file. 101169691Skan */ 102169691Skanint 103169691Skan_nc_read_termcap_entry(const char *const name, TERMTYPE *const tp) 104169691Skan{ 105169691Skan ENTRY *ep; 106169691Skan register char *p; 107169691Skan register char *cp; 108169691Skan char *dummy; 109169691Skan char **fname; 110169691Skan char *home; 111169691Skan int i; 112169691Skan char pathbuf[PBUFSIZ]; /* holds raw path of filenames */ 113169691Skan char *pathvec[PVECSIZ]; /* to point to names in pathbuf */ 114169691Skan char **pvec; /* holds usable tail of path vector */ 115169691Skan char *termpath; 116169691Skan 117169691Skan _nc_termcap[0] = '\0'; /* in case */ 118169691Skan dummy = NULL; 119169691Skan fname = pathvec; 120169691Skan pvec = pathvec; 121169691Skan p = pathbuf; 122169691Skan cp = getenv("TERMCAP"); 123169691Skan /* 124169691Skan * TERMCAP can have one of two things in it. It can be the 125169691Skan * name of a file to use instead of /etc/termcap. In this 126169691Skan * case it better start with a "/". Or it can be an entry to 127169691Skan * use so we don't have to read the file. In this case it 128169691Skan * has to already have the newlines crunched out. If TERMCAP 129169691Skan * does not hold a file name then a path of names is searched 130169691Skan * instead. The path is found in the TERMPATH variable, or 131169691Skan * becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists. 132169691Skan */ 133169691Skan if (!cp || *cp != '/') { /* no TERMCAP or it holds an entry */ 134169691Skan if ( (termpath = getenv("TERMPATH")) ) 135169691Skan strncpy(pathbuf, termpath, PBUFSIZ); 136169691Skan else { 137169691Skan if ( (home = getenv("HOME")) ) {/* set up default */ 138169691Skan strncpy(pathbuf, home, PBUFSIZ - 1); /* $HOME first */ 139169691Skan pathbuf[PBUFSIZ - 2] = '\0'; /* -2 because we add a slash */ 140169691Skan p += strlen(pathbuf); /* path, looking in */ 141169691Skan *p++ = '/'; 142169691Skan } /* if no $HOME look in current directory */ 143169691Skan strncpy(p, _PATH_DEF, PBUFSIZ - (p - pathbuf)); 144169691Skan } 145169691Skan } 146169691Skan else /* user-defined name in TERMCAP */ 147169691Skan strncpy(pathbuf, cp, PBUFSIZ); /* still can be tokenized */ 148169691Skan 149169691Skan /* For safety */ 150169691Skan if (issetugid()) 151169691Skan strcpy(pathbuf, _PATH_DEF_SEC); 152169691Skan 153169691Skan pathbuf[PBUFSIZ - 1] = '\0'; 154169691Skan 155169691Skan *fname++ = pathbuf; /* tokenize path into vector of names */ 156169691Skan while (*++p) 157169691Skan if (*p == ' ' || *p == ':') { 158169691Skan *p = '\0'; 159169691Skan while (*++p) 160169691Skan if (*p != ' ' && *p != ':') 161169691Skan break; 162169691Skan if (*p == '\0') 163169691Skan break; 164169691Skan *fname++ = p; 165169691Skan if (fname >= pathvec + PVECSIZ) { 166169691Skan fname--; 167169691Skan break; 168169691Skan } 169169691Skan } 170169691Skan *fname = (char *) 0; /* mark end of vector */ 171169691Skan if (cp && *cp && *cp != '/') 172169691Skan if (cgetset(cp) < 0) 173169691Skan return(-2); 174169691Skan 175169691Skan i = cgetent(&dummy, pathvec, (char *)name); 176169691Skan 177169691Skan if (i == 0) { 178169691Skan char *pd, *ps, *tok, *s, *tcs; 179169691Skan size_t len; 180169691Skan 181169691Skan pd = _nc_termcap; 182169691Skan ps = dummy; 183169691Skan if ((tok = strchr(ps, ':')) == NULL) { 184169691Skan len = strlen(ps); 185169691Skan if (len >= TBUFSIZ) 186169691Skan i = -1; 187169691Skan else 188169691Skan strcpy(pd, ps); 189169691Skan goto done; 190169691Skan } 191169691Skan len = tok - ps + 1; 192169691Skan if (pd + len + 1 - _nc_termcap >= TBUFSIZ) { 193169691Skan i = -1; 194169691Skan goto done; 195169691Skan } 196169691Skan memcpy(pd, ps, len); 197169691Skan ps += len; 198169691Skan pd += len; 199169691Skan *pd = '\0'; 200169691Skan tcs = pd - 1; 201169691Skan for (;;) { 202169691Skan while ((tok = strsep(&ps, ":")) != NULL && 203169691Skan (*tok == '\0' || *tok == '\\' || !isgraph(*tok))) 204169691Skan ; 205169691Skan if (tok == NULL) 206169691Skan break; 207169691Skan for (s = tcs; s != NULL && s[1] != '\0'; 208169691Skan s = strchr(s, ':')) { 209169691Skan s++; 210169691Skan if (s[0] == tok[0] && s[1] == tok[1]) 211169691Skan goto skip_it; 212169691Skan } 213169691Skan len = strlen(tok); 214169691Skan if (pd + len + 1 - _nc_termcap >= TBUFSIZ) { 215169691Skan i = -1; 216169691Skan break; 217169691Skan } 218169691Skan memcpy(pd, tok, len); 219169691Skan pd += len; 220169691Skan *pd++ = ':'; 221169691Skan *pd = '\0'; 222169691Skan skip_it: ; 223169691Skan } 224169691Skan } 225169691Skandone: 226169691Skan if (dummy) 227169691Skan free(dummy); 228169691Skan 229169691Skan 230169691Skan/* 231169691Skan * From here on is ncurses-specific glue code 232169691Skan */ 233169691Skan 234169691Skan if (i < 0) 235169691Skan return(ERR); 236169691Skan 237169691Skan _nc_set_source("TERMCAP"); 238169691Skan _nc_read_entry_source((FILE *)NULL, _nc_termcap, FALSE, TRUE, NULLHOOK); 239169691Skan 240169691Skan if (_nc_head == (ENTRY *)NULL) 241169691Skan return(ERR); 242169691Skan 243169691Skan /* resolve all use references */ 244169691Skan _nc_resolve_uses(TRUE); 245169691Skan 246169691Skan for_entry_list(ep) 247169691Skan if (_nc_name_match(ep->tterm.term_names, name, "|:")) 248169691Skan { 249169691Skan /* 250169691Skan * Make a local copy of the terminal capabilities. free 251169691Skan * all entry storage except the string table for the 252169691Skan * loaded type (which we disconnected from the list by 253169691Skan * NULLing out ep->tterm.str_table above). 254169691Skan */ 255169691Skan memcpy(tp, &ep->tterm, sizeof(TERMTYPE)); 256169691Skan ep->tterm.str_table = (char *)NULL; 257169691Skan _nc_free_entries(_nc_head); 258169691Skan _nc_head = _nc_tail = NULL; /* do not reuse! */ 259169691Skan 260169691Skan return 1; /* OK */ 261169691Skan } 262169691Skan 263169691Skan _nc_free_entries(_nc_head); 264169691Skan _nc_head = _nc_tail = NULL; /* do not reuse! */ 265169691Skan return(0); /* not found */ 266169691Skan} 267169691Skan