[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.7

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