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