summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-01-19 23:23:29 -0500
committerJune McEnroe <june@causal.agency>2021-01-19 23:23:59 -0500
commit3e4cbcf40a45dfe50b6ffc107307ec28ad6eb7e0 (patch)
tree4d9cae30c13edb696b8577ebc1356309277c2b17
parentUse mtags in source-filter (diff)
downloadsrc-3e4cbcf40a45dfe50b6ffc107307ec28ad6eb7e0.tar.gz
src-3e4cbcf40a45dfe50b6ffc107307ec28ad6eb7e0.zip
Escape \ and / in mtags search patterns
-rw-r--r--bin/mtags.c22
1 files 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 <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);
 }