diff options
author | Leah Neukirchen <leah@vuxu.org> | 2017-09-24 19:42:12 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2017-09-24 19:42:12 +0200 |
commit | 942288dfb98a1762220da96d6a4b899d0651c6f8 (patch) | |
tree | 04319e58972f2253dfe6f622bb0df182e1037c1b | |
parent | c188e1313bb9808bab82e0a11b358bb1ec781049 (diff) | |
download | xe-942288dfb98a1762220da96d6a4b899d0651c6f8.tar.gz xe-942288dfb98a1762220da96d6a4b899d0651c6f8.tar.xz xe-942288dfb98a1762220da96d6a4b899d0651c6f8.zip |
percent patterns without / match against basenames only
-rw-r--r-- | xe.1 | 1 | ||||
-rw-r--r-- | xe.c | 24 |
2 files changed, 23 insertions, 2 deletions
diff --git a/xe.1 b/xe.1 index b75209d..449879c 100644 --- a/xe.1 +++ b/xe.1 @@ -144,6 +144,7 @@ and replaces the first occurrence of .Li Sq \&% with the matched string in the remaining arguments, which are then used as the command to be executed. +Patterns without a slash are matched against the basenames only. .Pp Multiple runs of patterns and commands are separated by .Li Sq \&+ . diff --git a/xe.c b/xe.c index 1888775..19c1a26 100644 --- a/xe.c +++ b/xe.c @@ -362,6 +362,12 @@ parse_jobs(char *s) int perc_match(char *pat, char *arg) { + if (!strchr(pat, '/')) { + char *d = strrchr(arg, '/'); + if (d) + arg = d + 1; + } + char *s = strchr(pat, '%'); if (!s) @@ -382,6 +388,14 @@ perc_subst(char *pat, char *base, char *arg) { static char buf[2048]; size_t l; + char *dir = base; + + if (!strchr(pat, '/')) { + char *d = strrchr(base, '/'); + if (d) + base = d + 1; + } + char *s = strchr(pat, '%'); char *t = strchr(arg, '%'); @@ -389,7 +403,10 @@ perc_subst(char *pat, char *base, char *arg) return arg; if (s) - l = snprintf(buf, sizeof buf, "%.*s%.*s%.*s", + l = snprintf(buf, sizeof buf, "%.*s%.*s%.*s%.*s", + (int)(base - dir), + dir, + (int)(t - arg), arg, @@ -399,7 +416,10 @@ perc_subst(char *pat, char *base, char *arg) (int)(arg + strlen(arg) - t), t+1); else - l = snprintf(buf, sizeof buf, "%.*s%s%.*s", + l = snprintf(buf, sizeof buf, "%.*s%.*s%s%.*s", + (int)(base - dir), + dir, + (int)(t - arg), arg, |