Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.bin / vgrind / vgrindefs.c
blob31865b5fb0f6a5a92de903ee7be56498498bbe80
1 /* $NetBSD: vgrindefs.c,v 1.9 2003/07/14 09:25:37 itojun Exp $ */
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)vgrindefs.c 8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: vgrindefs.c,v 1.9 2003/07/14 09:25:37 itojun Exp $");
38 #endif /* not lint */
40 #define BUFSIZ 1024
41 #define MAXHOP 32 /* max number of tc= indirections */
43 #include <stdlib.h>
44 #include <string.h>
45 #include <ctype.h>
46 #include <fcntl.h>
47 #include <unistd.h>
49 * grindcap - routines for dealing with the language definitions data base
50 * (code stolen almost totally from termcap)
52 * BUG: Should use a "last" pointer in tbuf, so that searching
53 * for capabilities alphabetically would not be a n**2/2
54 * process when large numbers of capabilities are given.
55 * Note: If we add a last pointer now we will screw up the
56 * tc capability. We really should compile termcap.
58 * Essentially all the work here is scanning and decoding escapes
59 * in string capabilities. We don't use stdio because the editor
60 * doesn't, and because living w/o it is not hard.
63 static char *tbuf;
64 static char *filename;
65 static int hopcount; /* detect infinite loops in termcap, init 0 */
66 static char *tskip();
67 static char *tdecode();
68 char *tgetstr();
70 int tnamatch __P((char *np));
71 int tnchktc __P((void));
74 * Get an entry for terminal name in buffer bp,
75 * from the termcap file. Parse is very rudimentary;
76 * we just notice escaped newlines.
78 int
79 tgetent(bp, name, file)
80 char *bp, *name, *file;
82 char *cp;
83 int c;
84 int i = 0, cnt = 0;
85 char ibuf[BUFSIZ];
86 int tf;
88 tbuf = bp;
89 tf = 0;
90 filename = file;
91 tf = open(filename, 0);
92 if (tf < 0)
93 return (-1);
94 for (;;) {
95 cp = bp;
96 for (;;) {
97 if (i == cnt) {
98 cnt = read(tf, ibuf, BUFSIZ);
99 if (cnt <= 0) {
100 close(tf);
101 return (0);
103 i = 0;
105 c = ibuf[i++];
106 if (c == '\n') {
107 if (cp > bp && cp[-1] == '\\'){
108 cp--;
109 continue;
111 break;
113 if (cp >= bp+BUFSIZ) {
114 write(2,"Vgrind entry too long\n", 23);
115 break;
116 } else
117 *cp++ = c;
119 *cp = 0;
122 * The real work for the match.
124 if (tnamatch(name)) {
125 close(tf);
126 return(tnchktc());
132 * tnchktc: check the last entry, see if it's tc=xxx. If so,
133 * recursively find xxx and append that entry (minus the names)
134 * to take the place of the tc=xxx entry. This allows termcap
135 * entries to say "like an HP2621 but doesn't turn on the labels".
136 * Note that this works because of the left to right scan.
139 tnchktc()
141 char *p, *q;
142 char tcname[16]; /* name of similar terminal */
143 char tcbuf[BUFSIZ];
144 char *holdtbuf = tbuf;
145 int l;
147 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
148 while (*--p != ':')
149 if (p<tbuf) {
150 write(2, "Bad vgrind entry\n", 18);
151 return (0);
153 p++;
154 /* p now points to beginning of last field */
155 if (p[0] != 't' || p[1] != 'c')
156 return(1);
157 strlcpy(tcname, p + 3, sizeof(tcname));
158 q = tcname;
159 while (q && *q != ':')
160 q++;
161 *q = 0;
162 if (++hopcount > MAXHOP) {
163 write(2, "Infinite tc= loop\n", 18);
164 return (0);
166 if (tgetent(tcbuf, tcname, filename) != 1)
167 return(0);
168 for (q = tcbuf; *q != ':'; q++)
170 l = p - holdtbuf + strlen(q);
171 if (l > BUFSIZ) {
172 write(2, "Vgrind entry too long\n", 23);
173 q[BUFSIZ - (p - tbuf)] = 0;
175 strcpy(p, q + 1);
176 tbuf = holdtbuf;
177 return(1);
181 * Tnamatch deals with name matching. The first field of the termcap
182 * entry is a sequence of names separated by |'s, so we compare
183 * against each such name. The normal : terminator after the last
184 * name (before the first field) stops us.
187 tnamatch(np)
188 char *np;
190 char *Np, *Bp;
192 Bp = tbuf;
193 if (*Bp == '#')
194 return(0);
195 for (;;) {
196 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
197 continue;
198 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
199 return (1);
200 while (*Bp && *Bp != ':' && *Bp != '|')
201 Bp++;
202 if (*Bp == 0 || *Bp == ':')
203 return (0);
204 Bp++;
209 * Skip to the next field. Notice that this is very dumb, not
210 * knowing about \: escapes or any such. If necessary, :'s can be put
211 * into the termcap file in octal.
213 static char *
214 tskip(bp)
215 char *bp;
218 while (*bp && *bp != ':')
219 bp++;
220 if (*bp == ':')
221 bp++;
222 return (bp);
226 * Return the (numeric) option id.
227 * Numeric options look like
228 * li#80
229 * i.e. the option string is separated from the numeric value by
230 * a # character. If the option is not found we return -1.
231 * Note that we handle octal numbers beginning with 0.
234 tgetnum(id)
235 char *id;
237 int i, base;
238 char *bp = tbuf;
240 for (;;) {
241 bp = tskip(bp);
242 if (*bp == 0)
243 return (-1);
244 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
245 continue;
246 if (*bp == '@')
247 return(-1);
248 if (*bp != '#')
249 continue;
250 bp++;
251 base = 10;
252 if (*bp == '0')
253 base = 8;
254 i = 0;
255 while (isdigit(*bp))
256 i *= base, i += *bp++ - '0';
257 return (i);
262 * Handle a flag option.
263 * Flag options are given "naked", i.e. followed by a : or the end
264 * of the buffer. Return 1 if we find the option, or 0 if it is
265 * not given.
268 tgetflag(id)
269 char *id;
271 char *bp = tbuf;
273 for (;;) {
274 bp = tskip(bp);
275 if (!*bp)
276 return (0);
277 if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
278 if (!*bp || *bp == ':')
279 return (1);
280 else if (*bp == '@')
281 return(0);
287 * Get a string valued option.
288 * These are given as
289 * cl=^Z
290 * Much decoding is done on the strings, and the strings are
291 * placed in area, which is a ref parameter which is updated.
292 * No checking on area overflow.
294 char *
295 tgetstr(id, area)
296 char *id, **area;
298 char *bp = tbuf;
300 for (;;) {
301 bp = tskip(bp);
302 if (!*bp)
303 return (0);
304 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
305 continue;
306 if (*bp == '@')
307 return(0);
308 if (*bp != '=')
309 continue;
310 bp++;
311 return (tdecode(bp, area));
316 * Tdecode does the grung work to decode the
317 * string capability escapes.
319 static char *
320 tdecode(str, area)
321 char *str;
322 char **area;
324 char *cp;
325 int c;
327 cp = *area;
328 while (c = *str++) {
329 if (c == ':' && *(cp-1) != '\\')
330 break;
331 *cp++ = c;
333 *cp++ = 0;
334 str = *area;
335 *area = cp;
336 return (str);