3 A very, very (very!) simple program to process a config_h.sh file on
7 munchconfig config.sh config_h.sh [-f file] [foo=bar [baz=xyzzy [...]]] >config.h
9 which is to say, it takes as its first parameter a config.sh (or
10 equivalent), as its second a config_h.sh (or equivalent), an optional file
11 containing tag=value pairs (one on each line), and an optional list of
12 tag=value pairs on the command line.
14 It spits the processed config.h out to STDOUT.
25 /* The failure code to exit with */
28 #define EXIT_FAILURE 0
30 #define EXIT_FAILURE -1
34 /* The biggest line we can read in from a file */
35 #define LINEBUFFERSIZE 1024
36 #define NUMTILDESUBS 30
37 #define NUMCONFIGSUBS 1000
38 #define TOKENBUFFERSIZE 80
41 char Tag[TOKENBUFFERSIZE];
45 void tilde_sub(char [], Translate [], int);
48 main(int argc, char *argv[])
52 char WorkString[LINEBUFFERSIZE];
53 FILE *ConfigSH, *Config_H, *Extra_Subs;
54 char LineBuffer[LINEBUFFERSIZE], *TempValue, *StartTilde, *EndTilde;
55 char SecondaryLineBuffer[LINEBUFFERSIZE], OutBuf[LINEBUFFERSIZE];
56 char TokenBuffer[TOKENBUFFERSIZE];
57 int LineBufferLength, TempLength, DummyVariable, LineBufferLoop;
58 int TokenBufferLoop, ConfigSubLoop, GotIt, OutBufPos;
59 Translate TildeSub[NUMTILDESUBS]; /* Holds the tilde (~FOO~) */
61 Translate ConfigSub[NUMCONFIGSUBS]; /* Holds the substitutions from */
63 int TildeSubCount = 0, ConfigSubCount = 0; /* # of tilde substitutions */
64 /* and config substitutions, */
67 printf("Usage: munchconfig config.sh config_h.sh [-f file] [foo=bar [baz=xyzzy [...]]]\n");
71 optind = 3; /* skip config.sh and config_h.sh */
72 while ((c = getopt(argc, argv, "f:")) != -1) {
78 fprintf(stderr, "Option -%c requires an operand\n", optopt);
81 fprintf(stderr,"Unrecognised option: -%c\n", optopt);
85 /* First, open the input files */
86 if (NULL == (ConfigSH = fopen(argv[1], "r"))) {
87 printf("Error %i trying to open config.sh file %s\n", errno, argv[1]);
91 if (NULL == (Config_H = fopen(argv[2], "r"))) {
92 printf("Error %i trying to open config_h.sh file %s\n", errno, argv[2]);
96 if (ifile != NULL && NULL == (Extra_Subs = fopen(ifile, "r"))) {
97 printf("Error %i trying to open extra substitutions file %s\n", errno, ifile);
101 /* Any tag/value pairs on the command line? */
103 for (i=optind; i < argc && argv[i]; i++) {
105 strcpy(WorkString, argv[i]);
106 /* Stick a NULL over the = */
107 TempValue = strchr(WorkString, '=');
110 /* Copy the tag and value into the holding array */
111 strcpy(TildeSub[TildeSubCount].Tag, WorkString);
112 strcpy(TildeSub[TildeSubCount].Value, TempValue);
117 /* Now read in the tag/value pairs from the extra substitutions file, if any */
118 while(ifile && fgets(LineBuffer, LINEBUFFERSIZE - 1, Extra_Subs)) {
119 /* Force a trailing null, just in case */
120 LineBuffer[LINEBUFFERSIZE - 1] = '\0';
121 LineBufferLength = strlen(LineBuffer);
123 /* Chop trailing control characters */
124 while((LineBufferLength > 0) && (LineBuffer[LineBufferLength-1] < ' ')) {
125 LineBuffer[LineBufferLength - 1] = '\0';
129 /* If it's empty, then try again */
134 strcpy(WorkString, LineBuffer);
135 /* Stick a NULL over the = */
136 TempValue = strchr(WorkString, '=');
139 /* Copy the tag and value into the holding array */
140 strcpy(TildeSub[TildeSubCount].Tag, WorkString);
141 strcpy(TildeSub[TildeSubCount].Value, TempValue);
146 /* Now read in the config.sh file. */
147 while(fgets(LineBuffer, LINEBUFFERSIZE - 1, ConfigSH)) {
148 /* Force a trailing null, just in case */
149 LineBuffer[LINEBUFFERSIZE - 1] = '\0';
151 LineBufferLength = strlen(LineBuffer);
153 /* Chop trailing control characters */
154 while((LineBufferLength > 0) && (LineBuffer[LineBufferLength-1] < ' ')) {
155 LineBuffer[LineBufferLength - 1] = '\0';
159 /* If it's empty, then try again */
163 /* If the line begins with a '#' or ' ', skip */
164 if ((LineBuffer[0] == ' ') || (LineBuffer[0] == '#'))
167 /* We've got something. Guess we need to actually handle it */
168 /* Do the tilde substitution */
169 tilde_sub(LineBuffer, TildeSub, TildeSubCount);
171 /* Stick a NULL over the = */
172 TempValue = strchr(LineBuffer, '=');
174 /* And another over the leading ', which better be there */
177 /* Check to see if there's a trailing ' or ". If not, add a newline to
178 the buffer and grab another line. */
179 TempLength = strlen(TempValue);
180 while ((TempValue[TempLength-1] != '\'') &&
181 (TempValue[TempLength-1] != '"')) {
182 fgets(SecondaryLineBuffer, LINEBUFFERSIZE - 1, ConfigSH);
183 /* Force a trailing null, just in case */
184 SecondaryLineBuffer[LINEBUFFERSIZE - 1] = '\0';
186 tilde_sub(SecondaryLineBuffer, TildeSub, TildeSubCount);
187 /* Tack a nweline on the end of our primary buffer */
188 strcat(TempValue, "\n");
189 /* Concat the new line we just read */
190 strcat(TempValue, SecondaryLineBuffer);
192 /* Refigure the length */
193 TempLength = strlen(TempValue);
195 /* Chop trailing control characters */
196 while((TempLength > 0) && (TempValue[TempLength-1] < ' ')) {
197 TempValue[TempLength - 1] = '\0';
202 /* And finally one over the trailing ' */
203 TempValue[TempLength-1] = '\0';
205 /* Is there even anything left? */
207 /* Copy the tag over */
208 strcpy(ConfigSub[ConfigSubCount].Tag, LineBuffer);
209 /* Copy the value over */
210 strcpy(ConfigSub[ConfigSubCount].Value, TempValue);
218 /* Okay, we've read in all the substititions from our config.sh */
219 /* equivalent. Read in the config_h.sh equiv and start the substitution */
221 /* First, eat all the lines until we get to one with !GROK!THIS! in it */
222 while(!strstr(fgets(LineBuffer, LINEBUFFERSIZE, Config_H),
225 /* Dummy statement to shut up any compiler that'll whine about an empty */
230 /* Right, we've read all the lines through the first one with !GROK!THIS! */
231 /* in it. That gets us through the beginning stuff. Now start in earnest */
232 /* with our translations, which run until we get to another !GROK!THIS! */
233 while(!strstr(fgets(LineBuffer, LINEBUFFERSIZE, Config_H),
235 /* Force a trailing null, just in case */
236 LineBuffer[LINEBUFFERSIZE - 1] = '\0';
238 /* Tilde Substitute */
239 tilde_sub(LineBuffer, TildeSub, TildeSubCount);
241 LineBufferLength = strlen(LineBuffer);
243 /* Chop trailing control characters */
244 while((LineBufferLength > 0) && (LineBuffer[LineBufferLength-1] < ' ')) {
245 LineBuffer[LineBufferLength - 1] = '\0';
250 /* Right. Go looking for $s. */
251 for(LineBufferLoop = 0; LineBufferLoop < LineBufferLength;
253 /* Did we find one? */
254 if ('$' != LineBuffer[LineBufferLoop]) {
255 /* Nope, spit out the value */
256 OutBuf[OutBufPos++] = LineBuffer[LineBufferLoop];
258 /* Yes, we did. Is it escaped? */
259 if ((LineBufferLoop > 0) && ('\\' == LineBuffer[LineBufferLoop -
261 /* Yup. Spit it out */
262 OutBuf[OutBufPos++] = LineBuffer[LineBufferLoop];
264 /* Nope. Go grab us a token */
266 /* Advance to the next character in the input stream */
268 while((LineBufferLoop < LineBufferLength) &&
269 ((isalnum(LineBuffer[LineBufferLoop]) || ('_' ==
270 LineBuffer[LineBufferLoop])))) {
271 TokenBuffer[TokenBufferLoop] = LineBuffer[LineBufferLoop];
276 /* Trailing null on the token buffer */
277 TokenBuffer[TokenBufferLoop] = '\0';
279 /* Back the line buffer pointer up one */
282 /* Right, we're done grabbing a token. Check to make sure we got */
284 if (TokenBufferLoop) {
285 /* Well, we do. Run through all the tokens we've got in the */
286 /* ConfigSub array and see if any match */
288 for(ConfigSubLoop = 0; ConfigSubLoop < ConfigSubCount;
290 if (!strcmp(TokenBuffer, ConfigSub[ConfigSubLoop].Tag)) {
291 char *cp = ConfigSub[ConfigSubLoop].Value;
293 while (*cp) OutBuf[OutBufPos++] = *(cp++);
298 /* Did we find something? If not, spit out what was in our */
301 char *cp = TokenBuffer;
302 OutBuf[OutBufPos++] = '$';
303 while (*cp) OutBuf[OutBufPos++] = *(cp++);
307 /* Just a bare $. Spit it out */
308 OutBuf[OutBufPos++] = '$';
314 /* If we've created an #undef line, make sure we don't output anthing
315 * after the "#undef FOO" besides comments. We could do this as we
316 * go by recognizing the #undef as it goes by, and thus avoid another
317 * use of a fixed-length buffer, but this is simpler.
319 if (!strncmp(OutBuf,"#undef",6)) {
321 int i, incomment = 0;
323 OutBuf[OutBufPos] = '\0';
324 for (i = 0; i <= 1; i++) {
325 while (!isspace(*cp)) LineBuffer[LineBufferLoop++] = *(cp++);
326 while ( isspace(*cp)) LineBuffer[LineBufferLoop++] = *(cp++);
329 while (isspace(*cp)) LineBuffer[LineBufferLoop++] = *(cp++);
330 if (!incomment && *cp == '/' && *(cp+1) == '*') incomment = 1;
331 while (*cp && !isspace(*cp)) {
332 if (incomment) LineBuffer[LineBufferLoop++] = *cp;
335 if (incomment && *cp == '*' && *(cp+1) == '/') incomment = 0;
337 LineBuffer[LineBufferLoop] = '\0';
341 OutBuf[OutBufPos] = '\0';
346 /* Close the files */
349 if (ifile) fclose(Extra_Subs);
353 tilde_sub(char LineBuffer[], Translate TildeSub[], int TildeSubCount)
355 char TempBuffer[LINEBUFFERSIZE], TempTilde[TOKENBUFFERSIZE];
356 int TildeLoop, InTilde, CopiedBufferLength, TildeBufferLength, k, GotIt;
359 CopiedBufferLength = 0;
360 TildeBufferLength = 0;
361 TempLength = strlen(LineBuffer);
363 /* Grovel over our input looking for ~foo~ constructs */
364 for(TildeLoop = 0; TildeLoop < TempLength; TildeLoop++) {
365 /* Are we in a tilde? */
367 /* Yup. Is the current character a tilde? */
368 if (LineBuffer[TildeLoop] == '~') {
369 /* Yup. That means we're ready to do a substitution */
373 TempTilde[TildeBufferLength] = '\0';
374 for( k=0; k < TildeSubCount; k++) {
375 if (!strcmp(TildeSub[k].Tag, TempTilde)) {
377 /* Tack on the trailing null to the main buffer */
378 TempBuffer[CopiedBufferLength] = '\0';
379 /* Copy the tilde substitution over */
380 strcat(TempBuffer, TildeSub[k].Value);
381 CopiedBufferLength = strlen(TempBuffer);
385 /* Did we find anything? */
387 /* Guess not. Copy the whole thing out verbatim */
388 TempBuffer[CopiedBufferLength] = '\0';
389 TempBuffer[CopiedBufferLength++] = '~';
390 TempBuffer[CopiedBufferLength] = '\0';
391 strcat(TempBuffer, TempTilde);
392 strcat(TempBuffer, "~");
393 CopiedBufferLength = strlen(TempBuffer);
397 /* 'Kay, not a tilde. Is it a word character? */
398 if (isalnum(LineBuffer[TildeLoop]) ||
399 (LineBuffer[TildeLoop] == '-')) {
400 TempTilde[TildeBufferLength++] = LineBuffer[TildeLoop];
402 /* No, it's not a tilde character. For shame! We've got a */
403 /* bogus token. Copy a ~ into the output buffer, then append */
404 /* whatever we've got in our token buffer */
405 TempBuffer[CopiedBufferLength++] = '~';
406 TempBuffer[CopiedBufferLength] = '\0';
407 TempTilde[TildeBufferLength] = '\0';
408 strcat(TempBuffer, TempTilde);
409 CopiedBufferLength += TildeBufferLength;
414 /* We're not in a tilde. Do we want to be? */
415 if (LineBuffer[TildeLoop] == '~') {
418 TildeBufferLength = 0;
420 /* Nope. Copy the character to the output buffer */
421 TempBuffer[CopiedBufferLength++] = LineBuffer[TildeLoop];
426 /* Out of the loop. First, double-check to see if there was anything */
429 /* bogus token. Copy a ~ into the output buffer, then append */
430 /* whatever we've got in our token buffer */
431 TempBuffer[CopiedBufferLength++] = '~';
432 TempBuffer[CopiedBufferLength] = '\0';
433 TempTilde[TildeBufferLength] = '\0';
434 strcat(TempBuffer, TempTilde);
435 CopiedBufferLength += TildeBufferLength;
437 /* Nope, nothing pensing. Tack on a \0 */
438 TempBuffer[CopiedBufferLength] = '\0';
441 /* Okay, we're done. Copy the temp buffer back into the line buffer */
442 strcpy(LineBuffer, TempBuffer);