dhcpcd: update README.DRAGONFLY
[dragonfly.git] / usr.sbin / config / main.c
blob013c73b68db2c968c8eb32728eed350489101729
1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)main.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.sbin/config/main.c,v 1.37.2.3 2001/06/13 00:25:53 cg Exp $
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/mman.h>
37 #include <sys/param.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <dirent.h>
43 #include <sysexits.h>
44 #include <unistd.h>
45 #include "y.tab.h"
46 #include "config.h"
48 #ifndef TRUE
49 #define TRUE (1)
50 #endif
52 #ifndef FALSE
53 #define FALSE (0)
54 #endif
56 #define CDIR "../compile/"
58 char * platformname;
59 char * machinename;
60 char * machinearchname;
62 struct cputype *cputype;
63 struct opt *opt, *mkopt;
64 struct opt_list *otab;
66 char * PREFIX;
67 char destdir[MAXPATHLEN];
68 char srcdir[MAXPATHLEN - 32];
70 static int no_config_clobber = TRUE;
71 int debugging;
73 extern int yyparse(void);
74 static void configfile(void);
75 static void get_srcdir(void);
76 static void usage(void);
79 * Config builds a set of files for building a UNIX
80 * system given a description of the desired system.
82 int
83 main(int argc, char *argv[])
85 struct stat buf;
86 int ch, len;
87 char *p;
88 char linkdest[MAXPATHLEN];
89 #if 0
90 unsigned int i;
91 char linksrc[64];
92 static const char *emus[] = { "linux" };
93 #endif
95 while ((ch = getopt(argc, argv, "d:gr")) != -1)
96 switch (ch) {
97 case 'd':
98 if (*destdir == '\0')
99 strlcpy(destdir, optarg, sizeof(destdir));
100 else
101 errx(2, "directory already set");
102 break;
103 case 'g':
104 debugging++;
105 break;
106 case 'r':
107 no_config_clobber = FALSE;
108 break;
109 case '?':
110 default:
111 usage();
113 argc -= optind;
114 argv += optind;
116 if (argc != 1)
117 usage();
119 if (freopen(PREFIX = *argv, "r", stdin) == NULL)
120 err(2, "%s", PREFIX);
122 if (*destdir != '\0') {
123 len = strlen(destdir);
124 while (len > 1 && destdir[len - 1] == '/')
125 destdir[--len] = '\0';
126 get_srcdir();
127 } else {
128 strlcpy(destdir, CDIR, sizeof(destdir));
129 strlcat(destdir, PREFIX, sizeof(destdir));
132 p = path(NULL);
133 if (stat(p, &buf)) {
134 if (mkdir(p, 0777))
135 err(2, "%s", p);
137 else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
138 errx(2, "%s isn't a directory", p);
140 else if (!no_config_clobber) {
141 char tmp[strlen(p) + 8];
143 fprintf(stderr, "Removing old directory %s: ", p);
144 fflush(stderr);
145 snprintf(tmp, sizeof(tmp), "rm -rf %s", p);
146 if (system(tmp)) {
147 fprintf(stderr, "Failed!\n");
148 err(2, "%s", tmp);
150 fprintf(stderr, "Done.\n");
151 if (mkdir(p, 0777))
152 err(2, "%s", p);
155 dtab = NULL;
156 if (yyparse())
157 exit(3);
158 if (platformname == NULL) {
159 printf("Specify platform architecture, e.g. 'platform pc64'\n");
160 exit(1);
162 if (machinename == NULL) {
163 printf("Specify machine architecture, e.g. 'machine x86_64'\n");
164 exit(1);
166 if (machinearchname == NULL) {
167 printf("Specify cpu architecture, e.g. 'machine_arch x86_64'\n");
168 exit(1);
170 newbus_ioconf();
173 * "machine" points into platform/<PLATFORM>/include
175 if (*srcdir == '\0')
176 snprintf(linkdest, sizeof(linkdest), "../../platform/%s/include",
177 platformname);
178 else
179 snprintf(linkdest, sizeof(linkdest), "%s/platform/%s/include",
180 srcdir, platformname);
181 symlink(linkdest, path("machine"));
184 * "machine_base" points into platform/<PLATFORM>
186 if (*srcdir == '\0')
187 snprintf(linkdest, sizeof(linkdest), "../../platform/%s",
188 platformname);
189 else
190 snprintf(linkdest, sizeof(linkdest), "%s/platform/%s",
191 srcdir, platformname);
192 symlink(linkdest, path("machine_base"));
195 * "cpu" points to cpu/<MACHINE_ARCH>/include
197 if (*srcdir == '\0')
198 snprintf(linkdest, sizeof(linkdest),
199 "../../cpu/%s/include", machinearchname);
200 else
201 snprintf(linkdest, sizeof(linkdest),
202 "%s/cpu/%s/include", srcdir, machinearchname);
203 symlink(linkdest, path("cpu"));
206 * "cpu_base" points to cpu/<MACHINE_ARCH>
208 if (*srcdir == '\0')
209 snprintf(linkdest, sizeof(linkdest), "../../cpu/%s",
210 machinearchname);
211 else
212 snprintf(linkdest, sizeof(linkdest), "%s/cpu/%s",
213 srcdir, machinearchname);
214 symlink(linkdest, path("cpu_base"));
217 * XXX check directory structure for architecture subdirectories and
218 * create the symlinks automatically XXX
220 #if 0
221 for (i = 0; i < NELEM(emus); ++i) {
222 if (*srcdir == 0) {
223 snprintf(linkdest, sizeof(linkdest),
224 "../../emulation/%s/%s",
225 emus[i], machinearchname);
226 } else {
227 snprintf(linkdest, sizeof(linkdest),
228 "%s/emulation/%s/%s",
229 srcdir, emus[i], machinearchname);
231 snprintf(linksrc, sizeof(linksrc), "arch_%s", emus[i]);
232 symlink(linkdest, path(linksrc));
234 #endif
236 options(); /* make options .h files */
237 makefile(); /* build Makefile */
238 headers(); /* make a lot of .h files */
239 configfile(); /* put config file into kernel*/
240 printf("Kernel build directory is %s\n", p);
241 exit(EX_OK);
245 * get_srcdir
246 * determine the root of the kernel source tree
247 * and save that in srcdir.
249 static void
250 get_srcdir(void)
253 if (realpath("..", srcdir) == NULL)
254 errx(2, "Unable to find root of source tree");
257 static void
258 usage(void)
261 fprintf(stderr, "usage: config [-gpr] [-d destdir] sysname\n");
262 exit(1);
266 * get_word
267 * returns EOF on end of file
268 * NULL on end of line
269 * pointer to the word otherwise
271 char *
272 get_word(FILE *fp)
274 static char line[80];
275 int ch;
276 char *cp;
277 int escaped_nl = 0;
279 begin:
280 while ((ch = getc(fp)) != EOF)
281 if (ch != ' ' && ch != '\t')
282 break;
283 if (ch == EOF)
284 return((char *)EOF);
285 if (ch == '\\') {
286 escaped_nl = 1;
287 goto begin;
289 if (ch == '\n') {
290 if (escaped_nl) {
291 escaped_nl = 0;
292 goto begin;
294 else
295 return(NULL);
297 cp = line;
298 *cp++ = ch;
299 while ((ch = getc(fp)) != EOF) {
300 if (isspace(ch))
301 break;
302 *cp++ = ch;
304 *cp = 0;
305 if (ch == EOF)
306 return((char *)EOF);
307 ungetc(ch, fp);
308 return(line);
312 * get_quoted_word
313 * like get_word but will accept something in double or single quotes
314 * (to allow embedded spaces).
316 char *
317 get_quoted_word(FILE *fp)
319 static char line[256];
320 int ch;
321 char *cp;
322 int escaped_nl = 0;
324 begin:
325 while ((ch = getc(fp)) != EOF)
326 if (ch != ' ' && ch != '\t')
327 break;
328 if (ch == EOF)
329 return((char *)EOF);
330 if (ch == '\\') {
331 escaped_nl = 1;
332 goto begin;
334 if (ch == '\n') {
335 if (escaped_nl) {
336 escaped_nl = 0;
337 goto begin;
339 else
340 return(NULL);
342 cp = line;
343 if (ch == '"' || ch == '\'') {
344 int quote = ch;
346 while ((ch = getc(fp)) != EOF) {
347 if (ch == quote)
348 break;
349 if (ch == '\n') {
350 *cp = 0;
351 printf("config: missing quote reading `%s'\n",
352 line);
353 exit(2);
355 *cp++ = ch;
357 } else {
358 *cp++ = ch;
359 while ((ch = getc(fp)) != EOF) {
360 if (isspace(ch))
361 break;
362 *cp++ = ch;
364 if (ch != EOF)
365 ungetc(ch, fp);
367 *cp = 0;
368 if (ch == EOF)
369 return((char *)EOF);
370 return(line);
374 * prepend the path to a filename
376 char *
377 path(const char *file)
379 char *cp;
381 cp = malloc((size_t)(strlen(destdir) + (file ? strlen(file) : 0) + 2));
382 strcpy(cp, destdir);
383 if (file != NULL) {
384 strcat(cp, "/");
385 strcat(cp, file);
387 return(cp);
390 static void
391 configfile(void)
393 FILE *fi, *fo;
394 char *p;
395 int i;
397 fi = fopen(PREFIX, "r");
398 if (fi == NULL)
399 err(2, "%s", PREFIX);
400 fo = fopen(p = path("config.c.new"), "w");
401 if (fo == NULL)
402 err(2, "%s", p);
403 fprintf(fo, "#include \"opt_config.h\"\n");
404 fprintf(fo, "#ifdef INCLUDE_CONFIG_FILE \n");
405 fprintf(fo, "/* Mark config as used, so gcc doesn't optimize it away. */\n");
406 fprintf(fo, "#include <sys/types.h>\n");
407 fprintf(fo, "__used\n");
408 fprintf(fo, "static const char config[] = \"\\\n");
409 fprintf(fo, "START CONFIG FILE %s\\n\\\n___", PREFIX);
410 while (EOF != (i = getc(fi))) {
411 if (i == '\n') {
412 fprintf(fo, "\\n\\\n___");
413 } else if (i == '\"') {
414 fprintf(fo, "\\\"");
415 } else if (i == '\\') {
416 fprintf(fo, "\\\\");
417 } else {
418 putc(i, fo);
421 fprintf(fo, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX);
422 fprintf(fo, "\";\n");
423 fprintf(fo, "\n#endif /* INCLUDE_CONFIG_FILE */\n");
424 fclose(fi);
425 fclose(fo);
426 moveifchanged(path("config.c.new"), path("config.c"));
430 * moveifchanged --
431 * compare two files; rename if changed.
433 void
434 moveifchanged(const char *from_name, const char *to_name)
436 char *p, *q;
437 int changed;
438 size_t tsize;
439 struct stat from_sb, to_sb;
440 int from_fd, to_fd;
442 changed = 0;
444 if ((from_fd = open(from_name, O_RDONLY)) < 0)
445 err(EX_OSERR, "moveifchanged open(%s)", from_name);
447 if ((to_fd = open(to_name, O_RDONLY)) < 0)
448 changed++;
450 if (!changed && fstat(from_fd, &from_sb) < 0)
451 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
453 if (!changed && fstat(to_fd, &to_sb) < 0)
454 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
456 if (!changed && from_sb.st_size != to_sb.st_size)
457 changed++;
459 tsize = (size_t)from_sb.st_size;
461 if (!changed) {
462 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
463 #ifndef MAP_FAILED
464 #define MAP_FAILED ((caddr_t)-1)
465 #endif
466 if (p == MAP_FAILED)
467 err(EX_OSERR, "mmap %s", from_name);
468 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
469 if (q == MAP_FAILED)
470 err(EX_OSERR, "mmap %s", to_name);
472 changed = memcmp(p, q, tsize);
473 munmap(p, tsize);
474 munmap(q, tsize);
476 if (changed) {
477 if (rename(from_name, to_name) < 0)
478 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
479 } else {
480 if (unlink(from_name) < 0)
481 err(EX_OSERR, "unlink(%s)", from_name);