2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * builtins.c - builtin jam rules
12 * load_builtin() - define builtin rules
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
52 #include "matchglob.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. */
71 if (!t
->includes
) t
->includes
= copytarget(t
);
74 t
->depends
= targetlist(t
->depends
, sources
);
88 static void parse_echo_flags (echo_flags_t
*flg
, const LIST
*l
) {
91 flg
->flags
= LPFLAG_NO_TRSPACE
;
93 for (; l
; l
= list_next(l
)) {
94 const char *s
= l
->string
;
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
) {
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
);
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);
134 parse_echo_flags(&ef
, lol_get(args
, 1));
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 */
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;
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
);
178 * builtin_globbing() - GLOB rule
184 int cmptype
; // <0:glob; 0: plain; >0:# of regexps
191 static void builtin_glob_back (void *closure
, const char *file
, int status
, time_t time
) {
192 struct globbing
*globbing
= (struct globbing
*)closure
;
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
);
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;
209 fprintf(stderr, "buf: [%s]\n", buf);
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;
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);
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);
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);
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);
250 struct globbing globbing
;
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;
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
));
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
]);
289 return globbing
.results
;
294 * builtin_match() - MATCH rule, regexp matching
296 static LIST
*builtin_match (PARSE
*parse
, LOL
*args
, int *jmp
) {
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;
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
) {
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) {
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));
329 for (int i
= 1; i
< re9_nsub(re
->re
); ++i
) {
330 int l
= mt
[i
].ep
-mt
[i
].sp
;
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);
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);
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);
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
);
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");
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
);
399 /* backported from boost-jam; greatly improved */
400 /* Command shcmd [[ : options ]] */
401 static LIST
*builtin_command (PARSE
*parse
, LOL
*args
, int *jmp
) {
405 char buffer
[1024], buf1
[32], *spos
, *epos
;
408 int optExitStatus
= 0;
411 int optTrimRight
= 1;
412 int optStatus1st
= 0;
414 int optSpaceBreak
= 1;
418 int no_options
= ((l
= lol_get(args
, 1)) == NULL
);
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
)) {}
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 */
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
; }
456 while ((ret
= fread(buffer
, sizeof(char), sizeof(buffer
)-1, p
)) > 0) {
459 dstr_push_cstr(&str
, buffer
);
462 exitStatus
= pclose(p
);
465 snprintf(buf1
, sizeof(buf1
), "%d", exitStatus
);
466 res
= list_new(L0
, buf1
, 0);
471 if (optExitStatus
&& optStatus1st
) {
472 snprintf(buf1
, sizeof(buf1
), "%d", exitStatus
);
473 res
= list_new(res
, buf1
, 0);
475 /* trim output if necessary */
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
;
486 spos
= dstr_cstr(&str
);
488 // trim leading blanks
489 while (*spos
&& *((unsigned char *)spos
) <= ' ') ++spos
;
491 res
= list_new(res
, spos
, 0);
495 ret
= 0; /* was anything added? list must have at least one element */
496 spos
= dstr_cstr(&str
);
499 /* skip delimiters */
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;
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;
521 dstr_push_memrange(&tmp
, spos
, epos
);
522 res
= list_new(res
, dstr_cstr(&tmp
), 0);
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);
541 /* ExprI1 op0 math op1 */
542 static LIST
*builtin_expri1 (PARSE
*parse
, LOL
*args
, int *jmp
) {
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] == '#') {
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
);
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;
564 if (el
->next
->string
[1] == '=') res
= (op0
<= op1
); else res
= (op0
< op1
);
566 case '=': comp
= 1; res
= (op0
== op1
); break;
567 case '!': comp
= 1; res
= (op0
!= op1
); break;
570 if (el
->next
->string
[1] == '=') res
= (op0
>= op1
); else res
= (op0
> op1
);
573 printf("jam: rule ExprI1: unknown operator: '%s'\n", el
->next
->string
);
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);
591 /* build token array */
592 if (tokens
== NULL
) {
593 memset(token
, 1, sizeof(token
));
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
));
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
;
612 if (token
[(unsigned char)*ptr
]) {
613 size_t count
= ptr
-lastPtr
+explode
;
616 dstr_push_memrange(&str
, lastPtr
, ptr
+explode
);
617 res
= list_new(res
, dstr_cstr(&str
), 0);
623 if (ptr
> lastPtr
) res
= list_new(res
, lastPtr
, 0);
631 * builtin_dependslist()
633 * The DependsList builtin rule returns list of dependencies for
636 static LIST
*builtin_dependslist (PARSE
*parse
, LOL
*args
, int *jmp
) {
639 for (parents
= lol_get(args
, 0); parents
; parents
= parents
->next
) {
640 TARGET
*t
= bindtarget(parents
->string
);
642 for (child
= t
->depends
; child
; child
= child
->next
) res
= list_new(res
, child
->target
->name
, 1);
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
654 static LIST
*builtin_normpath (PARSE
*parse
, LOL
*args
, int *jmp
) {
655 LIST
*el
= lol_get(args
, 0);
658 if (!el
|| !el
->string
) return L0
;
659 bsz
= strlen(el
->string
)*2+1024;
661 if (buf
== NULL
) return L0
;
662 if (!normalize_path(el
->string
, buf
, bsz
)) { free(buf
); return L0
; }
663 el
= list_new(NULL
, buf
, 0);
672 static LIST
*builtin_listlength (PARSE
*parse
, LOL
*args
, int *jmp
) {
674 LIST
*el
= lol_get(args
, 0);
675 if (!el
) return list_new(L0
, "0", 0);
676 snprintf(buffer
, sizeof(buffer
), "%d", list_length(el
));
677 return list_new(L0
, buffer
, 0);
682 * HaveRule and HaveActions
698 static int hr_normal (const void *hdata
, void *udata
) {
699 const RULE
*r
= (const RULE
*)hdata
;
700 const HRNormalData
*d
= (const HRNormalData
*)udata
;
701 if (strcasecmp(r
->name
, d
->str
) == 0) {
702 if (d
->wantAction
&& r
->actions
) return 1; // got it
703 if (!d
->wantAction
&& r
->procedure
) return 1; // got it
709 static int hr_glob (const void *hdata
, void *udata
) {
710 const RULE
*r
= (const RULE
*)hdata
;
711 const HRNormalData
*d
= (const HRNormalData
*)udata
;
712 if (matchglobex(d
->str
, r
->name
, d
->casesens
) == 0) {
713 //fprintf(stderr, ":[%s]\n", r->name);
714 if (d
->wantAction
&& r
->actions
) return 1; // got it
715 if (!d
->wantAction
&& r
->procedure
) return 1; // got it
728 static int hr_regexp (const void *hdata
, void *udata
) {
729 const RULE
*r
= (const RULE
*)hdata
;
730 HRREData
*d
= (HRREData
*)udata
;
731 if (regexp_execute(d
->re
, r
->name
, NULL
, 0) > 0) {
732 //fprintf(stderr, ":[%s]\n", r->name);
733 if (d
->wantAction
&& r
->actions
) return 1; // got it
734 if (!d
->wantAction
&& r
->procedure
) return 1; // got it
740 static LIST
*builtin_haveruleactions (PARSE
*parse
, LOL
*args
, int *jmp
) {
741 LIST
*el
= lol_get(args
, 0), *l
;
742 int wantAction
= parse
->num
;
744 int cmptype
= 0; // <0:glob; >0:regexp
746 for (l
= lol_get(args
, 1); l
!= NULL
; l
= l
->next
) {
747 if (!strcmp("case-sensitive", l
->string
)) casesens
= 1;
748 else if (!strcmp("case-insensitive", l
->string
)) casesens
= 0;
749 else if (!strcmp("ignore-case", l
->string
)) casesens
= 0;
750 else if (!strcmp("glob", l
->string
)) cmptype
= -1;
751 else if (!strcmp("regexp", l
->string
)) cmptype
= 1;
752 else if (!strcmp("plain", l
->string
)) cmptype
= 0;
754 printf("jam: invalid option for Have%s built-in: '%s'\n", (wantAction
? "Actions" : "Rule"), l
->string
);
755 exit(EXITBAD
); /* yeech */
758 if (casesens
== 1 && cmptype
== 0) {
760 for (; el
; el
= el
->next
) {
761 RULE
*r
= findrule(el
->string
);
763 if (wantAction
&& !r
->actions
) return L0
;
764 if (!wantAction
&& !r
->procedure
) return L0
;
766 } else if (cmptype
< 0) {
769 nfo
.wantAction
= wantAction
;
770 nfo
.casesens
= casesens
;
771 for (; el
; el
= el
->next
) {
772 nfo
.str
= el
->string
;
773 if (!iteraterules(hr_glob
, &nfo
)) return L0
;
775 } else if (cmptype
> 0) {
778 nfo
.wantAction
= wantAction
;
779 for (; el
; el
= el
->next
) {
781 nfo
.re
= regexp_compile(el
->string
, (casesens
? 0 : RE9_FLAG_CASEINSENS
));
782 /*printf("FATAL: invalid regexp in Have%s: %s\n", (wantAction ? "Actions" : "Rule"), errmsg);*/
783 err
= iteraterules(hr_regexp
, &nfo
);
788 // normal, case-insensitive
790 nfo
.wantAction
= wantAction
;
791 for (; el
; el
= el
->next
) {
792 nfo
.str
= el
->string
;
793 if (!iteraterules(hr_normal
, &nfo
)) return L0
;
796 return list_new(L0
, "1", 0);
800 /* generate random name:
801 * [ RandName ] -- /tmp/XXX
802 * [ RandName "abc/" ] -- abc/XXX
803 * [ RandName "" ] -- XXX
805 static LIST
*builtin_randname (PARSE
*parse
, LOL
*args
, int *jmp
) {
806 static BJRandCtx rctx
;
807 static int initialized
= 0;
808 static const char alphabet
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
817 bjprngInit(&rctx
, bjprngGenRandSeed());
819 GetTempPath(sizeof(tp
), tp
);
822 for (int f
= 0; f
< 8; ++f
) buffer
[f
] = alphabet
[bjprngRand(&rctx
)%strlen(alphabet
)];
824 el
= lol_get(args
, 0);
825 path
= (el
!= NULL
&& el
->string
!= NULL
? el
->string
:
832 s
= alloca(strlen(path
)+strlen(buffer
)+2);
833 sprintf(s
, "%s%s", path
, buffer
);
834 return list_new(L0
, s
, 0);
838 /* write list to file:
839 * ListFileWrite filename : list [: terminator] [: append]
840 * default terminator is '\n'
841 * return success flag
843 static LIST
*builtin_listwrite (PARSE
*parse
, LOL
*args
, int *jmp
) {
844 LIST
*el
= lol_get(args
, 0);
845 if (el
!= NULL
&& el
->string
!= NULL
&& el
->string
[0]) {
846 LIST
*l
= lol_get(args
, 3);
847 FILE *fo
= fopen(el
->string
, (l
!= NULL
&& l
->string
[0] ? "a" : "w"));
850 l
= lol_get(args
, 2);
851 term
= (l
!= NULL
? l
->string
: "\n");
852 for (l
= lol_get(args
, 1); l
!= NULL
; l
= l
->next
) {
853 if (fprintf(fo
, "%s%s", l
->string
, term
) < 0) {
860 return list_new(L0
, "1", 0);
867 /* remove duplicates from list */
868 static LIST
*builtin_listremdups (PARSE
*parse
, LOL
*args
, int *jmp
) {
869 LIST
*el
= lol_get(args
, 0);
871 LIST
*l
, *res
= list_new(L0
, el
->string
, 1);
872 for (l
= el
->next
; l
!= NULL
; l
= l
->next
) {
874 for (LIST
*t
= el
; t
!= l
&& !found
; t
= t
->next
) if (strcmp(t
->string
, l
->string
) == 0) found
= 1;
875 if (!found
) res
= list_new(res
, l
->string
, 1);
879 return el
; /* no need to change anything */
884 * compile_builtin() - define builtin rules
887 #define P0 ((PARSE *)0)
888 #define C0 ((char *)0)
891 /* ":" -- previous name in upper case; "." -- previous name in lower case */
892 static inline void bind_builtin (PARSE
*pp
, const char *name
) {
893 bindrule(name
)->procedure
= pp
;
897 static inline void bind_builtin2 (PARSE
*pp
, const char *name
, const char *name1
) {
898 bindrule(name
)->procedure
= pp
;
899 bindrule(name1
)->procedure
= pp
;
903 void load_builtins (void) {
904 bind_builtin(parse_make(builtin_depends
, P0
, P0
, P0
, C0
, C0
, 0),
906 bind_builtin(parse_make(builtin_depends
, P0
, P0
, P0
, C0
, C0
, 1),
908 bind_builtin(parse_make(builtin_dependslist
, P0
, P0
, P0
, C0
, C0
, 0),
911 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_TOUCHED
),
913 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_LEAVES
),
915 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_NOCARE
),
917 bind_builtin2(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_NOTFILE
),
918 "NotFile", "NoTime");
919 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_NOUPDATE
),
921 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_TEMP
),
923 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_FORCECARE
),
925 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, 666),
928 bind_builtin(parse_make(builtin_echo
, P0
, P0
, P0
, C0
, C0
, 0),
931 bind_builtin(parse_make(builtin_exit
, P0
, P0
, P0
, C0
, C0
, 0),
934 bind_builtin(parse_make(builtin_glob
, P0
, P0
, P0
, C0
, C0
, 0),
936 bind_builtin(parse_make(builtin_match
, P0
, P0
, P0
, C0
, C0
, 0),
939 bind_builtin(parse_make(builtin_hdrmacro
, P0
, P0
, P0
, C0
, C0
, 0),
942 bind_builtin(parse_make(builtin_pwd
, P0
, P0
, P0
, C0
, C0
, 0),
945 bind_builtin(parse_make(builtin_sort
, P0
, P0
, P0
, C0
, C0
, 0),
948 bind_builtin(parse_make(builtin_command
, P0
, P0
, P0
, C0
, C0
, 0),
951 bind_builtin(parse_make(builtin_expri1
, P0
, P0
, P0
, C0
, C0
, 0),
954 bind_builtin(parse_make(builtin_split
, P0
, P0
, P0
, C0
, C0
, 0),
957 bind_builtin(parse_make(builtin_normpath
, P0
, P0
, P0
, C0
, C0
, 0),
960 bind_builtin(parse_make(builtin_listlength
, P0
, P0
, P0
, C0
, C0
, 0),
962 bind_builtin(parse_make(builtin_listwrite
, P0
, P0
, P0
, C0
, C0
, 0),
964 bind_builtin(parse_make(builtin_listremdups
, P0
, P0
, P0
, C0
, C0
, 0),
965 "ListRemoveDuplicates");
967 bind_builtin(parse_make(builtin_haveruleactions
, P0
, P0
, P0
, C0
, C0
, 0),
969 bind_builtin(parse_make(builtin_haveruleactions
, P0
, P0
, P0
, C0
, C0
, 1),
972 bind_builtin(parse_make(builtin_randname
, P0
, P0
, P0
, C0
, C0
, 0),