]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/vgrind/vgrindefs.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / vgrind / vgrindefs.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35
36 __FBSDID("$FreeBSD$");
37
38 #define BUFSIZ  1024
39 #define MAXHOP  32      /* max number of tc= indirections */
40
41 #include <ctype.h>
42 #include <unistd.h>
43
44 /*
45  * grindcap - routines for dealing with the language definitions data base
46  *      (code stolen almost totally from termcap)
47  *
48  * BUG:         Should use a "last" pointer in tbuf, so that searching
49  *              for capabilities alphabetically would not be a n**2/2
50  *              process when large numbers of capabilities are given.
51  * Note:        If we add a last pointer now we will screw up the
52  *              tc capability. We really should compile termcap.
53  *
54  * Essentially all the work here is scanning and decoding escapes
55  * in string capabilities.  We don't use stdio because the editor
56  * doesn't, and because living w/o it is not hard.
57  */
58
59 static  char *tbuf;
60 static  char *filename;
61 static  int hopcount;   /* detect infinite loops in termcap, init 0 */
62 char    *tskip();
63 char    *tgetstr();
64 char    *tdecode();
65 char    *getenv();
66
67 /*
68  * Get an entry for terminal name in buffer bp,
69  * from the termcap file.  Parse is very rudimentary;
70  * we just notice escaped newlines.
71  */
72 tgetent(bp, name, file)
73         char *bp, *name, *file;
74 {
75         register char *cp;
76         register int c;
77         register int i = 0, cnt = 0;
78         char ibuf[BUFSIZ];
79         char *cp2;
80         int tf;
81
82         tbuf = bp;
83         tf = 0;
84         filename = file;
85         tf = open(filename, 0);
86         if (tf < 0)
87                 return (-1);
88         for (;;) {
89                 cp = bp;
90                 for (;;) {
91                         if (i == cnt) {
92                                 cnt = read(tf, ibuf, BUFSIZ);
93                                 if (cnt <= 0) {
94                                         close(tf);
95                                         return (0);
96                                 }
97                                 i = 0;
98                         }
99                         c = ibuf[i++];
100                         if (c == '\n') {
101                                 if (cp > bp && cp[-1] == '\\'){
102                                         cp--;
103                                         continue;
104                                 }
105                                 break;
106                         }
107                         if (cp >= bp+BUFSIZ) {
108                                 write(STDERR_FILENO, "Vgrind entry too long\n", 23);
109                                 break;
110                         } else
111                                 *cp++ = c;
112                 }
113                 *cp = 0;
114
115                 /*
116                  * The real work for the match.
117                  */
118                 if (tnamatch(name)) {
119                         close(tf);
120                         return(tnchktc());
121                 }
122         }
123 }
124
125 /*
126  * tnchktc: check the last entry, see if it's tc=xxx. If so,
127  * recursively find xxx and append that entry (minus the names)
128  * to take the place of the tc=xxx entry. This allows termcap
129  * entries to say "like an HP2621 but doesn't turn on the labels".
130  * Note that this works because of the left to right scan.
131  */
132 tnchktc()
133 {
134         register char *p, *q;
135         char tcname[16];        /* name of similar terminal */
136         char tcbuf[BUFSIZ];
137         char *holdtbuf = tbuf;
138         int l;
139
140         p = tbuf + strlen(tbuf) - 2;    /* before the last colon */
141         while (*--p != ':')
142                 if (p<tbuf) {
143                         write(STDERR_FILENO, "Bad vgrind entry\n", 18);
144                         return (0);
145                 }
146         p++;
147         /* p now points to beginning of last field */
148         if (p[0] != 't' || p[1] != 'c')
149                 return(1);
150         strcpy(tcname,p+3);
151         q = tcname;
152         while (q && *q != ':')
153                 q++;
154         *q = 0;
155         if (++hopcount > MAXHOP) {
156                 write(STDERR_FILENO, "Infinite tc= loop\n", 18);
157                 return (0);
158         }
159         if (tgetent(tcbuf, tcname, filename) != 1)
160                 return(0);
161         for (q=tcbuf; *q != ':'; q++)
162                 ;
163         l = p - holdtbuf + strlen(q);
164         if (l > BUFSIZ) {
165                 write(STDERR_FILENO, "Vgrind entry too long\n", 23);
166                 q[BUFSIZ - (p-tbuf)] = 0;
167         }
168         strcpy(p, q+1);
169         tbuf = holdtbuf;
170         return(1);
171 }
172
173 /*
174  * Tnamatch deals with name matching.  The first field of the termcap
175  * entry is a sequence of names separated by |'s, so we compare
176  * against each such name.  The normal : terminator after the last
177  * name (before the first field) stops us.
178  */
179 tnamatch(np)
180         char *np;
181 {
182         register char *Np, *Bp;
183
184         Bp = tbuf;
185         if (*Bp == '#')
186                 return(0);
187         for (;;) {
188                 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
189                         continue;
190                 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
191                         return (1);
192                 while (*Bp && *Bp != ':' && *Bp != '|')
193                         Bp++;
194                 if (*Bp == 0 || *Bp == ':')
195                         return (0);
196                 Bp++;
197         }
198 }
199
200 /*
201  * Skip to the next field.  Notice that this is very dumb, not
202  * knowing about \: escapes or any such.  If necessary, :'s can be put
203  * into the termcap file in octal.
204  */
205 static char *
206 tskip(bp)
207         register char *bp;
208 {
209
210         while (*bp && *bp != ':')
211                 bp++;
212         if (*bp == ':')
213                 bp++;
214         return (bp);
215 }
216
217 /*
218  * Return the (numeric) option id.
219  * Numeric options look like
220  *      li#80
221  * i.e. the option string is separated from the numeric value by
222  * a # character.  If the option is not found we return -1.
223  * Note that we handle octal numbers beginning with 0.
224  */
225 tgetnum(id)
226         char *id;
227 {
228         register int i, base;
229         register char *bp = tbuf;
230
231         for (;;) {
232                 bp = tskip(bp);
233                 if (*bp == 0)
234                         return (-1);
235                 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
236                         continue;
237                 if (*bp == '@')
238                         return(-1);
239                 if (*bp != '#')
240                         continue;
241                 bp++;
242                 base = 10;
243                 if (*bp == '0')
244                         base = 8;
245                 i = 0;
246                 while (isdigit(*bp))
247                         i *= base, i += *bp++ - '0';
248                 return (i);
249         }
250 }
251
252 /*
253  * Handle a flag option.
254  * Flag options are given "naked", i.e. followed by a : or the end
255  * of the buffer.  Return 1 if we find the option, or 0 if it is
256  * not given.
257  */
258 tgetflag(id)
259         char *id;
260 {
261         register char *bp = tbuf;
262
263         for (;;) {
264                 bp = tskip(bp);
265                 if (!*bp)
266                         return (0);
267                 if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
268                         if (!*bp || *bp == ':')
269                                 return (1);
270                         else if (*bp == '@')
271                                 return(0);
272                 }
273         }
274 }
275
276 /*
277  * Get a string valued option.
278  * These are given as
279  *      cl=^Z
280  * Much decoding is done on the strings, and the strings are
281  * placed in area, which is a ref parameter which is updated.
282  * No checking on area overflow.
283  */
284 char *
285 tgetstr(id, area)
286         char *id, **area;
287 {
288         register char *bp = tbuf;
289
290         for (;;) {
291                 bp = tskip(bp);
292                 if (!*bp)
293                         return (0);
294                 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
295                         continue;
296                 if (*bp == '@')
297                         return(0);
298                 if (*bp != '=')
299                         continue;
300                 bp++;
301                 return (tdecode(bp, area));
302         }
303 }
304
305 /*
306  * Tdecode does the grung work to decode the
307  * string capability escapes.
308  */
309 static char *
310 tdecode(str, area)
311         register char *str;
312         char **area;
313 {
314         register char *cp;
315         register int c;
316         int i;
317
318         cp = *area;
319         while (c = *str++) {
320             if (c == ':' && *(cp-1) != '\\')
321                 break;
322             *cp++ = c;
323         }
324         *cp++ = 0;
325         str = *area;
326         *area = cp;
327         return (str);
328 }