man: support cat pages too (by Jason Curl <jcurlnews AT arcor.de>)
authorDenis Vlasenko <vda.linux@googlemail.com>
Fri, 4 Jul 2008 21:58:00 +0000 (21:58 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Fri, 4 Jul 2008 21:58:00 +0000 (21:58 -0000)
function                                             old     new   delta
show_manpage                                           -     112    +112
run_pipe                                              89     102     +13
man_main                                             721     725      +4

miscutils/man.c

index 8c55ae9b97c66ac91bde00d345f2b8c58fdf7837..20172f6b25e7b07d0468a7e1d8bde6f91823420a 100644 (file)
@@ -23,7 +23,7 @@ echo ".pl \n(nlu+10"
 
 */
 
-static int run_pipe(const char *unpacker, const char *pager, char *man_filename)
+static int run_pipe(const char *unpacker, const char *pager, char *man_filename, int cat)
 {
        char *cmd;
 
@@ -35,9 +35,10 @@ static int run_pipe(const char *unpacker, const char *pager, char *man_filename)
                return 1;
        }
 
-       /* "2>&1" added so that nroff errors are shown in pager too.
+       /* "2>&1" is added so that nroff errors are shown in pager too.
         * Otherwise it may show just empty screen */
-       cmd = xasprintf("%s '%s' | gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s",
+       cmd = xasprintf(cat ? "%s '%s' | %s"
+                       : "%s '%s' | gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s",
                        unpacker, man_filename, pager);
        system(cmd);
        free(cmd);
@@ -45,22 +46,22 @@ static int run_pipe(const char *unpacker, const char *pager, char *man_filename)
 }
 
 /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */
-static int show_manpage(const char *pager, char *man_filename)
+static int show_manpage(const char *pager, char *man_filename, int cat)
 {
        int len;
 
-       if (run_pipe("bunzip2 -c", pager, man_filename))
+       if (run_pipe("bunzip2 -c", pager, man_filename, cat))
                return 1;
 
        len = strlen(man_filename) - 1;
 
        man_filename[len] = '\0'; /* ".bz2" -> ".gz" */
        man_filename[len - 2] = 'g';
-       if (run_pipe("gunzip -c", pager, man_filename))
+       if (run_pipe("gunzip -c", pager, man_filename, cat))
                return 1;
 
        man_filename[len - 3] = '\0'; /* ".gz" -> "" */
-       if (run_pipe("cat", pager, man_filename))
+       if (run_pipe("cat", pager, man_filename, cat))
                return 1;
 
        return 0;
@@ -145,14 +146,30 @@ int man_main(int argc ATTRIBUTE_UNUSED, char **argv)
                                do { /* for each section */
                                        char *next_sect = strchrnul(cur_sect, ':');
                                        int sect_len = next_sect - cur_sect;
-
-                                       char *man_filename = xasprintf("%.*s/man%.*s/%s.%.*s" ".bz2",
-                                                               path_len, cur_path,
-                                                               sect_len, cur_sect,
-                                                               *argv,
-                                                               sect_len, cur_sect);
-                                       found |= show_manpage(pager, man_filename);
+                                       char *man_filename;
+                                       int found_here;
+
+                                       /* Search for cat page first */
+                                       man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" ".bz2",
+                                                       path_len, cur_path,
+                                                       "cat",
+                                                       sect_len, cur_sect,
+                                                       *argv,
+                                                       sect_len, cur_sect);
+                                       found_here = show_manpage(pager, man_filename, 1);
                                        free(man_filename);
+                                       if (!found_here) {
+                                               man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" ".bz2",
+                                                       path_len, cur_path,
+                                                       "man",
+                                                       sect_len, cur_sect,
+                                                       *argv,
+                                                       sect_len, cur_sect);
+                                               found_here = show_manpage(pager, man_filename, 0);
+                                               free(man_filename);
+                                       }
+                                       found |= found_here;
+
                                        if (found && !(opt & OPT_a))
                                                goto next_arg;
                                        cur_sect = next_sect;