4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 /* Copyright (c) 1979 Regents of the University of California */
34 /* 1) remember the name of the first tc= parameter */
35 /* encountered during parsing. */
36 /* 2) handle multiple invocations of tgetent(). */
37 /* 3) tskip() is now available outside of the library. */
38 /* 4) remember $TERM name for error messages. */
39 /* 5) have a larger buffer. */
40 /* 6) really fix the bug that 5) got around. This fix by */
41 /* Marion Hakanson, orstcs!hakanson */
45 #define MAXHOP 32 /* max number of tc= indirections */
47 #include <sys/types.h>
57 #include <signal.h> /* use this file to determine if this is SVR4.0 system */
58 #ifdef SIGSTOP /* SVR4.0 and beyond */
59 #define E_TERMCAP "/usr/share/lib/termcap"
61 #define E_TERMCAP "/etc/termcap"
65 * termcap - routines for dealing with the terminal capability data base
67 * BUG: Should use a "last" pointer in tbuf, so that searching
68 * for capabilities alphabetically would not be a n**2/2
69 * process when large numbers of capabilities are given.
70 * Note: If we add a last pointer now we will screw up the
71 * tc capability. We really should compile termcap.
73 * Essentially all the work here is scanning and decoding escapes
74 * in string capabilities. We don't use stdio because the editor
75 * doesn't, and because living w/o it is not hard.
79 static int hopcount
; /* detect infinite loops in termcap, init 0 */
81 char *otgetstr(char *, char **);
86 static char *termname
;
88 static int _tgetent(char *, char *);
89 static int otnchktc(void);
90 static char *tdecode(char *, char **);
91 static int otnamatch(char *);
93 * Get an entry for terminal name in buffer bp,
94 * from the termcap file. Parse is very rudimentary;
95 * we just notice escaped newlines.
98 otgetent(char *bp
, char *name
)
105 ret
= _tgetent(bp
, name
);
107 * There is some sort of bug in the check way down below to prevent
108 * buffer overflow. I really don't want to track it down, so I
109 * upped the standard buffer size and check here to see if the created
110 * buffer is larger than the old buffer size.
112 if (strlen(bp
) >= 1024)
113 (void) fprintf(stderr
,
114 "tgetent(): TERM=%s: Termcap entry is too long.\n",
120 _tgetent(char *bp
, char *name
)
132 cp
= getenv("TERMCAP");
134 * TERMCAP can have one of two things in it. It can be the
135 * name of a file to use instead of /etc/termcap. In this
136 * case it better start with a "/". Or it can be an entry to
137 * use so we don't have to read the file. In this case it
138 * has to already have the newlines crunched out.
142 cp2
= getenv("TERM");
143 if (cp2
== (char *)0 || strcmp(name
, cp2
) == 0) {
144 (void) strcpy(bp
, cp
);
147 tf
= open(E_TERMCAP
, 0);
153 tf
= open(E_TERMCAP
, 0);
155 tf
= open(E_TERMCAP
, 0);
163 cnt
= read(tf
, ibuf
, TBUFSIZE
);
172 if (cp
> bp
&& cp
[-1] == '\\') {
178 if (cp
>= bp
+ TBUFSIZE
) {
179 (void) fprintf(stderr
, "tgetent(): TERM=%s: "
180 "Termcap entry too long\n", termname
);
188 * The real work for the match.
190 if (otnamatch(name
)) {
198 * otnchktc: check the last entry, see if it's tc=xxx. If so,
199 * recursively find xxx and append that entry (minus the names)
200 * to take the place of the tc=xxx entry. This allows termcap
201 * entries to say "like an HP2621 but doesn't turn on the labels".
202 * Note that this works because of the left to right scan.
208 #define TERMNAMESIZE 16
209 char tcname
[TERMNAMESIZE
]; /* name of similar terminal */
210 char tcbuf
[TBUFSIZE
];
211 char *holdtbuf
= tbuf
;
214 p
= tbuf
+ strlen(tbuf
) - 2; /* before the last colon */
217 (void) fprintf(stderr
, "tnchktc(): TERM=%s: Bad "
218 "termcap entry\n", termname
);
222 /* p now points to beginning of last field */
223 if (p
[0] != 't' || p
[1] != 'c')
225 (void) strncpy(tcname
, p
+ 3, TERMNAMESIZE
); /* TLH */
227 while (*q
&& *q
!= ':')
230 if (++hopcount
> MAXHOP
) {
231 (void) fprintf(stderr
, "tnchktc(): TERM=%s: Infinite tc= "
235 if (_tgetent(tcbuf
, tcname
) != 1)
239 (void) strcpy(TLHtcname
, tcname
);
241 for (q
= tcbuf
; *q
!= ':'; q
++)
243 l
= p
- holdtbuf
+ strlen(q
);
245 (void) fprintf(stderr
, "tnchktc(): TERM=%s: Termcap entry "
246 "too long\n", termname
);
247 q
[TBUFSIZE
- (p
- holdtbuf
)] = 0;
249 (void) strcpy(p
, q
+ 1);
255 * Tnamatch deals with name matching. The first field of the termcap
256 * entry is a sequence of names separated by |'s, so we compare
257 * against each such name. The normal : terminator after the last
258 * name (before the first field) stops us.
269 for (Np
= np
; *Np
&& *Bp
== *Np
; Bp
++, Np
++)
271 if (*Np
== 0 && (*Bp
== '|' || *Bp
== ':' || *Bp
== 0))
273 while (*Bp
&& *Bp
!= ':' && *Bp
!= '|')
275 if (*Bp
== 0 || *Bp
== ':')
282 * Skip to the next field. Notice that this is very dumb, not
283 * knowing about \: escapes or any such. If necessary, :'s can be put
284 * into the termcap file in octal.
290 while (*bp
&& *bp
!= ':')
298 * Return the (numeric) option id.
299 * Numeric options look like
301 * i.e. the option string is separated from the numeric value by
302 * a # character. If the option is not found we return -1.
303 * Note that we handle octal numbers beginning with 0.
315 if (*bp
++ != id
[0] || *bp
== 0 || *bp
++ != id
[1])
327 i
*= base
, i
+= *bp
++ - '0';
333 * Handle a flag option.
334 * Flag options are given "naked", i.e. followed by a : or the end
335 * of the buffer. Return 1 if we find the option, or 0 if it is
347 if (*bp
++ == id
[0] && *bp
!= 0 && *bp
++ == id
[1]) {
348 if (!*bp
|| *bp
== ':')
357 * Get a string valued option.
360 * Much decoding is done on the strings, and the strings are
361 * placed in area, which is a ref parameter which is updated.
362 * No checking on area overflow.
365 otgetstr(char *id
, char **area
)
373 if (*bp
++ != id
[0] || *bp
== 0 || *bp
++ != id
[1])
380 return (tdecode(bp
, area
));
385 * Tdecode does the grung work to decode the
386 * string capability escapes.
389 tdecode(char *str
, char **area
)
397 while ((c
= *str
++) != '\0' && c
!= ':') {
405 dp
= "E\033^^\\\\::n\nr\rt\tb\bf\f";
418 c
<<= 3, c
|= *str
++ - '0';
419 while (--i
&& isdigit(*str
))