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