Annotation of mandoc/main.c, Revision 1.226
1.226 ! schwarze 1: /* $Id: main.c,v 1.225 2015/03/10 13:50:03 schwarze Exp $ */
1.1 kristaps 2: /*
1.183 schwarze 3: * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.212 schwarze 4: * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.169 schwarze 5: * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
1.1 kristaps 6: *
7: * Permission to use, copy, modify, and distribute this software for any
1.25 kristaps 8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 10: *
1.25 kristaps 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 18: */
1.58 kristaps 19: #include "config.h"
1.178 schwarze 20:
21: #include <sys/types.h>
1.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.180 schwarze 42: #include "manpath.h"
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.173 schwarze 104: static void parse(struct curparse *, int,
1.154 kristaps 105: const char *, enum mandoclevel *);
1.197 schwarze 106: static enum mandoclevel 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.5 kristaps 122: struct curparse curp;
1.180 schwarze 123: struct mansearch search;
124: struct manpaths paths;
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.205 schwarze 131: int prio, best_prio, synopsis_only;
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:
155: memset(&paths, 0, sizeof(struct manpaths));
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.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.197 schwarze 183: synopsis_only = 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':
202: (void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
1.197 schwarze 203: synopsis_only = 1;
1.225 schwarze 204: pager_pid = 0;
1.190 schwarze 205: outmode = OUTMODE_ALL;
206: break;
1.173 schwarze 207: case 'I':
1.166 schwarze 208: if (strncmp(optarg, "os=", 3)) {
1.173 schwarze 209: fprintf(stderr,
1.209 schwarze 210: "%s: -I %s: Bad argument\n",
1.176 schwarze 211: progname, optarg);
1.166 schwarze 212: return((int)MANDOCLEVEL_BADARG);
213: }
214: if (defos) {
1.173 schwarze 215: fprintf(stderr,
1.209 schwarze 216: "%s: -I %s: Duplicate argument\n",
1.176 schwarze 217: progname, optarg);
1.166 schwarze 218: return((int)MANDOCLEVEL_BADARG);
219: }
220: defos = mandoc_strdup(optarg + 3);
221: break;
1.181 schwarze 222: case 'i':
223: outmode = OUTMODE_INT;
224: break;
1.194 schwarze 225: case 'K':
226: if ( ! koptions(&options, optarg))
227: return((int)MANDOCLEVEL_BADARG);
228: break;
1.180 schwarze 229: case 'k':
230: search.argmode = ARG_EXPR;
231: break;
1.188 schwarze 232: case 'l':
233: search.argmode = ARG_FILE;
234: outmode = OUTMODE_ALL;
235: break;
1.180 schwarze 236: case 'M':
237: defpaths = optarg;
238: break;
1.173 schwarze 239: case 'm':
1.180 schwarze 240: auxpaths = optarg;
1.10 kristaps 241: break;
1.173 schwarze 242: case 'O':
1.180 schwarze 243: search.outkey = optarg;
1.49 kristaps 244: (void)strlcat(curp.outopts, optarg, BUFSIZ);
245: (void)strlcat(curp.outopts, ",", BUFSIZ);
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.180 schwarze 340: manpath_parse(&paths, conf_file, defpaths, auxpaths);
1.218 schwarze 341: #if HAVE_SQLITE3
1.180 schwarze 342: mansearch_setup(1);
343: if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
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)
355: fs_search(&search, &paths, argc, argv, &res, &sz);
1.187 schwarze 356:
357: if (sz == 0) {
358: rc = MANDOCLEVEL_BADARG;
359: goto out;
360: }
1.182 schwarze 361:
362: /*
363: * For standard man(1) and -a output mode,
364: * prepare for copying filename pointers
365: * into the program parameter array.
366: */
367:
368: if (outmode == OUTMODE_ONE) {
369: argc = 1;
370: best_prio = 10;
1.186 schwarze 371: } else if (outmode == OUTMODE_ALL)
1.182 schwarze 372: argc = (int)sz;
373:
374: /* Iterate all matching manuals. */
375:
1.213 schwarze 376: resp = res;
1.181 schwarze 377: for (i = 0; i < sz; i++) {
378: if (outmode == OUTMODE_FLN)
379: puts(res[i].file);
1.182 schwarze 380: else if (outmode == OUTMODE_LST)
1.181 schwarze 381: printf("%s - %s\n", res[i].names,
382: res[i].output == NULL ? "" :
383: res[i].output);
1.186 schwarze 384: else if (outmode == OUTMODE_ONE) {
1.182 schwarze 385: /* Search for the best section. */
386: isec = strcspn(res[i].file, "123456789");
387: sec = res[i].file[isec];
388: if ('\0' == sec)
389: continue;
390: prio = sec_prios[sec - '1'];
391: if (prio >= best_prio)
392: continue;
393: best_prio = prio;
1.186 schwarze 394: resp = res + i;
1.182 schwarze 395: }
1.181 schwarze 396: }
1.182 schwarze 397:
398: /*
399: * For man(1), -a and -i output mode, fall through
400: * to the main mandoc(1) code iterating files
401: * and running the parsers on each of them.
402: */
403:
404: if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
405: goto out;
1.180 schwarze 406: }
407:
408: /* mandoc(1) */
409:
1.209 schwarze 410: if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
1.180 schwarze 411: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 412:
1.195 schwarze 413: curp.mchars = mchars_alloc();
414: curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
415: curp.mchars, defos);
1.154 kristaps 416:
1.165 kristaps 417: /*
418: * Conditionally start up the lookaside buffer before parsing.
419: */
420: if (OUTT_MAN == curp.outtype)
421: mparse_keep(curp.mp);
422:
1.219 schwarze 423: if (argc < 1) {
1.225 schwarze 424: if (pager_pid == 1 && isatty(STDOUT_FILENO))
425: pager_pid = spawn_pager();
1.154 kristaps 426: parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.212 schwarze 427: }
1.64 kristaps 428:
1.219 schwarze 429: while (argc > 0) {
1.214 schwarze 430: rctmp = mparse_open(curp.mp, &fd,
1.218 schwarze 431: resp != NULL ? resp->file : *argv);
1.214 schwarze 432: if (rc < rctmp)
433: rc = rctmp;
1.212 schwarze 434:
435: if (fd != -1) {
1.225 schwarze 436: if (pager_pid == 1 && isatty(STDOUT_FILENO))
437: pager_pid = spawn_pager();
1.212 schwarze 438:
439: if (resp == NULL)
440: parse(&curp, fd, *argv, &rc);
1.192 schwarze 441: else if (resp->form & FORM_SRC) {
1.189 schwarze 442: /* For .so only; ignore failure. */
443: chdir(paths.paths[resp->ipath]);
1.192 schwarze 444: parse(&curp, fd, resp->file, &rc);
1.214 schwarze 445: } else {
446: rctmp = passthrough(resp->file, fd,
1.197 schwarze 447: synopsis_only);
1.214 schwarze 448: if (rc < rctmp)
449: rc = rctmp;
450: }
1.212 schwarze 451:
1.214 schwarze 452: rctmp = mparse_wait(curp.mp);
453: if (rc < rctmp)
454: rc = rctmp;
1.212 schwarze 455:
456: if (argc > 1 && curp.outtype <= OUTT_UTF8)
457: ascii_sepline(curp.outdata);
1.192 schwarze 458: }
459:
1.154 kristaps 460: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64 kristaps 461: break;
1.210 schwarze 462:
1.212 schwarze 463: if (resp != NULL)
464: resp++;
465: else
466: argv++;
467: if (--argc)
468: mparse_reset(curp.mp);
1.1 kristaps 469: }
470:
1.22 kristaps 471: if (curp.outfree)
472: (*curp.outfree)(curp.outdata);
1.195 schwarze 473: mparse_free(curp.mp);
474: mchars_free(curp.mchars);
1.182 schwarze 475:
476: out:
477: if (search.argmode != ARG_FILE) {
1.189 schwarze 478: manpath_free(&paths);
1.218 schwarze 479: #if HAVE_SQLITE3
1.182 schwarze 480: mansearch_free(res, sz);
481: mansearch_setup(0);
1.218 schwarze 482: #endif
1.182 schwarze 483: }
484:
1.166 schwarze 485: free(defos);
1.1 kristaps 486:
1.224 schwarze 487: /*
1.225 schwarze 488: * If a pager is attached, flush the pipe leading to it
489: * and signal end of file such that the user can browse
490: * to the end. Then wait for the user to close the pager.
1.224 schwarze 491: */
492:
1.225 schwarze 493: if (pager_pid != 0 && pager_pid != 1) {
494: fclose(stdout);
495: waitpid(pager_pid, NULL, 0);
496: }
1.224 schwarze 497:
1.154 kristaps 498: return((int)rc);
1.1 kristaps 499: }
500:
1.55 kristaps 501: static void
1.180 schwarze 502: usage(enum argmode argmode)
1.1 kristaps 503: {
504:
1.180 schwarze 505: switch (argmode) {
506: case ARG_FILE:
1.221 schwarze 507: fputs("usage: mandoc [-acfhkl] [-Ios=name] "
1.196 schwarze 508: "[-Kencoding] [-mformat] [-Ooption]\n"
509: "\t [-Toutput] [-Wlevel] [file ...]\n", stderr);
1.180 schwarze 510: break;
511: case ARG_NAME:
1.221 schwarze 512: fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
1.208 schwarze 513: "[-K encoding] [-M path] [-m path]\n"
514: "\t [-O option=value] [-S subsection] [-s section] "
515: "[-T output] [-W level]\n"
1.180 schwarze 516: "\t [section] name ...\n", stderr);
517: break;
518: case ARG_WORD:
1.221 schwarze 519: fputs("usage: whatis [-acfhklw] [-C file] "
1.188 schwarze 520: "[-M path] [-m path] [-O outkey] [-S arch]\n"
521: "\t [-s section] name ...\n", stderr);
1.180 schwarze 522: break;
523: case ARG_EXPR:
1.221 schwarze 524: fputs("usage: apropos [-acfhklw] [-C file] "
1.188 schwarze 525: "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.180 schwarze 526: "\t [-s section] expression ...\n", stderr);
527: break;
528: }
1.105 kristaps 529: exit((int)MANDOCLEVEL_BADARG);
1.68 joerg 530: }
1.213 schwarze 531:
532: static int
533: fs_lookup(const struct manpaths *paths, size_t ipath,
534: const char *sec, const char *arch, const char *name,
535: struct manpage **res, size_t *ressz)
536: {
1.222 schwarze 537: glob_t globinfo;
1.213 schwarze 538: struct manpage *page;
539: char *file;
1.222 schwarze 540: int form, globres;
1.213 schwarze 541:
1.222 schwarze 542: form = FORM_SRC;
1.213 schwarze 543: mandoc_asprintf(&file, "%s/man%s/%s.%s",
544: paths->paths[ipath], sec, name, sec);
1.222 schwarze 545: if (access(file, R_OK) != -1)
1.213 schwarze 546: goto found;
547: free(file);
548:
549: mandoc_asprintf(&file, "%s/cat%s/%s.0",
550: paths->paths[ipath], sec, name);
551: if (access(file, R_OK) != -1) {
552: form = FORM_CAT;
553: goto found;
554: }
555: free(file);
556:
557: if (arch != NULL) {
558: mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
559: paths->paths[ipath], sec, arch, name, sec);
1.222 schwarze 560: if (access(file, R_OK) != -1)
1.213 schwarze 561: goto found;
562: free(file);
563: }
1.222 schwarze 564:
565: mandoc_asprintf(&file, "%s/man%s/%s.*",
566: paths->paths[ipath], sec, name);
567: globres = glob(file, 0, NULL, &globinfo);
568: if (globres != 0 && globres != GLOB_NOMATCH)
569: fprintf(stderr, "%s: %s: glob: %s\n",
570: progname, file, strerror(errno));
571: free(file);
572: if (globres == 0)
573: file = mandoc_strdup(*globinfo.gl_pathv);
574: globfree(&globinfo);
575: if (globres != 0)
576: return(0);
1.213 schwarze 577:
578: found:
1.218 schwarze 579: #if HAVE_SQLITE3
1.213 schwarze 580: fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
581: " consider running # makewhatis %s\n",
582: progname, name, sec, paths->paths[ipath]);
1.218 schwarze 583: #endif
1.220 schwarze 584:
1.213 schwarze 585: *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
586: page = *res + (*ressz - 1);
587: page->file = file;
588: page->names = NULL;
589: page->output = NULL;
590: page->ipath = ipath;
591: page->bits = NAME_FILE & NAME_MASK;
592: page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
593: page->form = form;
594: return(1);
595: }
596:
597: static void
598: fs_search(const struct mansearch *cfg, const struct manpaths *paths,
599: int argc, char **argv, struct manpage **res, size_t *ressz)
600: {
601: const char *const sections[] =
602: {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
603: const size_t nsec = sizeof(sections)/sizeof(sections[0]);
604:
605: size_t ipath, isec, lastsz;
606:
607: assert(cfg->argmode == ARG_NAME);
608:
609: *res = NULL;
610: *ressz = lastsz = 0;
611: while (argc) {
612: for (ipath = 0; ipath < paths->sz; ipath++) {
613: if (cfg->sec != NULL) {
614: if (fs_lookup(paths, ipath, cfg->sec,
615: cfg->arch, *argv, res, ressz) &&
616: cfg->firstmatch)
617: return;
618: } else for (isec = 0; isec < nsec; isec++)
619: if (fs_lookup(paths, ipath, sections[isec],
620: cfg->arch, *argv, res, ressz) &&
621: cfg->firstmatch)
622: return;
623: }
624: if (*ressz == lastsz)
625: fprintf(stderr,
626: "%s: No entry for %s in the manual.\n",
627: progname, *argv);
628: lastsz = *ressz;
629: argv++;
630: argc--;
631: }
632: }
1.68 joerg 633:
1.64 kristaps 634: static void
1.173 schwarze 635: parse(struct curparse *curp, int fd, const char *file,
636: enum mandoclevel *level)
1.1 kristaps 637: {
1.154 kristaps 638: enum mandoclevel rc;
639: struct mdoc *mdoc;
640: struct man *man;
1.112 kristaps 641:
1.154 kristaps 642: /* Begin by parsing the file itself. */
1.112 kristaps 643:
1.154 kristaps 644: assert(file);
645: assert(fd >= -1);
1.112 kristaps 646:
1.154 kristaps 647: rc = mparse_readfd(curp->mp, fd, file);
1.112 kristaps 648:
1.1 kristaps 649: /*
1.154 kristaps 650: * With -Wstop and warnings or errors of at least the requested
651: * level, do not produce output.
1.1 kristaps 652: */
653:
1.154 kristaps 654: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.111 kristaps 655: goto cleanup;
656:
657: /* If unset, allocate output dev now (if applicable). */
658:
659: if ( ! (curp->outman && curp->outmdoc)) {
660: switch (curp->outtype) {
1.173 schwarze 661: case OUTT_HTML:
1.195 schwarze 662: curp->outdata = html_alloc(curp->mchars,
663: curp->outopts);
1.162 kristaps 664: curp->outfree = html_free;
665: break;
1.173 schwarze 666: case OUTT_UTF8:
1.195 schwarze 667: curp->outdata = utf8_alloc(curp->mchars,
668: curp->outopts);
1.163 kristaps 669: curp->outfree = ascii_free;
670: break;
1.173 schwarze 671: case OUTT_LOCALE:
1.195 schwarze 672: curp->outdata = locale_alloc(curp->mchars,
673: curp->outopts);
1.162 kristaps 674: curp->outfree = ascii_free;
1.111 kristaps 675: break;
1.173 schwarze 676: case OUTT_ASCII:
1.195 schwarze 677: curp->outdata = ascii_alloc(curp->mchars,
678: curp->outopts);
1.111 kristaps 679: curp->outfree = ascii_free;
680: break;
1.173 schwarze 681: case OUTT_PDF:
1.195 schwarze 682: curp->outdata = pdf_alloc(curp->mchars,
683: curp->outopts);
1.111 kristaps 684: curp->outfree = pspdf_free;
685: break;
1.173 schwarze 686: case OUTT_PS:
1.195 schwarze 687: curp->outdata = ps_alloc(curp->mchars,
688: curp->outopts);
1.111 kristaps 689: curp->outfree = pspdf_free;
690: break;
691: default:
692: break;
693: }
694:
695: switch (curp->outtype) {
1.173 schwarze 696: case OUTT_HTML:
1.111 kristaps 697: curp->outman = html_man;
698: curp->outmdoc = html_mdoc;
699: break;
1.173 schwarze 700: case OUTT_TREE:
1.111 kristaps 701: curp->outman = tree_man;
702: curp->outmdoc = tree_mdoc;
703: break;
1.173 schwarze 704: case OUTT_MAN:
1.164 schwarze 705: curp->outmdoc = man_mdoc;
1.165 kristaps 706: curp->outman = man_man;
1.164 schwarze 707: break;
1.173 schwarze 708: case OUTT_PDF:
1.111 kristaps 709: /* FALLTHROUGH */
1.173 schwarze 710: case OUTT_ASCII:
1.111 kristaps 711: /* FALLTHROUGH */
1.173 schwarze 712: case OUTT_UTF8:
1.163 kristaps 713: /* FALLTHROUGH */
1.173 schwarze 714: case OUTT_LOCALE:
1.162 kristaps 715: /* FALLTHROUGH */
1.173 schwarze 716: case OUTT_PS:
1.111 kristaps 717: curp->outman = terminal_man;
718: curp->outmdoc = terminal_mdoc;
719: break;
720: default:
721: break;
722: }
723: }
724:
1.171 schwarze 725: mparse_result(curp->mp, &mdoc, &man, NULL);
1.154 kristaps 726:
1.111 kristaps 727: /* Execute the out device, if it exists. */
728:
1.154 kristaps 729: if (man && curp->outman)
730: (*curp->outman)(curp->outdata, man);
731: if (mdoc && curp->outmdoc)
732: (*curp->outmdoc)(curp->outdata, mdoc);
1.111 kristaps 733:
1.212 schwarze 734: cleanup:
1.154 kristaps 735: if (*level < rc)
736: *level = rc;
1.186 schwarze 737: }
738:
739: static enum mandoclevel
1.197 schwarze 740: passthrough(const char *file, int fd, int synopsis_only)
1.186 schwarze 741: {
1.197 schwarze 742: const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
743: const char synr[] = "SYNOPSIS";
744:
745: FILE *stream;
1.186 schwarze 746: const char *syscall;
1.197 schwarze 747: char *line;
748: size_t len, off;
749: ssize_t nw;
750: int print;
1.210 schwarze 751:
752: fflush(stdout);
1.197 schwarze 753:
754: if ((stream = fdopen(fd, "r")) == NULL) {
755: close(fd);
756: syscall = "fdopen";
757: goto fail;
758: }
1.186 schwarze 759:
1.197 schwarze 760: print = 0;
761: while ((line = fgetln(stream, &len)) != NULL) {
762: if (synopsis_only) {
763: if (print) {
764: if ( ! isspace((unsigned char)*line))
765: goto done;
766: while (len &&
767: isspace((unsigned char)*line)) {
768: line++;
769: len--;
770: }
771: } else {
772: if ((len == sizeof(synb) &&
773: ! strncmp(line, synb, len - 1)) ||
774: (len == sizeof(synr) &&
775: ! strncmp(line, synr, len - 1)))
776: print = 1;
777: continue;
778: }
779: }
780: for (off = 0; off < len; off += nw)
781: if ((nw = write(STDOUT_FILENO, line + off,
782: len - off)) == -1 || nw == 0) {
783: fclose(stream);
1.186 schwarze 784: syscall = "write";
785: goto fail;
786: }
1.197 schwarze 787: }
1.186 schwarze 788:
1.197 schwarze 789: if (ferror(stream)) {
790: fclose(stream);
791: syscall = "fgetln";
792: goto fail;
793: }
1.193 schwarze 794:
1.197 schwarze 795: done:
796: fclose(stream);
797: return(MANDOCLEVEL_OK);
1.186 schwarze 798:
799: fail:
800: fprintf(stderr, "%s: %s: SYSERR: %s: %s",
801: progname, file, syscall, strerror(errno));
802: return(MANDOCLEVEL_SYSERR);
1.194 schwarze 803: }
804:
805: static int
806: koptions(int *options, char *arg)
807: {
808:
809: if ( ! strcmp(arg, "utf-8")) {
810: *options |= MPARSE_UTF8;
811: *options &= ~MPARSE_LATIN1;
812: } else if ( ! strcmp(arg, "iso-8859-1")) {
813: *options |= MPARSE_LATIN1;
814: *options &= ~MPARSE_UTF8;
815: } else if ( ! strcmp(arg, "us-ascii")) {
816: *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
817: } else {
1.209 schwarze 818: fprintf(stderr, "%s: -K %s: Bad argument\n",
1.194 schwarze 819: progname, arg);
820: return(0);
821: }
822: return(1);
1.10 kristaps 823: }
824:
825: static int
1.170 schwarze 826: moptions(int *options, char *arg)
1.10 kristaps 827: {
828:
1.180 schwarze 829: if (arg == NULL)
830: /* nothing to do */;
831: else if (0 == strcmp(arg, "doc"))
1.170 schwarze 832: *options |= MPARSE_MDOC;
1.19 kristaps 833: else if (0 == strcmp(arg, "andoc"))
1.170 schwarze 834: /* nothing to do */;
1.17 kristaps 835: else if (0 == strcmp(arg, "an"))
1.170 schwarze 836: *options |= MPARSE_MAN;
1.10 kristaps 837: else {
1.209 schwarze 838: fprintf(stderr, "%s: -m %s: Bad argument\n",
1.176 schwarze 839: progname, arg);
1.10 kristaps 840: return(0);
841: }
842:
843: return(1);
1.1 kristaps 844: }
845:
846: static int
1.60 kristaps 847: toptions(struct curparse *curp, char *arg)
1.1 kristaps 848: {
849:
850: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 851: curp->outtype = OUTT_ASCII;
852: else if (0 == strcmp(arg, "lint")) {
853: curp->outtype = OUTT_LINT;
1.103 schwarze 854: curp->wlevel = MANDOCLEVEL_WARNING;
1.154 kristaps 855: } else if (0 == strcmp(arg, "tree"))
1.60 kristaps 856: curp->outtype = OUTT_TREE;
1.164 schwarze 857: else if (0 == strcmp(arg, "man"))
858: curp->outtype = OUTT_MAN;
1.43 kristaps 859: else if (0 == strcmp(arg, "html"))
1.60 kristaps 860: curp->outtype = OUTT_HTML;
1.163 kristaps 861: else if (0 == strcmp(arg, "utf8"))
862: curp->outtype = OUTT_UTF8;
1.162 kristaps 863: else if (0 == strcmp(arg, "locale"))
864: curp->outtype = OUTT_LOCALE;
1.59 kristaps 865: else if (0 == strcmp(arg, "xhtml"))
1.195 schwarze 866: curp->outtype = OUTT_HTML;
1.85 kristaps 867: else if (0 == strcmp(arg, "ps"))
868: curp->outtype = OUTT_PS;
1.100 kristaps 869: else if (0 == strcmp(arg, "pdf"))
870: curp->outtype = OUTT_PDF;
1.1 kristaps 871: else {
1.209 schwarze 872: fprintf(stderr, "%s: -T %s: Bad argument\n",
1.176 schwarze 873: progname, arg);
1.1 kristaps 874: return(0);
875: }
876:
877: return(1);
878: }
879:
880: static int
1.103 schwarze 881: woptions(struct curparse *curp, char *arg)
1.1 kristaps 882: {
1.34 kristaps 883: char *v, *o;
1.217 schwarze 884: const char *toks[7];
1.1 kristaps 885:
1.103 schwarze 886: toks[0] = "stop";
887: toks[1] = "all";
888: toks[2] = "warning";
889: toks[3] = "error";
1.217 schwarze 890: toks[4] = "unsupp";
891: toks[5] = "fatal";
892: toks[6] = NULL;
1.1 kristaps 893:
1.34 kristaps 894: while (*arg) {
895: o = arg;
1.45 kristaps 896: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.173 schwarze 897: case 0:
1.103 schwarze 898: curp->wstop = 1;
1.1 kristaps 899: break;
1.173 schwarze 900: case 1:
1.103 schwarze 901: /* FALLTHROUGH */
1.173 schwarze 902: case 2:
1.103 schwarze 903: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 904: break;
1.173 schwarze 905: case 3:
1.103 schwarze 906: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 907: break;
1.173 schwarze 908: case 4:
1.217 schwarze 909: curp->wlevel = MANDOCLEVEL_UNSUPP;
910: break;
911: case 5:
1.215 schwarze 912: curp->wlevel = MANDOCLEVEL_BADARG;
1.50 kristaps 913: break;
1.1 kristaps 914: default:
1.209 schwarze 915: fprintf(stderr, "%s: -W %s: Bad argument\n",
1.176 schwarze 916: progname, o);
1.1 kristaps 917: return(0);
918: }
1.34 kristaps 919: }
1.1 kristaps 920:
921: return(1);
922: }
923:
1.153 kristaps 924: static void
1.173 schwarze 925: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.155 kristaps 926: const char *file, int line, int col, const char *msg)
1.71 kristaps 927: {
1.177 schwarze 928: const char *mparse_msg;
1.103 schwarze 929:
1.175 schwarze 930: fprintf(stderr, "%s: %s:", progname, file);
931:
932: if (line)
933: fprintf(stderr, "%d:%d:", line, col + 1);
934:
1.177 schwarze 935: fprintf(stderr, " %s", mparse_strlevel(lvl));
936:
937: if (NULL != (mparse_msg = mparse_strerror(t)))
938: fprintf(stderr, ": %s", mparse_msg);
1.154 kristaps 939:
1.73 kristaps 940: if (msg)
941: fprintf(stderr, ": %s", msg);
1.154 kristaps 942:
1.73 kristaps 943: fputc('\n', stderr);
1.183 schwarze 944: }
945:
1.226 ! schwarze 946: static void
! 947: handle_sigpipe(int signum)
! 948: {
! 949:
! 950: exit(rc);
! 951: }
! 952:
1.225 schwarze 953: static pid_t
1.183 schwarze 954: spawn_pager(void)
955: {
1.184 schwarze 956: #define MAX_PAGER_ARGS 16
957: char *argv[MAX_PAGER_ARGS];
958: const char *pager;
959: char *cp;
960: int fildes[2];
961: int argc;
1.225 schwarze 962: pid_t pager_pid;
1.183 schwarze 963:
964: if (pipe(fildes) == -1) {
965: fprintf(stderr, "%s: pipe: %s\n",
966: progname, strerror(errno));
1.225 schwarze 967: return(0);
1.183 schwarze 968: }
969:
1.225 schwarze 970: switch (pager_pid = fork()) {
1.183 schwarze 971: case -1:
972: fprintf(stderr, "%s: fork: %s\n",
973: progname, strerror(errno));
974: exit((int)MANDOCLEVEL_SYSERR);
975: case 0:
1.224 schwarze 976: break;
977: default:
1.183 schwarze 978: close(fildes[0]);
979: if (dup2(fildes[1], STDOUT_FILENO) == -1) {
980: fprintf(stderr, "%s: dup output: %s\n",
981: progname, strerror(errno));
982: exit((int)MANDOCLEVEL_SYSERR);
983: }
1.224 schwarze 984: close(fildes[1]);
1.226 ! schwarze 985: signal(SIGPIPE, handle_sigpipe);
1.225 schwarze 986: return(pager_pid);
1.184 schwarze 987: }
988:
1.224 schwarze 989: /* The child process becomes the pager. */
1.184 schwarze 990:
991: close(fildes[1]);
992: if (dup2(fildes[0], STDIN_FILENO) == -1) {
993: fprintf(stderr, "%s: dup input: %s\n",
994: progname, strerror(errno));
1.183 schwarze 995: exit((int)MANDOCLEVEL_SYSERR);
996: }
1.224 schwarze 997: close(fildes[0]);
1.184 schwarze 998:
999: pager = getenv("MANPAGER");
1000: if (pager == NULL || *pager == '\0')
1001: pager = getenv("PAGER");
1002: if (pager == NULL || *pager == '\0')
1003: pager = "/usr/bin/more -s";
1004: cp = mandoc_strdup(pager);
1005:
1006: /*
1007: * Parse the pager command into words.
1008: * Intentionally do not do anything fancy here.
1009: */
1010:
1011: argc = 0;
1012: while (argc + 1 < MAX_PAGER_ARGS) {
1013: argv[argc++] = cp;
1014: cp = strchr(cp, ' ');
1015: if (cp == NULL)
1016: break;
1017: *cp++ = '\0';
1018: while (*cp == ' ')
1019: cp++;
1020: if (*cp == '\0')
1021: break;
1022: }
1023: argv[argc] = NULL;
1024:
1025: /* Hand over to the pager. */
1026:
1027: execvp(argv[0], argv);
1028: fprintf(stderr, "%s: exec: %s\n",
1029: progname, strerror(errno));
1030: exit((int)MANDOCLEVEL_SYSERR);
1.71 kristaps 1031: }
CVSweb