1 /* $NetBSD: man.c,v 1.35 2007/02/05 19:46:24 jwise 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\n\ 36 The Regents of the University of California. All rights reserved.\n"); 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.35 2007/02/05 19:46:24 jwise Exp $"); 44 #endif 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/queue.h> 49 #include <sys/utsname.h> 50 51 #include <ctype.h> 52 #include <err.h> 53 #include <errno.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 63 #include "manconf.h" 64 #include "pathnames.h" 65 66 #ifndef MAN_DEBUG 67 #define MAN_DEBUG 0 /* debug path output */ 68 #endif 69 70 /* 71 * manstate: structure collecting the current global state so we can 72 * easily identify it and pass it to helper functions in one arg. 73 */ 74 struct manstate { 75 /* command line flags */ 76 int all; /* -a: show all matches rather than first */ 77 int cat; /* -c: do not use a pager */ 78 char *conffile; /* -C: use alternate config file */ 79 int how; /* -h: show SYNOPSIS only */ 80 char *manpath; /* -M: alternate MANPATH */ 81 char *addpath; /* -m: add these dirs to front of manpath */ 82 char *pathsearch; /* -S: path of man must contain this string */ 83 char *sectionname; /* -s: limit search to a given man section */ 84 int where; /* -w: just show paths of all matching files */ 85 86 /* important tags from the config file */ 87 TAG *defaultpath; /* _default: default MANPATH */ 88 TAG *subdirs; /* _subdir: default subdir search list */ 89 TAG *suffixlist; /* _suffix: for files that can be cat()'d */ 90 TAG *buildlist; /* _build: for files that must be built */ 91 92 /* tags for internal use */ 93 TAG *intmp; /* _intmp: tmp files we must cleanup */ 94 TAG *missinglist; /* _missing: pages we couldn't find */ 95 TAG *mymanpath; /* _new_path: final version of MANPATH */ 96 TAG *section; /* <sec>: tag for m.sectionname */ 97 98 /* other misc stuff */ 99 const char *pager; /* pager to use */ 100 size_t pagerlen; /* length of the above */ 101 }; 102 103 /* 104 * prototypes 105 */ 106 int main(int, char **); 107 static void build_page(char *, char **, struct manstate *); 108 static void cat(char *); 109 static const char *check_pager(const char *); 110 static int cleanup(void); 111 static void how(char *); 112 static void jump(char **, char *, char *); 113 static int manual(char *, struct manstate *, glob_t *); 114 static void onsig(int); 115 static void usage(void); 116 117 /* 118 * main function 119 */ 120 int 121 main(int argc, char **argv) 122 { 123 static struct manstate m = { 0 }; /* init to zero */ 124 int ch, abs_section, found; 125 const char *machine; 126 ENTRY *esubd, *epath; 127 char *p, **ap, *cmd, buf[MAXPATHLEN * 2]; 128 size_t len; 129 glob_t pg; 130 131 /* 132 * parse command line... 133 */ 134 while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:s:S:w")) != -1) 135 switch (ch) { 136 case 'a': 137 m.all = 1; 138 break; 139 case 'C': 140 m.conffile = optarg; 141 break; 142 case 'c': 143 case '-': /* XXX: '-' is a deprecated version of '-c' */ 144 m.cat = 1; 145 break; 146 case 'h': 147 m.how = 1; 148 break; 149 case 'm': 150 m.addpath = optarg; 151 break; 152 case 'M': 153 case 'P': /* -P for backward compatibility */ 154 m.manpath = strdup(optarg); 155 break; 156 /* 157 * The -f and -k options are backward compatible, 158 * undocumented ways of calling whatis(1) and apropos(1). 159 */ 160 case 'f': 161 jump(argv, "-f", "whatis"); 162 /* NOTREACHED */ 163 case 'k': 164 jump(argv, "-k", "apropos"); 165 /* NOTREACHED */ 166 case 's': 167 if (m.sectionname != NULL) 168 usage(); 169 m.sectionname = optarg; 170 break; 171 case 'S': 172 m.pathsearch = optarg; 173 break; 174 case 'w': 175 m.all = m.where = 1; 176 break; 177 case '?': 178 default: 179 usage(); 180 } 181 argc -= optind; 182 argv += optind; 183 184 if (!argc) 185 usage(); 186 187 /* 188 * read the configuration file and collect any other information 189 * we will need (machine type, pager, section [if specified 190 * without '-s'], and MANPATH through the environment). 191 */ 192 config(m.conffile); /* exits on error ... */ 193 194 if ((machine = getenv("MACHINE")) == NULL) { 195 struct utsname utsname; 196 197 if (uname(&utsname) == -1) { 198 perror("uname"); 199 exit(1); 200 } 201 machine = utsname.machine; 202 } 203 204 if (!m.cat && !m.how && !m.where) { /* if we need a pager ... */ 205 if (!isatty(STDOUT_FILENO)) { 206 m.cat = 1; 207 } else { 208 if ((m.pager = getenv("PAGER")) != NULL && 209 m.pager[0] != '\0') 210 m.pager = check_pager(m.pager); 211 else 212 m.pager = _PATH_PAGER; 213 m.pagerlen = strlen(m.pager); 214 } 215 } 216 217 /* do we need to set m.section to a non-null value? */ 218 if (m.sectionname) { 219 220 m.section = gettag(m.sectionname, 0); /* -s must be a section */ 221 if (m.section == NULL) 222 errx(1, "unknown section: %s", m.sectionname); 223 224 } else if (argc > 1) { 225 226 m.section = gettag(*argv, 0); /* might be a section? */ 227 if (m.section) { 228 argv++; 229 argc--; 230 } 231 232 } 233 234 if (m.manpath == NULL) 235 m.manpath = getenv("MANPATH"); /* note: -M overrides getenv */ 236 237 238 /* 239 * get default values from config file, plus create the tags we 240 * use for keeping internal state. make sure all our mallocs 241 * go through. 242 */ 243 /* from cfg file */ 244 m.defaultpath = gettag("_default", 1); 245 m.subdirs = gettag("_subdir", 1); 246 m.suffixlist = gettag("_suffix", 1); 247 m.buildlist = gettag("_build", 1); 248 /* internal use */ 249 m.mymanpath = gettag("_new_path", 1); 250 m.missinglist = gettag("_missing", 1); 251 m.intmp = gettag("_intmp", 1); 252 if (!m.defaultpath || !m.subdirs || !m.suffixlist || !m.buildlist || 253 !m.mymanpath || !m.missinglist || !m.intmp) 254 errx(1, "malloc failed"); 255 256 /* 257 * are we using a section whose elements are all absolute paths? 258 * (we only need to look at the first entry on the section list, 259 * as config() will ensure that any additional entries will match 260 * the first one.) 261 */ 262 abs_section = (m.section != NULL && 263 !TAILQ_EMPTY(&m.section->entrylist) && 264 *(TAILQ_FIRST(&m.section->entrylist)->s) == '/'); 265 266 /* 267 * now that we have all the data we need, we must determine the 268 * manpath we are going to use to find the requested entries using 269 * the following steps... 270 * 271 * [1] if the user specified a section and that section's elements 272 * from the config file are all absolute paths, then we override 273 * defaultpath and -M/MANPATH with the section's absolute paths. 274 */ 275 if (abs_section) { 276 m.manpath = NULL; /* ignore -M/MANPATH */ 277 m.defaultpath = m.section; /* overwrite _default path */ 278 m.section = NULL; /* promoted to defaultpath */ 279 } 280 281 /* 282 * [2] section can now only be non-null if the user asked for 283 * a section and that section's elements did not have 284 * absolute paths. in this case we use the section's 285 * elements to override _subdir from the config file. 286 * 287 * after this step, we are done processing "m.section"... 288 */ 289 if (m.section) 290 m.subdirs = m.section; 291 292 /* 293 * [3] we need to setup the path we want to use (m.mymanpath). 294 * if the user gave us a path (m.manpath) use it, otherwise 295 * go with the default. in either case we need to append 296 * the subdir and machine spec to each element of the path. 297 * 298 * for absolute section paths that come from the config file, 299 * we only append the subdir spec if the path ends in 300 * a '/' --- elements that do not end in '/' are assumed to 301 * not have subdirectories. this is mainly for backward compat, 302 * but it allows non-subdir configs like: 303 * sect3 /usr/share/man/{old/,}cat3 304 * doc /usr/{pkg,share}/doc/{sendmail/op,sendmail/intro} 305 * 306 * note that we try and be careful to not put double slashes 307 * in the path (e.g. we want /usr/share/man/man1, not 308 * /usr/share/man//man1) because "more" will put the filename 309 * we generate in its prompt and the double slashes look ugly. 310 */ 311 if (m.manpath) { 312 313 /* note: strtok is going to destroy m.manpath */ 314 for (p = strtok(m.manpath, ":") ; p ; p = strtok(NULL, ":")) { 315 len = strlen(p); 316 if (len < 1) 317 continue; 318 TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) { 319 snprintf(buf, sizeof(buf), "%s%s%s{/%s,}", 320 p, (p[len-1] == '/') ? "" : "/", 321 esubd->s, machine); 322 if (addentry(m.mymanpath, buf, 0) < 0) 323 errx(1, "malloc failed"); 324 } 325 } 326 327 } else { 328 329 TAILQ_FOREACH(epath, &m.defaultpath->entrylist, q) { 330 /* handle trailing "/" magic here ... */ 331 if (abs_section && 332 epath->s[epath->len - 1] != '/') { 333 334 (void)snprintf(buf, sizeof(buf), 335 "%s{/%s,}", epath->s, machine); 336 if (addentry(m.mymanpath, buf, 0) < 0) 337 errx(1, "malloc failed"); 338 continue; 339 } 340 341 TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) { 342 snprintf(buf, sizeof(buf), "%s%s%s{/%s,}", 343 epath->s, 344 (epath->s[epath->len-1] == '/') ? "" 345 : "/", 346 esubd->s, machine); 347 if (addentry(m.mymanpath, buf, 0) < 0) 348 errx(1, "malloc failed"); 349 } 350 } 351 352 } 353 354 /* 355 * [4] finally, prepend the "-m" m.addpath to mymanpath if it 356 * was specified. subdirs and machine are always applied to 357 * m.addpath. 358 */ 359 if (m.addpath) { 360 361 /* note: strtok is going to destroy m.addpath */ 362 for (p = strtok(m.addpath, ":") ; p ; p = strtok(NULL, ":")) { 363 len = strlen(p); 364 if (len < 1) 365 continue; 366 TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) { 367 snprintf(buf, sizeof(buf), "%s%s%s{/%s,}", 368 p, (p[len-1] == '/') ? "" : "/", 369 esubd->s, machine); 370 /* add at front */ 371 if (addentry(m.mymanpath, buf, 1) < 0) 372 errx(1, "malloc failed"); 373 } 374 } 375 376 } 377 378 /* 379 * now m.mymanpath is complete! 380 */ 381 #if MAN_DEBUG 382 printf("mymanpath:\n"); 383 TAILQ_FOREACH(epath, &m.mymanpath->entrylist, q) { 384 printf("\t%s\n", epath->s); 385 } 386 #endif 387 388 /* 389 * start searching for matching files and format them if necessary. 390 * setup an interrupt handler so that we can ensure that temporary 391 * files go away. 392 */ 393 (void)signal(SIGINT, onsig); 394 (void)signal(SIGHUP, onsig); 395 (void)signal(SIGPIPE, onsig); 396 397 memset(&pg, 0, sizeof(pg)); 398 for (found = 0; *argv; ++argv) 399 if (manual(*argv, &m, &pg)) { 400 found = 1; 401 } 402 403 /* if nothing found, we're done. */ 404 if (!found) { 405 (void)cleanup(); 406 exit (1); 407 } 408 409 /* 410 * handle the simple display cases first (m.cat, m.how, m.where) 411 */ 412 if (m.cat) { 413 for (ap = pg.gl_pathv; *ap != NULL; ++ap) { 414 if (**ap == '\0') 415 continue; 416 cat(*ap); 417 } 418 exit (cleanup()); 419 } 420 if (m.how) { 421 for (ap = pg.gl_pathv; *ap != NULL; ++ap) { 422 if (**ap == '\0') 423 continue; 424 how(*ap); 425 } 426 exit(cleanup()); 427 } 428 if (m.where) { 429 for (ap = pg.gl_pathv; *ap != NULL; ++ap) { 430 if (**ap == '\0') 431 continue; 432 (void)printf("%s\n", *ap); 433 } 434 exit(cleanup()); 435 } 436 437 /* 438 * normal case - we display things in a single command, so 439 * build a list of things to display. first compute total 440 * length of buffer we will need so we can malloc it. 441 */ 442 for (ap = pg.gl_pathv, len = m.pagerlen + 1; *ap != NULL; ++ap) { 443 if (**ap == '\0') 444 continue; 445 len += strlen(*ap) + 1; 446 } 447 if ((cmd = malloc(len)) == NULL) { 448 warn("malloc"); 449 (void)cleanup(); 450 exit(1); 451 } 452 453 /* now build the command string... */ 454 p = cmd; 455 len = m.pagerlen; 456 memcpy(p, m.pager, len); 457 p += len; 458 *p++ = ' '; 459 for (ap = pg.gl_pathv; *ap != NULL; ++ap) { 460 if (**ap == '\0') 461 continue; 462 len = strlen(*ap); 463 memcpy(p, *ap, len); 464 p += len; 465 *p++ = ' '; 466 } 467 *--p = '\0'; 468 469 /* Use system(3) in case someone's pager is "pager arg1 arg2". */ 470 (void)system(cmd); 471 472 exit(cleanup()); 473 } 474 475 /* 476 * manual -- 477 * Search the manuals for the pages. 478 */ 479 static int 480 manual(char *page, struct manstate *mp, glob_t *pg) 481 { 482 ENTRY *suffix, *mdir; 483 int anyfound, error, found; 484 size_t cnt; 485 char *p, buf[MAXPATHLEN], *escpage, *eptr; 486 static const char escglob[] = "\\~?*{}[]"; 487 488 anyfound = 0; 489 490 /* 491 * Fixup page which may contain glob(3) special characters, e.g. 492 * the famous "No man page for [" FAQ. 493 */ 494 if ((escpage = malloc((2 * strlen(page)) + 1)) == NULL) { 495 warn("malloc"); 496 (void)cleanup(); 497 exit(1); 498 } 499 500 p = page; 501 eptr = escpage; 502 503 while (*p) { 504 if (strchr(escglob, *p) != NULL) { 505 *eptr++ = '\\'; 506 *eptr++ = *p++; 507 } else 508 *eptr++ = *p++; 509 } 510 511 *eptr = '\0'; 512 513 /* For each man directory in mymanpath ... */ 514 TAILQ_FOREACH(mdir, &mp->mymanpath->entrylist, q) { 515 516 /* 517 * use glob(3) to look in the filesystem for matching files. 518 * match any suffix here, as we will check that later. 519 */ 520 (void)snprintf(buf, sizeof(buf), "%s/%s.*", mdir->s, escpage); 521 if ((error = glob(buf, 522 GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) { 523 if (error == GLOB_NOMATCH) 524 continue; 525 else { 526 warn("globbing"); 527 (void)cleanup(); 528 exit(1); 529 } 530 } 531 if (pg->gl_matchc == 0) 532 continue; 533 534 /* 535 * start going through the matches glob(3) just found and 536 * use m.pathsearch (if present) to filter out pages we 537 * don't want. then verify the suffix is valid, and build 538 * the page if we have a _build suffix. 539 */ 540 for (cnt = pg->gl_pathc - pg->gl_matchc; 541 cnt < pg->gl_pathc; ++cnt) { 542 543 /* filter on directory path name */ 544 if (mp->pathsearch) { 545 p = strstr(pg->gl_pathv[cnt], mp->pathsearch); 546 if (!p || strchr(p, '/') == NULL) { 547 pg->gl_pathv[cnt] = ""; /* zap! */ 548 continue; 549 } 550 } 551 552 /* 553 * Try the _suffix key words first. 554 * 555 * XXX 556 * Older versions of man.conf didn't have the suffix 557 * key words, it was assumed that everything was a .0. 558 * We just test for .0 first, it's fast and probably 559 * going to hit. 560 */ 561 (void)snprintf(buf, sizeof(buf), "*/%s.0", escpage); 562 if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) 563 goto next; 564 565 found = 0; 566 TAILQ_FOREACH(suffix, &mp->suffixlist->entrylist, q) { 567 (void)snprintf(buf, 568 sizeof(buf), "*/%s%s", escpage, 569 suffix->s); 570 if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) { 571 found = 1; 572 break; 573 } 574 } 575 if (found) 576 goto next; 577 578 /* Try the _build key words next. */ 579 found = 0; 580 TAILQ_FOREACH(suffix, &mp->buildlist->entrylist, q) { 581 for (p = suffix->s; 582 *p != '\0' && !isspace((unsigned char)*p); 583 ++p) 584 continue; 585 if (*p == '\0') 586 continue; 587 *p = '\0'; 588 (void)snprintf(buf, 589 sizeof(buf), "*/%s%s", escpage, 590 suffix->s); 591 if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) { 592 if (!mp->where) 593 build_page(p + 1, 594 &pg->gl_pathv[cnt], mp); 595 *p = ' '; 596 found = 1; 597 break; 598 } 599 *p = ' '; 600 } 601 if (found) { 602 next: anyfound = 1; 603 if (!mp->all) { 604 /* Delete any other matches. */ 605 while (++cnt< pg->gl_pathc) 606 pg->gl_pathv[cnt] = ""; 607 break; 608 } 609 continue; 610 } 611 612 /* It's not a man page, forget about it. */ 613 pg->gl_pathv[cnt] = ""; 614 } 615 616 if (anyfound && !mp->all) 617 break; 618 } 619 620 /* If not found, enter onto the missing list. */ 621 if (!anyfound) { 622 if (addentry(mp->missinglist, page, 0) < 0) { 623 warn("malloc"); 624 (void)cleanup(); 625 exit(1); 626 } 627 } 628 629 free(escpage); 630 return (anyfound); 631 } 632 633 /* 634 * build_page -- 635 * Build a man page for display. 636 */ 637 static void 638 build_page(char *fmt, char **pathp, struct manstate *mp) 639 { 640 static int warned; 641 int olddir, fd, n, tmpdirlen; 642 char *p, *b; 643 char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN]; 644 const char *tmpdir; 645 646 /* Let the user know this may take awhile. */ 647 if (!warned) { 648 warned = 1; 649 warnx("Formatting manual page..."); 650 } 651 652 /* 653 * Historically man chdir'd to the root of the man tree. 654 * This was used in man pages that contained relative ".so" 655 * directives (including other man pages for command aliases etc.) 656 * It even went one step farther, by examining the first line 657 * of the man page and parsing the .so filename so it would 658 * make hard(?) links to the cat'ted man pages for space savings. 659 * (We don't do that here, but we could). 660 */ 661 662 /* copy and find the end */ 663 for (b = buf, p = *pathp; (*b++ = *p++) != '\0';) 664 continue; 665 666 /* 667 * skip the last two path components, page name and man[n] ... 668 * (e.g. buf will be "/usr/share/man" and p will be "man1/man.1") 669 * we also save a pointer to our current directory so that we 670 * can fchdir() back to it. this allows relative MANDIR paths 671 * to work with multiple man pages... e.g. consider: 672 * cd /usr/share && man -M ./man cat ls 673 * when no "cat1" subdir files are present. 674 */ 675 olddir = -1; 676 for (--b, --p, n = 2; b != buf; b--, p--) 677 if (*b == '/') 678 if (--n == 0) { 679 *b = '\0'; 680 olddir = open(".", O_RDONLY); 681 (void) chdir(buf); 682 p++; 683 break; 684 } 685 686 687 /* advance fmt pass the suffix spec to the printf format string */ 688 for (; *fmt && isspace((unsigned char)*fmt); ++fmt) 689 continue; 690 691 /* 692 * Get a temporary file and build a version of the file 693 * to display. Replace the old file name with the new one. 694 */ 695 if ((tmpdir = getenv("TMPDIR")) == NULL) 696 tmpdir = _PATH_TMP; 697 tmpdirlen = strlen(tmpdir); 698 (void)snprintf(tpath, sizeof (tpath), "%s%s%s", tmpdir, 699 (tmpdirlen && tmpdir[tmpdirlen-1] == '/') ? "" : "/", TMPFILE); 700 if ((fd = mkstemp(tpath)) == -1) { 701 warn("%s", tpath); 702 (void)cleanup(); 703 exit(1); 704 } 705 (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath); 706 (void)snprintf(cmd, sizeof(cmd), buf, p); 707 (void)system(cmd); 708 (void)close(fd); 709 if ((*pathp = strdup(tpath)) == NULL) { 710 warn("malloc"); 711 (void)cleanup(); 712 exit(1); 713 } 714 715 /* Link the built file into the remove-when-done list. */ 716 if (addentry(mp->intmp, *pathp, 0) < 0) { 717 warn("malloc"); 718 (void)cleanup(); 719 exit(1); 720 } 721 722 /* restore old directory so relative manpaths still work */ 723 if (olddir != -1) { 724 fchdir(olddir); 725 close(olddir); 726 } 727 } 728 729 /* 730 * how -- 731 * display how information 732 */ 733 static void 734 how(char *fname) 735 { 736 FILE *fp; 737 738 int lcnt, print; 739 char *p, buf[256]; 740 741 if (!(fp = fopen(fname, "r"))) { 742 warn("%s", fname); 743 (void)cleanup(); 744 exit (1); 745 } 746 #define S1 "SYNOPSIS" 747 #define S2 "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS" 748 #define D1 "DESCRIPTION" 749 #define D2 "D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN" 750 for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) { 751 if (!strncmp(buf, S1, sizeof(S1) - 1) || 752 !strncmp(buf, S2, sizeof(S2) - 1)) { 753 print = 1; 754 continue; 755 } else if (!strncmp(buf, D1, sizeof(D1) - 1) || 756 !strncmp(buf, D2, sizeof(D2) - 1)) { 757 if (fp) 758 (void)fclose(fp); 759 return; 760 } 761 if (!print) 762 continue; 763 if (*buf == '\n') 764 ++lcnt; 765 else { 766 for(; lcnt; --lcnt) 767 (void)putchar('\n'); 768 for (p = buf; isspace((unsigned char)*p); ++p) 769 continue; 770 (void)fputs(p, stdout); 771 } 772 } 773 (void)fclose(fp); 774 } 775 776 /* 777 * cat -- 778 * cat out the file 779 */ 780 static void 781 cat(char *fname) 782 { 783 int fd, n; 784 char buf[2048]; 785 786 if ((fd = open(fname, O_RDONLY, 0)) < 0) { 787 warn("%s", fname); 788 (void)cleanup(); 789 exit(1); 790 } 791 while ((n = read(fd, buf, sizeof(buf))) > 0) 792 if (write(STDOUT_FILENO, buf, n) != n) { 793 warn("write"); 794 (void)cleanup(); 795 exit (1); 796 } 797 if (n == -1) { 798 warn("read"); 799 (void)cleanup(); 800 exit(1); 801 } 802 (void)close(fd); 803 } 804 805 /* 806 * check_pager -- 807 * check the user supplied page information 808 */ 809 static const char * 810 check_pager(const char *name) 811 { 812 const char *p; 813 814 /* 815 * if the user uses "more", we make it "more -s"; watch out for 816 * PAGER = "mypager /usr/ucb/more" 817 */ 818 for (p = name; *p && !isspace((unsigned char)*p); ++p) 819 continue; 820 for (; p > name && *p != '/'; --p); 821 if (p != name) 822 ++p; 823 824 /* make sure it's "more", not "morex" */ 825 if (!strncmp(p, "more", 4) && (!p[4] || isspace((unsigned char)p[4]))){ 826 char *newname; 827 (void)asprintf(&newname, "%s %s", p, "-s"); 828 name = newname; 829 } 830 831 return (name); 832 } 833 834 /* 835 * jump -- 836 * strip out flag argument and jump 837 */ 838 static void 839 jump(char **argv, char *flag, char *name) 840 { 841 char **arg; 842 843 argv[0] = name; 844 for (arg = argv + 1; *arg; ++arg) 845 if (!strcmp(*arg, flag)) 846 break; 847 for (; *arg; ++arg) 848 arg[0] = arg[1]; 849 execvp(name, argv); 850 (void)fprintf(stderr, "%s: Command not found.\n", name); 851 exit(1); 852 } 853 854 /* 855 * onsig -- 856 * If signaled, delete the temporary files. 857 */ 858 static void 859 onsig(int signo) 860 { 861 sigset_t set; 862 863 (void)cleanup(); 864 865 (void)signal(signo, SIG_DFL); 866 867 /* unblock the signal */ 868 sigemptyset(&set); 869 sigaddset(&set, signo); 870 sigprocmask(SIG_UNBLOCK, &set, (sigset_t *) NULL); 871 872 (void)kill(getpid(), signo); 873 874 /* NOTREACHED */ 875 exit (1); 876 } 877 878 /* 879 * cleanup -- 880 * Clean up temporary files, show any error messages. 881 */ 882 static int 883 cleanup() 884 { 885 TAG *intmpp, *missp; 886 ENTRY *ep; 887 int rval; 888 889 rval = 0; 890 /* 891 * note that _missing and _intmp were created by main(), so 892 * gettag() cannot return NULL here. 893 */ 894 missp = gettag("_missing", 0); /* missing man pages */ 895 intmpp = gettag("_intmp", 0); /* tmp files we need to unlink */ 896 897 TAILQ_FOREACH(ep, &missp->entrylist, q) { 898 warnx("no entry for %s in the manual.", ep->s); 899 rval = 1; 900 } 901 902 TAILQ_FOREACH(ep, &intmpp->entrylist, q) 903 (void)unlink(ep->s); 904 905 return (rval); 906 } 907 908 /* 909 * usage -- 910 * print usage message and die 911 */ 912 static void 913 usage() 914 { 915 (void)fprintf(stderr, "usage: %s [-acw|-h] [-C cfg] [-M path] " 916 "[-m path] [-S srch] [[-s] sect] name ...\n", getprogname()); 917 (void)fprintf(stderr, 918 "usage: %s -k [-C cfg] [-M path] [-m path] keyword ...\n", 919 getprogname()); 920 exit(1); 921 } 922