jam using new normalize_path(); NormPath built-in replaced with NormalizePath
[k8jam.git] / src / builtins.c
blobd53a5595ca89001e9f5bf216cc12f479ff029380
1 /*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
7 /*
8 * builtins.c - builtin jam rules
10 * External routines:
12 * load_builtin() - define builtin rules
14 * Internal routines:
16 * builtin_depends() - DEPENDS/INCLUDES rule
17 * builtin_echo() - ECHO rule
18 * builtin_exit() - EXIT rule
19 * builtin_flags() - NOCARE, NOTFILE, TEMPORARY rule
20 * builtin_glob() - GLOB rule
21 * builtin_match() - MATCH rule
22 * builtin_hdrmacro() - HDRMACRO rule
24 * 01/10/01 (seiwald) - split from compile.c
25 * 01/08/01 (seiwald) - new 'Glob' (file expansion) builtin
26 * 03/02/02 (seiwald) - new 'Match' (regexp match) builtin
27 * 04/03/02 (seiwald) - Glob matches only filename, not directory
28 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
29 * 10/22/02 (seiwald) - working return/break/continue statements
30 * 11/04/02 (seiwald) - const-ing for string literals
31 * 12/03/02 (seiwald) - fix odd includes support by grafting them onto depends
32 * 01/14/03 (seiwald) - fix includes fix with new internal includes TARGET
34 #include <limits.h>
35 #include <stdarg.h>
36 #include <stdint.h>
37 #include <strings.h>
38 #include <unistd.h>
40 #include "jam.h"
42 #include "lists.h"
43 #include "parse.h"
44 #include "bjprng.h"
45 #include "builtins.h"
46 #include "rules.h"
47 #include "filesys.h"
48 #include "newstr.h"
49 #include "re9.h"
50 #include "pathsys.h"
51 #include "hdrmacro.h"
52 #include "matchglob.h"
53 #include "dstrings.h"
57 * builtin_depends() - DEPENDS/INCLUDES rule
59 * The DEPENDS builtin rule appends each of the listed sources on the
60 * dependency list of each of the listed targets.
61 * It binds both the targets and sources as TARGETs.
63 static LIST *builtin_depends (PARSE *parse, LOL *args, int *jmp) {
64 LIST *targets = lol_get(args, 0);
65 LIST *sources = lol_get(args, 1);
66 for (LIST *l = targets; l; l = list_next(l)) {
67 TARGET *t = bindtarget(l->string);
68 /* If doing INCLUDES, switch to the TARGET's include TARGET, creating it if needed.
69 * The internal include TARGET shares the name of its parent. */
70 if (parse->num) {
71 if (!t->includes) t->includes = copytarget(t);
72 t = t->includes;
74 t->depends = targetlist(t->depends, sources);
76 return L0;
80 typedef struct {
81 int no_newline;
82 int no_out;
83 int flags;
84 FILE *stream;
85 } echo_flags_t;
88 static void parse_echo_flags (echo_flags_t *flg, const LIST *l) {
89 flg->no_newline = 0;
90 flg->no_out = 0;
91 flg->flags = LPFLAG_NO_TRSPACE;
92 flg->stream = stdout;
93 for (; l; l = list_next(l)) {
94 const char *s = l->string;
95 if (*s == '-') {
96 for (++s; *s; ++s) {
97 switch (*s) {
98 case 'n': flg->no_newline = 1; break;
99 case 'Q': flg->no_out = 1; break;
100 case 'S': flg->flags |= LPFLAG_NO_SPACES; break;
101 case 's': flg->flags &= ~LPFLAG_NO_TRSPACE; break;
102 case 'w': flg->stream = stderr; break;
111 * builtin_echo() - ECHO rule
113 * The ECHO builtin rule echoes the targets to the user.
114 * No other actions are taken.
116 static LIST *builtin_echo (PARSE *parse, LOL *args, int *jmp) {
117 echo_flags_t ef;
118 parse_echo_flags(&ef, lol_get(args, 1));
119 if (!ef.no_out) list_print_ex(ef.stream, lol_get(args, 0), ef.flags);
120 if (!ef.no_newline) fputc('\n', ef.stream); else fflush(ef.stream);
121 return L0;
126 * builtin_exit() - EXIT rule
128 * The EXIT builtin rule echoes the targets to the user and exits
129 * the program with a failure status.
131 static LIST *builtin_exit (PARSE *parse, LOL *args, int *jmp) {
132 LIST *l = lol_get(args, 0);
133 echo_flags_t ef;
134 parse_echo_flags(&ef, lol_get(args, 1));
135 if (l != NULL) {
136 if (!ef.no_out) list_print_ex(ef.stream, l, ef.flags);
137 if (!ef.no_newline) fputc('\n', ef.stream); else fflush(ef.stream);
138 exit(EXITBAD); /* yeech */
140 exit(EXITOK);
141 #if __GNUC__ <= 2
142 return L0;
143 #endif
148 * builtin_flags() - NOCARE, NOTFILE, TEMPORARY rule
150 * Builtin_flags() marks the target with the appropriate flag, for use by make0().
151 * It binds each target as a TARGET.
153 static LIST *builtin_flags (PARSE *parse, LOL *args, int *jmp) {
154 LIST *l = lol_get(args, 0);
155 int flag = parse->num, andflag = ~0;
156 switch (flag) {
157 case T_FLAG_NOCARE: andflag = ~T_FLAG_FORCECARE; break;
158 case T_FLAG_FORCECARE: andflag = ~T_FLAG_NOCARE; break;
159 case 666: flag = 0; andflag = ~T_FLAG_NOTFILE; break;
161 for (; l; l = list_next(l)) {
162 TARGET *t = bindtarget(l->string);
163 t->flags |= flag;
164 t->flags &= andflag;
166 return L0;
170 typedef enum {
171 GLOB_ANY,
172 GLOB_DIRS,
173 GLOB_FILES
174 } glob_mode;
178 * builtin_globbing() - GLOB rule
180 struct globbing {
181 LIST *patterns;
182 LIST *results;
183 int casesens;
184 int cmptype; // <0:glob; 0: plain; >0:# of regexps
185 glob_mode mode;
186 int namesonly;
187 regexp_t **re;
191 static void builtin_glob_back (void *closure, const char *file, int status, time_t time) {
192 struct globbing *globbing = (struct globbing *)closure;
193 LIST *l;
194 PATHNAME f;
195 static char buf[MAXJPATH];
196 /* null out directory for matching */
197 /* we wish we had file_dirscan() pass up a PATHNAME */
198 path_parse(file, &f);
199 f.f_dir.len = 0;
200 /* For globbing, we unconditionally ignore current and parent
201 * directory items. Since those items always exist, there's no
202 * reason why caller of GLOB would want to see them.
203 * We could also change file_dirscan, but then paths with embedded
204 * "." and ".." won't work anywhere. */
205 /* k8: will this break anything? it shouldn't... */
206 if (!strcmp(f.f_base.ptr, ".") || !strcmp(f.f_base.ptr, "..")) return;
207 path_build(buf, &f);
209 fprintf(stderr, "buf: [%s]\n", buf);
210 int c;
211 for (c = 0; c < 6; ++c) fprintf(stderr, " %d: [%s]\n", c, f.part[c].ptr);
213 if (globbing->mode != GLOB_ANY) {
214 int ftype = file_type(file);
215 switch (globbing->mode) {
216 case GLOB_DIRS: if (ftype != 1) return; break;
217 case GLOB_FILES: if (ftype != 0) return; break;
218 default: ;
221 if (globbing->cmptype < 0) {
222 for (l = globbing->patterns; l; l = l->next) {
223 if (matchglobex(l->string, buf, globbing->casesens) == 0) {
224 globbing->results = list_new(globbing->results, (globbing->namesonly ? buf : file), 0);
225 break;
228 } else if (globbing->cmptype > 0) {
229 for (int f = 0; f < globbing->cmptype; ++f) {
230 if (regexp_execute(globbing->re[f], buf, NULL, 0) > 0) {
231 globbing->results = list_new(globbing->results, (globbing->namesonly ? buf : file), 0);
232 break;
235 } else {
236 for (l = globbing->patterns; l; l = l->next) {
237 if ((globbing->casesens ? strcmp : strcasecmp)(l->string, buf) == 0) {
238 globbing->results = list_new(globbing->results, (globbing->namesonly ? buf : file), 0);
239 break;
246 static LIST *builtin_glob (PARSE *parse, LOL *args, int *jmp) {
247 LIST *l = lol_get(args, 0);
248 LIST *r = lol_get(args, 1);
249 LIST *lo;
250 struct globbing globbing;
251 if (!r) return L0;
252 globbing.results = L0;
253 globbing.patterns = r;
254 globbing.casesens = 1;
255 globbing.cmptype = -1;
256 globbing.mode = GLOB_ANY;
257 globbing.namesonly = 0;
258 for (lo = lol_get(args, 2); lo != NULL; lo = lo->next) {
259 if (!strcmp("case-sensitive", lo->string)) globbing.casesens = 1;
260 else if (!strcmp("case-insensitive", lo->string)) globbing.casesens = 0;
261 else if (!strcmp("ignore-case", lo->string)) globbing.casesens = 0;
262 else if (!strcmp("glob", lo->string)) globbing.cmptype = -1;
263 else if (!strcmp("regexp", lo->string)) globbing.cmptype = 1;
264 else if (!strcmp("plain", lo->string)) globbing.cmptype = 0;
265 else if (!strcmp("dirs-only", lo->string)) globbing.mode = GLOB_DIRS;
266 else if (!strcmp("files-only", lo->string)) globbing.mode = GLOB_FILES;
267 else if (!strcmp("any", lo->string)) globbing.mode = GLOB_ANY;
268 else if (!strcmp("names-only", lo->string)) globbing.namesonly = 1;
269 else if (!strcmp("full-path", lo->string)) globbing.namesonly = 0;
270 else {
271 printf("jam: invalid option for Glob built-in: '%s'\n", lo->string);
272 exit(EXITBAD); /* yeech */
275 if (globbing.cmptype > 0) {
276 /* compile regexps */
277 globbing.cmptype = list_length(r);
278 globbing.re = malloc(sizeof(globbing.re[0])*globbing.cmptype);
279 if (globbing.re == NULL) { printf("FATAL: out of memory in Glob\n"); exit(42); }
280 for (int f = 0; r; r = r->next, ++f) globbing.re[f] = regexp_compile(r->string, (globbing.casesens ? 0 : RE9_FLAG_CASEINSENS));
281 } else {
282 globbing.re = NULL;
284 for (; l; l = list_next(l)) file_dirscan(l->string, builtin_glob_back, &globbing);
285 if (globbing.re != NULL) {
286 for (int f = 0; f < globbing.cmptype; ++f) regexp_free(globbing.re[f]);
287 free(globbing.re);
289 return globbing.results;
294 * builtin_match() - MATCH rule, regexp matching
296 static LIST *builtin_match (PARSE *parse, LOL *args, int *jmp) {
297 LIST *l, *lo;
298 LIST *res = L0;
299 int casesens = 1;
300 int cmptype = 1;
301 for (lo = lol_get(args, 2); lo != NULL; lo = lo->next) {
302 if (!strcmp("case-sensitive", lo->string)) casesens = 1;
303 else if (!strcmp("case-insensitive", lo->string)) casesens = 0;
304 else if (!strcmp("ignore-case", lo->string)) casesens = 0;
305 else if (!strcmp("glob", lo->string)) cmptype = -1;
306 else if (!strcmp("regexp", lo->string)) cmptype = 1;
307 else if (!strcmp("plain", lo->string)) cmptype = 0;
308 else {
309 printf("jam: invalid option for Match built-in: '%s'\n", lo->string);
310 exit(EXITBAD); /* yeech */
313 /* for each pattern */
314 for (l = lol_get(args, 0); l; l = l->next) {
315 LIST *r;
316 if (cmptype > 0) {
317 regexp_t *re;
318 re9_sub_t mt[RE9_SUBEXP_MAX];
319 re = regexp_compile(l->string, (casesens ? 0: RE9_FLAG_CASEINSENS));
320 /* for each string to match against */
321 for (r = lol_get(args, 1); r; r = r->next) {
322 mt[0].sp = mt[0].ep = NULL;
323 if (regexp_execute(re, r->string, mt, RE9_SUBEXP_MAX) > 0) {
324 dstring_t buf;
325 /* add all parameters up to highest onto list */
326 /* must have parameters to have results! */
327 //fprintf(stderr, "re: <%s>: nsub=%d\n", re->restr, re9_nsub(re->re));
328 dstr_init(&buf);
329 for (int i = 1; i < re9_nsub(re->re); ++i) {
330 int l = mt[i].ep-mt[i].sp;
331 dstr_clear(&buf);
332 if (l > 0) dstr_push_buf(&buf, mt[i].sp, l);
333 res = list_new(res, dstr_cstr(&buf), 0);
335 /* add full match as last item */
337 int l = mt[0].ep-mt[0].sp;
338 if (l > 0) dstr_push_buf(&buf, mt[0].sp, l); else dstr_push_cstr(&buf, "1");
339 res = list_new(res, dstr_cstr(&buf), 0);
341 dstr_done(&buf);
344 regexp_free(re);
345 } else if (cmptype < 0) {
346 for (r = lol_get(args, 1); r; r = r->next) {
347 if (matchglobex(l->string, r->string, casesens) == 0) {
348 res = list_new(res, r->string, 0);
351 } else {
352 for (r = lol_get(args, 1); r; r = r->next) {
353 if ((casesens ? strcmp : strcasecmp)(l->string, r->string) == 0) {
354 res = list_new(res, r->string, 0);
359 return res;
363 static LIST *builtin_hdrmacro (PARSE *parse, LOL *args, int *jmp) {
364 LIST *l = lol_get(args, 0);
365 for (; l; l = list_next(l)) {
366 TARGET *t = bindtarget(l->string);
367 /* scan file for header filename macro definitions */
368 if (DEBUG_HEADER) printf("scanning '%s' for header file macro definitions\n", l->string);
369 macro_headers(t);
371 return L0;
375 /* backported from boost-jam */
377 * Return the current working directory.
379 * Usage: pwd = [ PWD ] ;
381 static LIST *builtin_pwd (PARSE *parse, LOL *args, int *jmp) {
382 char pwd_buffer[PATH_MAX];
383 if (!getcwd(pwd_buffer, sizeof(pwd_buffer))) {
384 perror("can not get current directory");
385 return L0;
387 return list_new(L0, pwd_buffer, 0);
391 /* backported from boost-jam */
392 static LIST *builtin_sort (PARSE *parse, LOL *args, int *jmp) {
393 LIST *arg = lol_get(args, 0);
394 arg = list_sort(arg);
395 return arg;
399 /* backported from boost-jam; greatly improved */
400 /* Command shcmd [[ : options ]] */
401 static LIST *builtin_command (PARSE *parse, LOL *args, int *jmp) {
402 LIST *res = L0;
403 LIST *l;
404 int ret;
405 char buffer[1024], buf1[32], *spos, *epos;
406 FILE *p = NULL;
407 int exitStatus = -1;
408 int optExitStatus = 0;
409 int optNoOutput = 0;
410 int optTrimLeft = 1;
411 int optTrimRight = 1;
412 int optStatus1st = 0;
413 int optParseOut = 0;
414 int optSpaceBreak = 1;
415 int optTabBreak = 1;
416 int optCRBreak = 1;
417 int optLFBreak = 1;
418 int no_options = ((l = lol_get(args, 1)) == NULL);
419 dstring_t str;
420 /* for each string in 2nd list: check for arg */
421 for (; l != NULL; l = l->next) {
422 if (!strcmp("exit-status", l->string)) optExitStatus = 1;
423 else if (!strcmp("exit-code", l->string)) optExitStatus = 1;
424 else if (!strcmp("status-first", l->string)) optStatus1st = 1;
425 else if (!strcmp("code-first", l->string)) optStatus1st = 1;
426 else if (!strcmp("no-output", l->string)) optNoOutput = 1;
427 else if (!strcmp("no-trim", l->string)) optTrimLeft = optTrimRight = 0;
428 else if (!strcmp("no-trim-left", l->string)) optTrimLeft = 0;
429 else if (!strcmp("no-trim-right", l->string)) optTrimRight = 0;
430 else if (!strcmp("parse-output", l->string)) optParseOut = 1;
431 else if (!strcmp("no-space-break", l->string)) optSpaceBreak = 0;
432 else if (!strcmp("no-tab-break", l->string)) optTabBreak = 0;
433 else if (!strcmp("no-nl-break", l->string)) optLFBreak = 0;
434 else if (!strcmp("no-lf-break", l->string)) optLFBreak = 0;
435 else if (!strcmp("no-cr-break", l->string)) optCRBreak = 0;
436 else if (!strcmp("dummy", l->string) || !strcmp("xyzzy", l->string)) {}
437 else {
438 printf("jam: invalid option for Command built-in: '%s'\n", l->string);
439 exit(EXITBAD); /* yeech */
442 if (no_options) optNoOutput = 1;
443 /* build shell command */
444 dstr_init(&str);
445 /* for each arg */
446 for (l = lol_get(args, 0); l; l = l->next) {
447 if (dstr_len(&str)) dstr_push_char(&str, ' ');
448 dstr_push_cstr(&str, l->string);
450 /* no shell command? */
451 if (dstr_len(&str) < 1) { dstr_done(&str); return L0; }
452 fflush(NULL); /* flush ALL output streams */
453 p = popen(dstr_cstr(&str), "r");
454 if (!p) { dstr_done(&str); return L0; }
455 dstr_clear(&str);
456 while ((ret = fread(buffer, sizeof(char), sizeof(buffer)-1, p)) > 0) {
457 if (!optNoOutput) {
458 buffer[ret] = 0;
459 dstr_push_cstr(&str, buffer);
462 exitStatus = pclose(p);
463 if (no_options) {
464 if (exitStatus) {
465 snprintf(buf1, sizeof(buf1), "%d", exitStatus);
466 res = list_new(L0, buf1, 0);
467 } else {
468 res = L0;
470 } else {
471 if (optExitStatus && optStatus1st) {
472 snprintf(buf1, sizeof(buf1), "%d", exitStatus);
473 res = list_new(res, buf1, 0);
475 /* trim output if necessary */
476 if (!optNoOutput) {
477 if (!optParseOut) {
478 /* don't parse */
479 if (optTrimRight) {
480 // trim trailing blanks
481 int sl = dstr_len(&str);
482 spos = dstr_cstr(&str);
483 while (sl > 0 && (unsigned char)spos[sl-1] <= ' ') --sl;
484 dstr_chop(&str, sl);
486 spos = dstr_cstr(&str);
487 if (optTrimLeft) {
488 // trim leading blanks
489 while (*spos && *((unsigned char *)spos) <= ' ') ++spos;
491 res = list_new(res, spos, 0);
492 } else {
493 dstring_t tmp;
494 /* parse output */
495 ret = 0; /* was anything added? list must have at least one element */
496 spos = dstr_cstr(&str);
497 dstr_init(&tmp);
498 while (*spos) {
499 /* skip delimiters */
500 while (*spos) {
501 unsigned char ch = (unsigned char)(*spos);
502 if (ch == ' ') { if (!optSpaceBreak) break; }
503 else if (ch == '\t') { if (!optTabBreak) break; }
504 else if (ch == '\r') { if (!optCRBreak) break; }
505 else if (ch == '\n') { if (!optLFBreak) break; }
506 else if (ch > ' ') break;
507 ++spos;
509 if (!*spos) break;
510 epos = spos+1;
511 while (*epos) {
512 int ch = *epos;
513 if (ch == ' ') { if (optSpaceBreak) break; }
514 else if (ch == '\t') { if (optTabBreak) break; }
515 else if (ch == '\r') { if (optCRBreak) break; }
516 else if (ch == '\n') { if (optLFBreak) break; }
517 else if ((unsigned char)ch <= ' ') break;
518 ++epos;
520 dstr_clear(&tmp);
521 dstr_push_memrange(&tmp, spos, epos);
522 res = list_new(res, dstr_cstr(&tmp), 0);
523 ret = 1;
524 spos = epos;
526 dstr_done(&tmp);
527 if (!ret) { buf1[0] = '\0'; res = list_new(res, buf1, 0); }
530 /* command exit result next */
531 if (optExitStatus && !optStatus1st) {
532 snprintf(buf1, sizeof(buf1), "%d", exitStatus);
533 res = list_new(res, buf1, 0);
536 dstr_done(&str);
537 return res;
541 /* ExprI1 op0 math op1 */
542 static LIST *builtin_expri1 (PARSE *parse, LOL *args, int *jmp) {
543 char buffer[100];
544 int op0, op1, res, comp = 0;
545 LIST *el = lol_get(args, 0);
546 if (!el || !el->next) return L0;
547 if (el->string[0] == '#') {
548 // string length
549 snprintf(buffer, sizeof(buffer), "%u", (unsigned int)(strlen(el->next->string)));
550 return list_new(L0, buffer, 0);
552 if (!el->next->next) return L0;
553 op0 = atoi(el->string);
554 op1 = atoi(el->next->next->string);
555 res = 0;
556 switch (el->next->string[0]) {
557 case '+': res = op0+op1; break;
558 case '-': res = op0-op1; break;
559 case '*': res = op0*op1; break;
560 case '/': res = op0/op1; break;
561 case '%': res = op0%op1; break;
562 case '<':
563 comp = 1;
564 if (el->next->string[1] == '=') res = (op0 <= op1); else res = (op0 < op1);
565 break;
566 case '=': comp = 1; res = (op0 == op1); break;
567 case '!': comp = 1; res = (op0 != op1); break;
568 case '>':
569 comp = 1;
570 if (el->next->string[1] == '=') res = (op0 >= op1); else res = (op0 > op1);
571 break;
572 default:
573 printf("jam: rule ExprI1: unknown operator: '%s'\n", el->next->string);
574 exit(EXITBAD);
576 if (comp) return (res ? list_new(L0, "tan", 0) : L0);
577 snprintf(buffer, sizeof(buffer), "%d", res);
578 return list_new(L0, buffer, 0);
582 /* based on the code from ftjam by David Turner */
583 static LIST *builtin_split (PARSE *parse, LOL *args, int *jmp) {
584 LIST *input = lol_get(args, 0);
585 LIST *tokens = lol_get(args, 1);
586 LIST *res = L0;
587 char token[256];
588 dstring_t str;
589 int explode = 0;
590 dstr_init(&str);
591 /* build token array */
592 if (tokens == NULL) {
593 memset(token, 1, sizeof(token));
594 explode = 1;
595 } else {
596 memset(token, 0, sizeof(token));
597 for (; tokens; tokens = tokens->next) {
598 const char *s = tokens->string;
599 for (; *s; ++s) token[(unsigned char)*s] = 1;
601 if (memchr(token, 1, sizeof(token)) == NULL) {
602 memset(token, 1, sizeof(token));
603 explode = 1;
606 token[0] = 0;
607 /* now parse the input and split it */
608 for (; input; input = input->next) {
609 const char *ptr = input->string;
610 const char *lastPtr = input->string;
611 while (*ptr) {
612 if (token[(unsigned char)*ptr]) {
613 size_t count = ptr-lastPtr+explode;
614 if (count > 0) {
615 dstr_clear(&str);
616 dstr_push_memrange(&str, lastPtr, ptr+explode);
617 res = list_new(res, dstr_cstr(&str), 0);
619 lastPtr = ptr+1;
621 ++ptr;
623 if (ptr > lastPtr) res = list_new(res, lastPtr, 0);
625 dstr_done(&str);
626 return res;
631 * builtin_dependslist()
633 * The DependsList builtin rule returns list of dependencies for
634 * a given target.
636 static LIST *builtin_dependslist (PARSE *parse, LOL *args, int *jmp) {
637 LIST *res = L0;
638 LIST *parents;
639 for (parents = lol_get(args, 0); parents; parents = parents->next) {
640 TARGET *t = bindtarget(parents->string);
641 TARGETS *child;
642 for (child = t->depends; child; child = child->next) res = list_new(res, child->target->name, 1);
644 return res;
649 * NormalizePath path [: pwd]
651 * it will add 'pwd' if path is not absolute and will try to resolve some '.' and '..' (only leading '..' though).
652 * it can be used in SubDir replacement to automate dir building
653 * if there is no $(2), use current directory as 'pwd'
655 static LIST *builtin_normpath (PARSE *parse, LOL *args, int *jmp) {
656 LIST *el = lol_get(args, 0), *pl = lol_get(args, 1);
657 char *buf;
658 int bsz;
659 if (!el || !el->string) return L0;
660 bsz = strlen(el->string)*2+1024;
661 buf = malloc(bsz);
662 if (buf == NULL) return L0;
663 if (!normalize_path(el->string, buf, bsz, (pl != NULL ? pl->string : NULL))) { free(buf); return L0; }
664 el = list_new(NULL, buf, 0);
665 free(buf);
666 return el;
671 * ListLength list
673 static LIST *builtin_listlength (PARSE *parse, LOL *args, int *jmp) {
674 char buffer[100];
675 LIST *el = lol_get(args, 0);
676 if (!el) return list_new(L0, "0", 0);
677 snprintf(buffer, sizeof(buffer), "%d", list_length(el));
678 return list_new(L0, buffer, 0);
683 * HaveRule and HaveActions
685 typedef struct {
686 LIST *el;
687 int wantAction;
688 int casesens;
689 } HRNGCI;
692 typedef struct {
693 const char *str;
694 int wantAction;
695 int casesens;
696 } HRNormalData;
699 static int hr_normal (const void *hdata, void *udata) {
700 const RULE *r = (const RULE *)hdata;
701 const HRNormalData *d = (const HRNormalData *)udata;
702 if (strcasecmp(r->name, d->str) == 0) {
703 if (d->wantAction && r->actions) return 1; // got it
704 if (!d->wantAction && r->procedure) return 1; // got it
706 return 0;
710 static int hr_glob (const void *hdata, void *udata) {
711 const RULE *r = (const RULE *)hdata;
712 const HRNormalData *d = (const HRNormalData *)udata;
713 if (matchglobex(d->str, r->name, d->casesens) == 0) {
714 //fprintf(stderr, ":[%s]\n", r->name);
715 if (d->wantAction && r->actions) return 1; // got it
716 if (!d->wantAction && r->procedure) return 1; // got it
718 return 0;
722 typedef struct {
723 regexp_t *re;
724 int reflags;
725 int wantAction;
726 } HRREData;
729 static int hr_regexp (const void *hdata, void *udata) {
730 const RULE *r = (const RULE *)hdata;
731 HRREData *d = (HRREData *)udata;
732 if (regexp_execute(d->re, r->name, NULL, 0) > 0) {
733 //fprintf(stderr, ":[%s]\n", r->name);
734 if (d->wantAction && r->actions) return 1; // got it
735 if (!d->wantAction && r->procedure) return 1; // got it
737 return 0;
741 static LIST *builtin_haveruleactions (PARSE *parse, LOL *args, int *jmp) {
742 LIST *el = lol_get(args, 0), *l;
743 int wantAction = parse->num;
744 int casesens = 1;
745 int cmptype = 0; // <0:glob; >0:regexp
746 if (!el) return L0;
747 for (l = lol_get(args, 1); l != NULL; l = l->next) {
748 if (!strcmp("case-sensitive", l->string)) casesens = 1;
749 else if (!strcmp("case-insensitive", l->string)) casesens = 0;
750 else if (!strcmp("ignore-case", l->string)) casesens = 0;
751 else if (!strcmp("glob", l->string)) cmptype = -1;
752 else if (!strcmp("regexp", l->string)) cmptype = 1;
753 else if (!strcmp("plain", l->string)) cmptype = 0;
754 else {
755 printf("jam: invalid option for Have%s built-in: '%s'\n", (wantAction ? "Actions" : "Rule"), l->string);
756 exit(EXITBAD); /* yeech */
759 if (casesens == 1 && cmptype == 0) {
760 // standard mode
761 for (; el; el = el->next) {
762 RULE *r = findrule(el->string);
763 if (!r) return L0;
764 if (wantAction && !r->actions) return L0;
765 if (!wantAction && !r->procedure) return L0;
767 } else if (cmptype < 0) {
768 // glob
769 HRNormalData nfo;
770 nfo.wantAction = wantAction;
771 nfo.casesens = casesens;
772 for (; el; el = el->next) {
773 nfo.str = el->string;
774 if (!iteraterules(hr_glob, &nfo)) return L0;
776 } else if (cmptype > 0) {
777 // regexp
778 HRREData nfo;
779 nfo.wantAction = wantAction;
780 for (; el; el = el->next) {
781 int err;
782 nfo.re = regexp_compile(el->string, (casesens ? 0 : RE9_FLAG_CASEINSENS));
783 /*printf("FATAL: invalid regexp in Have%s: %s\n", (wantAction ? "Actions" : "Rule"), errmsg);*/
784 err = iteraterules(hr_regexp, &nfo);
785 regexp_free(nfo.re);
786 if (!err) return L0;
788 } else {
789 // normal, case-insensitive
790 HRNormalData nfo;
791 nfo.wantAction = wantAction;
792 for (; el; el = el->next) {
793 nfo.str = el->string;
794 if (!iteraterules(hr_normal, &nfo)) return L0;
797 return list_new(L0, "1", 0);
801 /* generate random name:
802 * [ RandName ] -- /tmp/XXX
803 * [ RandName "abc/" ] -- abc/XXX
804 * [ RandName "" ] -- XXX
806 static LIST *builtin_randname (PARSE *parse, LOL *args, int *jmp) {
807 static BJRandCtx rctx;
808 static int initialized = 0;
809 static const char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyz";
810 LIST *el;
811 char buffer[9], *s;
812 const char *path;
813 #ifdef OS_NT
814 static char tp[8192]
815 #endif
816 if (!initialized) {
817 initialized = 1;
818 bjprngInit(&rctx, bjprngGenRandSeed());
819 #ifdef OS_NT
820 GetTempPath(sizeof(tp), tp);
821 #endif
823 for (int f = 0; f < 8; ++f) buffer[f] = alphabet[bjprngRand(&rctx)%strlen(alphabet)];
824 buffer[8] = 0;
825 el = lol_get(args, 0);
826 path = (el != NULL && el->string != NULL ? el->string :
827 #ifdef OS_NT
829 #else
830 "/tmp/"
831 #endif
833 s = alloca(strlen(path)+strlen(buffer)+2);
834 sprintf(s, "%s%s", path, buffer);
835 return list_new(L0, s, 0);
839 /* write list to file:
840 * ListFileWrite filename : list [: terminator] [: append]
841 * default terminator is '\n'
842 * return success flag
844 static LIST *builtin_listwrite (PARSE *parse, LOL *args, int *jmp) {
845 LIST *el = lol_get(args, 0);
846 if (el != NULL && el->string != NULL && el->string[0]) {
847 LIST *l = lol_get(args, 3);
848 FILE *fo = fopen(el->string, (l != NULL && l->string[0] ? "a" : "w"));
849 if (fo != NULL) {
850 const char *term;
851 l = lol_get(args, 2);
852 term = (l != NULL ? l->string : "\n");
853 for (l = lol_get(args, 1); l != NULL; l = l->next) {
854 if (fprintf(fo, "%s%s", l->string, term) < 0) {
855 fclose(fo);
856 unlink(el->string);
857 return L0;
860 fclose(fo);
861 return list_new(L0, "1", 0);
864 return L0;
868 /* remove duplicates from list */
869 static LIST *builtin_listremdups (PARSE *parse, LOL *args, int *jmp) {
870 LIST *el = lol_get(args, 0);
871 if (el != NULL) {
872 LIST *l, *res = list_new(L0, el->string, 1);
873 for (l = el->next; l != NULL; l = l->next) {
874 int found = 0;
875 for (LIST *t = el; t != l && !found; t = t->next) if (strcmp(t->string, l->string) == 0) found = 1;
876 if (!found) res = list_new(res, l->string, 1);
878 return res;
880 return el; /* no need to change anything */
885 * compile_builtin() - define builtin rules
888 #define P0 ((PARSE *)0)
889 #define C0 ((char *)0)
892 /* ":" -- previous name in upper case; "." -- previous name in lower case */
893 static inline void bind_builtin (PARSE *pp, const char *name) {
894 bindrule(name)->procedure = pp;
898 static inline void bind_builtin2 (PARSE *pp, const char *name, const char *name1) {
899 bindrule(name)->procedure = pp;
900 bindrule(name1)->procedure = pp;
904 void load_builtins (void) {
905 bind_builtin(parse_make(builtin_depends, P0, P0, P0, C0, C0, 0),
906 "Depends");
907 bind_builtin(parse_make(builtin_depends, P0, P0, P0, C0, C0, 1),
908 "Includes");
909 bind_builtin(parse_make(builtin_dependslist, P0, P0, P0, C0, C0, 0),
910 "DependsList");
912 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_TOUCHED),
913 "Always");
914 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_LEAVES),
915 "Leaves");
916 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_NOCARE),
917 "NoCare");
918 bind_builtin2(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_NOTFILE),
919 "NotFile", "NoTime");
920 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_NOUPDATE),
921 "NoUpdate");
922 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_TEMP),
923 "Temporary");
924 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_FORCECARE),
925 "ForceCare");
926 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, 666),
927 "ForceFile");
929 bind_builtin(parse_make(builtin_echo, P0, P0, P0, C0, C0, 0),
930 "Echo");
932 bind_builtin(parse_make(builtin_exit, P0, P0, P0, C0, C0, 0),
933 "Exit");
935 bind_builtin(parse_make(builtin_glob, P0, P0, P0, C0, C0, 0),
936 "Glob");
937 bind_builtin(parse_make(builtin_match, P0, P0, P0, C0, C0, 0),
938 "Match");
940 bind_builtin(parse_make(builtin_hdrmacro, P0, P0, P0, C0, C0, 0),
941 "HdrMacro");
943 bind_builtin(parse_make(builtin_pwd, P0, P0, P0, C0, C0, 0),
944 "Pwd");
946 bind_builtin(parse_make(builtin_sort, P0, P0, P0, C0, C0, 0),
947 "Sort");
949 bind_builtin(parse_make(builtin_command, P0, P0, P0, C0, C0, 0),
950 "Command");
952 bind_builtin(parse_make(builtin_expri1, P0, P0, P0, C0, C0, 0),
953 "ExprI1");
955 bind_builtin(parse_make(builtin_split, P0, P0, P0, C0, C0, 0),
956 "Split");
958 bind_builtin(parse_make(builtin_normpath, P0, P0, P0, C0, C0, 0),
959 "NormalizePath");
961 bind_builtin(parse_make(builtin_listlength, P0, P0, P0, C0, C0, 0),
962 "ListLength");
963 bind_builtin(parse_make(builtin_listwrite, P0, P0, P0, C0, C0, 0),
964 "ListWrite");
965 bind_builtin(parse_make(builtin_listremdups, P0, P0, P0, C0, C0, 0),
966 "ListRemoveDuplicates");
968 bind_builtin(parse_make(builtin_haveruleactions, P0, P0, P0, C0, C0, 0),
969 "HaveRule");
970 bind_builtin(parse_make(builtin_haveruleactions, P0, P0, P0, C0, C0, 1),
971 "HaveActions");
973 bind_builtin(parse_make(builtin_randname, P0, P0, P0, C0, C0, 0),
974 "RandName");