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
) {
326 HSRegExp
*re
= hsRxCompile(l
->string
);
327 /* for each string to match against */
328 for (r
= lol_get(args
, 1); r
; r
= r
->next
) {
329 if (hsRxExec(re
, r
->string
)) {
331 /* find highest parameter */
332 for (top
= NSUBEXP
; top
-- > 1;) if (re
->startp
[top
]) break;
333 /* and add all parameters up to highest onto list */
334 /* must have parameters to have results! */
335 for (i
= 1; i
<= top
; i
++) {
337 int l
= re
->endp
[i
]-re
->startp
[i
];
338 memcpy(buf
, re
->startp
[i
], l
);
340 res
= list_new(res
, buf
, 0);
350 LIST
*builtin_hdrmacro (PARSE
*parse
, LOL
*args
, int *jmp
) {
351 LIST
*l
= lol_get(args
, 0);
352 for (; l
; l
= list_next(l
)) {
353 TARGET
*t
= bindtarget(l
->string
);
354 /* scan file for header filename macro definitions */
355 if (DEBUG_HEADER
) printf("scanning '%s' for header file macro definitions\n", l
->string
);
362 /* backported from boost-jam */
364 * Return the current working directory.
366 * Usage: pwd = [ PWD ] ;
368 LIST
*builtin_pwd (PARSE
*parse
, LOL
*args
, int *jmp
) {
369 char pwd_buffer
[PATH_MAX
];
370 if (!getcwd(pwd_buffer
, sizeof(pwd_buffer
))) {
371 perror("can not get current directory");
374 return list_new(L0
, pwd_buffer
, 0);
378 /* backported from boost-jam */
379 LIST
*builtin_sort (PARSE
*parse
, LOL
*args
, int *jmp
) {
380 LIST
*arg
= lol_get(args
, 0);
381 arg
= list_sort(arg
);
386 /* backported from boost-jam; greatly improved */
387 /* Command shcmd [[ : options ]] */
388 LIST
*builtin_command (PARSE
*parse
, LOL
*args
, int *jmp
) {
392 char buffer
[1024], buf1
[32], *spos
, *epos
;
395 int optExitStatus
= 0;
398 int optTrimRight
= 1;
399 int optStatus1st
= 0;
401 int optSpaceBreak
= 1;
407 /* for each string in 2nd list: check for arg */
408 for (l
= lol_get(args
, 1); l
!= NULL
; l
= l
->next
) {
409 if (!strcmp("exit-status", l
->string
)) optExitStatus
= 1;
410 else if (!strcmp("exit-code", l
->string
)) optExitStatus
= 1;
411 else if (!strcmp("status-first", l
->string
)) optStatus1st
= 1;
412 else if (!strcmp("code-first", l
->string
)) optStatus1st
= 1;
413 else if (!strcmp("no-output", l
->string
)) optNoOutput
= 1;
414 else if (!strcmp("no-trim", l
->string
)) optTrimLeft
= optTrimRight
= 0;
415 else if (!strcmp("no-trim-left", l
->string
)) optTrimLeft
= 0;
416 else if (!strcmp("no-trim-right", l
->string
)) optTrimRight
= 0;
417 else if (!strcmp("parse-output", l
->string
)) optParseOut
= 1;
418 else if (!strcmp("no-space-break", l
->string
)) optSpaceBreak
= 0;
419 else if (!strcmp("no-tab-break", l
->string
)) optTabBreak
= 0;
420 else if (!strcmp("no-nl-break", l
->string
)) optLFBreak
= 0;
421 else if (!strcmp("no-lf-break", l
->string
)) optLFBreak
= 0;
422 else if (!strcmp("no-cr-break", l
->string
)) optCRBreak
= 0;
424 printf("jam: invalid option for COMMAND built-in: '%s'\n", l
->string
);
425 exit(EXITBAD
); /* yeech */
428 /* build shell command */
431 for (l
= lol_get(args
, 0); l
; l
= l
->next
) {
432 if (kStringLen(&str
)) kStringPushBack(&str
, ' ');
433 kStringAppendCStr(&str
, l
->string
);
435 /* no shell command? */
436 if (kStringLen(&str
) < 1) { kStringFree(&str
); return L0
; }
439 p
= popen(kStringCStr(&str
), "r");
440 if (!p
) { kStringFree(&str
); return L0
; }
443 while ((ret
= fread(buffer
, sizeof(char), sizeof(buffer
)-1, p
)) > 0) {
446 kStringAppendCStr(&str
, buffer
);
449 exitStatus
= pclose(p
);
450 if (optExitStatus
&& optStatus1st
) {
451 sprintf(buf1
, "%d", exitStatus
);
452 res
= list_new(res
, buf1
, 0);
454 /* trim output if necessary */
459 // trim trailing blanks
460 int sl
= kStringLen(&str
);
461 spos
= kStringCStr(&str
);
462 while (sl
> 0 && (unsigned char)spos
[sl
] <= ' ') --sl
;
463 kStringTruncate(&str
, sl
);
465 spos
= kStringCStr(&str
);
467 // trim leading blanks
468 while (*spos
&& *((unsigned char *)spos
) <= ' ') ++spos
;
470 res
= list_new(res
, spos
, 0);
474 ret
= 0; /* was anything added? list must have at least one element */
475 spos
= kStringCStr(&str
);
478 /* skip delimiters */
480 unsigned char ch
= (unsigned char)(*spos
);
481 if (ch
== ' ') { if (!optSpaceBreak
) break; }
482 else if (ch
== '\t') { if (!optTabBreak
) break; }
483 else if (ch
== '\r') { if (!optCRBreak
) break; }
484 else if (ch
== '\n') { if (!optLFBreak
) break; }
485 else if (ch
> ' ') break;
492 if (ch
== ' ') { if (optSpaceBreak
) break; }
493 else if (ch
== '\t') { if (optTabBreak
) break; }
494 else if (ch
== '\r') { if (optCRBreak
) break; }
495 else if (ch
== '\n') { if (optLFBreak
) break; }
496 else if ((unsigned char)ch
<= ' ') break;
500 kStringAppendRange(&tmp
, spos
, epos
);
501 res
= list_new(res
, kStringCStr(&tmp
), 0);
506 if (!ret
) { buf1
[0] = '\0'; res
= list_new(res
, buf1
, 0); }
509 res
= list_new(res
, kStringCStr(&str
), 0);
512 /* command exit result next */
513 if (optExitStatus
&& !optStatus1st
) {
514 sprintf(buf1
, "%d", exitStatus
);
515 res
= list_new(res
, buf1
, 0);
521 LIST
*builtin_expri1 (PARSE
*parse
, LOL
*args
, int *jmp
) {
526 LIST
*el
= lol_get(args
, 0);
528 if (!el
|| !el
->next
|| !el
->next
->next
) return L0
;
529 op0
= atoi(el
->string
);
530 op1
= atoi(el
->next
->next
->string
);
532 switch (el
->next
->string
[0]) {
533 case '+': res
= op0
+op1
; break;
534 case '-': res
= op0
-op1
; break;
535 case '*': res
= op0
*op1
; break;
536 case '/': res
= op0
/op1
; break;
537 case '%': res
= op0
%op1
; break;
539 printf("jam: rule ExprI1: unknown operator: '%s'\n", el
->next
->string
);
542 sprintf(buffer
, "%d", res
);
543 return list_new(L0
, buffer
, 0);
547 /* Based on code from ftjam by David Turner */
548 LIST
*builtin_split (PARSE
*parse
, LOL
*args
, int *jmp
) {
549 LIST
*input
= lol_get(args
, 0);
550 LIST
*tokens
= lol_get(args
, 1);
556 /* build token array */
557 if (tokens
== NULL
) {
558 memset(token
, 1, sizeof(token
));
560 memset(token
, 0, sizeof(token
));
561 for (; tokens
; tokens
= tokens
->next
) {
562 const char *s
= tokens
->string
;
563 for (; *s
; ++s
) token
[(unsigned char)*s
] = 1;
567 /* now parse the input and split it */
568 for (; input
; input
= input
->next
) {
569 const char *ptr
= input
->string
;
570 const char *lastPtr
= input
->string
;
572 if (token
[(unsigned char)*ptr
]) {
573 size_t count
= ptr
-lastPtr
;
576 kStringAppendRange(&str
, lastPtr
, ptr
);
577 res
= list_new(res
, kStringCStr(&str
), 0);
583 if (ptr
> lastPtr
) res
= list_new(res
, lastPtr
, 0);
590 LIST
*builtin_dependslist (PARSE
*parse
, LOL
*args
, int *jmp
) {
594 for (parents
= lol_get(args
, 0); parents
; parents
= parents
->next
) {
595 TARGET
*t
= bindtarget(parents
->string
);
598 for (child
= t
->depends
; child
; child
= child
->next
) res
= list_new(res
, child
->target
->name
, 1);
604 LIST
*builtin_normpath (PARSE
*parse
, LOL
*args
, int *jmp
) {
605 LIST
*el
= lol_get(args
, 0);
609 if (!el
|| !el
->string
) return L0
;
610 bsz
= strlen(el
->string
)*2+1024;
612 if (buf
== NULL
) return L0
;
613 if (!normalize_path(el
->string
, buf
, bsz
)) { free(buf
); return L0
; }
614 el
= list_new(NULL
, buf
, 0);