man: handle many MANPAGER directives so that they do not override

but accumulate. By Ivana Varekova <varekova AT redhat.com>

function                                             old     new   delta
man_main                                             567     684    +117
This commit is contained in:
Denis Vlasenko 2008-04-19 03:42:47 +00:00
parent e96dcb4dcf
commit 50d068cb76

View File

@ -69,9 +69,11 @@ int man_main(int argc ATTRIBUTE_UNUSED, char **argv)
{ {
FILE *cf; FILE *cf;
const char *pager; const char *pager;
char *man_path, *sec_list; char **man_path_list;
char *sec_list;
char *cur_path, *cur_sect; char *cur_path, *cur_sect;
char *line, *value; char *line, *value;
int count_mp, alloc_mp, cur_mp;
int opt; int opt;
opt_complementary = "-1"; /* at least one argument */ opt_complementary = "-1"; /* at least one argument */
@ -79,7 +81,12 @@ int man_main(int argc ATTRIBUTE_UNUSED, char **argv)
argv += optind; argv += optind;
sec_list = xstrdup("1:2:3:4:5:6:7:8:9"); sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
man_path = xstrdup(getenv("MANPATH")); alloc_mp = 10;
man_path_list = xmalloc(10 * sizeof(man_path_list[0]));
count_mp = 0;
man_path_list[0] = xstrdup(getenv("MANPATH"));
if (man_path_list[0])
count_mp++;
pager = getenv("MANPAGER"); pager = getenv("MANPAGER");
if (!pager) { if (!pager) {
pager = getenv("PAGER"); pager = getenv("PAGER");
@ -98,8 +105,13 @@ int man_main(int argc ATTRIBUTE_UNUSED, char **argv)
value = skip_whitespace(&line[8]); value = skip_whitespace(&line[8]);
*skip_non_whitespace(value) = '\0'; *skip_non_whitespace(value) = '\0';
if (strcmp("MANPATH", line) == 0) { if (strcmp("MANPATH", line) == 0) {
free(man_path); man_path_list[count_mp] = xstrdup(value);
man_path = xstrdup(value); count_mp++;
if (alloc_mp == count_mp) {
alloc_mp += 10;
man_path_list = xrealloc(man_path_list, alloc_mp * sizeof(man_path_list[0]));
}
/* thus man_path_list is always NULL terminated */
} }
if (strcmp("MANSECT", line) == 0) { if (strcmp("MANSECT", line) == 0) {
free(sec_list); free(sec_list);
@ -112,11 +124,12 @@ int man_main(int argc ATTRIBUTE_UNUSED, char **argv)
} }
do { /* for each argv[] */ do { /* for each argv[] */
cur_path = man_path; cur_mp = 0;
while ((cur_path = man_path_list[cur_mp++]) != NULL) {
/* for each MANPATH */
do { /* for each MANPATH item */ do { /* for each MANPATH item */
char *next_path = strchrnul(cur_path, ':'); char *next_path = strchrnul(cur_path, ':');
int path_len = next_path - cur_path; int path_len = next_path - cur_path;
cur_sect = sec_list; cur_sect = sec_list;
do { /* for each section */ do { /* for each section */
char *next_sect = strchrnul(cur_sect, ':'); char *next_sect = strchrnul(cur_sect, ':');
@ -127,22 +140,19 @@ int man_main(int argc ATTRIBUTE_UNUSED, char **argv)
sect_len, cur_sect, sect_len, cur_sect,
*argv, *argv,
sect_len, cur_sect); sect_len, cur_sect);
if (show_manpage(pager, man_filename)) { int found = show_manpage(pager, man_filename);
if (!(opt & OPT_a)) {
goto next_arg;
}
}
free(man_filename); free(man_filename);
if (found && !(opt & OPT_a))
goto next_arg;
cur_sect = next_sect; cur_sect = next_sect;
while (*cur_sect == ':') while (*cur_sect == ':')
cur_sect++; cur_sect++;
} while (*cur_sect); } while (*cur_sect);
cur_path = next_path; cur_path = next_path;
while (*cur_path == ':') while (*cur_path == ':')
cur_path++; cur_path++;
} while (*cur_path); } while (*cur_path);
}
next_arg: next_arg:
argv++; argv++;
} while (*argv); } while (*argv);