Enforce COMPAT_DF12 for now, this can be overriden via NO_COMPAT_DF12
[dragonfly.git] / usr.sbin / config / mkoptions.c
1 /*
2  * Copyright (c) 1995  Peter Wemm
3  * Copyright (c) 1980, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by the University of
17  *      California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * @(#)mkheaders.c      8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.sbin/config/mkoptions.c,v 1.17.2.3 2001/12/13 19:18:01 dillon Exp $
36  * $DragonFly: src/usr.sbin/config/mkoptions.c,v 1.15 2005/08/02 13:18:25 joerg Exp $
37  */
38
39 /*
40  * Make all the .h files for the optional entries
41  */
42
43 #include <ctype.h>
44 #include <err.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <sys/param.h>
48 #include "config.h"
49 #include "y.tab.h"
50
51 static char *lower(char *);
52 static void read_options(void);
53 static void do_option(char *);
54 static char *tooption(char *);
55
56 void
57 options(void)
58 {
59         char buf[40];
60         struct cputype *cp;
61         struct opt_list *ol;
62         struct opt *op;
63
64         /* Fake the cpu types as options. */
65         for (cp = cputype; cp != NULL; cp = cp->cpu_next) {
66                 op = malloc(sizeof(*op));
67                 bzero(op, sizeof(*op));
68                 op->op_name = strdup(cp->cpu_name);
69                 op->op_next = opt;
70                 opt = op;
71         }       
72
73         if (maxusers == 0) {
74                 /* printf("maxusers not specified; will auto-size\n"); */
75                 /* maxusers = 0; */
76         } else if (maxusers < 2) {
77                 puts("minimum of 2 maxusers assumed");
78                 maxusers = 2;
79         } else if (maxusers > 512) {
80                 printf("warning: maxusers > 512 (%d)\n", maxusers);
81         }
82
83         /* Fake MAXUSERS as an option. */
84         op = malloc(sizeof(*op));
85         bzero(op, sizeof(*op));
86         op->op_name = strdup("MAXUSERS");
87         snprintf(buf, sizeof(buf), "%d", maxusers);
88         op->op_value = strdup(buf);
89         op->op_next = opt;
90         opt = op;
91
92         read_options();
93         for (ol = otab; ol != NULL; ol = ol->o_next)
94                 do_option(ol->o_name);
95         for (op = opt; op != NULL; op = op->op_next) {
96                 if (!op->op_ownfile) {
97                         printf("%s:%d: unknown option \"%s\"\n",
98                                PREFIX, op->op_line, op->op_name);
99                         exit(1);
100                 }
101         }
102 }
103
104 /*
105  * Generate an <options>.h file
106  */
107
108 static void
109 do_option(char *name)
110 {
111         const char *basefile, *file;
112         char *inw;
113         struct opt_list *ol;
114         struct opt *op, *op_head, *topp;
115         FILE *inf, *outf;
116         char *value;
117         char *oldvalue;
118         int seen;
119         int tidy;
120
121         file = tooption(name);
122
123         /*
124          * Check to see if the option was specified..
125          */
126         value = NULL;
127         for (op = opt; op != NULL; op = op->op_next) {
128                 if (strcmp(name, op->op_name) == 0) {
129                         oldvalue = value;
130                         value = op->op_value;
131                         if (value == NULL)
132                                 value = strdup("1");
133                         if (oldvalue != NULL && strcmp(value, oldvalue) != 0)
134                                 printf(
135                             "%s:%d: option \"%s\" redefined from %s to %s\n",
136                                    PREFIX, op->op_line, op->op_name, oldvalue,
137                                    value);
138                         op->op_ownfile++;
139                 }
140         }
141
142         if (strcmp(name, "COMPAT_DF12") == 0 &&
143             getenv("NO_COMPAT_DF12") == NULL &&
144             (value == NULL || strcmp(value, "1") != 0))
145                 errx(1, "options COMPAT_DF12 not specified, "
146                      "set NO_COMPAT_DF12 to ignore");
147
148         inf = fopen(file, "r");
149         if (inf == NULL) {
150                 outf = fopen(file, "w");
151                 if (outf == NULL)
152                         err(1, "%s", file);
153
154                 /* was the option in the config file? */
155                 if (value) {
156                         fprintf(outf, "#define %s %s\n", name, value);
157                 } /* else empty file */
158
159                 fclose(outf);
160                 return;
161         }
162         basefile = "";
163         for (ol = otab; ol != NULL; ol = ol->o_next)
164                 if (strcmp(name, ol->o_name) == 0) {
165                         basefile = ol->o_file;
166                         break;
167                 }
168         oldvalue = NULL;
169         op_head = NULL;
170         seen = 0;
171         tidy = 0;
172         for (;;) {
173                 char *cp;
174                 char *invalue;
175
176                 /* get the #define */
177                 if ((inw = get_word(inf)) == NULL || inw == (char *)EOF)
178                         break;
179                 /* get the option name */
180                 if ((inw = get_word(inf)) == NULL || inw == (char *)EOF)
181                         break;
182                 inw = strdup(inw);
183                 /* get the option value */
184                 if ((cp = get_word(inf)) == NULL || cp == (char *)EOF)
185                         break;
186                 /* option value */
187                 invalue = strdup(cp); /* malloced */
188                 if (strcmp(inw, name) == 0) {
189                         oldvalue = invalue;
190                         invalue = value;
191                         seen++;
192                 }
193                 for (ol = otab; ol != NULL; ol = ol->o_next)
194                         if (strcmp(inw, ol->o_name) == 0)
195                                 break;
196                 if (strcmp(inw, name) != 0 && ol == NULL) {
197                         printf("WARNING: unknown option `%s' removed from %s\n",
198                                 inw, file);
199                         tidy++;
200                 } else if (ol != NULL && strcmp(basefile, ol->o_file) != 0) {
201                         printf("WARNING: option `%s' moved from %s to %s\n",
202                                 inw, basefile, ol->o_file);
203                         tidy++;
204                 } else {
205                         op = malloc(sizeof(*op));
206                         bzero(op, sizeof(*op));
207                         op->op_name = inw;
208                         op->op_value = invalue;
209                         op->op_next = op_head;
210                         op_head = op;
211                 }
212
213                 /* EOL? */
214                 cp = get_word(inf);
215                 if (cp == (char *)EOF)
216                         break;
217         }
218         fclose(inf);
219         if (!tidy && ((value == NULL && oldvalue == NULL) ||
220             (value && oldvalue && strcmp(value, oldvalue) == 0))) {     
221                 for (op = op_head; op != NULL; op = topp) {
222                         topp = op->op_next;
223                         free(op->op_name);
224                         free(op->op_value);
225                         free(op);
226                 }
227                 return;
228         }
229
230         if (value != NULL && !seen) {
231                 /* New option appears */
232                 op = malloc(sizeof(*op));
233                 bzero(op, sizeof(*op));
234                 op->op_name = strdup(name);
235                 op->op_value = value != NULL ? strdup(value) : NULL;
236                 op->op_next = op_head;
237                 op_head = op;
238         }
239
240         outf = fopen(file, "w");
241         if (outf == 0)
242                 err(1, "%s", file);
243         for (op = op_head; op != NULL; op = topp) {
244                 /* was the option in the config file? */
245                 if (op->op_value != NULL) {
246                         fprintf(outf, "#define %s %s\n",
247                                 op->op_name, op->op_value);
248                 }
249                 topp = op->op_next;
250                 free(op->op_name);
251                 free(op->op_value);
252                 free(op);
253         }
254         fclose(outf);
255 }
256
257 /*
258  * Find the filename to store the option spec into.
259  */
260 static char *
261 tooption(char *name)
262 {
263         static char hbuf[MAXPATHLEN];
264         char nbuf[MAXPATHLEN];
265         struct opt_list *po;
266
267         /* "cannot happen"?  the otab list should be complete.. */
268         strlcpy(nbuf, "options.h", sizeof(nbuf));
269
270         for (po = otab ; po != NULL; po = po->o_next) {
271                 if (strcmp(po->o_name, name) == 0) {
272                         strlcpy(nbuf, po->o_file, sizeof(nbuf));
273                         break;
274                 }
275         }
276
277         strlcpy(hbuf, path(nbuf), sizeof(hbuf));
278         return(hbuf);
279 }
280
281 /*
282  * read the options and options.<machine> files
283  */
284 static void
285 read_options(void)
286 {
287         FILE *fp;
288         char fname[MAXPATHLEN];
289         char *wd, *this, *val;
290         struct opt_list *po;
291         int first = 1;
292         char genopt[MAXPATHLEN];
293
294         otab = NULL;
295         if (ident == NULL) {
296                 printf("no ident line specified\n");
297                 exit(1);
298         }
299         snprintf(fname, sizeof(fname), "../../conf/options");
300 openit:
301         fp = fopen(fname, "r");
302         if (fp == NULL) {
303                 return;
304         }
305 next:
306         wd = get_word(fp);
307         if (wd == (char *)EOF) {
308                 fclose(fp);
309                 if (first == 1) {
310                         first++;
311                         snprintf(fname, sizeof(fname), "../../conf/options.%s", machinename);
312                         fp = fopen(fname, "r");
313                         if (fp != NULL)
314                                 goto next;
315                         snprintf(fname, sizeof(fname), "options.%s", machinename);
316                         goto openit;
317                 }
318                 if (first == 2) {
319                         first++;
320                         snprintf(fname, sizeof(fname), "options.%s", raisestr(ident));
321                         fp = fopen(fname, "r");
322                         if (fp != NULL)
323                                 goto next;
324                 }
325                 return;
326         }
327         if (wd == NULL)
328                 goto next;
329         if (wd[0] == '#')
330         {
331                 while (((wd = get_word(fp)) != (char *)EOF) && wd != NULL)
332                         ;
333                 goto next;
334         }
335         this = strdup(wd);
336         val = get_word(fp);
337         if (val == (char *)EOF)
338                 return;
339         if (val == 0) {
340                 char *s;
341         
342                 s = strdup(this);
343                 snprintf(genopt, sizeof(genopt), "opt_%s.h", lower(s));
344                 val = genopt;
345                 free(s);
346         }
347         val = strdup(val);
348
349         for (po = otab; po != NULL; po = po->o_next) {
350                 if (strcmp(po->o_name, this) == 0) {
351                         printf("%s: Duplicate option %s.\n",
352                                fname, this);
353                         exit(1);
354                 }
355         }
356         
357         po = malloc(sizeof(*po));
358         bzero(po, sizeof(*po));
359         po->o_name = this;
360         po->o_file = val;
361         po->o_next = otab;
362         otab = po;
363
364         goto next;
365 }
366
367 static char *
368 lower(char *str)
369 {
370         char *cp = str;
371
372         while (*str) {
373                 if (isupper(*str))
374                         *str = tolower(*str);
375                 str++;
376         }
377         return(cp);
378 }
379