From 3e4cbcf40a45dfe50b6ffc107307ec28ad6eb7e0 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Tue, 19 Jan 2021 23:23:29 -0500 Subject: Escape \ and / in mtags search patterns --- bin/mtags.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/bin/mtags.c b/bin/mtags.c index 3ff72c19..db66f1d4 100644 --- a/bin/mtags.c +++ b/bin/mtags.c @@ -24,6 +24,15 @@ #include #include +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); } -- cgit 1.4.1