annotate helpers/cfg-hdr-gen.c @ 686:59f07d67eb45

luna target split into luna1 and luna2 luna1 is FC Luna based on iWOW DSK v4.0 or v5.0 motherboard luna2 is FC Luna based on FC Caramel2 MB
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 12 Oct 2020 18:51:24 +0000
parents b8b08c302ace
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
388
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This helper program generates the set of *.cfg header files, based on a
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * template file and environment variables for the non-constant settings.
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <unistd.h>
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <ctype.h>
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <string.h>
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <strings.h>
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 char *infname;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 FILE *inf, *outf;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 char include_guard_symbol[32];
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 char linebuf[256];
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 int lineno;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 make_include_guard_symbol(fname)
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 char *fname;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 char *sp, *dp;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 int c;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 sp = fname;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 dp = include_guard_symbol;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 *dp++ = '_';
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 *dp++ = '_';
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 while (c = *sp++) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (islower(c))
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 c = toupper(c);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 else if (c == '.')
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 c = '_';
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 *dp++ = c;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 *dp++ = '_';
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 *dp++ = '_';
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 *dp = '\0';
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 close_output()
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (outf) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 fprintf(outf, "#endif /* %s */\n", include_guard_symbol);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 fclose(outf);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 outf = 0;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 bracket_line()
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 char *cp;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 close_output();
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 cp = index(linebuf+1, ']');
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (!cp) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 fprintf(stderr, "%s line %d: unterminated bracket line\n",
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 infname, lineno);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 exit(1);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 *cp = '\0';
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 outf = fopen(linebuf+1, "w");
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (!outf) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 perror(linebuf+1);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 exit(1);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 make_include_guard_symbol(linebuf+1);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 fprintf(outf, "#ifndef %s\n", include_guard_symbol);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 fprintf(outf, "#define %s\n", include_guard_symbol);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 process_line()
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 char *cp, *symbol, *value;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 if (linebuf[0] == '[')
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 return bracket_line();
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 for (cp = linebuf; isspace(*cp); cp++)
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 ;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 if (*cp == '\0' || *cp == '#')
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 return;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 for (symbol = cp; *cp && !isspace(*cp); cp++)
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 ;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 if (!*cp) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 inv: fprintf(stderr, "%s line %d: expected two fields\n",
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 infname, lineno);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 exit(1);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 *cp++ = '\0';
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 while (isspace(*cp))
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 cp++;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 if (*cp == '\0' || *cp == '#')
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 goto inv;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 for (value = cp; *cp && !isspace(*cp); cp++)
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 ;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 if (*cp)
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 *cp++ = '\0';
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 while (isspace(*cp))
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 cp++;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 if (*cp != '\0' && *cp != '#')
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 goto inv;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 if (!strcmp(value, "var")) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 value = getenv(symbol);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 if (!value) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 fprintf(stderr,
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 "%s line %d: no environment variable named %s\n",
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 infname, lineno, symbol);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 exit(1);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 if (!outf) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 fprintf(stderr, "%s line %d: no open output file\n",
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 infname, lineno);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 exit(1);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 fprintf(outf, "#define %s %s\n", symbol, value);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 main(argc, argv)
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 char **argv;
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 if (argc != 3) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 fprintf(stderr, "usage: %s template-file output-dir\n",
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 argv[0]);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 exit(1);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 infname = argv[1];
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 inf = fopen(infname, "r");
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 if (!inf) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 perror(infname);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 exit(1);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 if (chdir(argv[2]) < 0) {
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 perror(argv[2]);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 exit(1);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 }
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 process_line();
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 close_output();
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 exit(0);
b8b08c302ace cfg-hdr-gen C helper program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 }