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

1.45    ! schwarze    1: /*     $OpenBSD: man.c,v 1.44 2012/01/05 21:46:15 schwarze Exp $       */
1.1       deraadt     2: /*     $NetBSD: man.c,v 1.7 1995/09/28 06:05:34 tls Exp $      */
                      3:
                      4: /*
1.44      schwarze    5:  * Copyright (c) 2010, 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
1.39      schwarze    6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
                     11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     16:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     17:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19:
                     20: /*
1.1       deraadt    21:  * Copyright (c) 1987, 1993, 1994, 1995
                     22:  *     The Regents of the University of California.  All rights reserved.
                     23:  *
                     24:  * Redistribution and use in source and binary forms, with or without
                     25:  * modification, are permitted provided that the following conditions
                     26:  * are met:
                     27:  * 1. Redistributions of source code must retain the above copyright
                     28:  *    notice, this list of conditions and the following disclaimer.
                     29:  * 2. Redistributions in binary form must reproduce the above copyright
                     30:  *    notice, this list of conditions and the following disclaimer in the
                     31:  *    documentation and/or other materials provided with the distribution.
1.25      millert    32:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    33:  *    may be used to endorse or promote products derived from this software
                     34:  *    without specific prior written permission.
                     35:  *
                     36:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     37:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     38:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     39:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     40:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     41:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     42:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     43:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     44:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     45:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     46:  * SUCH DAMAGE.
                     47:  */
                     48:
1.42      schwarze   49: #include <sys/types.h>
1.1       deraadt    50: #include <sys/param.h>
                     51: #include <sys/queue.h>
1.42      schwarze   52: #include <sys/stat.h>
1.1       deraadt    53:
                     54: #include <ctype.h>
                     55: #include <err.h>
                     56: #include <errno.h>
                     57: #include <fcntl.h>
                     58: #include <fnmatch.h>
                     59: #include <glob.h>
                     60: #include <signal.h>
                     61: #include <stdio.h>
1.11      deraadt    62: #include <libgen.h>
1.1       deraadt    63: #include <stdlib.h>
                     64: #include <string.h>
                     65: #include <unistd.h>
                     66:
                     67: #include "config.h"
                     68: #include "pathnames.h"
                     69:
                     70: int f_all, f_where;
1.39      schwarze   71: static char gbuf[MAXPATHLEN * 2];
                     72: static TAG *section;
1.1       deraadt    73:
1.8       millert    74: extern char *__progname;
                     75:
1.39      schwarze   76: static void     clearlist(TAG *);
                     77: static void     parse_path(TAG *, char *);
                     78: static void     append_subdirs(TAG *, const char *);
1.20      millert    79: static void     build_page(char *, char **);
                     80: static void     cat(char *);
                     81: static char    *check_pager(char *);
1.41      deraadt    82: static int      cleanup(int);
1.20      millert    83: static void     how(char *);
                     84: static void     jump(char **, char *, char *);
                     85: static int      manual(char *, TAG *, glob_t *);
1.43      schwarze   86: static void     check_companion(char **, TAG *);
1.20      millert    87: static void     onsig(int);
                     88: static void     usage(void);
1.1       deraadt    89:
1.19      deraadt    90: sigset_t       blocksigs;
                     91:
1.1       deraadt    92: int
1.21      deraadt    93: main(int argc, char *argv[])
1.1       deraadt    94: {
                     95:        extern char *optarg;
                     96:        extern int optind;
1.39      schwarze   97:        TAG *searchlist;
1.1       deraadt    98:        glob_t pg;
                     99:        size_t len;
                    100:        int ch, f_cat, f_how, found;
1.39      schwarze  101:        char **ap, *cmd, *machine, *p, *p_add, *p_path, *pager, *sflag;
                    102:        char *conffile;
1.11      deraadt   103:
                    104:        if (argv[1] == NULL && strcmp(basename(__progname), "help") == 0) {
                    105:                static char *nargv[3];
                    106:                nargv[0] = "man";
1.15      aaron     107:                nargv[1] = "help";
1.11      deraadt   108:                nargv[2] = NULL;
                    109:                argv = nargv;
                    110:                argc = 2;
                    111:        }
1.1       deraadt   112:
1.7       millert   113:        machine = sflag = NULL;
1.1       deraadt   114:        f_cat = f_how = 0;
                    115:        conffile = p_add = p_path = NULL;
1.23      millert   116:        while ((ch = getopt(argc, argv, "aC:cfhkM:m:P:s:S:w-")) != -1)
1.1       deraadt   117:                switch (ch) {
                    118:                case 'a':
                    119:                        f_all = 1;
                    120:                        break;
                    121:                case 'C':
                    122:                        conffile = optarg;
                    123:                        break;
                    124:                case 'c':
                    125:                case '-':               /* Deprecated. */
                    126:                        f_cat = 1;
                    127:                        break;
                    128:                case 'h':
                    129:                        f_how = 1;
                    130:                        break;
                    131:                case 'm':
                    132:                        p_add = optarg;
                    133:                        break;
                    134:                case 'M':
                    135:                case 'P':               /* Backward compatibility. */
                    136:                        p_path = optarg;
                    137:                        break;
1.7       millert   138:                case 's':               /* SVR4 compatibility. */
                    139:                        sflag = optarg;
                    140:                        break;
                    141:                case 'S':
                    142:                        machine = optarg;
                    143:                        break;
1.1       deraadt   144:                /*
1.22      espie     145:                 * The -f and -k options are backward compatible
                    146:                 * ways of calling whatis(1) and apropos(1).
1.1       deraadt   147:                 */
                    148:                case 'f':
                    149:                        jump(argv, "-f", "whatis");
                    150:                        /* NOTREACHED */
                    151:                case 'k':
                    152:                        jump(argv, "-k", "apropos");
                    153:                        /* NOTREACHED */
                    154:                case 'w':
1.43      schwarze  155:                        f_where = 1;
1.1       deraadt   156:                        break;
                    157:                case '?':
                    158:                default:
                    159:                        usage();
                    160:                }
                    161:        argc -= optind;
                    162:        argv += optind;
                    163:
                    164:        if (!*argv)
                    165:                usage();
                    166:
1.17      millert   167:        if (!f_cat && !f_how && !f_where) {
1.1       deraadt   168:                if (!isatty(1))
                    169:                        f_cat = 1;
1.27      millert   170:                else if ((pager = getenv("MANPAGER")) != NULL &&
                    171:                                (*pager != '\0'))
                    172:                        pager = check_pager(pager);
1.13      pjanzen   173:                else if ((pager = getenv("PAGER")) != NULL && (*pager != '\0'))
1.1       deraadt   174:                        pager = check_pager(pager);
                    175:                else
                    176:                        pager = _PATH_PAGER;
1.17      millert   177:        }
1.1       deraadt   178:
                    179:        /* Read the configuration file. */
                    180:        config(conffile);
                    181:
1.39      schwarze  182:        /*
                    183:         * 1: If the user specified a section,
                    184:         *    use the section list from the configuration file.
                    185:         *    Otherwise, fall back to the default list or to an empty list.
                    186:         */
                    187:        if (sflag && (section = getlist(sflag)) == NULL)
                    188:                errx(1, "unknown manual section `%s'", sflag);
                    189:        else if (argv[1] && (section = getlist(*argv)) != NULL)
                    190:                ++argv;
                    191:
                    192:        searchlist = section;
                    193:        if (searchlist == NULL)
                    194:                searchlist = getlist("_default");
                    195:        if (searchlist == NULL)
                    196:                searchlist = addlist("_default");
1.1       deraadt   197:
                    198:        /*
1.39      schwarze  199:         * 2: If the user set the -M option or defined the MANPATH variable,
                    200:         *    clear what we have and take the user's list instead.
1.1       deraadt   201:         */
                    202:        if (p_path == NULL)
                    203:                p_path = getenv("MANPATH");
                    204:
1.39      schwarze  205:        if (p_path) {
                    206:                clearlist(searchlist);
                    207:                parse_path(searchlist, p_path);
1.1       deraadt   208:        }
                    209:
                    210:        /*
                    211:         * 3: If the user set the -m option, insert the user's list before
1.39      schwarze  212:         *    whatever list we have.
1.1       deraadt   213:         */
1.39      schwarze  214:        if (p_add)
                    215:                parse_path(searchlist, p_add);
                    216:
1.1       deraadt   217:        /*
1.39      schwarze  218:         * 4: Append the _subdir list where appropriate,
                    219:         *    and always append the machine type.
1.1       deraadt   220:         */
1.39      schwarze  221:        if (machine || (machine = getenv("MACHINE")))
                    222:                for (p = machine; *p; ++p)
                    223:                        *p = tolower(*p);
                    224:        else
                    225:                machine = MACHINE;
                    226:
                    227:        append_subdirs(searchlist, machine);
1.1       deraadt   228:
                    229:        /*
                    230:         * 5: Search for the files.  Set up an interrupt handler, so the
                    231:         *    temporary files go away.
                    232:         */
                    233:        (void)signal(SIGINT, onsig);
                    234:        (void)signal(SIGHUP, onsig);
                    235:
1.19      deraadt   236:        sigemptyset(&blocksigs);
                    237:        sigaddset(&blocksigs, SIGINT);
                    238:        sigaddset(&blocksigs, SIGHUP);
                    239:
1.1       deraadt   240:        memset(&pg, 0, sizeof(pg));
                    241:        for (found = 0; *argv; ++argv)
1.39      schwarze  242:                if (manual(*argv, searchlist, &pg))
1.1       deraadt   243:                        found = 1;
                    244:
                    245:        /* 6: If nothing found, we're done. */
                    246:        if (!found) {
1.41      deraadt   247:                (void)cleanup(0);
1.1       deraadt   248:                exit (1);
                    249:        }
                    250:
                    251:        /* 7: If it's simple, display it fast. */
                    252:        if (f_cat) {
                    253:                for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
                    254:                        if (**ap == '\0')
                    255:                                continue;
                    256:                        cat(*ap);
                    257:                }
1.41      deraadt   258:                exit (cleanup(0));
1.1       deraadt   259:        }
                    260:        if (f_how) {
                    261:                for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
                    262:                        if (**ap == '\0')
                    263:                                continue;
                    264:                        how(*ap);
                    265:                }
1.41      deraadt   266:                exit(cleanup(0));
1.1       deraadt   267:        }
                    268:        if (f_where) {
                    269:                for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
                    270:                        if (**ap == '\0')
                    271:                                continue;
1.17      millert   272:                        (void)puts(*ap);
1.1       deraadt   273:                }
1.41      deraadt   274:                exit(cleanup(0));
1.1       deraadt   275:        }
1.17      millert   276:
1.1       deraadt   277:        /*
                    278:         * 8: We display things in a single command; build a list of things
                    279:         *    to display.
                    280:         */
                    281:        for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
                    282:                if (**ap == '\0')
                    283:                        continue;
                    284:                len += strlen(*ap) + 1;
                    285:        }
                    286:        if ((cmd = malloc(len)) == NULL) {
                    287:                warn(NULL);
1.41      deraadt   288:                (void)cleanup(0);
1.1       deraadt   289:                exit(1);
                    290:        }
                    291:        p = cmd;
                    292:        len = strlen(pager);
1.17      millert   293:        memcpy(p, pager, len);
1.1       deraadt   294:        p += len;
                    295:        *p++ = ' ';
                    296:        for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
                    297:                if (**ap == '\0')
                    298:                        continue;
                    299:                len = strlen(*ap);
1.17      millert   300:                memcpy(p, *ap, len);
1.1       deraadt   301:                p += len;
                    302:                *p++ = ' ';
                    303:        }
1.12      deraadt   304:        *--p = '\0';
1.1       deraadt   305:
                    306:        /* Use system(3) in case someone's pager is "pager arg1 arg2". */
                    307:        (void)system(cmd);
                    308:
1.41      deraadt   309:        exit(cleanup(0));
1.39      schwarze  310: }
                    311:
                    312: /*
                    313:  * clearlist --
                    314:  *     Remove all entries from a list,
                    315:  *     but leave the list header intact.
                    316:  */
                    317: static void
                    318: clearlist(TAG *t)
                    319: {
                    320:        ENTRY *e;
                    321:
                    322:        while ((e = TAILQ_FIRST(&t->list)) != NULL) {
                    323:                free(e->s);
                    324:                TAILQ_REMOVE(&t->list, e, q);
                    325:                free(e);
                    326:        }
                    327: }
                    328:
                    329: /*
                    330:  * parse_path --
                    331:  *     Split the -M or -m argument or the MANPATH variable at colons,
                    332:  *     and insert the parts into the searchlist.
                    333:  */
                    334: static void
                    335: parse_path(TAG *t, char *path)
                    336: {
                    337:        ENTRY *eplast = NULL, *ep;
                    338:        char *p, *slashp;
                    339:
                    340:        while ((p = strsep(&path, ":")) != NULL) {
1.45    ! schwarze  341:                /* Skip empty fields */
        !           342:                if (*p == '\0')
        !           343:                        continue;
        !           344:
1.39      schwarze  345:                if ((ep = malloc(sizeof(ENTRY))) == NULL)
                    346:                        err(1, NULL);
                    347:
                    348:                /*
                    349:                 * Bring section specific paths to the same format as
                    350:                 * used in the configuration file, ending in /{cat,man}N.
                    351:                 */
                    352:                if (section) {
                    353:                        slashp = p[strlen(p) - 1] == '/' ? "" : "/";
                    354:                        (void)snprintf(gbuf, sizeof(gbuf),
                    355:                            "%s%s{cat,man}%s", p, slashp, t->s);
                    356:                        if ((ep->s = strdup(gbuf)) == NULL)
                    357:                                err(1, NULL);
                    358:                }
                    359:
                    360:                /* Without a section, subdirs will be appended later. */
                    361:                else
                    362:                        if ((ep->s = strdup(p)) == NULL)
                    363:                                err(1, NULL);
                    364:
                    365:                /*
                    366:                 * Even in case of -M, inserting in front is fine:
                    367:                 * We have just cleared the list.
                    368:                 */
                    369:                if (eplast)
                    370:                        TAILQ_INSERT_AFTER(&t->list, eplast, ep, q);
                    371:                else
                    372:                        TAILQ_INSERT_HEAD(&t->list, ep, q);
                    373:                eplast = ep;
                    374:        }
                    375: }
                    376:
                    377: /*
                    378:  * append_subdirs --
                    379:  *     Iterate the searchlist and append section and machine
                    380:  *     subdirectories as needed.
                    381:  */
                    382: static void
                    383: append_subdirs(TAG *t, const char *machine)
                    384: {
                    385:        TAG *tsub;
                    386:        ENTRY *eold, *elast, *enew, *esub;
                    387:        char *slashp;
                    388:
                    389:        eold = elast = TAILQ_FIRST(&t->list);
                    390:        while (eold) {
                    391:
                    392:                /*
                    393:                 * Section subdirectories *not* ending in a slash
                    394:                 * only get the machine suffix: They already had
                    395:                 * the {cat,man}N part in the configuration file
                    396:                 * or got it in parse_path().
                    397:                 */
                    398:                if (section && eold->s[strlen(eold->s)-1] != '/') {
                    399:                        (void)snprintf(gbuf, sizeof(gbuf), "%s{/%s,}",
                    400:                            eold->s, machine);
                    401:                        free(eold->s);
                    402:                        if ((eold->s = strdup(gbuf)) == NULL)
                    403:                                err(1, NULL);
                    404:                        eold = elast = TAILQ_NEXT(eold, q);
                    405:                        continue;
                    406:                }
                    407:
                    408:                /*
                    409:                 * Without a section, expand each entry using the
                    410:                 * subdir list, then drop the original entry.
                    411:                 */
                    412:                esub = (tsub = getlist("_subdir")) == NULL ?
                    413:                    NULL : TAILQ_FIRST(&tsub->list);
                    414:                while (esub) {
                    415:                        slashp = eold->s[strlen(eold->s)-1] == '/' ? "" : "/";
                    416:                        (void)snprintf(gbuf, sizeof(gbuf), "%s%s%s{/%s,}",
                    417:                            eold->s, slashp, esub->s, machine);
                    418:                        if ((enew = malloc(sizeof(ENTRY))) == NULL ||
                    419:                            (enew->s = strdup(gbuf)) == NULL)
                    420:                                err(1, NULL);
                    421:                        TAILQ_INSERT_AFTER(&t->list, elast, enew, q);
                    422:                        elast = enew;
                    423:                        esub = TAILQ_NEXT(esub, q);
                    424:                }
                    425:                elast = TAILQ_NEXT(elast, q);
                    426:                TAILQ_REMOVE(&t->list, eold, q);
                    427:                eold = elast;
                    428:        }
1.1       deraadt   429: }
                    430:
                    431: /*
                    432:  * manual --
                    433:  *     Search the manuals for the pages.
                    434:  */
                    435: static int
1.26      deraadt   436: manual(char *page, TAG *tag, glob_t *pg)
1.1       deraadt   437: {
                    438:        ENTRY *ep, *e_sufp, *e_tag;
                    439:        TAG *missp, *sufp;
1.45    ! schwarze  440:        int anyfound, cnt, found;
1.6       deraadt   441:        char *p, buf[MAXPATHLEN];
1.1       deraadt   442:
                    443:        anyfound = 0;
                    444:        buf[0] = '*';
                    445:
1.43      schwarze  446:        /* Expand the search path. */
                    447:        if (f_all != f_where) {
                    448:                e_tag = tag == NULL ? NULL : TAILQ_FIRST(&tag->list);
1.44      schwarze  449:                while (e_tag != NULL) {
1.43      schwarze  450:                        if (glob(e_tag->s, GLOB_BRACE | GLOB_NOSORT,
                    451:                            NULL, pg)) {
                    452:                                /* No GLOB_NOMATCH here due to {arch,}. */
                    453:                                warn("globbing directories");
                    454:                                (void)cleanup(0);
                    455:                                exit(1);
                    456:                        }
                    457:                        for (cnt = 0; cnt < pg->gl_pathc; cnt++) {
1.44      schwarze  458:                                if ((ep = malloc(sizeof(ENTRY))) == NULL ||
                    459:                                    (ep->s = strdup(pg->gl_pathv[cnt])) ==
1.43      schwarze  460:                                                NULL) {
                    461:                                        warn(NULL);
                    462:                                        (void)cleanup(0);
                    463:                                        exit(1);
                    464:                                }
1.44      schwarze  465:                                TAILQ_INSERT_BEFORE(e_tag, ep, q);
1.43      schwarze  466:                        }
1.44      schwarze  467:                        ep = e_tag;
                    468:                        e_tag = TAILQ_NEXT(e_tag, q);
1.43      schwarze  469:                        free(ep->s);
                    470:                        TAILQ_REMOVE(&tag->list, ep, q);
                    471:                        free(ep);
                    472:                        globfree(pg);
                    473:                        pg->gl_pathc = 0;
                    474:                }
                    475:        }
                    476:
1.1       deraadt   477:        /* For each element in the list... */
1.29      otto      478:        e_tag = tag == NULL ? NULL : TAILQ_FIRST(&tag->list);
                    479:        for (; e_tag != NULL; e_tag = TAILQ_NEXT(e_tag, q)) {
1.1       deraadt   480:                (void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
1.43      schwarze  481:                switch (glob(buf, GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT,
1.1       deraadt   482:                    NULL, pg)) {
1.43      schwarze  483:                case (0):
                    484:                        break;
                    485:                case (GLOB_NOMATCH):
                    486:                        continue;
                    487:                default:
                    488:                        warn("globbing files");
1.41      deraadt   489:                        (void)cleanup(0);
1.1       deraadt   490:                        exit(1);
                    491:                }
                    492:                if (pg->gl_matchc == 0)
                    493:                        continue;
                    494:
                    495:                /* Find out if it's really a man page. */
                    496:                for (cnt = pg->gl_pathc - pg->gl_matchc;
                    497:                    cnt < pg->gl_pathc; ++cnt) {
                    498:
1.43      schwarze  499:                        if (!f_all || !f_where) {
                    500:                                check_companion(pg->gl_pathv + cnt, tag);
                    501:                                if (*pg->gl_pathv[cnt] == '\0')
                    502:                                        continue;
                    503:                        }
1.42      schwarze  504:
1.1       deraadt   505:                        /*
                    506:                         * Try the _suffix key words first.
                    507:                         *
                    508:                         * XXX
                    509:                         * Older versions of man.conf didn't have the suffix
                    510:                         * key words, it was assumed that everything was a .0.
                    511:                         * We just test for .0 first, it's fast and probably
                    512:                         * going to hit.
                    513:                         */
                    514:                        (void)snprintf(buf, sizeof(buf), "*/%s.0", page);
                    515:                        if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
                    516:                                goto next;
                    517:
                    518:                        e_sufp = (sufp = getlist("_suffix")) == NULL ?
1.29      otto      519:                            NULL : TAILQ_FIRST(&sufp->list);
1.1       deraadt   520:                        for (found = 0;
1.29      otto      521:                            e_sufp != NULL; e_sufp = TAILQ_NEXT(e_sufp, q)) {
1.1       deraadt   522:                                (void)snprintf(buf,
                    523:                                     sizeof(buf), "*/%s%s", page, e_sufp->s);
                    524:                                if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
                    525:                                        found = 1;
                    526:                                        break;
                    527:                                }
                    528:                        }
                    529:                        if (found)
                    530:                                goto next;
                    531:
                    532:                        /* Try the _build key words next. */
                    533:                        e_sufp = (sufp = getlist("_build")) == NULL ?
1.29      otto      534:                            NULL : TAILQ_FIRST(&sufp->list);
1.1       deraadt   535:                        for (found = 0;
1.29      otto      536:                            e_sufp != NULL; e_sufp = TAILQ_NEXT(e_sufp, q)) {
1.1       deraadt   537:                                for (p = e_sufp->s;
                    538:                                    *p != '\0' && !isspace(*p); ++p);
                    539:                                if (*p == '\0')
                    540:                                        continue;
                    541:                                *p = '\0';
                    542:                                (void)snprintf(buf,
                    543:                                     sizeof(buf), "*/%s%s", page, e_sufp->s);
                    544:                                if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
                    545:                                        if (!f_where)
                    546:                                                build_page(p + 1,
                    547:                                                    &pg->gl_pathv[cnt]);
                    548:                                        *p = ' ';
                    549:                                        found = 1;
                    550:                                        break;
                    551:                                }
                    552:                                *p = ' ';
                    553:                        }
                    554:                        if (found) {
                    555: next:                          anyfound = 1;
1.43      schwarze  556:                                if (!f_all && !f_where) {
1.1       deraadt   557:                                        /* Delete any other matches. */
                    558:                                        while (++cnt< pg->gl_pathc)
                    559:                                                pg->gl_pathv[cnt] = "";
                    560:                                        break;
                    561:                                }
                    562:                                continue;
                    563:                        }
                    564:
                    565:                        /* It's not a man page, forget about it. */
                    566:                        pg->gl_pathv[cnt] = "";
                    567:                }
                    568:
1.43      schwarze  569:                if (anyfound && !f_all && !f_where)
1.1       deraadt   570:                        break;
                    571:        }
                    572:
                    573:        /* If not found, enter onto the missing list. */
                    574:        if (!anyfound) {
1.19      deraadt   575:                sigset_t osigs;
                    576:
                    577:                sigprocmask(SIG_BLOCK, &blocksigs, &osigs);
                    578:
1.1       deraadt   579:                if ((missp = getlist("_missing")) == NULL)
                    580:                        missp = addlist("_missing");
                    581:                if ((ep = malloc(sizeof(ENTRY))) == NULL ||
                    582:                    (ep->s = strdup(page)) == NULL) {
                    583:                        warn(NULL);
1.41      deraadt   584:                        (void)cleanup(0);
1.1       deraadt   585:                        exit(1);
                    586:                }
                    587:                TAILQ_INSERT_TAIL(&missp->list, ep, q);
1.19      deraadt   588:                sigprocmask(SIG_SETMASK, &osigs, NULL);
1.1       deraadt   589:        }
                    590:        return (anyfound);
1.42      schwarze  591: }
                    592:
                    593: /*
                    594:  * check_companion --
1.43      schwarze  595:  *     Check for a companion [un]formatted page.
                    596:  *     If one is found, skip this page.
                    597:  *     Use the companion instead, unless it will be found anyway.
1.42      schwarze  598:  */
                    599: static void
1.43      schwarze  600: check_companion(char **orig, TAG *tag) {
1.42      schwarze  601:        struct stat sb_orig, sb_comp;
                    602:        char *p, *pext, comp[MAXPATHLEN];
1.43      schwarze  603:        ENTRY *entry;
1.42      schwarze  604:        size_t len;
                    605:        int found;
                    606:
                    607:        len = strlcpy(comp, *orig, sizeof(comp));
                    608:        /* The minus 2 avoids a buffer overrun in case of a trailing dot. */
                    609:        p = comp + len - 2;
                    610:
                    611:        /* Locate the file name extension. */
                    612:        while (p > comp && *p != '.' && *p != '/')
                    613:                p--;
                    614:        if (*p != '.')
                    615:                return;
                    616:        pext = p + 1;
                    617:
                    618:        /* Search for slashes. */
                    619:        for (found = 0; 1; p--) {
1.43      schwarze  620:                if (*p != '/')
                    621:                        continue;
1.42      schwarze  622:
                    623:                /* Did not find /{cat,man}. */
                    624:                if (p == comp)
                    625:                        return;
                    626:
                    627:                /* Pass over one slash, the one before "page". */
1.43      schwarze  628:                if (!found++) {
                    629:                        len = p - comp;
1.42      schwarze  630:                        continue;
1.43      schwarze  631:                }
1.42      schwarze  632:
                    633:                /* Rewrite manN/page.N <-> catN/page.0. */
                    634:                if (!strncmp(p+1, "man", 3)) {
                    635:                        memcpy(++p, "cat", 3);
                    636:                        *pext++ = '0';
                    637:                        break;
                    638:                } else if (!strncmp(p+1, "cat", 3)) {
                    639:                        memcpy(++p, "man", 3);
                    640:                        p += 3;
                    641:                        while (*p != '/' && pext < comp + sizeof(comp) - 1)
                    642:                                *pext++ = *p++;
                    643:                        break;
                    644:
                    645:                /* Accept one architecture subdir, but not more. */
                    646:                } else if (found > 2)
                    647:                        return;
                    648:        }
                    649:        *pext = '\0';
                    650:
                    651:        /* Check whether both files exist. */
                    652:        if (stat(*orig, &sb_orig) || stat(comp, &sb_comp))
                    653:                return;
                    654:
                    655:        /* No action if the companion file is older. */
                    656:        if (sb_orig.st_mtim.tv_sec  > sb_comp.st_mtim.tv_sec || (
                    657:            sb_orig.st_mtim.tv_sec == sb_comp.st_mtim.tv_sec &&
                    658:            sb_orig.st_mtim.tv_nsec > sb_comp.st_mtim.tv_nsec))
                    659:                return;
1.43      schwarze  660:
                    661:        /* Drop the companion if it is in the path, too. */
                    662:        if (f_all || f_where)
                    663:                for(entry = TAILQ_FIRST(&tag->list); entry != NULL;
                    664:                    entry = TAILQ_NEXT(entry, q))
                    665:                        if (!strncmp(entry->s, comp, len)) {
                    666:                                **orig = '\0';
                    667:                                return;
                    668:                        }
1.42      schwarze  669:
                    670:        /* The companion file is newer, use it. */
                    671:        free(*orig);
                    672:        if ((p = strdup(comp)) == NULL) {
                    673:                warn(NULL);
                    674:                (void)cleanup(0);
                    675:                exit(1);
                    676:        }
                    677:        *orig = p;
1.1       deraadt   678: }
                    679:
1.17      millert   680: /*
1.1       deraadt   681:  * build_page --
                    682:  *     Build a man page for display.
                    683:  */
                    684: static void
1.21      deraadt   685: build_page(char *fmt, char **pathp)
1.1       deraadt   686: {
                    687:        ENTRY *ep;
                    688:        TAG *intmpp;
                    689:        int fd, n;
                    690:        char *p, *b;
1.4       deraadt   691:        char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
1.19      deraadt   692:        sigset_t osigs;
1.1       deraadt   693:
                    694:        /*
1.17      millert   695:         * Historically man chdir'd to the root of the man tree.
1.1       deraadt   696:         * This was used in man pages that contained relative ".so"
                    697:         * directives (including other man pages for command aliases etc.)
                    698:         * It even went one step farther, by examining the first line
                    699:         * of the man page and parsing the .so filename so it would
                    700:         * make hard(?) links to the cat'ted man pages for space savings.
                    701:         * (We don't do that here, but we could).
                    702:         */
1.17      millert   703:
1.1       deraadt   704:        /* copy and find the end */
                    705:        for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
                    706:                continue;
1.17      millert   707:
1.1       deraadt   708:        /* skip the last two path components, page name and man[n] */
                    709:        for (--b, n = 2; b != buf; b--)
                    710:                if (*b == '/')
                    711:                        if (--n == 0) {
                    712:                                *b = '\0';
                    713:                                (void) chdir(buf);
                    714:                        }
                    715:
                    716:
                    717:        /* Add a remove-when-done list. */
1.19      deraadt   718:        sigprocmask(SIG_BLOCK, &blocksigs, &osigs);
1.1       deraadt   719:        if ((intmpp = getlist("_intmp")) == NULL)
                    720:                intmpp = addlist("_intmp");
1.19      deraadt   721:        sigprocmask(SIG_SETMASK, &osigs, NULL);
1.1       deraadt   722:
                    723:        /* Move to the printf(3) format string. */
1.16      deraadt   724:        for (; *fmt && isspace(*fmt); ++fmt)
                    725:                ;
1.1       deraadt   726:
                    727:        /*
                    728:         * Get a temporary file and build a version of the file
                    729:         * to display.  Replace the old file name with the new one.
                    730:         */
1.18      deraadt   731:        (void)strlcpy(tpath, _PATH_TMPFILE, sizeof(tpath));
1.1       deraadt   732:        if ((fd = mkstemp(tpath)) == -1) {
                    733:                warn("%s", tpath);
1.41      deraadt   734:                (void)cleanup(0);
1.1       deraadt   735:                exit(1);
                    736:        }
                    737:        (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
                    738:        (void)snprintf(cmd, sizeof(cmd), buf, *pathp);
                    739:        (void)system(cmd);
                    740:        (void)close(fd);
                    741:        if ((*pathp = strdup(tpath)) == NULL) {
                    742:                warn(NULL);
1.41      deraadt   743:                (void)cleanup(0);
1.1       deraadt   744:                exit(1);
                    745:        }
                    746:
                    747:        /* Link the built file into the remove-when-done list. */
                    748:        if ((ep = malloc(sizeof(ENTRY))) == NULL) {
                    749:                warn(NULL);
1.41      deraadt   750:                (void)cleanup(0);
1.1       deraadt   751:                exit(1);
                    752:        }
                    753:        ep->s = *pathp;
1.41      deraadt   754:
                    755:        sigprocmask(SIG_BLOCK, &blocksigs, &osigs);
1.1       deraadt   756:        TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
1.41      deraadt   757:        sigprocmask(SIG_SETMASK, &osigs, NULL);
1.1       deraadt   758: }
                    759:
                    760: /*
                    761:  * how --
                    762:  *     display how information
                    763:  */
                    764: static void
1.21      deraadt   765: how(char *fname)
1.1       deraadt   766: {
                    767:        FILE *fp;
                    768:
                    769:        int lcnt, print;
                    770:        char *p, buf[256];
                    771:
                    772:        if (!(fp = fopen(fname, "r"))) {
                    773:                warn("%s", fname);
1.41      deraadt   774:                (void)cleanup(0);
1.1       deraadt   775:                exit (1);
                    776:        }
                    777: #define        S1      "SYNOPSIS"
                    778: #define        S2      "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
                    779:        for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
                    780:                if (!strncmp(buf, S1, sizeof(S1) - 1) ||
                    781:                    !strncmp(buf, S2, sizeof(S2) - 1)) {
                    782:                        print = 1;
                    783:                        continue;
1.34      otto      784:                } else if (print) {
                    785:                        char *p = buf;
                    786:                        int allcaps = 0;
                    787:
                    788:                        while (*p) {
                    789:                                if (!allcaps && isalpha(*p))
                    790:                                        allcaps = 1;
                    791:                                if (isalpha(*p) && !isupper(*p)) {
                    792:                                        allcaps = 0;
                    793:                                        break;
                    794:                                }
                    795:                                p++;
                    796:                        }
                    797:                        if (allcaps) {
                    798:                                (void)fclose(fp);
                    799:                                return;
                    800:                        }
1.32      jasper    801:                }
1.1       deraadt   802:                if (!print)
                    803:                        continue;
                    804:                if (*buf == '\n')
                    805:                        ++lcnt;
                    806:                else {
1.17      millert   807:                        while (lcnt) {
                    808:                                --lcnt;
1.1       deraadt   809:                                (void)putchar('\n');
1.17      millert   810:                        }
                    811:                        for (p = buf; isspace(*p); ++p)
                    812:                                ;
1.1       deraadt   813:                        (void)fputs(p, stdout);
                    814:                }
                    815:        }
                    816:        (void)fclose(fp);
                    817: }
                    818:
                    819: /*
                    820:  * cat --
                    821:  *     cat out the file
                    822:  */
                    823: static void
1.21      deraadt   824: cat(char *fname)
1.1       deraadt   825: {
                    826:        int fd, n;
                    827:        char buf[2048];
                    828:
                    829:        if ((fd = open(fname, O_RDONLY, 0)) < 0) {
                    830:                warn("%s", fname);
1.41      deraadt   831:                (void)cleanup(0);
1.1       deraadt   832:                exit(1);
                    833:        }
                    834:        while ((n = read(fd, buf, sizeof(buf))) > 0)
                    835:                if (write(STDOUT_FILENO, buf, n) != n) {
                    836:                        warn("write");
1.41      deraadt   837:                        (void)cleanup(0);
1.1       deraadt   838:                        exit (1);
                    839:                }
                    840:        if (n == -1) {
                    841:                warn("read");
1.41      deraadt   842:                (void)cleanup(0);
1.1       deraadt   843:                exit(1);
                    844:        }
                    845:        (void)close(fd);
                    846: }
                    847:
                    848: /*
                    849:  * check_pager --
                    850:  *     check the user supplied page information
                    851:  */
                    852: static char *
1.21      deraadt   853: check_pager(char *name)
1.1       deraadt   854: {
                    855:        char *p, *save;
                    856:
                    857:        /*
                    858:         * if the user uses "more", we make it "more -s"; watch out for
1.17      millert   859:         * PAGER = "mypager /usr/bin/more"
1.1       deraadt   860:         */
1.16      deraadt   861:        for (p = name; *p && !isspace(*p); ++p)
                    862:                ;
                    863:        for (; p > name && *p != '/'; --p)
                    864:                ;
1.1       deraadt   865:        if (p != name)
                    866:                ++p;
                    867:
                    868:        /* make sure it's "more", not "morex" */
1.27      millert   869:        if (!strncmp(p, "more", 4) && (p[4] == '\0' || isspace(p[4]))){
1.1       deraadt   870:                save = name;
                    871:                /* allocate space to add the "-s" */
1.31      deraadt   872:                if (asprintf(&name, "%s -s", save) == -1)
                    873:                        err(1, "asprintf");
1.1       deraadt   874:        }
                    875:        return(name);
                    876: }
                    877:
                    878: /*
                    879:  * jump --
                    880:  *     strip out flag argument and jump
                    881:  */
                    882: static void
1.21      deraadt   883: jump(char **argv, char *flag, char *name)
1.1       deraadt   884: {
                    885:        char **arg;
                    886:
                    887:        argv[0] = name;
                    888:        for (arg = argv + 1; *arg; ++arg)
                    889:                if (!strcmp(*arg, flag))
                    890:                        break;
                    891:        for (; *arg; ++arg)
                    892:                arg[0] = arg[1];
                    893:        execvp(name, argv);
                    894:        (void)fprintf(stderr, "%s: Command not found.\n", name);
                    895:        exit(1);
                    896: }
                    897:
1.17      millert   898: /*
1.1       deraadt   899:  * onsig --
                    900:  *     If signaled, delete the temporary files.
                    901:  */
                    902: static void
1.21      deraadt   903: onsig(int signo)
1.1       deraadt   904: {
1.41      deraadt   905:        (void)cleanup(1);
1.1       deraadt   906:
                    907:        (void)signal(signo, SIG_DFL);
                    908:        (void)kill(getpid(), signo);
                    909:
                    910:        /* NOTREACHED */
1.19      deraadt   911:        _exit(1);
1.1       deraadt   912: }
                    913:
                    914: /*
                    915:  * cleanup --
                    916:  *     Clean up temporary files, show any error messages.
                    917:  */
                    918: static int
1.41      deraadt   919: cleanup(int insig)
1.1       deraadt   920: {
                    921:        TAG *intmpp, *missp;
                    922:        ENTRY *ep;
1.41      deraadt   923:        int rval = 0;
1.1       deraadt   924:
1.41      deraadt   925:        if (insig == 0) {
                    926:                ep = (missp = getlist("_missing")) == NULL ?
                    927:                    NULL : TAILQ_FIRST(&missp->list);
                    928:                if (ep != NULL)
                    929:                        for (; ep != NULL; ep = TAILQ_NEXT(ep, q)) {
                    930:                                if (section)
                    931:                                        warnx("no entry for %s in "
                    932:                                            "section %s of the manual.",
                    933:                                                ep->s, section->s);
                    934:                                else
                    935:                                        warnx("no entry for %s in the manual.",
                    936:                                            ep->s);
                    937:                                rval = 1;
                    938:                        }
                    939:        }
1.1       deraadt   940:
                    941:        ep = (intmpp = getlist("_intmp")) == NULL ?
1.29      otto      942:            NULL : TAILQ_FIRST(&intmpp->list);
                    943:        for (; ep != NULL; ep = TAILQ_NEXT(ep, q))
1.1       deraadt   944:                (void)unlink(ep->s);
                    945:        return (rval);
                    946: }
                    947:
                    948: /*
                    949:  * usage --
                    950:  *     print usage message and die
                    951:  */
                    952: static void
1.21      deraadt   953: usage(void)
1.1       deraadt   954: {
1.8       millert   955:        (void)fprintf(stderr, "usage: %s [-achw] [-C file] [-M path] [-m path] "
1.35      sobrado   956:            "[-S subsection] [-s section]\n\t   [section] name ...\n",
1.28      jmc       957:            __progname);
1.36      jmc       958:        (void)fprintf(stderr, "       %s -f command ...\n", __progname);
                    959:        (void)fprintf(stderr, "       %s -k keyword ...\n", __progname);
1.1       deraadt   960:        exit(1);
                    961: }