Initial import from FreeBSD RELENG_4:
[games.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
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)main.c      8.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD: src/usr.sbin/config/main.c,v 1.37.2.3 2001/06/13 00:25:53 cg Exp $";
46 #endif /* not lint */
47
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <sys/file.h>
51 #include <sys/mman.h>
52 #include <sys/param.h>
53 #include <ctype.h>
54 #include <err.h>
55 #include <stdio.h>
56 #include <sysexits.h>
57 #include <unistd.h>
58 #include "y.tab.h"
59 #include "config.h"
60
61 #ifndef TRUE
62 #define TRUE    (1)
63 #endif
64
65 #ifndef FALSE
66 #define FALSE   (0)
67 #endif
68
69 #define CDIR    "../../compile/"
70
71 char *  PREFIX;
72 char    destdir[MAXPATHLEN];
73 char    srcdir[MAXPATHLEN];
74
75 static int no_config_clobber = TRUE;
76 int     debugging;
77 int     profiling;
78
79 static void configfile __P((void));
80 static void get_srcdir __P((void));
81 static void usage __P((void));
82
83 /*
84  * Config builds a set of files for building a UNIX
85  * system given a description of the desired system.
86  */
87 int
88 main(argc, argv)
89         int argc;
90         char **argv;
91 {
92
93         struct stat buf;
94         int ch, len;
95         char *p;
96
97         while ((ch = getopt(argc, argv, "d:gprn")) != -1)
98                 switch (ch) {
99                 case 'd':
100                         if (*destdir == '\0')
101                                 strlcpy(destdir, optarg, sizeof(destdir));
102                         else
103                                 errx(2, "directory already set");
104                         break;
105                 case 'g':
106                         debugging++;
107                         break;
108                 case 'p':
109                         profiling++;
110                         break;
111                 case 'n':
112                         /* no_config_clobber is now true by default, no-op */
113                         fprintf(stderr,
114                                 "*** Using obsolete config option '-n' ***\n");
115                         break;
116                 case 'r':
117                         no_config_clobber = FALSE;
118                         break;
119                 case '?':
120                 default:
121                         usage();
122                 }
123         argc -= optind;
124         argv += optind;
125
126         if (argc != 1)
127                 usage();
128
129         if (freopen(PREFIX = *argv, "r", stdin) == NULL)
130                 err(2, "%s", PREFIX);
131
132         if (*destdir != '\0') {
133                 len = strlen(destdir);
134                 while (len > 1 && destdir[len - 1] == '/')
135                         destdir[--len] = '\0';
136                 get_srcdir();
137         } else {
138                 strlcpy(destdir, CDIR, sizeof(destdir));
139                 strlcat(destdir, PREFIX, sizeof(destdir));
140         }
141
142         p = path((char *)NULL);
143         if (stat(p, &buf)) {
144                 if (mkdir(p, 0777))
145                         err(2, "%s", p);
146         }
147         else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
148                 errx(2, "%s isn't a directory", p);
149         }
150         else if (!no_config_clobber) {
151                 char tmp[strlen(p) + 8];
152
153                 fprintf(stderr, "Removing old directory %s:  ", p);
154                 fflush(stderr);
155                 snprintf(tmp, sizeof(tmp), "rm -rf %s", p);
156                 if (system(tmp)) {
157                         fprintf(stderr, "Failed!\n");
158                         err(2, "%s", tmp);
159                 }
160                 fprintf(stderr, "Done.\n");
161                 if (mkdir(p, 0777))
162                         err(2, "%s", p);
163         }
164
165         dtab = NULL;
166         if (yyparse())
167                 exit(3);
168         switch (machine) {
169
170         case MACHINE_I386:
171         case MACHINE_PC98:
172         case MACHINE_ALPHA:
173                 newbus_ioconf();        /* Print ioconf.c */
174                 break;
175
176         default:
177                 printf("Specify machine type, e.g. ``machine i386''\n");
178                 exit(1);
179         }
180         /*
181          * make symbolic links in compilation directory
182          * for "sys" (to make genassym.c work along with #include <sys/xxx>)
183          * and similarly for "machine".
184          */
185         {
186         char xxx[MAXPATHLEN];
187         if (*srcdir == '\0')
188                 (void)snprintf(xxx, sizeof(xxx), "../../%s/include",
189                     machinename);
190         else
191                 (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
192                     srcdir, machinename);
193         (void) symlink(xxx, path("machine"));
194         }
195         options();                      /* make options .h files */
196         makefile();                     /* build Makefile */
197         headers();                      /* make a lot of .h files */
198         configfile();                   /* put config file into kernel*/
199         printf("Kernel build directory is %s\n", p);
200         exit(0);
201 }
202
203 /*
204  * get_srcdir
205  *      determine the root of the kernel source tree
206  *      and save that in srcdir.
207  */
208 static void
209 get_srcdir()
210 {
211         if (realpath("../..", srcdir) == NULL)
212                 errx(2, "Unable to find root of source tree");
213 }
214
215 static void
216 usage()
217 {
218                 fprintf(stderr, "usage: config [-gpr] [-d destdir] sysname\n");
219                 exit(1);
220 }
221
222 /*
223  * get_word
224  *      returns EOF on end of file
225  *      NULL on end of line
226  *      pointer to the word otherwise
227  */
228 char *
229 get_word(fp)
230         register FILE *fp;
231 {
232         static char line[80];
233         register int ch;
234         register char *cp;
235         int escaped_nl = 0;
236
237 begin:
238         while ((ch = getc(fp)) != EOF)
239                 if (ch != ' ' && ch != '\t')
240                         break;
241         if (ch == EOF)
242                 return ((char *)EOF);
243         if (ch == '\\'){
244                 escaped_nl = 1;
245                 goto begin;
246         }
247         if (ch == '\n') {
248                 if (escaped_nl){
249                         escaped_nl = 0;
250                         goto begin;
251                 }
252                 else
253                         return (NULL);
254         }
255         cp = line;
256         *cp++ = ch;
257         while ((ch = getc(fp)) != EOF) {
258                 if (isspace(ch))
259                         break;
260                 *cp++ = ch;
261         }
262         *cp = 0;
263         if (ch == EOF)
264                 return ((char *)EOF);
265         (void) ungetc(ch, fp);
266         return (line);
267 }
268
269 /*
270  * get_quoted_word
271  *      like get_word but will accept something in double or single quotes
272  *      (to allow embedded spaces).
273  */
274 char *
275 get_quoted_word(fp)
276         register FILE *fp;
277 {
278         static char line[256];
279         register int ch;
280         register char *cp;
281         int escaped_nl = 0;
282
283 begin:
284         while ((ch = getc(fp)) != EOF)
285                 if (ch != ' ' && ch != '\t')
286                         break;
287         if (ch == EOF)
288                 return ((char *)EOF);
289         if (ch == '\\'){
290                 escaped_nl = 1;
291                 goto begin;
292         }
293         if (ch == '\n') {
294                 if (escaped_nl){
295                         escaped_nl = 0;
296                         goto begin;
297                 }
298                 else
299                         return (NULL);
300         }
301         cp = line;
302         if (ch == '"' || ch == '\'') {
303                 register int quote = ch;
304
305                 while ((ch = getc(fp)) != EOF) {
306                         if (ch == quote)
307                                 break;
308                         if (ch == '\n') {
309                                 *cp = 0;
310                                 printf("config: missing quote reading `%s'\n",
311                                         line);
312                                 exit(2);
313                         }
314                         *cp++ = ch;
315                 }
316         } else {
317                 *cp++ = ch;
318                 while ((ch = getc(fp)) != EOF) {
319                         if (isspace(ch))
320                                 break;
321                         *cp++ = ch;
322                 }
323                 if (ch != EOF)
324                         (void) ungetc(ch, fp);
325         }
326         *cp = 0;
327         if (ch == EOF)
328                 return ((char *)EOF);
329         return (line);
330 }
331
332 /*
333  * prepend the path to a filename
334  */
335 char *
336 path(file)
337         char *file;
338 {
339         register char *cp;
340
341         cp = malloc((size_t)(strlen(destdir) + (file ? strlen(file) : 0) + 2));
342         (void) strcpy(cp, destdir);
343         if (file) {
344                 (void) strcat(cp, "/");
345                 (void) strcat(cp, file);
346         }
347         return (cp);
348 }
349
350 static void
351 configfile()
352 {
353         FILE *fi, *fo;
354         char *p;
355         int i;
356         
357         fi = fopen(PREFIX, "r");
358         if (!fi)
359                 err(2, "%s", PREFIX);
360         fo = fopen(p=path("config.c.new"), "w");
361         if (!fo)
362                 err(2, "%s", p);
363         fprintf(fo, "#include \"opt_config.h\"\n");
364         fprintf(fo, "#ifdef INCLUDE_CONFIG_FILE \n");
365         fprintf(fo, "static const char config[] = \"\\\n");
366         fprintf(fo, "START CONFIG FILE %s\\n\\\n___", PREFIX);
367         while (EOF != (i=getc(fi))) {
368                 if (i == '\n') {
369                         fprintf(fo, "\\n\\\n___");
370                 } else if (i == '\"') {
371                         fprintf(fo, "\\\"");
372                 } else if (i == '\\') {
373                         fprintf(fo, "\\\\");
374                 } else {
375                         putc(i, fo);
376                 }
377         }
378         fprintf(fo, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX);
379         fprintf(fo, "\";\n");
380         fprintf(fo, "\n#endif /* INCLUDE_CONFIG_FILE */\n");
381         fclose(fi);
382         fclose(fo);
383         moveifchanged(path("config.c.new"), path("config.c"));
384 }
385
386 /*
387  * moveifchanged --
388  *      compare two files; rename if changed.
389  */
390 void
391 moveifchanged(const char *from_name, const char *to_name)
392 {
393         char *p, *q;
394         int changed;
395         size_t tsize;
396         struct stat from_sb, to_sb;
397         int from_fd, to_fd;
398
399         changed = 0;
400
401         if ((from_fd = open(from_name, O_RDONLY)) < 0)
402                 err(EX_OSERR, "moveifchanged open(%s)", from_name);
403
404         if ((to_fd = open(to_name, O_RDONLY)) < 0)
405                 changed++;
406
407         if (!changed && fstat(from_fd, &from_sb) < 0)
408                 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
409
410         if (!changed && fstat(to_fd, &to_sb) < 0)
411                 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
412
413         if (!changed && from_sb.st_size != to_sb.st_size)
414                 changed++;
415
416         tsize = (size_t)from_sb.st_size;
417
418         if (!changed) {
419                 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
420 #ifndef MAP_FAILED
421 #define MAP_FAILED ((caddr_t) -1)
422 #endif
423                 if (p == MAP_FAILED)
424                         err(EX_OSERR, "mmap %s", from_name);
425                 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
426                 if (q == MAP_FAILED)
427                         err(EX_OSERR, "mmap %s", to_name);
428
429                 changed = memcmp(p, q, tsize);
430                 munmap(p, tsize);
431                 munmap(q, tsize);
432         }
433         if (changed) {
434                 if (rename(from_name, to_name) < 0)
435                         err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
436         } else {
437                 if (unlink(from_name) < 0)
438                         err(EX_OSERR, "unlink(%s)", from_name);
439         }
440 }