1 /*--------------------------------------------------------------*/
4 /* This helper program emulates m4 pre-processor behavior. */
6 /* As of July 2006, XCircuit no longer uses m4, having replaced */
7 /* it with "sed" scripts. However, since this C code was */
8 /* written to bypass the lack of "m4" under Windows, it is */
9 /* still required to bypass the lack of "sed" under Windows. */
10 /* The "m4" emulation is maintained. However, the m4 files */
11 /* have been rewritten to remove the awkward "ifelse" syntax, */
12 /* which has been replaced with a simpler "<variable_name>" */
13 /* at the beginning of each variable-dependent line. This */
14 /* program also handles those situations. */
15 /*--------------------------------------------------------------*/
20 typedef struct _pattern
{
23 struct _pattern
*next
;
26 pattern
* parse_args(int* argc
, char ***argv
)
29 pattern
*p
= (pattern
*)malloc(sizeof(pattern
));
31 p
->pattern
= "`eval'";
35 for (i
=1; i
<(*argc
); i
++) {
36 if (strncmp((*argv
)[i
], "-D", 2) == 0) {
37 char *c
= strchr((*argv
)[i
], '=');
40 printf("Invalid argument: %s\n", (*argv
[i
]));
43 new_p
= (pattern
*)malloc(sizeof(pattern
));
47 p
->pattern
= strdup((*argv
)[i
]+2);
61 p
->string
= strdup(c
);
62 /*fprintf(stderr, "%s -> %s\n", p->pattern, p->string);*/
73 int main(int argc
, char **argv
)
79 pattern
*patterns
, *p
;
82 patterns
= parse_args(&argc
, &argv
);
85 fin
= fopen(argv
[0], "r");
87 printf("Unable to open file: %s\n", argv
[0]);
95 if (fgets(buffer
, 4096, fin
) == 0)
99 while ((c
= strstr(buffer
, p
->pattern
)) != NULL
) {
102 /* Handle variable-dependent lines */
103 if (c
== buffer
&& !strcmp(p
->string
, "1")) {
104 strcpy(buffer2
, c
+ strlen(p
->pattern
) + 1);
105 strcpy(buffer
, buffer2
);
107 else if (c
== buffer
&& !strcmp(p
->string
, "0")) {
112 strcpy(buffer2
, buffer
);
113 strcat(buffer2
, p
->string
);
114 strcat(buffer2
, c
+ strlen(p
->pattern
));
115 strcpy(buffer
, buffer2
);
118 if (p
== NULL
) break;
121 if (buffer
[0] != '\0')
122 printf("%s", buffer
);