xref: /csrg-svn/lib/libc/gen/getttyent.c (revision 61111)
121343Sdist /*
2*61111Sbostic  * Copyright (c) 1989, 1993
3*61111Sbostic  *	The Regents of the University of California.  All rights reserved.
439176Sbostic  *
542625Sbostic  * %sccs.include.redist.c%
621343Sdist  */
716420Sralph 
826562Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61111Sbostic static char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 06/04/93";
1039176Sbostic #endif /* LIBC_SCCS and not lint */
1121343Sdist 
1239176Sbostic #include <ttyent.h>
1316420Sralph #include <stdio.h>
1439176Sbostic #include <ctype.h>
1542024Sbostic #include <string.h>
1616420Sralph 
1728308Skarels static char zapchar;
1839176Sbostic static FILE *tf;
1916420Sralph 
2039176Sbostic struct ttyent *
getttynam(tty)2139176Sbostic getttynam(tty)
2246597Sdonn 	const char *tty;
2316420Sralph {
2439176Sbostic 	register struct ttyent *t;
2539176Sbostic 
2639176Sbostic 	setttyent();
2739176Sbostic 	while (t = getttyent())
2839176Sbostic 		if (!strcmp(tty, t->ty_name))
2939176Sbostic 			break;
3047645Skarels 	endttyent();
3147645Skarels 	return (t);
3216420Sralph }
3316420Sralph 
3439176Sbostic struct ttyent *
getttyent()3539176Sbostic getttyent()
3616420Sralph {
3739176Sbostic 	static struct ttyent tty;
3839176Sbostic 	register int c;
3939176Sbostic 	register char *p;
4039176Sbostic #define	MAXLINELENGTH	100
4139176Sbostic 	static char line[MAXLINELENGTH];
4246597Sdonn 	static char *skip(), *value();
4339176Sbostic 
4439176Sbostic 	if (!tf && !setttyent())
4547645Skarels 		return (NULL);
4640204Sbostic 	for (;;) {
4740204Sbostic 		if (!fgets(p = line, sizeof(line), tf))
4847645Skarels 			return (NULL);
4939176Sbostic 		/* skip lines that are too big */
5040204Sbostic 		if (!index(p, '\n')) {
5139176Sbostic 			while ((c = getc(tf)) != '\n' && c != EOF)
5239176Sbostic 				;
5339176Sbostic 			continue;
5439176Sbostic 		}
5540204Sbostic 		while (isspace(*p))
5640204Sbostic 			++p;
5740204Sbostic 		if (*p && *p != '#')
5840204Sbostic 			break;
5940204Sbostic 	}
6039176Sbostic 
6139176Sbostic 	zapchar = 0;
6239176Sbostic 	tty.ty_name = p;
6339176Sbostic 	p = skip(p);
6439176Sbostic 	if (!*(tty.ty_getty = p))
6539176Sbostic 		tty.ty_getty = tty.ty_type = NULL;
6639176Sbostic 	else {
6739176Sbostic 		p = skip(p);
6839176Sbostic 		if (!*(tty.ty_type = p))
6939176Sbostic 			tty.ty_type = NULL;
7039176Sbostic 		else
7139176Sbostic 			p = skip(p);
7216420Sralph 	}
7339176Sbostic 	tty.ty_status = 0;
7439176Sbostic 	tty.ty_window = NULL;
7539176Sbostic 
7639176Sbostic #define	scmp(e)	!strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
7739176Sbostic #define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
7839176Sbostic 	for (; *p; p = skip(p)) {
7939176Sbostic 		if (scmp(_TTYS_OFF))
8039176Sbostic 			tty.ty_status &= ~TTY_ON;
8139176Sbostic 		else if (scmp(_TTYS_ON))
8239176Sbostic 			tty.ty_status |= TTY_ON;
8339176Sbostic 		else if (scmp(_TTYS_SECURE))
8439176Sbostic 			tty.ty_status |= TTY_SECURE;
8539176Sbostic 		else if (vcmp(_TTYS_WINDOW))
8639176Sbostic 			tty.ty_window = value(p);
8739176Sbostic 		else
8839176Sbostic 			break;
8939176Sbostic 	}
9039176Sbostic 
9139176Sbostic 	if (zapchar == '#' || *p == '#')
9239176Sbostic 		while ((c = *++p) == ' ' || c == '\t')
9339176Sbostic 			;
9439176Sbostic 	tty.ty_comment = p;
9539176Sbostic 	if (*p == 0)
9639176Sbostic 		tty.ty_comment = 0;
9739176Sbostic 	if (p = index(p, '\n'))
9839176Sbostic 		*p = '\0';
9947645Skarels 	return (&tty);
10016420Sralph }
10116420Sralph 
10239176Sbostic #define	QUOTED	1
10317882Sralph 
10417882Sralph /*
10539176Sbostic  * Skip over the current field, removing quotes, and return a pointer to
10639176Sbostic  * the next field.
10717882Sralph  */
10816420Sralph static char *
skip(p)10916420Sralph skip(p)
11017882Sralph 	register char *p;
11116420Sralph {
11239176Sbostic 	register char *t;
11339176Sbostic 	register int c, q;
11416420Sralph 
11539176Sbostic 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
11617882Sralph 		if (c == '"') {
11717882Sralph 			q ^= QUOTED;	/* obscure, but nice */
11817882Sralph 			continue;
11917882Sralph 		}
12028308Skarels 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
12128308Skarels 			p++;
12228308Skarels 		*t++ = *p;
12317882Sralph 		if (q == QUOTED)
12417882Sralph 			continue;
12516420Sralph 		if (c == '#') {
12628308Skarels 			zapchar = c;
12728308Skarels 			*p = 0;
12816420Sralph 			break;
12916420Sralph 		}
13016420Sralph 		if (c == '\t' || c == ' ' || c == '\n') {
13128308Skarels 			zapchar = c;
13228308Skarels 			*p++ = 0;
13316420Sralph 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
13416420Sralph 				p++;
13516420Sralph 			break;
13616420Sralph 		}
13716420Sralph 	}
13828308Skarels 	*--t = '\0';
13947645Skarels 	return (p);
14016420Sralph }
14116420Sralph 
14217882Sralph static char *
value(p)14317882Sralph value(p)
14417882Sralph 	register char *p;
14517882Sralph {
14647645Skarels 
14747645Skarels 	return ((p = index(p, '=')) ? ++p : NULL);
14817882Sralph }
14917882Sralph 
15046597Sdonn int
setttyent()15139176Sbostic setttyent()
15216420Sralph {
15347645Skarels 
15439176Sbostic 	if (tf) {
15539176Sbostic 		(void)rewind(tf);
15647645Skarels 		return (1);
15739176Sbostic 	} else if (tf = fopen(_PATH_TTYS, "r"))
15847645Skarels 		return (1);
15947645Skarels 	return (0);
16039176Sbostic }
16116420Sralph 
16246597Sdonn int
endttyent()16339176Sbostic endttyent()
16439176Sbostic {
16539176Sbostic 	int rval;
16639176Sbostic 
16739176Sbostic 	if (tf) {
16839176Sbostic 		rval = !(fclose(tf) == EOF);
16939176Sbostic 		tf = NULL;
17047645Skarels 		return (rval);
17116420Sralph 	}
17247645Skarels 	return (1);
17316420Sralph }
174