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