From fc7e28de64a3a485425034e4cc4aff2b781450cd Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 26 Apr 2019 18:50:09 -0500 Subject: Read .gz/.xz man pages and check sections in "man man" order. --- toys/pending/man.c | 98 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 29 deletions(-) diff --git a/toys/pending/man.c b/toys/pending/man.c index 912be8b5..1ccae72f 100644 --- a/toys/pending/man.c +++ b/toys/pending/man.c @@ -4,15 +4,26 @@ * * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/man.html -USE_MAN(NEWTOY(man, "<1>1", TOYFLAG_USR|TOYFLAG_BIN)) +USE_MAN(NEWTOY(man, "<1>2k:", TOYFLAG_USR|TOYFLAG_BIN)) config MAN bool "man" default n help - usage: man COMMAND + usage: man [-k STRING] | [SECTION] COMMAND - Read manual for system command. + Read manual page for system command. + + -k Search short + + Man pages are divided into 8 sections, each with an info page (man 8 info). + 1) executables, 2) syscalls, 3) library functions, 4) /dev files, + 5) file formats (ala /etc/hosts), 6) games, 7) miscelanous, 8) sysadmin + + If you don't specify a section it'll show the lowest numbered one, + but "man 1 mkdir" and "man 2 mkdir" are different things. + + The shell builtins don't have section 1 man pages, see the "help" command. */ #define FOR_man @@ -20,6 +31,8 @@ config MAN #include GLOBALS( + char *k; + char any, cell, *f, *line; ) @@ -29,6 +42,7 @@ static void newln() if (TT.any && TT.cell != 2) putchar('\n'); // gawk alias TT.any = TT.cell = 0; } + static void put(char *x) { while (*x && *x != '\n') TT.any = putchar(*x++); @@ -59,6 +73,7 @@ static void do_man(FILE *fp) { size_t len = 0; char *line = 0; + while (getline(&line, &len, fp) > 0) { TT.line = line; s("\\fB", ""), s("\\fI", ""), s("\\fP", ""), s("\\fR", ""); // bash bold,ita @@ -68,9 +83,11 @@ static void do_man(FILE *fp) s("\\&", ""), s("\\f(CW", ""); // gawk,rsync fancy s("\\-", "-"), s("\\(", ""), s("\\^", ""), s("\\e", "\\"); // bash escape s("\\*(", "#"); // gawk var + if (start(".BR")) trim(".BR "), s(" ", ""); // bash boldpunct if (start(".IP")) newln(), trim(".IP "); // bash list if (start(".IR")) trim(".IR "), s(" ", ""); // bash itapunct + trim(".B "); // bash bold trim(".BI "); // gawk boldita trim(".FN "); // bash filename @@ -84,42 +101,65 @@ static void do_man(FILE *fp) else if (start(".TP")) newln(), TT.cell = 1; // bash table else if (start(".") || start("\'")); // bash,git garbage else if (!*TT.line); // emerge - else ((TT.cell != 0) && TT.cell++), put(" "), put(TT.line); + else { + if (TT.cell) TT.cell++; + put(" "); + put(TT.line); + } } newln(); free(line); fclose(fp); } -static FILE *bzcat() +// Try opening all the possible file extensions. +int tryfile(char *section, char *name) { - char cmd[FILENAME_MAX]; - snprintf(cmd, sizeof(cmd), "bzcat %s", TT.f); - return popen(cmd, "r"); -} + char *suf[] = {".gz", ".bz2", ".xz", ""}, *end, + *s = xmprintf("/usr/share/man/man%s/%s.%s.bz2", section, name, section); + int fd, i, and = 1; -static char *find(char *path, int suf) -{ - glob_t g; - int i; - size_t len = strlen(*toys.optargs); - char *name; - glob(path, 0, 0, &g); - for (i = 0; !TT.f && i < g.gl_pathc; i++) { - name = basename(g.gl_pathv[i]); - if (strlen(name) == len + suf && !strncmp(name, *toys.optargs, len)) - TT.f = strdup(g.gl_pathv[i]); - } - globfree(&g); - return TT.f; + end = s+strlen(s); + do { + for (i = 0; i