From 7e46e2e83472d2a22fcc3793aadb647d26defe92 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Tue, 19 Jan 2021 22:40:14 -0500 Subject: Add mtags to generate tags for make and mdoc --- bin/mtags.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 bin/mtags.c (limited to 'bin/mtags.c') diff --git a/bin/mtags.c b/bin/mtags.c new file mode 100644 index 00000000..257adbe7 --- /dev/null +++ b/bin/mtags.c @@ -0,0 +1,86 @@ +/* Copyright (C) 2021 June McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + bool append = false; + const char *path = "tags"; + for (int opt; 0 < (opt = getopt(argc, argv, "af:"));) { + switch (opt) { + break; case 'a': append = true; + break; case 'f': path = optarg; + break; default: return EX_USAGE; + } + } + + FILE *tags = fopen(path, (append ? "a" : "w")); + if (!tags) err(EX_CANTCREAT, "%s", path); + + regex_t makeFile, makeLine; + regex_t mdocFile, mdocLine; + int error = 0 + || regcomp(&makeFile, "(^|/)Makefile|[.]mk$", REG_EXTENDED | REG_NOSUB) + || regcomp( + &makeLine, + "^([.][^$A-Z][^$[:space:]]*|[^.$][^$[:space:]]*):", + REG_EXTENDED + ) + || regcomp(&mdocFile, "[.][1-9]$", REG_EXTENDED | REG_NOSUB) + || regcomp(&mdocLine, "^[.]S[hs] ([^\t\n]+)", REG_EXTENDED); + assert(!error); + + size_t cap = 0; + char *buf = NULL; + for (int i = optind; i < argc; ++i) { + const regex_t *regex; + if (!regexec(&makeFile, argv[i], 0, NULL, 0)) { + regex = &makeLine; + } else if (!regexec(&mdocFile, argv[i], 0, NULL, 0)) { + regex = &mdocLine; + } else { + warnx("skipping unknown file type %s", argv[i]); + continue; + } + + FILE *file = fopen(argv[i], "r"); + if (!file) err(EX_NOINPUT, "%s", argv[i]); + + 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", + (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] + ); + if (n < 0) err(EX_IOERR, "%s", path); + } + fclose(file); + } + + error = fclose(tags); + if (error) err(EX_IOERR, "%s", path); +} -- cgit 1.4.1