Annotation of mandoc/main.c, Revision 1.242
1.242 ! schwarze 1: /* $Id: main.c,v 1.241 2015/07/19 00:07:42 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:
461: if (argc > 1 && curp.outtype <= OUTT_UTF8)
462: ascii_sepline(curp.outdata);
1.192 schwarze 463: }
464:
1.154 kristaps 465: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64 kristaps 466: break;
1.210 schwarze 467:
1.212 schwarze 468: if (resp != NULL)
469: resp++;
470: else
471: argv++;
472: if (--argc)
473: mparse_reset(curp.mp);
1.1 kristaps 474: }
475:
1.22 kristaps 476: if (curp.outfree)
477: (*curp.outfree)(curp.outdata);
1.195 schwarze 478: mparse_free(curp.mp);
479: mchars_free(curp.mchars);
1.182 schwarze 480:
481: out:
482: if (search.argmode != ARG_FILE) {
1.229 schwarze 483: manconf_free(&conf);
1.218 schwarze 484: #if HAVE_SQLITE3
1.182 schwarze 485: mansearch_free(res, sz);
486: mansearch_setup(0);
1.218 schwarze 487: #endif
1.182 schwarze 488: }
489:
1.166 schwarze 490: free(defos);
1.1 kristaps 491:
1.224 schwarze 492: /*
1.225 schwarze 493: * If a pager is attached, flush the pipe leading to it
494: * and signal end of file such that the user can browse
495: * to the end. Then wait for the user to close the pager.
1.224 schwarze 496: */
497:
1.225 schwarze 498: if (pager_pid != 0 && pager_pid != 1) {
499: fclose(stdout);
1.239 schwarze 500: tag_write();
1.225 schwarze 501: waitpid(pager_pid, NULL, 0);
1.239 schwarze 502: tag_unlink();
1.225 schwarze 503: }
1.224 schwarze 504:
1.154 kristaps 505: return((int)rc);
1.1 kristaps 506: }
507:
1.55 kristaps 508: static void
1.180 schwarze 509: usage(enum argmode argmode)
1.1 kristaps 510: {
511:
1.180 schwarze 512: switch (argmode) {
513: case ARG_FILE:
1.228 schwarze 514: fputs("usage: mandoc [-acfhkl] [-I os=name] "
515: "[-K encoding] [-mformat] [-O option]\n"
516: "\t [-T output] [-W level] [file ...]\n", stderr);
1.180 schwarze 517: break;
518: case ARG_NAME:
1.221 schwarze 519: fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
1.208 schwarze 520: "[-K encoding] [-M path] [-m path]\n"
521: "\t [-O option=value] [-S subsection] [-s section] "
522: "[-T output] [-W level]\n"
1.180 schwarze 523: "\t [section] name ...\n", stderr);
524: break;
525: case ARG_WORD:
1.221 schwarze 526: fputs("usage: whatis [-acfhklw] [-C file] "
1.188 schwarze 527: "[-M path] [-m path] [-O outkey] [-S arch]\n"
528: "\t [-s section] name ...\n", stderr);
1.180 schwarze 529: break;
530: case ARG_EXPR:
1.221 schwarze 531: fputs("usage: apropos [-acfhklw] [-C file] "
1.188 schwarze 532: "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.180 schwarze 533: "\t [-s section] expression ...\n", stderr);
534: break;
535: }
1.105 kristaps 536: exit((int)MANDOCLEVEL_BADARG);
1.68 joerg 537: }
1.213 schwarze 538:
539: static int
540: fs_lookup(const struct manpaths *paths, size_t ipath,
541: const char *sec, const char *arch, const char *name,
542: struct manpage **res, size_t *ressz)
543: {
1.222 schwarze 544: glob_t globinfo;
1.213 schwarze 545: struct manpage *page;
546: char *file;
1.222 schwarze 547: int form, globres;
1.213 schwarze 548:
1.222 schwarze 549: form = FORM_SRC;
1.213 schwarze 550: mandoc_asprintf(&file, "%s/man%s/%s.%s",
551: paths->paths[ipath], sec, name, sec);
1.222 schwarze 552: if (access(file, R_OK) != -1)
1.213 schwarze 553: goto found;
554: free(file);
555:
556: mandoc_asprintf(&file, "%s/cat%s/%s.0",
557: paths->paths[ipath], sec, name);
558: if (access(file, R_OK) != -1) {
559: form = FORM_CAT;
560: goto found;
561: }
562: free(file);
563:
564: if (arch != NULL) {
565: mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
566: paths->paths[ipath], sec, arch, name, sec);
1.222 schwarze 567: if (access(file, R_OK) != -1)
1.213 schwarze 568: goto found;
569: free(file);
570: }
1.222 schwarze 571:
1.240 schwarze 572: mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
1.222 schwarze 573: paths->paths[ipath], sec, name);
574: globres = glob(file, 0, NULL, &globinfo);
575: if (globres != 0 && globres != GLOB_NOMATCH)
576: fprintf(stderr, "%s: %s: glob: %s\n",
577: progname, file, strerror(errno));
578: free(file);
579: if (globres == 0)
580: file = mandoc_strdup(*globinfo.gl_pathv);
581: globfree(&globinfo);
582: if (globres != 0)
583: return(0);
1.213 schwarze 584:
585: found:
1.218 schwarze 586: #if HAVE_SQLITE3
1.233 schwarze 587: fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry, run "
588: "makewhatis %s\n", progname, name, sec, paths->paths[ipath]);
1.218 schwarze 589: #endif
1.213 schwarze 590: *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
591: page = *res + (*ressz - 1);
592: page->file = file;
593: page->names = NULL;
594: page->output = NULL;
595: page->ipath = ipath;
596: page->bits = NAME_FILE & NAME_MASK;
597: page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
598: page->form = form;
599: return(1);
600: }
601:
602: static void
603: fs_search(const struct mansearch *cfg, const struct manpaths *paths,
604: int argc, char **argv, struct manpage **res, size_t *ressz)
605: {
606: const char *const sections[] =
607: {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
608: const size_t nsec = sizeof(sections)/sizeof(sections[0]);
609:
610: size_t ipath, isec, lastsz;
611:
612: assert(cfg->argmode == ARG_NAME);
613:
614: *res = NULL;
615: *ressz = lastsz = 0;
616: while (argc) {
617: for (ipath = 0; ipath < paths->sz; ipath++) {
618: if (cfg->sec != NULL) {
619: if (fs_lookup(paths, ipath, cfg->sec,
620: cfg->arch, *argv, res, ressz) &&
621: cfg->firstmatch)
622: return;
623: } else for (isec = 0; isec < nsec; isec++)
624: if (fs_lookup(paths, ipath, sections[isec],
625: cfg->arch, *argv, res, ressz) &&
626: cfg->firstmatch)
627: return;
628: }
629: if (*ressz == lastsz)
630: fprintf(stderr,
631: "%s: No entry for %s in the manual.\n",
632: progname, *argv);
633: lastsz = *ressz;
634: argv++;
635: argc--;
636: }
637: }
1.68 joerg 638:
1.64 kristaps 639: static void
1.227 schwarze 640: parse(struct curparse *curp, int fd, const char *file)
1.1 kristaps 641: {
1.227 schwarze 642: enum mandoclevel rctmp;
1.234 schwarze 643: struct roff_man *man;
1.112 kristaps 644:
1.154 kristaps 645: /* Begin by parsing the file itself. */
1.112 kristaps 646:
1.154 kristaps 647: assert(file);
648: assert(fd >= -1);
1.112 kristaps 649:
1.227 schwarze 650: rctmp = mparse_readfd(curp->mp, fd, file);
651: if (rc < rctmp)
652: rc = rctmp;
1.112 kristaps 653:
1.1 kristaps 654: /*
1.154 kristaps 655: * With -Wstop and warnings or errors of at least the requested
656: * level, do not produce output.
1.1 kristaps 657: */
658:
1.227 schwarze 659: if (rctmp != MANDOCLEVEL_OK && curp->wstop)
660: return;
1.111 kristaps 661:
662: /* If unset, allocate output dev now (if applicable). */
663:
664: if ( ! (curp->outman && curp->outmdoc)) {
665: switch (curp->outtype) {
1.173 schwarze 666: case OUTT_HTML:
1.195 schwarze 667: curp->outdata = html_alloc(curp->mchars,
668: curp->outopts);
1.162 kristaps 669: curp->outfree = html_free;
670: break;
1.173 schwarze 671: case OUTT_UTF8:
1.195 schwarze 672: curp->outdata = utf8_alloc(curp->mchars,
673: curp->outopts);
1.163 kristaps 674: curp->outfree = ascii_free;
675: break;
1.173 schwarze 676: case OUTT_LOCALE:
1.195 schwarze 677: curp->outdata = locale_alloc(curp->mchars,
678: curp->outopts);
1.162 kristaps 679: curp->outfree = ascii_free;
1.111 kristaps 680: break;
1.173 schwarze 681: case OUTT_ASCII:
1.195 schwarze 682: curp->outdata = ascii_alloc(curp->mchars,
683: curp->outopts);
1.111 kristaps 684: curp->outfree = ascii_free;
685: break;
1.173 schwarze 686: case OUTT_PDF:
1.195 schwarze 687: curp->outdata = pdf_alloc(curp->mchars,
688: curp->outopts);
1.111 kristaps 689: curp->outfree = pspdf_free;
690: break;
1.173 schwarze 691: case OUTT_PS:
1.195 schwarze 692: curp->outdata = ps_alloc(curp->mchars,
693: curp->outopts);
1.111 kristaps 694: curp->outfree = pspdf_free;
695: break;
696: default:
697: break;
698: }
699:
700: switch (curp->outtype) {
1.173 schwarze 701: case OUTT_HTML:
1.111 kristaps 702: curp->outman = html_man;
703: curp->outmdoc = html_mdoc;
704: break;
1.173 schwarze 705: case OUTT_TREE:
1.111 kristaps 706: curp->outman = tree_man;
707: curp->outmdoc = tree_mdoc;
708: break;
1.173 schwarze 709: case OUTT_MAN:
1.164 schwarze 710: curp->outmdoc = man_mdoc;
1.165 kristaps 711: curp->outman = man_man;
1.164 schwarze 712: break;
1.173 schwarze 713: case OUTT_PDF:
1.111 kristaps 714: /* FALLTHROUGH */
1.173 schwarze 715: case OUTT_ASCII:
1.111 kristaps 716: /* FALLTHROUGH */
1.173 schwarze 717: case OUTT_UTF8:
1.163 kristaps 718: /* FALLTHROUGH */
1.173 schwarze 719: case OUTT_LOCALE:
1.162 kristaps 720: /* FALLTHROUGH */
1.173 schwarze 721: case OUTT_PS:
1.111 kristaps 722: curp->outman = terminal_man;
723: curp->outmdoc = terminal_mdoc;
724: break;
725: default:
726: break;
727: }
728: }
729:
1.235 schwarze 730: mparse_result(curp->mp, &man, NULL);
1.154 kristaps 731:
1.111 kristaps 732: /* Execute the out device, if it exists. */
733:
1.235 schwarze 734: if (man == NULL)
735: return;
736: if (curp->outmdoc != NULL && man->macroset == MACROSET_MDOC)
737: (*curp->outmdoc)(curp->outdata, man);
738: if (curp->outman != NULL && man->macroset == MACROSET_MAN)
1.154 kristaps 739: (*curp->outman)(curp->outdata, man);
1.186 schwarze 740: }
741:
1.227 schwarze 742: static void
1.197 schwarze 743: passthrough(const char *file, int fd, int synopsis_only)
1.186 schwarze 744: {
1.197 schwarze 745: const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
746: const char synr[] = "SYNOPSIS";
747:
748: FILE *stream;
1.186 schwarze 749: const char *syscall;
1.197 schwarze 750: char *line;
751: size_t len, off;
752: ssize_t nw;
753: int print;
1.210 schwarze 754:
755: fflush(stdout);
1.197 schwarze 756:
757: if ((stream = fdopen(fd, "r")) == NULL) {
758: close(fd);
759: syscall = "fdopen";
760: goto fail;
761: }
1.186 schwarze 762:
1.197 schwarze 763: print = 0;
764: while ((line = fgetln(stream, &len)) != NULL) {
765: if (synopsis_only) {
766: if (print) {
767: if ( ! isspace((unsigned char)*line))
768: goto done;
769: while (len &&
770: isspace((unsigned char)*line)) {
771: line++;
772: len--;
773: }
774: } else {
775: if ((len == sizeof(synb) &&
776: ! strncmp(line, synb, len - 1)) ||
777: (len == sizeof(synr) &&
778: ! strncmp(line, synr, len - 1)))
779: print = 1;
780: continue;
781: }
782: }
783: for (off = 0; off < len; off += nw)
784: if ((nw = write(STDOUT_FILENO, line + off,
785: len - off)) == -1 || nw == 0) {
786: fclose(stream);
1.186 schwarze 787: syscall = "write";
788: goto fail;
789: }
1.197 schwarze 790: }
1.186 schwarze 791:
1.197 schwarze 792: if (ferror(stream)) {
793: fclose(stream);
794: syscall = "fgetln";
795: goto fail;
796: }
1.193 schwarze 797:
1.197 schwarze 798: done:
799: fclose(stream);
1.227 schwarze 800: return;
1.186 schwarze 801:
802: fail:
803: fprintf(stderr, "%s: %s: SYSERR: %s: %s",
804: progname, file, syscall, strerror(errno));
1.227 schwarze 805: if (rc < MANDOCLEVEL_SYSERR)
806: rc = MANDOCLEVEL_SYSERR;
1.194 schwarze 807: }
808:
809: static int
810: koptions(int *options, char *arg)
811: {
812:
813: if ( ! strcmp(arg, "utf-8")) {
814: *options |= MPARSE_UTF8;
815: *options &= ~MPARSE_LATIN1;
816: } else if ( ! strcmp(arg, "iso-8859-1")) {
817: *options |= MPARSE_LATIN1;
818: *options &= ~MPARSE_UTF8;
819: } else if ( ! strcmp(arg, "us-ascii")) {
820: *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
821: } else {
1.209 schwarze 822: fprintf(stderr, "%s: -K %s: Bad argument\n",
1.194 schwarze 823: progname, arg);
824: return(0);
825: }
826: return(1);
1.10 kristaps 827: }
828:
829: static int
1.170 schwarze 830: moptions(int *options, char *arg)
1.10 kristaps 831: {
832:
1.180 schwarze 833: if (arg == NULL)
834: /* nothing to do */;
835: else if (0 == strcmp(arg, "doc"))
1.170 schwarze 836: *options |= MPARSE_MDOC;
1.19 kristaps 837: else if (0 == strcmp(arg, "andoc"))
1.170 schwarze 838: /* nothing to do */;
1.17 kristaps 839: else if (0 == strcmp(arg, "an"))
1.170 schwarze 840: *options |= MPARSE_MAN;
1.10 kristaps 841: else {
1.209 schwarze 842: fprintf(stderr, "%s: -m %s: Bad argument\n",
1.176 schwarze 843: progname, arg);
1.10 kristaps 844: return(0);
845: }
846:
847: return(1);
1.1 kristaps 848: }
849:
850: static int
1.60 kristaps 851: toptions(struct curparse *curp, char *arg)
1.1 kristaps 852: {
853:
854: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 855: curp->outtype = OUTT_ASCII;
856: else if (0 == strcmp(arg, "lint")) {
857: curp->outtype = OUTT_LINT;
1.103 schwarze 858: curp->wlevel = MANDOCLEVEL_WARNING;
1.154 kristaps 859: } else if (0 == strcmp(arg, "tree"))
1.60 kristaps 860: curp->outtype = OUTT_TREE;
1.164 schwarze 861: else if (0 == strcmp(arg, "man"))
862: curp->outtype = OUTT_MAN;
1.43 kristaps 863: else if (0 == strcmp(arg, "html"))
1.60 kristaps 864: curp->outtype = OUTT_HTML;
1.163 kristaps 865: else if (0 == strcmp(arg, "utf8"))
866: curp->outtype = OUTT_UTF8;
1.162 kristaps 867: else if (0 == strcmp(arg, "locale"))
868: curp->outtype = OUTT_LOCALE;
1.59 kristaps 869: else if (0 == strcmp(arg, "xhtml"))
1.195 schwarze 870: curp->outtype = OUTT_HTML;
1.85 kristaps 871: else if (0 == strcmp(arg, "ps"))
872: curp->outtype = OUTT_PS;
1.100 kristaps 873: else if (0 == strcmp(arg, "pdf"))
874: curp->outtype = OUTT_PDF;
1.1 kristaps 875: else {
1.209 schwarze 876: fprintf(stderr, "%s: -T %s: Bad argument\n",
1.176 schwarze 877: progname, arg);
1.1 kristaps 878: return(0);
879: }
880:
881: return(1);
882: }
883:
884: static int
1.103 schwarze 885: woptions(struct curparse *curp, char *arg)
1.1 kristaps 886: {
1.34 kristaps 887: char *v, *o;
1.217 schwarze 888: const char *toks[7];
1.1 kristaps 889:
1.103 schwarze 890: toks[0] = "stop";
891: toks[1] = "all";
892: toks[2] = "warning";
893: toks[3] = "error";
1.217 schwarze 894: toks[4] = "unsupp";
895: toks[5] = "fatal";
896: toks[6] = NULL;
1.1 kristaps 897:
1.34 kristaps 898: while (*arg) {
899: o = arg;
1.45 kristaps 900: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.173 schwarze 901: case 0:
1.103 schwarze 902: curp->wstop = 1;
1.1 kristaps 903: break;
1.173 schwarze 904: case 1:
1.103 schwarze 905: /* FALLTHROUGH */
1.173 schwarze 906: case 2:
1.103 schwarze 907: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 908: break;
1.173 schwarze 909: case 3:
1.103 schwarze 910: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 911: break;
1.173 schwarze 912: case 4:
1.217 schwarze 913: curp->wlevel = MANDOCLEVEL_UNSUPP;
914: break;
915: case 5:
1.215 schwarze 916: curp->wlevel = MANDOCLEVEL_BADARG;
1.50 kristaps 917: break;
1.1 kristaps 918: default:
1.209 schwarze 919: fprintf(stderr, "%s: -W %s: Bad argument\n",
1.176 schwarze 920: progname, o);
1.1 kristaps 921: return(0);
922: }
1.34 kristaps 923: }
1.1 kristaps 924:
925: return(1);
926: }
927:
1.153 kristaps 928: static void
1.173 schwarze 929: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.155 kristaps 930: const char *file, int line, int col, const char *msg)
1.71 kristaps 931: {
1.177 schwarze 932: const char *mparse_msg;
1.103 schwarze 933:
1.175 schwarze 934: fprintf(stderr, "%s: %s:", progname, file);
935:
936: if (line)
937: fprintf(stderr, "%d:%d:", line, col + 1);
938:
1.177 schwarze 939: fprintf(stderr, " %s", mparse_strlevel(lvl));
940:
941: if (NULL != (mparse_msg = mparse_strerror(t)))
942: fprintf(stderr, ": %s", mparse_msg);
1.154 kristaps 943:
1.73 kristaps 944: if (msg)
945: fprintf(stderr, ": %s", msg);
1.154 kristaps 946:
1.73 kristaps 947: fputc('\n', stderr);
1.183 schwarze 948: }
949:
1.226 schwarze 950: static void
951: handle_sigpipe(int signum)
952: {
953:
1.227 schwarze 954: exit((int)rc);
1.226 schwarze 955: }
956:
1.225 schwarze 957: static pid_t
1.183 schwarze 958: spawn_pager(void)
959: {
1.184 schwarze 960: #define MAX_PAGER_ARGS 16
961: char *argv[MAX_PAGER_ARGS];
962: const char *pager;
963: char *cp;
1.239 schwarze 964: size_t cmdlen;
1.184 schwarze 965: int fildes[2];
966: int argc;
1.225 schwarze 967: pid_t pager_pid;
1.183 schwarze 968:
1.239 schwarze 969: pager = getenv("MANPAGER");
970: if (pager == NULL || *pager == '\0')
971: pager = getenv("PAGER");
972: if (pager == NULL || *pager == '\0')
973: pager = "more -s";
974: cp = mandoc_strdup(pager);
975:
976: /*
977: * Parse the pager command into words.
978: * Intentionally do not do anything fancy here.
979: */
980:
981: argc = 0;
982: while (argc + 4 < MAX_PAGER_ARGS) {
983: argv[argc++] = cp;
984: cp = strchr(cp, ' ');
985: if (cp == NULL)
986: break;
987: *cp++ = '\0';
988: while (*cp == ' ')
989: cp++;
990: if (*cp == '\0')
991: break;
992: }
993:
994: /* Read all text right away and use the tag file. */
995:
996: if ((cmdlen = strlen(argv[0])) >= 4) {
997: cp = argv[0] + cmdlen - 4;
998: if (strcmp(cp, "less") == 0 ||
999: strcmp(cp, "more") == 0) {
1000: tag_init();
1001: argv[argc++] = mandoc_strdup("+G1G");
1002: argv[argc++] = mandoc_strdup("-T");
1003: argv[argc++] = tag_filename();
1004: }
1005: }
1006: argv[argc] = NULL;
1007:
1.183 schwarze 1008: if (pipe(fildes) == -1) {
1009: fprintf(stderr, "%s: pipe: %s\n",
1010: progname, strerror(errno));
1.225 schwarze 1011: return(0);
1.183 schwarze 1012: }
1013:
1.225 schwarze 1014: switch (pager_pid = fork()) {
1.183 schwarze 1015: case -1:
1016: fprintf(stderr, "%s: fork: %s\n",
1017: progname, strerror(errno));
1018: exit((int)MANDOCLEVEL_SYSERR);
1019: case 0:
1.224 schwarze 1020: break;
1021: default:
1.183 schwarze 1022: close(fildes[0]);
1023: if (dup2(fildes[1], STDOUT_FILENO) == -1) {
1024: fprintf(stderr, "%s: dup output: %s\n",
1025: progname, strerror(errno));
1026: exit((int)MANDOCLEVEL_SYSERR);
1027: }
1.224 schwarze 1028: close(fildes[1]);
1.226 schwarze 1029: signal(SIGPIPE, handle_sigpipe);
1.225 schwarze 1030: return(pager_pid);
1.184 schwarze 1031: }
1032:
1.224 schwarze 1033: /* The child process becomes the pager. */
1.184 schwarze 1034:
1035: close(fildes[1]);
1036: if (dup2(fildes[0], STDIN_FILENO) == -1) {
1037: fprintf(stderr, "%s: dup input: %s\n",
1038: progname, strerror(errno));
1.183 schwarze 1039: exit((int)MANDOCLEVEL_SYSERR);
1040: }
1.224 schwarze 1041: close(fildes[0]);
1.184 schwarze 1042:
1043: /* Hand over to the pager. */
1044:
1045: execvp(argv[0], argv);
1.238 schwarze 1046: fprintf(stderr, "%s: exec %s: %s\n",
1047: progname, argv[0], strerror(errno));
1.184 schwarze 1048: exit((int)MANDOCLEVEL_SYSERR);
1.71 kristaps 1049: }
CVSweb