xref: /netbsd-src/lib/libc/gen/getttyent.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: getttyent.c,v 1.20 2003/08/07 16:42:51 agc 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 6/4/93";
36 #else
37 __RCSID("$NetBSD: getttyent.c,v 1.20 2003/08/07 16:42:51 agc Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include "namespace.h"
42 
43 #include <assert.h>
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <ttyent.h>
48 #include <errno.h>
49 #include <err.h>
50 #include <stdlib.h>
51 
52 #ifdef __weak_alias
53 __weak_alias(endttyent,_endttyent)
54 __weak_alias(getttyent,_getttyent)
55 __weak_alias(getttynam,_getttynam)
56 __weak_alias(setttyent,_setttyent)
57 #endif
58 
59 static char zapchar;
60 static FILE *tf;
61 static size_t lineno = 0;
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 	size_t len;
88 	static char *line = NULL;
89 
90 	if (!tf && !setttyent())
91 		return (NULL);
92 	if (line)
93 		free(line);
94 	for (;;) {
95 		errno = 0;
96 		line = fparseln(tf, &len, &lineno, NULL, FPARSELN_UNESCALL);
97 		if (line == NULL) {
98 			if (errno != 0)
99 				warn("gettyent");
100 			return NULL;
101 		}
102 		for (p = line; *p && isspace((unsigned char) *p); p++)
103 			continue;
104 		if (*p && *p != '#')
105 			break;
106 		free(line);
107 	}
108 
109 	zapchar = 0;
110 	tty.ty_name = p;
111 	p = skip(p);
112 	if (!*(tty.ty_getty = p))
113 		tty.ty_getty = tty.ty_type = NULL;
114 	else {
115 		p = skip(p);
116 		if (!*(tty.ty_type = p))
117 			tty.ty_type = NULL;
118 		else
119 			p = skip(p);
120 	}
121 	tty.ty_status = 0;
122 	tty.ty_window = NULL;
123 
124 #define	scmp(e)	!strncmp(p, e, sizeof(e) - 1) && (isspace((unsigned char) p[sizeof(e) - 1]) || p[sizeof(e) - 1] == '\0')
125 #define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
126 	for (; *p; p = skip(p)) {
127 		if (scmp(_TTYS_OFF))
128 			tty.ty_status &= ~TTY_ON;
129 		else if (scmp(_TTYS_ON))
130 			tty.ty_status |= TTY_ON;
131 		else if (scmp(_TTYS_SECURE))
132 			tty.ty_status |= TTY_SECURE;
133 		else if (scmp(_TTYS_LOCAL))
134 			tty.ty_status |= TTY_LOCAL;
135 		else if (scmp(_TTYS_RTSCTS))
136 			tty.ty_status |= TTY_RTSCTS;
137 		else if (scmp(_TTYS_DTRCTS))
138 			tty.ty_status |= TTY_DTRCTS;
139 		else if (scmp(_TTYS_SOFTCAR))
140 			tty.ty_status |= TTY_SOFTCAR;
141 		else if (scmp(_TTYS_MDMBUF))
142 			tty.ty_status |= TTY_MDMBUF;
143 		else if (vcmp(_TTYS_WINDOW))
144 			tty.ty_window = value(p);
145 		else if (vcmp(_TTYS_CLASS))
146 			tty.ty_class = value(p);
147 		else
148 			warnx("gettyent: %s, %lu: unknown option `%s'",
149 			    _PATH_TTYS, (unsigned long)lineno, p);
150 	}
151 
152 	if (zapchar == '#' || *p == '#')
153 		while ((c = *++p) == ' ' || c == '\t')
154 			;
155 	tty.ty_comment = p;
156 	if (*p == 0)
157 		tty.ty_comment = 0;
158 	if ((p = strchr(p, '\n')) != NULL)
159 		*p = '\0';
160 	return (&tty);
161 }
162 
163 #define	QUOTED	1
164 
165 /*
166  * Skip over the current field, removing quotes, and return a pointer to
167  * the next field.
168  */
169 static char *
170 skip(p)
171 	char *p;
172 {
173 	char *t;
174 	int c, q;
175 
176 	_DIAGASSERT(p != NULL);
177 
178 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
179 		if (c == '"') {
180 			q ^= QUOTED;	/* obscure, but nice */
181 			continue;
182 		}
183 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
184 			p++;
185 		*t++ = *p;
186 		if (q == QUOTED)
187 			continue;
188 		if (c == '#') {
189 			zapchar = c;
190 			*p = 0;
191 			break;
192 		}
193 		if (c == '\t' || c == ' ' || c == '\n') {
194 			zapchar = c;
195 			*p++ = 0;
196 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
197 				p++;
198 			break;
199 		}
200 	}
201 	*--t = '\0';
202 	return (p);
203 }
204 
205 static char *
206 value(p)
207 	char *p;
208 {
209 
210 	_DIAGASSERT(p != NULL);
211 
212 	return ((p = strchr(p, '=')) ? ++p : NULL);
213 }
214 
215 int
216 setttyent()
217 {
218 	lineno = 0;
219 	if (tf) {
220 		rewind(tf);
221 		return (1);
222 	} else if ((tf = fopen(_PATH_TTYS, "r")) != NULL)
223 		return (1);
224 	return (0);
225 }
226 
227 int
228 endttyent()
229 {
230 	int rval;
231 
232 	if (tf) {
233 		rval = !(fclose(tf) == EOF);
234 		tf = NULL;
235 		return (rval);
236 	}
237 	return (1);
238 }
239