xref: /netbsd-src/usr.sbin/mtree/spec.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: spec.c,v 1.18 1999/12/01 22:14:46 wennmach 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 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)spec.c	8.2 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: spec.c,v 1.18 1999/12/01 22:14:46 wennmach Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <ctype.h>
48 #include <errno.h>
49 #include <fts.h>
50 #include <grp.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <vis.h>
56 
57 #include "mtree.h"
58 #include "extern.h"
59 
60 int lineno;				/* Current spec line number. */
61 
62 static void	 set __P((char *, NODE *));
63 static void	 unset __P((char *, NODE *));
64 
65 NODE *
66 spec()
67 {
68 	NODE *centry, *last;
69 	char *p;
70 	NODE ginfo, *root;
71 	int c_cur, c_next;
72 	char buf[2048];
73 
74 	root = NULL;
75 	centry = last = NULL;
76 	memset(&ginfo, 0, sizeof(ginfo));
77 	c_cur = c_next = 0;
78 	for (lineno = 1; fgets(buf, sizeof(buf), stdin);
79 	    ++lineno, c_cur = c_next, c_next = 0) {
80 		/* Skip empty lines. */
81 		if (buf[0] == '\n')
82 			continue;
83 
84 		/* Find end of line. */
85 		if ((p = strchr(buf, '\n')) == NULL)
86 			mtree_err("line %d too long", lineno);
87 
88 		/* See if next line is continuation line. */
89 		if (p[-1] == '\\') {
90 			--p;
91 			c_next = 1;
92 		}
93 
94 		/* Null-terminate the line. */
95 		*p = '\0';
96 
97 		/* Skip leading whitespace. */
98 		for (p = buf; *p && isspace((unsigned char)*p); ++p);
99 
100 		/* If nothing but whitespace or comment char, continue. */
101 		if (!*p || *p == '#')
102 			continue;
103 
104 #ifdef DEBUG
105 		(void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
106 #endif
107 		if (c_cur) {
108 			set(p, centry);
109 			continue;
110 		}
111 
112 		/* Grab file name, "$", "set", or "unset". */
113 		if ((p = strtok(p, "\n\t ")) == NULL)
114 			mtree_err("missing field");
115 
116 		if (p[0] == '/')
117 			switch(p[1]) {
118 			case 's':
119 				if (strcmp(p + 1, "set"))
120 					break;
121 				set(NULL, &ginfo);
122 				continue;
123 			case 'u':
124 				if (strcmp(p + 1, "unset"))
125 					break;
126 				unset(NULL, &ginfo);
127 				continue;
128 			}
129 
130 		if (strchr(p, '/'))
131 			mtree_err("slash character in file name");
132 
133 		if (!strcmp(p, "..")) {
134 			/* Don't go up, if haven't gone down. */
135 			if (!root)
136 				goto noparent;
137 			if (last->type != F_DIR || last->flags & F_DONE) {
138 				if (last == root)
139 					goto noparent;
140 				last = last->parent;
141 			}
142 			last->flags |= F_DONE;
143 			continue;
144 
145 noparent:		mtree_err("no parent node");
146 		}
147 
148 		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
149 			mtree_err("%s", strerror(errno));
150 		*centry = ginfo;
151                 if (strunvis(centry->name, p) == -1)
152 			mtree_err("strunvis failed on %s", p);
153 #define	MAGIC	"?*["
154 		if (strpbrk(p, MAGIC))
155 			centry->flags |= F_MAGIC;
156 		set(NULL, centry);
157 
158 		if (!root) {
159 			last = root = centry;
160 			root->parent = root;
161 		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
162 			centry->parent = last;
163 			last = last->child = centry;
164 		} else {
165 			centry->parent = last->parent;
166 			centry->prev = last;
167 			last = last->next = centry;
168 		}
169 	}
170 	return (root);
171 }
172 
173 static void
174 set(t, ip)
175 	char *t;
176 	NODE *ip;
177 {
178 	int type;
179 	char *kw, *val, *md;
180 	struct group *gr;
181 	struct passwd *pw;
182 	mode_t *m;
183 	int value;
184 	char *ep;
185 
186 	val = NULL;
187 	for (; (kw = strtok(t, "= \t\n")) != NULL; t = NULL) {
188 		ip->flags |= type = parsekey(kw, &value);
189 		if (value && (val = strtok(NULL, " \t\n")) == NULL)
190 			mtree_err("missing value");
191 		switch(type) {
192 		case F_CKSUM:
193 			ip->cksum = strtoul(val, &ep, 10);
194 			if (*ep)
195 				mtree_err("invalid checksum %s", val);
196 			break;
197 		case F_FLAGS:
198 			if (strcmp("none", val) == 0)
199 				ip->st_flags = 0;
200 			else if (string_to_flags(&val, &ip->st_flags, NULL) != 0)
201 				mtree_err("invalid flag %s", val);
202 			break;
203 		case F_GID:
204 			ip->st_gid = (gid_t)strtoul(val, &ep, 10);
205 			if (*ep)
206 				mtree_err("invalid gid %s", val);
207 			break;
208 		case F_GNAME:
209 			if ((gr = getgrnam(val)) == NULL)
210 			    mtree_err("unknown group %s", val);
211 			ip->st_gid = gr->gr_gid;
212 			break;
213 		case F_IGN:
214 			/* just set flag bit */
215 			break;
216 		case F_MD5:
217 			if (val[0]=='0' && val[1]=='x')
218 				md=&val[2];
219 			else
220 				md=val;
221 			if ((ip->md5sum = strdup(md)) == NULL)
222 				mtree_err("memory allocation error");
223 			break;
224 		case F_MODE:
225 			if ((m = setmode(val)) == NULL)
226 				mtree_err("invalid file mode %s", val);
227 			ip->st_mode = getmode(m, 0);
228 			free(m);
229 			break;
230 		case F_NLINK:
231 			ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
232 			if (*ep)
233 				mtree_err("invalid link count %s", val);
234 			break;
235 		case F_SIZE:
236 			ip->st_size = (off_t)strtoq(val, &ep, 10);
237 			if (*ep)
238 				mtree_err("invalid size %s", val);
239 			break;
240 		case F_SLINK:
241 			if ((ip->slink = strdup(val)) == NULL)
242 				mtree_err("memory allocation error");
243 			break;
244 		case F_TIME:
245 #ifndef __APPLE__
246 			ip->st_mtimespec.tv_sec =
247 			    (time_t)strtoul(val, &ep, 10);
248 #else
249 			ip->st_mtimespec.ts_sec =
250 			    (time_t)strtoul(val, &ep, 10);
251 #endif
252 			if (*ep != '.')
253 				mtree_err("invalid time %s", val);
254 			val = ep + 1;
255 #ifndef __APPLE__
256 			ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
257 #else
258 			ip->st_mtimespec.ts_nsec = strtol(val, &ep, 10);
259 #endif
260 			if (*ep)
261 				mtree_err("invalid time %s", val);
262 			break;
263 		case F_TYPE:
264 			switch(*val) {
265 			case 'b':
266 				if (!strcmp(val, "block"))
267 					ip->type = F_BLOCK;
268 				break;
269 			case 'c':
270 				if (!strcmp(val, "char"))
271 					ip->type = F_CHAR;
272 				break;
273 			case 'd':
274 				if (!strcmp(val, "dir"))
275 					ip->type = F_DIR;
276 				break;
277 			case 'f':
278 				if (!strcmp(val, "file"))
279 					ip->type = F_FILE;
280 				if (!strcmp(val, "fifo"))
281 					ip->type = F_FIFO;
282 				break;
283 			case 'l':
284 				if (!strcmp(val, "link"))
285 					ip->type = F_LINK;
286 				break;
287 			case 's':
288 				if (!strcmp(val, "socket"))
289 					ip->type = F_SOCK;
290 				break;
291 			default:
292 				mtree_err("unknown file type %s", val);
293 			}
294 			break;
295 		case F_UID:
296 			ip->st_uid = (uid_t)strtoul(val, &ep, 10);
297 			if (*ep)
298 				mtree_err("invalid uid %s", val);
299 			break;
300 		case F_UNAME:
301 			if ((pw = getpwnam(val)) == NULL)
302 			    mtree_err("unknown user %s", val);
303 			ip->st_uid = pw->pw_uid;
304 			break;
305 		}
306 	}
307 }
308 
309 static void
310 unset(t, ip)
311 	char *t;
312 	NODE *ip;
313 {
314 	char *p;
315 
316 	while ((p = strtok(t, "\n\t ")) != NULL)
317 		ip->flags &= ~parsekey(p, NULL);
318 }
319