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