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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 #define MAXHOP 32 /* max number of tc= indirections */
41 * grindcap - routines for dealing with the language definitions data base
42 * (code stolen almost totally from termcap)
44 * BUG: Should use a "last" pointer in tbuf, so that searching
45 * for capabilities alphabetically would not be a n**2/2
46 * process when large numbers of capabilities are given.
47 * Note: If we add a last pointer now we will screw up the
48 * tc capability. We really should compile termcap.
50 * Essentially all the work here is scanning and decoding escapes
51 * in string capabilities. We don't use stdio because the editor
52 * doesn't, and because living w/o it is not hard.
56 static char *filename;
57 static int hopcount; /* detect infinite loops in termcap, init 0 */
64 * Get an entry for terminal name in buffer bp,
65 * from the termcap file. Parse is very rudimentary;
66 * we just notice escaped newlines.
68 tgetent(bp, name, file)
69 char *bp, *name, *file;
73 register int i = 0, cnt = 0;
81 tf = open(filename, 0);
88 cnt = read(tf, ibuf, BUFSIZ);
97 if (cp > bp && cp[-1] == '\\'){
103 if (cp >= bp+BUFSIZ) {
104 write(STDERR_FILENO, "Vgrind entry too long\n", 23);
112 * The real work for the match.
114 if (tnamatch(name)) {
122 * tnchktc: check the last entry, see if it's tc=xxx. If so,
123 * recursively find xxx and append that entry (minus the names)
124 * to take the place of the tc=xxx entry. This allows termcap
125 * entries to say "like an HP2621 but doesn't turn on the labels".
126 * Note that this works because of the left to right scan.
130 register char *p, *q;
131 char tcname[16]; /* name of similar terminal */
133 char *holdtbuf = tbuf;
136 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
139 write(STDERR_FILENO, "Bad vgrind entry\n", 18);
143 /* p now points to beginning of last field */
144 if (p[0] != 't' || p[1] != 'c')
148 while (q && *q != ':')
151 if (++hopcount > MAXHOP) {
152 write(STDERR_FILENO, "Infinite tc= loop\n", 18);
155 if (tgetent(tcbuf, tcname, filename) != 1)
157 for (q=tcbuf; *q != ':'; q++)
159 l = p - holdtbuf + strlen(q);
161 write(STDERR_FILENO, "Vgrind entry too long\n", 23);
162 q[BUFSIZ - (p-tbuf)] = 0;
170 * Tnamatch deals with name matching. The first field of the termcap
171 * entry is a sequence of names separated by |'s, so we compare
172 * against each such name. The normal : terminator after the last
173 * name (before the first field) stops us.
178 register char *Np, *Bp;
184 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
186 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
188 while (*Bp && *Bp != ':' && *Bp != '|')
190 if (*Bp == 0 || *Bp == ':')
197 * Skip to the next field. Notice that this is very dumb, not
198 * knowing about \: escapes or any such. If necessary, :'s can be put
199 * into the termcap file in octal.
206 while (*bp && *bp != ':')
214 * Return the (numeric) option id.
215 * Numeric options look like
217 * i.e. the option string is separated from the numeric value by
218 * a # character. If the option is not found we return -1.
219 * Note that we handle octal numbers beginning with 0.
224 register int i, base;
225 register char *bp = tbuf;
231 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
243 i *= base, i += *bp++ - '0';
249 * Handle a flag option.
250 * Flag options are given "naked", i.e. followed by a : or the end
251 * of the buffer. Return 1 if we find the option, or 0 if it is
257 register char *bp = tbuf;
263 if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
264 if (!*bp || *bp == ':')
273 * Get a string valued option.
276 * Much decoding is done on the strings, and the strings are
277 * placed in area, which is a ref parameter which is updated.
278 * No checking on area overflow.
284 register char *bp = tbuf;
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.
316 if (c == ':' && *(cp-1) != '\\')