spec.c revision 42561
1/*-
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)spec.c	8.1 (Berkeley) 6/6/93";
37#endif
38static const char rcsid[] =
39	"$Id: spec.c,v 1.8 1998/12/16 04:54:08 imp Exp $";
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <ctype.h>
45#include <err.h>
46#include <errno.h>
47#include <fts.h>
48#include <grp.h>
49#include <pwd.h>
50#include <stdio.h>
51#include <unistd.h>
52#include <vis.h>
53#include "mtree.h"
54#include "extern.h"
55
56int lineno;				/* Current spec line number. */
57
58static void	 set __P((char *, NODE *));
59static void	 unset __P((char *, NODE *));
60
61NODE *
62spec()
63{
64	register NODE *centry, *last;
65	register char *p;
66	NODE ginfo, *root;
67	int c_cur, c_next;
68	char buf[2048];
69
70	centry = last = root = NULL;
71	bzero(&ginfo, sizeof(ginfo));
72	c_cur = c_next = 0;
73	for (lineno = 1; fgets(buf, sizeof(buf), stdin);
74	    ++lineno, c_cur = c_next, c_next = 0) {
75		/* Skip empty lines. */
76		if (buf[0] == '\n')
77			continue;
78
79		/* Find end of line. */
80		if ((p = index(buf, '\n')) == NULL)
81			errx(1, "line %d too long", lineno);
82
83		/* See if next line is continuation line. */
84		if (p[-1] == '\\') {
85			--p;
86			c_next = 1;
87		}
88
89		/* Null-terminate the line. */
90		*p = '\0';
91
92		/* Skip leading whitespace. */
93		for (p = buf; *p && isspace(*p); ++p);
94
95		/* If nothing but whitespace or comment char, continue. */
96		if (!*p || *p == '#')
97			continue;
98
99#ifdef DEBUG
100		(void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
101#endif
102		if (c_cur) {
103			set(p, centry);
104			continue;
105		}
106
107		/* Grab file name, "$", "set", or "unset". */
108		if ((p = strtok(p, "\n\t ")) == NULL)
109			errx(1, "line %d: missing field", lineno);
110
111		if (p[0] == '/')
112			switch(p[1]) {
113			case 's':
114				if (strcmp(p + 1, "set"))
115					break;
116				set(NULL, &ginfo);
117				continue;
118			case 'u':
119				if (strcmp(p + 1, "unset"))
120					break;
121				unset(NULL, &ginfo);
122				continue;
123			}
124
125		if (index(p, '/'))
126			errx(1, "line %d: slash character in file name",
127			lineno);
128
129		if (!strcmp(p, "..")) {
130			/* Don't go up, if haven't gone down. */
131			if (!root)
132				goto noparent;
133			if (last->type != F_DIR || last->flags & F_DONE) {
134				if (last == root)
135					goto noparent;
136				last = last->parent;
137			}
138			last->flags |= F_DONE;
139			continue;
140
141noparent:		errx(1, "line %d: no parent node", lineno);
142		}
143
144		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
145			errx(1, "calloc");
146		*centry = ginfo;
147#define	MAGIC	"?*["
148		if (strpbrk(p, MAGIC))
149			centry->flags |= F_MAGIC;
150		if (strunvis(centry->name, p) == -1) {
151			warnx("filename %s is ill-encoded and literally used",
152			    p);
153			strcpy(centry->name, p);
154		}
155		set(NULL, centry);
156
157		if (!root) {
158			last = root = centry;
159			root->parent = root;
160		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
161			centry->parent = last;
162			last = last->child = centry;
163		} else {
164			centry->parent = last->parent;
165			centry->prev = last;
166			last = last->next = centry;
167		}
168	}
169	return (root);
170}
171
172static void
173set(t, ip)
174	char *t;
175	register NODE *ip;
176{
177	register int type;
178	register char *kw, *val = NULL;
179	struct group *gr;
180	struct passwd *pw;
181	mode_t *m;
182	int value;
183	char *ep;
184
185	for (; (kw = strtok(t, "= \t\n")); t = NULL) {
186		ip->flags |= type = parsekey(kw, &value);
187		if (value && (val = strtok(NULL, " \t\n")) == NULL)
188			errx(1, "line %d: missing value", lineno);
189		switch(type) {
190		case F_CKSUM:
191			ip->cksum = strtoul(val, &ep, 10);
192			if (*ep)
193				errx(1, "line %d: invalid checksum %s",
194				lineno, val);
195			break;
196		case F_MD5:
197			ip->md5digest = strdup(val);
198			if(!ip->md5digest) {
199				errx(1, "strdup");
200			}
201			break;
202		case F_GID:
203			ip->st_gid = strtoul(val, &ep, 10);
204			if (*ep)
205				errx(1, "line %d: invalid gid %s", lineno, val);
206			break;
207		case F_GNAME:
208			if ((gr = getgrnam(val)) == NULL)
209			    errx(1, "line %d: unknown group %s", lineno, val);
210			ip->st_gid = gr->gr_gid;
211			break;
212		case F_IGN:
213			/* just set flag bit */
214			break;
215		case F_MODE:
216			if ((m = setmode(val)) == NULL)
217				errx(1, "line %d: invalid file mode %s",
218				lineno, val);
219			ip->st_mode = getmode(m, 0);
220			free(m);
221			break;
222		case F_NLINK:
223			ip->st_nlink = strtoul(val, &ep, 10);
224			if (*ep)
225				errx(1, "line %d: invalid link count %s",
226				lineno,  val);
227			break;
228		case F_SIZE:
229			ip->st_size = strtoq(val, &ep, 10);
230			if (*ep)
231				errx(1, "line %d: invalid size %s",
232				lineno, val);
233			break;
234		case F_SLINK:
235			if ((ip->slink = strdup(val)) == NULL)
236				errx(1, "strdup");
237			break;
238		case F_TIME:
239			ip->st_mtimespec.tv_sec = strtoul(val, &ep, 10);
240			if (*ep != '.')
241				errx(1, "line %d: invalid time %s",
242				lineno, val);
243			val = ep + 1;
244			ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10);
245			if (*ep)
246				errx(1, "line %d: invalid time %s",
247				lineno, val);
248			break;
249		case F_TYPE:
250			switch(*val) {
251			case 'b':
252				if (!strcmp(val, "block"))
253					ip->type = F_BLOCK;
254				break;
255			case 'c':
256				if (!strcmp(val, "char"))
257					ip->type = F_CHAR;
258				break;
259			case 'd':
260				if (!strcmp(val, "dir"))
261					ip->type = F_DIR;
262				break;
263			case 'f':
264				if (!strcmp(val, "file"))
265					ip->type = F_FILE;
266				if (!strcmp(val, "fifo"))
267					ip->type = F_FIFO;
268				break;
269			case 'l':
270				if (!strcmp(val, "link"))
271					ip->type = F_LINK;
272				break;
273			case 's':
274				if (!strcmp(val, "socket"))
275					ip->type = F_SOCK;
276				break;
277			default:
278				errx(1, "line %d: unknown file type %s",
279				lineno, val);
280			}
281			break;
282		case F_UID:
283			ip->st_uid = strtoul(val, &ep, 10);
284			if (*ep)
285				errx(1, "line %d: invalid uid %s", lineno, val);
286			break;
287		case F_UNAME:
288			if ((pw = getpwnam(val)) == NULL)
289			    errx(1, "line %d: unknown user %s", lineno, val);
290			ip->st_uid = pw->pw_uid;
291			break;
292		}
293	}
294}
295
296static void
297unset(t, ip)
298	char *t;
299	register NODE *ip;
300{
301	register char *p;
302
303	while ((p = strtok(t, "\n\t ")))
304		ip->flags &= ~parsekey(p, NULL);
305}
306