Match: fixed 'full match' (was gibberish)
[k8jam.git] / src / builtins.c
blob786430fee651e6e74dd0e9eaa87611fdc3b3eae6
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 dstr_clear(&buf);
339 if (l > 0) dstr_push_buf(&buf, mt[0].sp, l); else dstr_push_cstr(&buf, "1");
340 res = list_new(res, dstr_cstr(&buf), 0);
342 dstr_done(&buf);
345 regexp_free(re);
346 } else if (cmptype < 0) {
347 for (r = lol_get(args, 1); r; r = r->next) {
348 if (matchglobex(l->string, r->string, casesens) == 0) {
349 res = list_new(res, r->string, 0);
352 } else {
353 for (r = lol_get(args, 1); r; r = r->next) {
354 if ((casesens ? strcmp : strcasecmp)(l->string, r->string) == 0) {
355 res = list_new(res, r->string, 0);
360 return res;
364 static LIST *builtin_hdrmacro (PARSE *parse, LOL *args, int *jmp) {
365 LIST *l = lol_get(args, 0);
366 for (; l; l = list_next(l)) {
367 TARGET *t = bindtarget(l->string);
368 /* scan file for header filename macro definitions */
369 if (DEBUG_HEADER) printf("scanning '%s' for header file macro definitions\n", l->string);
370 macro_headers(t);
372 return L0;
376 /* backported from boost-jam */
378 * Return the current working directory.
380 * Usage: pwd = [ PWD ] ;
382 static LIST *builtin_pwd (PARSE *parse, LOL *args, int *jmp) {
383 char pwd_buffer[PATH_MAX];
384 if (!getcwd(pwd_buffer, sizeof(pwd_buffer))) {
385 perror("can not get current directory");
386 return L0;
388 return list_new(L0, pwd_buffer, 0);
392 /* backported from boost-jam */
393 static LIST *builtin_sort (PARSE *parse, LOL *args, int *jmp) {
394 LIST *arg = lol_get(args, 0);
395 arg = list_sort(arg);
396 return arg;
400 /* backported from boost-jam; greatly improved */
401 /* Command shcmd [[ : options ]] */
402 static LIST *builtin_command (PARSE *parse, LOL *args, int *jmp) {
403 LIST *res = L0;
404 LIST *l;
405 int ret;
406 char buffer[1024], buf1[32], *spos, *epos;
407 FILE *p = NULL;
408 int exitStatus = -1;
409 int optExitStatus = 0;
410 int optNoOutput = 0;
411 int optTrimLeft = 1;
412 int optTrimRight = 1;
413 int optStatus1st = 0;
414 int optParseOut = 0;
415 int optSpaceBreak = 1;
416 int optTabBreak = 1;
417 int optCRBreak = 1;
418 int optLFBreak = 1;
419 int no_options = ((l = lol_get(args, 1)) == NULL);
420 dstring_t str;
421 /* for each string in 2nd list: check for arg */
422 for (; l != NULL; l = l->next) {
423 if (!strcmp("exit-status", l->string)) optExitStatus = 1;
424 else if (!strcmp("exit-code", l->string)) optExitStatus = 1;
425 else if (!strcmp("status-first", l->string)) optStatus1st = 1;
426 else if (!strcmp("code-first", l->string)) optStatus1st = 1;
427 else if (!strcmp("no-output", l->string)) optNoOutput = 1;
428 else if (!strcmp("no-trim", l->string)) optTrimLeft = optTrimRight = 0;
429 else if (!strcmp("no-trim-left", l->string)) optTrimLeft = 0;
430 else if (!strcmp("no-trim-right", l->string)) optTrimRight = 0;
431 else if (!strcmp("parse-output", l->string)) optParseOut = 1;
432 else if (!strcmp("no-space-break", l->string)) optSpaceBreak = 0;
433 else if (!strcmp("no-tab-break", l->string)) optTabBreak = 0;
434 else if (!strcmp("no-nl-break", l->string)) optLFBreak = 0;
435 else if (!strcmp("no-lf-break", l->string)) optLFBreak = 0;
436 else if (!strcmp("no-cr-break", l->string)) optCRBreak = 0;
437 else if (!strcmp("dummy", l->string) || !strcmp("xyzzy", l->string)) {}
438 else {
439 printf("jam: invalid option for Command built-in: '%s'\n", l->string);
440 exit(EXITBAD); /* yeech */
443 if (no_options) optNoOutput = 1;
444 /* build shell command */
445 dstr_init(&str);
446 /* for each arg */
447 for (l = lol_get(args, 0); l; l = l->next) {
448 if (dstr_len(&str)) dstr_push_char(&str, ' ');
449 dstr_push_cstr(&str, l->string);
451 /* no shell command? */
452 if (dstr_len(&str) < 1) { dstr_done(&str); return L0; }
453 fflush(NULL); /* flush ALL output streams */
454 p = popen(dstr_cstr(&str), "r");
455 if (!p) { dstr_done(&str); return L0; }
456 dstr_clear(&str);
457 while ((ret = fread(buffer, sizeof(char), sizeof(buffer)-1, p)) > 0) {
458 if (!optNoOutput) {
459 buffer[ret] = 0;
460 dstr_push_cstr(&str, buffer);
463 exitStatus = pclose(p);
464 if (no_options) {
465 if (exitStatus) {
466 snprintf(buf1, sizeof(buf1), "%d", exitStatus);
467 res = list_new(L0, buf1, 0);
468 } else {
469 res = L0;
471 } else {
472 if (optExitStatus && optStatus1st) {
473 snprintf(buf1, sizeof(buf1), "%d", exitStatus);
474 res = list_new(res, buf1, 0);
476 /* trim output if necessary */
477 if (!optNoOutput) {
478 if (!optParseOut) {
479 /* don't parse */
480 if (optTrimRight) {
481 // trim trailing blanks
482 int sl = dstr_len(&str);
483 spos = dstr_cstr(&str);
484 while (sl > 0 && (unsigned char)spos[sl-1] <= ' ') --sl;
485 dstr_chop(&str, sl);
487 spos = dstr_cstr(&str);
488 if (optTrimLeft) {
489 // trim leading blanks
490 while (*spos && *((unsigned char *)spos) <= ' ') ++spos;
492 res = list_new(res, spos, 0);
493 } else {
494 dstring_t tmp;
495 /* parse output */
496 ret = 0; /* was anything added? list must have at least one element */
497 spos = dstr_cstr(&str);
498 dstr_init(&tmp);
499 while (*spos) {
500 /* skip delimiters */
501 while (*spos) {
502 unsigned char ch = (unsigned char)(*spos);
503 if (ch == ' ') { if (!optSpaceBreak) break; }
504 else if (ch == '\t') { if (!optTabBreak) break; }
505 else if (ch == '\r') { if (!optCRBreak) break; }
506 else if (ch == '\n') { if (!optLFBreak) break; }
507 else if (ch > ' ') break;
508 ++spos;
510 if (!*spos) break;
511 epos = spos+1;
512 while (*epos) {
513 int ch = *epos;
514 if (ch == ' ') { if (optSpaceBreak) break; }
515 else if (ch == '\t') { if (optTabBreak) break; }
516 else if (ch == '\r') { if (optCRBreak) break; }
517 else if (ch == '\n') { if (optLFBreak) break; }
518 else if ((unsigned char)ch <= ' ') break;
519 ++epos;
521 dstr_clear(&tmp);
522 dstr_push_memrange(&tmp, spos, epos);
523 res = list_new(res, dstr_cstr(&tmp), 0);
524 ret = 1;
525 spos = epos;
527 dstr_done(&tmp);
528 if (!ret) { buf1[0] = '\0'; res = list_new(res, buf1, 0); }
531 /* command exit result next */
532 if (optExitStatus && !optStatus1st) {
533 snprintf(buf1, sizeof(buf1), "%d", exitStatus);
534 res = list_new(res, buf1, 0);
537 dstr_done(&str);
538 return res;
542 /* ExprI1 op0 math op1 */
543 static LIST *builtin_expri1 (PARSE *parse, LOL *args, int *jmp) {
544 char buffer[100];
545 int op0, op1, res, comp = 0;
546 LIST *el = lol_get(args, 0);
547 if (!el || !el->next) return L0;
548 if (el->string[0] == '#') {
549 // string length
550 snprintf(buffer, sizeof(buffer), "%u", (unsigned int)(strlen(el->next->string)));
551 return list_new(L0, buffer, 0);
553 if (!el->next->next) return L0;
554 op0 = atoi(el->string);
555 op1 = atoi(el->next->next->string);
556 res = 0;
557 switch (el->next->string[0]) {
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 '%': res = op0%op1; break;
563 case '<':
564 comp = 1;
565 if (el->next->string[1] == '=') res = (op0 <= op1); else res = (op0 < op1);
566 break;
567 case '=': comp = 1; res = (op0 == op1); break;
568 case '!': comp = 1; res = (op0 != op1); break;
569 case '>':
570 comp = 1;
571 if (el->next->string[1] == '=') res = (op0 >= op1); else res = (op0 > op1);
572 break;
573 default:
574 printf("jam: rule ExprI1: unknown operator: '%s'\n", el->next->string);
575 exit(EXITBAD);
577 if (comp) return (res ? list_new(L0, "tan", 0) : L0);
578 snprintf(buffer, sizeof(buffer), "%d", res);
579 return list_new(L0, buffer, 0);
583 /* based on the code from ftjam by David Turner */
584 static LIST *builtin_split (PARSE *parse, LOL *args, int *jmp) {
585 LIST *input = lol_get(args, 0);
586 LIST *tokens = lol_get(args, 1);
587 LIST *res = L0;
588 char token[256];
589 dstring_t str;
590 int explode = 0;
591 dstr_init(&str);
592 /* build token array */
593 if (tokens == NULL) {
594 memset(token, 1, sizeof(token));
595 explode = 1;
596 } else {
597 memset(token, 0, sizeof(token));
598 for (; tokens; tokens = tokens->next) {
599 const char *s = tokens->string;
600 for (; *s; ++s) token[(unsigned char)*s] = 1;
602 if (memchr(token, 1, sizeof(token)) == NULL) {
603 memset(token, 1, sizeof(token));
604 explode = 1;
607 token[0] = 0;
608 /* now parse the input and split it */
609 for (; input; input = input->next) {
610 const char *ptr = input->string;
611 const char *lastPtr = input->string;
612 while (*ptr) {
613 if (token[(unsigned char)*ptr]) {
614 size_t count = ptr-lastPtr+explode;
615 if (count > 0) {
616 dstr_clear(&str);
617 dstr_push_memrange(&str, lastPtr, ptr+explode);
618 res = list_new(res, dstr_cstr(&str), 0);
620 lastPtr = ptr+1;
622 ++ptr;
624 if (ptr > lastPtr) res = list_new(res, lastPtr, 0);
626 dstr_done(&str);
627 return res;
632 * builtin_dependslist()
634 * The DependsList builtin rule returns list of dependencies for
635 * a given target.
637 static LIST *builtin_dependslist (PARSE *parse, LOL *args, int *jmp) {
638 LIST *res = L0;
639 LIST *parents;
640 for (parents = lol_get(args, 0); parents; parents = parents->next) {
641 TARGET *t = bindtarget(parents->string);
642 TARGETS *child;
643 for (child = t->depends; child; child = child->next) res = list_new(res, child->target->name, 1);
645 return res;
650 * NormalizePath path [: pwd]
652 * it will add 'pwd' if path is not absolute and will try to resolve some '.' and '..' (only leading '..' though).
653 * it can be used in SubDir replacement to automate dir building
654 * if there is no $(2), use current directory as 'pwd'
656 static LIST *builtin_normpath (PARSE *parse, LOL *args, int *jmp) {
657 LIST *el = lol_get(args, 0), *pl = lol_get(args, 1);
658 char *buf;
659 int bsz;
660 if (!el || !el->string) return L0;
661 bsz = strlen(el->string)*2+1024;
662 buf = malloc(bsz);
663 if (buf == NULL) return L0;
664 if (!normalize_path(el->string, buf, bsz, (pl != NULL ? pl->string : NULL))) { free(buf); return L0; }
665 el = list_new(NULL, buf, 0);
666 free(buf);
667 return el;
672 * ListLength list
674 static LIST *builtin_listlength (PARSE *parse, LOL *args, int *jmp) {
675 char buffer[100];
676 LIST *el = lol_get(args, 0);
677 if (!el) return list_new(L0, "0", 0);
678 snprintf(buffer, sizeof(buffer), "%d", list_length(el));
679 return list_new(L0, buffer, 0);
684 * HaveRule and HaveActions
686 typedef struct {
687 LIST *el;
688 int wantAction;
689 int casesens;
690 } HRNGCI;
693 typedef struct {
694 const char *str;
695 int wantAction;
696 int casesens;
697 } HRNormalData;
700 static int hr_normal (const void *hdata, void *udata) {
701 const RULE *r = (const RULE *)hdata;
702 const HRNormalData *d = (const HRNormalData *)udata;
703 if (strcasecmp(r->name, d->str) == 0) {
704 if (d->wantAction && r->actions) return 1; // got it
705 if (!d->wantAction && r->procedure) return 1; // got it
707 return 0;
711 static int hr_glob (const void *hdata, void *udata) {
712 const RULE *r = (const RULE *)hdata;
713 const HRNormalData *d = (const HRNormalData *)udata;
714 if (matchglobex(d->str, r->name, d->casesens) == 0) {
715 //fprintf(stderr, ":[%s]\n", r->name);
716 if (d->wantAction && r->actions) return 1; // got it
717 if (!d->wantAction && r->procedure) return 1; // got it
719 return 0;
723 typedef struct {
724 regexp_t *re;
725 int reflags;
726 int wantAction;
727 } HRREData;
730 static int hr_regexp (const void *hdata, void *udata) {
731 const RULE *r = (const RULE *)hdata;
732 HRREData *d = (HRREData *)udata;
733 if (regexp_execute(d->re, r->name, NULL, 0) > 0) {
734 //fprintf(stderr, ":[%s]\n", r->name);
735 if (d->wantAction && r->actions) return 1; // got it
736 if (!d->wantAction && r->procedure) return 1; // got it
738 return 0;
742 static LIST *builtin_haveruleactions (PARSE *parse, LOL *args, int *jmp) {
743 LIST *el = lol_get(args, 0), *l;
744 int wantAction = parse->num;
745 int casesens = 1;
746 int cmptype = 0; // <0:glob; >0:regexp
747 if (!el) return L0;
748 for (l = lol_get(args, 1); l != NULL; l = l->next) {
749 if (!strcmp("case-sensitive", l->string)) casesens = 1;
750 else if (!strcmp("case-insensitive", l->string)) casesens = 0;
751 else if (!strcmp("ignore-case", l->string)) casesens = 0;
752 else if (!strcmp("glob", l->string)) cmptype = -1;
753 else if (!strcmp("regexp", l->string)) cmptype = 1;
754 else if (!strcmp("plain", l->string)) cmptype = 0;
755 else {
756 printf("jam: invalid option for Have%s built-in: '%s'\n", (wantAction ? "Actions" : "Rule"), l->string);
757 exit(EXITBAD); /* yeech */
760 if (casesens == 1 && cmptype == 0) {
761 // standard mode
762 for (; el; el = el->next) {
763 RULE *r = findrule(el->string);
764 if (!r) return L0;
765 if (wantAction && !r->actions) return L0;
766 if (!wantAction && !r->procedure) return L0;
768 } else if (cmptype < 0) {
769 // glob
770 HRNormalData nfo;
771 nfo.wantAction = wantAction;
772 nfo.casesens = casesens;
773 for (; el; el = el->next) {
774 nfo.str = el->string;
775 if (!iteraterules(hr_glob, &nfo)) return L0;
777 } else if (cmptype > 0) {
778 // regexp
779 HRREData nfo;
780 nfo.wantAction = wantAction;
781 for (; el; el = el->next) {
782 int err;
783 nfo.re = regexp_compile(el->string, (casesens ? 0 : RE9_FLAG_CASEINSENS));
784 /*printf("FATAL: invalid regexp in Have%s: %s\n", (wantAction ? "Actions" : "Rule"), errmsg);*/
785 err = iteraterules(hr_regexp, &nfo);
786 regexp_free(nfo.re);
787 if (!err) return L0;
789 } else {
790 // normal, case-insensitive
791 HRNormalData nfo;
792 nfo.wantAction = wantAction;
793 for (; el; el = el->next) {
794 nfo.str = el->string;
795 if (!iteraterules(hr_normal, &nfo)) return L0;
798 return list_new(L0, "1", 0);
802 /* generate random name:
803 * [ RandName ] -- /tmp/XXX
804 * [ RandName "abc/" ] -- abc/XXX
805 * [ RandName "" ] -- XXX
807 static LIST *builtin_randname (PARSE *parse, LOL *args, int *jmp) {
808 static BJRandCtx rctx;
809 static int initialized = 0;
810 static const char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyz";
811 LIST *el;
812 char buffer[9], *s;
813 const char *path;
814 #ifdef OS_NT
815 static char tp[8192]
816 #endif
817 if (!initialized) {
818 initialized = 1;
819 bjprngInit(&rctx, bjprngGenRandSeed());
820 #ifdef OS_NT
821 GetTempPath(sizeof(tp), tp);
822 #endif
824 for (int f = 0; f < 8; ++f) buffer[f] = alphabet[bjprngRand(&rctx)%strlen(alphabet)];
825 buffer[8] = 0;
826 el = lol_get(args, 0);
827 path = (el != NULL && el->string != NULL ? el->string :
828 #ifdef OS_NT
830 #else
831 "/tmp/"
832 #endif
834 s = alloca(strlen(path)+strlen(buffer)+2);
835 sprintf(s, "%s%s", path, buffer);
836 return list_new(L0, s, 0);
840 /* write list to file:
841 * ListFileWrite filename : list [: terminator] [: append]
842 * default terminator is '\n'
843 * return success flag
845 static LIST *builtin_listwrite (PARSE *parse, LOL *args, int *jmp) {
846 LIST *el = lol_get(args, 0);
847 if (el != NULL && el->string != NULL && el->string[0]) {
848 LIST *l = lol_get(args, 3);
849 FILE *fo = fopen(el->string, (l != NULL && l->string[0] ? "a" : "w"));
850 if (fo != NULL) {
851 const char *term;
852 l = lol_get(args, 2);
853 term = (l != NULL ? l->string : "\n");
854 for (l = lol_get(args, 1); l != NULL; l = l->next) {
855 if (fprintf(fo, "%s%s", l->string, term) < 0) {
856 fclose(fo);
857 unlink(el->string);
858 return L0;
861 fclose(fo);
862 return list_new(L0, "1", 0);
865 return L0;
869 /* remove duplicates from list */
870 static LIST *builtin_listremdups (PARSE *parse, LOL *args, int *jmp) {
871 LIST *el = lol_get(args, 0);
872 if (el != NULL) {
873 LIST *l, *res = list_new(L0, el->string, 1);
874 for (l = el->next; l != NULL; l = l->next) {
875 int found = 0;
876 for (LIST *t = el; t != l && !found; t = t->next) if (strcmp(t->string, l->string) == 0) found = 1;
877 if (!found) res = list_new(res, l->string, 1);
879 return res;
881 return el; /* no need to change anything */
886 * compile_builtin() - define builtin rules
889 #define P0 ((PARSE *)0)
890 #define C0 ((char *)0)
893 /* ":" -- previous name in upper case; "." -- previous name in lower case */
894 static inline void bind_builtin (PARSE *pp, const char *name) {
895 bindrule(name)->procedure = pp;
899 static inline void bind_builtin2 (PARSE *pp, const char *name, const char *name1) {
900 bindrule(name)->procedure = pp;
901 bindrule(name1)->procedure = pp;
905 void load_builtins (void) {
906 bind_builtin(parse_make(builtin_depends, P0, P0, P0, C0, C0, 0),
907 "Depends");
908 bind_builtin(parse_make(builtin_depends, P0, P0, P0, C0, C0, 1),
909 "Includes");
910 bind_builtin(parse_make(builtin_dependslist, P0, P0, P0, C0, C0, 0),
911 "DependsList");
913 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_TOUCHED),
914 "Always");
915 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_LEAVES),
916 "Leaves");
917 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_NOCARE),
918 "NoCare");
919 bind_builtin2(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_NOTFILE),
920 "NotFile", "NoTime");
921 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_NOUPDATE),
922 "NoUpdate");
923 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_TEMP),
924 "Temporary");
925 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, T_FLAG_FORCECARE),
926 "ForceCare");
927 bind_builtin(parse_make(builtin_flags, P0, P0, P0, C0, C0, 666),
928 "ForceFile");
930 bind_builtin(parse_make(builtin_echo, P0, P0, P0, C0, C0, 0),
931 "Echo");
933 bind_builtin(parse_make(builtin_exit, P0, P0, P0, C0, C0, 0),
934 "Exit");
936 bind_builtin(parse_make(builtin_glob, P0, P0, P0, C0, C0, 0),
937 "Glob");
938 bind_builtin(parse_make(builtin_match, P0, P0, P0, C0, C0, 0),
939 "Match");
941 bind_builtin(parse_make(builtin_hdrmacro, P0, P0, P0, C0, C0, 0),
942 "HdrMacro");
944 bind_builtin(parse_make(builtin_pwd, P0, P0, P0, C0, C0, 0),
945 "Pwd");
947 bind_builtin(parse_make(builtin_sort, P0, P0, P0, C0, C0, 0),
948 "Sort");
950 bind_builtin(parse_make(builtin_command, P0, P0, P0, C0, C0, 0),
951 "Command");
953 bind_builtin(parse_make(builtin_expri1, P0, P0, P0, C0, C0, 0),
954 "ExprI1");
956 bind_builtin(parse_make(builtin_split, P0, P0, P0, C0, C0, 0),
957 "Split");
959 bind_builtin(parse_make(builtin_normpath, P0, P0, P0, C0, C0, 0),
960 "NormalizePath");
962 bind_builtin(parse_make(builtin_listlength, P0, P0, P0, C0, C0, 0),
963 "ListLength");
964 bind_builtin(parse_make(builtin_listwrite, P0, P0, P0, C0, C0, 0),
965 "ListWrite");
966 bind_builtin(parse_make(builtin_listremdups, P0, P0, P0, C0, C0, 0),
967 "ListRemoveDuplicates");
969 bind_builtin(parse_make(builtin_haveruleactions, P0, P0, P0, C0, C0, 0),
970 "HaveRule");
971 bind_builtin(parse_make(builtin_haveruleactions, P0, P0, P0, C0, C0, 1),
972 "HaveActions");
974 bind_builtin(parse_make(builtin_randname, P0, P0, P0, C0, C0, 0),
975 "RandName");