]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
734aad71 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
e9ce8d39 A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
734aad71 A |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
e9ce8d39 A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | /* | |
24 | * Copyright (c) 1989, 1993 | |
25 | * The Regents of the University of California. All rights reserved. | |
26 | * | |
27 | * Redistribution and use in source and binary forms, with or without | |
28 | * modification, are permitted provided that the following conditions | |
29 | * are met: | |
30 | * 1. Redistributions of source code must retain the above copyright | |
31 | * notice, this list of conditions and the following disclaimer. | |
32 | * 2. Redistributions in binary form must reproduce the above copyright | |
33 | * notice, this list of conditions and the following disclaimer in the | |
34 | * documentation and/or other materials provided with the distribution. | |
35 | * 3. All advertising materials mentioning features or use of this software | |
36 | * must display the following acknowledgement: | |
37 | * This product includes software developed by the University of | |
38 | * California, Berkeley and its contributors. | |
39 | * 4. Neither the name of the University nor the names of its contributors | |
40 | * may be used to endorse or promote products derived from this software | |
41 | * without specific prior written permission. | |
42 | * | |
43 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
53 | * SUCH DAMAGE. | |
54 | */ | |
55 | ||
3d9156a7 | 56 | #include "xlocale_private.h" |
e9ce8d39 A |
57 | |
58 | #include <ttyent.h> | |
59 | #include <stdio.h> | |
60 | #include <ctype.h> | |
61 | #include <string.h> | |
5b2abdfb | 62 | #include <stdlib.h> |
e9ce8d39 A |
63 | |
64 | static char zapchar; | |
65 | static FILE *tf; | |
66 | ||
67 | struct ttyent * | |
68 | getttynam(tty) | |
69 | const char *tty; | |
70 | { | |
71 | register struct ttyent *t; | |
72 | ||
73 | setttyent(); | |
74 | while (t = getttyent()) | |
75 | if (!strcmp(tty, t->ty_name)) | |
76 | break; | |
77 | endttyent(); | |
78 | return (t); | |
79 | } | |
80 | ||
3d9156a7 A |
81 | static char *skip(), *value(); |
82 | ||
e9ce8d39 A |
83 | struct ttyent * |
84 | getttyent() | |
85 | { | |
86 | static struct ttyent tty; | |
87 | register int c; | |
88 | register char *p; | |
89 | #define MAXLINELENGTH 1024 | |
5b2abdfb | 90 | static char *line = NULL; |
3d9156a7 | 91 | locale_t loc = __current_locale(); |
e9ce8d39 | 92 | |
5b2abdfb A |
93 | if ( line == NULL ) { |
94 | line = malloc(MAXLINELENGTH); | |
95 | if( line == NULL ) | |
96 | return NULL; | |
97 | } | |
98 | ||
e9ce8d39 A |
99 | if (!tf && !setttyent()) |
100 | return (NULL); | |
101 | for (;;) { | |
5b2abdfb | 102 | if (!fgets(p = line, MAXLINELENGTH, tf)) |
e9ce8d39 A |
103 | return (NULL); |
104 | /* skip lines that are too big */ | |
105 | if (!index(p, '\n')) { | |
106 | while ((c = getc(tf)) != '\n' && c != EOF) | |
107 | ; | |
108 | continue; | |
109 | } | |
3d9156a7 | 110 | while (isspace_l(*p, loc)) |
e9ce8d39 A |
111 | ++p; |
112 | if (*p && *p != '#') | |
113 | break; | |
114 | } | |
115 | ||
116 | zapchar = 0; | |
117 | tty.ty_name = p; | |
118 | p = skip(p); | |
119 | if (!*(tty.ty_getty = p)) | |
120 | tty.ty_getty = tty.ty_type = NULL; | |
121 | else { | |
122 | p = skip(p); | |
123 | if (!*(tty.ty_type = p)) | |
124 | tty.ty_type = NULL; | |
125 | else | |
126 | p = skip(p); | |
127 | } | |
128 | tty.ty_status = 0; | |
129 | tty.ty_window = NULL; | |
130 | tty.ty_onerror = NULL; | |
131 | tty.ty_onoption = NULL; | |
132 | ||
3d9156a7 | 133 | #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace_l(p[sizeof(e) - 1], loc) |
e9ce8d39 A |
134 | #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' |
135 | for (; *p; p = skip(p)) { | |
136 | if (scmp(_TTYS_OFF)) | |
137 | tty.ty_status &= ~TTY_ON; | |
138 | else if (scmp(_TTYS_ON)) | |
139 | tty.ty_status |= TTY_ON; | |
140 | else if (scmp(_TTYS_SECURE)) | |
141 | tty.ty_status |= TTY_SECURE; | |
142 | else if (vcmp(_TTYS_WINDOW)) | |
143 | tty.ty_window = value(p); | |
144 | else if (vcmp(_TTYS_ONERROR)) | |
145 | tty.ty_onerror = value(p); | |
146 | else if (vcmp(_TTYS_ONOPTION)) | |
147 | tty.ty_onoption = value(p); | |
148 | else | |
149 | break; | |
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 = index(p, '\n')) | |
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 | register char *p; | |
172 | { | |
173 | register char *t; | |
174 | register int c, q; | |
175 | ||
176 | for (q = 0, t = p; (c = *p) != '\0'; p++) { | |
177 | if (c == '"') { | |
178 | q ^= QUOTED; /* obscure, but nice */ | |
179 | continue; | |
180 | } | |
181 | if (q == QUOTED && *p == '\\' && *(p+1) == '"') | |
182 | p++; | |
183 | *t++ = *p; | |
184 | if (q == QUOTED) | |
185 | continue; | |
186 | if (c == '#') { | |
187 | zapchar = c; | |
188 | *p = 0; | |
189 | break; | |
190 | } | |
191 | if (c == '\t' || c == ' ' || c == '\n') { | |
192 | zapchar = c; | |
193 | *p++ = 0; | |
194 | while ((c = *p) == '\t' || c == ' ' || c == '\n') | |
195 | p++; | |
196 | break; | |
197 | } | |
198 | } | |
199 | *--t = '\0'; | |
200 | return (p); | |
201 | } | |
202 | ||
203 | static char * | |
204 | value(p) | |
205 | register char *p; | |
206 | { | |
207 | ||
208 | return ((p = index(p, '=')) ? ++p : NULL); | |
209 | } | |
210 | ||
211 | int | |
212 | setttyent() | |
213 | { | |
214 | ||
215 | if (tf) { | |
216 | (void)rewind(tf); | |
217 | return (1); | |
218 | } else if (tf = fopen(_PATH_TTYS, "r")) | |
219 | return (1); | |
220 | return (0); | |
221 | } | |
222 | ||
223 | int | |
224 | endttyent() | |
225 | { | |
226 | int rval; | |
227 | ||
228 | if (tf) { | |
229 | rval = !(fclose(tf) == EOF); | |
230 | tf = NULL; | |
231 | return (rval); | |
232 | } | |
233 | return (1); | |
234 | } |