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_echon() - ECHO-N rule
19 * builtin_oflush() - O-FLUSH rule
20 * builtin_exit() - EXIT rule
21 * builtin_flags() - NOCARE, NOTFILE, TEMPORARY rule
22 * builtin_glob() - GLOB rule
23 * builtin_match() - MATCH rule
24 * builtin_hdrmacro() - HDRMACRO rule
26 * 01/10/01 (seiwald) - split from compile.c
27 * 01/08/01 (seiwald) - new 'Glob' (file expansion) builtin
28 * 03/02/02 (seiwald) - new 'Match' (regexp match) builtin
29 * 04/03/02 (seiwald) - Glob matches only filename, not directory
30 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
31 * 10/22/02 (seiwald) - working return/break/continue statements
32 * 11/04/02 (seiwald) - const-ing for string literals
33 * 12/03/02 (seiwald) - fix odd includes support by grafting them onto depends
34 * 01/14/03 (seiwald) - fix includes fix with new internal includes TARGET
56 * compile_builtin() - define builtin rules
63 int glob (const char *s
, const char *c
);
66 void load_builtins (void) {
67 bindrule("Always")->procedure
=
68 bindrule("ALWAYS")->procedure
=
69 parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_TOUCHED
);
71 bindrule("Depends")->procedure
=
72 bindrule("DEPENDS")->procedure
=
73 parse_make(builtin_depends
, P0
, P0
, P0
, C0
, C0
, 0);
75 bindrule("echo")->procedure
=
76 bindrule("Echo")->procedure
=
77 bindrule("ECHO")->procedure
=
78 parse_make(builtin_echo
, P0
, P0
, P0
, C0
, C0
, 0);
80 bindrule("echo-n")->procedure
=
81 bindrule("Echo-n")->procedure
=
82 bindrule("ECHO-N")->procedure
=
83 parse_make(builtin_echon
, P0
, P0
, P0
, C0
, C0
, 0);
85 bindrule("o-flush")->procedure
=
86 bindrule("O-flush")->procedure
=
87 bindrule("O-Flush")->procedure
=
88 bindrule("O-FLUSH")->procedure
=
89 parse_make(builtin_oflush
, P0
, P0
, P0
, C0
, C0
, 0);
91 bindrule("exit")->procedure
=
92 bindrule("Exit")->procedure
=
93 bindrule("EXIT")->procedure
=
94 parse_make(builtin_exit
, P0
, P0
, P0
, C0
, C0
, 0);
96 bindrule("Glob")->procedure
=
97 bindrule("GLOB")->procedure
=
98 parse_make(builtin_glob
, P0
, P0
, P0
, C0
, C0
, 0);
100 bindrule("Includes")->procedure
=
101 bindrule("INCLUDES")->procedure
=
102 parse_make(builtin_depends
, P0
, P0
, P0
, C0
, C0
, 1);
104 bindrule("Leaves")->procedure
=
105 bindrule("LEAVES")->procedure
=
106 parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_LEAVES
);
108 bindrule("Match")->procedure
=
109 bindrule("MATCH")->procedure
=
110 parse_make(builtin_match
, P0
, P0
, P0
, C0
, C0
, 0);
112 bindrule("NoCare")->procedure
=
113 bindrule("NOCARE")->procedure
=
114 parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_NOCARE
);
116 bindrule("NOTIME")->procedure
=
117 bindrule("NotFile")->procedure
=
118 bindrule("NOTFILE")->procedure
=
119 parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_NOTFILE
);
121 bindrule("NoUpdate")->procedure
=
122 bindrule("NOUPDATE")->procedure
=
123 parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_NOUPDATE
);
125 bindrule("Temporary")->procedure
=
126 bindrule("TEMPORARY")->procedure
=
127 parse_make(builtin_flags
, P0
, P0
, P0
, C0
, C0
, T_FLAG_TEMP
);
129 bindrule("HdrMacro")->procedure
=
130 bindrule("HDRMACRO")->procedure
=
131 parse_make(builtin_hdrmacro
, P0
, P0
, P0
, C0
, C0
, 0);
133 bindrule("PWD")->procedure
=
134 bindrule("Pwd")->procedure
=
135 parse_make(builtin_pwd
, P0
, P0
, P0
, C0
, C0
, 0);
137 bindrule("SORT")->procedure
=
138 bindrule("Sort")->procedure
=
139 parse_make(builtin_sort
, P0
, P0
, P0
, C0
, C0
, 0);
141 bindrule("COMMAND")->procedure
=
142 bindrule("Command")->procedure
=
143 parse_make(builtin_command
, P0
, P0
, P0
, C0
, C0
, 0);
145 bindrule("ForceCare")->procedure
=
146 bindrule("FORCECARE")->procedure
=
147 parse_make(builtin_flags_forcecare
, P0
, P0
, P0
, C0
, C0
, T_FLAG_FORCECARE
);
149 bindrule("ExprI1")->procedure
=
150 bindrule("EXPRI1")->procedure
=
151 parse_make(builtin_expri1
, P0
, P0
, P0
, C0
, C0
, 0);
153 bindrule("Split")->procedure
=
154 bindrule("SPLIT")->procedure
=
155 parse_make(builtin_split
, P0
, P0
, P0
, C0
, C0
, 0);
157 bindrule("DependsList")->procedure
=
158 bindrule("DEPENDSLIST")->procedure
=
159 parse_make(builtin_dependslist
, P0
, P0
, P0
, C0
, C0
, 0);
161 bindrule("NormPath")->procedure
=
162 bindrule("NORMPATH")->procedure
=
163 parse_make(builtin_normpath
, P0
, P0
, P0
, C0
, C0
, 0);
168 * builtin_depends() - DEPENDS/INCLUDES rule
170 * The DEPENDS builtin rule appends each of the listed sources on the
171 * dependency list of each of the listed targets.
172 * It binds both the targets and sources as TARGETs.
174 LIST
*builtin_depends (PARSE
*parse
, LOL
*args
, int *jmp
) {
175 LIST
*targets
= lol_get(args
, 0);
176 LIST
*sources
= lol_get(args
, 1);
179 for (l
= targets
; l
; l
= list_next(l
)) {
180 TARGET
*t
= bindtarget(l
->string
);
181 /* If doing INCLUDES, switch to the TARGET's include */
182 /* TARGET, creating it if needed. The internal include */
183 /* TARGET shares the name of its parent. */
185 if (!t
->includes
) t
->includes
= copytarget(t
);
188 t
->depends
= targetlist(t
->depends
, sources
);
195 * builtin_echo() - ECHO rule
197 * The ECHO builtin rule echoes the targets to the user.
198 * No other actions are taken.
200 LIST
*builtin_echo (PARSE
*parse
, LOL
*args
, int *jmp
) {
201 list_print(lol_get(args
, 0));
208 * builtin_echon() - ECHO-N rule
210 * The ECHO-N builtin rule echoes the targets to the user.
211 * No other actions are taken, no newline is written.
213 LIST
*builtin_echon (PARSE
*parse
, LOL
*args
, int *jmp
) {
214 list_print(lol_get(args
, 0));
220 * builtin_oflush() - O-FLUSH rule
222 * The O-FLUSH builtin rule flushes current output stream.
225 LIST
*builtin_oflush (PARSE
*parse
, LOL
*args
, int *jmp
) {
232 * builtin_exit() - EXIT rule
234 * The EXIT builtin rule echoes the targets to the user and exits
235 * the program with a failure status.
237 LIST
*builtin_exit (PARSE
*parse
, LOL
*args
, int *jmp
) {
238 list_print(lol_get(args
, 0));
240 exit(EXITBAD
); /* yeech */
246 * builtin_flags() - NOCARE, NOTFILE, TEMPORARY rule
248 * Builtin_flags() marks the target with the appropriate flag, for use by make0().
249 * It binds each target as a TARGET.
251 LIST
*builtin_flags (PARSE
*parse
, LOL
*args
, int *jmp
) {
252 LIST
*l
= lol_get(args
, 0);
253 for (; l
; l
= list_next(l
)) bindtarget(l
->string
)->flags
|= parse
->num
;
259 * builtin_flags_forcecare() - ForceCare rule
261 LIST
*builtin_flags_forcecare (PARSE
*parse
, LOL
*args
, int *jmp
) {
262 LIST
*l
= lol_get(args
, 0);
263 for( ; l
; l
= list_next(l
)) {
264 TARGET
*t
= bindtarget(l
->string
);
265 t
->flags
|= T_FLAG_FORCECARE
;
266 t
->flags
&= ~T_FLAG_NOCARE
;
273 * builtin_globbing() - GLOB rule
281 static void builtin_glob_back (void *closure
, const char *file
, int status
, time_t time
) {
282 struct globbing
*globbing
= (struct globbing
*)closure
;
286 /* null out directory for matching */
287 /* we wish we had file_dirscan() pass up a PATHNAME */
288 path_parse(file
, &f
);
290 /* For globbing, we unconditionally ignore current and parent
291 * directory items. Since those items always exist, there's no
292 * reason why caller of GLOB would want to see them.
293 * We could also change file_dirscan, but then paths with embedded
294 * "." and ".." won't work anywhere. */
295 /* k8: will this break anything? it shouldn't... */
296 if (!strcmp(f
.f_base
.ptr
, ".") || !strcmp(f
.f_base
.ptr
, "..")) return;
297 path_build(&f
, buf
, 0);
298 for (l
= globbing
->patterns
; l
; l
= l
->next
) {
299 if (!glob(l
->string
, buf
)) {
300 globbing
->results
= list_new(globbing
->results
, file
, 0);
307 LIST
*builtin_glob (PARSE
*parse
, LOL
*args
, int *jmp
) {
308 struct globbing globbing
;
309 LIST
*l
= lol_get(args
, 0);
310 LIST
*r
= lol_get(args
, 1);
311 globbing
.results
= L0
;
312 globbing
.patterns
= r
;
313 for (; l
; l
= list_next(l
)) file_dirscan(l
->string
, builtin_glob_back
, &globbing
);
314 return globbing
.results
;
319 * builtin_match() - MATCH rule, regexp matching
321 LIST
*builtin_match (PARSE
*parse
, LOL
*args
, int *jmp
) {
324 /* for each pattern */
325 for (l
= lol_get(args
, 0); l
; l
= l
->next
) {
330 if ((err
= hsrxCompile(&re
, l
->string
, HSRX_EXTENDED
|HSRX_NEWLINE
)) != 0) {
331 static char errbuf
[512];
333 hsrxError(err
, &re
, errbuf
, sizeof(errbuf
));
335 printf("FATAL: %s\n", errbuf
);
338 mt
= malloc(sizeof(HSRxMatch
)*(re
.re_nsub
+1));
339 if (mt
== NULL
) { printf("FATAL: out of memory!\n"); exit(42); }
340 /* for each string to match against */
341 for (r
= lol_get(args
, 1); r
; r
= r
->next
) {
342 if (hsrxExec(&re
, r
->string
, re
.re_nsub
+1, mt
, 0) == HSRX_NOERROR
) {
344 /* add all parameters up to highest onto list */
345 /* must have parameters to have results! */
346 for (i
= 1; i
<= re
.re_nsub
; ++i
) {
349 int l
= mt
[i
].rm_eo
-mt
[i
].rm_so
;
350 if (l
> 0) memcpy(buf
, r
->string
+mt
[i
].rm_so
, l
);
352 res
= list_new(res
, buf
, 0);
363 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 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 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 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;
420 /* for each string in 2nd list: check for arg */
421 for (l
= lol_get(args
, 1); 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;
437 printf("jam: invalid option for COMMAND built-in: '%s'\n", l
->string
);
438 exit(EXITBAD
); /* yeech */
441 /* build shell command */
444 for (l
= lol_get(args
, 0); l
; l
= l
->next
) {
445 if (kStringLen(&str
)) kStringPushBack(&str
, ' ');
446 kStringAppendCStr(&str
, l
->string
);
448 /* no shell command? */
449 if (kStringLen(&str
) < 1) { kStringFree(&str
); return L0
; }
452 p
= popen(kStringCStr(&str
), "r");
453 if (!p
) { kStringFree(&str
); return L0
; }
456 while ((ret
= fread(buffer
, sizeof(char), sizeof(buffer
)-1, p
)) > 0) {
459 kStringAppendCStr(&str
, buffer
);
462 exitStatus
= pclose(p
);
463 if (optExitStatus
&& optStatus1st
) {
464 sprintf(buf1
, "%d", exitStatus
);
465 res
= list_new(res
, buf1
, 0);
467 /* trim output if necessary */
472 // trim trailing blanks
473 int sl
= kStringLen(&str
);
474 spos
= kStringCStr(&str
);
475 while (sl
> 0 && (unsigned char)spos
[sl
] <= ' ') --sl
;
476 kStringTruncate(&str
, sl
);
478 spos
= kStringCStr(&str
);
480 // trim leading blanks
481 while (*spos
&& *((unsigned char *)spos
) <= ' ') ++spos
;
483 res
= list_new(res
, spos
, 0);
487 ret
= 0; /* was anything added? list must have at least one element */
488 spos
= kStringCStr(&str
);
491 /* skip delimiters */
493 unsigned char ch
= (unsigned char)(*spos
);
494 if (ch
== ' ') { if (!optSpaceBreak
) break; }
495 else if (ch
== '\t') { if (!optTabBreak
) break; }
496 else if (ch
== '\r') { if (!optCRBreak
) break; }
497 else if (ch
== '\n') { if (!optLFBreak
) break; }
498 else if (ch
> ' ') break;
505 if (ch
== ' ') { if (optSpaceBreak
) break; }
506 else if (ch
== '\t') { if (optTabBreak
) break; }
507 else if (ch
== '\r') { if (optCRBreak
) break; }
508 else if (ch
== '\n') { if (optLFBreak
) break; }
509 else if ((unsigned char)ch
<= ' ') break;
513 kStringAppendRange(&tmp
, spos
, epos
);
514 res
= list_new(res
, kStringCStr(&tmp
), 0);
519 if (!ret
) { buf1
[0] = '\0'; res
= list_new(res
, buf1
, 0); }
522 res
= list_new(res
, kStringCStr(&str
), 0);
525 /* command exit result next */
526 if (optExitStatus
&& !optStatus1st
) {
527 sprintf(buf1
, "%d", exitStatus
);
528 res
= list_new(res
, buf1
, 0);
534 LIST
*builtin_expri1 (PARSE
*parse
, LOL
*args
, int *jmp
) {
536 int op0
, op1
, res
, comp
= 0;
537 LIST
*el
= lol_get(args
, 0);
539 if (!el
|| !el
->next
|| !el
->next
->next
) return L0
;
540 op0
= atoi(el
->string
);
541 op1
= atoi(el
->next
->next
->string
);
543 switch (el
->next
->string
[0]) {
544 case '+': res
= op0
+op1
; break;
545 case '-': res
= op0
-op1
; break;
546 case '*': res
= op0
*op1
; break;
547 case '/': res
= op0
/op1
; break;
548 case '%': res
= op0
%op1
; break;
551 if (el
->next
->string
[1] == '=') res
= op0
<=op1
; else res
= op0
<op1
;
553 case '=': comp
= 1; res
= op0
==op1
; break;
554 case '!': comp
= 1; res
= op0
!=op1
; break;
557 if (el
->next
->string
[1] == '=') res
= op0
>=op1
; else res
= op0
>op1
;
560 printf("jam: rule ExprI1: unknown operator: '%s'\n", el
->next
->string
);
563 if (comp
) return res
?list_new(L0
, "tan", 0):L0
;
564 sprintf(buffer
, "%d", res
);
565 return list_new(L0
, buffer
, 0);
569 /* Based on code from ftjam by David Turner */
570 LIST
*builtin_split (PARSE
*parse
, LOL
*args
, int *jmp
) {
571 LIST
*input
= lol_get(args
, 0);
572 LIST
*tokens
= lol_get(args
, 1);
578 /* build token array */
579 if (tokens
== NULL
) {
580 memset(token
, 1, sizeof(token
));
582 memset(token
, 0, sizeof(token
));
583 for (; tokens
; tokens
= tokens
->next
) {
584 const char *s
= tokens
->string
;
585 for (; *s
; ++s
) token
[(unsigned char)*s
] = 1;
589 /* now parse the input and split it */
590 for (; input
; input
= input
->next
) {
591 const char *ptr
= input
->string
;
592 const char *lastPtr
= input
->string
;
594 if (token
[(unsigned char)*ptr
]) {
595 size_t count
= ptr
-lastPtr
;
598 kStringAppendRange(&str
, lastPtr
, ptr
);
599 res
= list_new(res
, kStringCStr(&str
), 0);
605 if (ptr
> lastPtr
) res
= list_new(res
, lastPtr
, 0);
612 LIST
*builtin_dependslist (PARSE
*parse
, LOL
*args
, int *jmp
) {
616 for (parents
= lol_get(args
, 0); parents
; parents
= parents
->next
) {
617 TARGET
*t
= bindtarget(parents
->string
);
620 for (child
= t
->depends
; child
; child
= child
->next
) res
= list_new(res
, child
->target
->name
, 1);
626 LIST
*builtin_normpath (PARSE
*parse
, LOL
*args
, int *jmp
) {
627 LIST
*el
= lol_get(args
, 0);
631 if (!el
|| !el
->string
) return L0
;
632 bsz
= strlen(el
->string
)*2+1024;
634 if (buf
== NULL
) return L0
;
635 if (!normalize_path(el
->string
, buf
, bsz
)) { free(buf
); return L0
; }
636 el
= list_new(NULL
, buf
, 0);