Subversion Repositories planix.SVN

Rev

Rev 39 | 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
 *
104 7u83 5
 * Copyright (c) 2019 The PLANIX Project
6
 * All right reserved
7
 *
36 7u83 8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. All advertising materials mentioning features or use of this software
17
 *    must display the following acknowledgement:
18
 *	This product includes software developed by the University of
19
 *	California, Berkeley and its contributors.
20
 * 4. Neither the name of the University nor the names of its contributors
21
 *    may be used to endorse or promote products derived from this software
22
 *    without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34
 * SUCH DAMAGE.
35
 */
36
 
37
 
104 7u83 38
#include <stdio.h>
39
 
40
#define	BUFSIZE		1024
36 7u83 41
#define MAXHOP		32	/* max number of tc= indirections */
104 7u83 42
#define	PBUFSIZE		512	/* max length of filename path */
36 7u83 43
#define	PVECSIZ		32	/* max number of names in path */
44
 
45
#include <ctype.h>
46
#include <stdlib.h>
47
#include "pathnames.h"
48
 
37 7u83 49
#include <string.h>
50
#include <unistd.h>
51
#include <fcntl.h>
52
 
53
 
54
int tnamatch();
55
int tnchktc();
56
int tfindent();
57
 
58
 
36 7u83 59
/*
60
 * termcap - routines for dealing with the terminal capability data base
61
 *
62
 * BUG:		Should use a "last" pointer in tbuf, so that searching
63
 *		for capabilities alphabetically would not be a n**2/2
64
 *		process when large numbers of capabilities are given.
65
 * Note:	If we add a last pointer now we will screw up the
66
 *		tc capability. We really should compile termcap.
67
 *
68
 * Essentially all the work here is scanning and decoding escapes
69
 * in string capabilities.  We don't use stdio because the editor
70
 * doesn't, and because living w/o it is not hard.
71
 */
72
 
73
static	char *tbuf;
74
static	int hopcount;	/* detect infinite loops in termcap, init 0 */
104 7u83 75
static	char pathbuf[PBUFSIZE];		/* holds raw path of filenames */
36 7u83 76
static	char *pathvec[PVECSIZ];		/* to point to names in pathbuf */
77
static	char **pvec;			/* holds usable tail of path vector */
78
 
39 7u83 79
static	char *tdecode ();
80
static	char *tskip ();
36 7u83 81
 
82
/*
83
 * Get an entry for terminal name in buffer bp from the termcap file.
84
 */
85
tgetent(bp, name)
86
	char *bp, *name;
87
{
88
	register char *p;
89
	register char *cp;
90
	register int c;
37 7u83 91
	char /* *term,*/ *home, *termpath;
36 7u83 92
	char **fname = pathvec;
93
 
94
	pvec = pathvec;
95
	tbuf = bp;
96
	p = pathbuf;
97
	cp = getenv("TERMCAP");
98
	/*
99
	 * TERMCAP can have one of two things in it. It can be the
100
	 * name of a file to use instead of /etc/termcap. In this
101
	 * case it better start with a "/". Or it can be an entry to
102
	 * use so we don't have to read the file. In this case it
103
	 * has to already have the newlines crunched out.  If TERMCAP
104
	 * does not hold a file name then a path of names is searched
105
	 * instead.  The path is found in the TERMPATH variable, or
106
	 * becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
107
	 */
108
	if (!cp || *cp != '/') {	/* no TERMCAP or it holds an entry */
37 7u83 109
		if ( (termpath = getenv("TERMPATH")) )
104 7u83 110
			strncpy(pathbuf, termpath, PBUFSIZE);
36 7u83 111
		else {
37 7u83 112
			if ( (home = getenv("HOME")) ) {	/* set up default */
36 7u83 113
				p += strlen(home);	/* path, looking in */
114
				strcpy(pathbuf, home);	/* $HOME first */
115
				*p++ = '/';
116
			}	/* if no $HOME look in current directory */
104 7u83 117
			strncpy(p, _PATH_DEF, PBUFSIZE - (p - pathbuf));
36 7u83 118
		}
119
	}
120
	else				/* user-defined name in TERMCAP */
104 7u83 121
		strncpy(pathbuf, cp, PBUFSIZE);	/* still can be tokenized */
36 7u83 122
 
123
	*fname++ = pathbuf;	/* tokenize path into vector of names */
124
	while (*++p)
125
		if (*p == ' ' || *p == ':') {
126
			*p = '\0';
127
			while (*++p)
128
				if (*p != ' ' && *p != ':')
129
					break;
130
			if (*p == '\0')
131
				break;
132
			*fname++ = p;
133
			if (fname >= pathvec + PVECSIZ) {
134
				fname--;
135
				break;
136
			}
137
		}
138
	*fname = (char *) 0;			/* mark end of vector */
139
	if (cp && *cp && *cp != '/') {
140
		tbuf = cp;
141
		c = tnamatch(name);
142
		tbuf = bp;
143
		if (c) {
144
			strcpy(bp,cp);
145
			return (tnchktc());
146
		}
147
	}
148
	return (tfindent(bp, name));	/* find terminal entry in path */
149
}
150
 
151
/*
152
 * tfindent - reads through the list of files in pathvec as if they were one
153
 * continuous file searching for terminal entries along the way.  It will
154
 * participate in indirect recursion if the call to tnchktc() finds a tc=
155
 * field, which is only searched for in the current file and files ocurring
156
 * after it in pathvec.  The usable part of this vector is kept in the global
157
 * variable pvec.  Terminal entries may not be broken across files.  Parse is
158
 * very rudimentary; we just notice escaped newlines.
159
 */
160
tfindent(bp, name)
161
	char *bp, *name;
162
{
163
	register char *cp;
164
	register int c;
165
	register int i, cnt;
104 7u83 166
	char ibuf[BUFSIZE];
36 7u83 167
	int opencnt = 0;
168
	int tf;
169
 
170
	tbuf = bp;
171
nextfile:
172
	i = cnt = 0;
173
	while (*pvec && (tf = open(*pvec, 0)) < 0)
174
		pvec++;
175
	if (!*pvec)
176
		return (opencnt ? 0 : -1);
177
	opencnt++;
178
	for (;;) {
179
		cp = bp;
180
		for (;;) {
181
			if (i == cnt) {
104 7u83 182
				cnt = read(tf, ibuf, BUFSIZE);
36 7u83 183
				if (cnt <= 0) {
184
					close(tf);
185
					pvec++;
186
					goto nextfile;
187
				}
188
				i = 0;
189
			}
190
			c = ibuf[i++];
191
			if (c == '\n') {
192
				if (cp > bp && cp[-1] == '\\'){
193
					cp--;
194
					continue;
195
				}
196
				break;
197
			}
104 7u83 198
			if (cp >= bp+BUFSIZE) {
199
				fprintf(stderr,"Termcap entry too long\n");
36 7u83 200
				break;
201
			} else
202
				*cp++ = c;
203
		}
204
		*cp = 0;
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 */
104 7u83 226
	char tcbuf[BUFSIZE];
36 7u83 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) {
104 7u83 233
			fprintf(stderr, "Bad termcap entry\n");
36 7u83 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) {
104 7u83 246
		fprintf(stderr, "Infinite tc= loop\n");
36 7u83 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);
104 7u83 256
	if (l > BUFSIZE) {
257
		fprintf(stderr, "Termcap entry too long!\n");
258
		q[BUFSIZE - (p-tbuf)] = 0;
36 7u83 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
}