Home | History | Annotate | Line # | Download | only in gen
getttyent.c revision 1.20.4.1
      1 /*	$NetBSD: getttyent.c,v 1.20.4.1 2005/05/13 18:26:09 riz 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.20.4.1 2005/05/13 18:26:09 riz 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 
    124 #define	scmp(e)	!strncmp(p, e, sizeof(e) - 1) && (isspace((unsigned char) p[sizeof(e) - 1]) || p[sizeof(e) - 1] == '\0')
    125 #define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
    126 	for (; *p; p = skip(p, &zapchar)) {
    127 		if (scmp(_TTYS_OFF))
    128 			tty.ty_status &= ~TTY_ON;
    129 		else if (scmp(_TTYS_ON))
    130 			tty.ty_status |= TTY_ON;
    131 		else if (scmp(_TTYS_SECURE))
    132 			tty.ty_status |= TTY_SECURE;
    133 		else if (scmp(_TTYS_LOCAL))
    134 			tty.ty_status |= TTY_LOCAL;
    135 		else if (scmp(_TTYS_RTSCTS))
    136 			tty.ty_status |= TTY_RTSCTS;
    137 		else if (scmp(_TTYS_DTRCTS))
    138 			tty.ty_status |= TTY_DTRCTS;
    139 		else if (scmp(_TTYS_SOFTCAR))
    140 			tty.ty_status |= TTY_SOFTCAR;
    141 		else if (scmp(_TTYS_MDMBUF))
    142 			tty.ty_status |= TTY_MDMBUF;
    143 		else if (vcmp(_TTYS_WINDOW))
    144 			tty.ty_window = value(p);
    145 		else if (vcmp(_TTYS_CLASS))
    146 			tty.ty_class = value(p);
    147 		else
    148 			warnx("%s: %s, %lu: unknown option `%s'",
    149 			    __func__, _PATH_TTYS, (unsigned long)lineno, p);
    150 	}
    151 
    152 	if (zapchar == '#' || *p == '#')
    153 		while ((c = *++p) == ' ' || c == '\t')
    154 			continue;
    155 	tty.ty_comment = p;
    156 	if (*p == '\0')
    157 		tty.ty_comment = NULL;
    158 	if ((p = strchr(p, '\n')) != NULL)
    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(char *p, char *zapchar)
    171 {
    172 	char *t;
    173 	int c, q;
    174 
    175 	_DIAGASSERT(p != NULL);
    176 	*zapchar = '\0';
    177 
    178 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
    179 		if (c == '"') {
    180 			q ^= QUOTED;	/* obscure, but nice */
    181 			continue;
    182 		}
    183 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
    184 			p++;
    185 		*t++ = *p;
    186 		if (q == QUOTED)
    187 			continue;
    188 		if (c == '#') {
    189 			*zapchar = c;
    190 			*p = '\0';
    191 			*--t = '\0';
    192 			return p;
    193 		}
    194 		if (c == '\t' || c == ' ' || c == '\n') {
    195 			*zapchar = c;
    196 			*p++ = '\0';
    197 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
    198 				p++;
    199 			*--t = '\0';
    200 			return p;
    201 		}
    202 	}
    203 	if (t != p)
    204 		*t = '\0';
    205 	return p;
    206 }
    207 
    208 static char *
    209 value(char *p)
    210 {
    211 
    212 	_DIAGASSERT(p != NULL);
    213 
    214 	return (p = strchr(p, '=')) != NULL ? ++p : NULL;
    215 }
    216 
    217 int
    218 setttyent(void)
    219 {
    220 	lineno = 0;
    221 	if (tf) {
    222 		rewind(tf);
    223 		return 1;
    224 	} else if ((tf = fopen(_PATH_TTYS, "r")) != NULL)
    225 		return 1;
    226 	return 0;
    227 }
    228 
    229 int
    230 endttyent(void)
    231 {
    232 	int rval;
    233 
    234 	if (tf) {
    235 		rval = !(fclose(tf) == EOF);
    236 		tf = NULL;
    237 		return rval;
    238 	}
    239 	return 1;
    240 }
    241