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