Annotation of mandoc/main.c, Revision 1.229
1.229 ! schwarze 1: /* $Id: main.c,v 1.228 2015/03/27 16:36:31 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 */
86: char outopts[BUFSIZ]; /* buf of output opts */
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.205 schwarze 130: int prio, best_prio, synopsis_only;
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.194 schwarze 177: options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
1.166 schwarze 178: defos = NULL;
1.19 kristaps 179:
1.225 schwarze 180: pager_pid = 1;
1.180 schwarze 181: show_usage = 0;
1.197 schwarze 182: synopsis_only = 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':
201: (void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
1.197 schwarze 202: synopsis_only = 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.49 kristaps 243: (void)strlcat(curp.outopts, optarg, BUFSIZ);
244: (void)strlcat(curp.outopts, ",", BUFSIZ);
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
447: passthrough(resp->file, fd, synopsis_only);
1.212 schwarze 448:
1.214 schwarze 449: rctmp = mparse_wait(curp.mp);
450: if (rc < rctmp)
451: rc = rctmp;
1.212 schwarze 452:
453: if (argc > 1 && curp.outtype <= OUTT_UTF8)
454: ascii_sepline(curp.outdata);
1.192 schwarze 455: }
456:
1.154 kristaps 457: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64 kristaps 458: break;
1.210 schwarze 459:
1.212 schwarze 460: if (resp != NULL)
461: resp++;
462: else
463: argv++;
464: if (--argc)
465: mparse_reset(curp.mp);
1.1 kristaps 466: }
467:
1.22 kristaps 468: if (curp.outfree)
469: (*curp.outfree)(curp.outdata);
1.195 schwarze 470: mparse_free(curp.mp);
471: mchars_free(curp.mchars);
1.182 schwarze 472:
473: out:
474: if (search.argmode != ARG_FILE) {
1.229 ! schwarze 475: manconf_free(&conf);
1.218 schwarze 476: #if HAVE_SQLITE3
1.182 schwarze 477: mansearch_free(res, sz);
478: mansearch_setup(0);
1.218 schwarze 479: #endif
1.182 schwarze 480: }
481:
1.166 schwarze 482: free(defos);
1.1 kristaps 483:
1.224 schwarze 484: /*
1.225 schwarze 485: * If a pager is attached, flush the pipe leading to it
486: * and signal end of file such that the user can browse
487: * to the end. Then wait for the user to close the pager.
1.224 schwarze 488: */
489:
1.225 schwarze 490: if (pager_pid != 0 && pager_pid != 1) {
491: fclose(stdout);
492: waitpid(pager_pid, NULL, 0);
493: }
1.224 schwarze 494:
1.154 kristaps 495: return((int)rc);
1.1 kristaps 496: }
497:
1.55 kristaps 498: static void
1.180 schwarze 499: usage(enum argmode argmode)
1.1 kristaps 500: {
501:
1.180 schwarze 502: switch (argmode) {
503: case ARG_FILE:
1.228 schwarze 504: fputs("usage: mandoc [-acfhkl] [-I os=name] "
505: "[-K encoding] [-mformat] [-O option]\n"
506: "\t [-T output] [-W level] [file ...]\n", stderr);
1.180 schwarze 507: break;
508: case ARG_NAME:
1.221 schwarze 509: fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
1.208 schwarze 510: "[-K encoding] [-M path] [-m path]\n"
511: "\t [-O option=value] [-S subsection] [-s section] "
512: "[-T output] [-W level]\n"
1.180 schwarze 513: "\t [section] name ...\n", stderr);
514: break;
515: case ARG_WORD:
1.221 schwarze 516: fputs("usage: whatis [-acfhklw] [-C file] "
1.188 schwarze 517: "[-M path] [-m path] [-O outkey] [-S arch]\n"
518: "\t [-s section] name ...\n", stderr);
1.180 schwarze 519: break;
520: case ARG_EXPR:
1.221 schwarze 521: fputs("usage: apropos [-acfhklw] [-C file] "
1.188 schwarze 522: "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.180 schwarze 523: "\t [-s section] expression ...\n", stderr);
524: break;
525: }
1.105 kristaps 526: exit((int)MANDOCLEVEL_BADARG);
1.68 joerg 527: }
1.213 schwarze 528:
529: static int
530: fs_lookup(const struct manpaths *paths, size_t ipath,
531: const char *sec, const char *arch, const char *name,
532: struct manpage **res, size_t *ressz)
533: {
1.222 schwarze 534: glob_t globinfo;
1.213 schwarze 535: struct manpage *page;
536: char *file;
1.222 schwarze 537: int form, globres;
1.213 schwarze 538:
1.222 schwarze 539: form = FORM_SRC;
1.213 schwarze 540: mandoc_asprintf(&file, "%s/man%s/%s.%s",
541: paths->paths[ipath], sec, name, sec);
1.222 schwarze 542: if (access(file, R_OK) != -1)
1.213 schwarze 543: goto found;
544: free(file);
545:
546: mandoc_asprintf(&file, "%s/cat%s/%s.0",
547: paths->paths[ipath], sec, name);
548: if (access(file, R_OK) != -1) {
549: form = FORM_CAT;
550: goto found;
551: }
552: free(file);
553:
554: if (arch != NULL) {
555: mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
556: paths->paths[ipath], sec, arch, name, sec);
1.222 schwarze 557: if (access(file, R_OK) != -1)
1.213 schwarze 558: goto found;
559: free(file);
560: }
1.222 schwarze 561:
562: mandoc_asprintf(&file, "%s/man%s/%s.*",
563: paths->paths[ipath], sec, name);
564: globres = glob(file, 0, NULL, &globinfo);
565: if (globres != 0 && globres != GLOB_NOMATCH)
566: fprintf(stderr, "%s: %s: glob: %s\n",
567: progname, file, strerror(errno));
568: free(file);
569: if (globres == 0)
570: file = mandoc_strdup(*globinfo.gl_pathv);
571: globfree(&globinfo);
572: if (globres != 0)
573: return(0);
1.213 schwarze 574:
575: found:
1.218 schwarze 576: #if HAVE_SQLITE3
1.213 schwarze 577: fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
578: " consider running # makewhatis %s\n",
579: progname, name, sec, paths->paths[ipath]);
1.218 schwarze 580: #endif
1.220 schwarze 581:
1.213 schwarze 582: *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
583: page = *res + (*ressz - 1);
584: page->file = file;
585: page->names = NULL;
586: page->output = NULL;
587: page->ipath = ipath;
588: page->bits = NAME_FILE & NAME_MASK;
589: page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
590: page->form = form;
591: return(1);
592: }
593:
594: static void
595: fs_search(const struct mansearch *cfg, const struct manpaths *paths,
596: int argc, char **argv, struct manpage **res, size_t *ressz)
597: {
598: const char *const sections[] =
599: {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
600: const size_t nsec = sizeof(sections)/sizeof(sections[0]);
601:
602: size_t ipath, isec, lastsz;
603:
604: assert(cfg->argmode == ARG_NAME);
605:
606: *res = NULL;
607: *ressz = lastsz = 0;
608: while (argc) {
609: for (ipath = 0; ipath < paths->sz; ipath++) {
610: if (cfg->sec != NULL) {
611: if (fs_lookup(paths, ipath, cfg->sec,
612: cfg->arch, *argv, res, ressz) &&
613: cfg->firstmatch)
614: return;
615: } else for (isec = 0; isec < nsec; isec++)
616: if (fs_lookup(paths, ipath, sections[isec],
617: cfg->arch, *argv, res, ressz) &&
618: cfg->firstmatch)
619: return;
620: }
621: if (*ressz == lastsz)
622: fprintf(stderr,
623: "%s: No entry for %s in the manual.\n",
624: progname, *argv);
625: lastsz = *ressz;
626: argv++;
627: argc--;
628: }
629: }
1.68 joerg 630:
1.64 kristaps 631: static void
1.227 schwarze 632: parse(struct curparse *curp, int fd, const char *file)
1.1 kristaps 633: {
1.227 schwarze 634: enum mandoclevel rctmp;
1.154 kristaps 635: struct mdoc *mdoc;
636: struct man *man;
1.112 kristaps 637:
1.154 kristaps 638: /* Begin by parsing the file itself. */
1.112 kristaps 639:
1.154 kristaps 640: assert(file);
641: assert(fd >= -1);
1.112 kristaps 642:
1.227 schwarze 643: rctmp = mparse_readfd(curp->mp, fd, file);
644: if (rc < rctmp)
645: rc = rctmp;
1.112 kristaps 646:
1.1 kristaps 647: /*
1.154 kristaps 648: * With -Wstop and warnings or errors of at least the requested
649: * level, do not produce output.
1.1 kristaps 650: */
651:
1.227 schwarze 652: if (rctmp != MANDOCLEVEL_OK && curp->wstop)
653: return;
1.111 kristaps 654:
655: /* If unset, allocate output dev now (if applicable). */
656:
657: if ( ! (curp->outman && curp->outmdoc)) {
658: switch (curp->outtype) {
1.173 schwarze 659: case OUTT_HTML:
1.195 schwarze 660: curp->outdata = html_alloc(curp->mchars,
661: curp->outopts);
1.162 kristaps 662: curp->outfree = html_free;
663: break;
1.173 schwarze 664: case OUTT_UTF8:
1.195 schwarze 665: curp->outdata = utf8_alloc(curp->mchars,
666: curp->outopts);
1.163 kristaps 667: curp->outfree = ascii_free;
668: break;
1.173 schwarze 669: case OUTT_LOCALE:
1.195 schwarze 670: curp->outdata = locale_alloc(curp->mchars,
671: curp->outopts);
1.162 kristaps 672: curp->outfree = ascii_free;
1.111 kristaps 673: break;
1.173 schwarze 674: case OUTT_ASCII:
1.195 schwarze 675: curp->outdata = ascii_alloc(curp->mchars,
676: curp->outopts);
1.111 kristaps 677: curp->outfree = ascii_free;
678: break;
1.173 schwarze 679: case OUTT_PDF:
1.195 schwarze 680: curp->outdata = pdf_alloc(curp->mchars,
681: curp->outopts);
1.111 kristaps 682: curp->outfree = pspdf_free;
683: break;
1.173 schwarze 684: case OUTT_PS:
1.195 schwarze 685: curp->outdata = ps_alloc(curp->mchars,
686: curp->outopts);
1.111 kristaps 687: curp->outfree = pspdf_free;
688: break;
689: default:
690: break;
691: }
692:
693: switch (curp->outtype) {
1.173 schwarze 694: case OUTT_HTML:
1.111 kristaps 695: curp->outman = html_man;
696: curp->outmdoc = html_mdoc;
697: break;
1.173 schwarze 698: case OUTT_TREE:
1.111 kristaps 699: curp->outman = tree_man;
700: curp->outmdoc = tree_mdoc;
701: break;
1.173 schwarze 702: case OUTT_MAN:
1.164 schwarze 703: curp->outmdoc = man_mdoc;
1.165 kristaps 704: curp->outman = man_man;
1.164 schwarze 705: break;
1.173 schwarze 706: case OUTT_PDF:
1.111 kristaps 707: /* FALLTHROUGH */
1.173 schwarze 708: case OUTT_ASCII:
1.111 kristaps 709: /* FALLTHROUGH */
1.173 schwarze 710: case OUTT_UTF8:
1.163 kristaps 711: /* FALLTHROUGH */
1.173 schwarze 712: case OUTT_LOCALE:
1.162 kristaps 713: /* FALLTHROUGH */
1.173 schwarze 714: case OUTT_PS:
1.111 kristaps 715: curp->outman = terminal_man;
716: curp->outmdoc = terminal_mdoc;
717: break;
718: default:
719: break;
720: }
721: }
722:
1.171 schwarze 723: mparse_result(curp->mp, &mdoc, &man, NULL);
1.154 kristaps 724:
1.111 kristaps 725: /* Execute the out device, if it exists. */
726:
1.154 kristaps 727: if (man && curp->outman)
728: (*curp->outman)(curp->outdata, man);
729: if (mdoc && curp->outmdoc)
730: (*curp->outmdoc)(curp->outdata, mdoc);
1.186 schwarze 731: }
732:
1.227 schwarze 733: static void
1.197 schwarze 734: passthrough(const char *file, int fd, int synopsis_only)
1.186 schwarze 735: {
1.197 schwarze 736: const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
737: const char synr[] = "SYNOPSIS";
738:
739: FILE *stream;
1.186 schwarze 740: const char *syscall;
1.197 schwarze 741: char *line;
742: size_t len, off;
743: ssize_t nw;
744: int print;
1.210 schwarze 745:
746: fflush(stdout);
1.197 schwarze 747:
748: if ((stream = fdopen(fd, "r")) == NULL) {
749: close(fd);
750: syscall = "fdopen";
751: goto fail;
752: }
1.186 schwarze 753:
1.197 schwarze 754: print = 0;
755: while ((line = fgetln(stream, &len)) != NULL) {
756: if (synopsis_only) {
757: if (print) {
758: if ( ! isspace((unsigned char)*line))
759: goto done;
760: while (len &&
761: isspace((unsigned char)*line)) {
762: line++;
763: len--;
764: }
765: } else {
766: if ((len == sizeof(synb) &&
767: ! strncmp(line, synb, len - 1)) ||
768: (len == sizeof(synr) &&
769: ! strncmp(line, synr, len - 1)))
770: print = 1;
771: continue;
772: }
773: }
774: for (off = 0; off < len; off += nw)
775: if ((nw = write(STDOUT_FILENO, line + off,
776: len - off)) == -1 || nw == 0) {
777: fclose(stream);
1.186 schwarze 778: syscall = "write";
779: goto fail;
780: }
1.197 schwarze 781: }
1.186 schwarze 782:
1.197 schwarze 783: if (ferror(stream)) {
784: fclose(stream);
785: syscall = "fgetln";
786: goto fail;
787: }
1.193 schwarze 788:
1.197 schwarze 789: done:
790: fclose(stream);
1.227 schwarze 791: return;
1.186 schwarze 792:
793: fail:
794: fprintf(stderr, "%s: %s: SYSERR: %s: %s",
795: progname, file, syscall, strerror(errno));
1.227 schwarze 796: if (rc < MANDOCLEVEL_SYSERR)
797: rc = MANDOCLEVEL_SYSERR;
1.194 schwarze 798: }
799:
800: static int
801: koptions(int *options, char *arg)
802: {
803:
804: if ( ! strcmp(arg, "utf-8")) {
805: *options |= MPARSE_UTF8;
806: *options &= ~MPARSE_LATIN1;
807: } else if ( ! strcmp(arg, "iso-8859-1")) {
808: *options |= MPARSE_LATIN1;
809: *options &= ~MPARSE_UTF8;
810: } else if ( ! strcmp(arg, "us-ascii")) {
811: *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
812: } else {
1.209 schwarze 813: fprintf(stderr, "%s: -K %s: Bad argument\n",
1.194 schwarze 814: progname, arg);
815: return(0);
816: }
817: return(1);
1.10 kristaps 818: }
819:
820: static int
1.170 schwarze 821: moptions(int *options, char *arg)
1.10 kristaps 822: {
823:
1.180 schwarze 824: if (arg == NULL)
825: /* nothing to do */;
826: else if (0 == strcmp(arg, "doc"))
1.170 schwarze 827: *options |= MPARSE_MDOC;
1.19 kristaps 828: else if (0 == strcmp(arg, "andoc"))
1.170 schwarze 829: /* nothing to do */;
1.17 kristaps 830: else if (0 == strcmp(arg, "an"))
1.170 schwarze 831: *options |= MPARSE_MAN;
1.10 kristaps 832: else {
1.209 schwarze 833: fprintf(stderr, "%s: -m %s: Bad argument\n",
1.176 schwarze 834: progname, arg);
1.10 kristaps 835: return(0);
836: }
837:
838: return(1);
1.1 kristaps 839: }
840:
841: static int
1.60 kristaps 842: toptions(struct curparse *curp, char *arg)
1.1 kristaps 843: {
844:
845: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 846: curp->outtype = OUTT_ASCII;
847: else if (0 == strcmp(arg, "lint")) {
848: curp->outtype = OUTT_LINT;
1.103 schwarze 849: curp->wlevel = MANDOCLEVEL_WARNING;
1.154 kristaps 850: } else if (0 == strcmp(arg, "tree"))
1.60 kristaps 851: curp->outtype = OUTT_TREE;
1.164 schwarze 852: else if (0 == strcmp(arg, "man"))
853: curp->outtype = OUTT_MAN;
1.43 kristaps 854: else if (0 == strcmp(arg, "html"))
1.60 kristaps 855: curp->outtype = OUTT_HTML;
1.163 kristaps 856: else if (0 == strcmp(arg, "utf8"))
857: curp->outtype = OUTT_UTF8;
1.162 kristaps 858: else if (0 == strcmp(arg, "locale"))
859: curp->outtype = OUTT_LOCALE;
1.59 kristaps 860: else if (0 == strcmp(arg, "xhtml"))
1.195 schwarze 861: curp->outtype = OUTT_HTML;
1.85 kristaps 862: else if (0 == strcmp(arg, "ps"))
863: curp->outtype = OUTT_PS;
1.100 kristaps 864: else if (0 == strcmp(arg, "pdf"))
865: curp->outtype = OUTT_PDF;
1.1 kristaps 866: else {
1.209 schwarze 867: fprintf(stderr, "%s: -T %s: Bad argument\n",
1.176 schwarze 868: progname, arg);
1.1 kristaps 869: return(0);
870: }
871:
872: return(1);
873: }
874:
875: static int
1.103 schwarze 876: woptions(struct curparse *curp, char *arg)
1.1 kristaps 877: {
1.34 kristaps 878: char *v, *o;
1.217 schwarze 879: const char *toks[7];
1.1 kristaps 880:
1.103 schwarze 881: toks[0] = "stop";
882: toks[1] = "all";
883: toks[2] = "warning";
884: toks[3] = "error";
1.217 schwarze 885: toks[4] = "unsupp";
886: toks[5] = "fatal";
887: toks[6] = NULL;
1.1 kristaps 888:
1.34 kristaps 889: while (*arg) {
890: o = arg;
1.45 kristaps 891: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.173 schwarze 892: case 0:
1.103 schwarze 893: curp->wstop = 1;
1.1 kristaps 894: break;
1.173 schwarze 895: case 1:
1.103 schwarze 896: /* FALLTHROUGH */
1.173 schwarze 897: case 2:
1.103 schwarze 898: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 899: break;
1.173 schwarze 900: case 3:
1.103 schwarze 901: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 902: break;
1.173 schwarze 903: case 4:
1.217 schwarze 904: curp->wlevel = MANDOCLEVEL_UNSUPP;
905: break;
906: case 5:
1.215 schwarze 907: curp->wlevel = MANDOCLEVEL_BADARG;
1.50 kristaps 908: break;
1.1 kristaps 909: default:
1.209 schwarze 910: fprintf(stderr, "%s: -W %s: Bad argument\n",
1.176 schwarze 911: progname, o);
1.1 kristaps 912: return(0);
913: }
1.34 kristaps 914: }
1.1 kristaps 915:
916: return(1);
917: }
918:
1.153 kristaps 919: static void
1.173 schwarze 920: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.155 kristaps 921: const char *file, int line, int col, const char *msg)
1.71 kristaps 922: {
1.177 schwarze 923: const char *mparse_msg;
1.103 schwarze 924:
1.175 schwarze 925: fprintf(stderr, "%s: %s:", progname, file);
926:
927: if (line)
928: fprintf(stderr, "%d:%d:", line, col + 1);
929:
1.177 schwarze 930: fprintf(stderr, " %s", mparse_strlevel(lvl));
931:
932: if (NULL != (mparse_msg = mparse_strerror(t)))
933: fprintf(stderr, ": %s", mparse_msg);
1.154 kristaps 934:
1.73 kristaps 935: if (msg)
936: fprintf(stderr, ": %s", msg);
1.154 kristaps 937:
1.73 kristaps 938: fputc('\n', stderr);
1.183 schwarze 939: }
940:
1.226 schwarze 941: static void
942: handle_sigpipe(int signum)
943: {
944:
1.227 schwarze 945: exit((int)rc);
1.226 schwarze 946: }
947:
1.225 schwarze 948: static pid_t
1.183 schwarze 949: spawn_pager(void)
950: {
1.184 schwarze 951: #define MAX_PAGER_ARGS 16
952: char *argv[MAX_PAGER_ARGS];
953: const char *pager;
954: char *cp;
955: int fildes[2];
956: int argc;
1.225 schwarze 957: pid_t pager_pid;
1.183 schwarze 958:
959: if (pipe(fildes) == -1) {
960: fprintf(stderr, "%s: pipe: %s\n",
961: progname, strerror(errno));
1.225 schwarze 962: return(0);
1.183 schwarze 963: }
964:
1.225 schwarze 965: switch (pager_pid = fork()) {
1.183 schwarze 966: case -1:
967: fprintf(stderr, "%s: fork: %s\n",
968: progname, strerror(errno));
969: exit((int)MANDOCLEVEL_SYSERR);
970: case 0:
1.224 schwarze 971: break;
972: default:
1.183 schwarze 973: close(fildes[0]);
974: if (dup2(fildes[1], STDOUT_FILENO) == -1) {
975: fprintf(stderr, "%s: dup output: %s\n",
976: progname, strerror(errno));
977: exit((int)MANDOCLEVEL_SYSERR);
978: }
1.224 schwarze 979: close(fildes[1]);
1.226 schwarze 980: signal(SIGPIPE, handle_sigpipe);
1.225 schwarze 981: return(pager_pid);
1.184 schwarze 982: }
983:
1.224 schwarze 984: /* The child process becomes the pager. */
1.184 schwarze 985:
986: close(fildes[1]);
987: if (dup2(fildes[0], STDIN_FILENO) == -1) {
988: fprintf(stderr, "%s: dup input: %s\n",
989: progname, strerror(errno));
1.183 schwarze 990: exit((int)MANDOCLEVEL_SYSERR);
991: }
1.224 schwarze 992: close(fildes[0]);
1.184 schwarze 993:
994: pager = getenv("MANPAGER");
995: if (pager == NULL || *pager == '\0')
996: pager = getenv("PAGER");
997: if (pager == NULL || *pager == '\0')
998: pager = "/usr/bin/more -s";
999: cp = mandoc_strdup(pager);
1000:
1001: /*
1002: * Parse the pager command into words.
1003: * Intentionally do not do anything fancy here.
1004: */
1005:
1006: argc = 0;
1007: while (argc + 1 < MAX_PAGER_ARGS) {
1008: argv[argc++] = cp;
1009: cp = strchr(cp, ' ');
1010: if (cp == NULL)
1011: break;
1012: *cp++ = '\0';
1013: while (*cp == ' ')
1014: cp++;
1015: if (*cp == '\0')
1016: break;
1017: }
1018: argv[argc] = NULL;
1019:
1020: /* Hand over to the pager. */
1021:
1022: execvp(argv[0], argv);
1023: fprintf(stderr, "%s: exec: %s\n",
1024: progname, strerror(errno));
1025: exit((int)MANDOCLEVEL_SYSERR);
1.71 kristaps 1026: }
CVSweb