config(8): Comment out some linux emulation remains.
[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. 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.
16  *
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.
28  *
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 $
32  */
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/file.h>
37 #include <sys/mman.h>
38 #include <sys/param.h>
39 #include <ctype.h>
40 #include <err.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"
47
48 #ifndef TRUE
49 #define TRUE    (1)
50 #endif
51
52 #ifndef FALSE
53 #define FALSE   (0)
54 #endif
55
56 #define CDIR    "../compile/"
57
58 char *  PREFIX;
59 char    destdir[MAXPATHLEN];
60 char    srcdir[MAXPATHLEN];
61
62 static int no_config_clobber = TRUE;
63 int     debugging;
64 int     profiling;
65
66 extern int yyparse(void);
67 static void configfile(void);
68 static void get_srcdir(void);
69 static void usage(void);
70
71 /*
72  * Config builds a set of files for building a UNIX
73  * system given a description of the desired system.
74  */
75 int
76 main(int argc, char *argv[])
77 {
78         struct stat buf;
79         int ch, len;
80         char *p;
81         char linkdest[MAXPATHLEN];
82 #if 0
83         unsigned int i;
84         char linksrc[64];
85         static const char *emus[] = { "linux" };
86 #endif
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 pc64'\n");
156                 exit(1);
157         }
158         if (machinename == NULL) {
159                 printf("Specify machine architecture, e.g. 'machine x86_64'\n");
160                 exit(1);
161         }
162         if (machinearchname == NULL) {
163                 printf("Specify cpu architecture, e.g. 'machine_arch x86_64'\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 #if 0
217         for (i = 0; i < sizeof(emus) / sizeof(emus[0]); ++i) {
218                 if (*srcdir == 0)  {
219                         snprintf(linkdest, sizeof(linkdest),
220                             "../../emulation/%s/%s",
221                             emus[i], machinearchname);
222                 } else {
223                         snprintf(linkdest, sizeof(linkdest),
224                             "%s/emulation/%s/%s",
225                             srcdir, emus[i], machinearchname);
226                 }
227                 snprintf(linksrc, sizeof(linksrc), "arch_%s", emus[i]);
228                 symlink(linkdest, path(linksrc));
229         }
230 #endif
231
232         options();                      /* make options .h files */
233         makefile();                     /* build Makefile */
234         headers();                      /* make a lot of .h files */
235         configfile();                   /* put config file into kernel*/
236         printf("Kernel build directory is %s\n", p);
237         exit(EX_OK);
238 }
239
240 /*
241  * get_srcdir
242  *      determine the root of the kernel source tree
243  *      and save that in srcdir.
244  */
245 static void
246 get_srcdir(void)
247 {
248         
249         if (realpath("..", srcdir) == NULL)
250                 errx(2, "Unable to find root of source tree");
251 }
252
253 static void
254 usage(void)
255 {
256         
257         fprintf(stderr, "usage: config [-gpr] [-d destdir] sysname\n");
258         exit(1);
259 }
260
261 /*
262  * get_word
263  *      returns EOF on end of file
264  *      NULL on end of line
265  *      pointer to the word otherwise
266  */
267 char *
268 get_word(FILE *fp)
269 {
270         static char line[80];
271         int ch;
272         char *cp;
273         int escaped_nl = 0;
274
275 begin:
276         while ((ch = getc(fp)) != EOF)
277                 if (ch != ' ' && ch != '\t')
278                         break;
279         if (ch == EOF)
280                 return((char *)EOF);
281         if (ch == '\\') {
282                 escaped_nl = 1;
283                 goto begin;
284         }
285         if (ch == '\n') {
286                 if (escaped_nl) {
287                         escaped_nl = 0;
288                         goto begin;
289                 }
290                 else
291                         return(NULL);
292         }
293         cp = line;
294         *cp++ = ch;
295         while ((ch = getc(fp)) != EOF) {
296                 if (isspace(ch))
297                         break;
298                 *cp++ = ch;
299         }
300         *cp = 0;
301         if (ch == EOF)
302                 return((char *)EOF);
303         ungetc(ch, fp);
304         return(line);
305 }
306
307 /*
308  * get_quoted_word
309  *      like get_word but will accept something in double or single quotes
310  *      (to allow embedded spaces).
311  */
312 char *
313 get_quoted_word(FILE *fp)
314 {
315         static char line[256];
316         int ch;
317         char *cp;
318         int escaped_nl = 0;
319
320 begin:
321         while ((ch = getc(fp)) != EOF)
322                 if (ch != ' ' && ch != '\t')
323                         break;
324         if (ch == EOF)
325                 return((char *)EOF);
326         if (ch == '\\') {
327                 escaped_nl = 1;
328                 goto begin;
329         }
330         if (ch == '\n') {
331                 if (escaped_nl) {
332                         escaped_nl = 0;
333                         goto begin;
334                 }
335                 else
336                         return(NULL);
337         }
338         cp = line;
339         if (ch == '"' || ch == '\'') {
340                 int quote = ch;
341
342                 while ((ch = getc(fp)) != EOF) {
343                         if (ch == quote)
344                                 break;
345                         if (ch == '\n') {
346                                 *cp = 0;
347                                 printf("config: missing quote reading `%s'\n",
348                                         line);
349                                 exit(2);
350                         }
351                         *cp++ = ch;
352                 }
353         } else {
354                 *cp++ = ch;
355                 while ((ch = getc(fp)) != EOF) {
356                         if (isspace(ch))
357                                 break;
358                         *cp++ = ch;
359                 }
360                 if (ch != EOF)
361                         ungetc(ch, fp);
362         }
363         *cp = 0;
364         if (ch == EOF)
365                 return((char *)EOF);
366         return(line);
367 }
368
369 /*
370  * prepend the path to a filename
371  */
372 char *
373 path(const char *file)
374 {
375         char *cp;
376
377         cp = malloc((size_t)(strlen(destdir) + (file ? strlen(file) : 0) + 2));
378         strcpy(cp, destdir);
379         if (file != NULL) {
380                 strcat(cp, "/");
381                 strcat(cp, file);
382         }
383         return(cp);
384 }
385
386 static void
387 configfile(void)
388 {
389         FILE *fi, *fo;
390         char *p;
391         int i;
392         
393         fi = fopen(PREFIX, "r");
394         if (fi == NULL)
395                 err(2, "%s", PREFIX);
396         fo = fopen(p = path("config.c.new"), "w");
397         if (fo == NULL)
398                 err(2, "%s", p);
399         fprintf(fo, "#include \"opt_config.h\"\n");
400         fprintf(fo, "#ifdef INCLUDE_CONFIG_FILE \n");
401         fprintf(fo, "/* Mark config as used, so gcc doesn't optimize it away. */\n");
402         fprintf(fo, "#include <sys/types.h>\n");
403         fprintf(fo, "__used\n");
404         fprintf(fo, "static const char config[] = \"\\\n");
405         fprintf(fo, "START CONFIG FILE %s\\n\\\n___", PREFIX);
406         while (EOF != (i = getc(fi))) {
407                 if (i == '\n') {
408                         fprintf(fo, "\\n\\\n___");
409                 } else if (i == '\"') {
410                         fprintf(fo, "\\\"");
411                 } else if (i == '\\') {
412                         fprintf(fo, "\\\\");
413                 } else {
414                         putc(i, fo);
415                 }
416         }
417         fprintf(fo, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX);
418         fprintf(fo, "\";\n");
419         fprintf(fo, "\n#endif /* INCLUDE_CONFIG_FILE */\n");
420         fclose(fi);
421         fclose(fo);
422         moveifchanged(path("config.c.new"), path("config.c"));
423 }
424
425 /*
426  * moveifchanged --
427  *      compare two files; rename if changed.
428  */
429 void
430 moveifchanged(const char *from_name, const char *to_name)
431 {
432         char *p, *q;
433         int changed;
434         size_t tsize;
435         struct stat from_sb, to_sb;
436         int from_fd, to_fd;
437
438         changed = 0;
439
440         if ((from_fd = open(from_name, O_RDONLY)) < 0)
441                 err(EX_OSERR, "moveifchanged open(%s)", from_name);
442
443         if ((to_fd = open(to_name, O_RDONLY)) < 0)
444                 changed++;
445
446         if (!changed && fstat(from_fd, &from_sb) < 0)
447                 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
448
449         if (!changed && fstat(to_fd, &to_sb) < 0)
450                 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
451
452         if (!changed && from_sb.st_size != to_sb.st_size)
453                 changed++;
454
455         tsize = (size_t)from_sb.st_size;
456
457         if (!changed) {
458                 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
459 #ifndef MAP_FAILED
460 #define MAP_FAILED ((caddr_t)-1)
461 #endif
462                 if (p == MAP_FAILED)
463                         err(EX_OSERR, "mmap %s", from_name);
464                 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
465                 if (q == MAP_FAILED)
466                         err(EX_OSERR, "mmap %s", to_name);
467
468                 changed = memcmp(p, q, tsize);
469                 munmap(p, tsize);
470                 munmap(q, tsize);
471         }
472         if (changed) {
473                 if (rename(from_name, to_name) < 0)
474                         err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
475         } else {
476                 if (unlink(from_name) < 0)
477                         err(EX_OSERR, "unlink(%s)", from_name);
478         }
479 }