2 * Copyright 1993, 2000 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
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> ;
30 * headers1() - scan a target for "#include MACRO" lines and try
31 * to resolve them when needed
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
{
48 const char *filename
; /* we could maybe use a LIST here ?? */
51 static struct hash
*header_macros_hash
= 0;
55 * headers() - scan a target for include files and call HDRRULE
59 void macro_headers (TARGET
*t
) {
62 int line_size
= 16384;
63 char *buf
; /* line_size size */
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(); }
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
);
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 */
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
;