Annotation of src/usr.bin/mandoc/main.c, Revision 1.149
1.149 ! schwarze 1: /* $OpenBSD: main.c,v 1.148 2015/07/21 03:26:02 schwarze Exp $ */
1.1 kristaps 2: /*
1.95 schwarze 3: * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.118 schwarze 4: * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.86 schwarze 5: * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
1.1 kristaps 6: *
7: * Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 10: *
1.134 schwarze 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.2 schwarze 12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.134 schwarze 13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.2 schwarze 14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 18: */
19:
1.95 schwarze 20: #include <sys/types.h>
1.122 schwarze 21: #include <sys/param.h> /* MACHINE */
1.129 schwarze 22: #include <sys/wait.h>
1.95 schwarze 23:
1.1 kristaps 24: #include <assert.h>
1.95 schwarze 25: #include <ctype.h>
26: #include <errno.h>
27: #include <fcntl.h>
1.127 schwarze 28: #include <glob.h>
1.131 schwarze 29: #include <signal.h>
1.1 kristaps 30: #include <stdio.h>
1.17 schwarze 31: #include <stdint.h>
1.1 kristaps 32: #include <stdlib.h>
33: #include <string.h>
34: #include <unistd.h>
35:
1.136 schwarze 36: #include "mandoc_aux.h"
1.30 schwarze 37: #include "mandoc.h"
1.136 schwarze 38: #include "roff.h"
1.1 kristaps 39: #include "mdoc.h"
40: #include "man.h"
1.144 schwarze 41: #include "tag.h"
1.136 schwarze 42: #include "main.h"
1.134 schwarze 43: #include "manconf.h"
1.95 schwarze 44: #include "mansearch.h"
45:
46: enum outmode {
47: OUTMODE_DEF = 0,
48: OUTMODE_FLN,
49: OUTMODE_LST,
50: OUTMODE_ALL,
51: OUTMODE_INT,
52: OUTMODE_ONE
53: };
1.1 kristaps 54:
1.139 schwarze 55: typedef void (*out_mdoc)(void *, const struct roff_man *);
56: typedef void (*out_man)(void *, const struct roff_man *);
1.1 kristaps 57: typedef void (*out_free)(void *);
58:
59: enum outt {
1.76 schwarze 60: OUTT_ASCII = 0, /* -Tascii */
1.77 schwarze 61: OUTT_LOCALE, /* -Tlocale */
62: OUTT_UTF8, /* -Tutf8 */
1.76 schwarze 63: OUTT_TREE, /* -Ttree */
1.78 schwarze 64: OUTT_MAN, /* -Tman */
1.76 schwarze 65: OUTT_HTML, /* -Thtml */
66: OUTT_LINT, /* -Tlint */
67: OUTT_PS, /* -Tps */
68: OUTT_PDF /* -Tpdf */
1.1 kristaps 69: };
70:
71: struct curparse {
1.76 schwarze 72: struct mparse *mp;
1.102 schwarze 73: struct mchars *mchars; /* character table */
1.75 schwarze 74: enum mandoclevel wlevel; /* ignore messages below this */
75: int wstop; /* stop after a file with a warning */
1.90 schwarze 76: enum outt outtype; /* which output to use */
1.33 schwarze 77: out_mdoc outmdoc; /* mdoc output ptr */
1.90 schwarze 78: out_man outman; /* man output ptr */
1.33 schwarze 79: out_free outfree; /* free output ptr */
80: void *outdata; /* data for output */
1.135 schwarze 81: struct manoutput *outopts; /* output options */
1.33 schwarze 82: };
83:
1.119 schwarze 84: static int fs_lookup(const struct manpaths *,
85: size_t ipath, const char *,
86: const char *, const char *,
87: struct manpage **, size_t *);
88: static void fs_search(const struct mansearch *,
89: const struct manpaths *, int, char**,
90: struct manpage **, size_t *);
1.103 schwarze 91: static int koptions(int *, char *);
1.79 schwarze 92: int mandocdb(int, char**);
1.87 schwarze 93: static int moptions(int *, char *);
1.76 schwarze 94: static void mmsg(enum mandocerr, enum mandoclevel,
95: const char *, int, int, const char *);
1.132 schwarze 96: static void parse(struct curparse *, int, const char *);
97: static void passthrough(const char *, int, int);
1.149 ! schwarze 98: static pid_t spawn_pager(struct tag_files *);
1.27 schwarze 99: static int toptions(struct curparse *, char *);
1.95 schwarze 100: static void usage(enum argmode) __attribute__((noreturn));
1.45 schwarze 101: static int woptions(struct curparse *, char *);
1.1 kristaps 102:
1.95 schwarze 103: static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
1.110 schwarze 104: static char help_arg[] = "help";
105: static char *help_argv[] = {help_arg, NULL};
1.19 schwarze 106: static const char *progname;
1.131 schwarze 107: static enum mandoclevel rc;
1.1 kristaps 108:
1.90 schwarze 109:
1.1 kristaps 110: int
111: main(int argc, char *argv[])
112: {
1.134 schwarze 113: struct manconf conf;
1.1 kristaps 114: struct curparse curp;
1.95 schwarze 115: struct mansearch search;
1.149 ! schwarze 116: struct tag_files *tag_files;
1.111 schwarze 117: char *auxpaths;
1.95 schwarze 118: char *defos;
1.113 schwarze 119: unsigned char *uc;
1.95 schwarze 120: struct manpage *res, *resp;
1.111 schwarze 121: char *conf_file, *defpaths;
1.95 schwarze 122: size_t isec, i, sz;
1.135 schwarze 123: int prio, best_prio;
1.95 schwarze 124: char sec;
1.131 schwarze 125: enum mandoclevel rctmp;
1.95 schwarze 126: enum outmode outmode;
1.100 schwarze 127: int fd;
1.95 schwarze 128: int show_usage;
1.87 schwarze 129: int options;
1.149 ! schwarze 130: int use_pager;
1.95 schwarze 131: int c;
1.1 kristaps 132:
1.124 schwarze 133: if (argc < 1)
134: progname = "mandoc";
135: else if ((progname = strrchr(argv[0], '/')) == NULL)
1.19 schwarze 136: progname = argv[0];
137: else
138: ++progname;
1.79 schwarze 139:
1.82 schwarze 140: if (0 == strncmp(progname, "mandocdb", 8) ||
141: 0 == strncmp(progname, "makewhatis", 10))
1.79 schwarze 142: return(mandocdb(argc, argv));
1.19 schwarze 143:
1.95 schwarze 144: /* Search options. */
145:
1.134 schwarze 146: memset(&conf, 0, sizeof(conf));
1.111 schwarze 147: conf_file = defpaths = NULL;
148: auxpaths = NULL;
1.95 schwarze 149:
150: memset(&search, 0, sizeof(struct mansearch));
151: search.outkey = "Nd";
152:
153: if (strcmp(progname, "man") == 0)
154: search.argmode = ARG_NAME;
155: else if (strncmp(progname, "apropos", 7) == 0)
156: search.argmode = ARG_EXPR;
157: else if (strncmp(progname, "whatis", 6) == 0)
158: search.argmode = ARG_WORD;
1.110 schwarze 159: else if (strncmp(progname, "help", 4) == 0)
160: search.argmode = ARG_NAME;
1.95 schwarze 161: else
162: search.argmode = ARG_FILE;
163:
164: /* Parser and formatter options. */
165:
1.19 schwarze 166: memset(&curp, 0, sizeof(struct curparse));
1.109 schwarze 167: curp.outtype = OUTT_LOCALE;
1.121 schwarze 168: curp.wlevel = MANDOCLEVEL_BADARG;
1.135 schwarze 169: curp.outopts = &conf.output;
1.103 schwarze 170: options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
1.83 schwarze 171: defos = NULL;
1.1 kristaps 172:
1.149 ! schwarze 173: use_pager = 1;
! 174: tag_files = NULL;
1.95 schwarze 175: show_usage = 0;
176: outmode = OUTMODE_DEF;
177:
1.103 schwarze 178: while (-1 != (c = getopt(argc, argv,
179: "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
1.1 kristaps 180: switch (c) {
1.95 schwarze 181: case 'a':
182: outmode = OUTMODE_ALL;
183: break;
184: case 'C':
185: conf_file = optarg;
186: break;
187: case 'c':
1.149 ! schwarze 188: use_pager = 0;
1.95 schwarze 189: break;
190: case 'f':
191: search.argmode = ARG_WORD;
192: break;
1.98 schwarze 193: case 'h':
1.135 schwarze 194: conf.output.synopsisonly = 1;
1.149 ! schwarze 195: use_pager = 0;
1.98 schwarze 196: outmode = OUTMODE_ALL;
197: break;
1.90 schwarze 198: case 'I':
1.83 schwarze 199: if (strncmp(optarg, "os=", 3)) {
1.90 schwarze 200: fprintf(stderr,
1.115 schwarze 201: "%s: -I %s: Bad argument\n",
1.93 schwarze 202: progname, optarg);
1.83 schwarze 203: return((int)MANDOCLEVEL_BADARG);
204: }
205: if (defos) {
1.90 schwarze 206: fprintf(stderr,
1.115 schwarze 207: "%s: -I %s: Duplicate argument\n",
1.93 schwarze 208: progname, optarg);
1.83 schwarze 209: return((int)MANDOCLEVEL_BADARG);
210: }
211: defos = mandoc_strdup(optarg + 3);
212: break;
1.95 schwarze 213: case 'i':
214: outmode = OUTMODE_INT;
215: break;
1.103 schwarze 216: case 'K':
217: if ( ! koptions(&options, optarg))
218: return((int)MANDOCLEVEL_BADARG);
219: break;
1.95 schwarze 220: case 'k':
221: search.argmode = ARG_EXPR;
222: break;
1.96 schwarze 223: case 'l':
224: search.argmode = ARG_FILE;
225: outmode = OUTMODE_ALL;
226: break;
1.95 schwarze 227: case 'M':
228: defpaths = optarg;
229: break;
1.90 schwarze 230: case 'm':
1.95 schwarze 231: auxpaths = optarg;
1.1 kristaps 232: break;
1.90 schwarze 233: case 'O':
1.95 schwarze 234: search.outkey = optarg;
1.135 schwarze 235: while (optarg != NULL)
236: manconf_output(&conf.output,
237: strsep(&optarg, ","));
1.17 schwarze 238: break;
1.95 schwarze 239: case 'S':
240: search.arch = optarg;
241: break;
242: case 's':
243: search.sec = optarg;
244: break;
1.90 schwarze 245: case 'T':
1.22 schwarze 246: if ( ! toptions(&curp, optarg))
1.47 schwarze 247: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 248: break;
1.90 schwarze 249: case 'W':
1.45 schwarze 250: if ( ! woptions(&curp, optarg))
1.47 schwarze 251: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 252: break;
1.95 schwarze 253: case 'w':
254: outmode = OUTMODE_FLN;
255: break;
1.1 kristaps 256: default:
1.95 schwarze 257: show_usage = 1;
258: break;
259: }
260: }
261:
262: if (show_usage)
263: usage(search.argmode);
264:
265: /* Postprocess options. */
266:
267: if (outmode == OUTMODE_DEF) {
268: switch (search.argmode) {
269: case ARG_FILE:
270: outmode = OUTMODE_ALL;
1.149 ! schwarze 271: use_pager = 0;
1.95 schwarze 272: break;
273: case ARG_NAME:
274: outmode = OUTMODE_ONE;
275: break;
276: default:
277: outmode = OUTMODE_LST;
278: break;
279: }
280: }
281:
282: /* Parse arguments. */
283:
1.124 schwarze 284: if (argc > 0) {
285: argc -= optind;
286: argv += optind;
287: }
1.95 schwarze 288: resp = NULL;
289:
1.110 schwarze 290: /*
291: * Quirks for help(1)
292: * and for a man(1) section argument without -s.
293: */
294:
295: if (search.argmode == ARG_NAME) {
296: if (*progname == 'h') {
297: if (argc == 0) {
298: argv = help_argv;
299: argc = 1;
300: }
1.117 schwarze 301: } else if (argc > 1 &&
1.128 schwarze 302: ((uc = (unsigned char *)argv[0]) != NULL) &&
1.113 schwarze 303: ((isdigit(uc[0]) && (uc[1] == '\0' ||
304: (isalpha(uc[1]) && uc[2] == '\0'))) ||
305: (uc[0] == 'n' && uc[1] == '\0'))) {
1.128 schwarze 306: search.sec = (char *)uc;
1.110 schwarze 307: argv++;
308: argc--;
309: }
1.122 schwarze 310: if (search.arch == NULL)
311: search.arch = getenv("MACHINE");
312: if (search.arch == NULL)
313: search.arch = MACHINE;
1.95 schwarze 314: }
315:
316: rc = MANDOCLEVEL_OK;
317:
318: /* man(1), whatis(1), apropos(1) */
319:
320: if (search.argmode != ARG_FILE) {
321: if (argc == 0)
322: usage(search.argmode);
1.107 schwarze 323:
324: if (search.argmode == ARG_NAME &&
325: outmode == OUTMODE_ONE)
326: search.firstmatch = 1;
1.95 schwarze 327:
328: /* Access the mandoc database. */
329:
1.134 schwarze 330: manconf_parse(&conf, conf_file, defpaths, auxpaths);
1.95 schwarze 331: mansearch_setup(1);
1.134 schwarze 332: if ( ! mansearch(&search, &conf.manpath,
333: argc, argv, &res, &sz))
1.95 schwarze 334: usage(search.argmode);
1.119 schwarze 335:
1.142 schwarze 336: if (sz == 0) {
337: if (search.argmode == ARG_NAME)
338: fs_search(&search, &conf.manpath,
339: argc, argv, &res, &sz);
340: else
341: fprintf(stderr,
342: "%s: nothing appropriate\n",
343: progname);
344: }
1.95 schwarze 345:
346: if (sz == 0) {
347: rc = MANDOCLEVEL_BADARG;
348: goto out;
349: }
350:
351: /*
352: * For standard man(1) and -a output mode,
353: * prepare for copying filename pointers
354: * into the program parameter array.
355: */
356:
357: if (outmode == OUTMODE_ONE) {
358: argc = 1;
359: best_prio = 10;
360: } else if (outmode == OUTMODE_ALL)
361: argc = (int)sz;
362:
363: /* Iterate all matching manuals. */
364:
1.119 schwarze 365: resp = res;
1.95 schwarze 366: for (i = 0; i < sz; i++) {
367: if (outmode == OUTMODE_FLN)
368: puts(res[i].file);
369: else if (outmode == OUTMODE_LST)
370: printf("%s - %s\n", res[i].names,
371: res[i].output == NULL ? "" :
372: res[i].output);
373: else if (outmode == OUTMODE_ONE) {
374: /* Search for the best section. */
375: isec = strcspn(res[i].file, "123456789");
376: sec = res[i].file[isec];
377: if ('\0' == sec)
378: continue;
379: prio = sec_prios[sec - '1'];
380: if (prio >= best_prio)
381: continue;
382: best_prio = prio;
383: resp = res + i;
384: }
1.1 kristaps 385: }
386:
1.95 schwarze 387: /*
388: * For man(1), -a and -i output mode, fall through
389: * to the main mandoc(1) code iterating files
390: * and running the parsers on each of them.
391: */
392:
393: if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
394: goto out;
395: }
396:
397: /* mandoc(1) */
398:
1.115 schwarze 399: if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
1.95 schwarze 400: return((int)MANDOCLEVEL_BADARG);
401:
1.149 ! schwarze 402: if (use_pager && ! isatty(STDOUT_FILENO))
! 403: use_pager = 0;
1.146 schwarze 404:
1.102 schwarze 405: curp.mchars = mchars_alloc();
406: curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
407: curp.mchars, defos);
1.76 schwarze 408:
1.80 schwarze 409: /*
410: * Conditionally start up the lookaside buffer before parsing.
411: */
412: if (OUTT_MAN == curp.outtype)
413: mparse_keep(curp.mp);
414:
1.124 schwarze 415: if (argc < 1) {
1.149 ! schwarze 416: if (use_pager)
! 417: tag_files = tag_init();
1.132 schwarze 418: parse(&curp, STDIN_FILENO, "<stdin>");
1.118 schwarze 419: }
1.27 schwarze 420:
1.124 schwarze 421: while (argc > 0) {
1.120 schwarze 422: rctmp = mparse_open(curp.mp, &fd,
1.118 schwarze 423: resp != NULL ? resp->file : *argv);
1.120 schwarze 424: if (rc < rctmp)
425: rc = rctmp;
1.118 schwarze 426:
427: if (fd != -1) {
1.149 ! schwarze 428: if (use_pager) {
! 429: tag_files = tag_init();
! 430: use_pager = 0;
! 431: }
1.118 schwarze 432:
433: if (resp == NULL)
1.132 schwarze 434: parse(&curp, fd, *argv);
1.100 schwarze 435: else if (resp->form & FORM_SRC) {
1.97 schwarze 436: /* For .so only; ignore failure. */
1.134 schwarze 437: chdir(conf.manpath.paths[resp->ipath]);
1.132 schwarze 438: parse(&curp, fd, resp->file);
439: } else
1.135 schwarze 440: passthrough(resp->file, fd,
441: conf.output.synopsisonly);
1.118 schwarze 442:
443: if (argc > 1 && curp.outtype <= OUTT_UTF8)
444: ascii_sepline(curp.outdata);
1.100 schwarze 445: }
446:
1.76 schwarze 447: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.27 schwarze 448: break;
1.116 schwarze 449:
1.118 schwarze 450: if (resp != NULL)
451: resp++;
452: else
453: argv++;
454: if (--argc)
455: mparse_reset(curp.mp);
1.1 kristaps 456: }
457:
458: if (curp.outfree)
459: (*curp.outfree)(curp.outdata);
1.102 schwarze 460: mparse_free(curp.mp);
461: mchars_free(curp.mchars);
1.95 schwarze 462:
463: out:
464: if (search.argmode != ARG_FILE) {
1.134 schwarze 465: manconf_free(&conf);
1.95 schwarze 466: mansearch_free(res, sz);
467: mansearch_setup(0);
468: }
469:
1.83 schwarze 470: free(defos);
1.1 kristaps 471:
1.129 schwarze 472: /*
1.149 ! schwarze 473: * When using a pager, finish writing both temporary files,
! 474: * fork it, wait for the user to close it, and clean up.
1.129 schwarze 475: */
476:
1.149 ! schwarze 477: if (tag_files != NULL) {
1.130 schwarze 478: fclose(stdout);
1.144 schwarze 479: tag_write();
1.149 ! schwarze 480: waitpid(spawn_pager(tag_files), NULL, 0);
1.144 schwarze 481: tag_unlink();
1.130 schwarze 482: }
1.129 schwarze 483:
1.76 schwarze 484: return((int)rc);
1.1 kristaps 485: }
486:
1.20 schwarze 487: static void
1.95 schwarze 488: usage(enum argmode argmode)
1.1 kristaps 489: {
490:
1.95 schwarze 491: switch (argmode) {
492: case ARG_FILE:
1.133 schwarze 493: fputs("usage: mandoc [-acfhkl] [-I os=name] "
494: "[-K encoding] [-mformat] [-O option]\n"
495: "\t [-T output] [-W level] [file ...]\n", stderr);
1.95 schwarze 496: break;
497: case ARG_NAME:
1.126 schwarze 498: fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
1.114 jmc 499: "[-K encoding] [-M path] [-m path]\n"
500: "\t [-O option=value] [-S subsection] [-s section] "
501: "[-T output] [-W level]\n"
1.95 schwarze 502: "\t [section] name ...\n", stderr);
503: break;
504: case ARG_WORD:
1.126 schwarze 505: fputs("usage: whatis [-acfhklw] [-C file] "
1.96 schwarze 506: "[-M path] [-m path] [-O outkey] [-S arch]\n"
507: "\t [-s section] name ...\n", stderr);
1.95 schwarze 508: break;
509: case ARG_EXPR:
1.126 schwarze 510: fputs("usage: apropos [-acfhklw] [-C file] "
1.96 schwarze 511: "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.95 schwarze 512: "\t [-s section] expression ...\n", stderr);
513: break;
514: }
1.47 schwarze 515: exit((int)MANDOCLEVEL_BADARG);
1.119 schwarze 516: }
517:
518: static int
519: fs_lookup(const struct manpaths *paths, size_t ipath,
520: const char *sec, const char *arch, const char *name,
521: struct manpage **res, size_t *ressz)
522: {
1.127 schwarze 523: glob_t globinfo;
1.119 schwarze 524: struct manpage *page;
525: char *file;
1.127 schwarze 526: int form, globres;
1.119 schwarze 527:
1.127 schwarze 528: form = FORM_SRC;
1.119 schwarze 529: mandoc_asprintf(&file, "%s/man%s/%s.%s",
530: paths->paths[ipath], sec, name, sec);
1.127 schwarze 531: if (access(file, R_OK) != -1)
1.119 schwarze 532: goto found;
533: free(file);
534:
535: mandoc_asprintf(&file, "%s/cat%s/%s.0",
536: paths->paths[ipath], sec, name);
537: if (access(file, R_OK) != -1) {
538: form = FORM_CAT;
539: goto found;
540: }
541: free(file);
542:
543: if (arch != NULL) {
544: mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
545: paths->paths[ipath], sec, arch, name, sec);
1.127 schwarze 546: if (access(file, R_OK) != -1)
1.119 schwarze 547: goto found;
548: free(file);
549: }
1.127 schwarze 550:
1.145 schwarze 551: mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
1.127 schwarze 552: paths->paths[ipath], sec, name);
553: globres = glob(file, 0, NULL, &globinfo);
554: if (globres != 0 && globres != GLOB_NOMATCH)
555: fprintf(stderr, "%s: %s: glob: %s\n",
556: progname, file, strerror(errno));
557: free(file);
558: if (globres == 0)
559: file = mandoc_strdup(*globinfo.gl_pathv);
560: globfree(&globinfo);
561: if (globres != 0)
562: return(0);
1.119 schwarze 563:
564: found:
1.138 schwarze 565: fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry, run "
566: "makewhatis %s\n", progname, name, sec, paths->paths[ipath]);
1.119 schwarze 567: *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
568: page = *res + (*ressz - 1);
569: page->file = file;
570: page->names = NULL;
571: page->output = NULL;
572: page->ipath = ipath;
573: page->bits = NAME_FILE & NAME_MASK;
574: page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
575: page->form = form;
576: return(1);
577: }
578:
579: static void
580: fs_search(const struct mansearch *cfg, const struct manpaths *paths,
581: int argc, char **argv, struct manpage **res, size_t *ressz)
582: {
583: const char *const sections[] =
584: {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
585: const size_t nsec = sizeof(sections)/sizeof(sections[0]);
586:
587: size_t ipath, isec, lastsz;
588:
589: assert(cfg->argmode == ARG_NAME);
590:
591: *res = NULL;
592: *ressz = lastsz = 0;
593: while (argc) {
594: for (ipath = 0; ipath < paths->sz; ipath++) {
595: if (cfg->sec != NULL) {
596: if (fs_lookup(paths, ipath, cfg->sec,
597: cfg->arch, *argv, res, ressz) &&
598: cfg->firstmatch)
599: return;
600: } else for (isec = 0; isec < nsec; isec++)
601: if (fs_lookup(paths, ipath, sections[isec],
602: cfg->arch, *argv, res, ressz) &&
603: cfg->firstmatch)
604: return;
605: }
606: if (*ressz == lastsz)
607: fprintf(stderr,
608: "%s: No entry for %s in the manual.\n",
609: progname, *argv);
610: lastsz = *ressz;
611: argv++;
612: argc--;
613: }
1.1 kristaps 614: }
615:
1.27 schwarze 616: static void
1.132 schwarze 617: parse(struct curparse *curp, int fd, const char *file)
1.52 schwarze 618: {
1.132 schwarze 619: enum mandoclevel rctmp;
1.139 schwarze 620: struct roff_man *man;
1.52 schwarze 621:
1.76 schwarze 622: /* Begin by parsing the file itself. */
1.52 schwarze 623:
1.76 schwarze 624: assert(file);
625: assert(fd >= -1);
1.52 schwarze 626:
1.132 schwarze 627: rctmp = mparse_readfd(curp->mp, fd, file);
628: if (rc < rctmp)
629: rc = rctmp;
1.1 kristaps 630:
1.51 schwarze 631: /*
1.76 schwarze 632: * With -Wstop and warnings or errors of at least the requested
633: * level, do not produce output.
1.51 schwarze 634: */
635:
1.132 schwarze 636: if (rctmp != MANDOCLEVEL_OK && curp->wstop)
637: return;
1.51 schwarze 638:
639: /* If unset, allocate output dev now (if applicable). */
640:
641: if ( ! (curp->outman && curp->outmdoc)) {
642: switch (curp->outtype) {
1.90 schwarze 643: case OUTT_HTML:
1.102 schwarze 644: curp->outdata = html_alloc(curp->mchars,
645: curp->outopts);
1.77 schwarze 646: curp->outfree = html_free;
647: break;
1.90 schwarze 648: case OUTT_UTF8:
1.102 schwarze 649: curp->outdata = utf8_alloc(curp->mchars,
650: curp->outopts);
1.77 schwarze 651: curp->outfree = ascii_free;
652: break;
1.90 schwarze 653: case OUTT_LOCALE:
1.102 schwarze 654: curp->outdata = locale_alloc(curp->mchars,
655: curp->outopts);
1.77 schwarze 656: curp->outfree = ascii_free;
1.51 schwarze 657: break;
1.90 schwarze 658: case OUTT_ASCII:
1.102 schwarze 659: curp->outdata = ascii_alloc(curp->mchars,
660: curp->outopts);
1.51 schwarze 661: curp->outfree = ascii_free;
662: break;
1.90 schwarze 663: case OUTT_PDF:
1.102 schwarze 664: curp->outdata = pdf_alloc(curp->mchars,
665: curp->outopts);
1.51 schwarze 666: curp->outfree = pspdf_free;
667: break;
1.90 schwarze 668: case OUTT_PS:
1.102 schwarze 669: curp->outdata = ps_alloc(curp->mchars,
670: curp->outopts);
1.51 schwarze 671: curp->outfree = pspdf_free;
672: break;
673: default:
674: break;
675: }
676:
677: switch (curp->outtype) {
1.90 schwarze 678: case OUTT_HTML:
1.51 schwarze 679: curp->outman = html_man;
680: curp->outmdoc = html_mdoc;
681: break;
1.90 schwarze 682: case OUTT_TREE:
1.51 schwarze 683: curp->outman = tree_man;
684: curp->outmdoc = tree_mdoc;
685: break;
1.90 schwarze 686: case OUTT_MAN:
1.78 schwarze 687: curp->outmdoc = man_mdoc;
1.80 schwarze 688: curp->outman = man_man;
1.78 schwarze 689: break;
1.90 schwarze 690: case OUTT_PDF:
1.51 schwarze 691: /* FALLTHROUGH */
1.90 schwarze 692: case OUTT_ASCII:
1.51 schwarze 693: /* FALLTHROUGH */
1.90 schwarze 694: case OUTT_UTF8:
1.77 schwarze 695: /* FALLTHROUGH */
1.90 schwarze 696: case OUTT_LOCALE:
1.77 schwarze 697: /* FALLTHROUGH */
1.90 schwarze 698: case OUTT_PS:
1.51 schwarze 699: curp->outman = terminal_man;
700: curp->outmdoc = terminal_mdoc;
701: break;
702: default:
703: break;
704: }
705: }
706:
1.140 schwarze 707: mparse_result(curp->mp, &man, NULL);
1.76 schwarze 708:
1.51 schwarze 709: /* Execute the out device, if it exists. */
710:
1.140 schwarze 711: if (man == NULL)
712: return;
713: if (curp->outmdoc != NULL && man->macroset == MACROSET_MDOC)
714: (*curp->outmdoc)(curp->outdata, man);
715: if (curp->outman != NULL && man->macroset == MACROSET_MAN)
1.76 schwarze 716: (*curp->outman)(curp->outdata, man);
1.1 kristaps 717: }
718:
1.132 schwarze 719: static void
1.105 schwarze 720: passthrough(const char *file, int fd, int synopsis_only)
1.95 schwarze 721: {
1.105 schwarze 722: const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
723: const char synr[] = "SYNOPSIS";
724:
725: FILE *stream;
1.95 schwarze 726: const char *syscall;
1.105 schwarze 727: char *line;
728: size_t len, off;
729: ssize_t nw;
730: int print;
1.116 schwarze 731:
732: fflush(stdout);
1.105 schwarze 733:
734: if ((stream = fdopen(fd, "r")) == NULL) {
735: close(fd);
736: syscall = "fdopen";
737: goto fail;
738: }
1.95 schwarze 739:
1.105 schwarze 740: print = 0;
741: while ((line = fgetln(stream, &len)) != NULL) {
742: if (synopsis_only) {
743: if (print) {
744: if ( ! isspace((unsigned char)*line))
745: goto done;
746: while (len &&
747: isspace((unsigned char)*line)) {
748: line++;
749: len--;
750: }
751: } else {
752: if ((len == sizeof(synb) &&
753: ! strncmp(line, synb, len - 1)) ||
754: (len == sizeof(synr) &&
755: ! strncmp(line, synr, len - 1)))
756: print = 1;
757: continue;
758: }
759: }
760: for (off = 0; off < len; off += nw)
761: if ((nw = write(STDOUT_FILENO, line + off,
762: len - off)) == -1 || nw == 0) {
763: fclose(stream);
1.95 schwarze 764: syscall = "write";
765: goto fail;
766: }
1.105 schwarze 767: }
1.95 schwarze 768:
1.105 schwarze 769: if (ferror(stream)) {
770: fclose(stream);
771: syscall = "fgetln";
772: goto fail;
773: }
1.101 schwarze 774:
1.105 schwarze 775: done:
776: fclose(stream);
1.132 schwarze 777: return;
1.95 schwarze 778:
779: fail:
780: fprintf(stderr, "%s: %s: SYSERR: %s: %s",
781: progname, file, syscall, strerror(errno));
1.132 schwarze 782: if (rc < MANDOCLEVEL_SYSERR)
783: rc = MANDOCLEVEL_SYSERR;
1.103 schwarze 784: }
785:
786: static int
787: koptions(int *options, char *arg)
788: {
789:
790: if ( ! strcmp(arg, "utf-8")) {
791: *options |= MPARSE_UTF8;
792: *options &= ~MPARSE_LATIN1;
793: } else if ( ! strcmp(arg, "iso-8859-1")) {
794: *options |= MPARSE_LATIN1;
795: *options &= ~MPARSE_UTF8;
796: } else if ( ! strcmp(arg, "us-ascii")) {
797: *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
798: } else {
1.115 schwarze 799: fprintf(stderr, "%s: -K %s: Bad argument\n",
1.103 schwarze 800: progname, arg);
801: return(0);
802: }
803: return(1);
1.95 schwarze 804: }
805:
1.1 kristaps 806: static int
1.87 schwarze 807: moptions(int *options, char *arg)
1.1 kristaps 808: {
809:
1.95 schwarze 810: if (arg == NULL)
811: /* nothing to do */;
812: else if (0 == strcmp(arg, "doc"))
1.87 schwarze 813: *options |= MPARSE_MDOC;
1.1 kristaps 814: else if (0 == strcmp(arg, "andoc"))
1.87 schwarze 815: /* nothing to do */;
1.1 kristaps 816: else if (0 == strcmp(arg, "an"))
1.87 schwarze 817: *options |= MPARSE_MAN;
1.1 kristaps 818: else {
1.115 schwarze 819: fprintf(stderr, "%s: -m %s: Bad argument\n",
1.93 schwarze 820: progname, arg);
1.1 kristaps 821: return(0);
822: }
823:
824: return(1);
825: }
826:
827: static int
1.22 schwarze 828: toptions(struct curparse *curp, char *arg)
1.1 kristaps 829: {
830:
831: if (0 == strcmp(arg, "ascii"))
1.22 schwarze 832: curp->outtype = OUTT_ASCII;
833: else if (0 == strcmp(arg, "lint")) {
834: curp->outtype = OUTT_LINT;
1.45 schwarze 835: curp->wlevel = MANDOCLEVEL_WARNING;
1.76 schwarze 836: } else if (0 == strcmp(arg, "tree"))
1.22 schwarze 837: curp->outtype = OUTT_TREE;
1.78 schwarze 838: else if (0 == strcmp(arg, "man"))
839: curp->outtype = OUTT_MAN;
1.17 schwarze 840: else if (0 == strcmp(arg, "html"))
1.22 schwarze 841: curp->outtype = OUTT_HTML;
1.77 schwarze 842: else if (0 == strcmp(arg, "utf8"))
843: curp->outtype = OUTT_UTF8;
844: else if (0 == strcmp(arg, "locale"))
845: curp->outtype = OUTT_LOCALE;
1.21 schwarze 846: else if (0 == strcmp(arg, "xhtml"))
1.102 schwarze 847: curp->outtype = OUTT_HTML;
1.36 schwarze 848: else if (0 == strcmp(arg, "ps"))
849: curp->outtype = OUTT_PS;
1.43 schwarze 850: else if (0 == strcmp(arg, "pdf"))
851: curp->outtype = OUTT_PDF;
1.1 kristaps 852: else {
1.115 schwarze 853: fprintf(stderr, "%s: -T %s: Bad argument\n",
1.93 schwarze 854: progname, arg);
1.1 kristaps 855: return(0);
856: }
857:
858: return(1);
859: }
860:
861: static int
1.45 schwarze 862: woptions(struct curparse *curp, char *arg)
1.1 kristaps 863: {
1.10 schwarze 864: char *v, *o;
1.123 schwarze 865: const char *toks[7];
1.1 kristaps 866:
1.45 schwarze 867: toks[0] = "stop";
868: toks[1] = "all";
869: toks[2] = "warning";
870: toks[3] = "error";
1.123 schwarze 871: toks[4] = "unsupp";
872: toks[5] = "fatal";
873: toks[6] = NULL;
1.1 kristaps 874:
1.10 schwarze 875: while (*arg) {
876: o = arg;
1.17 schwarze 877: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.90 schwarze 878: case 0:
1.45 schwarze 879: curp->wstop = 1;
1.1 kristaps 880: break;
1.90 schwarze 881: case 1:
1.45 schwarze 882: /* FALLTHROUGH */
1.90 schwarze 883: case 2:
1.45 schwarze 884: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 885: break;
1.90 schwarze 886: case 3:
1.45 schwarze 887: curp->wlevel = MANDOCLEVEL_ERROR;
1.1 kristaps 888: break;
1.90 schwarze 889: case 4:
1.123 schwarze 890: curp->wlevel = MANDOCLEVEL_UNSUPP;
891: break;
892: case 5:
1.121 schwarze 893: curp->wlevel = MANDOCLEVEL_BADARG;
1.19 schwarze 894: break;
1.1 kristaps 895: default:
1.115 schwarze 896: fprintf(stderr, "%s: -W %s: Bad argument\n",
1.93 schwarze 897: progname, o);
1.1 kristaps 898: return(0);
899: }
1.10 schwarze 900: }
1.1 kristaps 901:
902: return(1);
903: }
904:
1.75 schwarze 905: static void
1.90 schwarze 906: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.76 schwarze 907: const char *file, int line, int col, const char *msg)
1.30 schwarze 908: {
1.94 schwarze 909: const char *mparse_msg;
1.45 schwarze 910:
1.92 schwarze 911: fprintf(stderr, "%s: %s:", progname, file);
912:
913: if (line)
914: fprintf(stderr, "%d:%d:", line, col + 1);
915:
1.94 schwarze 916: fprintf(stderr, " %s", mparse_strlevel(lvl));
917:
918: if (NULL != (mparse_msg = mparse_strerror(t)))
919: fprintf(stderr, ": %s", mparse_msg);
1.30 schwarze 920:
921: if (msg)
922: fprintf(stderr, ": %s", msg);
1.76 schwarze 923:
1.30 schwarze 924: fputc('\n', stderr);
1.95 schwarze 925: }
926:
1.130 schwarze 927: static pid_t
1.149 ! schwarze 928: spawn_pager(struct tag_files *tag_files)
1.95 schwarze 929: {
930: #define MAX_PAGER_ARGS 16
931: char *argv[MAX_PAGER_ARGS];
932: const char *pager;
933: char *cp;
1.144 schwarze 934: size_t cmdlen;
1.95 schwarze 935: int argc;
1.130 schwarze 936: pid_t pager_pid;
1.95 schwarze 937:
1.144 schwarze 938: pager = getenv("MANPAGER");
939: if (pager == NULL || *pager == '\0')
940: pager = getenv("PAGER");
941: if (pager == NULL || *pager == '\0')
942: pager = "more -s";
943: cp = mandoc_strdup(pager);
944:
945: /*
946: * Parse the pager command into words.
947: * Intentionally do not do anything fancy here.
948: */
949:
950: argc = 0;
951: while (argc + 4 < MAX_PAGER_ARGS) {
952: argv[argc++] = cp;
953: cp = strchr(cp, ' ');
954: if (cp == NULL)
955: break;
956: *cp++ = '\0';
957: while (*cp == ' ')
958: cp++;
959: if (*cp == '\0')
960: break;
961: }
962:
1.149 ! schwarze 963: /* For more(1) and less(1), use the tag file. */
1.144 schwarze 964:
1.149 ! schwarze 965: if ((cmdlen = strlen(argv[0])) >= 4) {
1.144 schwarze 966: cp = argv[0] + cmdlen - 4;
1.149 ! schwarze 967: if (strcmp(cp, "less") == 0 || strcmp(cp, "more") == 0) {
! 968: argv[argc++] = mandoc_strdup("-T");
! 969: argv[argc++] = tag_files->tfn;
! 970: }
1.144 schwarze 971: }
1.149 ! schwarze 972: argv[argc++] = tag_files->ofn;
1.144 schwarze 973: argv[argc] = NULL;
974:
1.130 schwarze 975: switch (pager_pid = fork()) {
1.95 schwarze 976: case -1:
977: fprintf(stderr, "%s: fork: %s\n",
978: progname, strerror(errno));
979: exit((int)MANDOCLEVEL_SYSERR);
980: case 0:
1.129 schwarze 981: break;
982: default:
1.130 schwarze 983: return(pager_pid);
1.95 schwarze 984: }
985:
1.129 schwarze 986: /* The child process becomes the pager. */
1.95 schwarze 987:
1.149 ! schwarze 988: if (dup2(tag_files->ofd, STDOUT_FILENO) == -1) {
! 989: fprintf(stderr, "pager: stdout: %s\n", strerror(errno));
1.95 schwarze 990: exit((int)MANDOCLEVEL_SYSERR);
991: }
1.149 ! schwarze 992: close(tag_files->ofd);
! 993: close(tag_files->tfd);
1.95 schwarze 994: execvp(argv[0], argv);
1.143 schwarze 995: fprintf(stderr, "%s: exec %s: %s\n",
996: progname, argv[0], strerror(errno));
1.95 schwarze 997: exit((int)MANDOCLEVEL_SYSERR);
1.30 schwarze 998: }