getttyent.c revision 1.14 1 /* $NetBSD: getttyent.c,v 1.14 1999/01/15 18:47:49 tsarna 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.14 1999/01/15 18:47:49 tsarna Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #include "namespace.h"
46 #include <ttyent.h>
47 #include <stdio.h>
48 #include <ctype.h>
49 #include <string.h>
50
51 #ifdef __weak_alias
52 __weak_alias(endttyent,_endttyent);
53 __weak_alias(getttyent,_getttyent);
54 __weak_alias(getttynam,_getttynam);
55 __weak_alias(setttyent,_setttyent);
56 #endif
57
58 static char zapchar;
59 static FILE *tf;
60 static char *skip __P((char *));
61 static char *value __P((char *));
62
63 struct ttyent *
64 getttynam(tty)
65 const char *tty;
66 {
67 struct ttyent *t;
68
69 setttyent();
70 while ((t = getttyent()) != NULL)
71 if (!strcmp(tty, t->ty_name))
72 break;
73 endttyent();
74 return (t);
75 }
76
77 struct ttyent *
78 getttyent()
79 {
80 static struct ttyent tty;
81 int c;
82 char *p;
83 #define MAXLINELENGTH 200
84 static char line[MAXLINELENGTH];
85
86 if (!tf && !setttyent())
87 return (NULL);
88 for (;;) {
89 if (!fgets(p = line, sizeof(line), tf))
90 return (NULL);
91 /* skip lines that are too big */
92 if (!strchr(p, '\n')) {
93 while ((c = getc(tf)) != '\n' && c != EOF)
94 ;
95 continue;
96 }
97 while (isspace(*p))
98 ++p;
99 if (*p && *p != '#')
100 break;
101 }
102
103 zapchar = 0;
104 tty.ty_name = p;
105 p = skip(p);
106 if (!*(tty.ty_getty = p))
107 tty.ty_getty = tty.ty_type = NULL;
108 else {
109 p = skip(p);
110 if (!*(tty.ty_type = p))
111 tty.ty_type = NULL;
112 else
113 p = skip(p);
114 }
115 tty.ty_status = 0;
116 tty.ty_window = NULL;
117
118 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
119 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
120 for (; *p; p = skip(p)) {
121 if (scmp(_TTYS_OFF))
122 tty.ty_status &= ~TTY_ON;
123 else if (scmp(_TTYS_ON))
124 tty.ty_status |= TTY_ON;
125 else if (scmp(_TTYS_SECURE))
126 tty.ty_status |= TTY_SECURE;
127 else if (scmp(_TTYS_LOCAL))
128 tty.ty_status |= TTY_LOCAL;
129 else if (scmp(_TTYS_RTSCTS))
130 tty.ty_status |= TTY_RTSCTS;
131 else if (scmp(_TTYS_DTRCTS))
132 tty.ty_status |= TTY_DTRCTS;
133 else if (scmp(_TTYS_SOFTCAR))
134 tty.ty_status |= TTY_SOFTCAR;
135 else if (scmp(_TTYS_MDMBUF))
136 tty.ty_status |= TTY_MDMBUF;
137 else if (vcmp(_TTYS_WINDOW))
138 tty.ty_window = value(p);
139 else if (vcmp(_TTYS_CLASS))
140 tty.ty_class = value(p);
141 else
142 break;
143 }
144
145 if (zapchar == '#' || *p == '#')
146 while ((c = *++p) == ' ' || c == '\t')
147 ;
148 tty.ty_comment = p;
149 if (*p == 0)
150 tty.ty_comment = 0;
151 if ((p = strchr(p, '\n')) != NULL)
152 *p = '\0';
153 return (&tty);
154 }
155
156 #define QUOTED 1
157
158 /*
159 * Skip over the current field, removing quotes, and return a pointer to
160 * the next field.
161 */
162 static char *
163 skip(p)
164 char *p;
165 {
166 char *t;
167 int c, q;
168
169 for (q = 0, t = p; (c = *p) != '\0'; p++) {
170 if (c == '"') {
171 q ^= QUOTED; /* obscure, but nice */
172 continue;
173 }
174 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
175 p++;
176 *t++ = *p;
177 if (q == QUOTED)
178 continue;
179 if (c == '#') {
180 zapchar = c;
181 *p = 0;
182 break;
183 }
184 if (c == '\t' || c == ' ' || c == '\n') {
185 zapchar = c;
186 *p++ = 0;
187 while ((c = *p) == '\t' || c == ' ' || c == '\n')
188 p++;
189 break;
190 }
191 }
192 *--t = '\0';
193 return (p);
194 }
195
196 static char *
197 value(p)
198 char *p;
199 {
200
201 return ((p = strchr(p, '=')) ? ++p : NULL);
202 }
203
204 int
205 setttyent()
206 {
207
208 if (tf) {
209 rewind(tf);
210 return (1);
211 } else if ((tf = fopen(_PATH_TTYS, "r")) != NULL)
212 return (1);
213 return (0);
214 }
215
216 int
217 endttyent()
218 {
219 int rval;
220
221 if (tf) {
222 rval = !(fclose(tf) == EOF);
223 tf = NULL;
224 return (rval);
225 }
226 return (1);
227 }
228