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