2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)vgrindefs.c 8.1 (Berkeley) 6/6/93
35 * $DragonFly: src/usr.bin/vgrind/vgrindefs.c,v 1.5 2008/10/16 01:52:34 swildner Exp $
39 #define MAXHOP 32 /* max number of tc= indirections */
43 * grindcap - routines for dealing with the language definitions data base
44 * (code stolen almost totally from termcap)
46 * BUG: Should use a "last" pointer in tbuf, so that searching
47 * for capabilities alphabetically would not be a n**2/2
48 * process when large numbers of capabilities are given.
49 * Note: If we add a last pointer now we will screw up the
50 * tc capability. We really should compile termcap.
52 * Essentially all the work here is scanning and decoding escapes
53 * in string capabilities. We don't use stdio because the editor
54 * doesn't, and because living w/o it is not hard.
58 static char *filename
;
59 static int hopcount
; /* detect infinite loops in termcap, init 0 */
65 * Get an entry for terminal name in buffer bp,
66 * from the termcap file. Parse is very rudimentary;
67 * we just notice escaped newlines.
70 tgetent(char *bp
, char *name
, char *file
)
82 tf
= open(filename
, 0);
89 cnt
= read(tf
, ibuf
, BUFSIZ
);
98 if (cp
> bp
&& cp
[-1] == '\\'){
104 if (cp
>= bp
+BUFSIZ
) {
105 write(2,"Vgrind entry too long\n", 23);
113 * The real work for the match.
115 if (tnamatch(name
)) {
123 * tnchktc: check the last entry, see if it's tc=xxx. If so,
124 * recursively find xxx and append that entry (minus the names)
125 * to take the place of the tc=xxx entry. This allows termcap
126 * entries to say "like an HP2621 but doesn't turn on the labels".
127 * Note that this works because of the left to right scan.
133 char tcname
[16]; /* name of similar terminal */
135 char *holdtbuf
= tbuf
;
138 p
= tbuf
+ strlen(tbuf
) - 2; /* before the last colon */
141 write(2, "Bad vgrind entry\n", 18);
145 /* p now points to beginning of last field */
146 if (p
[0] != 't' || p
[1] != 'c')
150 while (q
&& *q
!= ':')
153 if (++hopcount
> MAXHOP
) {
154 write(2, "Infinite tc= loop\n", 18);
157 if (tgetent(tcbuf
, tcname
, filename
) != 1)
159 for (q
=tcbuf
; *q
!= ':'; q
++)
161 l
= p
- holdtbuf
+ strlen(q
);
163 write(2, "Vgrind entry too long\n", 23);
164 q
[BUFSIZ
- (p
-tbuf
)] = 0;
172 * Tnamatch deals with name matching. The first field of the termcap
173 * entry is a sequence of names separated by |'s, so we compare
174 * against each such name. The normal : terminator after the last
175 * name (before the first field) stops us.
186 for (Np
= np
; *Np
&& *Bp
== *Np
; Bp
++, Np
++)
188 if (*Np
== 0 && (*Bp
== '|' || *Bp
== ':' || *Bp
== 0))
190 while (*Bp
&& *Bp
!= ':' && *Bp
!= '|')
192 if (*Bp
== 0 || *Bp
== ':')
199 * Skip to the next field. Notice that this is very dumb, not
200 * knowing about \: escapes or any such. If necessary, :'s can be put
201 * into the termcap file in octal.
207 while (*bp
&& *bp
!= ':')
215 * Return the (numeric) option id.
216 * Numeric options look like
218 * i.e. the option string is separated from the numeric value by
219 * a # character. If the option is not found we return -1.
220 * Note that we handle octal numbers beginning with 0.
232 if (*bp
++ != id
[0] || *bp
== 0 || *bp
++ != id
[1])
244 i
*= base
, i
+= *bp
++ - '0';
250 * Handle a flag option.
251 * Flag options are given "naked", i.e. followed by a : or the end
252 * of the buffer. Return 1 if we find the option, or 0 if it is
264 if (*bp
++ == id
[0] && *bp
!= 0 && *bp
++ == id
[1]) {
265 if (!*bp
|| *bp
== ':')
274 * Get a string valued option.
277 * Much decoding is done on the strings, and the strings are
278 * placed in area, which is a ref parameter which is updated.
279 * No checking on area overflow.
282 tgetstr(char *id
, char **area
)
290 if (*bp
++ != id
[0] || *bp
== 0 || *bp
++ != id
[1])
297 return (tdecode(bp
, area
));
302 * Tdecode does the grung work to decode the
303 * string capability escapes.
306 tdecode(char *str
, char **area
)
314 if (c
== ':' && *(cp
-1) != '\\')