add a hack for Decap Attack
[picodrive.git] / tools / make_carthw_c.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
4
5 int main(int argc, char *argv[])
6 {
7         FILE *fi, *fo;
8         char buf[256];
9
10         if (argc != 3) {
11                 printf("usage:\n%s <carthw.cfg> <carthw.c>\n", argv[0]);
12                 return 1;
13         }
14
15         fi = fopen(argv[1], "r");
16         fo = fopen(argv[2], "w");
17         if (fi == NULL || fo == NULL) {
18                 printf("fopen failed\n");
19                 return 1;
20         }
21
22         fprintf(fo, "/* generated by %s, do not modify */\n", argv[0]);
23         fprintf(fo, "static const char builtin_carthw_cfg[] =\n");
24
25         while ((fgets(buf, sizeof(buf), fi)))
26         {
27                 char bufd[256];
28                 char *d = bufd, *p = buf;
29                 int quote = 0;
30
31                 while (*p && isspace(*p))
32                         p++;
33
34                 if (*p == 0 || *p == '#')
35                         continue;
36
37                 /* section names not needed */
38                 if (*p == '[')
39                         strcpy(p, "[]");
40
41                 for (; *p != 0; p++) {
42                         if (!quote && isspace(*p))
43                                 continue;
44                         if (*p == '"') {
45                                 quote = !quote;
46                                 *d++ = '\\';
47                         }
48                         *d++ = *p;
49                 }
50                 *d = 0;
51
52
53                 fprintf(fo, "  \"%s\\n\"\n", bufd);
54         }
55
56         fprintf(fo, ";\n");
57
58         fclose(fi);
59         fclose(fo);
60
61         return 0;
62 }