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
;
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);
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);
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);
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
);
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");
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
);
400 /* backported from boost-jam; greatly improved */
401 /* Command shcmd [[ : options ]] */
402 static LIST
*builtin_command (PARSE
*parse
, LOL
*args
, int *jmp
) {
406 char buffer
[1024], buf1
[32], *spos
, *epos
;
409 int optExitStatus
= 0;
412 int optTrimRight
= 1;
413 int optStatus1st
= 0;
415 int optSpaceBreak
= 1;
419 int no_options
= ((l
= lol_get(args
, 1)) == NULL
);
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
)) {}
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 */
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
; }
457 while ((ret
= fread(buffer
, sizeof(char), sizeof(buffer
)-1, p
)) > 0) {
460 dstr_push_cstr(&str
, buffer
);
463 exitStatus
= pclose(p
);
466 snprintf(buf1
, sizeof(buf1
), "%d", exitStatus
);
467 res
= list_new(L0
, buf1
, 0);
472 if (optExitStatus
&& optStatus1st
) {
473 snprintf(buf1
, sizeof(buf1
), "%d", exitStatus
);
474 res
= list_new(res
, buf1
, 0);
476 /* trim output if necessary */
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
;
487 spos
= dstr_cstr(&str
);
489 // trim leading blanks
490 while (*spos
&& *((unsigned char *)spos
) <= ' ') ++spos
;
492 res
= list_new(res
, spos
, 0);
496 ret
= 0; /* was anything added? list must have at least one element */
497 spos
= dstr_cstr(&str
);
500 /* skip delimiters */
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;
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;
522 dstr_push_memrange(&tmp
, spos
, epos
);
523 res
= list_new(res
, dstr_cstr(&tmp
), 0);
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);
542 /* ExprI1 op0 math op1 */
543 static LIST
*builtin_expri1 (PARSE
*parse
, LOL
*args
, int *jmp
) {
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] == '#') {
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
);
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;
565 if (el
->next
->string
[1] == '=') res
= (op0
<= op1
); else res
= (op0
< op1
);
567 case '=': comp
= 1; res
= (op0
== op1
); break;
568 case '!': comp
= 1; res
= (op0
!= op1
); break;
571 if (el
->next
->string
[1] == '=') res
= (op0
>= op1
); else res
= (op0
> op1
);
574 printf("jam: rule ExprI1: unknown operator: '%s'\n", el
->next
->string
);
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);
592 /* build token array */
593 if (tokens
== NULL
) {
594 memset(token
, 1, sizeof(token
));
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
));
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
;
613 if (token
[(unsigned char)*ptr
]) {
614 size_t count
= ptr
-lastPtr
+explode
;
617 dstr_push_memrange(&str
, lastPtr
, ptr
+explode
);
618 res
= list_new(res
, dstr_cstr(&str
), 0);
624 if (ptr
> lastPtr
) res
= list_new(res
, lastPtr
, 0);
632 * builtin_dependslist()
634 * The DependsList builtin rule returns list of dependencies for
637 static LIST
*builtin_dependslist (PARSE
*parse
, LOL
*args
, int *jmp
) {
640 for (parents
= lol_get(args
, 0); parents
; parents
= parents
->next
) {
641 TARGET
*t
= bindtarget(parents
->string
);
643 for (child
= t
->depends
; child
; child
= child
->next
) res
= list_new(res
, child
->target
->name
, 1);
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);
660 if (!el
|| !el
->string
) return L0
;
661 bsz
= strlen(el
->string
)*2+1024;
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);
674 static LIST
*builtin_listlength (PARSE
*parse
, LOL
*args
, int *jmp
) {
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
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
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
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
742 static LIST
*builtin_haveruleactions (PARSE
*parse
, LOL
*args
, int *jmp
) {
743 LIST
*el
= lol_get(args
, 0), *l
;
744 int wantAction
= parse
->num
;
746 int cmptype
= 0; // <0:glob; >0:regexp
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;
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) {
762 for (; el
; el
= el
->next
) {
763 RULE
*r
= findrule(el
->string
);
765 if (wantAction
&& !r
->actions
) return L0
;
766 if (!wantAction
&& !r
->procedure
) return L0
;
768 } else if (cmptype
< 0) {
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) {
780 nfo
.wantAction
= wantAction
;
781 for (; el
; el
= el
->next
) {
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
);
790 // normal, case-insensitive
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";
819 bjprngInit(&rctx
, bjprngGenRandSeed());
821 GetTempPath(sizeof(tp
), tp
);
824 for (int f
= 0; f
< 8; ++f
) buffer
[f
] = alphabet
[bjprngRand(&rctx
)%strlen(alphabet
)];
826 el
= lol_get(args
, 0);
827 path
= (el
!= NULL
&& el
->string
!= NULL
? el
->string
:
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"));
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) {
862 return list_new(L0
, "1", 0);
869 /* remove duplicates from list */
870 static LIST
*builtin_listremdups (PARSE
*parse
, LOL
*args
, int *jmp
) {
871 LIST
*el
= lol_get(args
, 0);
873 LIST
*l
, *res
= list_new(L0
, el
->string
, 1);
874 for (l
= el
->next
; l
!= NULL
; l
= l
->next
) {
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);
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),
908 bind_builtin(parse_make(builtin_depends
, P0
, P0
, P0
, C0
, C0
, 1),
910 bind_builtin(parse_make(builtin_dependslist
, P0
, P0
, P0
, C0
, C0
, 0),
913 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_TOUCHED
),
915 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_LEAVES
),
917 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_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
),
923 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_TEMP
),
925 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_FORCECARE
),
927 bind_builtin(parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, 666),
930 bind_builtin(parse_make(builtin_echo
, P0
, P0
, P0
, C0
, C0
, 0),
933 bind_builtin(parse_make(builtin_exit
, P0
, P0
, P0
, C0
, C0
, 0),
936 bind_builtin(parse_make(builtin_glob
, P0
, P0
, P0
, C0
, C0
, 0),
938 bind_builtin(parse_make(builtin_match
, P0
, P0
, P0
, C0
, C0
, 0),
941 bind_builtin(parse_make(builtin_hdrmacro
, P0
, P0
, P0
, C0
, C0
, 0),
944 bind_builtin(parse_make(builtin_pwd
, P0
, P0
, P0
, C0
, C0
, 0),
947 bind_builtin(parse_make(builtin_sort
, P0
, P0
, P0
, C0
, C0
, 0),
950 bind_builtin(parse_make(builtin_command
, P0
, P0
, P0
, C0
, C0
, 0),
953 bind_builtin(parse_make(builtin_expri1
, P0
, P0
, P0
, C0
, C0
, 0),
956 bind_builtin(parse_make(builtin_split
, P0
, P0
, P0
, C0
, C0
, 0),
959 bind_builtin(parse_make(builtin_normpath
, P0
, P0
, P0
, C0
, C0
, 0),
962 bind_builtin(parse_make(builtin_listlength
, P0
, P0
, P0
, C0
, C0
, 0),
964 bind_builtin(parse_make(builtin_listwrite
, P0
, P0
, P0
, C0
, C0
, 0),
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),
971 bind_builtin(parse_make(builtin_haveruleactions
, P0
, P0
, P0
, C0
, C0
, 1),
974 bind_builtin(parse_make(builtin_randname
, P0
, P0
, P0
, C0
, C0
, 0),