regexp cosmetix
[k8jam.git] / src / hdrmacro.c
blob8e5abe5b95df4d369e05562aef68921f9dbdd72a
1 /*
2 * Copyright 1993, 2000 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6 #include "jam.h"
7 #include "lists.h"
8 #include "parse.h"
9 #include "compile.h"
10 #include "rules.h"
11 #include "variable.h"
12 #include "hsregexp.h"
13 #include "hdrmacro.h"
14 #include "hash.h"
15 #include "newstr.h"
18 * hdrmacro.c - handle header files that define macros used in
19 * #include statements.
21 * we look for lines like "#define MACRO <....>" or '#define MACRO " "'
22 * in the target file. When found, we
24 * we then phony up a rule invocation like:
26 * $(HDRRULE) <target> : <resolved included files> ;
28 * External routines:
29 * headers1() - scan a target for "#include MACRO" lines and try
30 * to resolve them when needed
32 * Internal routines:
33 * headers1() - using regexp, scan a file and build include LIST
35 * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
36 * 09/10/00 (seiwald) - replaced call to compile_rule with evaluate_rule,
37 * so that headers() doesn't have to mock up a parse structure
38 * just to invoke a rule.
41 /*k8: static LIST *header_macros1(LIST *l, char *file, int rec, HSRegExp *re[]);*/
44 /* this type is used to store a dictionary of file header macros */
45 typedef struct header_macro {
46 const char* symbol;
47 const char* filename; /* we could maybe use a LIST here ?? */
48 } HEADER_MACRO;
50 static struct hash *header_macros_hash = 0;
54 * headers() - scan a target for include files and call HDRRULE
56 #define MAXINC (10)
58 void macro_headers (TARGET *t) {
59 HSRegExp *re;
60 FILE *f;
61 char buf[1024];
63 if (DEBUG_HEADER) printf("macro header scan for %s\n", t->name);
64 /* this regexp is used to detect lines of the form */
65 /* "#define MACRO <....>" or "#define MACRO "....." */
66 /* in the header macro files */
67 re = hsRxCompile("^[ \t]*#[ \t]*define[ \t]*([A-Za-z][A-Za-z0-9_]*)[ \t]*[<\"]([^\">]*)[\">].*$");
68 if ((f = fopen(t->boundname, "r")) == 0) return;
69 while (fgets(buf, sizeof(buf), f)) {
70 HEADER_MACRO var, *v = &var;
72 if (hsRxExec(re, buf) && re->startp[1]) {
73 char buf1[MAXSYM], buf2[MAXSYM];
74 int l1, l2;
76 l1 = re->endp[1]-re->startp[1];
77 l2 = re->endp[2]-re->startp[2];
78 memcpy(buf1, re->startp[1], l1);
79 memcpy(buf2, re->startp[2], l2);
80 buf1[l1] = '\0';
81 buf2[l2] = '\0';
82 /* we detected a line that looks like "#define MACRO filename */
83 if (DEBUG_HEADER) printf("macro '%s' used to define filename '%s' in '%s'\n", buf1, buf2, t->boundname);
84 /* add macro definition to hash table */
85 if (!header_macros_hash) header_macros_hash = hashinit(sizeof(HEADER_MACRO), "hdrmacros");
86 v->symbol = (const char*)buf1;
87 v->filename = 0;
88 if (hashenter(header_macros_hash, (HASHDATA **)&v)) {
89 v->symbol = newstr(buf1); /* never freed */
90 v->filename = newstr(buf2); /* never freed */
92 /* XXXX: FOR NOW, WE IGNORE MULTIPLE MACRO DEFINITIONS! */
93 /* WE MIGHT AS WELL USE A LIST TO STORE THEM */
96 fclose(f);
97 hsRxFree(re);
101 const char *macro_header_get (const char *macro_name) {
102 HEADER_MACRO var, *v = &var;
104 v->symbol = (char*)macro_name;
105 if (header_macros_hash && hashcheck(header_macros_hash, (HASHDATA **)&v)) {
106 if (DEBUG_HEADER) printf("### macro '%s' evaluated to '%s'\n", macro_name, v->filename);
107 return (const char *)v->filename;
109 return 0;