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