getttyent.c revision 1.22 1 /* $NetBSD: getttyent.c,v 1.22 2005/05/14 15:43:47 christos 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.22 2005/05/14 15:43:47 christos 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 FILE *tf;
60 static size_t lineno = 0;
61 static char *skip(char *, char *);
62 static char *value(char *);
63
64 struct ttyent *
65 getttynam(const char *tty)
66 {
67 struct ttyent *t;
68
69 _DIAGASSERT(tty != NULL);
70
71 setttyent();
72 while ((t = getttyent()) != NULL)
73 if (!strcmp(tty, t->ty_name))
74 break;
75 endttyent();
76 return (t);
77 }
78
79 struct ttyent *
80 getttyent(void)
81 {
82 static struct ttyent tty;
83 int c;
84 char *p;
85 size_t len;
86 static char *line = NULL;
87 char zapchar;
88
89 if (line)
90 free(line);
91
92 if (!tf && !setttyent())
93 return NULL;
94
95 for (;;) {
96 errno = 0;
97 line = fparseln(tf, &len, &lineno, NULL, FPARSELN_UNESCALL);
98 if (line == NULL) {
99 if (errno != 0)
100 warn(__func__);
101 return NULL;
102 }
103 for (p = line; *p && isspace((unsigned char)*p); p++)
104 continue;
105 if (*p && *p != '#')
106 break;
107 free(line);
108 }
109
110 tty.ty_name = p;
111 p = skip(p, &zapchar);
112 if (*(tty.ty_getty = p) == '\0')
113 tty.ty_getty = tty.ty_type = NULL;
114 else {
115 p = skip(p, &zapchar);
116 if (*(tty.ty_type = p) == '\0')
117 tty.ty_type = NULL;
118 else
119 p = skip(p, &zapchar);
120 }
121 tty.ty_status = 0;
122 tty.ty_window = NULL;
123 tty.ty_class = NULL;
124
125 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && (isspace((unsigned char) p[sizeof(e) - 1]) || p[sizeof(e) - 1] == '\0')
126 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
127 for (; *p; p = skip(p, &zapchar)) {
128 if (scmp(_TTYS_OFF))
129 tty.ty_status &= ~TTY_ON;
130 else if (scmp(_TTYS_ON))
131 tty.ty_status |= TTY_ON;
132 else if (scmp(_TTYS_SECURE))
133 tty.ty_status |= TTY_SECURE;
134 else if (scmp(_TTYS_LOCAL))
135 tty.ty_status |= TTY_LOCAL;
136 else if (scmp(_TTYS_RTSCTS))
137 tty.ty_status |= TTY_RTSCTS;
138 else if (scmp(_TTYS_DTRCTS))
139 tty.ty_status |= TTY_DTRCTS;
140 else if (scmp(_TTYS_SOFTCAR))
141 tty.ty_status |= TTY_SOFTCAR;
142 else if (scmp(_TTYS_MDMBUF))
143 tty.ty_status |= TTY_MDMBUF;
144 else if (vcmp(_TTYS_WINDOW))
145 tty.ty_window = value(p);
146 else if (vcmp(_TTYS_CLASS))
147 tty.ty_class = value(p);
148 else
149 warnx("%s: %s, %lu: unknown option `%s'",
150 __func__, _PATH_TTYS, (unsigned long)lineno, p);
151 }
152
153 if (zapchar == '#' || *p == '#')
154 while ((c = *++p) == ' ' || c == '\t')
155 continue;
156 tty.ty_comment = p;
157 if (*p == '\0')
158 tty.ty_comment = NULL;
159 if ((p = strchr(p, '\n')) != NULL)
160 *p = '\0';
161 return &tty;
162 }
163
164 #define QUOTED 1
165
166 /*
167 * Skip over the current field, removing quotes, and return a pointer to
168 * the next field.
169 */
170 static char *
171 skip(char *p, char *zapchar)
172 {
173 char *t;
174 int c, q;
175
176 _DIAGASSERT(p != NULL);
177 *zapchar = '\0';
178
179 for (q = 0, t = p; (c = *p) != '\0'; p++) {
180 if (c == '"') {
181 q ^= QUOTED; /* obscure, but nice */
182 continue;
183 }
184 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
185 p++;
186 *t++ = *p;
187 if (q == QUOTED)
188 continue;
189 if (c == '#') {
190 *zapchar = c;
191 *p = '\0';
192 *--t = '\0';
193 return p;
194 }
195 if (c == '\t' || c == ' ' || c == '\n') {
196 *zapchar = c;
197 *p++ = '\0';
198 while ((c = *p) == '\t' || c == ' ' || c == '\n')
199 p++;
200 *--t = '\0';
201 return p;
202 }
203 }
204 if (t != p)
205 *t = '\0';
206 return p;
207 }
208
209 static char *
210 value(char *p)
211 {
212
213 _DIAGASSERT(p != NULL);
214
215 return (p = strchr(p, '=')) != NULL ? ++p : NULL;
216 }
217
218 int
219 setttyent(void)
220 {
221 lineno = 0;
222 if (tf) {
223 rewind(tf);
224 return 1;
225 } else if ((tf = fopen(_PATH_TTYS, "r")) != NULL)
226 return 1;
227 return 0;
228 }
229
230 int
231 endttyent(void)
232 {
233 int rval;
234
235 if (tf) {
236 rval = !(fclose(tf) == EOF);
237 tf = NULL;
238 return rval;
239 }
240 return 1;
241 }
242