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