option.c: fixed warnings
[k8jam.git] / src / compile.c
blob6c1749f3c3d737f99eab346801c7ef5478925ead
1 /*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
3 * This file is part of Jam - see jam.c for Copyright information.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * compile.c - compile parsed jam statements
20 * External routines:
22 * compile_append() - append list results of two statements
23 * compile_break() - compile 'break/continue/return' rule
24 * compile_eval() - evaluate if to determine which leg to compile
25 * compile_foreach() - compile the "for x in y" statement
26 * compile_if() - compile 'if' rule
27 * compile_include() - support for 'include' - call include() on file
28 * compile_list() - expand and return a list
29 * compile_local() - declare (and set) local variables
30 * compile_null() - do nothing -- a stub for parsing
31 * compile_on() - run rule under influence of on-target variables
32 * compile_rule() - compile a single user defined rule
33 * compile_rules() - compile a chain of rules
34 * compile_set() - compile the "set variable" statement
35 * compile_setcomp() - support for `rule` - save parse tree
36 * compile_setexec() - support for `actions` - save execution string
37 * compile_settings() - compile the "on =" (set variable on exec) statement
38 * compile_switch() - compile 'switch' rule
40 * Internal routines:
42 * debug_compile() - printf with indent to show rule expansion.
43 * evaluate_rule() - execute a rule invocation
45 #include "jam.h"
47 #include "lists.h"
48 #include "parse.h"
49 #include "compile.h"
50 #include "variable.h"
51 #include "expand.h"
52 #include "rules.h"
53 #include "newstr.h"
54 #include "search.h"
55 #include "matchglob.h"
56 #include "filesys.h"
59 static const char *set_names[] = { "=", "+=", "-=", "?=" };
60 static void debug_compile (int which, const char *s);
64 * compile_append() - append list results of two statements
66 * parse->left more compile_append() by left-recursion
67 * parse->right single rule
69 LIST *compile_append (PARSE *parse, LOL *args, int *jmp) {
70 /* Append right to left. */
71 return list_append(
72 (*parse->left->func)(parse->left, args, jmp),
73 (*parse->right->func)(parse->right, args, jmp)
79 * compile_break() - compile 'break/continue/return' rule
81 * parse->left results
82 * parse->num JMP_BREAK/CONTINUE/RETURN
84 LIST *compile_break (PARSE *parse, LOL *args, int *jmp) {
85 LIST *lv = (*parse->left->func)(parse->left, args, jmp);
86 *jmp = parse->num;
87 return lv;
92 * compile_eval() - evaluate if to determine which leg to compile
94 * Returns:
95 * list if expression true - compile 'then' clause
96 * L0 if expression false - compile 'else' clause
98 static JAMFA_PURE int lcmp (LIST *t, LIST *s) {
99 int status = 0;
100 while (!status && (t || s)) {
101 const char *st = (t ? t->string : "");
102 const char *ss = (s ? s->string : "");
103 status = strcmp(st, ss);
104 t = (t ? list_next(t) : t);
105 s = (s ? list_next(s) : s);
107 return status;
111 static int recmp (LIST *s, LIST *restr) {
112 int res;
114 printf("restr: <%s>\n", (restr ? restr->string : "(null)"));
115 printf("s : <%s>\n", (s ? s->string : "(null)"));
117 regexp_t *re = regexp_compile((restr ? restr->string : ""), 0);
118 res = regexp_execute(re, (s ? s->string : ""), NULL, 0);
119 regexp_free(re);
120 return (res > 0);
124 LIST *compile_eval (PARSE *parse, LOL *args, int *jmp) {
125 LIST *ll, *lr, *s, *t;
126 int status = 0;
127 /* short circuit lr eval for &&, ||, 'in' and 'any-in' */
128 ll = (*parse->left->func)(parse->left, args, jmp);
129 lr = 0;
130 switch (parse->num) {
131 case EXPR_AND:
132 case EXPR_IN:
133 case EXPR_ANYIN:
134 if (ll) goto eval;
135 break;
136 case EXPR_OR:
137 if (!ll) goto eval;
138 break;
139 default:
140 eval: lr = (*parse->right->func)(parse->right, args, jmp);
141 break;
143 /* now eval */
144 switch (parse->num) {
145 case EXPR_NOT: if (!ll) status = 1; break;
146 case EXPR_AND: if (ll && lr) status = 1; break;
147 case EXPR_OR: if (ll || lr) status = 1; break;
148 case EXPR_IN:
149 /* "a in b": make sure each of ll is equal to something in lr */
150 for (t = ll; t; t = list_next(t)) {
151 for (s = lr; s; s = list_next(s)) if (strcmp(t->string, s->string) == 0) break;
152 if (!s) break;
154 /* No more ll? success */
155 if (!t) status = 1;
156 break;
157 case EXPR_ANYIN:
158 /* "a any-in b": does b contains at least one item from a? */
159 for (t = ll; t && !status; t = list_next(t)) {
160 for (s = lr; s; s = list_next(s)) if (strcmp(t->string, s->string) == 0) { status = 1; break; } /* success */
162 break;
163 case EXPR_EXISTS: if (lcmp(ll, L0) != 0) status = 1; break;
164 case EXPR_EQUALS: if (lcmp(ll, lr) == 0) status = 1; break;
165 case EXPR_NOTEQ: if (lcmp(ll, lr) != 0) status = 1; break;
166 case EXPR_LESS: if (lcmp(ll, lr) < 0) status = 1; break;
167 case EXPR_LESSEQ: if (lcmp(ll, lr) <= 0) status = 1; break;
168 case EXPR_MORE: if (lcmp(ll, lr) > 0) status = 1; break;
169 case EXPR_MOREEQ: if (lcmp(ll, lr) >= 0) status = 1; break;
170 case EXPR_REXPEQ: if (recmp(ll, lr)) status = 1; break;
172 if (DEBUG_IF) {
173 debug_compile(0, "if");
174 list_print(ll);
175 printf("(%d) ", status);
176 list_print_ex(stdout, lr, LPFLAG_NO_TRSPACE);
177 printf("\n");
179 /* find something to return */
180 /* in odd circumstances (like "" = "") we'll have to return a new string */
181 if (!status) t = NULL;
182 else if (ll) { t = ll; ll = NULL; }
183 else if (lr) { t = lr; lr = NULL; }
184 else t = list_new(L0, "1", 0);
185 if (ll) list_free(ll);
186 if (lr) list_free(lr);
187 return t;
192 * compile_foreach() - compile the "for x in y" statement
194 * Compile_foreach() resets the given variable name to each specified
195 * value, executing the commands enclosed in braces for each iteration.
197 * parse->string index variable
198 * parse->left variable values
199 * parse->right rule to compile
201 LIST *compile_foreach (PARSE *p, LOL *args, int *jmp) {
202 LIST *nv = (*p->left->func)(p->left, args, jmp);
203 LIST *result = NULL;
204 SETTINGS *s = NULL;
205 if (p->num) {
206 /* have 'local' */
207 s = addsettings(s, VAR_SET, p->string, L0);
208 pushsettings(s);
210 /* for each value for var */
211 for (LIST *l = nv; l && *jmp == JMP_NONE; l = list_next(l)) {
212 /* reset $(p->string) for each val */
213 var_set(p->string, list_new(L0, l->string, 1), VAR_SET);
214 /* keep only last result */
215 list_free(result);
216 result = (*p->right->func)(p->right, args, jmp);
217 /* continue loop? */
218 if (*jmp == JMP_CONTINUE) *jmp = JMP_NONE;
220 /* here by break/continue? */
221 if (*jmp == JMP_BREAK || *jmp == JMP_CONTINUE) *jmp = JMP_NONE;
222 if (p->num) {
223 /* restore locals */
224 popsettings(s);
225 freesettings(s);
227 list_free(nv);
228 /* returns result of last loop */
229 return result;
234 * compile_if() - compile 'if' rule
236 * parse->left condition tree
237 * parse->right then tree
238 * parse->third else tree
240 LIST *compile_if (PARSE *p, LOL *args, int *jmp) {
241 LIST *l = (*p->left->func)(p->left, args, jmp);
242 p = (l ? p->right : p->third);
243 list_free(l);
244 return (*p->func)(p, args, jmp);
249 * compile_include() - support for 'include' - call include() on file
251 * parse->left list of files to include (can only do 1)
252 * parse->num !0: softinclude
254 LIST *compile_include (PARSE *parse, LOL *args, int *jmp) {
255 int softinc = parse->num;
256 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
257 if (DEBUG_COMPILE) {
258 debug_compile(0, (softinc ? "softinclude" : "include"));
259 list_print_ex(stdout, nt, LPFLAG_NO_TRSPACE);
260 printf("\n");
262 if (nt) {
263 TARGET *t = bindtarget(nt->string);
264 /* bind the include file under the influence of "on-target" variables */
265 /* though they are targets, include files are not built with make() */
266 /* needn't copysettings(), as search sets no vars */
267 pushsettings(t->settings);
268 t->boundname = search(t->name, &t->time);
269 popsettings(t->settings);
270 /* don't parse missing file if NOCARE set */
271 if (t->time || !(t->flags&T_FLAG_NOCARE)) {
272 int doit = 1;
273 if (file_type(t->boundname) != 0 || access(t->boundname, R_OK) != 0) {
274 if (!softinc) {
275 printf("Failed to include file '%s'\n", t->boundname);
276 exit(EXITBAD);
278 doit = 0;
280 if (doit) parse_file(t->boundname);
283 list_free(nt);
284 return L0;
289 * compile_list() - expand and return a list
291 * parse->string - character string to expand
292 * parse->num - "don't expand" flag
294 LIST *compile_list (PARSE *parse, LOL *args, int *jmp) {
295 /* voodoo 1 means: parse->string is a copyable string */
296 return (parse->num ? list_new(L0, parse->string, 1) : var_expand(parse->string, NULL, args, 1));
301 * compile_local() - declare (and set) local variables
303 * parse->left list of variables
304 * parse->right list of values
305 * parse->third rules to execute
307 LIST *compile_local (PARSE *parse, LOL *args, int *jmp) {
308 LIST *l;
309 SETTINGS *s = NULL;
310 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
311 LIST *ns = (*parse->right->func)(parse->right, args, jmp);
312 LIST *result;
313 if (DEBUG_COMPILE) {
314 debug_compile(0, "local");
315 list_print(nt);
316 printf("= ");
317 list_print_ex(stdout, ns, LPFLAG_NO_TRSPACE);
318 printf("\n");
320 /* initial value is ns */
321 for (l = nt; l; l = list_next(l)) s = addsettings(s, 0, l->string, list_copy((LIST *)0, ns));
322 list_free(ns);
323 list_free(nt);
324 /* note that callees of the current context get this "local" */
325 /* variable, making it not so much local as layered */
326 pushsettings(s);
327 result = (*parse->third->func)(parse->third, args, jmp);
328 popsettings(s);
329 freesettings(s);
330 return result;
335 * compile_null() - do nothing -- a stub for parsing
337 JAMFA_CONST LIST *compile_null (PARSE *parse, LOL *args, int *jmp) {
338 return L0;
343 * compile_on() - run rule under influence of on-target variables
345 * parse->left target list; only first used
346 * parse->right rule to run
348 LIST *compile_on (PARSE *parse, LOL *args, int *jmp) {
349 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
350 LIST *result = NULL;
351 if (DEBUG_COMPILE) {
352 debug_compile(0, "on");
353 list_print_ex(stdout, nt, LPFLAG_NO_TRSPACE);
354 printf("\n");
356 /* copy settings, so that 'on target var on target = val' doesn't set var globally */
357 if (nt) {
358 TARGET *t = bindtarget(nt->string);
359 SETTINGS *s = copysettings(t->settings);
360 pushsettings(s);
361 result = (*parse->right->func)(parse->right, args, jmp);
362 popsettings(s);
363 freesettings(s);
365 list_free(nt);
366 return result;
371 * compile_rule() - compile a single user defined rule
373 * parse->left list of rules to run
374 * parse->right parameters (list of lists) to rule, recursing left
376 * Wrapped around evaluate_rule() so that headers() can share it.
378 LIST *compile_rule (PARSE *parse, LOL *args, int *jmp) {
379 LOL nargs[1];
380 LIST *result = NULL;
381 LIST *ll, *l;
382 PARSE *p;
383 /* list of rules to run -- normally 1! */
384 ll = (*parse->left->func)(parse->left, args, jmp);
385 /* build up the list of arg lists */
386 lol_init(nargs);
387 for (p = parse->right; p; p = p->left) lol_add(nargs, (*p->right->func)(p->right, args, jmp));
388 /* run rules, appending results from each */
389 for (l = ll; l; l = list_next(l)) result = evaluate_rule(l->string, nargs, result);
390 list_free(ll);
391 lol_free(nargs);
392 return result;
397 * evaluate_rule() - execute a rule invocation
399 LIST *evaluate_rule (const char *rulename, LOL *args, LIST *result) {
400 //#infdef OPT_EXPAND_RULE_NAMES_EXT
401 /*RULE *rule = bindrule(rulename);*/
402 //#else
403 RULE *rule;
404 char *expanded, *c;
405 dstring_t buf;
406 dstr_init(&buf);
407 if (var_string(rulename, &buf, args, ' ') < 0) {
408 dstr_done(&buf);
409 printf("Failed to expand rule %s -- expansion too long\n", rulename);
410 exit(EXITBAD);
412 expanded = dstr_cstr(&buf);
413 while (expanded[0] == ' ') ++expanded;
414 while ((c = strrchr(expanded, ' '))) *c = '\0';
415 if (DEBUG_COMPILE) {
416 debug_compile(1, rulename);
417 if (strcmp(rulename, expanded)) printf("-> %s ", expanded);
418 lol_print(args);
419 printf("\n");
421 rule = bindrule(expanded);
422 dstr_done(&buf);
423 //#endif
424 /* check traditional targets $(<) and sources $(>) */
425 if (!rule->actions && !rule->procedure) printf("warning: unknown rule %s\n", rule->name);
426 /* if this rule will be executed for updating the targets then construct the action for make() */
427 if (rule->actions) {
428 TARGETS *t;
429 ACTION *action;
430 /* the action is associated with this instance of this rule */
431 action = (ACTION *)malloc(sizeof(ACTION));
432 memset((char *)action, '\0', sizeof(*action));
433 action->rule = rule;
434 action->targets = targetlist((TARGETS *)0, lol_get(args, 0));
435 action->sources = targetlist((TARGETS *)0, lol_get(args, 1));
436 /* append this action to the actions of each target */
437 for (t = action->targets; t; t = t->next) t->target->actions = actionlist(t->target->actions, action);
439 /* now recursively compile any parse tree associated with this rule */
440 if (rule->procedure) {
441 PARSE *parse = rule->procedure;
442 SETTINGS *s = 0;
443 int jmp = JMP_NONE;
444 LIST *l;
445 int i;
446 /* build parameters as local vars */
447 for (l = rule->params, i = 0; l; l = l->next, i++) s = addsettings(s, 0, l->string, list_copy(L0, lol_get(args, i)));
448 /* run rule */
449 /* bring in local params */
450 /* refer/free to ensure rule not freed during use */
451 parse_refer(parse);
452 pushsettings(s);
453 result = list_append(result, (*parse->func)(parse, args, &jmp));
454 popsettings(s);
455 freesettings(s);
456 parse_free(parse);
458 if (DEBUG_COMPILE) debug_compile(-1, 0);
459 return result;
464 * compile_rules() - compile a chain of rules
466 * parse->left single rule
467 * parse->right more compile_rules() by right-recursion
469 LIST *compile_rules (PARSE *parse, LOL *args, int *jmp) {
470 /* ignore result from first statement; return the 2nd */
471 /* optimize recursion on the right by looping */
472 LIST *result = NULL;
473 while (*jmp == JMP_NONE && parse->func == compile_rules) {
474 list_free(result);
475 result = (*parse->left->func)(parse->left, args, jmp);
476 parse = parse->right;
478 if (*jmp == JMP_NONE) {
479 list_free(result);
480 result = (*parse->func)(parse, args, jmp);
482 return result;
487 * compile_set() - compile the "set variable" statement
489 * parse->left variable names
490 * parse->right variable values
491 * parse->num VAR_SET/APPEND/DEFAULT
493 LIST *compile_set (PARSE *parse, LOL *args, int *jmp) {
494 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
495 LIST *ns = (*parse->right->func)(parse->right, args, jmp);
496 LIST *l;
497 if (DEBUG_COMPILE) {
498 debug_compile(0, "set");
499 list_print(nt);
500 printf("%s ", set_names[parse->num]);
501 list_print_ex(stdout, ns, LPFLAG_NO_TRSPACE);
502 printf("\n");
504 /* call var_set to set variable */
505 /* var_set keeps ns, so need to copy it */
506 for (l = nt; l; l = list_next(l)) var_set(l->string, list_copy(L0, ns), parse->num);
507 list_free(nt);
508 return ns;
513 * compile_setcomp() - support for `rule` - save parse tree
515 * parse->string rule name
516 * parse->left list of argument names
517 * parse->right rules for rule
519 LIST *compile_setcomp (PARSE *parse, LOL *args, int *jmp) {
520 RULE *rule = bindrule(parse->string);
521 LIST *params = 0;
522 PARSE *p;
523 /* build param list */
524 for (p = parse->left; p; p = p->left) params = list_new(params, p->string, 1);
525 if (DEBUG_COMPILE) {
526 debug_compile(0, "rule");
527 printf("%s ", parse->string);
528 list_print_ex(stdout, params, LPFLAG_NO_TRSPACE);
529 printf("\n");
531 /* free old one, if present */
532 if (rule->procedure) parse_free(rule->procedure);
533 if (rule->params) list_free(rule->params);
534 rule->procedure = parse->right;
535 rule->params = params;
536 /* we now own this parse tree */
537 /* don't let parse_free() release it */
538 parse_refer(parse->right);
539 return L0;
544 * compile_setexec() - support for `actions` - save execution string
546 * parse->string rule name
547 * parse->string1 OS command string
548 * parse->num flags
549 * parse->left `bind` variables
551 * Note that the parse flags (as defined in compile.h) are transfered
552 * directly to the rule flags (as defined in rules.h).
554 LIST *compile_setexec (PARSE *parse, LOL *args, int *jmp) {
555 RULE *rule = bindrule(parse->string);
556 LIST *bindlist = (*parse->left->func)(parse->left, args, jmp);
557 /* free old one, if present */
558 if (rule->actions) {
559 freestr(rule->actions);
560 list_free(rule->bindlist);
562 rule->actions = copystr(parse->string1);
563 rule->bindlist = bindlist;
564 rule->flags = parse->num;
565 return L0;
570 * compile_settings() - compile the "on =" (set variable on exec) statement
572 * parse->left variable names
573 * parse->right target name
574 * parse->third variable value
575 * parse->num VAR_SET/APPEND/DEFAULT
577 LIST *compile_settings (PARSE *parse, LOL *args, int *jmp) {
578 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
579 LIST *ns = (*parse->third->func)(parse->third, args, jmp);
580 LIST *targets = (*parse->right->func)(parse->right, args, jmp);
581 LIST *ts;
582 if (DEBUG_COMPILE) {
583 debug_compile(0, "set");
584 list_print(nt);
585 printf("on ");
586 list_print(targets);
587 printf("%s ", set_names[parse->num]);
588 list_print_ex(stdout, ns, LPFLAG_NO_TRSPACE);
589 printf("\n");
591 /* call addsettings to save variable setting addsettings keeps ns, so need to copy it */
592 /* pass append flag to addsettings() */
593 for (ts = targets; ts; ts = list_next(ts)) {
594 TARGET *t = bindtarget(ts->string);
595 for (LIST *l = nt; l; l = list_next(l)) {
596 t->settings = addsettings(t->settings, parse->num, l->string, list_copy((LIST *)0, ns));
599 list_free(nt);
600 list_free(targets);
601 return ns;
606 * compile_switch() - compile 'switch' rule
608 * parse->left switch value (only 1st used)
609 * parse->right cases
611 * cases->left 1st case
612 * cases->right next cases
614 * case->string argument to match
615 * case->left parse tree to execute
617 LIST *compile_switch (PARSE *parse, LOL *args, int *jmp) {
618 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
619 LIST *result = NULL;
620 if (DEBUG_COMPILE) {
621 debug_compile(0, "switch");
622 list_print_ex(stdout, nt, LPFLAG_NO_TRSPACE);
623 printf("\n");
625 /* step through cases */
626 for (parse = parse->right; parse; parse = parse->right) {
627 if (!matchglob(parse->left->string, nt?nt->string:"")) {
628 /* get and exec parse tree for this case */
629 parse = parse->left->left;
630 result = (*parse->func)(parse, args, jmp);
631 break;
634 list_free(nt);
635 return result;
640 * compile_while() - compile 'while' rule
642 * parse->left condition tree
643 * parse->right execution tree
645 LIST *compile_while (PARSE *p, LOL *args, int *jmp) {
646 LIST *result = NULL;
647 LIST *l;
648 /* returns the value from the last execution of the block */
649 while ((*jmp == JMP_NONE ) && (l = (*p->left->func)(p->left, args, jmp))) {
650 /* always toss while's expression */
651 list_free(l);
652 /* keep only last result */
653 list_free(result);
654 result = (*p->right->func)(p->right, args, jmp);
655 /* continue loop? */
656 if (*jmp == JMP_CONTINUE) *jmp = JMP_NONE;
658 /* here by break/continue? */
659 if (*jmp == JMP_BREAK || *jmp == JMP_CONTINUE) *jmp = JMP_NONE;
660 /* returns result of last loop */
661 return result;
666 * debug_compile() - printf with indent to show rule expansion.
668 static void debug_compile (int which, const char *s) {
669 static int level = 0;
670 static const char indent[36] = ">>>>|>>>>|>>>>|>>>>|>>>>|>>>>|>>>>|";
671 int i = ((1+level)*2)%35;
672 if (which >= 0) printf("%*.*s ", i, i, indent);
673 if (s) printf("%s ", s);
674 level += which;