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