]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.sbin/config/main.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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  * 4. 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
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)main.c      8.1 (Berkeley) 6/6/93";
39 #endif
40 static const char rcsid[] =
41   "$FreeBSD$";
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/sbuf.h>
47 #include <sys/file.h>
48 #include <sys/mman.h>
49 #include <sys/param.h>
50
51 #include <assert.h>
52 #include <ctype.h>
53 #include <err.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <sysexits.h>
57 #include <unistd.h>
58 #include <dirent.h>
59 #include "y.tab.h"
60 #include "config.h"
61 #include "configvers.h"
62
63 #ifndef TRUE
64 #define TRUE    (1)
65 #endif
66
67 #ifndef FALSE
68 #define FALSE   (0)
69 #endif
70
71 #define CDIR    "../compile/"
72
73 char *  PREFIX;
74 char    destdir[MAXPATHLEN];
75 char    srcdir[MAXPATHLEN];
76
77 int     debugging;
78 int     profiling;
79 int     found_defaults;
80 int     incignore;
81
82 /*
83  * Preserve old behaviour in INCLUDE_CONFIG_FILE handling (files are included
84  * literally).
85  */
86 int     filebased = 0;
87
88 static void configfile(void);
89 static void get_srcdir(void);
90 static void usage(void);
91 static void cleanheaders(char *);
92 static void kernconfdump(const char *);
93 static void checkversion(void);
94
95 struct hdr_list {
96         char *h_name;
97         struct hdr_list *h_next;
98 } *htab;
99
100 /*
101  * Config builds a set of files for building a UNIX
102  * system given a description of the desired system.
103  */
104 int
105 main(int argc, char **argv)
106 {
107
108         struct stat buf;
109         int ch, len;
110         char *p;
111         char xxx[MAXPATHLEN];
112         char *kernfile;
113
114         kernfile = NULL;
115         while ((ch = getopt(argc, argv, "Cd:gpVx:")) != -1)
116                 switch (ch) {
117                 case 'C':
118                         filebased = 1;
119                         break;
120                 case 'd':
121                         if (*destdir == '\0')
122                                 strlcpy(destdir, optarg, sizeof(destdir));
123                         else
124                                 errx(EXIT_FAILURE, "directory already set");
125                         break;
126                 case 'g':
127                         debugging++;
128                         break;
129                 case 'p':
130                         profiling++;
131                         break;
132                 case 'V':
133                         printf("%d\n", CONFIGVERS);
134                         exit(0);
135                 case 'x':
136                         kernfile = optarg;
137                         break;
138                 case '?':
139                 default:
140                         usage();
141                 }
142         argc -= optind;
143         argv += optind;
144
145         if (kernfile != NULL) {
146                 kernconfdump(kernfile);
147                 exit(EXIT_SUCCESS);
148         }
149
150         if (argc != 1)
151                 usage();
152
153         PREFIX = *argv;
154         if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
155                 err(2, "%s", PREFIX);
156         if (freopen("DEFAULTS", "r", stdin) != NULL) {
157                 found_defaults = 1;
158                 yyfile = "DEFAULTS";
159         } else {
160                 if (freopen(PREFIX, "r", stdin) == NULL)
161                         err(2, "%s", PREFIX);
162                 yyfile = PREFIX;
163         }
164         if (*destdir != '\0') {
165                 len = strlen(destdir);
166                 while (len > 1 && destdir[len - 1] == '/')
167                         destdir[--len] = '\0';
168                 get_srcdir();
169         } else {
170                 strlcpy(destdir, CDIR, sizeof(destdir));
171                 strlcat(destdir, PREFIX, sizeof(destdir));
172         }
173
174         p = path((char *)NULL);
175         if (stat(p, &buf)) {
176                 if (mkdir(p, 0777))
177                         err(2, "%s", p);
178         } else if (!S_ISDIR(buf.st_mode))
179                 errx(EXIT_FAILURE, "%s isn't a directory", p);
180
181         SLIST_INIT(&cputype);
182         SLIST_INIT(&mkopt);
183         SLIST_INIT(&opt);
184         SLIST_INIT(&rmopts);
185         STAILQ_INIT(&cfgfiles);
186         STAILQ_INIT(&dtab);
187         STAILQ_INIT(&fntab);
188         STAILQ_INIT(&ftab);
189         STAILQ_INIT(&hints);
190         if (yyparse())
191                 exit(3);
192
193         /*
194          * Ensure that required elements (machine, cpu, ident) are present.
195          */
196         if (machinename == NULL) {
197                 printf("Specify machine type, e.g. ``machine i386''\n");
198                 exit(1);
199         }
200         if (ident == NULL) {
201                 printf("no ident line specified\n");
202                 exit(1);
203         }
204         if (SLIST_EMPTY(&cputype)) {
205                 printf("cpu type must be specified\n");
206                 exit(1);
207         }
208         checkversion();
209
210         /*
211          * make symbolic links in compilation directory
212          * for "sys" (to make genassym.c work along with #include <sys/xxx>)
213          * and similarly for "machine".
214          */
215         if (*srcdir == '\0')
216                 (void)snprintf(xxx, sizeof(xxx), "../../include");
217         else
218                 (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
219                     srcdir, machinename);
220         (void) unlink(path("machine"));
221         (void) symlink(xxx, path("machine"));
222         if (strcmp(machinename, machinearch) != 0) {
223                 /*
224                  * make symbolic links in compilation directory for
225                  * machinearch, if it is different than machinename.
226                  */
227                 if (*srcdir == '\0')
228                         (void)snprintf(xxx, sizeof(xxx), "../../../%s/include",
229                             machinearch);
230                 else
231                         (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
232                             srcdir, machinearch);
233                 (void) unlink(path(machinearch));
234                 (void) symlink(xxx, path(machinearch));
235         }
236         configfile();                   /* put config file into kernel*/
237         options();                      /* make options .h files */
238         makefile();                     /* build Makefile */
239         makeenv();                      /* build env.c */
240         makehints();                    /* build hints.c */
241         headers();                      /* make a lot of .h files */
242         cleanheaders(p);
243         printf("Kernel build directory is %s\n", p);
244         printf("Don't forget to do ``make cleandepend && make depend''\n");
245         exit(0);
246 }
247
248 /*
249  * get_srcdir
250  *      determine the root of the kernel source tree
251  *      and save that in srcdir.
252  */
253 static void
254 get_srcdir(void)
255 {
256         struct stat lg, phy;
257         char *p, *pwd;
258         int i;
259
260         if (realpath("../..", srcdir) == NULL)
261                 err(EXIT_FAILURE, "Unable to find root of source tree");
262         if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
263             (pwd = strdup(pwd)) != NULL) {
264                 /* Remove the last two path components. */
265                 for (i = 0; i < 2; i++) {
266                         if ((p = strrchr(pwd, '/')) == NULL) {
267                                 free(pwd);
268                                 return;
269                         }
270                         *p = '\0';
271                 }
272                 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
273                     lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
274                         strlcpy(srcdir, pwd, MAXPATHLEN);
275                 free(pwd);
276         }
277 }
278
279 static void
280 usage(void)
281 {
282
283         fprintf(stderr, "usage: config [-CgpV] [-d destdir] sysname\n");
284         fprintf(stderr, "       config -x kernel\n");
285         exit(EX_USAGE);
286 }
287
288 /*
289  * get_word
290  *      returns EOF on end of file
291  *      NULL on end of line
292  *      pointer to the word otherwise
293  */
294 char *
295 get_word(FILE *fp)
296 {
297         static char line[80];
298         int ch;
299         char *cp;
300         int escaped_nl = 0;
301
302 begin:
303         while ((ch = getc(fp)) != EOF)
304                 if (ch != ' ' && ch != '\t')
305                         break;
306         if (ch == EOF)
307                 return ((char *)EOF);
308         if (ch == '\\'){
309                 escaped_nl = 1;
310                 goto begin;
311         }
312         if (ch == '\n') {
313                 if (escaped_nl){
314                         escaped_nl = 0;
315                         goto begin;
316                 }
317                 else
318                         return (NULL);
319         }
320         cp = line;
321         *cp++ = ch;
322         while ((ch = getc(fp)) != EOF) {
323                 if (isspace(ch))
324                         break;
325                 *cp++ = ch;
326         }
327         *cp = 0;
328         if (ch == EOF)
329                 return ((char *)EOF);
330         (void) ungetc(ch, fp);
331         return (line);
332 }
333
334 /*
335  * get_quoted_word
336  *      like get_word but will accept something in double or single quotes
337  *      (to allow embedded spaces).
338  */
339 char *
340 get_quoted_word(FILE *fp)
341 {
342         static char line[256];
343         int ch;
344         char *cp;
345         int escaped_nl = 0;
346
347 begin:
348         while ((ch = getc(fp)) != EOF)
349                 if (ch != ' ' && ch != '\t')
350                         break;
351         if (ch == EOF)
352                 return ((char *)EOF);
353         if (ch == '\\'){
354                 escaped_nl = 1;
355                 goto begin;
356         }
357         if (ch == '\n') {
358                 if (escaped_nl){
359                         escaped_nl = 0;
360                         goto begin;
361                 }
362                 else
363                         return (NULL);
364         }
365         cp = line;
366         if (ch == '"' || ch == '\'') {
367                 int quote = ch;
368
369                 while ((ch = getc(fp)) != EOF) {
370                         if (ch == quote)
371                                 break;
372                         if (ch == '\n') {
373                                 *cp = 0;
374                                 printf("config: missing quote reading `%s'\n",
375                                         line);
376                                 exit(2);
377                         }
378                         *cp++ = ch;
379                 }
380         } else {
381                 *cp++ = ch;
382                 while ((ch = getc(fp)) != EOF) {
383                         if (isspace(ch))
384                                 break;
385                         *cp++ = ch;
386                 }
387                 if (ch != EOF)
388                         (void) ungetc(ch, fp);
389         }
390         *cp = 0;
391         if (ch == EOF)
392                 return ((char *)EOF);
393         return (line);
394 }
395
396 /*
397  * prepend the path to a filename
398  */
399 char *
400 path(const char *file)
401 {
402         char *cp = NULL;
403
404         if (file)
405                 asprintf(&cp, "%s/%s", destdir, file);
406         else
407                 cp = strdup(destdir);
408         return (cp);
409 }
410
411 /*
412  * Generate configuration file based on actual settings. With this mode, user
413  * will be able to obtain and build conifguration file with one command.
414  */
415 static void
416 configfile_dynamic(struct sbuf *sb)
417 {
418         struct cputype *cput;
419         struct device *d;
420         struct opt *ol;
421         char *lend;
422         unsigned int i;
423
424         asprintf(&lend, "\\n\\\n");
425         assert(lend != NULL);
426         sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
427         sbuf_printf(sb, "ident\t%s%s", ident, lend);
428         sbuf_printf(sb, "machine\t%s%s", machinename, lend);
429         SLIST_FOREACH(cput, &cputype, cpu_next)
430                 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
431         SLIST_FOREACH(ol, &mkopt, op_next)
432                 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
433                     ol->op_value, lend);
434         SLIST_FOREACH(ol, &opt, op_next) {
435                 if (strncmp(ol->op_name, "DEV_", 4) == 0)
436                         continue;
437                 sbuf_printf(sb, "options\t%s", ol->op_name);
438                 if (ol->op_value != NULL) {
439                         sbuf_putc(sb, '=');
440                         for (i = 0; i < strlen(ol->op_value); i++) {
441                                 if (ol->op_value[i] == '"')
442                                         sbuf_printf(sb, "\\%c",
443                                             ol->op_value[i]);
444                                 else
445                                         sbuf_printf(sb, "%c",
446                                             ol->op_value[i]);
447                         }
448                         sbuf_printf(sb, "%s", lend);
449                 } else {
450                         sbuf_printf(sb, "%s", lend);
451                 }
452         }
453         /*
454          * Mark this file as containing everything we need.
455          */
456         STAILQ_FOREACH(d, &dtab, d_next)
457                 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
458         free(lend);
459 }
460
461 /*
462  * Generate file from the configuration files.
463  */
464 static void
465 configfile_filebased(struct sbuf *sb)
466 {
467         FILE *cff;
468         struct cfgfile *cf;
469         int i;
470
471         /*
472          * Try to read all configuration files. Since those will be present as
473          * C string in the macro, we have to slash their ends then the line
474          * wraps.
475          */
476         STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
477                 cff = fopen(cf->cfg_path, "r");
478                 if (cff == NULL) {
479                         warn("Couldn't open file %s", cf->cfg_path);
480                         continue;
481                 }
482                 while ((i = getc(cff)) != EOF) {
483                         if (i == '\n')
484                                 sbuf_printf(sb, "\\n\\\n");
485                         else if (i == '"' || i == '\'')
486                                 sbuf_printf(sb, "\\%c", i);
487                         else
488                                 sbuf_putc(sb, i);
489                 }
490                 fclose(cff);
491         }
492 }
493
494 static void
495 configfile(void)
496 {
497         FILE *fo;
498         struct sbuf *sb;
499         char *p;
500
501         /* Add main configuration file to the list of files to be included */
502         cfgfile_add(PREFIX);
503         p = path("config.c.new");
504         fo = fopen(p, "w");
505         if (!fo)
506                 err(2, "%s", p);
507         sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
508         assert(sb != NULL);
509         sbuf_clear(sb);
510         if (filebased) {
511                 /* Is needed, can be used for backward compatibility. */
512                 configfile_filebased(sb);
513         } else {
514                 configfile_dynamic(sb);
515         }
516         sbuf_finish(sb);
517         /* 
518          * We print first part of the template, replace our tag with
519          * configuration files content and later continue writing our
520          * template.
521          */
522         p = strstr(kernconfstr, KERNCONFTAG);
523         if (p == NULL)
524                 errx(EXIT_FAILURE, "Something went terribly wrong!");
525         *p = '\0';
526         fprintf(fo, "%s", kernconfstr);
527         fprintf(fo, "%s", sbuf_data(sb));
528         p += strlen(KERNCONFTAG);
529         fprintf(fo, "%s", p);
530         sbuf_delete(sb);
531         fclose(fo);
532         moveifchanged(path("config.c.new"), path("config.c"));
533         cfgfile_removeall();
534 }
535
536 /*
537  * moveifchanged --
538  *      compare two files; rename if changed.
539  */
540 void
541 moveifchanged(const char *from_name, const char *to_name)
542 {
543         char *p, *q;
544         int changed;
545         size_t tsize;
546         struct stat from_sb, to_sb;
547         int from_fd, to_fd;
548
549         changed = 0;
550
551         if ((from_fd = open(from_name, O_RDONLY)) < 0)
552                 err(EX_OSERR, "moveifchanged open(%s)", from_name);
553
554         if ((to_fd = open(to_name, O_RDONLY)) < 0)
555                 changed++;
556
557         if (!changed && fstat(from_fd, &from_sb) < 0)
558                 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
559
560         if (!changed && fstat(to_fd, &to_sb) < 0)
561                 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
562
563         if (!changed && from_sb.st_size != to_sb.st_size)
564                 changed++;
565
566         tsize = (size_t)from_sb.st_size;
567
568         if (!changed) {
569                 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
570                 if (p == MAP_FAILED)
571                         err(EX_OSERR, "mmap %s", from_name);
572                 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
573                 if (q == MAP_FAILED)
574                         err(EX_OSERR, "mmap %s", to_name);
575
576                 changed = memcmp(p, q, tsize);
577                 munmap(p, tsize);
578                 munmap(q, tsize);
579         }
580         if (changed) {
581                 if (rename(from_name, to_name) < 0)
582                         err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
583         } else {
584                 if (unlink(from_name) < 0)
585                         err(EX_OSERR, "unlink(%s)", from_name);
586         }
587 }
588
589 static void
590 cleanheaders(char *p)
591 {
592         DIR *dirp;
593         struct dirent *dp;
594         struct file_list *fl;
595         struct hdr_list *hl;
596         int i;
597
598         remember("y.tab.h");
599         remember("setdefs.h");
600         STAILQ_FOREACH(fl, &ftab, f_next)
601                 remember(fl->f_fn);
602
603         /*
604          * Scan the build directory and clean out stuff that looks like
605          * it might have been a leftover NFOO header, etc.
606          */
607         if ((dirp = opendir(p)) == NULL)
608                 err(EX_OSERR, "opendir %s", p);
609         while ((dp = readdir(dirp)) != NULL) {
610                 i = dp->d_namlen - 2;
611                 /* Skip non-headers */
612                 if (dp->d_name[i] != '.' || dp->d_name[i + 1] != 'h')
613                         continue;
614                 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
615                 if (index(dp->d_name, '_') &&
616                     strncmp(dp->d_name, "opt_", 4) != 0)
617                         continue;
618                 /* Check if it is a target file */
619                 for (hl = htab; hl != NULL; hl = hl->h_next) {
620                         if (eq(dp->d_name, hl->h_name)) {
621                                 break;
622                         }
623                 }
624                 if (hl)
625                         continue;
626                 printf("Removing stale header: %s\n", dp->d_name);
627                 if (unlink(path(dp->d_name)) == -1)
628                         warn("unlink %s", dp->d_name);
629         }
630         (void)closedir(dirp);
631 }
632
633 void
634 remember(const char *file)
635 {
636         char *s;
637         struct hdr_list *hl;
638
639         if ((s = strrchr(file, '/')) != NULL)
640                 s = ns(s + 1);
641         else
642                 s = ns(file);
643
644         if (index(s, '_') && strncmp(s, "opt_", 4) != 0) {
645                 free(s);
646                 return;
647         }
648         for (hl = htab; hl != NULL; hl = hl->h_next) {
649                 if (eq(s, hl->h_name)) {
650                         free(s);
651                         return;
652                 }
653         }
654         hl = calloc(1, sizeof(*hl));
655         if (hl == NULL)
656                 err(EXIT_FAILURE, "calloc");
657         hl->h_name = s;
658         hl->h_next = htab;
659         htab = hl;
660 }
661
662 /*
663  * This one is quick hack. Will be probably moved to elf(3) interface.
664  * It takes kernel configuration file name, passes it as an argument to
665  * elfdump -a, which output is parsed by some UNIX tools...
666  */
667 static void
668 kernconfdump(const char *file)
669 {
670         struct stat st;
671         FILE *fp, *pp;
672         int error, len, osz, r;
673         unsigned int i, off, size, t1, t2, align;
674         char *cmd, *o;
675
676         r = open(file, O_RDONLY);
677         if (r == -1)
678                 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
679         error = fstat(r, &st);
680         if (error == -1)
681                 err(EXIT_FAILURE, "fstat() failed");
682         if (S_ISDIR(st.st_mode))
683                 errx(EXIT_FAILURE, "'%s' is a directory", file);
684         fp = fdopen(r, "r");
685         if (fp == NULL)
686                 err(EXIT_FAILURE, "fdopen() failed");
687         osz = 1024;
688         o = calloc(1, osz);
689         if (o == NULL)
690                 err(EXIT_FAILURE, "Couldn't allocate memory");
691         /* ELF note section header. */
692         asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
693             "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
694         if (cmd == NULL)
695                 errx(EXIT_FAILURE, "asprintf() failed");
696         pp = popen(cmd, "r");
697         if (pp == NULL)
698                 errx(EXIT_FAILURE, "popen() failed");
699         free(cmd);
700         len = fread(o, osz, 1, pp);
701         pclose(pp);
702         r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
703         free(o);
704         if (r != 5)
705                 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
706                     "file. Either unsupported, or not compiled with "
707                     "INCLUDE_CONFIG_FILE", file);
708         r = fseek(fp, off, SEEK_CUR);
709         if (r != 0)
710                 err(EXIT_FAILURE, "fseek() failed");
711         for (i = 0; i < size; i++) {
712                 r = fgetc(fp);
713                 if (r == EOF)
714                         break;
715                 /* 
716                  * If '\0' is present in the middle of the configuration
717                  * string, this means something very weird is happening.
718                  * Make such case very visible.  However, some architectures
719                  * pad the length of the section with NULs to a multiple of
720                  * sh_addralign, allow a NUL in that part of the section.
721                  */
722                 if (r == '\0' && (size - i) < align)
723                         break;
724                 assert(r != '\0' && ("Char present in the configuration "
725                     "string mustn't be equal to 0"));
726                 fputc(r, stdout);
727         }
728         fclose(fp);
729 }
730
731 static void 
732 badversion(int versreq)
733 {
734         fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
735         fprintf(stderr, "config version = %d, ", CONFIGVERS);
736         fprintf(stderr, "version required = %d\n\n", versreq);
737         fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
738         fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
739         fprintf(stderr, "before trying this again.\n\n");
740         fprintf(stderr, "If running the new config fails check your config\n");
741         fprintf(stderr, "file against the GENERIC or LINT config files for\n");
742         fprintf(stderr, "changes in config syntax, or option/device naming\n");
743         fprintf(stderr, "conventions\n\n");
744         exit(1);
745 }
746
747 static void
748 checkversion(void)
749 {
750         FILE *ifp;
751         char line[BUFSIZ];
752         int versreq;
753
754         ifp = open_makefile_template();
755         while (fgets(line, BUFSIZ, ifp) != 0) {
756                 if (*line != '%')
757                         continue;
758                 if (strncmp(line, "%VERSREQ=", 9) != 0)
759                         continue;
760                 versreq = atoi(line + 9);
761                 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
762                     versreq <= CONFIGVERS)
763                         continue;
764                 badversion(versreq);
765         }
766         fclose(ifp);
767 }