spec.c revision 1.10
1217309Snwhitehorn/*	$NetBSD: spec.c,v 1.10 1997/10/17 05:24:32 mrg Exp $	*/
2251843Sbapt
3217309Snwhitehorn/*-
4217309Snwhitehorn * Copyright (c) 1989, 1993
5217309Snwhitehorn *	The Regents of the University of California.  All rights reserved.
6217309Snwhitehorn *
7251843Sbapt * Redistribution and use in source and binary forms, with or without
8217309Snwhitehorn * modification, are permitted provided that the following conditions
9217309Snwhitehorn * are met:
10217309Snwhitehorn * 1. Redistributions of source code must retain the above copyright
11217309Snwhitehorn *    notice, this list of conditions and the following disclaimer.
12217309Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
13217309Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
14217309Snwhitehorn *    documentation and/or other materials provided with the distribution.
15217309Snwhitehorn * 3. All advertising materials mentioning features or use of this software
16217309Snwhitehorn *    must display the following acknowledgement:
17217309Snwhitehorn *	This product includes software developed by the University of
18217309Snwhitehorn *	California, Berkeley and its contributors.
19217309Snwhitehorn * 4. Neither the name of the University nor the names of its contributors
20217309Snwhitehorn *    may be used to endorse or promote products derived from this software
21217309Snwhitehorn *    without specific prior written permission.
22217309Snwhitehorn *
23217309Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24217309Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25217309Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26217309Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27217309Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28217309Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29217309Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30217309Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31217309Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32217309Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33217309Snwhitehorn * SUCH DAMAGE.
34217309Snwhitehorn */
35217309Snwhitehorn
36217309Snwhitehorn#ifndef lint
37217309Snwhitehorn#if 0
38217309Snwhitehornstatic char sccsid[] = "@(#)spec.c	8.2 (Berkeley) 4/28/95";
39217309Snwhitehorn#else
40217309Snwhitehornstatic char rcsid[] = "$NetBSD: spec.c,v 1.10 1997/10/17 05:24:32 mrg Exp $";
41217309Snwhitehorn#endif
42217309Snwhitehorn#endif /* not lint */
43217309Snwhitehorn
44217309Snwhitehorn#include <sys/types.h>
45217309Snwhitehorn#include <sys/stat.h>
46217309Snwhitehorn#include <fts.h>
47217309Snwhitehorn#include <pwd.h>
48217309Snwhitehorn#include <grp.h>
49251843Sbapt#include <errno.h>
50217309Snwhitehorn#include <unistd.h>
51217309Snwhitehorn#include <stdio.h>
52217309Snwhitehorn#include <ctype.h>
53217309Snwhitehorn#include "mtree.h"
54217309Snwhitehorn#include "extern.h"
55217309Snwhitehorn
56217309Snwhitehornint lineno;				/* Current spec line number. */
57217309Snwhitehorn
58217309Snwhitehornstatic void	 set __P((char *, NODE *));
59217309Snwhitehornstatic void	 unset __P((char *, NODE *));
60217309Snwhitehorn
61217309SnwhitehornNODE *
62251843Sbaptspec()
63251843Sbapt{
64217309Snwhitehorn	register NODE *centry, *last;
65217309Snwhitehorn	register char *p;
66217309Snwhitehorn	NODE ginfo, *root;
67217309Snwhitehorn	int c_cur, c_next;
68251843Sbapt	char buf[2048];
69217309Snwhitehorn
70251843Sbapt	root = NULL;
71251843Sbapt	bzero(&ginfo, sizeof(ginfo));
72251843Sbapt	c_cur = c_next = 0;
73251843Sbapt	for (lineno = 1; fgets(buf, sizeof(buf), stdin);
74217309Snwhitehorn	    ++lineno, c_cur = c_next, c_next = 0) {
75217309Snwhitehorn		/* Skip empty lines. */
76217309Snwhitehorn		if (buf[0] == '\n')
77217309Snwhitehorn			continue;
78217309Snwhitehorn
79217309Snwhitehorn		/* Find end of line. */
80217309Snwhitehorn		if ((p = index(buf, '\n')) == NULL)
81217309Snwhitehorn			err("line %d too long", lineno);
82217309Snwhitehorn
83217309Snwhitehorn		/* See if next line is continuation line. */
84217309Snwhitehorn		if (p[-1] == '\\') {
85217309Snwhitehorn			--p;
86217309Snwhitehorn			c_next = 1;
87217309Snwhitehorn		}
88217309Snwhitehorn
89217309Snwhitehorn		/* Null-terminate the line. */
90217309Snwhitehorn		*p = '\0';
91217309Snwhitehorn
92217309Snwhitehorn		/* Skip leading whitespace. */
93217309Snwhitehorn		for (p = buf; *p && isspace(*p); ++p);
94217309Snwhitehorn
95217309Snwhitehorn		/* If nothing but whitespace or comment char, continue. */
96217309Snwhitehorn		if (!*p || *p == '#')
97217309Snwhitehorn			continue;
98217309Snwhitehorn
99217309Snwhitehorn#ifdef DEBUG
100217309Snwhitehorn		(void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
101217309Snwhitehorn#endif
102217309Snwhitehorn		if (c_cur) {
103217309Snwhitehorn			set(p, centry);
104217309Snwhitehorn			continue;
105251843Sbapt		}
106217309Snwhitehorn
107217309Snwhitehorn		/* Grab file name, "$", "set", or "unset". */
108217309Snwhitehorn		if ((p = strtok(p, "\n\t ")) == NULL)
109217309Snwhitehorn			err("missing field");
110217309Snwhitehorn
111220749Snwhitehorn		if (p[0] == '/')
112220749Snwhitehorn			switch(p[1]) {
113220749Snwhitehorn			case 's':
114220749Snwhitehorn				if (strcmp(p + 1, "set"))
115220749Snwhitehorn					break;
116224014Snwhitehorn				set(NULL, &ginfo);
117220749Snwhitehorn				continue;
118251843Sbapt			case 'u':
119220749Snwhitehorn				if (strcmp(p + 1, "unset"))
120220749Snwhitehorn					break;
121220749Snwhitehorn				unset(NULL, &ginfo);
122220749Snwhitehorn				continue;
123251843Sbapt			}
124220749Snwhitehorn
125220749Snwhitehorn		if (index(p, '/'))
126220749Snwhitehorn			err("slash character in file name");
127220749Snwhitehorn
128251843Sbapt		if (!strcmp(p, "..")) {
129251843Sbapt			/* Don't go up, if haven't gone down. */
130220749Snwhitehorn			if (!root)
131251843Sbapt				goto noparent;
132251843Sbapt			if (last->type != F_DIR || last->flags & F_DONE) {
133251843Sbapt				if (last == root)
134220749Snwhitehorn					goto noparent;
135220749Snwhitehorn				last = last->parent;
136220749Snwhitehorn			}
137251843Sbapt			last->flags |= F_DONE;
138220749Snwhitehorn			continue;
139220749Snwhitehorn
140220749Snwhitehornnoparent:		err("no parent node");
141251843Sbapt		}
142251843Sbapt
143251843Sbapt		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
144251843Sbapt			err("%s", strerror(errno));
145251843Sbapt		*centry = ginfo;
146251843Sbapt		(void)strcpy(centry->name, p);
147251843Sbapt#define	MAGIC	"?*["
148251843Sbapt		if (strpbrk(p, MAGIC))
149251843Sbapt			centry->flags |= F_MAGIC;
150251843Sbapt		set(NULL, centry);
151220749Snwhitehorn
152220749Snwhitehorn		if (!root) {
153220749Snwhitehorn			last = root = centry;
154220749Snwhitehorn			root->parent = root;
155220749Snwhitehorn		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
156251843Sbapt			centry->parent = last;
157220749Snwhitehorn			last = last->child = centry;
158220749Snwhitehorn		} else {
159220749Snwhitehorn			centry->parent = last->parent;
160220749Snwhitehorn			centry->prev = last;
161220749Snwhitehorn			last = last->next = centry;
162251843Sbapt		}
163251843Sbapt	}
164251843Sbapt	return (root);
165220749Snwhitehorn}
166251843Sbapt
167251843Sbaptstatic void
168251843Sbaptset(t, ip)
169220749Snwhitehorn	char *t;
170251843Sbapt	register NODE *ip;
171251843Sbapt{
172220749Snwhitehorn	register int type;
173220749Snwhitehorn	register char *kw, *val;
174251843Sbapt	struct group *gr;
175251843Sbapt	struct passwd *pw;
176251843Sbapt	mode_t *m;
177251843Sbapt	int value;
178251843Sbapt	char *ep;
179251843Sbapt
180251843Sbapt	for (; (kw = strtok(t, "= \t\n")) != NULL; t = NULL) {
181220749Snwhitehorn		ip->flags |= type = parsekey(kw, &value);
182220749Snwhitehorn		if (value && (val = strtok(NULL, " \t\n")) == NULL)
183251843Sbapt			err("missing value");
184220749Snwhitehorn		switch(type) {
185220749Snwhitehorn		case F_CKSUM:
186220749Snwhitehorn			ip->cksum = strtoul(val, &ep, 10);
187220749Snwhitehorn			if (*ep)
188220749Snwhitehorn				err("invalid checksum %s", val);
189251843Sbapt			break;
190251843Sbapt		case F_GID:
191220749Snwhitehorn			ip->st_gid = (gid_t)strtoul(val, &ep, 10);
192220749Snwhitehorn			if (*ep)
193220749Snwhitehorn				err("invalid gid %s", val);
194217309Snwhitehorn			break;
195220749Snwhitehorn		case F_GNAME:
196220749Snwhitehorn			if ((gr = getgrnam(val)) == NULL)
197220749Snwhitehorn			    err("unknown group %s", val);
198220749Snwhitehorn			ip->st_gid = gr->gr_gid;
199220749Snwhitehorn			break;
200220749Snwhitehorn		case F_IGN:
201217309Snwhitehorn			/* just set flag bit */
202217309Snwhitehorn			break;
203217309Snwhitehorn		case F_MODE:
204217309Snwhitehorn			if ((m = setmode(val)) == NULL)
205217309Snwhitehorn				err("invalid file mode %s", val);
206217309Snwhitehorn			ip->st_mode = getmode(m, 0);
207220749Snwhitehorn			break;
208217309Snwhitehorn		case F_NLINK:
209217309Snwhitehorn			ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
210217309Snwhitehorn			if (*ep)
211217309Snwhitehorn				err("invalid link count %s", val);
212217309Snwhitehorn			break;
213217309Snwhitehorn		case F_SIZE:
214217309Snwhitehorn			ip->st_size = (off_t)strtoq(val, &ep, 10);
215217309Snwhitehorn			if (*ep)
216217309Snwhitehorn				err("invalid size %s", val);
217217309Snwhitehorn			break;
218217309Snwhitehorn		case F_SLINK:
219217309Snwhitehorn			if ((ip->slink = strdup(val)) == NULL)
220251843Sbapt				err("%s", strerror(errno));
221217309Snwhitehorn			break;
222224014Snwhitehorn		case F_TIME:
223217309Snwhitehorn			ip->st_mtimespec.tv_sec =
224217309Snwhitehorn			    (time_t)strtoul(val, &ep, 10);
225217309Snwhitehorn			if (*ep != '.')
226217309Snwhitehorn				err("invalid time %s", val);
227251843Sbapt			val = ep + 1;
228217309Snwhitehorn			ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
229217309Snwhitehorn			if (*ep)
230251843Sbapt				err("invalid time %s", val);
231217309Snwhitehorn			break;
232217309Snwhitehorn		case F_TYPE:
233217309Snwhitehorn			switch(*val) {
234217309Snwhitehorn			case 'b':
235217309Snwhitehorn				if (!strcmp(val, "block"))
236217309Snwhitehorn					ip->type = F_BLOCK;
237217309Snwhitehorn				break;
238217309Snwhitehorn			case 'c':
239217309Snwhitehorn				if (!strcmp(val, "char"))
240217309Snwhitehorn					ip->type = F_CHAR;
241217309Snwhitehorn				break;
242217309Snwhitehorn			case 'd':
243217309Snwhitehorn				if (!strcmp(val, "dir"))
244217309Snwhitehorn					ip->type = F_DIR;
245217309Snwhitehorn				break;
246217309Snwhitehorn			case 'f':
247217309Snwhitehorn				if (!strcmp(val, "file"))
248217309Snwhitehorn					ip->type = F_FILE;
249217309Snwhitehorn				if (!strcmp(val, "fifo"))
250217309Snwhitehorn					ip->type = F_FIFO;
251217309Snwhitehorn				break;
252220749Snwhitehorn			case 'l':
253217309Snwhitehorn				if (!strcmp(val, "link"))
254220749Snwhitehorn					ip->type = F_LINK;
255217309Snwhitehorn				break;
256217309Snwhitehorn			case 's':
257217309Snwhitehorn				if (!strcmp(val, "socket"))
258217309Snwhitehorn					ip->type = F_SOCK;
259217309Snwhitehorn				break;
260217309Snwhitehorn			default:
261217309Snwhitehorn				err("unknown file type %s", val);
262217309Snwhitehorn			}
263217309Snwhitehorn			break;
264217309Snwhitehorn		case F_UID:
265217309Snwhitehorn			ip->st_uid = (uid_t)strtoul(val, &ep, 10);
266217309Snwhitehorn			if (*ep)
267217309Snwhitehorn				err("invalid uid %s", val);
268217309Snwhitehorn			break;
269251843Sbapt		case F_UNAME:
270217309Snwhitehorn			if ((pw = getpwnam(val)) == NULL)
271217309Snwhitehorn			    err("unknown user %s", val);
272217309Snwhitehorn			ip->st_uid = pw->pw_uid;
273220749Snwhitehorn			break;
274220749Snwhitehorn		}
275220749Snwhitehorn	}
276220749Snwhitehorn}
277220749Snwhitehorn
278220749Snwhitehornstatic void
279220749Snwhitehornunset(t, ip)
280220749Snwhitehorn	char *t;
281220749Snwhitehorn	register NODE *ip;
282220749Snwhitehorn{
283220749Snwhitehorn	register char *p;
284217309Snwhitehorn
285217309Snwhitehorn	while ((p = strtok(t, "\n\t ")) != NULL)
286217309Snwhitehorn		ip->flags &= ~parsekey(p, NULL);
287217309Snwhitehorn}
288251843Sbapt