[BACK]Return to man.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / man

Annotation of src/usr.bin/man/man.c, Revision 1.13

1.13    ! pjanzen     1: /*     $OpenBSD: man.c,v 1.12 1998/11/16 06:28:32 deraadt Exp $        */
1.1       deraadt     2: /*     $NetBSD: man.c,v 1.7 1995/09/28 06:05:34 tls Exp $      */
                      3:
                      4: /*
                      5:  * Copyright (c) 1987, 1993, 1994, 1995
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)man.c      8.17 (Berkeley) 1/31/95";
                     46: #else
1.13    ! pjanzen    47: static char rcsid[] = "$OpenBSD: man.c,v 1.12 1998/11/16 06:28:32 deraadt Exp $";
1.1       deraadt    48: #endif
                     49: #endif /* not lint */
                     50:
                     51: #include <sys/param.h>
                     52: #include <sys/queue.h>
                     53:
                     54: #include <ctype.h>
                     55: #include <err.h>
                     56: #include <errno.h>
                     57: #include <fcntl.h>
                     58: #include <fnmatch.h>
                     59: #include <glob.h>
                     60: #include <signal.h>
                     61: #include <stdio.h>
1.11      deraadt    62: #include <libgen.h>
1.1       deraadt    63: #include <stdlib.h>
                     64: #include <string.h>
                     65: #include <unistd.h>
                     66:
                     67: #include "config.h"
                     68: #include "pathnames.h"
                     69:
                     70: int f_all, f_where;
1.9       espie      71: static TAG *section;   /* could be passed to cleanup() instead */
1.1       deraadt    72:
1.8       millert    73: extern char *__progname;
                     74:
1.1       deraadt    75: static void     build_page __P((char *, char **));
                     76: static void     cat __P((char *));
                     77: static char    *check_pager __P((char *));
                     78: static int      cleanup __P((void));
                     79: static void     how __P((char *));
                     80: static void     jump __P((char **, char *, char *));
                     81: static int      manual __P((char *, TAG *, glob_t *));
                     82: static void     onsig __P((int));
                     83: static void     usage __P((void));
                     84:
                     85: int
                     86: main(argc, argv)
                     87:        int argc;
                     88:        char *argv[];
                     89: {
                     90:        extern char *optarg;
                     91:        extern int optind;
1.9       espie      92:        TAG *defp, *defnewp, *sectnewp, *subp;
1.1       deraadt    93:        ENTRY *e_defp, *e_sectp, *e_subp, *ep;
                     94:        glob_t pg;
                     95:        size_t len;
                     96:        int ch, f_cat, f_how, found;
1.7       millert    97:        char **ap, *cmd, *machine, *p, *p_add, *p_path, *pager, *sflag, *slashp;
1.1       deraadt    98:        char *conffile, buf[MAXPATHLEN * 2];
1.11      deraadt    99:
                    100:        if (argv[1] == NULL && strcmp(basename(__progname), "help") == 0) {
                    101:                static char *nargv[3];
                    102:                nargv[0] = "man";
                    103:                nargv[1] = "man";
                    104:                nargv[2] = NULL;
                    105:                argv = nargv;
                    106:                argc = 2;
                    107:        }
1.1       deraadt   108:
1.7       millert   109:        machine = sflag = NULL;
1.1       deraadt   110:        f_cat = f_how = 0;
                    111:        conffile = p_add = p_path = NULL;
1.7       millert   112:        while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:s:S:w")) != -1)
1.1       deraadt   113:                switch (ch) {
                    114:                case 'a':
                    115:                        f_all = 1;
                    116:                        break;
                    117:                case 'C':
                    118:                        conffile = optarg;
                    119:                        break;
                    120:                case 'c':
                    121:                case '-':               /* Deprecated. */
                    122:                        f_cat = 1;
                    123:                        break;
                    124:                case 'h':
                    125:                        f_how = 1;
                    126:                        break;
                    127:                case 'm':
                    128:                        p_add = optarg;
                    129:                        break;
                    130:                case 'M':
                    131:                case 'P':               /* Backward compatibility. */
                    132:                        p_path = optarg;
                    133:                        break;
1.7       millert   134:                case 's':               /* SVR4 compatibility. */
                    135:                        sflag = optarg;
                    136:                        break;
                    137:                case 'S':
                    138:                        machine = optarg;
                    139:                        break;
1.1       deraadt   140:                /*
                    141:                 * The -f and -k options are backward compatible,
                    142:                 * undocumented ways of calling whatis(1) and apropos(1).
                    143:                 */
                    144:                case 'f':
                    145:                        jump(argv, "-f", "whatis");
                    146:                        /* NOTREACHED */
                    147:                case 'k':
                    148:                        jump(argv, "-k", "apropos");
                    149:                        /* NOTREACHED */
                    150:                case 'w':
                    151:                        f_all = f_where = 1;
                    152:                        break;
                    153:                case '?':
                    154:                default:
                    155:                        usage();
                    156:                }
                    157:        argc -= optind;
                    158:        argv += optind;
                    159:
                    160:        if (!*argv)
                    161:                usage();
                    162:
                    163:        if (!f_cat && !f_how && !f_where)
                    164:                if (!isatty(1))
                    165:                        f_cat = 1;
1.13    ! pjanzen   166:                else if ((pager = getenv("PAGER")) != NULL && (*pager != '\0'))
1.1       deraadt   167:                        pager = check_pager(pager);
                    168:                else
                    169:                        pager = _PATH_PAGER;
                    170:
                    171:        /* Read the configuration file. */
                    172:        config(conffile);
                    173:
1.7       millert   174:        /* Get the machine type unless specified by -S. */
                    175:        if (machine == NULL && (machine = getenv("MACHINE")) == NULL)
1.1       deraadt   176:                machine = MACHINE;
                    177:
                    178:        /* If there's no _default list, create an empty one. */
                    179:        if ((defp = getlist("_default")) == NULL)
                    180:                defp = addlist("_default");
                    181:
                    182:        /*
                    183:         * 1: If the user specified a MANPATH variable, or set the -M
                    184:         *    option, we replace the _default list with the user's list,
                    185:         *    appending the entries in the _subdir list and the machine.
                    186:         */
                    187:        if (p_path == NULL)
                    188:                p_path = getenv("MANPATH");
                    189:        if (p_path != NULL) {
                    190:                while ((e_defp = defp->list.tqh_first) != NULL) {
                    191:                        free(e_defp->s);
                    192:                        TAILQ_REMOVE(&defp->list, e_defp, q);
                    193:                }
                    194:                for (p = strtok(p_path, ":");
                    195:                    p != NULL; p = strtok(NULL, ":")) {
                    196:                        slashp = p[strlen(p) - 1] == '/' ? "" : "/";
                    197:                        e_subp = (subp = getlist("_subdir")) == NULL ?
                    198:                            NULL : subp->list.tqh_first;
                    199:                        for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
                    200:                                (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
                    201:                                    p, slashp, e_subp->s, machine);
                    202:                                if ((ep = malloc(sizeof(ENTRY))) == NULL ||
                    203:                                    (ep->s = strdup(buf)) == NULL)
                    204:                                        err(1, NULL);
                    205:                                TAILQ_INSERT_TAIL(&defp->list, ep, q);
                    206:                        }
                    207:                }
                    208:        }
                    209:
                    210:        /*
                    211:         * 2: If the user did not specify MANPATH, -M or a section, rewrite
                    212:         *    the _default list to include the _subdir list and the machine.
                    213:         */
1.7       millert   214:        if (sflag == NULL && argv[1] == NULL)
1.1       deraadt   215:                section = NULL;
1.7       millert   216:        else {
                    217:                if (sflag != NULL && (section = getlist(sflag)) == NULL)
                    218:                        errx(1, "unknown manual section `%s'", sflag);
                    219:                else if (sflag == NULL && (section = getlist(*argv)) != NULL)
                    220:                        ++argv;
                    221:        }
1.1       deraadt   222:        if (p_path == NULL && section == NULL) {
                    223:                defnewp = addlist("_default_new");
                    224:                e_defp =
                    225:                    defp->list.tqh_first == NULL ? NULL : defp->list.tqh_first;
                    226:                for (; e_defp != NULL; e_defp = e_defp->q.tqe_next) {
                    227:                        slashp =
                    228:                            e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
                    229:                        e_subp = (subp = getlist("_subdir")) == NULL ?
                    230:                            NULL : subp->list.tqh_first;
                    231:                        for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
                    232:                                (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
                    233:                                e_defp->s, slashp, e_subp->s, machine);
                    234:                                if ((ep = malloc(sizeof(ENTRY))) == NULL ||
                    235:                                    (ep->s = strdup(buf)) == NULL)
                    236:                                        err(1, NULL);
                    237:                                TAILQ_INSERT_TAIL(&defnewp->list, ep, q);
                    238:                        }
                    239:                }
                    240:                defp = getlist("_default");
                    241:                while ((e_defp = defp->list.tqh_first) != NULL) {
                    242:                        free(e_defp->s);
                    243:                        TAILQ_REMOVE(&defp->list, e_defp, q);
                    244:                }
                    245:                free(defp->s);
                    246:                TAILQ_REMOVE(&head, defp, q);
                    247:                defnewp = getlist("_default_new");
                    248:                free(defnewp->s);
                    249:                defnewp->s = "_default";
                    250:                defp = defnewp;
                    251:        }
                    252:
                    253:        /*
                    254:         * 3: If the user set the -m option, insert the user's list before
                    255:         *    whatever list we have, again appending the _subdir list and
1.3       michaels  256:         *    the machine.
1.1       deraadt   257:         */
1.3       michaels  258:        if (p_add != NULL) {
                    259:                e_sectp = NULL;
1.1       deraadt   260:                for (p = strtok(p_add, ":"); p != NULL; p = strtok(NULL, ":")) {
                    261:                        slashp = p[strlen(p) - 1] == '/' ? "" : "/";
                    262:                        e_subp = (subp = getlist("_subdir")) == NULL ?
                    263:                            NULL : subp->list.tqh_first;
                    264:                        for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
                    265:                                (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
                    266:                                    p, slashp, e_subp->s, machine);
                    267:                                if ((ep = malloc(sizeof(ENTRY))) == NULL ||
                    268:                                    (ep->s = strdup(buf)) == NULL)
                    269:                                        err(1, NULL);
1.3       michaels  270:                                /* puts it at the end, should be at the top, but then the added
                    271:                                        entries would be in reverse order, fix later when all are added*/
                    272:                                TAILQ_INSERT_TAIL(&defp->list, ep, q);
                    273:                                if (e_sectp == NULL)    /* save first added, to-be the new top */
                    274:                                        e_sectp = ep;
1.1       deraadt   275:                        }
                    276:                }
1.3       michaels  277:                if (e_sectp != NULL) { /* entries added, fix order */
                    278:                        ep->q.tqe_next = defp->list.tqh_first;  /* save original head                   */
                    279:                        defp->list.tqh_first = e_sectp;                 /* first added entry, new top */
                    280:                        *e_sectp->q.tqe_prev = NULL;                            /* terminate list                                       */
                    281:                }
                    282:        }
1.1       deraadt   283:        /*
                    284:         * 4: If no -m was specified, and a section was, rewrite the section's
                    285:         *    paths (if they have a trailing slash) to append the _subdir list
                    286:         *    and the machine.  This then becomes the _default list.
                    287:         */
                    288:        if (p_add == NULL && section != NULL) {
                    289:                sectnewp = addlist("_section_new");
                    290:                for (e_sectp = section->list.tqh_first;
                    291:                    e_sectp != NULL; e_sectp = e_sectp->q.tqe_next) {
                    292:                        if (e_sectp->s[strlen(e_sectp->s) - 1] != '/') {
                    293:                                (void)snprintf(buf, sizeof(buf),
                    294:                                    "%s{/%s,}", e_sectp->s, machine);
                    295:                                if ((ep = malloc(sizeof(ENTRY))) == NULL ||
                    296:                                    (ep->s = strdup(buf)) == NULL)
                    297:                                        err(1, NULL);
                    298:                                TAILQ_INSERT_TAIL(&sectnewp->list, ep, q);
                    299:                                continue;
                    300:                        }
                    301:                        e_subp = (subp = getlist("_subdir")) == NULL ?
                    302:                            NULL : subp->list.tqh_first;
                    303:                        for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
                    304:                                (void)snprintf(buf, sizeof(buf), "%s%s{/%s,}",
                    305:                                    e_sectp->s, e_subp->s, machine);
                    306:                                if ((ep = malloc(sizeof(ENTRY))) == NULL ||
                    307:                                    (ep->s = strdup(buf)) == NULL)
                    308:                                        err(1, NULL);
                    309:                                TAILQ_INSERT_TAIL(&sectnewp->list, ep, q);
                    310:                        }
                    311:                }
                    312:                sectnewp->s = section->s;
                    313:                defp = sectnewp;
                    314:                TAILQ_REMOVE(&head, section, q);
                    315:        }
                    316:
                    317:        /*
                    318:         * 5: Search for the files.  Set up an interrupt handler, so the
                    319:         *    temporary files go away.
                    320:         */
                    321:        (void)signal(SIGINT, onsig);
                    322:        (void)signal(SIGHUP, onsig);
                    323:
                    324:        memset(&pg, 0, sizeof(pg));
                    325:        for (found = 0; *argv; ++argv)
                    326:                if (manual(*argv, defp, &pg))
                    327:                        found = 1;
                    328:
                    329:        /* 6: If nothing found, we're done. */
                    330:        if (!found) {
                    331:                (void)cleanup();
                    332:                exit (1);
                    333:        }
                    334:
                    335:        /* 7: If it's simple, display it fast. */
                    336:        if (f_cat) {
                    337:                for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
                    338:                        if (**ap == '\0')
                    339:                                continue;
                    340:                        cat(*ap);
                    341:                }
                    342:                exit (cleanup());
                    343:        }
                    344:        if (f_how) {
                    345:                for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
                    346:                        if (**ap == '\0')
                    347:                                continue;
                    348:                        how(*ap);
                    349:                }
                    350:                exit(cleanup());
                    351:        }
                    352:        if (f_where) {
                    353:                for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
                    354:                        if (**ap == '\0')
                    355:                                continue;
                    356:                        (void)printf("%s\n", *ap);
                    357:                }
                    358:                exit(cleanup());
                    359:        }
                    360:
                    361:        /*
                    362:         * 8: We display things in a single command; build a list of things
                    363:         *    to display.
                    364:         */
                    365:        for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
                    366:                if (**ap == '\0')
                    367:                        continue;
                    368:                len += strlen(*ap) + 1;
                    369:        }
                    370:        if ((cmd = malloc(len)) == NULL) {
                    371:                warn(NULL);
                    372:                (void)cleanup();
                    373:                exit(1);
                    374:        }
                    375:        p = cmd;
                    376:        len = strlen(pager);
                    377:        memmove(p, pager, len);
                    378:        p += len;
                    379:        *p++ = ' ';
                    380:        for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
                    381:                if (**ap == '\0')
                    382:                        continue;
                    383:                len = strlen(*ap);
                    384:                memmove(p, *ap, len);
                    385:                p += len;
                    386:                *p++ = ' ';
                    387:        }
1.12      deraadt   388:        *--p = '\0';
1.1       deraadt   389:
                    390:        /* Use system(3) in case someone's pager is "pager arg1 arg2". */
                    391:        (void)system(cmd);
                    392:
                    393:        exit(cleanup());
                    394: }
                    395:
                    396: /*
                    397:  * manual --
                    398:  *     Search the manuals for the pages.
                    399:  */
                    400: static int
                    401: manual(page, tag, pg)
                    402:        char *page;
                    403:        TAG *tag;
                    404:        glob_t *pg;
                    405: {
                    406:        ENTRY *ep, *e_sufp, *e_tag;
                    407:        TAG *missp, *sufp;
                    408:        int anyfound, cnt, found;
1.6       deraadt   409:        char *p, buf[MAXPATHLEN];
1.1       deraadt   410:
                    411:        anyfound = 0;
                    412:        buf[0] = '*';
                    413:
                    414:        /* For each element in the list... */
                    415:        e_tag = tag == NULL ? NULL : tag->list.tqh_first;
                    416:        for (; e_tag != NULL; e_tag = e_tag->q.tqe_next) {
                    417:                (void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
                    418:                if (glob(buf,
                    419:                    GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
                    420:                    NULL, pg)) {
                    421:                        warn("globbing");
                    422:                        (void)cleanup();
                    423:                        exit(1);
                    424:                }
                    425:                if (pg->gl_matchc == 0)
                    426:                        continue;
                    427:
                    428:                /* Find out if it's really a man page. */
                    429:                for (cnt = pg->gl_pathc - pg->gl_matchc;
                    430:                    cnt < pg->gl_pathc; ++cnt) {
                    431:
                    432:                        /*
                    433:                         * Try the _suffix key words first.
                    434:                         *
                    435:                         * XXX
                    436:                         * Older versions of man.conf didn't have the suffix
                    437:                         * key words, it was assumed that everything was a .0.
                    438:                         * We just test for .0 first, it's fast and probably
                    439:                         * going to hit.
                    440:                         */
                    441:                        (void)snprintf(buf, sizeof(buf), "*/%s.0", page);
                    442:                        if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
                    443:                                goto next;
                    444:
                    445:                        e_sufp = (sufp = getlist("_suffix")) == NULL ?
                    446:                            NULL : sufp->list.tqh_first;
                    447:                        for (found = 0;
                    448:                            e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
                    449:                                (void)snprintf(buf,
                    450:                                     sizeof(buf), "*/%s%s", page, e_sufp->s);
                    451:                                if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
                    452:                                        found = 1;
                    453:                                        break;
                    454:                                }
                    455:                        }
                    456:                        if (found)
                    457:                                goto next;
                    458:
                    459:                        /* Try the _build key words next. */
                    460:                        e_sufp = (sufp = getlist("_build")) == NULL ?
                    461:                            NULL : sufp->list.tqh_first;
                    462:                        for (found = 0;
                    463:                            e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
                    464:                                for (p = e_sufp->s;
                    465:                                    *p != '\0' && !isspace(*p); ++p);
                    466:                                if (*p == '\0')
                    467:                                        continue;
                    468:                                *p = '\0';
                    469:                                (void)snprintf(buf,
                    470:                                     sizeof(buf), "*/%s%s", page, e_sufp->s);
                    471:                                if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
                    472:                                        if (!f_where)
                    473:                                                build_page(p + 1,
                    474:                                                    &pg->gl_pathv[cnt]);
                    475:                                        *p = ' ';
                    476:                                        found = 1;
                    477:                                        break;
                    478:                                }
                    479:                                *p = ' ';
                    480:                        }
                    481:                        if (found) {
                    482: next:                          anyfound = 1;
                    483:                                if (!f_all) {
                    484:                                        /* Delete any other matches. */
                    485:                                        while (++cnt< pg->gl_pathc)
                    486:                                                pg->gl_pathv[cnt] = "";
                    487:                                        break;
                    488:                                }
                    489:                                continue;
                    490:                        }
                    491:
                    492:                        /* It's not a man page, forget about it. */
                    493:                        pg->gl_pathv[cnt] = "";
                    494:                }
                    495:
                    496:                if (anyfound && !f_all)
                    497:                        break;
                    498:        }
                    499:
                    500:        /* If not found, enter onto the missing list. */
                    501:        if (!anyfound) {
                    502:                if ((missp = getlist("_missing")) == NULL)
                    503:                        missp = addlist("_missing");
                    504:                if ((ep = malloc(sizeof(ENTRY))) == NULL ||
                    505:                    (ep->s = strdup(page)) == NULL) {
                    506:                        warn(NULL);
                    507:                        (void)cleanup();
                    508:                        exit(1);
                    509:                }
                    510:                TAILQ_INSERT_TAIL(&missp->list, ep, q);
                    511:        }
                    512:        return (anyfound);
                    513: }
                    514:
                    515: /*
                    516:  * build_page --
                    517:  *     Build a man page for display.
                    518:  */
                    519: static void
                    520: build_page(fmt, pathp)
                    521:        char *fmt, **pathp;
                    522: {
                    523:        static int warned;
                    524:        ENTRY *ep;
                    525:        TAG *intmpp;
                    526:        int fd, n;
                    527:        char *p, *b;
1.4       deraadt   528:        char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
1.1       deraadt   529:
                    530:        /* Let the user know this may take awhile. */
                    531:        if (!warned) {
                    532:                warned = 1;
                    533:                warnx("Formatting manual page...");
                    534:        }
                    535:
                    536:        /*
                    537:         * Historically man chdir'd to the root of the man tree.
                    538:         * This was used in man pages that contained relative ".so"
                    539:         * directives (including other man pages for command aliases etc.)
                    540:         * It even went one step farther, by examining the first line
                    541:         * of the man page and parsing the .so filename so it would
                    542:         * make hard(?) links to the cat'ted man pages for space savings.
                    543:         * (We don't do that here, but we could).
                    544:         */
                    545:
                    546:        /* copy and find the end */
                    547:        for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
                    548:                continue;
                    549:
                    550:        /* skip the last two path components, page name and man[n] */
                    551:        for (--b, n = 2; b != buf; b--)
                    552:                if (*b == '/')
                    553:                        if (--n == 0) {
                    554:                                *b = '\0';
                    555:                                (void) chdir(buf);
                    556:                        }
                    557:
                    558:
                    559:        /* Add a remove-when-done list. */
                    560:        if ((intmpp = getlist("_intmp")) == NULL)
                    561:                intmpp = addlist("_intmp");
                    562:
                    563:        /* Move to the printf(3) format string. */
                    564:        for (; *fmt && isspace(*fmt); ++fmt);
                    565:
                    566:        /*
                    567:         * Get a temporary file and build a version of the file
                    568:         * to display.  Replace the old file name with the new one.
                    569:         */
1.4       deraadt   570:        (void)strcpy(tpath, _PATH_TMPFILE);
1.1       deraadt   571:        if ((fd = mkstemp(tpath)) == -1) {
                    572:                warn("%s", tpath);
                    573:                (void)cleanup();
                    574:                exit(1);
                    575:        }
                    576:        (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
                    577:        (void)snprintf(cmd, sizeof(cmd), buf, *pathp);
                    578:        (void)system(cmd);
                    579:        (void)close(fd);
                    580:        if ((*pathp = strdup(tpath)) == NULL) {
                    581:                warn(NULL);
                    582:                (void)cleanup();
                    583:                exit(1);
                    584:        }
                    585:
                    586:        /* Link the built file into the remove-when-done list. */
                    587:        if ((ep = malloc(sizeof(ENTRY))) == NULL) {
                    588:                warn(NULL);
                    589:                (void)cleanup();
                    590:                exit(1);
                    591:        }
                    592:        ep->s = *pathp;
                    593:        TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
                    594: }
                    595:
                    596: /*
                    597:  * how --
                    598:  *     display how information
                    599:  */
                    600: static void
                    601: how(fname)
                    602:        char *fname;
                    603: {
                    604:        FILE *fp;
                    605:
                    606:        int lcnt, print;
                    607:        char *p, buf[256];
                    608:
                    609:        if (!(fp = fopen(fname, "r"))) {
                    610:                warn("%s", fname);
                    611:                (void)cleanup();
                    612:                exit (1);
                    613:        }
                    614: #define        S1      "SYNOPSIS"
                    615: #define        S2      "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
                    616: #define        D1      "DESCRIPTION"
                    617: #define        D2      "D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
                    618:        for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
                    619:                if (!strncmp(buf, S1, sizeof(S1) - 1) ||
                    620:                    !strncmp(buf, S2, sizeof(S2) - 1)) {
                    621:                        print = 1;
                    622:                        continue;
                    623:                } else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
                    624:                    !strncmp(buf, D2, sizeof(D2) - 1))
                    625:                        return;
                    626:                if (!print)
                    627:                        continue;
                    628:                if (*buf == '\n')
                    629:                        ++lcnt;
                    630:                else {
                    631:                        for(; lcnt; --lcnt)
                    632:                                (void)putchar('\n');
                    633:                        for (p = buf; isspace(*p); ++p);
                    634:                        (void)fputs(p, stdout);
                    635:                }
                    636:        }
                    637:        (void)fclose(fp);
                    638: }
                    639:
                    640: /*
                    641:  * cat --
                    642:  *     cat out the file
                    643:  */
                    644: static void
                    645: cat(fname)
                    646:        char *fname;
                    647: {
                    648:        int fd, n;
                    649:        char buf[2048];
                    650:
                    651:        if ((fd = open(fname, O_RDONLY, 0)) < 0) {
                    652:                warn("%s", fname);
                    653:                (void)cleanup();
                    654:                exit(1);
                    655:        }
                    656:        while ((n = read(fd, buf, sizeof(buf))) > 0)
                    657:                if (write(STDOUT_FILENO, buf, n) != n) {
                    658:                        warn("write");
                    659:                        (void)cleanup();
                    660:                        exit (1);
                    661:                }
                    662:        if (n == -1) {
                    663:                warn("read");
                    664:                (void)cleanup();
                    665:                exit(1);
                    666:        }
                    667:        (void)close(fd);
                    668: }
                    669:
                    670: /*
                    671:  * check_pager --
                    672:  *     check the user supplied page information
                    673:  */
                    674: static char *
                    675: check_pager(name)
                    676:        char *name;
                    677: {
                    678:        char *p, *save;
                    679:
                    680:        /*
                    681:         * if the user uses "more", we make it "more -s"; watch out for
                    682:         * PAGER = "mypager /usr/ucb/more"
                    683:         */
                    684:        for (p = name; *p && !isspace(*p); ++p);
                    685:        for (; p > name && *p != '/'; --p);
                    686:        if (p != name)
                    687:                ++p;
                    688:
                    689:        /* make sure it's "more", not "morex" */
                    690:        if (!strncmp(p, "more", 4) && (!p[4] || isspace(p[4]))){
                    691:                save = name;
                    692:                /* allocate space to add the "-s" */
                    693:                if (!(name =
                    694:                    malloc((u_int)(strlen(save) + sizeof("-s") + 1))))
                    695:                        err(1, NULL);
                    696:                (void)sprintf(name, "%s %s", save, "-s");
                    697:        }
                    698:        return(name);
                    699: }
                    700:
                    701: /*
                    702:  * jump --
                    703:  *     strip out flag argument and jump
                    704:  */
                    705: static void
                    706: jump(argv, flag, name)
                    707:        char **argv, *flag, *name;
                    708: {
                    709:        char **arg;
                    710:
                    711:        argv[0] = name;
                    712:        for (arg = argv + 1; *arg; ++arg)
                    713:                if (!strcmp(*arg, flag))
                    714:                        break;
                    715:        for (; *arg; ++arg)
                    716:                arg[0] = arg[1];
                    717:        execvp(name, argv);
                    718:        (void)fprintf(stderr, "%s: Command not found.\n", name);
                    719:        exit(1);
                    720: }
                    721:
                    722: /*
                    723:  * onsig --
                    724:  *     If signaled, delete the temporary files.
                    725:  */
                    726: static void
                    727: onsig(signo)
                    728:        int signo;
                    729: {
                    730:        (void)cleanup();
                    731:
                    732:        (void)signal(signo, SIG_DFL);
                    733:        (void)kill(getpid(), signo);
                    734:
                    735:        /* NOTREACHED */
                    736:        exit (1);
                    737: }
                    738:
                    739: /*
                    740:  * cleanup --
                    741:  *     Clean up temporary files, show any error messages.
                    742:  */
                    743: static int
                    744: cleanup()
                    745: {
                    746:        TAG *intmpp, *missp;
                    747:        ENTRY *ep;
                    748:        int rval;
                    749:
                    750:        rval = 0;
                    751:        ep = (missp = getlist("_missing")) == NULL ?
                    752:            NULL : missp->list.tqh_first;
                    753:        if (ep != NULL)
                    754:                for (; ep != NULL; ep = ep->q.tqe_next) {
1.9       espie     755:                        if (section)
                    756:                                warnx("no entry for %s in section %s of the manual.",
                    757:                                        ep->s, section->s);
                    758:                        else
                    759:                                warnx("no entry for %s in the manual.", ep->s);
1.1       deraadt   760:                        rval = 1;
                    761:                }
                    762:
                    763:        ep = (intmpp = getlist("_intmp")) == NULL ?
                    764:            NULL : intmpp->list.tqh_first;
                    765:        for (; ep != NULL; ep = ep->q.tqe_next)
                    766:                (void)unlink(ep->s);
                    767:        return (rval);
                    768: }
                    769:
                    770: /*
                    771:  * usage --
                    772:  *     print usage message and die
                    773:  */
                    774: static void
                    775: usage()
                    776: {
1.8       millert   777:        (void)fprintf(stderr, "usage: %s [-achw] [-C file] [-M path] [-m path] "
                    778:            "[-s section] [-S subsection] [section] title ...\n", __progname);
1.10      deraadt   779:        (void)fprintf(stderr, "usage: %s -k keyword\n", __progname);
                    780:        (void)fprintf(stderr, "usage: %s -f command\n", __progname);
1.1       deraadt   781:        exit(1);
                    782: }