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