Subversion Repositories planix.SVN

Rev

Rev 36 | Rev 39 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
36 7u83 1
/*
2
 * Copyright (c) 1980 The Regents of the University of California.
3
 * 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
 
37 7u83 34
/*
36 7u83 35
#ifndef lint
36
static char sccsid[] = "@(#)termcap.c	5.6 (Berkeley) 7/3/92";
37 7u83 37
#endif */  /* not lint */
36 7u83 38
 
39
#define	BUFSIZ		1024
40
#define MAXHOP		32	/* max number of tc= indirections */
41
#define	PBUFSIZ		512	/* max length of filename path */
42
#define	PVECSIZ		32	/* max number of names in path */
43
 
44
#include <ctype.h>
45
#include <stdlib.h>
46
#include "pathnames.h"
47
 
37 7u83 48
#include <string.h>
49
#include <unistd.h>
50
#include <fcntl.h>
51
 
52
 
53
int tnamatch();
54
int tnchktc();
55
int tfindent();
56
 
57
 
36 7u83 58
/*
59
 * termcap - routines for dealing with the terminal capability data base
60
 *
61
 * BUG:		Should use a "last" pointer in tbuf, so that searching
62
 *		for capabilities alphabetically would not be a n**2/2
63
 *		process when large numbers of capabilities are given.
64
 * Note:	If we add a last pointer now we will screw up the
65
 *		tc capability. We really should compile termcap.
66
 *
67
 * Essentially all the work here is scanning and decoding escapes
68
 * in string capabilities.  We don't use stdio because the editor
69
 * doesn't, and because living w/o it is not hard.
70
 */
71
 
72
static	char *tbuf;
73
static	int hopcount;	/* detect infinite loops in termcap, init 0 */
74
static	char pathbuf[PBUFSIZ];		/* holds raw path of filenames */
75
static	char *pathvec[PVECSIZ];		/* to point to names in pathbuf */
76
static	char **pvec;			/* holds usable tail of path vector */
77
 
78
static	char *tdecode __P((char *, char **));
79
static	char *tskip __P ((char *));
80
 
81
/*
82
 * Get an entry for terminal name in buffer bp from the termcap file.
83
 */
84
tgetent(bp, name)
85
	char *bp, *name;
86
{
87
	register char *p;
88
	register char *cp;
89
	register int c;
37 7u83 90
	char /* *term,*/ *home, *termpath;
36 7u83 91
	char **fname = pathvec;
92
 
93
	pvec = pathvec;
94
	tbuf = bp;
95
	p = pathbuf;
96
	cp = getenv("TERMCAP");
97
	/*
98
	 * TERMCAP can have one of two things in it. It can be the
99
	 * name of a file to use instead of /etc/termcap. In this
100
	 * case it better start with a "/". Or it can be an entry to
101
	 * use so we don't have to read the file. In this case it
102
	 * has to already have the newlines crunched out.  If TERMCAP
103
	 * does not hold a file name then a path of names is searched
104
	 * instead.  The path is found in the TERMPATH variable, or
105
	 * becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
106
	 */
107
	if (!cp || *cp != '/') {	/* no TERMCAP or it holds an entry */
37 7u83 108
		if ( (termpath = getenv("TERMPATH")) )
36 7u83 109
			strncpy(pathbuf, termpath, PBUFSIZ);
110
		else {
37 7u83 111
			if ( (home = getenv("HOME")) ) {	/* set up default */
36 7u83 112
				p += strlen(home);	/* path, looking in */
113
				strcpy(pathbuf, home);	/* $HOME first */
114
				*p++ = '/';
115
			}	/* if no $HOME look in current directory */
116
			strncpy(p, _PATH_DEF, PBUFSIZ - (p - pathbuf));
117
		}
118
	}
119
	else				/* user-defined name in TERMCAP */
120
		strncpy(pathbuf, cp, PBUFSIZ);	/* still can be tokenized */
121
 
122
	*fname++ = pathbuf;	/* tokenize path into vector of names */
123
	while (*++p)
124
		if (*p == ' ' || *p == ':') {
125
			*p = '\0';
126
			while (*++p)
127
				if (*p != ' ' && *p != ':')
128
					break;
129
			if (*p == '\0')
130
				break;
131
			*fname++ = p;
132
			if (fname >= pathvec + PVECSIZ) {
133
				fname--;
134
				break;
135
			}
136
		}
137
	*fname = (char *) 0;			/* mark end of vector */
138
	if (cp && *cp && *cp != '/') {
139
		tbuf = cp;
140
		c = tnamatch(name);
141
		tbuf = bp;
142
		if (c) {
143
			strcpy(bp,cp);
144
			return (tnchktc());
145
		}
146
	}
147
	return (tfindent(bp, name));	/* find terminal entry in path */
148
}
149
 
150
/*
151
 * tfindent - reads through the list of files in pathvec as if they were one
152
 * continuous file searching for terminal entries along the way.  It will
153
 * participate in indirect recursion if the call to tnchktc() finds a tc=
154
 * field, which is only searched for in the current file and files ocurring
155
 * after it in pathvec.  The usable part of this vector is kept in the global
156
 * variable pvec.  Terminal entries may not be broken across files.  Parse is
157
 * very rudimentary; we just notice escaped newlines.
158
 */
159
tfindent(bp, name)
160
	char *bp, *name;
161
{
162
	register char *cp;
163
	register int c;
164
	register int i, cnt;
165
	char ibuf[BUFSIZ];
166
	int opencnt = 0;
167
	int tf;
168
 
169
	tbuf = bp;
170
nextfile:
171
	i = cnt = 0;
172
	while (*pvec && (tf = open(*pvec, 0)) < 0)
173
		pvec++;
174
	if (!*pvec)
175
		return (opencnt ? 0 : -1);
176
	opencnt++;
177
	for (;;) {
178
		cp = bp;
179
		for (;;) {
180
			if (i == cnt) {
181
				cnt = read(tf, ibuf, BUFSIZ);
182
				if (cnt <= 0) {
183
					close(tf);
184
					pvec++;
185
					goto nextfile;
186
				}
187
				i = 0;
188
			}
189
			c = ibuf[i++];
190
			if (c == '\n') {
191
				if (cp > bp && cp[-1] == '\\'){
192
					cp--;
193
					continue;
194
				}
195
				break;
196
			}
197
			if (cp >= bp+BUFSIZ) {
198
				write(2,"Termcap entry too long\n", 23);
199
				break;
200
			} else
201
				*cp++ = c;
202
		}
203
		*cp = 0;
204
 
205
		/*
206
		 * The real work for the match.
207
		 */
208
		if (tnamatch(name)) {
209
			close(tf);
210
			return(tnchktc());
211
		}
212
	}
213
}
214
 
215
/*
216
 * tnchktc: check the last entry, see if it's tc=xxx. If so,
217
 * recursively find xxx and append that entry (minus the names)
218
 * to take the place of the tc=xxx entry. This allows termcap
219
 * entries to say "like an HP2621 but doesn't turn on the labels".
220
 * Note that this works because of the left to right scan.
221
 */
222
tnchktc()
223
{
224
	register char *p, *q;
225
	char tcname[16];	/* name of similar terminal */
226
	char tcbuf[BUFSIZ];
227
	char *holdtbuf = tbuf;
228
	int l;
229
 
230
	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
231
	while (*--p != ':')
232
		if (p<tbuf) {
233
			write(2, "Bad termcap entry\n", 18);
234
			return (0);
235
		}
236
	p++;
237
	/* p now points to beginning of last field */
238
	if (p[0] != 't' || p[1] != 'c')
239
		return(1);
240
	strcpy(tcname,p+3);
241
	q = tcname;
242
	while (*q && *q != ':')
243
		q++;
244
	*q = 0;
245
	if (++hopcount > MAXHOP) {
246
		write(2, "Infinite tc= loop\n", 18);
247
		return (0);
248
	}
249
	if (tfindent(tcbuf, tcname) != 1) {
250
		hopcount = 0;		/* unwind recursion */
251
		return(0);
252
	}
253
	for (q=tcbuf; *q != ':'; q++)
254
		;
255
	l = p - holdtbuf + strlen(q);
256
	if (l > BUFSIZ) {
257
		write(2, "Termcap entry too long\n", 23);
258
		q[BUFSIZ - (p-tbuf)] = 0;
259
	}
260
	strcpy(p, q+1);
261
	tbuf = holdtbuf;
262
	hopcount = 0;			/* unwind recursion */
263
	return(1);
264
}
265
 
266
/*
267
 * Tnamatch deals with name matching.  The first field of the termcap
268
 * entry is a sequence of names separated by |'s, so we compare
269
 * against each such name.  The normal : terminator after the last
270
 * name (before the first field) stops us.
271
 */
272
tnamatch(np)
273
	char *np;
274
{
275
	register char *Np, *Bp;
276
 
277
	Bp = tbuf;
278
	if (*Bp == '#')
279
		return(0);
280
	for (;;) {
281
		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
282
			continue;
283
		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
284
			return (1);
285
		while (*Bp && *Bp != ':' && *Bp != '|')
286
			Bp++;
287
		if (*Bp == 0 || *Bp == ':')
288
			return (0);
289
		Bp++;
290
	}
291
}
292
 
293
/*
294
 * Skip to the next field.  Notice that this is very dumb, not
295
 * knowing about \: escapes or any such.  If necessary, :'s can be put
296
 * into the termcap file in octal.
297
 */
298
static char *
299
tskip(bp)
300
	register char *bp;
301
{
302
 
303
	while (*bp && *bp != ':')
304
		bp++;
305
	if (*bp == ':')
306
		bp++;
307
	return (bp);
308
}
309
 
310
/*
311
 * Return the (numeric) option id.
312
 * Numeric options look like
313
 *	li#80
314
 * i.e. the option string is separated from the numeric value by
315
 * a # character.  If the option is not found we return -1.
316
 * Note that we handle octal numbers beginning with 0.
317
 */
318
tgetnum(id)
319
	char *id;
320
{
321
	register int i, base;
322
	register char *bp = tbuf;
323
 
324
	for (;;) {
325
		bp = tskip(bp);
326
		if (*bp == 0)
327
			return (-1);
328
		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
329
			continue;
330
		if (*bp == '@')
331
			return(-1);
332
		if (*bp != '#')
333
			continue;
334
		bp++;
335
		base = 10;
336
		if (*bp == '0')
337
			base = 8;
338
		i = 0;
339
		while (isdigit(*bp))
340
			i *= base, i += *bp++ - '0';
341
		return (i);
342
	}
343
}
344
 
345
/*
346
 * Handle a flag option.
347
 * Flag options are given "naked", i.e. followed by a : or the end
348
 * of the buffer.  Return 1 if we find the option, or 0 if it is
349
 * not given.
350
 */
351
tgetflag(id)
352
	char *id;
353
{
354
	register char *bp = tbuf;
355
 
356
	for (;;) {
357
		bp = tskip(bp);
358
		if (!*bp)
359
			return (0);
360
		if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
361
			if (!*bp || *bp == ':')
362
				return (1);
363
			else if (*bp == '@')
364
				return(0);
365
		}
366
	}
367
}
368
 
369
/*
370
 * Get a string valued option.
371
 * These are given as
372
 *	cl=^Z
373
 * Much decoding is done on the strings, and the strings are
374
 * placed in area, which is a ref parameter which is updated.
375
 * No checking on area overflow.
376
 */
377
char *
378
tgetstr(id, area)
379
	char *id, **area;
380
{
381
	register char *bp = tbuf;
382
 
383
	for (;;) {
384
		bp = tskip(bp);
385
		if (!*bp)
386
			return (0);
387
		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
388
			continue;
389
		if (*bp == '@')
390
			return(0);
391
		if (*bp != '=')
392
			continue;
393
		bp++;
394
		return (tdecode(bp, area));
395
	}
396
}
397
 
398
/*
399
 * Tdecode does the grung work to decode the
400
 * string capability escapes.
401
 */
402
static char *
403
tdecode(str, area)
404
	register char *str;
405
	char **area;
406
{
407
	register char *cp;
408
	register int c;
409
	register char *dp;
410
	int i;
411
 
412
	cp = *area;
413
	while ((c = *str++) && c != ':') {
414
		switch (c) {
415
 
416
		case '^':
417
			c = *str++ & 037;
418
			break;
419
 
420
		case '\\':
421
			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
422
			c = *str++;
423
nextc:
424
			if (*dp++ == c) {
425
				c = *dp++;
426
				break;
427
			}
428
			dp++;
429
			if (*dp)
430
				goto nextc;
431
			if (isdigit(c)) {
432
				c -= '0', i = 2;
433
				do
434
					c <<= 3, c |= *str++ - '0';
435
				while (--i && isdigit(*str));
436
			}
437
			break;
438
		}
439
		*cp++ = c;
440
	}
441
	*cp++ = 0;
442
	str = *area;
443
	*area = cp;
444
	return (str);
445
}