9beb644d3e9a8cd7f3e59bbf6848b80c04050a0b
[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.3 2003/08/07 21:19:25 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 __P((void));
71 static void get_srcdir __P((void));
72 static void usage __P((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(argc, argv)
80         int argc;
81         char **argv;
82 {
83
84         struct stat buf;
85         int ch, len;
86         char *p;
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         switch (machine) {
160
161         case MACHINE_I386:
162         case MACHINE_PC98:
163         case MACHINE_ALPHA:
164                 newbus_ioconf();        /* Print ioconf.c */
165                 break;
166
167         default:
168                 printf("Specify machine type, e.g. ``machine i386''\n");
169                 exit(1);
170         }
171         /*
172          * make symbolic links in compilation directory
173          * for "sys" (to make genassym.c work along with #include <sys/xxx>)
174          * and similarly for "machine".
175          */
176         {
177         char xxx[MAXPATHLEN];
178         if (*srcdir == '\0')
179                 (void)snprintf(xxx, sizeof(xxx), "../../%s/include",
180                     machinename);
181         else
182                 (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
183                     srcdir, machinename);
184         (void) symlink(xxx, path("machine"));
185         }
186
187         /*
188          * XXX check directory structure for architecture subdirectories and
189          * create the symlinks automatically XXX
190          */
191         {
192         char xxx[MAXPATHLEN];
193         if (*srcdir == '\0')
194                 (void)snprintf(xxx, sizeof(xxx), "../../../../../net/i4b/include/%s",
195                     machinename);
196         else
197                 (void)snprintf(xxx, sizeof(xxx), "%s/net/i4b/include/%s",
198                     srcdir, machinename);
199         (void) mkdir(path("net"), 0755);
200         (void) mkdir(path("net/i4b"), 0755);
201         (void) mkdir(path("net/i4b/include"), 0755);
202         (void) symlink(xxx, path("net/i4b/include/machine"));
203         }
204         {
205         char xxx[MAXPATHLEN];
206         if (*srcdir == '\0')
207                 (void)snprintf(xxx, sizeof(xxx), "../../../../emulation/linux/%s",
208                     machinename);
209         else
210                 (void)snprintf(xxx, sizeof(xxx), "%s/emulation/linux/%s",
211                     srcdir, machinename);
212         (void) mkdir(path("emulation"), 0755);
213         (void) mkdir(path("emulation/linux"), 0755);
214         (void) symlink(xxx, path("emulation/linux/machine"));
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(0);
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()
232 {
233         if (realpath("../..", srcdir) == NULL)
234                 errx(2, "Unable to find root of source tree");
235 }
236
237 static void
238 usage()
239 {
240                 fprintf(stderr, "usage: config [-gpr] [-d destdir] sysname\n");
241                 exit(1);
242 }
243
244 /*
245  * get_word
246  *      returns EOF on end of file
247  *      NULL on end of line
248  *      pointer to the word otherwise
249  */
250 char *
251 get_word(fp)
252         register FILE *fp;
253 {
254         static char line[80];
255         register int ch;
256         register char *cp;
257         int escaped_nl = 0;
258
259 begin:
260         while ((ch = getc(fp)) != EOF)
261                 if (ch != ' ' && ch != '\t')
262                         break;
263         if (ch == EOF)
264                 return ((char *)EOF);
265         if (ch == '\\'){
266                 escaped_nl = 1;
267                 goto begin;
268         }
269         if (ch == '\n') {
270                 if (escaped_nl){
271                         escaped_nl = 0;
272                         goto begin;
273                 }
274                 else
275                         return (NULL);
276         }
277         cp = line;
278         *cp++ = ch;
279         while ((ch = getc(fp)) != EOF) {
280                 if (isspace(ch))
281                         break;
282                 *cp++ = ch;
283         }
284         *cp = 0;
285         if (ch == EOF)
286                 return ((char *)EOF);
287         (void) ungetc(ch, fp);
288         return (line);
289 }
290
291 /*
292  * get_quoted_word
293  *      like get_word but will accept something in double or single quotes
294  *      (to allow embedded spaces).
295  */
296 char *
297 get_quoted_word(fp)
298         register FILE *fp;
299 {
300         static char line[256];
301         register int ch;
302         register 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                 register 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                         (void) 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(file)
359         char *file;
360 {
361         register char *cp;
362
363         cp = malloc((size_t)(strlen(destdir) + (file ? strlen(file) : 0) + 2));
364         (void) strcpy(cp, destdir);
365         if (file) {
366                 (void) strcat(cp, "/");
367                 (void) strcat(cp, file);
368         }
369         return (cp);
370 }
371
372 static void
373 configfile()
374 {
375         FILE *fi, *fo;
376         char *p;
377         int i;
378         
379         fi = fopen(PREFIX, "r");
380         if (!fi)
381                 err(2, "%s", PREFIX);
382         fo = fopen(p=path("config.c.new"), "w");
383         if (!fo)
384                 err(2, "%s", p);
385         fprintf(fo, "#include \"opt_config.h\"\n");
386         fprintf(fo, "#ifdef INCLUDE_CONFIG_FILE \n");
387         fprintf(fo, "static const char config[] = \"\\\n");
388         fprintf(fo, "START CONFIG FILE %s\\n\\\n___", PREFIX);
389         while (EOF != (i=getc(fi))) {
390                 if (i == '\n') {
391                         fprintf(fo, "\\n\\\n___");
392                 } else if (i == '\"') {
393                         fprintf(fo, "\\\"");
394                 } else if (i == '\\') {
395                         fprintf(fo, "\\\\");
396                 } else {
397                         putc(i, fo);
398                 }
399         }
400         fprintf(fo, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX);
401         fprintf(fo, "\";\n");
402         fprintf(fo, "\n#endif /* INCLUDE_CONFIG_FILE */\n");
403         fclose(fi);
404         fclose(fo);
405         moveifchanged(path("config.c.new"), path("config.c"));
406 }
407
408 /*
409  * moveifchanged --
410  *      compare two files; rename if changed.
411  */
412 void
413 moveifchanged(const char *from_name, const char *to_name)
414 {
415         char *p, *q;
416         int changed;
417         size_t tsize;
418         struct stat from_sb, to_sb;
419         int from_fd, to_fd;
420
421         changed = 0;
422
423         if ((from_fd = open(from_name, O_RDONLY)) < 0)
424                 err(EX_OSERR, "moveifchanged open(%s)", from_name);
425
426         if ((to_fd = open(to_name, O_RDONLY)) < 0)
427                 changed++;
428
429         if (!changed && fstat(from_fd, &from_sb) < 0)
430                 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
431
432         if (!changed && fstat(to_fd, &to_sb) < 0)
433                 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
434
435         if (!changed && from_sb.st_size != to_sb.st_size)
436                 changed++;
437
438         tsize = (size_t)from_sb.st_size;
439
440         if (!changed) {
441                 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
442 #ifndef MAP_FAILED
443 #define MAP_FAILED ((caddr_t) -1)
444 #endif
445                 if (p == MAP_FAILED)
446                         err(EX_OSERR, "mmap %s", from_name);
447                 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
448                 if (q == MAP_FAILED)
449                         err(EX_OSERR, "mmap %s", to_name);
450
451                 changed = memcmp(p, q, tsize);
452                 munmap(p, tsize);
453                 munmap(q, tsize);
454         }
455         if (changed) {
456                 if (rename(from_name, to_name) < 0)
457                         err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
458         } else {
459                 if (unlink(from_name) < 0)
460                         err(EX_OSERR, "unlink(%s)", from_name);
461         }
462 }