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