From d6b449f4ac88ec30a3882272e6261651d3d1348b Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Tue, 12 Jan 2021 20:53:29 -0500 Subject: Add htagml -x --- bin/htagml.c | 35 ++++++++++++++++++++++++++++------- bin/man1/htagml.1 | 18 +++++++++++++----- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/bin/htagml.c b/bin/htagml.c index 1c8c683f..9c48ed48 100644 --- a/bin/htagml.c +++ b/bin/htagml.c @@ -20,10 +20,19 @@ #include #include #include -#include #include #include +struct Tag { + char *tag; + int num; + regex_t regex; +}; + +static int compar(const void *a, const void *b) { + return ((const struct Tag *)a)->num - ((const struct Tag *)b)->num; +} + static char *nomagic(const char *pattern) { char *buf = malloc(2 * strlen(pattern) + 1); if (!buf) err(EX_OSERR, "malloc"); @@ -50,11 +59,13 @@ static size_t escape(const char *ptr, size_t len) { int main(int argc, char *argv[]) { bool pre = false; + bool index = false; const char *tagsFile = "tags"; - for (int opt; 0 < (opt = getopt(argc, argv, "f:p"));) { + for (int opt; 0 < (opt = getopt(argc, argv, "f:px"));) { switch (opt) { break; case 'f': tagsFile = optarg; break; case 'p': pre = true; + break; case 'x': index = true; break; default: return EX_USAGE; } } @@ -66,11 +77,7 @@ int main(int argc, char *argv[]) { size_t len = 0; size_t cap = 256; - struct Tag { - char *tag; - int num; - regex_t regex; - } *tags = malloc(cap * sizeof(*tags)); + struct Tag *tags = malloc(cap * sizeof(*tags)); if (!tags) err(EX_OSERR, "malloc"); char *buf = NULL; @@ -131,6 +138,7 @@ int main(int argc, char *argv[]) { tag->num = num; break; } + if (index) continue; if (!tag) { escape(buf, strlen(buf)); continue; @@ -157,4 +165,17 @@ int main(int argc, char *argv[]) { if (match) escape(match, strlen(match)); } if (pre) printf(""); + + if (!index) return EX_OK; + qsort(tags, len, sizeof(*tags), compar); + printf("\n"); } diff --git a/bin/man1/htagml.1 b/bin/man1/htagml.1 index 70619647..f313c09d 100644 --- a/bin/man1/htagml.1 +++ b/bin/man1/htagml.1 @@ -8,9 +8,9 @@ . .Sh SYNOPSIS .Nm -.Op Fl p -.Op Fl f Ar tags_file -.Ar name +.Op Fl px +.Op Fl f Ar tagsfile +.Ar file . .Sh DESCRIPTION The @@ -25,9 +25,9 @@ with the class .Pp The arguments are as follows: .Bl -tag -width Ds -.It Fl f Ar tags_file +.It Fl f Ar tagsfile Read the tag descriptions from a file called -.Ar tags_file . +.Ar tagsfile . The default behavior is to read them from a file called .Pa tags . @@ -35,6 +35,14 @@ to read them from a file called Wrap the output in a .Sy pre element. +.It Fl x +Instead produce an index of tags +ordered by their occurrence in +.Ar file . +The index is formatted as a +.Sy ul +element with the class +.Qq index . .El . .Sh FILES -- cgit 1.4.1