diff options
author | June McEnroe <june@causal.agency> | 2021-01-19 23:23:29 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2021-01-19 23:23:59 -0500 |
commit | 39f9c9699111cf398ed5eb7db518c34a2ed99d4a (patch) | |
tree | 1e206679fb83560b8d8c7f65bccf7b3518b4c578 /bin | |
parent | Use mtags in source-filter (diff) | |
download | src-39f9c9699111cf398ed5eb7db518c34a2ed99d4a.tar.gz src-39f9c9699111cf398ed5eb7db518c34a2ed99d4a.zip |
Escape \ and / in mtags search patterns
Diffstat (limited to 'bin')
-rw-r--r-- | bin/mtags.c | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/bin/mtags.c b/bin/mtags.c index 257adbe7..8f83496c 100644 --- a/bin/mtags.c +++ b/bin/mtags.c @@ -24,6 +24,15 @@ #include <sysexits.h> #include <unistd.h> +static void escape(FILE *file, const char *str, size_t len) { + for (size_t i = 0; i < len; ++i) { + if (str[i] == '\\' || str[i] == '/') { + putc('\\', file); + } + putc(str[i], file); + } +} + int main(int argc, char *argv[]) { bool append = false; const char *path = "tags"; @@ -70,17 +79,14 @@ int main(int argc, char *argv[]) { while (0 < getline(&buf, &cap, file)) { regmatch_t match[2]; if (regexec(regex, buf, 2, match, 0)) continue; - int n = fprintf( - tags, "%.*s\t%s\t/^%.*s/\n", + fprintf( + tags, "%.*s\t%s\t/^", (int)(match[1].rm_eo - match[1].rm_so), &buf[match[1].rm_so], - argv[i], - (int)(match[0].rm_eo - match[0].rm_so), &buf[match[0].rm_so] + argv[i] ); - if (n < 0) err(EX_IOERR, "%s", path); + escape(tags, buf, match[0].rm_eo); + fprintf(tags, "/\n"); } fclose(file); } - - error = fclose(tags); - if (error) err(EX_IOERR, "%s", path); } |