fixed pkg-config rule
[k8jam.git] / src / rules.c
bloba96e4f116b6b72c3a3fdfcd5df161025a37c799e
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;
47 int iteraterules (HashIteratorCB itercb, void *udata) {
48 if (!rulehash) return 0;
49 return hashiterate(rulehash, itercb, udata);
53 RULE *findrule (const char *rulename) {
54 RULE rule, *r = &rule;
56 if (!rulehash || !rulename || !rulename[0]) return 0;
57 r->name = rulename;
58 return hashcheck(rulehash, (HASHDATA **)&r) ? r : NULL;
63 * haverule() - return !0 if rule exists
65 int haverule (const char *rulename) {
66 return findrule(rulename) ? 1 : 0;
71 * bindrule() - return pointer to RULE, creating it if necessary
73 RULE *bindrule (const char *rulename) {
74 RULE rule, *r = &rule;
76 if (!rulehash) rulehash = hashinit(sizeof(RULE), "rules");
77 r->name = rulename;
78 if (hashenter(rulehash, (HASHDATA **)&r)) {
79 r->name = newstr(rulename); /* never freed */
80 r->procedure = (PARSE *)0;
81 r->actions = (char *)0;
82 r->bindlist = L0;
83 r->params = L0;
84 r->flags = 0;
86 return r;
91 * bindtarget() - return pointer to TARGET, creating it if necessary
93 TARGET *bindtarget (const char *targetname) {
94 TARGET target, *t = ⌖
96 if (!targethash) targethash = hashinit(sizeof(TARGET), "targets");
97 t->name = targetname;
98 if (hashenter(targethash, (HASHDATA **)&t)) {
99 memset((char *)t, '\0', sizeof(*t));
100 t->name = newstr(targetname); /* never freed */
101 t->boundname = t->name; /* default for T_FLAG_NOTFILE */
103 return t;
108 * copytarget() - make a new target with the old target's name
110 * Not entered into hash table -- for internal nodes.
112 TARGET *copytarget (const TARGET *ot) {
113 TARGET *t = (TARGET *)malloc(sizeof(*t));
114 memset((char *)t, '\0', sizeof(*t));
115 t->name = copystr(ot->name);
116 t->boundname = t->name;
117 t->flags |= T_FLAG_NOTFILE|T_FLAG_INTERNAL;
118 return t;
123 * touchtarget() - mark a target to simulate being new
125 void touchtarget (const char *t) {
126 bindtarget(t)->flags |= T_FLAG_TOUCHED;
131 * targetlist() - turn list of target names into a TARGET chain
133 * Inputs:
134 * chain existing TARGETS to append to
135 * targets list of target names
137 TARGETS *targetlist (TARGETS *chain, LIST *targets) {
138 for (; targets; targets = list_next(targets)) chain = targetentry(chain, bindtarget(targets->string));
139 return chain;
144 * targetentry() - add a TARGET to a chain of TARGETS
146 * Inputs:
147 * chain exisitng TARGETS to append to
148 * target new target to append
150 TARGETS *targetentry (TARGETS *chain, TARGET *target) {
151 TARGETS *c = (TARGETS *)malloc(sizeof(TARGETS));
152 c->target = target;
153 if (!chain) chain = c; else chain->tail->next = c;
154 chain->tail = c;
155 c->next = 0;
156 return chain;
161 * targetchain() - append two TARGET chains
163 * Inputs:
164 * chain exisitng TARGETS to append to
165 * target new target to append
167 TARGETS *targetchain (TARGETS *chain, TARGETS *targets) {
168 if (!targets) return chain;
169 if (!chain) return targets;
170 chain->tail->next = targets;
171 chain->tail = targets->tail;
172 return chain;
177 * actionlist() - append to an ACTION chain
179 ACTIONS *actionlist (ACTIONS *chain, ACTION *action) {
180 ACTIONS *actions = (ACTIONS *)malloc(sizeof(ACTIONS));
181 actions->action = action;
182 if (!chain) chain = actions; else chain->tail->next = actions;
183 chain->tail = actions;
184 actions->next = 0;
185 return chain;
190 * addsettings() - add a deferred "set" command to a target
192 * Adds a variable setting (varname=list) onto a chain of settings
193 * for a particular target. Replaces the previous previous value,
194 * if any, unless 'append' says to append the new list onto the old.
195 * Returns the head of the chain of settings.
197 SETTINGS *addsettings (SETTINGS *head, int setflag, const char *symbol, LIST *value) {
198 SETTINGS *v;
199 /* look for previous setting */
200 for (v = head; v; v = v->next) if (!strcmp(v->symbol, symbol)) break;
201 /* if not previously set, alloc a new */
202 /* if appending, do so */
203 /* else free old and set new */
204 if (!v) {
205 v = (SETTINGS *)malloc(sizeof(*v));
206 v->symbol = newstr(symbol);
207 v->value = value;
208 v->next = head;
209 head = v;
210 } else {
211 switch (setflag) {
212 case VAR_SET:
213 /* toss old, set new */
214 list_free(v->value);
215 v->value = value;
216 break;
217 case VAR_APPEND:
218 /* append new to old */
219 v->value = list_append(v->value, value);
220 break;
221 case VAR_REMOVE:
222 v->value = list_removeall(v->value, value);
223 list_free(value);
224 break;
225 case VAR_DEFAULT:
226 /* toss new, old already set */
227 list_free(value);
228 break;
231 /* return (new) head of list */
232 return head;
237 * copysettings() - copy a settings list for temp use
239 * When target-specific variables are pushed into place with pushsettings(),
240 * any global variables with the same name are swapped onto the target's
241 * SETTINGS chain. If that chain gets modified (by using the "on target"
242 * syntax), popsettings() would wrongly swap those modified values back
243 * as the new global values.
245 * copysettings() protects the target's SETTINGS chain by providing a
246 * copy of the chain to pass to pushsettings() and popsettings(), so that
247 * the target's original SETTINGS chain can be modified using the usual
248 * "on target" syntax.
250 SETTINGS *copysettings (SETTINGS *from) {
251 SETTINGS *head = 0;
253 for (; from; from = from->next) {
254 SETTINGS *v = (SETTINGS *)malloc(sizeof(*v));
255 v->symbol = copystr(from->symbol);
256 v->value = list_copy(0, from->value);
257 v->next = head;
258 head = v;
260 return head;
265 * pushsettings() - set all target specific variables
267 void pushsettings (SETTINGS *v) {
268 for (; v; v = v->next) v->value = var_swap(v->symbol, v->value);
273 * popsettings() - reset target specific variables to their pre-push values
275 void popsettings (SETTINGS *v) {
276 pushsettings(v); /* just swap again */
281 * freesettings() - delete a settings list
283 void freesettings (SETTINGS *v) {
284 while (v != NULL) {
285 SETTINGS *n = v->next;
287 freestr(v->symbol);
288 list_free(v->value);
289 free((char *)v);
290 v = n;
296 * donerules() - free RULE and TARGET tables
298 void donerules (void) {
299 hashdone(rulehash);
300 hashdone(targethash);