1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
59int include_all_lines = 1;
60
61void 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
69void make_intro (FILE *sourcefile)
70{
71 fprintf (sourcefile, "#include <linux/init.h>\n");
72
73 fprintf (sourcefile, "\n");
74
75 fprintf (sourcefile, "static char __attribute__ ((unused)) *configs[] __initdata = {\n");
76 fprintf (sourcefile, " \"CONFIG_BEGIN=n\\n\" ,\n");
77}
78
79void make_ending (FILE *sourcefile)
80{
81 fprintf (sourcefile, " \"CONFIG_END=n\\n\"\n");
82 fprintf (sourcefile, "};\n");
83
84}
85
86void make_lines (FILE *configfile, FILE *sourcefile)
87{
88 char cfgline[LINE_SIZE];
89 char *ch;
90
91 while (fgets (cfgline, LINE_SIZE, configfile)) {
92
93 cfgline[strlen (cfgline) - 1] = '\0';
94
95
96 if ((cfgline[0] == '#' && cfgline[1] == '\0') ||
97 cfgline[0] == '\0')
98 continue;
99
100 if (!include_all_lines &&
101 cfgline[0] == '#')
102 continue;
103
104
105
106 if (strncmp (cfgline, "CONFIG_", 7) &&
107 strncmp (cfgline, "# CONFIG_", 9))
108 continue;
109
110
111
112
113
114
115
116 if (!strchr (cfgline, '"')) {
117 fprintf (sourcefile, " \"%s\\n\" ,\n", cfgline);
118 continue;
119 }
120
121
122
123 fprintf (sourcefile, " \"");
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
133void make_configs (FILE *configfile, FILE *sourcefile)
134{
135 make_intro (sourcefile);
136 make_lines (configfile, sourcefile);
137 make_ending (sourcefile);
138}
139
140int 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
182