]>
Commit | Line | Data |
---|---|---|
e9ce8d39 | 1 | /* |
224c7076 | 2 | * Copyright (c) 1999, 2005 Apple Computer, Inc. All rights reserved. |
e9ce8d39 A |
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> |
224c7076 A |
63 | #include <regex.h> |
64 | #include <limits.h> | |
e9ce8d39 | 65 | |
70ad1dc8 A |
66 | #pragma clang diagnostic push |
67 | #pragma clang diagnostic ignored "-Wstrict-prototypes" | |
68 | ||
e9ce8d39 A |
69 | static char zapchar; |
70 | static FILE *tf; | |
71 | ||
72 | struct ttyent * | |
73 | getttynam(tty) | |
74 | const char *tty; | |
75 | { | |
76 | register struct ttyent *t; | |
77 | ||
78 | setttyent(); | |
224c7076 | 79 | while ((t = getttyent()) != NULL) |
e9ce8d39 A |
80 | if (!strcmp(tty, t->ty_name)) |
81 | break; | |
82 | endttyent(); | |
83 | return (t); | |
84 | } | |
85 | ||
3d9156a7 A |
86 | static char *skip(), *value(); |
87 | ||
224c7076 A |
88 | /* |
89 | * 4372480: Support for sequences in the tty name. Expressions like [000-999] | |
90 | * for decimal sequences and [0x0-0xf] for hexidecimal sequences causes a | |
91 | * sequence of all combinations of names to be returned by getttyent(). | |
92 | * | |
93 | * There is also a slot=nn option, which will cause getttyent() to return | |
94 | * non-existent ttyent structs until the slot number nn is reached. Note, slot | |
95 | * numbers begin at 1. | |
96 | */ | |
97 | struct seq { | |
98 | int first; | |
99 | int count; | |
100 | int index; | |
101 | char fmt[NAME_MAX + 17]; | |
102 | }; | |
103 | ||
6465356a | 104 | static const char *brapat = "\\[(.*)]"; |
224c7076 | 105 | static regex_t brapreg; |
6465356a | 106 | static const char *decpat = "^([0-9]+)-([0-9]+)$"; |
224c7076 | 107 | static regex_t decpreg; |
6465356a | 108 | static const char *hexpat = "^0x([0-9a-f]+)-0x([0-9a-f]+)$"; |
224c7076 A |
109 | static regex_t hexpreg; |
110 | static struct seq *seq = NULL; | |
111 | static int slot; | |
112 | ||
e9ce8d39 | 113 | struct ttyent * |
6465356a | 114 | getttyent(void) |
e9ce8d39 A |
115 | { |
116 | static struct ttyent tty; | |
6465356a | 117 | static const struct ttyent nonexistent = { |
224c7076 A |
118 | "\01", /* this shouldn't match anything */ |
119 | NULL, | |
120 | NULL, | |
121 | 0, | |
122 | NULL, | |
123 | NULL, | |
124 | NULL | |
125 | }; | |
e9ce8d39 A |
126 | register int c; |
127 | register char *p; | |
128 | #define MAXLINELENGTH 1024 | |
5b2abdfb | 129 | static char *line = NULL; |
3d9156a7 | 130 | locale_t loc = __current_locale(); |
224c7076 A |
131 | int newslot, hex; |
132 | long b, e; | |
133 | regmatch_t match[3], save, bracket; | |
e9ce8d39 | 134 | |
5b2abdfb A |
135 | if ( line == NULL ) { |
136 | line = malloc(MAXLINELENGTH); | |
137 | if( line == NULL ) | |
138 | return NULL; | |
139 | } | |
140 | ||
e9ce8d39 A |
141 | if (!tf && !setttyent()) |
142 | return (NULL); | |
224c7076 A |
143 | |
144 | restart: | |
145 | if (slot < seq->first) { | |
146 | /* | |
147 | * the slot= option was set, and we are returning non-existent | |
148 | * entries until we catch up. | |
149 | */ | |
150 | slot++; | |
6465356a | 151 | return (struct ttyent *)&nonexistent; |
224c7076 A |
152 | } |
153 | ||
154 | if (seq->count > 0) { | |
155 | /* | |
156 | * generate the next tty name; the rest of the tty entries | |
157 | * is the same. | |
158 | */ | |
159 | sprintf(tty.ty_name, seq->fmt, seq->index++); | |
160 | slot++; | |
161 | seq->count--; | |
162 | return &tty; | |
163 | } | |
164 | ||
165 | if (slot == seq->first) { | |
166 | /* | |
167 | * this was a regular entry with slot= | |
168 | */ | |
169 | slot++; | |
170 | return &tty; | |
171 | } | |
172 | ||
e9ce8d39 | 173 | for (;;) { |
5b2abdfb | 174 | if (!fgets(p = line, MAXLINELENGTH, tf)) |
e9ce8d39 A |
175 | return (NULL); |
176 | /* skip lines that are too big */ | |
177 | if (!index(p, '\n')) { | |
178 | while ((c = getc(tf)) != '\n' && c != EOF) | |
179 | ; | |
180 | continue; | |
181 | } | |
3d9156a7 | 182 | while (isspace_l(*p, loc)) |
e9ce8d39 A |
183 | ++p; |
184 | if (*p && *p != '#') | |
185 | break; | |
186 | } | |
187 | ||
188 | zapchar = 0; | |
189 | tty.ty_name = p; | |
190 | p = skip(p); | |
191 | if (!*(tty.ty_getty = p)) | |
192 | tty.ty_getty = tty.ty_type = NULL; | |
193 | else { | |
194 | p = skip(p); | |
195 | if (!*(tty.ty_type = p)) | |
196 | tty.ty_type = NULL; | |
197 | else | |
198 | p = skip(p); | |
199 | } | |
200 | tty.ty_status = 0; | |
201 | tty.ty_window = NULL; | |
202 | tty.ty_onerror = NULL; | |
203 | tty.ty_onoption = NULL; | |
204 | ||
3d9156a7 | 205 | #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace_l(p[sizeof(e) - 1], loc) |
e9ce8d39 | 206 | #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' |
224c7076 | 207 | newslot = -1; |
e9ce8d39 A |
208 | for (; *p; p = skip(p)) { |
209 | if (scmp(_TTYS_OFF)) | |
210 | tty.ty_status &= ~TTY_ON; | |
211 | else if (scmp(_TTYS_ON)) | |
212 | tty.ty_status |= TTY_ON; | |
213 | else if (scmp(_TTYS_SECURE)) | |
214 | tty.ty_status |= TTY_SECURE; | |
215 | else if (vcmp(_TTYS_WINDOW)) | |
216 | tty.ty_window = value(p); | |
217 | else if (vcmp(_TTYS_ONERROR)) | |
218 | tty.ty_onerror = value(p); | |
219 | else if (vcmp(_TTYS_ONOPTION)) | |
220 | tty.ty_onoption = value(p); | |
224c7076 A |
221 | else if (vcmp(_TTYS_SLOT)) { |
222 | char *slotstr = value(p); | |
223 | if (slotstr) | |
224 | newslot = atoi(slotstr); | |
225 | } else | |
e9ce8d39 A |
226 | break; |
227 | } | |
228 | ||
229 | if (zapchar == '#' || *p == '#') | |
230 | while ((c = *++p) == ' ' || c == '\t') | |
231 | ; | |
232 | tty.ty_comment = p; | |
233 | if (*p == 0) | |
234 | tty.ty_comment = 0; | |
224c7076 | 235 | if ((p = index(p, '\n')) != NULL) |
e9ce8d39 | 236 | *p = '\0'; |
224c7076 A |
237 | |
238 | /* check if tty.tyname has a sequence */ | |
239 | if (regexec(&brapreg, tty.ty_name, 3, match, 0) != 0) | |
240 | goto out; | |
241 | ||
242 | /* | |
243 | * save the range of the bracketed range, so we can find the strings | |
244 | * before and after | |
245 | */ | |
246 | bracket = match[0]; | |
247 | /* use REG_STARTEND to limit matching with the bracketed range */ | |
248 | match[0] = save = match[1]; | |
249 | if (regexec(&decpreg, tty.ty_name, 3, match, REG_STARTEND) == 0) { | |
250 | /* a decimal range */ | |
251 | b = strtol(tty.ty_name + match[1].rm_so, NULL, 10); | |
252 | e = strtol(tty.ty_name + match[2].rm_so, NULL, 10); | |
253 | hex = 0; | |
254 | } else { | |
255 | match[0] = save; | |
256 | if (regexec(&hexpreg, tty.ty_name, 3, match, REG_STARTEND) == 0) { | |
257 | /* a hexidecimal range */ | |
258 | b = strtol(tty.ty_name + match[1].rm_so, NULL, 16); | |
259 | e = strtol(tty.ty_name + match[2].rm_so, NULL, 16); | |
260 | hex = 1; | |
261 | } else | |
262 | goto out; | |
263 | } | |
264 | if (b > e) /* skip */ | |
265 | goto restart; | |
266 | ||
267 | /* seq->first is already less than slot, so just leave it */ | |
b061a43b A |
268 | seq->count = (int)(e - b + 1); |
269 | seq->index = (int)(b); | |
224c7076 A |
270 | /* |
271 | * The fmt string contains the characters before the bracket, the | |
272 | * a format specifier (either decimal or hex) and any characters | |
273 | * after the bracket. Note that the length of the lower range is | |
274 | * use as a minimum digit length, with zero fill, so the format | |
275 | * specifier will look something like %03d. | |
276 | */ | |
277 | sprintf(seq->fmt, "%.*s%%0%d%c%s", | |
278 | (int)bracket.rm_so, tty.ty_name, | |
279 | (int)(match[1].rm_eo - match[1].rm_so), | |
280 | hex ? 'x' : 'd', | |
281 | tty.ty_name + bracket.rm_eo); | |
282 | ||
283 | out: | |
284 | if (newslot > slot) { | |
285 | /* set up to skip until newslot */ | |
286 | seq->first = newslot; | |
287 | goto restart; | |
288 | } | |
289 | if (seq->count > 0) /* restart if we are doing a sequence */ | |
290 | goto restart; | |
291 | /* regular single value return */ | |
292 | slot++; | |
e9ce8d39 A |
293 | return (&tty); |
294 | } | |
295 | ||
296 | #define QUOTED 1 | |
297 | ||
298 | /* | |
299 | * Skip over the current field, removing quotes, and return a pointer to | |
300 | * the next field. | |
301 | */ | |
302 | static char * | |
303 | skip(p) | |
304 | register char *p; | |
305 | { | |
306 | register char *t; | |
307 | register int c, q; | |
308 | ||
309 | for (q = 0, t = p; (c = *p) != '\0'; p++) { | |
310 | if (c == '"') { | |
311 | q ^= QUOTED; /* obscure, but nice */ | |
312 | continue; | |
313 | } | |
314 | if (q == QUOTED && *p == '\\' && *(p+1) == '"') | |
315 | p++; | |
316 | *t++ = *p; | |
317 | if (q == QUOTED) | |
318 | continue; | |
319 | if (c == '#') { | |
320 | zapchar = c; | |
321 | *p = 0; | |
322 | break; | |
323 | } | |
324 | if (c == '\t' || c == ' ' || c == '\n') { | |
325 | zapchar = c; | |
326 | *p++ = 0; | |
327 | while ((c = *p) == '\t' || c == ' ' || c == '\n') | |
328 | p++; | |
329 | break; | |
330 | } | |
331 | } | |
332 | *--t = '\0'; | |
333 | return (p); | |
334 | } | |
335 | ||
336 | static char * | |
337 | value(p) | |
338 | register char *p; | |
339 | { | |
340 | ||
341 | return ((p = index(p, '=')) ? ++p : NULL); | |
342 | } | |
343 | ||
344 | int | |
345 | setttyent() | |
346 | { | |
347 | ||
224c7076 A |
348 | /* initialize seq and the three regexp patterns */ |
349 | if (!seq) { | |
350 | if (regcomp(&brapreg, brapat, REG_EXTENDED) != 0) | |
351 | return 0; | |
352 | if (regcomp(&decpreg, decpat, REG_EXTENDED) != 0) { | |
353 | regfree(&brapreg); | |
354 | return 0; | |
355 | } | |
356 | if (regcomp(&hexpreg, hexpat, REG_EXTENDED | REG_ICASE) != 0) { | |
357 | regfree(&decpreg); | |
358 | regfree(&brapreg); | |
359 | return 0; | |
360 | } | |
361 | if ((seq = malloc(sizeof(struct seq))) == NULL) { | |
362 | regfree(&hexpreg); | |
363 | regfree(&decpreg); | |
364 | regfree(&brapreg); | |
365 | return 0; | |
366 | } | |
367 | } | |
368 | seq->first = seq->count = 0; | |
369 | slot = 1; | |
370 | ||
e9ce8d39 A |
371 | if (tf) { |
372 | (void)rewind(tf); | |
373 | return (1); | |
224c7076 | 374 | } else if ((tf = fopen(_PATH_TTYS, "r")) != NULL) |
e9ce8d39 A |
375 | return (1); |
376 | return (0); | |
377 | } | |
378 | ||
379 | int | |
380 | endttyent() | |
381 | { | |
382 | int rval; | |
383 | ||
384 | if (tf) { | |
385 | rval = !(fclose(tf) == EOF); | |
386 | tf = NULL; | |
387 | return (rval); | |
388 | } | |
389 | return (1); | |
390 | } | |
70ad1dc8 A |
391 | #pragma clang diagnostic pop |
392 |