lexer rules relaxed a little; please, don't use tokens like abc: -- use "abc:" instead
[k8jam.git] / src / headers.c
blob1da3c5f38104c6c7b08aa258d61f9d21befd8ed3
1 /*
2 * Copyright 1993, 2000 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
7 /*
8 * headers.c - handle #includes in source files
10 * Using regular expressions provided as the variable $(HDRSCAN),
11 * headers() searches a file for #include files and phonies up a
12 * rule invocation:
14 * $(HDRRULE) <target> : <include files> ;
16 * External routines:
17 * headers() - scan a target for include files and call HDRRULE
19 * Internal routines:
20 * headers1() - using regexp, scan a file and build include LIST
22 * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
23 * 09/10/00 (seiwald) - replaced call to compile_rule with evaluate_rule,
24 * so that headers() doesn't have to mock up a parse structure
25 * just to invoke a rule.
26 * 03/02/02 (seiwald) - rules can be invoked via variable names
27 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
28 * 11/04/02 (seiwald) - const-ing for string literals
29 * 12/09/02 (seiwald) - push regexp creation down to headers1().
31 #include "jam.h"
32 #include "lists.h"
33 #include "parse.h"
34 #include "compile.h"
35 #include "rules.h"
36 #include "variable.h"
37 #include "hsregexp.h"
38 #include "headers.h"
39 #include "newstr.h"
40 #include "hdrmacro.h"
43 static LIST *headers1 (const char *file, LIST *hdrscan);
47 * headers() - scan a target for include files and call HDRRULE
49 #define MAXINC (10)
51 void headers (TARGET *t) {
52 LIST *hdrscan;
53 LIST *hdrrule;
54 LOL lol;
56 if (!(hdrscan = var_get("HDRSCAN")) || !(hdrrule = var_get("HDRRULE"))) return;
57 /* doctor up call to HDRRULE rule */
58 /* call headers1() to get LIST of included files */
59 if (DEBUG_HEADER) printf("header scan %s\n", t->name);
60 lol_init(&lol);
61 lol_add(&lol, list_new(L0, t->name, 1));
62 lol_add(&lol, headers1(t->boundname, hdrscan));
63 if (lol_get(&lol, 1)) list_free(evaluate_rule(hdrrule->string, &lol, L0));
64 /* clean up */
65 lol_free(&lol);
70 * headers1() - using regexp, scan a file and build include LIST
72 static LIST *headers1 (const char *file, LIST *hdrscan) {
73 FILE *f;
74 int i;
75 int rec = 0;
76 LIST *result = 0;
77 HSRegExp *re[MAXINC];
78 HSRegExp *re_macros;
79 char buf[1024];
81 if (!(f = fopen(file, "r"))) return result;
82 while (rec < MAXINC && hdrscan) {
83 re[rec++] = hsRxCompile(hdrscan->string);
84 hdrscan = list_next(hdrscan);
86 /* the following regexp is used to detect cases where a */
87 /* file is included through a line line "#include MACRO" */
88 re_macros = hsRxCompile("^[ \t]*#[ \t]*include[ \t]*([A-Za-z][A-Za-z0-9_]*).*$");
89 while (fgets(buf, sizeof(buf), f)) {
90 for (i = 0; i < rec; ++i) {
91 if (hsRxExec(re[i], buf) && re[i]->startp[1]) {
92 /* copy and terminate extracted string */
93 char buf2[MAXSYM];
94 int l = re[i]->endp[1]-re[i]->startp[1];
96 memcpy(buf2, re[i]->startp[1], l);
97 buf2[l] = 0;
98 result = list_new(result, buf2, 0);
99 if (DEBUG_HEADER) printf("header found: %s\n", buf2);
102 /* special treatment for #include MACRO */
103 if (hsRxExec(re_macros, buf) && re_macros->startp[1]) {
104 const char *header_filename;
105 char buf2[MAXSYM];
106 int l = re_macros->endp[1]-re_macros->startp[1];
108 memcpy(buf2, re_macros->startp[1], l);
109 buf2[l] = 0;
110 if (DEBUG_HEADER) printf("macro header found: %s", buf2);
111 header_filename = macro_header_get(buf2);
112 if (header_filename) {
113 if (DEBUG_HEADER) printf(" resolved to '%s'\n", header_filename);
114 result = list_new(result, header_filename, 0);
115 } else {
116 if (DEBUG_HEADER) printf(" ignored !!\n");
120 hsRxFree(re_macros);
121 while (rec) hsRxFree(re[--rec]);
122 fclose(f);
124 return result;