man: accept a list of dirs in $MANPATH

function                                             old     new   delta
add_MANPATH                                            -     143    +143
man_main                                             852     731    -121
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/1 up/down: 143/-121)           Total: 22 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2014-11-27 00:40:08 +01:00
parent 04c1417602
commit ee41094b80

View File

@ -147,15 +147,49 @@ static int show_manpage(const char *pager, char *man_filename, int man, int leve
return run_pipe(pager, man_filename, man, level); return run_pipe(pager, man_filename, man, level);
} }
static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
{
if (path) while (*path) {
char *next_path;
char **path_element;
next_path = strchr(path, ':');
if (next_path) {
*next_path = '\0';
if (next_path++ == path) /* "::"? */
goto next;
}
/* Do we already have path? */
path_element = man_path_list;
if (path_element) while (*path_element) {
if (strcmp(*path_element, path) == 0)
goto skip;
path_element++;
}
man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
man_path_list[*count_mp] = xstrdup(path);
(*count_mp)++;
/* man_path_list is NULL terminated */
/* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
skip:
if (!next_path)
break;
next:
path = next_path;
}
return man_path_list;
}
int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int man_main(int argc UNUSED_PARAM, char **argv) int man_main(int argc UNUSED_PARAM, char **argv)
{ {
parser_t *parser; parser_t *parser;
const char *pager = ENABLE_LESS ? "less" : "more"; const char *pager = ENABLE_LESS ? "less" : "more";
char **man_path_list;
char *sec_list; char *sec_list;
char *cur_path, *cur_sect; char *cur_path, *cur_sect;
int count_mp, cur_mp; char **man_path_list;
int count_mp;
int cur_mp;
int opt, not_found; int opt, not_found;
char *token[2]; char *token[2];
@ -164,14 +198,20 @@ int man_main(int argc UNUSED_PARAM, char **argv)
argv += optind; argv += optind;
sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9"); sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
/* Last valid man_path_list[] is [0x10] */
count_mp = 0; count_mp = 0;
man_path_list = xzalloc(0x11 * sizeof(man_path_list[0])); man_path_list = add_MANPATH(NULL, &count_mp,
man_path_list[0] = getenv("MANPATH"); getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
if (!man_path_list[0]) /* default, may be overridden by /etc/man.conf */ );
if (!man_path_list) {
/* default, may be overridden by /etc/man.conf */
man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
man_path_list[0] = (char*)"/usr/man"; man_path_list[0] = (char*)"/usr/man";
else /* count_mp stays 0.
count_mp++; * Thus, man.conf will overwrite man_path_list[0]
* if a path is defined there.
*/
}
/* Parse man.conf[ig] or man_db.conf */ /* Parse man.conf[ig] or man_db.conf */
/* man version 1.6f uses man.config */ /* man version 1.6f uses man.config */
@ -193,35 +233,7 @@ int man_main(int argc UNUSED_PARAM, char **argv)
if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */ if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
|| strcmp("MANDATORY_MANPATH", token[0]) == 0 || strcmp("MANDATORY_MANPATH", token[0]) == 0
) { ) {
char *path = token[1]; man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
while (*path) {
char *next_path;
char **path_element;
next_path = strchr(path, ':');
if (next_path) {
*next_path = '\0';
if (next_path++ == path) /* "::"? */
goto next;
}
/* Do we already have path? */
path_element = man_path_list;
while (*path_element) {
if (strcmp(*path_element, path) == 0)
goto skip;
path_element++;
}
man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
man_path_list[count_mp] = xstrdup(path);
count_mp++;
/* man_path_list is NULL terminated */
/*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
skip:
if (!next_path)
break;
next:
path = next_path;
}
} }
if (strcmp("MANSECT", token[0]) == 0) { if (strcmp("MANSECT", token[0]) == 0) {
free(sec_list); free(sec_list);