1 /* $OpenBSD: main.c,v 1.253 2020/06/15 17:25:03 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2010-2012, 2014-2020 Ingo Schwarze <schwarze@openbsd.org> 4 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv> 5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 * 19 * Main program for mandoc(1), man(1), apropos(1), whatis(1), and help(1). 20 */ 21 #include <sys/types.h> 22 #include <sys/ioctl.h> 23 #include <sys/param.h> /* MACHINE */ 24 #include <sys/stat.h> 25 #include <sys/wait.h> 26 27 #include <assert.h> 28 #include <ctype.h> 29 #include <err.h> 30 #include <errno.h> 31 #include <fcntl.h> 32 #include <glob.h> 33 #include <limits.h> 34 #include <signal.h> 35 #include <stdio.h> 36 #include <stdint.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <termios.h> 40 #include <time.h> 41 #include <unistd.h> 42 43 #include "mandoc_aux.h" 44 #include "mandoc.h" 45 #include "mandoc_xr.h" 46 #include "roff.h" 47 #include "mdoc.h" 48 #include "man.h" 49 #include "mandoc_parse.h" 50 #include "tag.h" 51 #include "term_tag.h" 52 #include "main.h" 53 #include "manconf.h" 54 #include "mansearch.h" 55 56 #define BINM_APROPOS "apropos" 57 #define BINM_MAN "man" 58 #define BINM_MAKEWHATIS "makewhatis" 59 #define BINM_WHATIS "whatis" 60 #define OSENUM MANDOC_OS_OPENBSD 61 62 enum outmode { 63 OUTMODE_DEF = 0, 64 OUTMODE_FLN, 65 OUTMODE_LST, 66 OUTMODE_ALL, 67 OUTMODE_ONE 68 }; 69 70 enum outt { 71 OUTT_ASCII = 0, /* -Tascii */ 72 OUTT_LOCALE, /* -Tlocale */ 73 OUTT_UTF8, /* -Tutf8 */ 74 OUTT_TREE, /* -Ttree */ 75 OUTT_MAN, /* -Tman */ 76 OUTT_HTML, /* -Thtml */ 77 OUTT_MARKDOWN, /* -Tmarkdown */ 78 OUTT_LINT, /* -Tlint */ 79 OUTT_PS, /* -Tps */ 80 OUTT_PDF /* -Tpdf */ 81 }; 82 83 struct outstate { 84 struct tag_files *tag_files; /* Tagging state variables. */ 85 void *outdata; /* data for output */ 86 int use_pager; 87 int wstop; /* stop after a file with a warning */ 88 int had_output; /* Some output was generated. */ 89 enum outt outtype; /* which output to use */ 90 }; 91 92 93 int mandocdb(int, char *[]); 94 95 static void check_xr(void); 96 static int fs_lookup(const struct manpaths *, 97 size_t ipath, const char *, 98 const char *, const char *, 99 struct manpage **, size_t *); 100 static int fs_search(const struct mansearch *, 101 const struct manpaths *, const char *, 102 struct manpage **, size_t *); 103 static void glob_esc(char **, const char *, const char *); 104 static void outdata_alloc(struct outstate *, struct manoutput *); 105 static void parse(struct mparse *, int, const char *, 106 struct outstate *, struct manoutput *); 107 static void passthrough(int, int); 108 static void process_onefile(struct mparse *, struct manpage *, 109 int, struct outstate *, struct manconf *); 110 static void run_pager(struct outstate *, char *); 111 static pid_t spawn_pager(struct outstate *, char *); 112 static void usage(enum argmode) __attribute__((__noreturn__)); 113 static int woptions(char *, enum mandoc_os *, int *); 114 115 static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; 116 static char help_arg[] = "help"; 117 static char *help_argv[] = {help_arg, NULL}; 118 119 120 int 121 main(int argc, char *argv[]) 122 { 123 struct manconf conf; /* Manpaths and output options. */ 124 struct outstate outst; /* Output state. */ 125 struct winsize ws; /* Result of ioctl(TIOCGWINSZ). */ 126 struct mansearch search; /* Search options. */ 127 struct manpage *res; /* Complete list of search results. */ 128 struct manpage *resn; /* Search results for one name. */ 129 struct mparse *mp; /* Opaque parser object. */ 130 const char *conf_file; /* -C: alternate config file. */ 131 const char *os_s; /* -I: Operating system for display. */ 132 const char *progname, *sec; 133 char *defpaths; /* -M: override manpaths. */ 134 char *auxpaths; /* -m: additional manpaths. */ 135 char *oarg; /* -O: output option string. */ 136 char *tagarg; /* -O tag: default value. */ 137 unsigned char *uc; 138 size_t ressz; /* Number of elements in res[]. */ 139 size_t resnsz; /* Number of elements in resn[]. */ 140 size_t i, ib, ssz; 141 int options; /* Parser options. */ 142 int show_usage; /* Invalid argument: give up. */ 143 int prio, best_prio; 144 int startdir; 145 int c; 146 enum mandoc_os os_e; /* Check base system conventions. */ 147 enum outmode outmode; /* According to command line. */ 148 149 progname = getprogname(); 150 mandoc_msg_setoutfile(stderr); 151 if (strncmp(progname, "mandocdb", 8) == 0 || 152 strcmp(progname, BINM_MAKEWHATIS) == 0) 153 return mandocdb(argc, argv); 154 155 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1) { 156 mandoc_msg(MANDOCERR_PLEDGE, 0, 0, "%s", strerror(errno)); 157 return mandoc_msg_getrc(); 158 } 159 160 /* Search options. */ 161 162 memset(&conf, 0, sizeof(conf)); 163 conf_file = NULL; 164 defpaths = auxpaths = NULL; 165 166 memset(&search, 0, sizeof(struct mansearch)); 167 search.outkey = "Nd"; 168 oarg = NULL; 169 170 if (strcmp(progname, BINM_MAN) == 0) 171 search.argmode = ARG_NAME; 172 else if (strcmp(progname, BINM_APROPOS) == 0) 173 search.argmode = ARG_EXPR; 174 else if (strcmp(progname, BINM_WHATIS) == 0) 175 search.argmode = ARG_WORD; 176 else if (strncmp(progname, "help", 4) == 0) 177 search.argmode = ARG_NAME; 178 else 179 search.argmode = ARG_FILE; 180 181 /* Parser options. */ 182 183 options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1; 184 os_e = MANDOC_OS_OTHER; 185 os_s = NULL; 186 187 /* Formatter options. */ 188 189 memset(&outst, 0, sizeof(outst)); 190 outst.tag_files = NULL; 191 outst.outtype = OUTT_LOCALE; 192 outst.use_pager = 1; 193 194 show_usage = 0; 195 outmode = OUTMODE_DEF; 196 197 while ((c = getopt(argc, argv, 198 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) { 199 if (c == 'i' && search.argmode == ARG_EXPR) { 200 optind--; 201 break; 202 } 203 switch (c) { 204 case 'a': 205 outmode = OUTMODE_ALL; 206 break; 207 case 'C': 208 conf_file = optarg; 209 break; 210 case 'c': 211 outst.use_pager = 0; 212 break; 213 case 'f': 214 search.argmode = ARG_WORD; 215 break; 216 case 'h': 217 conf.output.synopsisonly = 1; 218 outst.use_pager = 0; 219 outmode = OUTMODE_ALL; 220 break; 221 case 'I': 222 if (strncmp(optarg, "os=", 3) != 0) { 223 mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, 224 "-I %s", optarg); 225 return mandoc_msg_getrc(); 226 } 227 if (os_s != NULL) { 228 mandoc_msg(MANDOCERR_BADARG_DUPE, 0, 0, 229 "-I %s", optarg); 230 return mandoc_msg_getrc(); 231 } 232 os_s = optarg + 3; 233 break; 234 case 'K': 235 options &= ~(MPARSE_UTF8 | MPARSE_LATIN1); 236 if (strcmp(optarg, "utf-8") == 0) 237 options |= MPARSE_UTF8; 238 else if (strcmp(optarg, "iso-8859-1") == 0) 239 options |= MPARSE_LATIN1; 240 else if (strcmp(optarg, "us-ascii") != 0) { 241 mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, 242 "-K %s", optarg); 243 return mandoc_msg_getrc(); 244 } 245 break; 246 case 'k': 247 search.argmode = ARG_EXPR; 248 break; 249 case 'l': 250 search.argmode = ARG_FILE; 251 outmode = OUTMODE_ALL; 252 break; 253 case 'M': 254 defpaths = optarg; 255 break; 256 case 'm': 257 auxpaths = optarg; 258 break; 259 case 'O': 260 oarg = optarg; 261 break; 262 case 'S': 263 search.arch = optarg; 264 break; 265 case 's': 266 search.sec = optarg; 267 break; 268 case 'T': 269 if (strcmp(optarg, "ascii") == 0) 270 outst.outtype = OUTT_ASCII; 271 else if (strcmp(optarg, "lint") == 0) { 272 outst.outtype = OUTT_LINT; 273 mandoc_msg_setoutfile(stdout); 274 mandoc_msg_setmin(MANDOCERR_BASE); 275 } else if (strcmp(optarg, "tree") == 0) 276 outst.outtype = OUTT_TREE; 277 else if (strcmp(optarg, "man") == 0) 278 outst.outtype = OUTT_MAN; 279 else if (strcmp(optarg, "html") == 0) 280 outst.outtype = OUTT_HTML; 281 else if (strcmp(optarg, "markdown") == 0) 282 outst.outtype = OUTT_MARKDOWN; 283 else if (strcmp(optarg, "utf8") == 0) 284 outst.outtype = OUTT_UTF8; 285 else if (strcmp(optarg, "locale") == 0) 286 outst.outtype = OUTT_LOCALE; 287 else if (strcmp(optarg, "ps") == 0) 288 outst.outtype = OUTT_PS; 289 else if (strcmp(optarg, "pdf") == 0) 290 outst.outtype = OUTT_PDF; 291 else { 292 mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, 293 "-T %s", optarg); 294 return mandoc_msg_getrc(); 295 } 296 break; 297 case 'W': 298 if (woptions(optarg, &os_e, &outst.wstop) == -1) 299 return mandoc_msg_getrc(); 300 break; 301 case 'w': 302 outmode = OUTMODE_FLN; 303 break; 304 default: 305 show_usage = 1; 306 break; 307 } 308 } 309 310 if (show_usage) 311 usage(search.argmode); 312 313 /* Postprocess options. */ 314 315 switch (outmode) { 316 case OUTMODE_DEF: 317 switch (search.argmode) { 318 case ARG_FILE: 319 outmode = OUTMODE_ALL; 320 outst.use_pager = 0; 321 break; 322 case ARG_NAME: 323 outmode = OUTMODE_ONE; 324 break; 325 default: 326 outmode = OUTMODE_LST; 327 break; 328 } 329 break; 330 case OUTMODE_FLN: 331 if (search.argmode == ARG_FILE) 332 outmode = OUTMODE_ALL; 333 break; 334 case OUTMODE_ALL: 335 break; 336 case OUTMODE_LST: 337 case OUTMODE_ONE: 338 abort(); 339 } 340 341 if (oarg != NULL) { 342 if (outmode == OUTMODE_LST) 343 search.outkey = oarg; 344 else { 345 while (oarg != NULL) { 346 if (manconf_output(&conf.output, 347 strsep(&oarg, ","), 0) == -1) 348 return mandoc_msg_getrc(); 349 } 350 } 351 } 352 353 if (outst.outtype != OUTT_TREE || conf.output.noval == 0) 354 options |= MPARSE_VALIDATE; 355 356 if (outmode == OUTMODE_FLN || 357 outmode == OUTMODE_LST || 358 !isatty(STDOUT_FILENO)) 359 outst.use_pager = 0; 360 361 if (outst.use_pager && 362 (conf.output.width == 0 || conf.output.indent == 0) && 363 ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 && 364 ws.ws_col > 1) { 365 if (conf.output.width == 0 && ws.ws_col < 79) 366 conf.output.width = ws.ws_col - 1; 367 if (conf.output.indent == 0 && ws.ws_col < 66) 368 conf.output.indent = 3; 369 } 370 371 if (outst.use_pager == 0) { 372 if (pledge("stdio rpath", NULL) == -1) { 373 mandoc_msg(MANDOCERR_PLEDGE, 0, 0, 374 "%s", strerror(errno)); 375 return mandoc_msg_getrc(); 376 } 377 } 378 379 /* Parse arguments. */ 380 381 if (argc > 0) { 382 argc -= optind; 383 argv += optind; 384 } 385 386 /* 387 * Quirks for help(1) and man(1), 388 * in particular for a section argument without -s. 389 */ 390 391 if (search.argmode == ARG_NAME) { 392 if (*progname == 'h') { 393 if (argc == 0) { 394 argv = help_argv; 395 argc = 1; 396 } 397 } else if (argc > 1 && 398 ((uc = (unsigned char *)argv[0]) != NULL) && 399 ((isdigit(uc[0]) && (uc[1] == '\0' || 400 isalpha(uc[1]))) || 401 (uc[0] == 'n' && uc[1] == '\0'))) { 402 search.sec = (char *)uc; 403 argv++; 404 argc--; 405 } 406 if (search.arch == NULL) 407 search.arch = getenv("MACHINE"); 408 if (search.arch == NULL) 409 search.arch = MACHINE; 410 if (outmode == OUTMODE_ONE) 411 search.firstmatch = 1; 412 } 413 414 /* 415 * Use the first argument for -O tag in addition to 416 * using it as a search term for man(1) or apropos(1). 417 */ 418 419 if (conf.output.tag != NULL && *conf.output.tag == '\0') { 420 tagarg = argc > 0 && search.argmode == ARG_EXPR ? 421 strchr(*argv, '=') : NULL; 422 conf.output.tag = tagarg == NULL ? *argv : tagarg + 1; 423 } 424 425 /* Read the configuration file. */ 426 427 if (search.argmode != ARG_FILE) 428 manconf_parse(&conf, conf_file, defpaths, auxpaths); 429 430 /* man(1): Resolve each name individually. */ 431 432 if (search.argmode == ARG_NAME) { 433 if (argc < 1) { 434 if (outmode != OUTMODE_FLN) 435 usage(ARG_NAME); 436 if (conf.manpath.sz == 0) { 437 warnx("The manpath is empty."); 438 mandoc_msg_setrc(MANDOCLEVEL_BADARG); 439 } else { 440 for (i = 0; i + 1 < conf.manpath.sz; i++) 441 printf("%s:", conf.manpath.paths[i]); 442 printf("%s\n", conf.manpath.paths[i]); 443 } 444 manconf_free(&conf); 445 return (int)mandoc_msg_getrc(); 446 } 447 for (res = NULL, ressz = 0; argc > 0; argc--, argv++) { 448 (void)mansearch(&search, &conf.manpath, 449 1, argv, &resn, &resnsz); 450 if (resnsz == 0) 451 (void)fs_search(&search, &conf.manpath, 452 *argv, &resn, &resnsz); 453 if (resnsz == 0 && strchr(*argv, '/') == NULL) { 454 if (search.arch != NULL && 455 arch_valid(search.arch, OSENUM) == 0) 456 warnx("Unknown architecture \"%s\".", 457 search.arch); 458 else if (search.sec != NULL) 459 warnx("No entry for %s in " 460 "section %s of the manual.", 461 *argv, search.sec); 462 else 463 warnx("No entry for %s in " 464 "the manual.", *argv); 465 mandoc_msg_setrc(MANDOCLEVEL_BADARG); 466 continue; 467 } 468 if (resnsz == 0) { 469 if (access(*argv, R_OK) == -1) { 470 mandoc_msg_setinfilename(*argv); 471 mandoc_msg(MANDOCERR_BADARG_BAD, 472 0, 0, "%s", strerror(errno)); 473 mandoc_msg_setinfilename(NULL); 474 continue; 475 } 476 resnsz = 1; 477 resn = mandoc_calloc(resnsz, sizeof(*res)); 478 resn->file = mandoc_strdup(*argv); 479 resn->ipath = SIZE_MAX; 480 resn->form = FORM_SRC; 481 } 482 if (outmode != OUTMODE_ONE || resnsz == 1) { 483 res = mandoc_reallocarray(res, 484 ressz + resnsz, sizeof(*res)); 485 memcpy(res + ressz, resn, 486 sizeof(*resn) * resnsz); 487 ressz += resnsz; 488 continue; 489 } 490 491 /* Search for the best section. */ 492 493 best_prio = 40; 494 for (ib = i = 0; i < resnsz; i++) { 495 sec = resn[i].file; 496 sec += strcspn(sec, "123456789"); 497 if (sec[0] == '\0') 498 continue; /* No section at all. */ 499 prio = sec_prios[sec[0] - '1']; 500 if (search.sec != NULL) { 501 ssz = strlen(search.sec); 502 if (strncmp(sec, search.sec, ssz) == 0) 503 sec += ssz; 504 } else 505 sec++; /* Prefer without suffix. */ 506 if (*sec != '/') 507 prio += 10; /* Wrong dir name. */ 508 if (search.sec != NULL && 509 (strlen(sec) <= ssz + 3 || 510 strcmp(sec + strlen(sec) - ssz, 511 search.sec) != 0)) 512 prio += 20; /* Wrong file ext. */ 513 if (prio >= best_prio) 514 continue; 515 best_prio = prio; 516 ib = i; 517 } 518 res = mandoc_reallocarray(res, ressz + 1, 519 sizeof(*res)); 520 memcpy(res + ressz++, resn + ib, sizeof(*resn)); 521 } 522 523 /* apropos(1), whatis(1): Process the full search expression. */ 524 525 } else if (search.argmode != ARG_FILE) { 526 if (mansearch(&search, &conf.manpath, 527 argc, argv, &res, &ressz) == 0) 528 usage(search.argmode); 529 530 if (ressz == 0) { 531 warnx("nothing appropriate"); 532 mandoc_msg_setrc(MANDOCLEVEL_BADARG); 533 goto out; 534 } 535 536 /* mandoc(1): Take command line arguments as file names. */ 537 538 } else { 539 ressz = argc > 0 ? argc : 1; 540 res = mandoc_calloc(ressz, sizeof(*res)); 541 for (i = 0; i < ressz; i++) { 542 if (argc > 0) 543 res[i].file = mandoc_strdup(argv[i]); 544 res[i].ipath = SIZE_MAX; 545 res[i].form = FORM_SRC; 546 } 547 } 548 549 switch (outmode) { 550 case OUTMODE_FLN: 551 for (i = 0; i < ressz; i++) 552 puts(res[i].file); 553 goto out; 554 case OUTMODE_LST: 555 for (i = 0; i < ressz; i++) 556 printf("%s - %s\n", res[i].names, 557 res[i].output == NULL ? "" : 558 res[i].output); 559 goto out; 560 default: 561 break; 562 } 563 564 if (search.argmode == ARG_FILE && auxpaths != NULL) { 565 if (strcmp(auxpaths, "doc") == 0) 566 options |= MPARSE_MDOC; 567 else if (strcmp(auxpaths, "an") == 0) 568 options |= MPARSE_MAN; 569 } 570 571 mchars_alloc(); 572 mp = mparse_alloc(options, os_e, os_s); 573 574 /* 575 * Remember the original working directory, if possible. 576 * This will be needed if some names on the command line 577 * are page names and some are relative file names. 578 * Do not error out if the current directory is not 579 * readable: Maybe it won't be needed after all. 580 */ 581 startdir = open(".", O_RDONLY | O_DIRECTORY); 582 for (i = 0; i < ressz; i++) { 583 process_onefile(mp, res + i, startdir, &outst, &conf); 584 if (outst.wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK) 585 break; 586 } 587 if (startdir != -1) { 588 (void)fchdir(startdir); 589 close(startdir); 590 } 591 if (conf.output.tag != NULL && conf.output.tag_found == 0) { 592 mandoc_msg(MANDOCERR_TAG, 0, 0, "%s", conf.output.tag); 593 conf.output.tag = NULL; 594 } 595 if (outst.outdata != NULL) { 596 switch (outst.outtype) { 597 case OUTT_HTML: 598 html_free(outst.outdata); 599 break; 600 case OUTT_UTF8: 601 case OUTT_LOCALE: 602 case OUTT_ASCII: 603 ascii_free(outst.outdata); 604 break; 605 case OUTT_PDF: 606 case OUTT_PS: 607 pspdf_free(outst.outdata); 608 break; 609 default: 610 break; 611 } 612 } 613 mandoc_xr_free(); 614 mparse_free(mp); 615 mchars_free(); 616 617 out: 618 mansearch_free(res, ressz); 619 if (search.argmode != ARG_FILE) 620 manconf_free(&conf); 621 622 if (outst.tag_files != NULL) { 623 if (term_tag_close() != -1) 624 run_pager(&outst, conf.output.tag); 625 term_tag_unlink(); 626 } else if (outst.had_output && outst.outtype != OUTT_LINT) 627 mandoc_msg_summary(); 628 629 return (int)mandoc_msg_getrc(); 630 } 631 632 static void 633 usage(enum argmode argmode) 634 { 635 switch (argmode) { 636 case ARG_FILE: 637 fputs("usage: mandoc [-ac] [-I os=name] " 638 "[-K encoding] [-mdoc | -man] [-O options]\n" 639 "\t [-T output] [-W level] [file ...]\n", stderr); 640 break; 641 case ARG_NAME: 642 fputs("usage: man [-acfhklw] [-C file] [-M path] " 643 "[-m path] [-S subsection]\n" 644 "\t [[-s] section] name ...\n", stderr); 645 break; 646 case ARG_WORD: 647 fputs("usage: whatis [-afk] [-C file] " 648 "[-M path] [-m path] [-O outkey] [-S arch]\n" 649 "\t [-s section] name ...\n", stderr); 650 break; 651 case ARG_EXPR: 652 fputs("usage: apropos [-afk] [-C file] " 653 "[-M path] [-m path] [-O outkey] [-S arch]\n" 654 "\t [-s section] expression ...\n", stderr); 655 break; 656 } 657 exit((int)MANDOCLEVEL_BADARG); 658 } 659 660 static void 661 glob_esc(char **dst, const char *src, const char *suffix) 662 { 663 while (*src != '\0') { 664 if (strchr("*?[", *src) != NULL) 665 *(*dst)++ = '\\'; 666 *(*dst)++ = *src++; 667 } 668 while (*suffix != '\0') 669 *(*dst)++ = *suffix++; 670 } 671 672 static int 673 fs_lookup(const struct manpaths *paths, size_t ipath, 674 const char *sec, const char *arch, const char *name, 675 struct manpage **res, size_t *ressz) 676 { 677 struct stat sb; 678 glob_t globinfo; 679 struct manpage *page; 680 char *file, *cp; 681 int globres; 682 enum form form; 683 684 const char *const slman = "/man"; 685 const char *const slash = "/"; 686 const char *const sglob = ".[01-9]*"; 687 688 form = FORM_SRC; 689 mandoc_asprintf(&file, "%s/man%s/%s.%s", 690 paths->paths[ipath], sec, name, sec); 691 if (stat(file, &sb) != -1) 692 goto found; 693 free(file); 694 695 mandoc_asprintf(&file, "%s/cat%s/%s.0", 696 paths->paths[ipath], sec, name); 697 if (stat(file, &sb) != -1) { 698 form = FORM_CAT; 699 goto found; 700 } 701 free(file); 702 703 if (arch != NULL) { 704 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s", 705 paths->paths[ipath], sec, arch, name, sec); 706 if (stat(file, &sb) != -1) 707 goto found; 708 free(file); 709 } 710 711 cp = file = mandoc_malloc(strlen(paths->paths[ipath]) * 2 + 712 strlen(slman) + strlen(sec) * 2 + strlen(slash) + 713 strlen(name) * 2 + strlen(sglob) + 1); 714 glob_esc(&cp, paths->paths[ipath], slman); 715 glob_esc(&cp, sec, slash); 716 glob_esc(&cp, name, sglob); 717 *cp = '\0'; 718 globres = glob(file, 0, NULL, &globinfo); 719 if (globres != 0 && globres != GLOB_NOMATCH) 720 mandoc_msg(MANDOCERR_GLOB, 0, 0, 721 "%s: %s", file, strerror(errno)); 722 free(file); 723 if (globres == 0) 724 file = mandoc_strdup(*globinfo.gl_pathv); 725 globfree(&globinfo); 726 if (globres == 0) { 727 if (stat(file, &sb) != -1) 728 goto found; 729 free(file); 730 } 731 if (res != NULL || ipath + 1 != paths->sz) 732 return -1; 733 734 mandoc_asprintf(&file, "%s.%s", name, sec); 735 globres = stat(file, &sb); 736 free(file); 737 return globres; 738 739 found: 740 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s", 741 name, sec, BINM_MAKEWHATIS, paths->paths[ipath]); 742 if (res == NULL) { 743 free(file); 744 return 0; 745 } 746 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(**res)); 747 page = *res + (*ressz - 1); 748 page->file = file; 749 page->names = NULL; 750 page->output = NULL; 751 page->bits = NAME_FILE & NAME_MASK; 752 page->ipath = ipath; 753 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10; 754 page->form = form; 755 return 0; 756 } 757 758 static int 759 fs_search(const struct mansearch *cfg, const struct manpaths *paths, 760 const char *name, struct manpage **res, size_t *ressz) 761 { 762 const char *const sections[] = 763 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"}; 764 const size_t nsec = sizeof(sections)/sizeof(sections[0]); 765 766 size_t ipath, isec; 767 768 assert(cfg->argmode == ARG_NAME); 769 if (res != NULL) 770 *res = NULL; 771 *ressz = 0; 772 for (ipath = 0; ipath < paths->sz; ipath++) { 773 if (cfg->sec != NULL) { 774 if (fs_lookup(paths, ipath, cfg->sec, cfg->arch, 775 name, res, ressz) != -1 && cfg->firstmatch) 776 return 0; 777 } else { 778 for (isec = 0; isec < nsec; isec++) 779 if (fs_lookup(paths, ipath, sections[isec], 780 cfg->arch, name, res, ressz) != -1 && 781 cfg->firstmatch) 782 return 0; 783 } 784 } 785 return -1; 786 } 787 788 static void 789 process_onefile(struct mparse *mp, struct manpage *resp, int startdir, 790 struct outstate *outst, struct manconf *conf) 791 { 792 int fd; 793 794 /* 795 * Changing directories is not needed in ARG_FILE mode. 796 * Do it on a best-effort basis. Even in case of 797 * failure, some functionality may still work. 798 */ 799 if (resp->ipath != SIZE_MAX) 800 (void)chdir(conf->manpath.paths[resp->ipath]); 801 else if (startdir != -1) 802 (void)fchdir(startdir); 803 804 mandoc_msg_setinfilename(resp->file); 805 if (resp->file != NULL) { 806 if ((fd = mparse_open(mp, resp->file)) == -1) { 807 mandoc_msg(resp->ipath == SIZE_MAX ? 808 MANDOCERR_BADARG_BAD : MANDOCERR_OPEN, 809 0, 0, "%s", strerror(errno)); 810 mandoc_msg_setinfilename(NULL); 811 return; 812 } 813 } else 814 fd = STDIN_FILENO; 815 816 if (outst->use_pager) { 817 outst->use_pager = 0; 818 outst->tag_files = term_tag_init(); 819 } 820 if (outst->had_output && outst->outtype <= OUTT_UTF8) { 821 if (outst->outdata == NULL) 822 outdata_alloc(outst, &conf->output); 823 terminal_sepline(outst->outdata); 824 } 825 826 if (resp->form == FORM_SRC) 827 parse(mp, fd, resp->file, outst, &conf->output); 828 else { 829 passthrough(fd, conf->output.synopsisonly); 830 outst->had_output = 1; 831 } 832 833 if (ferror(stdout)) { 834 if (outst->tag_files != NULL) { 835 mandoc_msg(MANDOCERR_WRITE, 0, 0, "%s: %s", 836 outst->tag_files->ofn, strerror(errno)); 837 term_tag_unlink(); 838 outst->tag_files = NULL; 839 } else 840 mandoc_msg(MANDOCERR_WRITE, 0, 0, "%s", 841 strerror(errno)); 842 } 843 mandoc_msg_setinfilename(NULL); 844 } 845 846 static void 847 parse(struct mparse *mp, int fd, const char *file, 848 struct outstate *outst, struct manoutput *outconf) 849 { 850 static int previous; 851 struct roff_meta *meta; 852 853 assert(fd >= 0); 854 if (file == NULL) 855 file = "<stdin>"; 856 857 if (previous) 858 mparse_reset(mp); 859 else 860 previous = 1; 861 862 mparse_readfd(mp, fd, file); 863 if (fd != STDIN_FILENO) 864 close(fd); 865 866 /* 867 * With -Wstop and warnings or errors of at least the requested 868 * level, do not produce output. 869 */ 870 871 if (outst->wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK) 872 return; 873 874 if (outst->outdata == NULL) 875 outdata_alloc(outst, outconf); 876 else if (outst->outtype == OUTT_HTML) 877 html_reset(outst->outdata); 878 879 mandoc_xr_reset(); 880 meta = mparse_result(mp); 881 882 /* Execute the out device, if it exists. */ 883 884 outst->had_output = 1; 885 if (meta->macroset == MACROSET_MDOC) { 886 switch (outst->outtype) { 887 case OUTT_HTML: 888 html_mdoc(outst->outdata, meta); 889 break; 890 case OUTT_TREE: 891 tree_mdoc(outst->outdata, meta); 892 break; 893 case OUTT_MAN: 894 man_mdoc(outst->outdata, meta); 895 break; 896 case OUTT_PDF: 897 case OUTT_ASCII: 898 case OUTT_UTF8: 899 case OUTT_LOCALE: 900 case OUTT_PS: 901 terminal_mdoc(outst->outdata, meta); 902 break; 903 case OUTT_MARKDOWN: 904 markdown_mdoc(outst->outdata, meta); 905 break; 906 default: 907 break; 908 } 909 } 910 if (meta->macroset == MACROSET_MAN) { 911 switch (outst->outtype) { 912 case OUTT_HTML: 913 html_man(outst->outdata, meta); 914 break; 915 case OUTT_TREE: 916 tree_man(outst->outdata, meta); 917 break; 918 case OUTT_MAN: 919 mparse_copy(mp); 920 break; 921 case OUTT_PDF: 922 case OUTT_ASCII: 923 case OUTT_UTF8: 924 case OUTT_LOCALE: 925 case OUTT_PS: 926 terminal_man(outst->outdata, meta); 927 break; 928 default: 929 break; 930 } 931 } 932 if (outconf->tag != NULL && outconf->tag_found == 0 && 933 tag_exists(outconf->tag)) 934 outconf->tag_found = 1; 935 if (mandoc_msg_getmin() < MANDOCERR_STYLE) 936 check_xr(); 937 } 938 939 static void 940 check_xr(void) 941 { 942 static struct manpaths paths; 943 struct mansearch search; 944 struct mandoc_xr *xr; 945 size_t sz; 946 947 if (paths.sz == 0) 948 manpath_base(&paths); 949 950 for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) { 951 if (xr->line == -1) 952 continue; 953 search.arch = NULL; 954 search.sec = xr->sec; 955 search.outkey = NULL; 956 search.argmode = ARG_NAME; 957 search.firstmatch = 1; 958 if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz)) 959 continue; 960 if (fs_search(&search, &paths, xr->name, NULL, &sz) != -1) 961 continue; 962 if (xr->count == 1) 963 mandoc_msg(MANDOCERR_XR_BAD, xr->line, 964 xr->pos + 1, "Xr %s %s", xr->name, xr->sec); 965 else 966 mandoc_msg(MANDOCERR_XR_BAD, xr->line, 967 xr->pos + 1, "Xr %s %s (%d times)", 968 xr->name, xr->sec, xr->count); 969 } 970 } 971 972 static void 973 outdata_alloc(struct outstate *outst, struct manoutput *outconf) 974 { 975 switch (outst->outtype) { 976 case OUTT_HTML: 977 outst->outdata = html_alloc(outconf); 978 break; 979 case OUTT_UTF8: 980 outst->outdata = utf8_alloc(outconf); 981 break; 982 case OUTT_LOCALE: 983 outst->outdata = locale_alloc(outconf); 984 break; 985 case OUTT_ASCII: 986 outst->outdata = ascii_alloc(outconf); 987 break; 988 case OUTT_PDF: 989 outst->outdata = pdf_alloc(outconf); 990 break; 991 case OUTT_PS: 992 outst->outdata = ps_alloc(outconf); 993 break; 994 default: 995 break; 996 } 997 } 998 999 static void 1000 passthrough(int fd, int synopsis_only) 1001 { 1002 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"; 1003 const char synr[] = "SYNOPSIS"; 1004 1005 FILE *stream; 1006 char *line, *cp; 1007 size_t linesz; 1008 ssize_t len, written; 1009 int lno, print; 1010 1011 stream = NULL; 1012 line = NULL; 1013 linesz = 0; 1014 1015 if (fflush(stdout) == EOF) { 1016 mandoc_msg(MANDOCERR_FFLUSH, 0, 0, "%s", strerror(errno)); 1017 goto done; 1018 } 1019 if ((stream = fdopen(fd, "r")) == NULL) { 1020 close(fd); 1021 mandoc_msg(MANDOCERR_FDOPEN, 0, 0, "%s", strerror(errno)); 1022 goto done; 1023 } 1024 1025 lno = print = 0; 1026 while ((len = getline(&line, &linesz, stream)) != -1) { 1027 lno++; 1028 cp = line; 1029 if (synopsis_only) { 1030 if (print) { 1031 if ( ! isspace((unsigned char)*cp)) 1032 goto done; 1033 while (isspace((unsigned char)*cp)) { 1034 cp++; 1035 len--; 1036 } 1037 } else { 1038 if (strcmp(cp, synb) == 0 || 1039 strcmp(cp, synr) == 0) 1040 print = 1; 1041 continue; 1042 } 1043 } 1044 for (; len > 0; len -= written) { 1045 if ((written = write(STDOUT_FILENO, cp, len)) == -1) { 1046 mandoc_msg(MANDOCERR_WRITE, 0, 0, 1047 "%s", strerror(errno)); 1048 goto done; 1049 } 1050 } 1051 } 1052 if (ferror(stream)) 1053 mandoc_msg(MANDOCERR_GETLINE, lno, 0, "%s", strerror(errno)); 1054 1055 done: 1056 free(line); 1057 if (stream != NULL) 1058 fclose(stream); 1059 } 1060 1061 static int 1062 woptions(char *arg, enum mandoc_os *os_e, int *wstop) 1063 { 1064 char *v, *o; 1065 const char *toks[11]; 1066 1067 toks[0] = "stop"; 1068 toks[1] = "all"; 1069 toks[2] = "base"; 1070 toks[3] = "style"; 1071 toks[4] = "warning"; 1072 toks[5] = "error"; 1073 toks[6] = "unsupp"; 1074 toks[7] = "fatal"; 1075 toks[8] = "openbsd"; 1076 toks[9] = "netbsd"; 1077 toks[10] = NULL; 1078 1079 while (*arg) { 1080 o = arg; 1081 switch (getsubopt(&arg, (char * const *)toks, &v)) { 1082 case 0: 1083 *wstop = 1; 1084 break; 1085 case 1: 1086 case 2: 1087 mandoc_msg_setmin(MANDOCERR_BASE); 1088 break; 1089 case 3: 1090 mandoc_msg_setmin(MANDOCERR_STYLE); 1091 break; 1092 case 4: 1093 mandoc_msg_setmin(MANDOCERR_WARNING); 1094 break; 1095 case 5: 1096 mandoc_msg_setmin(MANDOCERR_ERROR); 1097 break; 1098 case 6: 1099 mandoc_msg_setmin(MANDOCERR_UNSUPP); 1100 break; 1101 case 7: 1102 mandoc_msg_setmin(MANDOCERR_BADARG); 1103 break; 1104 case 8: 1105 mandoc_msg_setmin(MANDOCERR_BASE); 1106 *os_e = MANDOC_OS_OPENBSD; 1107 break; 1108 case 9: 1109 mandoc_msg_setmin(MANDOCERR_BASE); 1110 *os_e = MANDOC_OS_NETBSD; 1111 break; 1112 default: 1113 mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, "-W %s", o); 1114 return -1; 1115 } 1116 } 1117 return 0; 1118 } 1119 1120 /* 1121 * Wait until moved to the foreground, 1122 * then fork the pager and wait for the user to close it. 1123 */ 1124 static void 1125 run_pager(struct outstate *outst, char *tag_target) 1126 { 1127 int signum, status; 1128 pid_t man_pgid, tc_pgid; 1129 pid_t pager_pid, wait_pid; 1130 1131 man_pgid = getpgid(0); 1132 outst->tag_files->tcpgid = 1133 man_pgid == getpid() ? getpgid(getppid()) : man_pgid; 1134 pager_pid = 0; 1135 signum = SIGSTOP; 1136 1137 for (;;) { 1138 /* Stop here until moved to the foreground. */ 1139 1140 tc_pgid = tcgetpgrp(STDOUT_FILENO); 1141 if (tc_pgid != man_pgid) { 1142 if (tc_pgid == pager_pid) { 1143 (void)tcsetpgrp(STDOUT_FILENO, man_pgid); 1144 if (signum == SIGTTIN) 1145 continue; 1146 } else 1147 outst->tag_files->tcpgid = tc_pgid; 1148 kill(0, signum); 1149 continue; 1150 } 1151 1152 /* Once in the foreground, activate the pager. */ 1153 1154 if (pager_pid) { 1155 (void)tcsetpgrp(STDOUT_FILENO, pager_pid); 1156 kill(pager_pid, SIGCONT); 1157 } else 1158 pager_pid = spawn_pager(outst, tag_target); 1159 1160 /* Wait for the pager to stop or exit. */ 1161 1162 while ((wait_pid = waitpid(pager_pid, &status, 1163 WUNTRACED)) == -1 && errno == EINTR) 1164 continue; 1165 1166 if (wait_pid == -1) { 1167 mandoc_msg(MANDOCERR_WAIT, 0, 0, 1168 "%s", strerror(errno)); 1169 break; 1170 } 1171 if (!WIFSTOPPED(status)) 1172 break; 1173 1174 signum = WSTOPSIG(status); 1175 } 1176 } 1177 1178 static pid_t 1179 spawn_pager(struct outstate *outst, char *tag_target) 1180 { 1181 const struct timespec timeout = { 0, 100000000 }; /* 0.1s */ 1182 #define MAX_PAGER_ARGS 16 1183 char *argv[MAX_PAGER_ARGS]; 1184 const char *pager; 1185 char *cp; 1186 size_t cmdlen; 1187 int argc, use_ofn; 1188 pid_t pager_pid; 1189 1190 assert(outst->tag_files->ofd == -1); 1191 assert(outst->tag_files->tfs == NULL); 1192 1193 pager = getenv("MANPAGER"); 1194 if (pager == NULL || *pager == '\0') 1195 pager = getenv("PAGER"); 1196 if (pager == NULL || *pager == '\0') 1197 pager = "more -s"; 1198 cp = mandoc_strdup(pager); 1199 1200 /* 1201 * Parse the pager command into words. 1202 * Intentionally do not do anything fancy here. 1203 */ 1204 1205 argc = 0; 1206 while (argc + 5 < MAX_PAGER_ARGS) { 1207 argv[argc++] = cp; 1208 cp = strchr(cp, ' '); 1209 if (cp == NULL) 1210 break; 1211 *cp++ = '\0'; 1212 while (*cp == ' ') 1213 cp++; 1214 if (*cp == '\0') 1215 break; 1216 } 1217 1218 /* For more(1) and less(1), use the tag file. */ 1219 1220 use_ofn = 1; 1221 if (*outst->tag_files->tfn != '\0' && 1222 (cmdlen = strlen(argv[0])) >= 4) { 1223 cp = argv[0] + cmdlen - 4; 1224 if (strcmp(cp, "less") == 0 || strcmp(cp, "more") == 0) { 1225 argv[argc++] = mandoc_strdup("-T"); 1226 argv[argc++] = outst->tag_files->tfn; 1227 if (tag_target != NULL) { 1228 argv[argc++] = mandoc_strdup("-t"); 1229 argv[argc++] = tag_target; 1230 use_ofn = 0; 1231 } 1232 } 1233 } 1234 if (use_ofn) { 1235 if (outst->outtype == OUTT_HTML && tag_target != NULL) 1236 mandoc_asprintf(&argv[argc], "file://%s#%s", 1237 outst->tag_files->ofn, tag_target); 1238 else 1239 argv[argc] = outst->tag_files->ofn; 1240 argc++; 1241 } 1242 argv[argc] = NULL; 1243 1244 switch (pager_pid = fork()) { 1245 case -1: 1246 mandoc_msg(MANDOCERR_FORK, 0, 0, "%s", strerror(errno)); 1247 exit(mandoc_msg_getrc()); 1248 case 0: 1249 break; 1250 default: 1251 (void)setpgid(pager_pid, 0); 1252 (void)tcsetpgrp(STDOUT_FILENO, pager_pid); 1253 if (pledge("stdio rpath tmppath tty proc", NULL) == -1) { 1254 mandoc_msg(MANDOCERR_PLEDGE, 0, 0, 1255 "%s", strerror(errno)); 1256 exit(mandoc_msg_getrc()); 1257 } 1258 outst->tag_files->pager_pid = pager_pid; 1259 return pager_pid; 1260 } 1261 1262 /* 1263 * The child process becomes the pager. 1264 * Do not start it before controlling the terminal. 1265 */ 1266 1267 while (tcgetpgrp(STDOUT_FILENO) != getpid()) 1268 nanosleep(&timeout, NULL); 1269 1270 execvp(argv[0], argv); 1271 mandoc_msg(MANDOCERR_EXEC, 0, 0, "%s: %s", argv[0], strerror(errno)); 1272 _exit(mandoc_msg_getrc()); 1273 } 1274