=================================================================== RCS file: /cvs/mandoc/main.c,v retrieving revision 1.338 retrieving revision 1.344 diff -u -p -r1.338 -r1.344 --- mandoc/main.c 2019/07/28 18:23:17 1.338 +++ mandoc/main.c 2020/02/24 21:16:31 1.344 @@ -1,7 +1,7 @@ -/* $Id: main.c,v 1.338 2019/07/28 18:23:17 schwarze Exp $ */ +/* $Id: main.c,v 1.344 2020/02/24 21:16:31 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons - * Copyright (c) 2010-2012, 2014-2019 Ingo Schwarze + * Copyright (c) 2010-2012, 2014-2020 Ingo Schwarze * Copyright (c) 2010 Joerg Sonnenberger * * Permission to use, copy, modify, and distribute this software for any @@ -32,6 +32,7 @@ #include #include #include +#include #if HAVE_SANDBOX_INIT #include #endif @@ -95,8 +96,9 @@ static int fs_lookup(const struct manpaths *, const char *, const char *, struct manpage **, size_t *); static int fs_search(const struct mansearch *, - const struct manpaths *, int, char**, + const struct manpaths *, const char *, struct manpage **, size_t *); +static void glob_esc(char **, const char *, const char *); static void outdata_alloc(struct outstate *, struct manoutput *); static void parse(struct mparse *, int, const char *, struct outstate *, struct manoutput *); @@ -325,7 +327,8 @@ main(int argc, char *argv[]) /* Postprocess options. */ - if (outmode == OUTMODE_DEF) { + switch (outmode) { + case OUTMODE_DEF: switch (search.argmode) { case ARG_FILE: outmode = OUTMODE_ALL; @@ -338,6 +341,16 @@ main(int argc, char *argv[]) outmode = OUTMODE_LST; break; } + break; + case OUTMODE_FLN: + if (search.argmode == ARG_FILE) + outmode = OUTMODE_ALL; + break; + case OUTMODE_ALL: + break; + case OUTMODE_LST: + case OUTMODE_ONE: + abort(); } if (oarg != NULL) { @@ -436,19 +449,42 @@ main(int argc, char *argv[]) /* man(1): Resolve each name individually. */ if (search.argmode == ARG_NAME) { - if (argc < 1) - usage(ARG_NAME); + if (argc < 1) { + if (outmode != OUTMODE_FLN) + usage(ARG_NAME); + if (conf.manpath.sz == 0) { + warnx("The manpath is empty."); + mandoc_msg_setrc(MANDOCLEVEL_BADARG); + } else { + for (i = 0; i + 1 < conf.manpath.sz; i++) + printf("%s:", conf.manpath.paths[i]); + printf("%s\n", conf.manpath.paths[i]); + } + manconf_free(&conf); + return (int)mandoc_msg_getrc(); + } for (res = NULL, ressz = 0; argc > 0; argc--, argv++) { (void)mansearch(&search, &conf.manpath, 1, argv, &resn, &resnsz); if (resnsz == 0) (void)fs_search(&search, &conf.manpath, - 1, argv, &resn, &resnsz); + *argv, &resn, &resnsz); + if (resnsz == 0 && strchr(*argv, '/') == NULL) { + if (search.arch != NULL && + arch_valid(search.arch, OSENUM) == 0) + warnx("Unknown architecture \"%s\".", + search.arch); + else if (search.sec != NULL) + warnx("No entry for %s in " + "section %s of the manual.", + *argv, search.sec); + else + warnx("No entry for %s in " + "the manual.", *argv); + mandoc_msg_setrc(MANDOCLEVEL_BADARG); + continue; + } if (resnsz == 0) { - if (strchr(*argv, '/') == NULL) { - mandoc_msg_setrc(MANDOCLEVEL_BADARG); - continue; - } if (access(*argv, R_OK) == -1) { mandoc_msg_setinfilename(*argv); mandoc_msg(MANDOCERR_BADARG_BAD, @@ -544,22 +580,6 @@ main(int argc, char *argv[]) break; } -#if HAVE_PLEDGE - if (outst.use_pager) { - if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1) { - mandoc_msg(MANDOCERR_PLEDGE, 0, 0, - "%s", strerror(errno)); - return mandoc_msg_getrc(); - } - } else { - if (pledge("stdio rpath", NULL) == -1) { - mandoc_msg(MANDOCERR_PLEDGE, 0, 0, - "%s", strerror(errno)); - return mandoc_msg_getrc(); - } - } -#endif - if (search.argmode == ARG_FILE && auxpaths != NULL) { if (strcmp(auxpaths, "doc") == 0) options |= MPARSE_MDOC; @@ -655,6 +675,18 @@ usage(enum argmode argmode) exit((int)MANDOCLEVEL_BADARG); } +static void +glob_esc(char **dst, const char *src, const char *suffix) +{ + while (*src != '\0') { + if (strchr("*?[", *src) != NULL) + *(*dst)++ = '\\'; + *(*dst)++ = *src++; + } + while (*suffix != '\0') + *(*dst)++ = *suffix++; +} + static int fs_lookup(const struct manpaths *paths, size_t ipath, const char *sec, const char *arch, const char *name, @@ -663,10 +695,14 @@ fs_lookup(const struct manpaths *paths, size_t ipath, struct stat sb; glob_t globinfo; struct manpage *page; - char *file; + char *file, *cp; int globres; enum form form; + const char *const slman = "/man"; + const char *const slash = "/"; + const char *const sglob = ".[01-9]*"; + form = FORM_SRC; mandoc_asprintf(&file, "%s/man%s/%s.%s", paths->paths[ipath], sec, name, sec); @@ -690,8 +726,13 @@ fs_lookup(const struct manpaths *paths, size_t ipath, free(file); } - mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*", - paths->paths[ipath], sec, name); + cp = file = mandoc_malloc(strlen(paths->paths[ipath]) * 2 + + strlen(slman) + strlen(sec) * 2 + strlen(slash) + + strlen(name) * 2 + strlen(sglob) + 1); + glob_esc(&cp, paths->paths[ipath], slman); + glob_esc(&cp, sec, slash); + glob_esc(&cp, name, sglob); + *cp = '\0'; globres = glob(file, 0, NULL, &globinfo); if (globres != 0 && globres != GLOB_NOMATCH) mandoc_msg(MANDOCERR_GLOB, 0, 0, @@ -734,48 +775,30 @@ found: static int fs_search(const struct mansearch *cfg, const struct manpaths *paths, - int argc, char **argv, struct manpage **res, size_t *ressz) + const char *name, struct manpage **res, size_t *ressz) { const char *const sections[] = {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"}; const size_t nsec = sizeof(sections)/sizeof(sections[0]); - size_t ipath, isec, lastsz; + size_t ipath, isec; assert(cfg->argmode == ARG_NAME); - if (res != NULL) *res = NULL; - *ressz = lastsz = 0; - while (argc) { - for (ipath = 0; ipath < paths->sz; ipath++) { - if (cfg->sec != NULL) { - if (fs_lookup(paths, ipath, cfg->sec, - cfg->arch, *argv, res, ressz) != -1 && - cfg->firstmatch) - return 0; - } else for (isec = 0; isec < nsec; isec++) + *ressz = 0; + for (ipath = 0; ipath < paths->sz; ipath++) { + if (cfg->sec != NULL) { + if (fs_lookup(paths, ipath, cfg->sec, cfg->arch, + name, res, ressz) != -1 && cfg->firstmatch) + return 0; + } else { + for (isec = 0; isec < nsec; isec++) if (fs_lookup(paths, ipath, sections[isec], - cfg->arch, *argv, res, ressz) != -1 && + cfg->arch, name, res, ressz) != -1 && cfg->firstmatch) return 0; } - if (res != NULL && *ressz == lastsz && - strchr(*argv, '/') == NULL) { - if (cfg->arch != NULL && - arch_valid(cfg->arch, OSENUM) == 0) - warnx("Unknown architecture \"%s\".", - cfg->arch); - else if (cfg->sec == NULL) - warnx("No entry for %s in the manual.", - *argv); - else - warnx("No entry for %s in section %s " - "of the manual.", *argv, cfg->sec); - } - lastsz = *ressz; - argv++; - argc--; } return -1; } @@ -950,7 +973,7 @@ check_xr(void) search.firstmatch = 1; if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz)) continue; - if (fs_search(&search, &paths, 1, &xr->name, NULL, &sz) != -1) + if (fs_search(&search, &paths, xr->name, NULL, &sz) != -1) continue; if (xr->count == 1) mandoc_msg(MANDOCERR_XR_BAD, xr->line,