man: add handling of "man links", by Ivana Varekova <varekova AT redhat.com>

function                                             old     new   delta
run_pipe                                             102     354    +252
show_manpage                                           -     126    +126
man_main                                             705     615     -90
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 378/-90)           Total: 288 bytes
This commit is contained in:
Denis Vlasenko 2008-08-05 13:16:18 +00:00
parent e9ad84dfd4
commit 540baf646a
2 changed files with 68 additions and 17 deletions

View File

@ -6,22 +6,22 @@
menu "Archival Utilities" menu "Archival Utilities"
config FEATURE_SEAMLESS_LZMA config FEATURE_SEAMLESS_LZMA
bool "Make tar, rpm, man, modprobe etc understand .lzma data" bool "Make tar, rpm, modprobe etc understand .lzma data"
default n default n
help help
Make tar, rpm, man, modprobe etc understand .lzma data. Make tar, rpm, modprobe etc understand .lzma data.
config FEATURE_SEAMLESS_BZ2 config FEATURE_SEAMLESS_BZ2
bool "Make tar, rpm, man, modprobe etc understand .bz2 data" bool "Make tar, rpm, modprobe etc understand .bz2 data"
default n default n
help help
Make tar, rpm, man, modprobe etc understand .bz2 data. Make tar, rpm, modprobe etc understand .bz2 data.
config FEATURE_SEAMLESS_GZ config FEATURE_SEAMLESS_GZ
bool "Make tar, rpm, man, modprobe etc understand .gz data" bool "Make tar, rpm, modprobe etc understand .gz data"
default n default n
help help
Make tar, rpm, man, modprobe etc understand .gz data. Make tar, rpm, modprobe etc understand .gz data.
config FEATURE_SEAMLESS_Z config FEATURE_SEAMLESS_Z
bool "Make tar and gunzip understand .Z data" bool "Make tar and gunzip understand .Z data"

View File

@ -27,10 +27,19 @@ echo ".pl \n(nlu+10"
#define STR_catNULmanNUL "cat\0man" #define STR_catNULmanNUL "cat\0man"
#define STR_cat "cat\0man" #define STR_cat "cat\0man"
static int run_pipe(const char *unpacker, const char *pager, char *man_filename, int man) //TODO: make gz/bz2 support conditional on FEATURE_SEAMLESS_GZ/BZ2,
// add SEAMLESS_LZMA support
static int show_manpage(const char *pager, char *man_filename, int man, int level);
static int run_pipe(const char *unpacker, const char *pager, char *man_filename, int man, int level)
{ {
char *cmd; char *cmd;
/* Prevent man page link loops */
if (level > 10)
return 0;
if (access(man_filename, R_OK) != 0) if (access(man_filename, R_OK) != 0)
return 0; return 0;
@ -39,6 +48,53 @@ static int run_pipe(const char *unpacker, const char *pager, char *man_filename,
return 1; return 1;
} }
if (man) { /* man page, not cat page */
/* Test whether the man page is not a link to another one. */
/* The link has the following on the first line: */
/* ".so another_man_page" */
struct stat sb;
char *line;
char *linkname, *p;
/* On my system:
* man1/genhostid.1.gz: 203 bytes - smallest real manpage
* man2/path_resolution.2.gz: 114 bytes - largest link
*/
xstat(man_filename, &sb);
if (sb.st_size > 300) /* err on the safe side */
goto ordinary_manpage;
line = xmalloc_open_zipped_read_close(man_filename, NULL);
if (!line || strncmp(line, ".so ", 4) != 0) {
free(line);
goto ordinary_manpage;
}
/* Example: man2/path_resolution.2.gz contains
* ".so man7/path_resolution.7\n<junk>"
*/
*strchrnul(line, '\n') = '\0';
linkname = p = skip_whitespace(&line[4]);
while (1) {
char *newname_slash, *oldname_slash;
oldname_slash = strrchr(man_filename, '/');
if (!oldname_slash)
goto ordinary_manpage;
*oldname_slash = '\0';
newname_slash = strchr(p, '/');
if (!newname_slash)
break;
p = newname_slash + 1;
}
man_filename = xasprintf("%s/%s" ".bz2", man_filename, linkname);
free(line);
/* Note: we leak "new" man_filename string as well... */
if (show_manpage(pager, man_filename, man, level + 1))
return 1;
/* else: show the link, it's better than nothing */
}
ordinary_manpage:
/* "2>&1" is 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 */ * Otherwise it may show just empty screen */
cmd = xasprintf( cmd = xasprintf(
@ -51,22 +107,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" */ /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */
static int show_manpage(const char *pager, char *man_filename, int man) static int show_manpage(const char *pager, char *man_filename, int man, int level)
{ {
int len; int len;
if (run_pipe("bunzip2 -c", pager, man_filename, man)) if (run_pipe("bunzip2 -c", pager, man_filename, man, level))
return 1; return 1;
len = strlen(man_filename) - 1; len = strlen(man_filename) - 1;
man_filename[len] = '\0'; /* ".bz2" -> ".gz" */ man_filename[len] = '\0'; /* ".bz2" -> ".gz" */
man_filename[len - 2] = 'g'; man_filename[len - 2] = 'g';
if (run_pipe("gunzip -c", pager, man_filename, man)) if (run_pipe("gunzip -c", pager, man_filename, man, level))
return 1; return 1;
man_filename[len - 3] = '\0'; /* ".gz" -> "" */ man_filename[len - 3] = '\0'; /* ".gz" -> "" */
if (run_pipe(STR_cat, pager, man_filename, man)) if (run_pipe(STR_cat, pager, man_filename, man, level))
return 1; return 1;
return 0; return 0;
@ -121,11 +177,6 @@ int man_main(int argc UNUSED_PARAM, char **argv)
} }
config_close(parser); config_close(parser);
// TODO: my man3/getpwuid.3.gz contains just one line:
// .so man3/getpwnam.3
// (and I _dont_ have man3/getpwnam.3, I have man3/getpwnam.3.gz)
// need to support this...
not_found = 0; not_found = 0;
do { /* for each argv[] */ do { /* for each argv[] */
int found = 0; int found = 0;
@ -151,7 +202,7 @@ int man_main(int argc UNUSED_PARAM, char **argv)
sect_len, cur_sect, sect_len, cur_sect,
*argv, *argv,
sect_len, cur_sect); sect_len, cur_sect);
found_here = show_manpage(pager, man_filename, cat0man1); found_here = show_manpage(pager, man_filename, cat0man1, 0);
found |= found_here; found |= found_here;
cat0man1 += found_here + 1; cat0man1 += found_here + 1;
free(man_filename); free(man_filename);