cosmetix
[k8jam.git] / rules.c
blobc0987f15da23f8e1901edee0682acfe59332053c
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"
42 static struct hash *rulehash = NULL;
43 static struct hash *targethash = NULL;
47 * bindrule() - return pointer to RULE, creating it if necessary
49 RULE *bindrule (const char *rulename) {
50 RULE rule, *r = &rule;
52 if (!rulehash) rulehash = hashinit(sizeof(RULE), "rules");
53 r->name = rulename;
54 if (hashenter(rulehash, (HASHDATA **)&r)) {
55 r->name = newstr(rulename); /* never freed */
56 r->procedure = (PARSE *)0;
57 r->actions = (char *)0;
58 r->bindlist = L0;
59 r->params = L0;
60 r->flags = 0;
62 return r;
67 * bindtarget() - return pointer to TARGET, creating it if necessary
69 TARGET *bindtarget (const char *targetname) {
70 TARGET target, *t = ⌖
72 if (!targethash) targethash = hashinit(sizeof(TARGET), "targets");
73 t->name = targetname;
74 if (hashenter(targethash, (HASHDATA **)&t)) {
75 memset((char *)t, '\0', sizeof(*t));
76 t->name = newstr(targetname); /* never freed */
77 t->boundname = t->name; /* default for T_FLAG_NOTFILE */
79 return t;
84 * copytarget() - make a new target with the old target's name
86 * Not entered into hash table -- for internal nodes.
88 TARGET *copytarget (const TARGET *ot) {
89 TARGET *t = (TARGET *)malloc(sizeof(*t));
90 memset((char *)t, '\0', sizeof(*t));
91 t->name = copystr(ot->name);
92 t->boundname = t->name;
93 t->flags |= T_FLAG_NOTFILE | T_FLAG_INTERNAL;
94 return t;
99 * touchtarget() - mark a target to simulate being new
101 void touchtarget (const char *t) {
102 bindtarget(t)->flags |= T_FLAG_TOUCHED;
107 * targetlist() - turn list of target names into a TARGET chain
109 * Inputs:
110 * chain existing TARGETS to append to
111 * targets list of target names
113 TARGETS *targetlist (TARGETS *chain, LIST *targets) {
114 for (; targets; targets = list_next(targets)) chain = targetentry(chain, bindtarget(targets->string));
115 return chain;
120 * targetentry() - add a TARGET to a chain of TARGETS
122 * Inputs:
123 * chain exisitng TARGETS to append to
124 * target new target to append
126 TARGETS *targetentry (TARGETS *chain, TARGET *target) {
127 TARGETS *c = (TARGETS *)malloc(sizeof(TARGETS));
128 c->target = target;
129 if (!chain) chain = c; else chain->tail->next = c;
130 chain->tail = c;
131 c->next = 0;
132 return chain;
137 * targetchain() - append two TARGET chains
139 * Inputs:
140 * chain exisitng TARGETS to append to
141 * target new target to append
143 TARGETS *targetchain (TARGETS *chain, TARGETS *targets) {
144 if (!targets) return chain;
145 if (!chain) return targets;
146 chain->tail->next = targets;
147 chain->tail = targets->tail;
148 return chain;
153 * actionlist() - append to an ACTION chain
155 ACTIONS *actionlist (ACTIONS *chain, ACTION *action) {
156 ACTIONS *actions = (ACTIONS *)malloc(sizeof(ACTIONS));
157 actions->action = action;
158 if (!chain) chain = actions; else chain->tail->next = actions;
159 chain->tail = actions;
160 actions->next = 0;
161 return chain;
166 * addsettings() - add a deferred "set" command to a target
168 * Adds a variable setting (varname=list) onto a chain of settings
169 * for a particular target. Replaces the previous previous value,
170 * if any, unless 'append' says to append the new list onto the old.
171 * Returns the head of the chain of settings.
173 SETTINGS *addsettings (SETTINGS *head, int setflag, const char *symbol, LIST *value) {
174 SETTINGS *v;
175 /* look for previous setting */
176 for (v = head; v; v = v->next) if (!strcmp(v->symbol, symbol)) break;
177 /* if not previously set, alloc a new */
178 /* if appending, do so */
179 /* else free old and set new */
180 if (!v) {
181 v = (SETTINGS *)malloc(sizeof(*v));
182 v->symbol = newstr(symbol);
183 v->value = value;
184 v->next = head;
185 head = v;
186 } else {
187 switch (setflag) {
188 case VAR_SET:
189 /* toss old, set new */
190 list_free(v->value);
191 v->value = value;
192 break;
193 case VAR_APPEND:
194 /* append new to old */
195 v->value = list_append(v->value, value);
196 break;
197 case VAR_DEFAULT:
198 /* toss new, old already set */
199 list_free(value);
200 break;
203 /* return (new) head of list */
204 return head;
209 * copysettings() - copy a settings list for temp use
211 * When target-specific variables are pushed into place with pushsettings(),
212 * any global variables with the same name are swapped onto the target's
213 * SETTINGS chain. If that chain gets modified (by using the "on target"
214 * syntax), popsettings() would wrongly swap those modified values back
215 * as the new global values.
217 * copysettings() protects the target's SETTINGS chain by providing a
218 * copy of the chain to pass to pushsettings() and popsettings(), so that
219 * the target's original SETTINGS chain can be modified using the usual
220 * "on target" syntax.
222 SETTINGS *copysettings (SETTINGS *from) {
223 SETTINGS *head = 0;
225 for (; from; from = from->next) {
226 SETTINGS *v = (SETTINGS *)malloc(sizeof(*v));
227 v->symbol = copystr(from->symbol);
228 v->value = list_copy(0, from->value);
229 v->next = head;
230 head = v;
232 return head;
237 * pushsettings() - set all target specific variables
239 void pushsettings (SETTINGS *v) {
240 for (; v; v = v->next) v->value = var_swap(v->symbol, v->value);
245 * popsettings() - reset target specific variables to their pre-push values
247 void popsettings (SETTINGS *v) {
248 pushsettings(v); /* just swap again */
253 * freesettings() - delete a settings list
255 void freesettings (SETTINGS *v) {
256 while (v) {
257 SETTINGS *n = v->next;
258 freestr(v->symbol);
259 list_free(v->value);
260 free((char *)v);
261 v = n;
267 * donerules() - free RULE and TARGET tables
269 void donerules (void) {
270 hashdone(rulehash);
271 hashdone(targethash);