+ /* man(1), whatis(1), apropos(1) */
+
+ if (search.argmode != ARG_FILE) {
+ if (search.argmode == ARG_NAME &&
+ outmode == OUTMODE_ONE)
+ search.firstmatch = 1;
+
+ /* Access the mandoc database. */
+
+ manconf_parse(&conf, conf_file, defpaths, auxpaths);
+ if ( ! mansearch(&search, &conf.manpath,
+ argc, argv, &res, &sz))
+ usage(search.argmode);
+
+ if (sz == 0 && search.argmode == ARG_NAME)
+ fs_search(&search, &conf.manpath,
+ argc, argv, &res, &sz);
+
+ if (search.argmode == ARG_NAME) {
+ for (c = 0; c < argc; c++) {
+ if (strchr(argv[c], '/') == NULL)
+ continue;
+ if (access(argv[c], R_OK) == -1) {
+ warn("%s", argv[c]);
+ continue;
+ }
+ res = mandoc_reallocarray(res,
+ sz + 1, sizeof(*res));
+ res[sz].file = mandoc_strdup(argv[c]);
+ res[sz].names = NULL;
+ res[sz].output = NULL;
+ res[sz].ipath = SIZE_MAX;
+ res[sz].bits = 0;
+ res[sz].sec = 10;
+ res[sz].form = FORM_SRC;
+ sz++;
+ }
+ }
+
+ if (sz == 0) {
+ if (search.argmode != ARG_NAME)
+ warnx("nothing appropriate");
+ rc = MANDOCLEVEL_BADARG;
+ goto out;
+ }
+
+ /*
+ * For standard man(1) and -a output mode,
+ * prepare for copying filename pointers
+ * into the program parameter array.
+ */
+
+ if (outmode == OUTMODE_ONE) {
+ argc = 1;
+ best_prio = 20;
+ } else if (outmode == OUTMODE_ALL)
+ argc = (int)sz;
+
+ /* Iterate all matching manuals. */
+
+ resp = res;
+ for (i = 0; i < sz; i++) {
+ if (outmode == OUTMODE_FLN)
+ puts(res[i].file);
+ else if (outmode == OUTMODE_LST)
+ printf("%s - %s\n", res[i].names,
+ res[i].output == NULL ? "" :
+ res[i].output);
+ else if (outmode == OUTMODE_ONE) {
+ /* Search for the best section. */
+ sec = res[i].file;
+ sec += strcspn(sec, "123456789");
+ if (sec[0] == '\0')
+ continue;
+ prio = sec_prios[sec[0] - '1'];
+ if (sec[1] != '/')
+ prio += 10;
+ if (prio >= best_prio)
+ continue;
+ best_prio = prio;
+ resp = res + i;
+ }
+ }
+
+ /*
+ * For man(1), -a and -i output mode, fall through
+ * to the main mandoc(1) code iterating files
+ * and running the parsers on each of them.
+ */
+
+ if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
+ goto out;
+ }
+
+ /* mandoc(1) */
+
+#if HAVE_PLEDGE
+ if (use_pager) {
+ if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
+ err((int)MANDOCLEVEL_SYSERR, "pledge");
+ } else {
+ if (pledge("stdio rpath", NULL) == -1)
+ err((int)MANDOCLEVEL_SYSERR, "pledge");
+ }
+#endif
+
+ if (search.argmode == ARG_FILE)
+ moptions(&options, auxpaths);
+
+ mchars_alloc();
+ curp.mp = mparse_alloc(options, curp.mmin, mmsg,
+ curp.os_e, curp.os_s);
+
+ if (argc < 1) {
+ if (use_pager)
+ tag_files = tag_init();
+ parse(&curp, STDIN_FILENO, "<stdin>");
+ }
+
+ /*
+ * Remember the original working directory, if possible.
+ * This will be needed if some names on the command line
+ * are page names and some are relative file names.
+ * Do not error out if the current directory is not
+ * readable: Maybe it won't be needed after all.
+ */
+ startdir = open(".", O_RDONLY | O_DIRECTORY);
+
+ while (argc > 0) {
+
+ /*
+ * Changing directories is not needed in ARG_FILE mode.
+ * Do it on a best-effort basis. Even in case of
+ * failure, some functionality may still work.
+ */
+ if (resp != NULL) {
+ if (resp->ipath != SIZE_MAX)
+ (void)chdir(conf.manpath.paths[resp->ipath]);
+ else if (startdir != -1)
+ (void)fchdir(startdir);
+ }
+
+ fd = mparse_open(curp.mp, resp != NULL ? resp->file : *argv);
+ if (fd != -1) {
+ if (use_pager) {
+ tag_files = tag_init();
+ use_pager = 0;
+ }
+
+ if (resp == NULL)
+ parse(&curp, fd, *argv);
+ else if (resp->form == FORM_SRC)
+ parse(&curp, fd, resp->file);
+ else
+ passthrough(resp->file, fd,
+ conf.output.synopsisonly);
+
+ if (ferror(stdout)) {
+ if (tag_files != NULL) {
+ warn("%s", tag_files->ofn);
+ tag_unlink();
+ tag_files = NULL;
+ } else
+ warn("stdout");
+ rc = MANDOCLEVEL_SYSERR;
+ break;
+ }
+
+ if (argc > 1 && curp.outtype <= OUTT_UTF8) {
+ if (curp.outdata == NULL)
+ outdata_alloc(&curp);
+ terminal_sepline(curp.outdata);
+ }
+ } else if (rc < MANDOCLEVEL_ERROR)
+ rc = MANDOCLEVEL_ERROR;