xref: /dragonfly/usr.sbin/config/main.c (revision 28c7b939)
1 /*
2  * Copyright (c) 1980, 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  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)main.c	8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.sbin/config/main.c,v 1.37.2.3 2001/06/13 00:25:53 cg Exp $
36  * $DragonFly: src/usr.sbin/config/main.c,v 1.7 2004/01/31 03:26:56 dillon Exp $
37  */
38 
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/file.h>
42 #include <sys/mman.h>
43 #include <sys/param.h>
44 #include <ctype.h>
45 #include <err.h>
46 #include <stdio.h>
47 #include <sysexits.h>
48 #include <unistd.h>
49 #include "y.tab.h"
50 #include "config.h"
51 
52 #ifndef TRUE
53 #define TRUE	(1)
54 #endif
55 
56 #ifndef FALSE
57 #define FALSE	(0)
58 #endif
59 
60 #define	CDIR	"../../compile/"
61 
62 char *	PREFIX;
63 char 	destdir[MAXPATHLEN];
64 char 	srcdir[MAXPATHLEN];
65 
66 static int no_config_clobber = TRUE;
67 int	debugging;
68 int	profiling;
69 
70 static void configfile(void);
71 static void get_srcdir(void);
72 static void usage(void);
73 
74 /*
75  * Config builds a set of files for building a UNIX
76  * system given a description of the desired system.
77  */
78 int
79 main(int argc, char **argv)
80 {
81 
82 	struct stat buf;
83 	int ch, len;
84 	char *p;
85 
86 	while ((ch = getopt(argc, argv, "d:gprn")) != -1)
87 		switch (ch) {
88 		case 'd':
89 			if (*destdir == '\0')
90 				strlcpy(destdir, optarg, sizeof(destdir));
91 			else
92 				errx(2, "directory already set");
93 			break;
94 		case 'g':
95 			debugging++;
96 			break;
97 		case 'p':
98 			profiling++;
99 			break;
100 		case 'n':
101 			/* no_config_clobber is now true by default, no-op */
102 			fprintf(stderr,
103 				"*** Using obsolete config option '-n' ***\n");
104 			break;
105 		case 'r':
106 			no_config_clobber = FALSE;
107 			break;
108 		case '?':
109 		default:
110 			usage();
111 		}
112 	argc -= optind;
113 	argv += optind;
114 
115 	if (argc != 1)
116 		usage();
117 
118 	if (freopen(PREFIX = *argv, "r", stdin) == NULL)
119 		err(2, "%s", PREFIX);
120 
121 	if (*destdir != '\0') {
122 		len = strlen(destdir);
123 		while (len > 1 && destdir[len - 1] == '/')
124 			destdir[--len] = '\0';
125 		get_srcdir();
126 	} else {
127 		strlcpy(destdir, CDIR, sizeof(destdir));
128 		strlcat(destdir, PREFIX, sizeof(destdir));
129 	}
130 
131 	p = path((char *)NULL);
132 	if (stat(p, &buf)) {
133 		if (mkdir(p, 0777))
134 			err(2, "%s", p);
135 	}
136 	else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
137 		errx(2, "%s isn't a directory", p);
138 	}
139 	else if (!no_config_clobber) {
140 		char tmp[strlen(p) + 8];
141 
142 		fprintf(stderr, "Removing old directory %s:  ", p);
143 		fflush(stderr);
144 		snprintf(tmp, sizeof(tmp), "rm -rf %s", p);
145 		if (system(tmp)) {
146 			fprintf(stderr, "Failed!\n");
147 			err(2, "%s", tmp);
148 		}
149 		fprintf(stderr, "Done.\n");
150 		if (mkdir(p, 0777))
151 			err(2, "%s", p);
152 	}
153 
154 	dtab = NULL;
155 	if (yyparse())
156 		exit(3);
157 	if (machinename == NULL) {
158 		printf("Specify machine type, e.g. ``machine i386''\n");
159 		exit(1);
160 	}
161 	newbus_ioconf();
162 	/*
163 	 * make symbolic links in compilation directory
164 	 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
165 	 * and similarly for "machine".
166 	 */
167 	{
168 	char xxx[MAXPATHLEN];
169 	if (*srcdir == '\0')
170 		(void)snprintf(xxx, sizeof(xxx), "../../%s/include",
171 		    machinename);
172 	else
173 		(void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
174 		    srcdir, machinename);
175 	(void) symlink(xxx, path("machine"));
176 	}
177 
178 	/*
179 	 * XXX check directory structure for architecture subdirectories and
180 	 * create the symlinks automatically XXX
181 	 */
182 	{
183 	char xxx[MAXPATHLEN];
184 	if (*srcdir == '\0')
185 		(void)snprintf(xxx, sizeof(xxx), "../../../../../net/i4b/include/%s",
186 		    machinename);
187 	else
188 		(void)snprintf(xxx, sizeof(xxx), "%s/net/i4b/include/%s",
189 		    srcdir, machinename);
190 	(void) mkdir(path("net"), 0755);
191 	(void) mkdir(path("net/i4b"), 0755);
192 	(void) mkdir(path("net/i4b/include"), 0755);
193 	(void) symlink(xxx, path("net/i4b/include/machine"));
194 	}
195 
196 	{
197 	    static char *ary[] = { "linux", "svr4" };
198 	    char xxx[MAXPATHLEN];
199 	    char yyy[64];
200 	    int i;
201 
202 	    for (i = 0; i < sizeof(ary)/sizeof(ary[0]); ++i) {
203 		if (*srcdir == 0)  {
204 		    snprintf(xxx, sizeof(xxx), "../../emulation/%s/%s",
205 			ary[i], machinename);
206 		} else {
207 		    snprintf(xxx, sizeof(xxx), "%s/emulation/%s/%s",
208 			srcdir, ary[i], machinename);
209 		}
210 		snprintf(yyy, sizeof(yyy), "arch_%s", ary[i]);
211 		symlink(xxx, path(yyy));
212 	    }
213 	}
214 
215 	options();			/* make options .h files */
216 	makefile();			/* build Makefile */
217 	headers();			/* make a lot of .h files */
218 	configfile();			/* put config file into kernel*/
219 	printf("Kernel build directory is %s\n", p);
220 	exit(0);
221 }
222 
223 /*
224  * get_srcdir
225  *	determine the root of the kernel source tree
226  *	and save that in srcdir.
227  */
228 static void
229 get_srcdir(void)
230 {
231 	if (realpath("../..", srcdir) == NULL)
232 		errx(2, "Unable to find root of source tree");
233 }
234 
235 static void
236 usage(void)
237 {
238 		fprintf(stderr, "usage: config [-gpr] [-d destdir] sysname\n");
239 		exit(1);
240 }
241 
242 /*
243  * get_word
244  *	returns EOF on end of file
245  *	NULL on end of line
246  *	pointer to the word otherwise
247  */
248 char *
249 get_word(register FILE *fp)
250 {
251 	static char line[80];
252 	register int ch;
253 	register char *cp;
254 	int escaped_nl = 0;
255 
256 begin:
257 	while ((ch = getc(fp)) != EOF)
258 		if (ch != ' ' && ch != '\t')
259 			break;
260 	if (ch == EOF)
261 		return ((char *)EOF);
262 	if (ch == '\\'){
263 		escaped_nl = 1;
264 		goto begin;
265 	}
266 	if (ch == '\n') {
267 		if (escaped_nl){
268 			escaped_nl = 0;
269 			goto begin;
270 		}
271 		else
272 			return (NULL);
273 	}
274 	cp = line;
275 	*cp++ = ch;
276 	while ((ch = getc(fp)) != EOF) {
277 		if (isspace(ch))
278 			break;
279 		*cp++ = ch;
280 	}
281 	*cp = 0;
282 	if (ch == EOF)
283 		return ((char *)EOF);
284 	(void) ungetc(ch, fp);
285 	return (line);
286 }
287 
288 /*
289  * get_quoted_word
290  *	like get_word but will accept something in double or single quotes
291  *	(to allow embedded spaces).
292  */
293 char *
294 get_quoted_word(register FILE *fp)
295 {
296 	static char line[256];
297 	register int ch;
298 	register char *cp;
299 	int escaped_nl = 0;
300 
301 begin:
302 	while ((ch = getc(fp)) != EOF)
303 		if (ch != ' ' && ch != '\t')
304 			break;
305 	if (ch == EOF)
306 		return ((char *)EOF);
307 	if (ch == '\\'){
308 		escaped_nl = 1;
309 		goto begin;
310 	}
311 	if (ch == '\n') {
312 		if (escaped_nl){
313 			escaped_nl = 0;
314 			goto begin;
315 		}
316 		else
317 			return (NULL);
318 	}
319 	cp = line;
320 	if (ch == '"' || ch == '\'') {
321 		register int quote = ch;
322 
323 		while ((ch = getc(fp)) != EOF) {
324 			if (ch == quote)
325 				break;
326 			if (ch == '\n') {
327 				*cp = 0;
328 				printf("config: missing quote reading `%s'\n",
329 					line);
330 				exit(2);
331 			}
332 			*cp++ = ch;
333 		}
334 	} else {
335 		*cp++ = ch;
336 		while ((ch = getc(fp)) != EOF) {
337 			if (isspace(ch))
338 				break;
339 			*cp++ = ch;
340 		}
341 		if (ch != EOF)
342 			(void) ungetc(ch, fp);
343 	}
344 	*cp = 0;
345 	if (ch == EOF)
346 		return ((char *)EOF);
347 	return (line);
348 }
349 
350 /*
351  * prepend the path to a filename
352  */
353 char *
354 path(char *file)
355 {
356 	register char *cp;
357 
358 	cp = malloc((size_t)(strlen(destdir) + (file ? strlen(file) : 0) + 2));
359 	(void) strcpy(cp, destdir);
360 	if (file) {
361 		(void) strcat(cp, "/");
362 		(void) strcat(cp, file);
363 	}
364 	return (cp);
365 }
366 
367 static void
368 configfile(void)
369 {
370 	FILE *fi, *fo;
371 	char *p;
372 	int i;
373 
374 	fi = fopen(PREFIX, "r");
375 	if (!fi)
376 		err(2, "%s", PREFIX);
377 	fo = fopen(p=path("config.c.new"), "w");
378 	if (!fo)
379 		err(2, "%s", p);
380 	fprintf(fo, "#include \"opt_config.h\"\n");
381 	fprintf(fo, "#ifdef INCLUDE_CONFIG_FILE \n");
382 	fprintf(fo, "static const char config[] = \"\\\n");
383 	fprintf(fo, "START CONFIG FILE %s\\n\\\n___", PREFIX);
384 	while (EOF != (i=getc(fi))) {
385 		if (i == '\n') {
386 			fprintf(fo, "\\n\\\n___");
387 		} else if (i == '\"') {
388 			fprintf(fo, "\\\"");
389 		} else if (i == '\\') {
390 			fprintf(fo, "\\\\");
391 		} else {
392 			putc(i, fo);
393 		}
394 	}
395 	fprintf(fo, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX);
396 	fprintf(fo, "\";\n");
397 	fprintf(fo, "\n#endif /* INCLUDE_CONFIG_FILE */\n");
398 	fclose(fi);
399 	fclose(fo);
400 	moveifchanged(path("config.c.new"), path("config.c"));
401 }
402 
403 /*
404  * moveifchanged --
405  *	compare two files; rename if changed.
406  */
407 void
408 moveifchanged(const char *from_name, const char *to_name)
409 {
410 	char *p, *q;
411 	int changed;
412 	size_t tsize;
413 	struct stat from_sb, to_sb;
414 	int from_fd, to_fd;
415 
416 	changed = 0;
417 
418 	if ((from_fd = open(from_name, O_RDONLY)) < 0)
419 		err(EX_OSERR, "moveifchanged open(%s)", from_name);
420 
421 	if ((to_fd = open(to_name, O_RDONLY)) < 0)
422 		changed++;
423 
424 	if (!changed && fstat(from_fd, &from_sb) < 0)
425 		err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
426 
427 	if (!changed && fstat(to_fd, &to_sb) < 0)
428 		err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
429 
430 	if (!changed && from_sb.st_size != to_sb.st_size)
431 		changed++;
432 
433 	tsize = (size_t)from_sb.st_size;
434 
435 	if (!changed) {
436 		p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
437 #ifndef MAP_FAILED
438 #define MAP_FAILED ((caddr_t) -1)
439 #endif
440 		if (p == MAP_FAILED)
441 			err(EX_OSERR, "mmap %s", from_name);
442 		q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
443 		if (q == MAP_FAILED)
444 			err(EX_OSERR, "mmap %s", to_name);
445 
446 		changed = memcmp(p, q, tsize);
447 		munmap(p, tsize);
448 		munmap(q, tsize);
449 	}
450 	if (changed) {
451 		if (rename(from_name, to_name) < 0)
452 			err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
453 	} else {
454 		if (unlink(from_name) < 0)
455 			err(EX_OSERR, "unlink(%s)", from_name);
456 	}
457 }
458