Subversion Repositories planix.SVN

Rev

Rev 39 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 39 Rev 104
Line 2... Line 2...
2
 * Copyright (c) 1980 The Regents of the University of California.
2
 * Copyright (c) 1980 The Regents of the University of California.
3
 * All rights reserved.
3
 * All rights reserved.
-
 
4
 *
-
 
5
 * Copyright (c) 2019 The PLANIX Project
-
 
6
 * All right reserved
4
 *
7
 *
5
 * Redistribution and use in source and binary forms, with or without
8
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
9
 * modification, are permitted provided that the following conditions
7
 * are met:
10
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
11
 * 1. Redistributions of source code must retain the above copyright
Line 29... Line 32...
29
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * 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
33
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * SUCH DAMAGE.
34
 * SUCH DAMAGE.
32
 */
35
 */
33
 
36
 
34
/*
-
 
35
#ifndef lint
-
 
36
static char sccsid[] = "@(#)termcap.c	5.6 (Berkeley) 7/3/92";
-
 
37
#endif */  /* not lint */
-
 
38
 
37
 
-
 
38
#include <stdio.h>
-
 
39
 
39
#define	BUFSIZ		1024
40
#define	BUFSIZE		1024
40
#define MAXHOP		32	/* max number of tc= indirections */
41
#define MAXHOP		32	/* max number of tc= indirections */
41
#define	PBUFSIZ		512	/* max length of filename path */
42
#define	PBUFSIZE		512	/* max length of filename path */
42
#define	PVECSIZ		32	/* max number of names in path */
43
#define	PVECSIZ		32	/* max number of names in path */
43
 
44
 
44
#include <ctype.h>
45
#include <ctype.h>
45
#include <stdlib.h>
46
#include <stdlib.h>
46
#include "pathnames.h"
47
#include "pathnames.h"
47
 
48
 
48
#include <string.h>
49
#include <string.h>
49
#include <unistd.h>
50
#include <unistd.h>
50
#include <fcntl.h>
51
#include <fcntl.h>
51
 
52
 
52
 
53
 
53
int tnamatch();
54
int tnamatch();
54
int tnchktc();
55
int tnchktc();
55
int tfindent();
56
int tfindent();
56
 
57
 
57
 
58
 
58
/*
59
/*
59
 * termcap - routines for dealing with the terminal capability data base
60
 * termcap - routines for dealing with the terminal capability data base
60
 *
61
 *
61
 * BUG:		Should use a "last" pointer in tbuf, so that searching
62
 * BUG:		Should use a "last" pointer in tbuf, so that searching
62
 *		for capabilities alphabetically would not be a n**2/2
63
 *		for capabilities alphabetically would not be a n**2/2
Line 69... Line 70...
69
 * doesn't, and because living w/o it is not hard.
70
 * doesn't, and because living w/o it is not hard.
70
 */
71
 */
71
 
72
 
72
static	char *tbuf;
73
static	char *tbuf;
73
static	int hopcount;	/* detect infinite loops in termcap, init 0 */
74
static	int hopcount;	/* detect infinite loops in termcap, init 0 */
74
static	char pathbuf[PBUFSIZ];		/* holds raw path of filenames */
75
static	char pathbuf[PBUFSIZE];		/* holds raw path of filenames */
75
static	char *pathvec[PVECSIZ];		/* to point to names in pathbuf */
76
static	char *pathvec[PVECSIZ];		/* to point to names in pathbuf */
76
static	char **pvec;			/* holds usable tail of path vector */
77
static	char **pvec;			/* holds usable tail of path vector */
77
 
78
 
78
static	char *tdecode ();
79
static	char *tdecode ();
79
static	char *tskip ();
80
static	char *tskip ();
80
 
81
 
81
/*
82
/*
82
 * Get an entry for terminal name in buffer bp from the termcap file.
83
 * Get an entry for terminal name in buffer bp from the termcap file.
83
 */
84
 */
84
tgetent(bp, name)
85
tgetent(bp, name)
85
	char *bp, *name;
86
	char *bp, *name;
86
{
87
{
Line 104... Line 105...
104
	 * instead.  The path is found in the TERMPATH variable, or
105
	 * instead.  The path is found in the TERMPATH variable, or
105
	 * becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
106
	 * becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
106
	 */
107
	 */
107
	if (!cp || *cp != '/') {	/* no TERMCAP or it holds an entry */
108
	if (!cp || *cp != '/') {	/* no TERMCAP or it holds an entry */
108
		if ( (termpath = getenv("TERMPATH")) )
109
		if ( (termpath = getenv("TERMPATH")) )
109
			strncpy(pathbuf, termpath, PBUFSIZ);
110
			strncpy(pathbuf, termpath, PBUFSIZE);
110
		else {
111
		else {
111
			if ( (home = getenv("HOME")) ) {	/* set up default */
112
			if ( (home = getenv("HOME")) ) {	/* set up default */
112
				p += strlen(home);	/* path, looking in */
113
				p += strlen(home);	/* path, looking in */
113
				strcpy(pathbuf, home);	/* $HOME first */
114
				strcpy(pathbuf, home);	/* $HOME first */
114
				*p++ = '/';
115
				*p++ = '/';
115
			}	/* if no $HOME look in current directory */
116
			}	/* if no $HOME look in current directory */
116
			strncpy(p, _PATH_DEF, PBUFSIZ - (p - pathbuf));
117
			strncpy(p, _PATH_DEF, PBUFSIZE - (p - pathbuf));
117
		}
118
		}
118
	}
119
	}
119
	else				/* user-defined name in TERMCAP */
120
	else				/* user-defined name in TERMCAP */
120
		strncpy(pathbuf, cp, PBUFSIZ);	/* still can be tokenized */
121
		strncpy(pathbuf, cp, PBUFSIZE);	/* still can be tokenized */
121
 
122
 
122
	*fname++ = pathbuf;	/* tokenize path into vector of names */
123
	*fname++ = pathbuf;	/* tokenize path into vector of names */
123
	while (*++p)
124
	while (*++p)
124
		if (*p == ' ' || *p == ':') {
125
		if (*p == ' ' || *p == ':') {
125
			*p = '\0';
126
			*p = '\0';
126
			while (*++p)
127
			while (*++p)
127
				if (*p != ' ' && *p != ':')
128
				if (*p != ' ' && *p != ':')
128
					break;
129
					break;
129
			if (*p == '\0')
130
			if (*p == '\0')
130
				break;
131
				break;
131
			*fname++ = p;
132
			*fname++ = p;
132
			if (fname >= pathvec + PVECSIZ) {
133
			if (fname >= pathvec + PVECSIZ) {
133
				fname--;
134
				fname--;
134
				break;
135
				break;
135
			}
136
			}
Line 160... Line 161...
160
	char *bp, *name;
161
	char *bp, *name;
161
{
162
{
162
	register char *cp;
163
	register char *cp;
163
	register int c;
164
	register int c;
164
	register int i, cnt;
165
	register int i, cnt;
165
	char ibuf[BUFSIZ];
166
	char ibuf[BUFSIZE];
166
	int opencnt = 0;
167
	int opencnt = 0;
167
	int tf;
168
	int tf;
168
 
169
 
169
	tbuf = bp;
170
	tbuf = bp;
170
nextfile:
171
nextfile:
Line 176... Line 177...
176
	opencnt++;
177
	opencnt++;
177
	for (;;) {
178
	for (;;) {
178
		cp = bp;
179
		cp = bp;
179
		for (;;) {
180
		for (;;) {
180
			if (i == cnt) {
181
			if (i == cnt) {
181
				cnt = read(tf, ibuf, BUFSIZ);
182
				cnt = read(tf, ibuf, BUFSIZE);
182
				if (cnt <= 0) {
183
				if (cnt <= 0) {
183
					close(tf);
184
					close(tf);
184
					pvec++;
185
					pvec++;
185
					goto nextfile;
186
					goto nextfile;
186
				}
187
				}
Line 192... Line 193...
192
					cp--;
193
					cp--;
193
					continue;
194
					continue;
194
				}
195
				}
195
				break;
196
				break;
196
			}
197
			}
197
			if (cp >= bp+BUFSIZ) {
198
			if (cp >= bp+BUFSIZE) {
198
				write(2,"Termcap entry too long\n", 23);
199
				fprintf(stderr,"Termcap entry too long\n");
199
				break;
200
				break;
200
			} else
201
			} else
201
				*cp++ = c;
202
				*cp++ = c;
202
		}
203
		}
203
		*cp = 0;
204
		*cp = 0;
204
 
-
 
205
		/*
205
		/*
206
		 * The real work for the match.
206
		 * The real work for the match.
207
		 */
207
		 */
208
		if (tnamatch(name)) {
208
		if (tnamatch(name)) {
209
			close(tf);
209
			close(tf);
Line 221... Line 221...
221
 */
221
 */
222
tnchktc()
222
tnchktc()
223
{
223
{
224
	register char *p, *q;
224
	register char *p, *q;
225
	char tcname[16];	/* name of similar terminal */
225
	char tcname[16];	/* name of similar terminal */
226
	char tcbuf[BUFSIZ];
226
	char tcbuf[BUFSIZE];
227
	char *holdtbuf = tbuf;
227
	char *holdtbuf = tbuf;
228
	int l;
228
	int l;
229
 
229
 
230
	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
230
	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
231
	while (*--p != ':')
231
	while (*--p != ':')
232
		if (p<tbuf) {
232
		if (p<tbuf) {
233
			write(2, "Bad termcap entry\n", 18);
233
			fprintf(stderr, "Bad termcap entry\n");
234
			return (0);
234
			return (0);
235
		}
235
		}
236
	p++;
236
	p++;
237
	/* p now points to beginning of last field */
237
	/* p now points to beginning of last field */
238
	if (p[0] != 't' || p[1] != 'c')
238
	if (p[0] != 't' || p[1] != 'c')
Line 241... Line 241...
241
	q = tcname;
241
	q = tcname;
242
	while (*q && *q != ':')
242
	while (*q && *q != ':')
243
		q++;
243
		q++;
244
	*q = 0;
244
	*q = 0;
245
	if (++hopcount > MAXHOP) {
245
	if (++hopcount > MAXHOP) {
246
		write(2, "Infinite tc= loop\n", 18);
246
		fprintf(stderr, "Infinite tc= loop\n");
247
		return (0);
247
		return (0);
248
	}
248
	}
249
	if (tfindent(tcbuf, tcname) != 1) {
249
	if (tfindent(tcbuf, tcname) != 1) {
250
		hopcount = 0;		/* unwind recursion */
250
		hopcount = 0;		/* unwind recursion */
251
		return(0);
251
		return(0);
252
	}
252
	}
253
	for (q=tcbuf; *q != ':'; q++)
253
	for (q=tcbuf; *q != ':'; q++)
254
		;
254
		;
255
	l = p - holdtbuf + strlen(q);
255
	l = p - holdtbuf + strlen(q);
256
	if (l > BUFSIZ) {
256
	if (l > BUFSIZE) {
257
		write(2, "Termcap entry too long\n", 23);
257
		fprintf(stderr, "Termcap entry too long!\n");
258
		q[BUFSIZ - (p-tbuf)] = 0;
258
		q[BUFSIZE - (p-tbuf)] = 0;
259
	}
259
	}
260
	strcpy(p, q+1);
260
	strcpy(p, q+1);
261
	tbuf = holdtbuf;
261
	tbuf = holdtbuf;
262
	hopcount = 0;			/* unwind recursion */
262
	hopcount = 0;			/* unwind recursion */
263
	return(1);
263
	return(1);