Annotation of mandoc/main.c, Revision 1.181
1.181 ! schwarze 1: /* $Id: main.c,v 1.180 2014/08/17 03:24:47 schwarze Exp $ */
1.1 kristaps 2: /*
1.133 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.170 schwarze 4: * Copyright (c) 2010, 2011, 2012, 2014 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.58 kristaps 22:
1.1 kristaps 23: #include <assert.h>
24: #include <stdio.h>
1.45 kristaps 25: #include <stdint.h>
1.1 kristaps 26: #include <stdlib.h>
27: #include <string.h>
28: #include <unistd.h>
29:
1.71 kristaps 30: #include "mandoc.h"
1.172 schwarze 31: #include "mandoc_aux.h"
1.90 kristaps 32: #include "main.h"
1.1 kristaps 33: #include "mdoc.h"
1.10 kristaps 34: #include "man.h"
1.180 schwarze 35: #include "manpath.h"
36: #include "mansearch.h"
1.101 joerg 37:
1.55 kristaps 38: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56 kristaps 39: # if !defined(lint)
40: # define __attribute__(x)
41: # endif
1.55 kristaps 42: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16 kristaps 43:
1.181 ! schwarze 44: enum outmode {
! 45: OUTMODE_DEF = 0,
! 46: OUTMODE_FLN,
! 47: OUTMODE_LST,
! 48: OUTMODE_ALL,
! 49: OUTMODE_INT,
! 50: OUTMODE_ONE
! 51: };
! 52:
1.42 kristaps 53: typedef void (*out_mdoc)(void *, const struct mdoc *);
54: typedef void (*out_man)(void *, const struct man *);
1.22 kristaps 55: typedef void (*out_free)(void *);
56:
1.19 kristaps 57: enum outt {
1.154 kristaps 58: OUTT_ASCII = 0, /* -Tascii */
1.162 kristaps 59: OUTT_LOCALE, /* -Tlocale */
1.163 kristaps 60: OUTT_UTF8, /* -Tutf8 */
1.154 kristaps 61: OUTT_TREE, /* -Ttree */
1.164 schwarze 62: OUTT_MAN, /* -Tman */
1.154 kristaps 63: OUTT_HTML, /* -Thtml */
64: OUTT_XHTML, /* -Txhtml */
65: OUTT_LINT, /* -Tlint */
66: OUTT_PS, /* -Tps */
67: OUTT_PDF /* -Tpdf */
1.19 kristaps 68: };
69:
1.5 kristaps 70: struct curparse {
1.154 kristaps 71: struct mparse *mp;
1.148 kristaps 72: enum mandoclevel wlevel; /* ignore messages below this */
73: int wstop; /* stop after a file with a warning */
1.173 schwarze 74: enum outt outtype; /* which output to use */
1.79 kristaps 75: out_mdoc outmdoc; /* mdoc output ptr */
1.173 schwarze 76: out_man outman; /* man output ptr */
1.79 kristaps 77: out_free outfree; /* free output ptr */
78: void *outdata; /* data for output */
79: char outopts[BUFSIZ]; /* buf of output opts */
80: };
81:
1.170 schwarze 82: static int moptions(int *, char *);
1.155 kristaps 83: static void mmsg(enum mandocerr, enum mandoclevel,
84: const char *, int, int, const char *);
1.173 schwarze 85: static void parse(struct curparse *, int,
1.154 kristaps 86: const char *, enum mandoclevel *);
1.66 kristaps 87: static int toptions(struct curparse *, char *);
1.180 schwarze 88: static void usage(enum argmode) __attribute__((noreturn));
1.55 kristaps 89: static void version(void) __attribute__((noreturn));
1.103 schwarze 90: static int woptions(struct curparse *, char *);
1.1 kristaps 91:
1.54 kristaps 92: static const char *progname;
1.1 kristaps 93:
1.173 schwarze 94:
1.1 kristaps 95: int
96: main(int argc, char *argv[])
97: {
1.5 kristaps 98: struct curparse curp;
1.180 schwarze 99: struct mansearch search;
100: struct manpaths paths;
101: char *conf_file, *defpaths, *auxpaths;
102: char *defos;
103: #if HAVE_SQLITE3
104: struct manpage *res;
105: size_t i, sz;
106: #endif
107: enum mandoclevel rc;
1.181 ! schwarze 108: enum outmode outmode;
1.180 schwarze 109: int show_usage;
1.170 schwarze 110: int options;
1.180 schwarze 111: int c;
1.1 kristaps 112:
1.54 kristaps 113: progname = strrchr(argv[0], '/');
114: if (progname == NULL)
115: progname = argv[0];
116: else
117: ++progname;
1.179 schwarze 118:
1.180 schwarze 119: /* Search options. */
120:
121: memset(&paths, 0, sizeof(struct manpaths));
122: conf_file = defpaths = auxpaths = NULL;
123:
124: memset(&search, 0, sizeof(struct mansearch));
125: search.outkey = "Nd";
126:
127: if (strcmp(progname, "man") == 0)
128: search.argmode = ARG_NAME;
129: else if (strncmp(progname, "apropos", 7) == 0)
130: search.argmode = ARG_EXPR;
131: else if (strncmp(progname, "whatis", 6) == 0)
132: search.argmode = ARG_WORD;
133: else
134: search.argmode = ARG_FILE;
135:
136: /* Parser and formatter options. */
1.54 kristaps 137:
1.51 kristaps 138: memset(&curp, 0, sizeof(struct curparse));
1.22 kristaps 139: curp.outtype = OUTT_ASCII;
1.103 schwarze 140: curp.wlevel = MANDOCLEVEL_FATAL;
1.180 schwarze 141: options = MPARSE_SO;
1.166 schwarze 142: defos = NULL;
1.19 kristaps 143:
1.180 schwarze 144: show_usage = 0;
1.181 ! schwarze 145: outmode = OUTMODE_DEF;
! 146: while (-1 != (c = getopt(argc, argv, "aC:fI:ikM:m:O:S:s:T:VW:w"))) {
1.1 kristaps 147: switch (c) {
1.181 ! schwarze 148: case 'a':
! 149: outmode = OUTMODE_ALL;
! 150: break;
1.180 schwarze 151: case 'C':
152: conf_file = optarg;
153: break;
154: case 'f':
155: search.argmode = ARG_WORD;
156: break;
1.173 schwarze 157: case 'I':
1.166 schwarze 158: if (strncmp(optarg, "os=", 3)) {
1.173 schwarze 159: fprintf(stderr,
1.176 schwarze 160: "%s: -I%s: Bad argument\n",
161: progname, optarg);
1.166 schwarze 162: return((int)MANDOCLEVEL_BADARG);
163: }
164: if (defos) {
1.173 schwarze 165: fprintf(stderr,
1.176 schwarze 166: "%s: -I%s: Duplicate argument\n",
167: progname, optarg);
1.166 schwarze 168: return((int)MANDOCLEVEL_BADARG);
169: }
170: defos = mandoc_strdup(optarg + 3);
171: break;
1.181 ! schwarze 172: case 'i':
! 173: outmode = OUTMODE_INT;
! 174: break;
1.180 schwarze 175: case 'k':
176: search.argmode = ARG_EXPR;
177: break;
178: case 'M':
179: defpaths = optarg;
180: break;
1.173 schwarze 181: case 'm':
1.180 schwarze 182: auxpaths = optarg;
1.10 kristaps 183: break;
1.173 schwarze 184: case 'O':
1.180 schwarze 185: search.outkey = optarg;
1.49 kristaps 186: (void)strlcat(curp.outopts, optarg, BUFSIZ);
187: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44 kristaps 188: break;
1.180 schwarze 189: case 'S':
190: search.arch = optarg;
191: break;
192: case 's':
193: search.sec = optarg;
194: break;
1.173 schwarze 195: case 'T':
1.60 kristaps 196: if ( ! toptions(&curp, optarg))
1.105 kristaps 197: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 198: break;
1.173 schwarze 199: case 'W':
1.103 schwarze 200: if ( ! woptions(&curp, optarg))
1.105 kristaps 201: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 202: break;
1.181 ! schwarze 203: case 'w':
! 204: outmode = OUTMODE_FLN;
! 205: break;
1.173 schwarze 206: case 'V':
1.1 kristaps 207: version();
208: /* NOTREACHED */
209: default:
1.180 schwarze 210: show_usage = 1;
211: break;
1.1 kristaps 212: }
1.180 schwarze 213: }
214:
215: if (show_usage)
216: usage(search.argmode);
217:
1.181 ! schwarze 218: if (outmode == OUTMODE_DEF) {
! 219: switch (search.argmode) {
! 220: case ARG_FILE:
! 221: outmode = OUTMODE_ALL;
! 222: break;
! 223: case ARG_NAME:
! 224: outmode = OUTMODE_ONE;
! 225: break;
! 226: default:
! 227: outmode = OUTMODE_LST;
! 228: break;
! 229: }
! 230: }
! 231:
1.180 schwarze 232: argc -= optind;
233: argv += optind;
234:
235: /* man(1), whatis(1), apropos(1) */
236:
237: if (search.argmode != ARG_FILE) {
238: #if HAVE_SQLITE3
239: if (argc == 0)
240: usage(search.argmode);
241: manpath_parse(&paths, conf_file, defpaths, auxpaths);
242: mansearch_setup(1);
243: if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
244: usage(search.argmode);
245: manpath_free(&paths);
1.181 ! schwarze 246: for (i = 0; i < sz; i++) {
! 247: if (outmode == OUTMODE_FLN)
! 248: puts(res[i].file);
! 249: else
! 250: printf("%s - %s\n", res[i].names,
! 251: res[i].output == NULL ? "" :
! 252: res[i].output);
! 253: }
1.180 schwarze 254: mansearch_free(res, sz);
255: mansearch_setup(0);
256: return((int)MANDOCLEVEL_OK);
257: #else
258: fputs("mandoc: database support not compiled in\n",
259: stderr);
260: return((int)MANDOCLEVEL_BADARG);
261: #endif
262: }
263:
264: /* mandoc(1) */
265:
266: if ( ! moptions(&options, auxpaths))
267: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 268:
1.170 schwarze 269: curp.mp = mparse_alloc(options, curp.wlevel, mmsg, defos);
1.154 kristaps 270:
1.165 kristaps 271: /*
272: * Conditionally start up the lookaside buffer before parsing.
273: */
274: if (OUTT_MAN == curp.outtype)
275: mparse_keep(curp.mp);
276:
1.154 kristaps 277: rc = MANDOCLEVEL_OK;
1.39 kristaps 278:
1.154 kristaps 279: if (NULL == *argv)
280: parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.64 kristaps 281:
282: while (*argv) {
1.154 kristaps 283: parse(&curp, -1, *argv, &rc);
284: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64 kristaps 285: break;
286: ++argv;
1.1 kristaps 287: }
288:
1.22 kristaps 289: if (curp.outfree)
290: (*curp.outfree)(curp.outdata);
1.154 kristaps 291: if (curp.mp)
292: mparse_free(curp.mp);
1.166 schwarze 293: free(defos);
1.1 kristaps 294:
1.154 kristaps 295: return((int)rc);
1.1 kristaps 296: }
297:
1.55 kristaps 298: static void
1.1 kristaps 299: version(void)
300: {
301:
1.180 schwarze 302: printf("mandoc %s\n", VERSION);
1.105 kristaps 303: exit((int)MANDOCLEVEL_OK);
1.1 kristaps 304: }
305:
1.55 kristaps 306: static void
1.180 schwarze 307: usage(enum argmode argmode)
1.1 kristaps 308: {
309:
1.180 schwarze 310: switch (argmode) {
311: case ARG_FILE:
312: fputs("usage: mandoc [-V] [-Ios=name] [-mformat]"
313: " [-Ooption] [-Toutput] [-Wlevel]\n"
314: "\t [file ...]\n", stderr);
315: break;
316: case ARG_NAME:
317: fputs("usage: man [-acfhkVw] [-C file] "
318: "[-M path] [-m path] [-S arch] [-s section]\n"
319: "\t [section] name ...\n", stderr);
320: break;
321: case ARG_WORD:
322: fputs("usage: whatis [-V] [-C file] [-M path] [-m path] "
323: "[-S arch] [-s section] name ...\n", stderr);
324: break;
325: case ARG_EXPR:
326: fputs("usage: apropos [-V] [-C file] [-M path] [-m path] "
327: "[-O outkey] [-S arch]\n"
328: "\t [-s section] expression ...\n", stderr);
329: break;
330: }
1.105 kristaps 331: exit((int)MANDOCLEVEL_BADARG);
1.68 joerg 332: }
333:
1.64 kristaps 334: static void
1.173 schwarze 335: parse(struct curparse *curp, int fd, const char *file,
336: enum mandoclevel *level)
1.1 kristaps 337: {
1.154 kristaps 338: enum mandoclevel rc;
339: struct mdoc *mdoc;
340: struct man *man;
1.112 kristaps 341:
1.154 kristaps 342: /* Begin by parsing the file itself. */
1.112 kristaps 343:
1.154 kristaps 344: assert(file);
345: assert(fd >= -1);
1.112 kristaps 346:
1.154 kristaps 347: rc = mparse_readfd(curp->mp, fd, file);
1.112 kristaps 348:
1.154 kristaps 349: /* Stop immediately if the parse has failed. */
1.92 kristaps 350:
1.154 kristaps 351: if (MANDOCLEVEL_FATAL <= rc)
1.111 kristaps 352: goto cleanup;
353:
1.1 kristaps 354: /*
1.154 kristaps 355: * With -Wstop and warnings or errors of at least the requested
356: * level, do not produce output.
1.1 kristaps 357: */
358:
1.154 kristaps 359: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.111 kristaps 360: goto cleanup;
361:
362: /* If unset, allocate output dev now (if applicable). */
363:
364: if ( ! (curp->outman && curp->outmdoc)) {
365: switch (curp->outtype) {
1.173 schwarze 366: case OUTT_XHTML:
1.111 kristaps 367: curp->outdata = xhtml_alloc(curp->outopts);
1.162 kristaps 368: curp->outfree = html_free;
1.111 kristaps 369: break;
1.173 schwarze 370: case OUTT_HTML:
1.111 kristaps 371: curp->outdata = html_alloc(curp->outopts);
1.162 kristaps 372: curp->outfree = html_free;
373: break;
1.173 schwarze 374: case OUTT_UTF8:
1.163 kristaps 375: curp->outdata = utf8_alloc(curp->outopts);
376: curp->outfree = ascii_free;
377: break;
1.173 schwarze 378: case OUTT_LOCALE:
1.162 kristaps 379: curp->outdata = locale_alloc(curp->outopts);
380: curp->outfree = ascii_free;
1.111 kristaps 381: break;
1.173 schwarze 382: case OUTT_ASCII:
1.111 kristaps 383: curp->outdata = ascii_alloc(curp->outopts);
384: curp->outfree = ascii_free;
385: break;
1.173 schwarze 386: case OUTT_PDF:
1.111 kristaps 387: curp->outdata = pdf_alloc(curp->outopts);
388: curp->outfree = pspdf_free;
389: break;
1.173 schwarze 390: case OUTT_PS:
1.111 kristaps 391: curp->outdata = ps_alloc(curp->outopts);
392: curp->outfree = pspdf_free;
393: break;
394: default:
395: break;
396: }
397:
398: switch (curp->outtype) {
1.173 schwarze 399: case OUTT_HTML:
1.111 kristaps 400: /* FALLTHROUGH */
1.173 schwarze 401: case OUTT_XHTML:
1.111 kristaps 402: curp->outman = html_man;
403: curp->outmdoc = html_mdoc;
404: break;
1.173 schwarze 405: case OUTT_TREE:
1.111 kristaps 406: curp->outman = tree_man;
407: curp->outmdoc = tree_mdoc;
408: break;
1.173 schwarze 409: case OUTT_MAN:
1.164 schwarze 410: curp->outmdoc = man_mdoc;
1.165 kristaps 411: curp->outman = man_man;
1.164 schwarze 412: break;
1.173 schwarze 413: case OUTT_PDF:
1.111 kristaps 414: /* FALLTHROUGH */
1.173 schwarze 415: case OUTT_ASCII:
1.111 kristaps 416: /* FALLTHROUGH */
1.173 schwarze 417: case OUTT_UTF8:
1.163 kristaps 418: /* FALLTHROUGH */
1.173 schwarze 419: case OUTT_LOCALE:
1.162 kristaps 420: /* FALLTHROUGH */
1.173 schwarze 421: case OUTT_PS:
1.111 kristaps 422: curp->outman = terminal_man;
423: curp->outmdoc = terminal_mdoc;
424: break;
425: default:
426: break;
427: }
428: }
429:
1.171 schwarze 430: mparse_result(curp->mp, &mdoc, &man, NULL);
1.154 kristaps 431:
1.111 kristaps 432: /* Execute the out device, if it exists. */
433:
1.154 kristaps 434: if (man && curp->outman)
435: (*curp->outman)(curp->outdata, man);
436: if (mdoc && curp->outmdoc)
437: (*curp->outmdoc)(curp->outdata, mdoc);
1.111 kristaps 438:
439: cleanup:
1.112 kristaps 440:
1.154 kristaps 441: mparse_reset(curp->mp);
1.111 kristaps 442:
1.154 kristaps 443: if (*level < rc)
444: *level = rc;
1.10 kristaps 445: }
446:
447: static int
1.170 schwarze 448: moptions(int *options, char *arg)
1.10 kristaps 449: {
450:
1.180 schwarze 451: if (arg == NULL)
452: /* nothing to do */;
453: else if (0 == strcmp(arg, "doc"))
1.170 schwarze 454: *options |= MPARSE_MDOC;
1.19 kristaps 455: else if (0 == strcmp(arg, "andoc"))
1.170 schwarze 456: /* nothing to do */;
1.17 kristaps 457: else if (0 == strcmp(arg, "an"))
1.170 schwarze 458: *options |= MPARSE_MAN;
1.10 kristaps 459: else {
1.176 schwarze 460: fprintf(stderr, "%s: -m%s: Bad argument\n",
461: progname, arg);
1.10 kristaps 462: return(0);
463: }
464:
465: return(1);
1.1 kristaps 466: }
467:
468: static int
1.60 kristaps 469: toptions(struct curparse *curp, char *arg)
1.1 kristaps 470: {
471:
472: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 473: curp->outtype = OUTT_ASCII;
474: else if (0 == strcmp(arg, "lint")) {
475: curp->outtype = OUTT_LINT;
1.103 schwarze 476: curp->wlevel = MANDOCLEVEL_WARNING;
1.154 kristaps 477: } else if (0 == strcmp(arg, "tree"))
1.60 kristaps 478: curp->outtype = OUTT_TREE;
1.164 schwarze 479: else if (0 == strcmp(arg, "man"))
480: curp->outtype = OUTT_MAN;
1.43 kristaps 481: else if (0 == strcmp(arg, "html"))
1.60 kristaps 482: curp->outtype = OUTT_HTML;
1.163 kristaps 483: else if (0 == strcmp(arg, "utf8"))
484: curp->outtype = OUTT_UTF8;
1.162 kristaps 485: else if (0 == strcmp(arg, "locale"))
486: curp->outtype = OUTT_LOCALE;
1.59 kristaps 487: else if (0 == strcmp(arg, "xhtml"))
1.60 kristaps 488: curp->outtype = OUTT_XHTML;
1.85 kristaps 489: else if (0 == strcmp(arg, "ps"))
490: curp->outtype = OUTT_PS;
1.100 kristaps 491: else if (0 == strcmp(arg, "pdf"))
492: curp->outtype = OUTT_PDF;
1.1 kristaps 493: else {
1.176 schwarze 494: fprintf(stderr, "%s: -T%s: Bad argument\n",
495: progname, arg);
1.1 kristaps 496: return(0);
497: }
498:
499: return(1);
500: }
501:
502: static int
1.103 schwarze 503: woptions(struct curparse *curp, char *arg)
1.1 kristaps 504: {
1.34 kristaps 505: char *v, *o;
1.173 schwarze 506: const char *toks[6];
1.1 kristaps 507:
1.103 schwarze 508: toks[0] = "stop";
509: toks[1] = "all";
510: toks[2] = "warning";
511: toks[3] = "error";
512: toks[4] = "fatal";
513: toks[5] = NULL;
1.1 kristaps 514:
1.34 kristaps 515: while (*arg) {
516: o = arg;
1.45 kristaps 517: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.173 schwarze 518: case 0:
1.103 schwarze 519: curp->wstop = 1;
1.1 kristaps 520: break;
1.173 schwarze 521: case 1:
1.103 schwarze 522: /* FALLTHROUGH */
1.173 schwarze 523: case 2:
1.103 schwarze 524: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 525: break;
1.173 schwarze 526: case 3:
1.103 schwarze 527: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 528: break;
1.173 schwarze 529: case 4:
1.103 schwarze 530: curp->wlevel = MANDOCLEVEL_FATAL;
1.50 kristaps 531: break;
1.1 kristaps 532: default:
1.176 schwarze 533: fprintf(stderr, "%s: -W%s: Bad argument\n",
534: progname, o);
1.1 kristaps 535: return(0);
536: }
1.34 kristaps 537: }
1.1 kristaps 538:
539: return(1);
540: }
541:
1.153 kristaps 542: static void
1.173 schwarze 543: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.155 kristaps 544: const char *file, int line, int col, const char *msg)
1.71 kristaps 545: {
1.177 schwarze 546: const char *mparse_msg;
1.103 schwarze 547:
1.175 schwarze 548: fprintf(stderr, "%s: %s:", progname, file);
549:
550: if (line)
551: fprintf(stderr, "%d:%d:", line, col + 1);
552:
1.177 schwarze 553: fprintf(stderr, " %s", mparse_strlevel(lvl));
554:
555: if (NULL != (mparse_msg = mparse_strerror(t)))
556: fprintf(stderr, ": %s", mparse_msg);
1.154 kristaps 557:
1.73 kristaps 558: if (msg)
559: fprintf(stderr, ": %s", msg);
1.154 kristaps 560:
1.73 kristaps 561: fputc('\n', stderr);
1.71 kristaps 562: }
CVSweb