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