Merge from vendor branch CVS:
[dragonfly.git] / usr.sbin / config / main.c
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.14 2005/01/12 00:26:03 cpressey 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", "svr4" };
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          * make symbolic links in compilation directory
167          * for "sys" (to make genassym.c work along with #include <sys/xxx>)
168          * and similarly for "machine".
169          */
170         if (*srcdir == '\0')
171                 snprintf(linkdest, sizeof(linkdest), "../../%s/include",
172                     machinename);
173         else
174                 snprintf(linkdest, sizeof(linkdest), "%s/%s/include",
175                     srcdir, machinename);
176         symlink(linkdest, path("machine"));
177
178         /*
179          * XXX check directory structure for architecture subdirectories and
180          * create the symlinks automatically XXX
181          */
182         if (*srcdir == '\0')
183                 snprintf(linkdest, sizeof(linkdest),
184                     "../../../../../net/i4b/include/%s",
185                     machinename);
186         else
187                 snprintf(linkdest, sizeof(linkdest), "%s/net/i4b/include/%s",
188                     srcdir, machinename);
189         mkdir(path("net"), 0755);
190         mkdir(path("net/i4b"), 0755);
191         mkdir(path("net/i4b/include"), 0755);
192         symlink(linkdest, path("net/i4b/include/machine"));
193
194         for (i = 0; i < sizeof(emus) / sizeof(emus[0]); ++i) {
195                 if (*srcdir == 0)  {
196                         snprintf(linkdest, sizeof(linkdest),
197                             "../../emulation/%s/%s",
198                             emus[i], machinename);
199                 } else {
200                         snprintf(linkdest, sizeof(linkdest),
201                             "%s/emulation/%s/%s",
202                             srcdir, emus[i], machinename);
203                 }
204                 snprintf(linksrc, sizeof(linksrc), "arch_%s", emus[i]);
205                 symlink(linkdest, path(linksrc));
206         }
207
208         options();                      /* make options .h files */
209         makefile();                     /* build Makefile */
210         headers();                      /* make a lot of .h files */
211         configfile();                   /* put config file into kernel*/
212         printf("Kernel build directory is %s\n", p);
213         exit(EX_OK);
214 }
215
216 /*
217  * get_srcdir
218  *      determine the root of the kernel source tree
219  *      and save that in srcdir.
220  */
221 static void
222 get_srcdir(void)
223 {
224         
225         if (realpath("../..", srcdir) == NULL)
226                 errx(2, "Unable to find root of source tree");
227 }
228
229 static void
230 usage(void)
231 {
232         
233         fprintf(stderr, "usage: config [-gpr] [-d destdir] sysname\n");
234         exit(1);
235 }
236
237 /*
238  * get_word
239  *      returns EOF on end of file
240  *      NULL on end of line
241  *      pointer to the word otherwise
242  */
243 char *
244 get_word(FILE *fp)
245 {
246         static char line[80];
247         int ch;
248         char *cp;
249         int escaped_nl = 0;
250
251 begin:
252         while ((ch = getc(fp)) != EOF)
253                 if (ch != ' ' && ch != '\t')
254                         break;
255         if (ch == EOF)
256                 return((char *)EOF);
257         if (ch == '\\') {
258                 escaped_nl = 1;
259                 goto begin;
260         }
261         if (ch == '\n') {
262                 if (escaped_nl) {
263                         escaped_nl = 0;
264                         goto begin;
265                 }
266                 else
267                         return(NULL);
268         }
269         cp = line;
270         *cp++ = ch;
271         while ((ch = getc(fp)) != EOF) {
272                 if (isspace(ch))
273                         break;
274                 *cp++ = ch;
275         }
276         *cp = 0;
277         if (ch == EOF)
278                 return((char *)EOF);
279         ungetc(ch, fp);
280         return(line);
281 }
282
283 /*
284  * get_quoted_word
285  *      like get_word but will accept something in double or single quotes
286  *      (to allow embedded spaces).
287  */
288 char *
289 get_quoted_word(FILE *fp)
290 {
291         static char line[256];
292         int ch;
293         char *cp;
294         int escaped_nl = 0;
295
296 begin:
297         while ((ch = getc(fp)) != EOF)
298                 if (ch != ' ' && ch != '\t')
299                         break;
300         if (ch == EOF)
301                 return((char *)EOF);
302         if (ch == '\\') {
303                 escaped_nl = 1;
304                 goto begin;
305         }
306         if (ch == '\n') {
307                 if (escaped_nl) {
308                         escaped_nl = 0;
309                         goto begin;
310                 }
311                 else
312                         return(NULL);
313         }
314         cp = line;
315         if (ch == '"' || ch == '\'') {
316                 int quote = ch;
317
318                 while ((ch = getc(fp)) != EOF) {
319                         if (ch == quote)
320                                 break;
321                         if (ch == '\n') {
322                                 *cp = 0;
323                                 printf("config: missing quote reading `%s'\n",
324                                         line);
325                                 exit(2);
326                         }
327                         *cp++ = ch;
328                 }
329         } else {
330                 *cp++ = ch;
331                 while ((ch = getc(fp)) != EOF) {
332                         if (isspace(ch))
333                                 break;
334                         *cp++ = ch;
335                 }
336                 if (ch != EOF)
337                         ungetc(ch, fp);
338         }
339         *cp = 0;
340         if (ch == EOF)
341                 return((char *)EOF);
342         return(line);
343 }
344
345 /*
346  * prepend the path to a filename
347  */
348 char *
349 path(const char *file)
350 {
351         char *cp;
352
353         cp = malloc((size_t)(strlen(destdir) + (file ? strlen(file) : 0) + 2));
354         strcpy(cp, destdir);
355         if (file != NULL) {
356                 strcat(cp, "/");
357                 strcat(cp, file);
358         }
359         return(cp);
360 }
361
362 static void
363 configfile(void)
364 {
365         FILE *fi, *fo;
366         char *p;
367         int i;
368         
369         fi = fopen(PREFIX, "r");
370         if (fi == NULL)
371                 err(2, "%s", PREFIX);
372         fo = fopen(p = path("config.c.new"), "w");
373         if (fo == NULL)
374                 err(2, "%s", p);
375         fprintf(fo, "#include \"opt_config.h\"\n");
376         fprintf(fo, "#ifdef INCLUDE_CONFIG_FILE \n");
377         fprintf(fo, "static const char config[] = \"\\\n");
378         fprintf(fo, "START CONFIG FILE %s\\n\\\n___", PREFIX);
379         while (EOF != (i = getc(fi))) {
380                 if (i == '\n') {
381                         fprintf(fo, "\\n\\\n___");
382                 } else if (i == '\"') {
383                         fprintf(fo, "\\\"");
384                 } else if (i == '\\') {
385                         fprintf(fo, "\\\\");
386                 } else {
387                         putc(i, fo);
388                 }
389         }
390         fprintf(fo, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX);
391         fprintf(fo, "\";\n");
392         fprintf(fo, "\n#endif /* INCLUDE_CONFIG_FILE */\n");
393         fclose(fi);
394         fclose(fo);
395         moveifchanged(path("config.c.new"), path("config.c"));
396 }
397
398 /*
399  * moveifchanged --
400  *      compare two files; rename if changed.
401  */
402 void
403 moveifchanged(const char *from_name, const char *to_name)
404 {
405         char *p, *q;
406         int changed;
407         size_t tsize;
408         struct stat from_sb, to_sb;
409         int from_fd, to_fd;
410
411         changed = 0;
412
413         if ((from_fd = open(from_name, O_RDONLY)) < 0)
414                 err(EX_OSERR, "moveifchanged open(%s)", from_name);
415
416         if ((to_fd = open(to_name, O_RDONLY)) < 0)
417                 changed++;
418
419         if (!changed && fstat(from_fd, &from_sb) < 0)
420                 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
421
422         if (!changed && fstat(to_fd, &to_sb) < 0)
423                 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
424
425         if (!changed && from_sb.st_size != to_sb.st_size)
426                 changed++;
427
428         tsize = (size_t)from_sb.st_size;
429
430         if (!changed) {
431                 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
432 #ifndef MAP_FAILED
433 #define MAP_FAILED ((caddr_t)-1)
434 #endif
435                 if (p == MAP_FAILED)
436                         err(EX_OSERR, "mmap %s", from_name);
437                 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
438                 if (q == MAP_FAILED)
439                         err(EX_OSERR, "mmap %s", to_name);
440
441                 changed = memcmp(p, q, tsize);
442                 munmap(p, tsize);
443                 munmap(q, tsize);
444         }
445         if (changed) {
446                 if (rename(from_name, to_name) < 0)
447                         err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
448         } else {
449                 if (unlink(from_name) < 0)
450                         err(EX_OSERR, "unlink(%s)", from_name);
451         }
452 }