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