1 /* $NetBSD: man.c,v 1.75 2025/09/08 19:44:40 jschauma Exp $ */ 2 3 /* 4 * Copyright (c) 1987, 1993, 1994, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 34 #ifndef lint 35 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994, 1995\ 36 The Regents of the University of California. All rights reserved."); 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)man.c 8.17 (Berkeley) 1/31/95"; 42 #else 43 __RCSID("$NetBSD: man.c,v 1.75 2025/09/08 19:44:40 jschauma Exp $"); 44 #endif 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/queue.h> 49 #include <sys/stat.h> 50 #include <sys/utsname.h> 51 52 #include <ctype.h> 53 #include <err.h> 54 #include <fcntl.h> 55 #include <fnmatch.h> 56 #include <glob.h> 57 #include <signal.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 #include <util.h> 63 #include <locale.h> 64 65 #include "manconf.h" 66 #include "pathnames.h" 67 68 #ifndef MAN_DEBUG 69 #define MAN_DEBUG 0 /* debug path output */ 70 #endif 71 72 enum inserttype { 73 INS_TAIL, 74 INS_HEAD 75 }; 76 77 /* 78 * manstate: structure collecting the current global state so we can 79 * easily identify it and pass it to helper functions in one arg. 80 */ 81 struct manstate { 82 /* command line flags */ 83 int all; /* -a: show all matches rather than first */ 84 int cat; /* -c: do not use a pager */ 85 char *conffile; /* -C: use alternate config file */ 86 int how; /* -h: show SYNOPSIS only */ 87 int local; /* -l: interpret arguments as filenames */ 88 char *manpath; /* -M: alternate MANPATH */ 89 char *addpath; /* -m: add these dirs to front of manpath */ 90 char *pathsearch; /* -S: path of man must contain this string */ 91 char *sectionname; /* -s: limit search to a given man section */ 92 int where; /* -w: just show paths of all matching files */ 93 int getpath; /* -p: print the path of directories containing man pages */ 94 95 /* important tags from the config file */ 96 TAG *defaultpath; /* _default: default MANPATH */ 97 TAG *subdirs; /* _subdir: default subdir search list */ 98 TAG *suffixlist; /* _suffix: for files that can be cat()'d */ 99 TAG *buildlist; /* _build: for files that must be built */ 100 101 /* tags for internal use */ 102 TAG *intmp; /* _intmp: tmp files we must cleanup */ 103 TAG *missinglist; /* _missing: pages we couldn't find */ 104 TAG *mymanpath; /* _new_path: final version of MANPATH */ 105 TAG *section; /* <sec>: tag for m.sectionname */ 106 107 /* other misc stuff */ 108 const char *pager; /* pager to use */ 109 size_t pagerlen; /* length of the above */ 110 const char *machine; /* machine */ 111 const char *machclass; /* machine class */ 112 }; 113 114 /* 115 * prototypes 116 */ 117 static void build_page(const char *, char **, struct manstate *); 118 static void cat(const char *); 119 static const char *check_pager(const char *); 120 static int cleanup(void); 121 static void how(const char *); 122 static void jump(char **, const char *, const char *) __dead; 123 static int manual(char *, struct manstate *, glob_t *); 124 static void onsig(int) __dead; 125 static void usage(void) __dead; 126 static void addpath(struct manstate *, const char *, size_t, const char *, 127 enum inserttype); 128 static const char *getclass(const char *); 129 static void printmanpath(struct manstate *); 130 131 /* 132 * main function 133 */ 134 int 135 main(int argc, char **argv) 136 { 137 static struct manstate m; 138 struct utsname utsname; 139 int ch, abs_section, found; 140 ENTRY *esubd, *epath; 141 char *p, **ap, *cmd; 142 size_t len; 143 glob_t pg; 144 145 setprogname(argv[0]); 146 setlocale(LC_ALL, ""); 147 /* 148 * parse command line... 149 */ 150 while ((ch = getopt(argc, argv, "-aC:cfhklM:m:P:ps:S:w")) != -1) 151 switch (ch) { 152 case 'a': 153 m.all = 1; 154 break; 155 case 'C': 156 m.conffile = optarg; 157 break; 158 case 'c': 159 case '-': /* XXX: '-' is a deprecated version of '-c' */ 160 m.cat = 1; 161 break; 162 case 'h': 163 m.how = 1; 164 break; 165 case 'l': 166 m.local = 1; 167 break; 168 case 'm': 169 m.addpath = optarg; 170 break; 171 case 'M': 172 case 'P': /* -P for backward compatibility */ 173 if ((m.manpath = strdup(optarg)) == NULL) 174 err(EXIT_FAILURE, "malloc failed"); 175 break; 176 case 'p': 177 m.getpath = 1; 178 break; 179 /* 180 * The -f and -k options are backward compatible 181 * ways of calling whatis(1) and apropos(1). 182 */ 183 case 'f': 184 jump(argv, "-f", "whatis"); 185 /* NOTREACHED */ 186 case 'k': 187 jump(argv, "-k", "apropos"); 188 /* NOTREACHED */ 189 case 's': 190 if (m.sectionname != NULL) 191 usage(); 192 m.sectionname = optarg; 193 break; 194 case 'S': 195 m.pathsearch = optarg; 196 break; 197 case 'w': 198 m.all = m.where = 1; 199 break; 200 case '?': 201 default: 202 usage(); 203 } 204 argc -= optind; 205 argv += optind; 206 207 if (!m.getpath && !argc) 208 usage(); 209 210 /* 211 * read the configuration file and collect any other information 212 * we will need (machine type, pager, section [if specified 213 * without '-s'], and MANPATH through the environment). 214 */ 215 config(m.conffile); /* exits on error ... */ 216 217 if ((m.machine = getenv("MACHINE")) == NULL) { 218 if (uname(&utsname) == -1) 219 err(EXIT_FAILURE, "uname"); 220 m.machine = utsname.machine; 221 } 222 223 m.machclass = getclass(m.machine); 224 225 if (!m.cat && !m.how && !m.where) { /* if we need a pager ... */ 226 if (!isatty(STDOUT_FILENO)) { 227 m.cat = 1; 228 } else { 229 if ((m.pager = getenv("PAGER")) != NULL && 230 m.pager[0] != '\0') 231 m.pager = check_pager(m.pager); 232 else 233 m.pager = _PATH_PAGER; 234 m.pagerlen = strlen(m.pager); 235 } 236 } 237 238 /* do we need to set m.section to a non-null value? */ 239 if (m.sectionname) { 240 241 m.section = gettag(m.sectionname, 0); /* -s must be a section */ 242 if (m.section == NULL) 243 errx(EXIT_FAILURE, "unknown section: %s", m.sectionname); 244 245 } else if (argc > 1) { 246 247 m.section = gettag(*argv, 0); /* might be a section? */ 248 if (m.section) { 249 argv++; 250 argc--; 251 } 252 253 } 254 255 if (m.manpath == NULL) 256 m.manpath = getenv("MANPATH"); /* note: -M overrides getenv */ 257 258 259 /* 260 * get default values from config file, plus create the tags we 261 * use for keeping internal state. make sure all our mallocs 262 * go through. 263 */ 264 /* from cfg file */ 265 m.defaultpath = gettag("_default", 1); 266 m.subdirs = gettag("_subdir", 1); 267 m.suffixlist = gettag("_suffix", 1); 268 m.buildlist = gettag("_build", 1); 269 /* internal use */ 270 m.mymanpath = gettag("_new_path", 1); 271 m.missinglist = gettag("_missing", 1); 272 m.intmp = gettag("_intmp", 1); 273 if (!m.defaultpath || !m.subdirs || !m.suffixlist || !m.buildlist || 274 !m.mymanpath || !m.missinglist || !m.intmp) 275 errx(EXIT_FAILURE, "malloc failed"); 276 277 /* 278 * are we using a section whose elements are all absolute paths? 279 * (we only need to look at the first entry on the section list, 280 * as config() will ensure that any additional entries will match 281 * the first one.) 282 */ 283 abs_section = (m.section != NULL && 284 !TAILQ_EMPTY(&m.section->entrylist) && 285 *(TAILQ_FIRST(&m.section->entrylist)->s) == '/'); 286 287 /* 288 * now that we have all the data we need, we must determine the 289 * manpath we are going to use to find the requested entries using 290 * the following steps... 291 * 292 * [1] if the user specified a section and that section's elements 293 * from the config file are all absolute paths, then we override 294 * defaultpath and -M/MANPATH with the section's absolute paths. 295 */ 296 if (abs_section) { 297 m.manpath = NULL; /* ignore -M/MANPATH */ 298 m.defaultpath = m.section; /* overwrite _default path */ 299 m.section = NULL; /* promoted to defaultpath */ 300 } 301 302 /* 303 * [2] section can now only be non-null if the user asked for 304 * a section and that section's elements did not have 305 * absolute paths. in this case we use the section's 306 * elements to override _subdir from the config file. 307 * 308 * after this step, we are done processing "m.section"... 309 */ 310 if (m.section) 311 m.subdirs = m.section; 312 313 /* 314 * [3] we need to setup the path we want to use (m.mymanpath). 315 * if the user gave us a path (m.manpath) use it, otherwise 316 * go with the default. in either case we need to append 317 * the subdir and machine spec to each element of the path. 318 * 319 * for absolute section paths that come from the config file, 320 * we only append the subdir spec if the path ends in 321 * a '/' --- elements that do not end in '/' are assumed to 322 * not have subdirectories. this is mainly for backward compat, 323 * but it allows non-subdir configs like: 324 * sect3 /usr/share/man/{old/,}cat3 325 * doc /usr/{pkg,share}/doc/{sendmail/op,sendmail/intro} 326 * 327 * note that we try and be careful to not put double slashes 328 * in the path (e.g. we want /usr/share/man/man1, not 329 * /usr/share/man//man1) because "more" will put the filename 330 * we generate in its prompt and the double slashes look ugly. 331 */ 332 if (m.manpath) { 333 334 /* note: strtok is going to destroy m.manpath */ 335 for (p = strtok(m.manpath, ":") ; p ; p = strtok(NULL, ":")) { 336 len = strlen(p); 337 if (len < 1) 338 continue; 339 TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) 340 addpath(&m, p, len, esubd->s, INS_TAIL); 341 } 342 343 } else { 344 345 TAILQ_FOREACH(epath, &m.defaultpath->entrylist, q) { 346 /* handle trailing "/" magic here ... */ 347 if (abs_section && epath->s[epath->len - 1] != '/') { 348 addpath(&m, "", 1, epath->s, INS_TAIL); 349 continue; 350 } 351 352 TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) 353 addpath(&m, epath->s, epath->len, esubd->s, INS_TAIL); 354 } 355 356 } 357 358 /* 359 * [4] finally, prepend the "-m" m.addpath to mymanpath if it 360 * was specified. subdirs and machine are always applied to 361 * m.addpath. 362 */ 363 if (m.addpath) { 364 365 /* note: strtok is going to destroy m.addpath */ 366 for (p = strtok(m.addpath, ":") ; p ; p = strtok(NULL, ":")) { 367 len = strlen(p); 368 if (len < 1) 369 continue; 370 TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) 371 addpath(&m, p, len, esubd->s, INS_HEAD); /* Add to front */ 372 } 373 374 } 375 376 if (m.getpath) { 377 printmanpath(&m); 378 exit(cleanup()); 379 } 380 381 /* 382 * now m.mymanpath is complete! 383 */ 384 #if MAN_DEBUG 385 printf("mymanpath:\n"); 386 TAILQ_FOREACH(epath, &m.mymanpath->entrylist, q) { 387 printf("\t%s\n", epath->s); 388 } 389 #endif 390 391 /* 392 * start searching for matching files and format them if necessary. 393 * setup an interrupt handler so that we can ensure that temporary 394 * files go away. 395 */ 396 (void)signal(SIGINT, onsig); 397 (void)signal(SIGHUP, onsig); 398 (void)signal(SIGPIPE, onsig); 399 400 memset(&pg, 0, sizeof(pg)); 401 for (found = 0; *argv; ++argv) 402 if (manual(*argv, &m, &pg)) { 403 found = 1; 404 } 405 406 /* if nothing found, we're done. */ 407 if (!found) { 408 (void)cleanup(); 409 exit(EXIT_FAILURE); 410 } 411 412 /* 413 * handle the simple display cases first (m.cat, m.how, m.where) 414 */ 415 if (m.cat) { 416 for (ap = pg.gl_pathv; *ap != NULL; ++ap) { 417 if (**ap == '\0') 418 continue; 419 cat(*ap); 420 } 421 exit(cleanup()); 422 } 423 if (m.how) { 424 for (ap = pg.gl_pathv; *ap != NULL; ++ap) { 425 if (**ap == '\0') 426 continue; 427 how(*ap); 428 } 429 exit(cleanup()); 430 } 431 if (m.where) { 432 for (ap = pg.gl_pathv; *ap != NULL; ++ap) { 433 if (**ap == '\0') 434 continue; 435 (void)printf("%s\n", *ap); 436 } 437 exit(cleanup()); 438 } 439 440 /* 441 * normal case - we display things in a single command, so 442 * build a list of things to display. first compute total 443 * length of buffer we will need so we can malloc it. 444 */ 445 for (ap = pg.gl_pathv, len = m.pagerlen + 1; *ap != NULL; ++ap) { 446 if (**ap == '\0') 447 continue; 448 len += strlen(*ap) + 1; 449 } 450 if ((cmd = malloc(len)) == NULL) { 451 warn("malloc"); 452 (void)cleanup(); 453 exit(EXIT_FAILURE); 454 } 455 456 /* now build the command string... */ 457 p = cmd; 458 len = m.pagerlen; 459 memcpy(p, m.pager, len); 460 p += len; 461 *p++ = ' '; 462 for (ap = pg.gl_pathv; *ap != NULL; ++ap) { 463 if (**ap == '\0') 464 continue; 465 len = strlen(*ap); 466 memcpy(p, *ap, len); 467 p += len; 468 *p++ = ' '; 469 } 470 *--p = '\0'; 471 472 /* Use system(3) in case someone's pager is "pager arg1 arg2". */ 473 (void)system(cmd); 474 475 exit(cleanup()); 476 } 477 478 static int 479 manual_find_literalfile(struct manstate *mp, char **pv) 480 { 481 ENTRY *suffix; 482 int found; 483 char buf[MAXPATHLEN]; 484 const char *p; 485 int suflen; 486 487 found = 0; 488 489 /* 490 * Expand both '*' and suffix to force an actual 491 * match via fnmatch(3). Since the only match in pg 492 * is the literal file, the match is genuine. 493 */ 494 495 TAILQ_FOREACH(suffix, &mp->buildlist->entrylist, q) { 496 for (p = suffix->s, suflen = 0; 497 *p != '\0' && !isspace((unsigned char)*p); 498 ++p) 499 ++suflen; 500 if (*p == '\0') 501 continue; 502 503 (void)snprintf(buf, sizeof(buf), "*%.*s", suflen, suffix->s); 504 505 if (!fnmatch(buf, *pv, 0)) { 506 if (!mp->where) 507 build_page(p + 1, pv, mp); 508 found = 1; 509 break; 510 } 511 } 512 513 return found; 514 } 515 516 static int 517 manual_find_buildkeyword(const char *prefix, const char *escpage, 518 struct manstate *mp, char **pv) 519 { 520 ENTRY *suffix; 521 int found; 522 char buf[MAXPATHLEN]; 523 const char *p; 524 int suflen; 525 526 found = 0; 527 /* Try the _build keywords next. */ 528 TAILQ_FOREACH(suffix, &mp->buildlist->entrylist, q) { 529 for (p = suffix->s, suflen = 0; 530 *p != '\0' && !isspace((unsigned char)*p); 531 ++p) 532 ++suflen; 533 if (*p == '\0') 534 continue; 535 536 (void)snprintf(buf, sizeof(buf), "%s%s%.*s", 537 prefix, escpage, suflen, suffix->s); 538 if (!fnmatch(buf, *pv, 0)) { 539 if (!mp->where) 540 build_page(p + 1, pv, mp); 541 found = 1; 542 break; 543 } 544 } 545 546 return found; 547 } 548 549 /* 550 * manual -- 551 * Search the manuals for the pages. 552 */ 553 static int 554 manual(char *page, struct manstate *mp, glob_t *pg) 555 { 556 ENTRY *suffix, *mdir; 557 int anyfound, error, found; 558 size_t cnt; 559 char *p, buf[MAXPATHLEN], *escpage, *eptr; 560 static const char escglob[] = "\\~?*{}[]"; 561 562 anyfound = 0; 563 564 /* 565 * Fixup page which may contain glob(3) special characters, e.g. 566 * the famous "No man page for [" FAQ. 567 */ 568 if ((escpage = malloc((2 * strlen(page)) + 1)) == NULL) { 569 warn("malloc"); 570 (void)cleanup(); 571 exit(EXIT_FAILURE); 572 } 573 574 p = page; 575 eptr = escpage; 576 577 while (*p) { 578 if (strchr(escglob, *p) != NULL) { 579 *eptr++ = '\\'; 580 *eptr++ = *p++; 581 } else 582 *eptr++ = *p++; 583 } 584 585 *eptr = '\0'; 586 587 /* 588 * If 'page' is given with an absolute path, 589 * or a relative path explicitly beginning with "./" 590 * or "../", then interpret it as a file specification. 591 */ 592 if (mp->local 593 || (page[0] == '/') 594 || (page[0] == '.' && page[1] == '/') 595 || (page[0] == '.' && page[1] == '.' && page[2] == '/') 596 ) { 597 /* check if file actually exists */ 598 (void)strlcpy(buf, escpage, sizeof(buf)); 599 error = glob(buf, GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg); 600 if (error != 0) { 601 if (error == GLOB_NOMATCH) { 602 goto notfound; 603 } else { 604 errx(EXIT_FAILURE, "glob failed"); 605 } 606 } 607 608 if (pg->gl_matchc == 0) 609 goto notfound; 610 611 /* literal file only yields one match */ 612 cnt = pg->gl_pathc - pg->gl_matchc; 613 614 if (manual_find_literalfile(mp, &pg->gl_pathv[cnt])) { 615 anyfound = 1; 616 } else { 617 /* It's not a man page, forget about it. */ 618 *pg->gl_pathv[cnt] = '\0'; 619 } 620 621 notfound: 622 if (!anyfound) { 623 if (addentry(mp->missinglist, page, 0) < 0) { 624 warn("malloc"); 625 (void)cleanup(); 626 exit(EXIT_FAILURE); 627 } 628 } 629 free(escpage); 630 return anyfound; 631 } 632 633 /* For each man directory in mymanpath ... */ 634 TAILQ_FOREACH(mdir, &mp->mymanpath->entrylist, q) { 635 636 /* 637 * use glob(3) to look in the filesystem for matching files. 638 * match any suffix here, as we will check that later. 639 */ 640 (void)snprintf(buf, sizeof(buf), "%s/%s.*", mdir->s, escpage); 641 if ((error = glob(buf, 642 GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) { 643 if (error == GLOB_NOMATCH) 644 continue; 645 else { 646 warn("globbing"); 647 (void)cleanup(); 648 exit(EXIT_FAILURE); 649 } 650 } 651 if (pg->gl_matchc == 0) 652 continue; 653 654 /* 655 * start going through the matches glob(3) just found and 656 * use m.pathsearch (if present) to filter out pages we 657 * don't want. then verify the suffix is valid, and build 658 * the page if we have a _build suffix. 659 */ 660 for (cnt = pg->gl_pathc - pg->gl_matchc; 661 cnt < pg->gl_pathc; ++cnt) { 662 663 /* filter on directory path name */ 664 if (mp->pathsearch) { 665 p = strstr(pg->gl_pathv[cnt], mp->pathsearch); 666 if (!p || strchr(p, '/') == NULL) { 667 *pg->gl_pathv[cnt] = '\0'; /* zap! */ 668 continue; 669 } 670 } 671 672 /* 673 * Try the _suffix keywords first. 674 * 675 * XXX 676 * Older versions of man.conf didn't have the _suffix 677 * keywords, it was assumed that everything was a .0. 678 * We just test for .0 first, it's fast and probably 679 * going to hit. 680 */ 681 (void)snprintf(buf, sizeof(buf), "*/%s.0", escpage); 682 if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) 683 goto next; 684 685 found = 0; 686 TAILQ_FOREACH(suffix, &mp->suffixlist->entrylist, q) { 687 (void)snprintf(buf, 688 sizeof(buf), "*/%s%s", escpage, 689 suffix->s); 690 if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) { 691 found = 1; 692 break; 693 } 694 } 695 if (found) 696 goto next; 697 698 /* Try the _build keywords next. */ 699 found = manual_find_buildkeyword("*/", escpage, 700 mp, &pg->gl_pathv[cnt]); 701 if (found) { 702 next: anyfound = 1; 703 if (!mp->all) { 704 /* Delete any other matches. */ 705 while (++cnt< pg->gl_pathc) 706 *pg->gl_pathv[cnt] = '\0'; 707 break; 708 } 709 continue; 710 } 711 712 /* It's not a man page, forget about it. */ 713 *pg->gl_pathv[cnt] = '\0'; 714 } 715 716 if (anyfound && !mp->all) 717 break; 718 } 719 720 /* If not found, enter onto the missing list. */ 721 if (!anyfound) { 722 if (addentry(mp->missinglist, page, 0) < 0) { 723 warn("malloc"); 724 (void)cleanup(); 725 exit(EXIT_FAILURE); 726 } 727 } 728 729 free(escpage); 730 return anyfound; 731 } 732 733 /* 734 * A do-nothing counterpart to fmtcheck(3) that only supplies the 735 * __format_arg marker. Actual fmtcheck(3) call is done once in 736 * config(). 737 */ 738 __always_inline __format_arg(2) 739 static inline const char * 740 fmtcheck_ok(const char *userfmt, const char *template) 741 { 742 return userfmt; 743 } 744 745 /* 746 * build_page -- 747 * Build a man page for display. 748 */ 749 static void 750 build_page(const char *fmt, char **pathp, struct manstate *mp) 751 { 752 static int warned; 753 int olddir, fd, n; 754 size_t tmpdirlen; 755 char *p, *b; 756 char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN]; 757 const char *tmpdir; 758 759 /* Let the user know this may take awhile. */ 760 if (!warned) { 761 warned = 1; 762 warnx("Formatting manual page..."); 763 } 764 765 /* 766 * Historically man chdir'd to the root of the man tree. 767 * This was used in man pages that contained relative ".so" 768 * directives (including other man pages for command aliases etc.) 769 * It even went one step farther, by examining the first line 770 * of the man page and parsing the .so filename so it would 771 * make hard(?) links to the cat'ted man pages for space savings. 772 * (We don't do that here, but we could). 773 */ 774 775 /* copy and find the end */ 776 for (b = buf, p = *pathp; (*b++ = *p++) != '\0';) 777 continue; 778 779 /* 780 * skip the last two path components, page name and man[n] ... 781 * (e.g. buf will be "/usr/share/man" and p will be "man1/man.1") 782 * we also save a pointer to our current directory so that we 783 * can fchdir() back to it. this allows relative MANDIR paths 784 * to work with multiple man pages... e.g. consider: 785 * cd /usr/share && man -M ./man cat ls 786 * when no "cat1" subdir files are present. 787 */ 788 olddir = -1; 789 for (--b, --p, n = 2; b != buf; b--, p--) 790 if (*b == '/') 791 if (--n == 0) { 792 *b = '\0'; 793 olddir = open(".", O_RDONLY); 794 (void) chdir(buf); 795 p++; 796 break; 797 } 798 799 800 /* advance fmt past the suffix spec to the printf format string */ 801 for (; *fmt && isspace((unsigned char)*fmt); ++fmt) 802 continue; 803 804 /* 805 * Get a temporary file and build a version of the file 806 * to display. Replace the old file name with the new one. 807 */ 808 if ((tmpdir = getenv("TMPDIR")) == NULL) 809 tmpdir = _PATH_TMP; 810 tmpdirlen = strlen(tmpdir); 811 (void)snprintf(tpath, sizeof (tpath), "%s%s%s", tmpdir, 812 (tmpdirlen > 0 && tmpdir[tmpdirlen-1] == '/') ? "" : "/", TMPFILE); 813 if ((fd = mkstemp(tpath)) == -1) { 814 warn("%s", tpath); 815 (void)cleanup(); 816 exit(EXIT_FAILURE); 817 } 818 (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath); 819 (void)snprintf(cmd, sizeof(cmd), fmtcheck_ok(buf, "%s"), p); 820 (void)system(cmd); 821 (void)close(fd); 822 if ((*pathp = strdup(tpath)) == NULL) { 823 warn("malloc"); 824 (void)cleanup(); 825 exit(EXIT_FAILURE); 826 } 827 828 /* Link the built file into the remove-when-done list. */ 829 if (addentry(mp->intmp, *pathp, 0) < 0) { 830 warn("malloc"); 831 (void)cleanup(); 832 exit(EXIT_FAILURE); 833 } 834 835 /* restore old directory so relative manpaths still work */ 836 if (olddir != -1) { 837 fchdir(olddir); 838 close(olddir); 839 } 840 } 841 842 /* 843 * how -- 844 * display how information 845 */ 846 static void 847 how(const char *fname) 848 { 849 FILE *fp; 850 851 int lcnt, print; 852 char buf[256]; 853 const char *p; 854 855 if (!(fp = fopen(fname, "r"))) { 856 warn("%s", fname); 857 (void)cleanup(); 858 exit(EXIT_FAILURE); 859 } 860 #define S1 "SYNOPSIS" 861 #define S2 "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS" 862 #define D1 "DESCRIPTION" 863 #define D2 "D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN" 864 for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) { 865 if (!strncmp(buf, S1, sizeof(S1) - 1) || 866 !strncmp(buf, S2, sizeof(S2) - 1)) { 867 print = 1; 868 continue; 869 } else if (!strncmp(buf, D1, sizeof(D1) - 1) || 870 !strncmp(buf, D2, sizeof(D2) - 1)) { 871 if (fp) 872 (void)fclose(fp); 873 return; 874 } 875 if (!print) 876 continue; 877 if (*buf == '\n') 878 ++lcnt; 879 else { 880 for(; lcnt; --lcnt) 881 (void)putchar('\n'); 882 for (p = buf; isspace((unsigned char)*p); ++p) 883 continue; 884 (void)fputs(p, stdout); 885 } 886 } 887 (void)fclose(fp); 888 } 889 890 /* 891 * cat -- 892 * cat out the file 893 */ 894 static void 895 cat(const char *fname) 896 { 897 int fd; 898 ssize_t n; 899 char buf[2048]; 900 901 if ((fd = open(fname, O_RDONLY, 0)) < 0) { 902 warn("%s", fname); 903 (void)cleanup(); 904 exit(EXIT_FAILURE); 905 } 906 while ((n = read(fd, buf, sizeof(buf))) > 0) 907 if (write(STDOUT_FILENO, buf, (size_t)n) != n) { 908 warn("write"); 909 (void)cleanup(); 910 exit(EXIT_FAILURE); 911 } 912 if (n == -1) { 913 warn("read"); 914 (void)cleanup(); 915 exit(EXIT_FAILURE); 916 } 917 (void)close(fd); 918 } 919 920 /* 921 * check_pager -- 922 * check the user supplied page information 923 */ 924 static const char * 925 check_pager(const char *name) 926 { 927 const char *p; 928 929 /* 930 * if the user uses "more", we make it "more -s"; watch out for 931 * PAGER = "mypager /usr/ucb/more" 932 */ 933 for (p = name; *p && !isspace((unsigned char)*p); ++p) 934 continue; 935 for (; p > name && *p != '/'; --p); 936 if (p != name) 937 ++p; 938 939 /* make sure it's "more", not "morex" */ 940 if (!strncmp(p, "more", 4) && (!p[4] || isspace((unsigned char)p[4]))) { 941 char *newname; 942 (void)asprintf(&newname, "%s %s", p, "-s"); 943 name = newname; 944 } 945 946 return name; 947 } 948 949 /* 950 * jump -- 951 * strip out flag argument and jump 952 */ 953 static void 954 jump(char **argv, const char *flag, const char *name) 955 { 956 char **arg; 957 958 argv[0] = __UNCONST(name); 959 for (arg = argv + 1; *arg; ++arg) 960 if (!strcmp(*arg, flag)) 961 break; 962 for (; *arg; ++arg) 963 arg[0] = arg[1]; 964 execvp(name, argv); 965 err(EXIT_FAILURE, "Cannot execute `%s'", name); 966 } 967 968 /* 969 * onsig -- 970 * If signaled, delete the temporary files. 971 */ 972 static void 973 onsig(int signo) 974 { 975 976 (void)cleanup(); 977 978 (void)raise_default_signal(signo); 979 980 /* NOTREACHED */ 981 exit(EXIT_FAILURE); 982 } 983 984 /* 985 * cleanup -- 986 * Clean up temporary files, show any error messages. 987 */ 988 static int 989 cleanup(void) 990 { 991 TAG *intmpp, *missp; 992 ENTRY *ep; 993 int rval; 994 995 rval = EXIT_SUCCESS; 996 /* 997 * note that _missing and _intmp were created by main(), so 998 * gettag() cannot return NULL here. 999 */ 1000 missp = gettag("_missing", 0); /* missing man pages */ 1001 intmpp = gettag("_intmp", 0); /* tmp files we need to unlink */ 1002 1003 TAILQ_FOREACH(ep, &missp->entrylist, q) { 1004 warnx("no entry for %s in the manual.", ep->s); 1005 rval = EXIT_FAILURE; 1006 } 1007 1008 TAILQ_FOREACH(ep, &intmpp->entrylist, q) 1009 (void)unlink(ep->s); 1010 1011 return rval; 1012 } 1013 1014 static const char * 1015 getclass(const char *machine) 1016 { 1017 char buf[BUFSIZ]; 1018 TAG *t; 1019 snprintf(buf, sizeof(buf), "_%s", machine); 1020 t = gettag(buf, 0); 1021 return t != NULL && !TAILQ_EMPTY(&t->entrylist) ? 1022 TAILQ_FIRST(&t->entrylist)->s : NULL; 1023 } 1024 1025 static void 1026 addpath(struct manstate *m, const char *dir, size_t len, const char *sub, 1027 enum inserttype ishead) 1028 { 1029 char buf[2 * MAXPATHLEN + 1]; 1030 (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,%s%s%s}", 1031 dir, (dir[len - 1] == '/') ? "" : "/", sub, m->machine, 1032 m->machclass ? "/" : "", m->machclass ? m->machclass : "", 1033 m->machclass ? "," : ""); 1034 if (addentry(m->mymanpath, buf, (int)ishead) < 0) 1035 errx(EXIT_FAILURE, "malloc failed"); 1036 } 1037 1038 /* 1039 * usage -- 1040 * print usage message and die 1041 */ 1042 static void 1043 usage(void) 1044 { 1045 (void)fprintf(stderr, "Usage: %s [-acw|-h] [-C cfg] [-M path] " 1046 "[-m path] [-S srch] [[-s] sect] name ...\n", getprogname()); 1047 (void)fprintf(stderr, "Usage: %s [-C file] -f command ...\n", getprogname()); 1048 (void)fprintf(stderr, 1049 "Usage: %s [-C file] -k keyword ...\n", 1050 getprogname()); 1051 (void)fprintf(stderr, "Usage: %s -p\n", getprogname()); 1052 exit(EXIT_FAILURE); 1053 } 1054 1055 /* 1056 * printmanpath -- 1057 * Prints a list of directories containing man pages. 1058 */ 1059 static void 1060 printmanpath(struct manstate *m) 1061 { 1062 ENTRY *epath; 1063 char **ap; 1064 glob_t pg; 1065 struct stat sb; 1066 TAG *path = m->mymanpath; 1067 1068 /* the tail queue is empty if no _default tag is defined in * man.conf */ 1069 if (TAILQ_EMPTY(&path->entrylist)) 1070 errx(EXIT_FAILURE, "Empty manpath"); 1071 1072 TAILQ_FOREACH(epath, &path->entrylist, q) { 1073 if (glob(epath->s, GLOB_BRACE | GLOB_NOSORT, NULL, &pg) != 0) 1074 err(EXIT_FAILURE, "glob failed"); 1075 1076 if (pg.gl_matchc == 0) { 1077 globfree(&pg); 1078 continue; 1079 } 1080 1081 for (ap = pg.gl_pathv; *ap != NULL; ++ap) { 1082 /* Skip cat page directories */ 1083 if (strstr(*ap, "/cat") != NULL) 1084 continue; 1085 /* Skip non-directories. */ 1086 if (stat(*ap, &sb) == 0 && S_ISDIR(sb.st_mode)) 1087 printf("%s\n", *ap); 1088 } 1089 globfree(&pg); 1090 } 1091 } 1092