From 49daadea24808f007fbd2fe46ac537cb38705b07 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 8 Feb 2019 00:42:41 -0500 Subject: Match only the basename in hi --- bin/hi.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'bin') diff --git a/bin/hi.c b/bin/hi.c index e11193d9..c2d9f3c0 100644 --- a/bin/hi.c +++ b/bin/hi.c @@ -148,7 +148,7 @@ static const struct Language { size_t len; } Languages[] = { { "c", "\\.[ch]$", CSyntax, ARRAY_LEN(CSyntax) }, - { "make", "Makefile$|\\.mk$", MakeSyntax, ARRAY_LEN(MakeSyntax) }, + { "make", "^Makefile$|\\.mk$", MakeSyntax, ARRAY_LEN(MakeSyntax) }, { "mdoc", "\\.[1-9]$", MdocSyntax, ARRAY_LEN(MdocSyntax) }, }; @@ -223,7 +223,7 @@ static void check(void) { } } -typedef void HeaderFn(const char *path); +typedef void HeaderFn(const char *name); typedef void OutputFn(enum Class class, const char *str, size_t len); enum SGR { @@ -323,12 +323,12 @@ static void ircOutput(enum Class class, const char *str, size_t len) { } } -static void htmlHeader(const char *path) { - (void)path; +static void htmlHeader(const char *name) { + (void)name; printf("
");
 }
-static void htmlFooter(const char *path) {
-	(void)path;
+static void htmlFooter(const char *name) {
+	(void)name;
 	printf("
\n"); } @@ -365,11 +365,9 @@ static void htmlOutput(enum Class class, const char *str, size_t len) { printf(""); } -static void htmlDocumentHeader(const char *path) { - const char *slash = strrchr(path, '/'); - if (slash) path = &slash[1]; +static void htmlDocumentHeader(const char *name) { printf("\n"); - htmlEscape(path, strlen(path)); + htmlEscape(name, strlen(name)); printf( "\n" "\n" ); - htmlHeader(path); + htmlHeader(name); } static const struct Format { @@ -437,17 +435,20 @@ int main(int argc, char *argv[]) { if (!file) err(EX_NOINPUT, "%s", path); } + const char *name = strrchr(path, '/'); + name = (name ? &name[1] : path); + if (!lang) { for (size_t i = 0; i < ARRAY_LEN(Languages); ++i) { regex_t regex = compile(Languages[i].pattern, REG_NOSUB); - bool match = !regexec(®ex, path, 0, NULL, 0); + bool match = !regexec(®ex, name, 0, NULL, 0); regfree(®ex); if (match) { lang = &Languages[i]; break; } } - if (!lang) errx(EX_USAGE, "cannot infer language for %s", path); + if (!lang) errx(EX_USAGE, "cannot infer language for %s", name); } if (!format) format = &Formats[0]; @@ -471,7 +472,7 @@ int main(int argc, char *argv[]) { highlight(*lang, hi, str); - if (format->header) format->header(path); + if (format->header) format->header(name); size_t run = 0; for (size_t i = 0; i < len; i += run) { for (run = 0; i + run < len; ++run) { @@ -479,5 +480,5 @@ int main(int argc, char *argv[]) { } format->output(hi[i], &str[i], run); } - if (format->footer) format->footer(path); + if (format->footer) format->footer(name); } -- cgit 1.4.1 2022-06-02Don't duplicate path stringJune McEnroe 2022-06-02Use stderr instead of /dev/tty, realloc buffer if lines too longJune McEnroe For some reason I haven't been able to figure out, trying to poll /dev/tty returns POLLNVAL (and this was using 100% CPU looping), but using stderr instead works fine. 2022-06-02Add initial working version of qfJune McEnroe 2022-05-29Set prompt for okshJune McEnroe