xref: /netbsd/lib/libc/gen/getttyent.c (revision bf9ec67e)
1 /*	$NetBSD: getttyent.c,v 1.18 2000/07/07 08:03:37 itohy Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: getttyent.c,v 1.18 2000/07/07 08:03:37 itohy Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44 
45 #include "namespace.h"
46 
47 #include <assert.h>
48 #include <ctype.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <ttyent.h>
52 
53 #ifdef __weak_alias
54 __weak_alias(endttyent,_endttyent)
55 __weak_alias(getttyent,_getttyent)
56 __weak_alias(getttynam,_getttynam)
57 __weak_alias(setttyent,_setttyent)
58 #endif
59 
60 static char zapchar;
61 static FILE *tf;
62 static char *skip __P((char *));
63 static char *value __P((char *));
64 
65 struct ttyent *
66 getttynam(tty)
67 	const char *tty;
68 {
69 	struct ttyent *t;
70 
71 	_DIAGASSERT(tty != NULL);
72 
73 	setttyent();
74 	while ((t = getttyent()) != NULL)
75 		if (!strcmp(tty, t->ty_name))
76 			break;
77 	endttyent();
78 	return (t);
79 }
80 
81 struct ttyent *
82 getttyent()
83 {
84 	static struct ttyent tty;
85 	int c;
86 	char *p;
87 #define	MAXLINELENGTH	200
88 	static char line[MAXLINELENGTH];
89 
90 	if (!tf && !setttyent())
91 		return (NULL);
92 	for (;;) {
93 		if (!fgets(p = line, sizeof(line), tf))
94 			return (NULL);
95 		/* skip lines that are too big */
96 		if (!strchr(p, '\n')) {
97 			while ((c = getc(tf)) != '\n' && c != EOF)
98 				;
99 			continue;
100 		}
101 		while (isspace((unsigned char) *p))
102 			++p;
103 		if (*p && *p != '#')
104 			break;
105 	}
106 
107 	zapchar = 0;
108 	tty.ty_name = p;
109 	p = skip(p);
110 	if (!*(tty.ty_getty = p))
111 		tty.ty_getty = tty.ty_type = NULL;
112 	else {
113 		p = skip(p);
114 		if (!*(tty.ty_type = p))
115 			tty.ty_type = NULL;
116 		else
117 			p = skip(p);
118 	}
119 	tty.ty_status = 0;
120 	tty.ty_window = NULL;
121 
122 #define	scmp(e)	!strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char) p[sizeof(e) - 1])
123 #define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
124 	for (; *p; p = skip(p)) {
125 		if (scmp(_TTYS_OFF))
126 			tty.ty_status &= ~TTY_ON;
127 		else if (scmp(_TTYS_ON))
128 			tty.ty_status |= TTY_ON;
129 		else if (scmp(_TTYS_SECURE))
130 			tty.ty_status |= TTY_SECURE;
131 		else if (scmp(_TTYS_LOCAL))
132 			tty.ty_status |= TTY_LOCAL;
133 		else if (scmp(_TTYS_RTSCTS))
134 			tty.ty_status |= TTY_RTSCTS;
135 		else if (scmp(_TTYS_DTRCTS))
136 			tty.ty_status |= TTY_DTRCTS;
137 		else if (scmp(_TTYS_SOFTCAR))
138 			tty.ty_status |= TTY_SOFTCAR;
139 		else if (scmp(_TTYS_MDMBUF))
140 			tty.ty_status |= TTY_MDMBUF;
141 		else if (vcmp(_TTYS_WINDOW))
142 			tty.ty_window = value(p);
143 		else if (vcmp(_TTYS_CLASS))
144 			tty.ty_class = value(p);
145 		else
146 			break;
147 	}
148 
149 	if (zapchar == '#' || *p == '#')
150 		while ((c = *++p) == ' ' || c == '\t')
151 			;
152 	tty.ty_comment = p;
153 	if (*p == 0)
154 		tty.ty_comment = 0;
155 	if ((p = strchr(p, '\n')) != NULL)
156 		*p = '\0';
157 	return (&tty);
158 }
159 
160 #define	QUOTED	1
161 
162 /*
163  * Skip over the current field, removing quotes, and return a pointer to
164  * the next field.
165  */
166 static char *
167 skip(p)
168 	char *p;
169 {
170 	char *t;
171 	int c, q;
172 
173 	_DIAGASSERT(p != NULL);
174 
175 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
176 		if (c == '"') {
177 			q ^= QUOTED;	/* obscure, but nice */
178 			continue;
179 		}
180 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
181 			p++;
182 		*t++ = *p;
183 		if (q == QUOTED)
184 			continue;
185 		if (c == '#') {
186 			zapchar = c;
187 			*p = 0;
188 			break;
189 		}
190 		if (c == '\t' || c == ' ' || c == '\n') {
191 			zapchar = c;
192 			*p++ = 0;
193 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
194 				p++;
195 			break;
196 		}
197 	}
198 	*--t = '\0';
199 	return (p);
200 }
201 
202 static char *
203 value(p)
204 	char *p;
205 {
206 
207 	_DIAGASSERT(p != NULL);
208 
209 	return ((p = strchr(p, '=')) ? ++p : NULL);
210 }
211 
212 int
213 setttyent()
214 {
215 
216 	if (tf) {
217 		rewind(tf);
218 		return (1);
219 	} else if ((tf = fopen(_PATH_TTYS, "r")) != NULL)
220 		return (1);
221 	return (0);
222 }
223 
224 int
225 endttyent()
226 {
227 	int rval;
228 
229 	if (tf) {
230 		rval = !(fclose(tf) == EOF);
231 		tf = NULL;
232 		return (rval);
233 	}
234 	return (1);
235 }
236