added cool gen-boiler rule and improved default help
[k8jam.git] / src / rules.c
blobf3cf602d5afea0990945ea24ec71377207f8da67
1 /*
2 * Copyright 1993, 1995 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6 /*
7 * rules.c - access to RULEs, TARGETs, and ACTIONs
9 * External routines:
11 * bindrule() - return pointer to RULE, creating it if necessary
12 * bindtarget() - return pointer to TARGET, creating it if necessary
13 * copytarget() - make a new target with the old target's name
14 * touchtarget() - mark a target to simulate being new
15 * targetlist() - turn list of target names into a TARGET chain
16 * targetentry() - add a TARGET to a chain of TARGETS
17 * targetchain() - append two TARGET chains
18 * actionlist() - append to an ACTION chain
19 * addsettings() - add a deferred "set" command to a target
20 * copysettings() - copy a settings list for temp use
21 * pushsettings() - set all target specific variables
22 * popsettings() - reset target specific variables to their pre-push values
23 * freesettings() - delete a settings list
24 * donerules() - free RULE and TARGET tables
26 * 04/12/94 (seiwald) - actionlist() now just appends a single action.
27 * 08/23/94 (seiwald) - Support for '+=' (append to variable)
28 * 06/21/02 (seiwald) - support for named parameters
29 * 11/04/02 (seiwald) - const-ing for string literals
30 * 12/03/02 (seiwald) - fix odd includes support by grafting them onto depends
31 * 12/17/02 (seiwald) - new copysettings() to protect target-specific vars
32 * 01/14/03 (seiwald) - fix includes fix with new internal includes TARGET
34 #include "jam.h"
35 #include "lists.h"
36 #include "parse.h"
37 #include "variable.h"
38 #include "rules.h"
39 #include "newstr.h"
40 #include "hash.h"
43 static struct hash *rulehash = NULL;
44 static struct hash *targethash = NULL;
48 * bindrule() - return pointer to RULE, creating it if necessary
50 RULE *bindrule (const char *rulename) {
51 RULE rule, *r = &rule;
53 if (!rulehash) rulehash = hashinit(sizeof(RULE), "rules");
54 r->name = rulename;
55 if (hashenter(rulehash, (HASHDATA **)&r)) {
56 r->name = newstr(rulename); /* never freed */
57 r->procedure = (PARSE *)0;
58 r->actions = (char *)0;
59 r->bindlist = L0;
60 r->params = L0;
61 r->flags = 0;
63 return r;
68 * bindtarget() - return pointer to TARGET, creating it if necessary
70 TARGET *bindtarget (const char *targetname) {
71 TARGET target, *t = ⌖
73 if (!targethash) targethash = hashinit(sizeof(TARGET), "targets");
74 t->name = targetname;
75 if (hashenter(targethash, (HASHDATA **)&t)) {
76 memset((char *)t, '\0', sizeof(*t));
77 t->name = newstr(targetname); /* never freed */
78 t->boundname = t->name; /* default for T_FLAG_NOTFILE */
80 return t;
85 * copytarget() - make a new target with the old target's name
87 * Not entered into hash table -- for internal nodes.
89 TARGET *copytarget (const TARGET *ot) {
90 TARGET *t = (TARGET *)malloc(sizeof(*t));
91 memset((char *)t, '\0', sizeof(*t));
92 t->name = copystr(ot->name);
93 t->boundname = t->name;
94 t->flags |= T_FLAG_NOTFILE|T_FLAG_INTERNAL;
95 return t;
100 * touchtarget() - mark a target to simulate being new
102 void touchtarget (const char *t) {
103 bindtarget(t)->flags |= T_FLAG_TOUCHED;
108 * targetlist() - turn list of target names into a TARGET chain
110 * Inputs:
111 * chain existing TARGETS to append to
112 * targets list of target names
114 TARGETS *targetlist (TARGETS *chain, LIST *targets) {
115 for (; targets; targets = list_next(targets)) chain = targetentry(chain, bindtarget(targets->string));
116 return chain;
121 * targetentry() - add a TARGET to a chain of TARGETS
123 * Inputs:
124 * chain exisitng TARGETS to append to
125 * target new target to append
127 TARGETS *targetentry (TARGETS *chain, TARGET *target) {
128 TARGETS *c = (TARGETS *)malloc(sizeof(TARGETS));
129 c->target = target;
130 if (!chain) chain = c; else chain->tail->next = c;
131 chain->tail = c;
132 c->next = 0;
133 return chain;
138 * targetchain() - append two TARGET chains
140 * Inputs:
141 * chain exisitng TARGETS to append to
142 * target new target to append
144 TARGETS *targetchain (TARGETS *chain, TARGETS *targets) {
145 if (!targets) return chain;
146 if (!chain) return targets;
147 chain->tail->next = targets;
148 chain->tail = targets->tail;
149 return chain;
154 * actionlist() - append to an ACTION chain
156 ACTIONS *actionlist (ACTIONS *chain, ACTION *action) {
157 ACTIONS *actions = (ACTIONS *)malloc(sizeof(ACTIONS));
158 actions->action = action;
159 if (!chain) chain = actions; else chain->tail->next = actions;
160 chain->tail = actions;
161 actions->next = 0;
162 return chain;
167 * addsettings() - add a deferred "set" command to a target
169 * Adds a variable setting (varname=list) onto a chain of settings
170 * for a particular target. Replaces the previous previous value,
171 * if any, unless 'append' says to append the new list onto the old.
172 * Returns the head of the chain of settings.
174 SETTINGS *addsettings (SETTINGS *head, int setflag, const char *symbol, LIST *value) {
175 SETTINGS *v;
176 /* look for previous setting */
177 for (v = head; v; v = v->next) if (!strcmp(v->symbol, symbol)) break;
178 /* if not previously set, alloc a new */
179 /* if appending, do so */
180 /* else free old and set new */
181 if (!v) {
182 v = (SETTINGS *)malloc(sizeof(*v));
183 v->symbol = newstr(symbol);
184 v->value = value;
185 v->next = head;
186 head = v;
187 } else {
188 switch (setflag) {
189 case VAR_SET:
190 /* toss old, set new */
191 list_free(v->value);
192 v->value = value;
193 break;
194 case VAR_APPEND:
195 /* append new to old */
196 v->value = list_append(v->value, value);
197 break;
198 case VAR_REMOVE:
199 v->value = list_removeall(v->value, value);
200 list_free(value);
201 break;
202 case VAR_DEFAULT:
203 /* toss new, old already set */
204 list_free(value);
205 break;
208 /* return (new) head of list */
209 return head;
214 * copysettings() - copy a settings list for temp use
216 * When target-specific variables are pushed into place with pushsettings(),
217 * any global variables with the same name are swapped onto the target's
218 * SETTINGS chain. If that chain gets modified (by using the "on target"
219 * syntax), popsettings() would wrongly swap those modified values back
220 * as the new global values.
222 * copysettings() protects the target's SETTINGS chain by providing a
223 * copy of the chain to pass to pushsettings() and popsettings(), so that
224 * the target's original SETTINGS chain can be modified using the usual
225 * "on target" syntax.
227 SETTINGS *copysettings (SETTINGS *from) {
228 SETTINGS *head = 0;
230 for (; from; from = from->next) {
231 SETTINGS *v = (SETTINGS *)malloc(sizeof(*v));
232 v->symbol = copystr(from->symbol);
233 v->value = list_copy(0, from->value);
234 v->next = head;
235 head = v;
237 return head;
242 * pushsettings() - set all target specific variables
244 void pushsettings (SETTINGS *v) {
245 for (; v; v = v->next) v->value = var_swap(v->symbol, v->value);
250 * popsettings() - reset target specific variables to their pre-push values
252 void popsettings (SETTINGS *v) {
253 pushsettings(v); /* just swap again */
258 * freesettings() - delete a settings list
260 void freesettings (SETTINGS *v) {
261 while (v != NULL) {
262 SETTINGS *n = v->next;
264 freestr(v->symbol);
265 list_free(v->value);
266 free((char *)v);
267 v = n;
273 * donerules() - free RULE and TARGET tables
275 void donerules (void) {
276 hashdone(rulehash);
277 hashdone(targethash);