import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / scripts / mkconfigs.c
1 /***************************************************************************
2  * mkconfigs.c
3  * (C) 2002 Randy Dunlap <rddunlap@osdl.org>
4
5 #   This program is free software; you can redistribute it and/or modify
6 #   it under the terms of the GNU General Public License as published by
7 #   the Free Software Foundation; either version 2 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU General Public License for more details.
14 #
15 #   You should have received a copy of the GNU General Public License
16 #   along with this program; if not, write to the Free Software
17 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 # Rules for scripts/mkconfigs.c input.config output.c
20 # to generate configs.c from linux/.config:
21 # - drop lines that begin with '#'
22 # - drop blank lines
23 # - lines that use double-quotes must \\-escape-quote them
24
25 ################## skeleton configs.c file: ####################
26
27 #include <linux/init.h>
28 #include <linux/module.h>
29
30 static char *configs[] __initdata =
31
32   <insert lines selected lines of .config, quoted, with added '\n'>,
33
34 ;
35
36 ################### end configs.c file ######################
37
38  * Changelog for ver. 0.2, 2002-02-15, rddunlap@osdl.org:
39  - strip leading "CONFIG_" from config option strings;
40  - use "static" and "__attribute__((unused))";
41  - don't use EXPORT_SYMBOL();
42  - separate each config line with \newline instead of space;
43
44  * Changelog for ver. 0.3, 2002-02-18, rddunlap@osdl.org:
45  - keep all "not set" comment lines from .config so that 'make *config'
46    will be happy, but don't keep other comments;
47  - keep leading "CONFIG_" on each line;
48
49 ****************************************************************/
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <errno.h>
55
56 #define VERSION         "0.2"
57 #define LINE_SIZE       1000
58
59 int include_all_lines = 1;      // whether to include "=n" lines in the output
60
61 void usage (const char *progname)
62 {
63         fprintf (stderr, "%s ver. %s\n", progname, VERSION);
64         fprintf (stderr, "usage:  %s input.config.name path/configs.c\n",
65                         progname);
66         exit (1);
67 }
68
69 void make_intro (FILE *sourcefile)
70 {
71         fprintf (sourcefile, "#include <linux/init.h>\n");
72 /////   fprintf (sourcefile, "#include <linux/module.h>\n");
73         fprintf (sourcefile, "\n");
74 /////   fprintf (sourcefile, "char *configs[] __initdata = {\n");
75         fprintf (sourcefile, "static char __attribute__ ((unused)) *configs[] __initdata = {\n");
76         fprintf (sourcefile, "  \"CONFIG_BEGIN=n\\n\" ,\n");
77 }
78
79 void make_ending (FILE *sourcefile)
80 {
81         fprintf (sourcefile, "  \"CONFIG_END=n\\n\"\n");
82         fprintf (sourcefile, "};\n");
83 /////   fprintf (sourcefile, "EXPORT_SYMBOL (configs);\n");
84 }
85
86 void make_lines (FILE *configfile, FILE *sourcefile)
87 {
88         char cfgline[LINE_SIZE];
89         char *ch;
90
91         while (fgets (cfgline, LINE_SIZE, configfile)) {
92                 /* kill the trailing newline in cfgline */
93                 cfgline[strlen (cfgline) - 1] = '\0';
94
95                 /* don't keep #-only line or an empty/blank line */
96                 if ((cfgline[0] == '#' && cfgline[1] == '\0') ||
97                     cfgline[0] == '\0')
98                         continue;
99
100                 if (!include_all_lines &&
101                     cfgline[0] == '#') // strip out all comment lines
102                         continue;
103
104                 /* really only want to keep lines that begin with
105                  * "CONFIG_" or "# CONFIG_" */
106                 if (strncmp (cfgline, "CONFIG_", 7) &&
107                     strncmp (cfgline, "# CONFIG_", 9))
108                         continue;
109
110                 /*
111                  * use strchr() to check for "-quote in cfgline;
112                  * if not found, output the line, quoted;
113                  * if found, output a char at a time, with \\-quote
114                  * preceding double-quote chars
115                  */
116                 if (!strchr (cfgline, '"')) {
117                         fprintf (sourcefile, "  \"%s\\n\" ,\n", cfgline);
118                         continue;
119                 }
120
121                 /* go to char-at-a-time mode for this config and
122                  * precede any double-quote with a backslash */
123                 fprintf (sourcefile, "  \"");   /* lead-in */
124                 for (ch = cfgline; *ch; ch++) {
125                         if (*ch == '"')
126                                 fputc ('\\', sourcefile);
127                         fputc (*ch, sourcefile);
128                 }
129                 fprintf (sourcefile, "\\n\" ,\n");
130         }
131 }
132
133 void make_configs (FILE *configfile, FILE *sourcefile)
134 {
135         make_intro (sourcefile);
136         make_lines (configfile, sourcefile);
137         make_ending (sourcefile);
138 }
139
140 int main (int argc, char *argv[])
141 {
142         char *progname = argv[0];
143         char *configname, *sourcename;
144         FILE *configfile, *sourcefile;
145
146         if (argc != 3)
147                 usage (progname);
148
149         configname = argv[1];
150         sourcename = argv[2];
151
152         configfile = fopen (configname, "r");
153         if (!configfile) {
154                 fprintf (stderr, "%s: cannot open '%s'\n",
155                                 progname, configname);
156                 exit (2);
157         }
158         sourcefile = fopen (sourcename, "w");
159         if (!sourcefile) {
160                 fprintf (stderr, "%s: cannot open '%s'\n",
161                                 progname, sourcename);
162                 exit (2);
163         }
164
165         make_configs (configfile, sourcefile);
166
167         if (fclose (sourcefile)) {
168                 fprintf (stderr, "%s: error %d closing '%s'\n",
169                                 progname, errno, sourcename);
170                 exit (3);
171         }
172         if (fclose (configfile)) {
173                 fprintf (stderr, "%s: error %d closing '%s'\n",
174                                 progname, errno, configname);
175                 exit (3);
176         }
177
178         exit (0);
179 }
180
181 /* end mkconfigs.c */