using new string modifiers in Jambase
[k8jam.git] / src / hdrmacro.c
blob97b2ee22de7297d489f8fc1481a60a5d5929a51f
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 "re9.h"
13 #include "hdrmacro.h"
14 #include "hash.h"
15 #include "newstr.h"
16 #include "dstrings.h"
19 * hdrmacro.c - handle header files that define macros used in
20 * #include statements.
22 * we look for lines like "#define MACRO <....>" or '#define MACRO " "'
23 * in the target file. When found, we
25 * we then phony up a rule invocation like:
27 * $(HDRRULE) <target> : <resolved included files> ;
29 * External routines:
30 * headers1() - scan a target for "#include MACRO" lines and try
31 * to resolve them when needed
33 * Internal routines:
34 * headers1() - using regexp, scan a file and build include LIST
36 * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
37 * 09/10/00 (seiwald) - replaced call to compile_rule with evaluate_rule,
38 * so that headers() doesn't have to mock up a parse structure
39 * just to invoke a rule.
42 /*k8: static LIST *header_macros1(LIST *l, char *file, int rec, HSRegExp *re[]);*/
45 /* this type is used to store a dictionary of file header macros */
46 typedef struct header_macro {
47 const char *symbol;
48 const char *filename; /* we could maybe use a LIST here ?? */
49 } HEADER_MACRO;
51 static struct hash *header_macros_hash = 0;
55 * headers() - scan a target for include files and call HDRRULE
57 //#define MAXINC (10)
59 void macro_headers (TARGET *t) {
60 regexp_t *re;
61 re9_sub_t mt[4];
62 int line_size = 16384;
63 char *buf; /* line_size size */
64 dstring_t buf1, buf2;
65 FILE *f;
66 if (DEBUG_HEADER) printf("macro header scan for %s\n", t->name);
67 /* this regexp is used to detect lines of the form */
68 /* "#define MACRO <....>" or "#define MACRO "....." */
69 /* in the header macro files */
70 if ((f = fopen(t->boundname, "r")) == 0) return;
71 re = regexp_compile("^\\s*#\\s*define\\s+([A-Za-z_][A-Za-z0-9_]*)\\s+[<\"]([^\">]+)[\">].*$", 0);
72 if ((buf = malloc(line_size)) == NULL) { fprintf(stderr, "FATAL: out of memory!\n"); abort(); }
73 dstr_init(&buf1);
74 dstr_init(&buf2);
75 while (fgets(buf, sizeof(buf), f)) {
76 HEADER_MACRO var, *v = &var;
77 mt[0].sp = mt[0].ep = NULL;
78 if (regexp_execute(re, buf, mt, 4) > 0) {
79 int l1 = mt[1].ep-mt[1].sp;
80 int l2 = mt[2].ep-mt[2].sp;
81 if (l1 > 0 && l2 > 0) {
82 dstr_set_buf(&buf1, mt[1].sp, l1);
83 dstr_set_buf(&buf2, mt[2].sp, l2);
84 /* we detected a line that looks like "#define MACRO filename */
85 if (DEBUG_HEADER) printf("macro '%s' used to define filename '%s' in '%s'\n", dstr_cstr(&buf1), dstr_cstr(&buf2), t->boundname);
86 /* add macro definition to hash table */
87 if (!header_macros_hash) header_macros_hash = hashinit(sizeof(HEADER_MACRO), "hdrmacros");
88 v->symbol = (const char *)dstr_cstr(&buf1);
89 v->filename = 0;
90 if (hashenter(header_macros_hash, (HASHDATA **)&v)) {
91 v->symbol = newstr(dstr_cstr(&buf1)); /* never freed */
92 v->filename = newstr(dstr_cstr(&buf2)); /* never freed */
95 /* XXXX: FOR NOW, WE IGNORE MULTIPLE MACRO DEFINITIONS! */
96 /* WE MIGHT AS WELL USE A LIST TO STORE THEM */
99 dstr_done(&buf2);
100 dstr_done(&buf1);
101 free(buf);
102 fclose(f);
103 regexp_free(re);
107 const char *macro_header_get (const char *macro_name) {
108 HEADER_MACRO var, *v = &var;
109 v->symbol = (char*)macro_name;
110 if (header_macros_hash && hashcheck(header_macros_hash, (HASHDATA **)&v)) {
111 if (DEBUG_HEADER) printf("### macro '%s' evaluated to '%s'\n", macro_name, v->filename);
112 return (const char *)v->filename;
114 return 0;