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

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