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