termcap.c revision 225736
1/* A portion of this file is from ncurses: */ 2/*************************************************************************** 3* COPYRIGHT NOTICE * 4**************************************************************************** 5* ncurses is copyright (C) 1992-1995 * 6* Zeyd M. Ben-Halim * 7* zmbenhal@netcom.com * 8* Eric S. Raymond * 9* esr@snark.thyrsus.com * 10* * 11* Permission is hereby granted to reproduce and distribute ncurses * 12* by any means and for any fee, whether alone or as part of a * 13* larger distribution, in source or in binary form, PROVIDED * 14* this notice is included with any such distribution, and is not * 15* removed from any of its header files. Mention of ncurses in any * 16* applications linked with it is highly appreciated. * 17* * 18* ncurses comes AS IS with no warranty, implied or expressed. * 19* * 20***************************************************************************/ 21 22#include <curses.priv.h> 23 24#include <string.h> 25#include <term.h> 26#include <tic.h> 27#include <term_entry.h> 28 29/* The rest is from BSD */ 30/* 31 * Copyright (c) 1980, 1993 32 * The Regents of the University of California. All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 4. Neither the name of the University nor the names of its contributors 43 * may be used to endorse or promote products derived from this software 44 * without specific prior written permission. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 56 * SUCH DAMAGE. 57 */ 58 59#include <sys/cdefs.h> 60__FBSDID("$FreeBSD: stable/9/lib/ncurses/ncurses/termcap.c 166130 2007-01-20 07:46:45Z rafan $"); 61 62#ifndef lint 63static const char sccsid[] = "@(#)termcap.c 8.1 (Berkeley) 6/4/93"; 64#endif /* not lint */ 65 66#include <stdio.h> 67#include <ctype.h> 68#include <stdlib.h> 69#include <string.h> 70#include <unistd.h> 71#include <sys/param.h> 72#include "pathnames.h" 73 74#define PBUFSIZ MAXPATHLEN /* max length of filename path */ 75#define PVECSIZ 32 /* max number of names in path */ 76#define TBUFSIZ 1024 /* max length of _nc_tgetent buffer */ 77 78char _nc_termcap[TBUFSIZ + 1]; /* Last getcap, provided to tgetent() emul */ 79 80/* 81 * termcap - routines for dealing with the terminal capability data base 82 * 83 * BUG: Should use a "last" pointer in tbuf, so that searching 84 * for capabilities alphabetically would not be a n**2/2 85 * process when large numbers of capabilities are given. 86 * Note: If we add a last pointer now we will screw up the 87 * tc capability. We really should compile termcap. 88 * 89 * Essentially all the work here is scanning and decoding escapes 90 * in string capabilities. We don't use stdio because the editor 91 * doesn't, and because living w/o it is not hard. 92 */ 93 94/* 95 * Get an entry for terminal name in buffer _nc_termcap from the termcap 96 * file. 97 */ 98int 99_nc_read_termcap_entry(const char *const name, TERMTYPE *const tp) 100{ 101 ENTRY *ep; 102 char *p; 103 char *cp; 104 char *dummy; 105 char **fname; 106 char *home; 107 int i; 108 char pathbuf[PBUFSIZ]; /* holds raw path of filenames */ 109 char *pathvec[PVECSIZ]; /* to point to names in pathbuf */ 110 char **pvec; /* holds usable tail of path vector */ 111 char *termpath; 112 113 _nc_termcap[0] = '\0'; /* in case */ 114 dummy = NULL; 115 fname = pathvec; 116 pvec = pathvec; 117 p = pathbuf; 118 cp = getenv("TERMCAP"); 119 /* 120 * TERMCAP can have one of two things in it. It can be the 121 * name of a file to use instead of /etc/termcap. In this 122 * case it better start with a "/". Or it can be an entry to 123 * use so we don't have to read the file. In this case it 124 * has to already have the newlines crunched out. If TERMCAP 125 * does not hold a file name then a path of names is searched 126 * instead. The path is found in the TERMPATH variable, or 127 * becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists. 128 */ 129 if (!cp || *cp != '/') { /* no TERMCAP or it holds an entry */ 130 if ( (termpath = getenv("TERMPATH")) ) 131 strncpy(pathbuf, termpath, PBUFSIZ); 132 else { 133 if ( (home = getenv("HOME")) ) {/* set up default */ 134 strncpy(pathbuf, home, PBUFSIZ - 1); /* $HOME first */ 135 pathbuf[PBUFSIZ - 2] = '\0'; /* -2 because we add a slash */ 136 p += strlen(pathbuf); /* path, looking in */ 137 *p++ = '/'; 138 } /* if no $HOME look in current directory */ 139 strncpy(p, _PATH_DEF, PBUFSIZ - (p - pathbuf)); 140 } 141 } 142 else /* user-defined name in TERMCAP */ 143 strncpy(pathbuf, cp, PBUFSIZ); /* still can be tokenized */ 144 145 /* For safety */ 146 if (issetugid()) 147 strcpy(pathbuf, _PATH_DEF_SEC); 148 149 pathbuf[PBUFSIZ - 1] = '\0'; 150 151 *fname++ = pathbuf; /* tokenize path into vector of names */ 152 while (*++p) 153 if (*p == ' ' || *p == ':') { 154 *p = '\0'; 155 while (*++p) 156 if (*p != ' ' && *p != ':') 157 break; 158 if (*p == '\0') 159 break; 160 *fname++ = p; 161 if (fname >= pathvec + PVECSIZ) { 162 fname--; 163 break; 164 } 165 } 166 *fname = (char *) 0; /* mark end of vector */ 167 if (cp && *cp && *cp != '/') 168 if (cgetset(cp) < 0) 169 return(-2); 170 171 i = cgetent(&dummy, pathvec, (char *)name); 172 173 if (i == 0) { 174 char *pd, *ps, *tok, *s, *tcs; 175 size_t len; 176 177 pd = _nc_termcap; 178 ps = dummy; 179 if ((tok = strchr(ps, ':')) == NULL) { 180 len = strlen(ps); 181 if (len >= TBUFSIZ) 182 i = -1; 183 else 184 strcpy(pd, ps); 185 goto done; 186 } 187 len = tok - ps + 1; 188 if (pd + len + 1 - _nc_termcap >= TBUFSIZ) { 189 i = -1; 190 goto done; 191 } 192 memcpy(pd, ps, len); 193 ps += len; 194 pd += len; 195 *pd = '\0'; 196 tcs = pd - 1; 197 for (;;) { 198 while ((tok = strsep(&ps, ":")) != NULL && 199 *(tok - 2) != '\\' && 200 (*tok == '\0' || *tok == '\\' || !isgraph(UChar(*tok)))) 201 ; 202 if (tok == NULL) 203 break; 204 for (s = tcs; s != NULL && s[1] != '\0'; 205 s = strchr(s, ':')) { 206 s++; 207 if (s[0] == tok[0] && s[1] == tok[1]) 208 goto skip_it; 209 } 210 len = strlen(tok); 211 if (pd + len + 1 - _nc_termcap >= TBUFSIZ) { 212 i = -1; 213 break; 214 } 215 memcpy(pd, tok, len); 216 pd += len; 217 *pd++ = ':'; 218 *pd = '\0'; 219 skip_it: ; 220 } 221 } 222done: 223 if (dummy) 224 free(dummy); 225 226 227/* 228 * From here on is ncurses-specific glue code 229 */ 230 231 if (i < 0) 232 return(TGETENT_ERR); 233 234 _nc_set_source("TERMCAP"); 235 _nc_read_entry_source((FILE *)NULL, _nc_termcap, FALSE, TRUE, NULLHOOK); 236 237 if (_nc_head == (ENTRY *)NULL) 238 return(TGETENT_ERR); 239 240 /* resolve all use references */ 241 _nc_resolve_uses2(TRUE, FALSE); 242 243 for_entry_list(ep) 244 if (_nc_name_match(ep->tterm.term_names, name, "|:")) 245 { 246 /* 247 * Make a local copy of the terminal capabilities, delinked 248 * from the list. 249 */ 250 memcpy(tp, &ep->tterm, sizeof(TERMTYPE)); 251 _nc_delink_entry(_nc_head, &(ep->tterm)); 252 free(ep); 253 _nc_free_entries(_nc_head); 254 _nc_head = _nc_tail = NULL; /* do not reuse! */ 255 256 return TGETENT_YES; /* OK */ 257 } 258 259 _nc_free_entries(_nc_head); 260 _nc_head = _nc_tail = NULL; /* do not reuse! */ 261 return(TGETENT_NO); /* not found */ 262} 263