Skip to content

Commit ba903ec

Browse files
committed
Add %D Flag
Added %D flag which expands to the directory part of the current file path. Example: "path/to/file.txt" -> "path/to/". Updated tup.1 to include new flag documentation.
1 parent 1306ed4 commit ba903ec

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/tup/parser.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,6 +4039,25 @@ static char *tup_printf(struct tupfile *tf, const char *cmd, int cmd_len,
40394039
*/
40404040
estring_append(&e, tent->name.s, tent->name.len);
40414041
}
4042+
} else if(*next == 'D') {
4043+
/* D: Expands to the directory portion of the current file path.
4044+
* Example: "path/to/file.txt" -> "path/to/"
4045+
*/
4046+
int first = 1;
4047+
if(nl->num_entries == 0) {
4048+
fprintf(tf->f, "tup error: %%D used in rule pattern and no input files were specified.\n");
4049+
return NULL;
4050+
}
4051+
TAILQ_FOREACH(nle, &nl->entries, list) {
4052+
if(!first) {
4053+
estring_append(&e, " ", 1);
4054+
}
4055+
int dirlen = nle->len - nle->baselen - (*p == '/' ? 1 : 0);
4056+
if (dirlen > 0) {
4057+
estring_append(&e, nle->path, dirlen);
4058+
}
4059+
first = 0;
4060+
}
40424061
} else if(*next == 'g') {
40434062
/* g: Expands to the "glob" portion of an *, ?, [] expansion.
40444063
* Given the filnames: a_text.txt, b_text.txt and c_text.txt,

tup.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,9 @@ The name of the lowest level of the directory. For example, in foo/bar/Tupfile,
548548
.fi
549549
Using this macro in foo/bar/Tupfile would then create foo/bar/libbar.a
550550
.TP
551+
.B %D
552+
Like %f, but is just the directory part of the file path. The basename is stripped off. For example, "src/foo.c" would become "src/".
553+
.TP
551554
.B %g
552555
The string that a glob operator matched. For example with the files a_text.txt and b_text.txt, the rule:
553556
.nf

0 commit comments

Comments
 (0)