comparison helpers/cfg-hdr-gen.c @ 388:b8b08c302ace

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