[BACK]Return to manpath.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/manpath.c, Revision 1.21

1.21    ! schwarze    1: /*     $Id: manpath.c,v 1.20 2015/03/21 17:19:36 schwarze Exp $ */
1.1       kristaps    2: /*
1.20      schwarze    3:  * Copyright (c) 2011, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    4:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18: #include "config.h"
1.16      schwarze   19:
                     20: #include <sys/types.h>
1.18      schwarze   21: #include <sys/stat.h>
1.1       kristaps   22:
                     23: #include <assert.h>
                     24: #include <ctype.h>
                     25: #include <limits.h>
                     26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
                     29:
1.13      schwarze   30: #include "mandoc_aux.h"
1.1       kristaps   31: #include "manpath.h"
                     32:
                     33: #define MAN_CONF_FILE  "/etc/man.conf"
                     34: #define MAN_CONF_KEY   "_whatdb"
                     35:
1.18      schwarze   36: static void     manpath_add(struct manpaths *, const char *, int);
                     37: static void     manpath_parseline(struct manpaths *, char *, int);
1.1       kristaps   38:
                     39: void
1.5       schwarze   40: manpath_parse(struct manpaths *dirs, const char *file,
                     41:                char *defp, char *auxp)
1.1       kristaps   42: {
1.17      schwarze   43: #if HAVE_MANPATH
1.11      schwarze   44:        char             cmd[(PATH_MAX * 3) + 20];
1.6       kristaps   45:        FILE            *stream;
                     46:        char            *buf;
                     47:        size_t           sz, bsz;
                     48:
                     49:        strlcpy(cmd, "manpath", sizeof(cmd));
                     50:        if (file) {
                     51:                strlcat(cmd, " -C ", sizeof(cmd));
                     52:                strlcat(cmd, file, sizeof(cmd));
                     53:        }
                     54:        if (auxp) {
                     55:                strlcat(cmd, " -m ", sizeof(cmd));
                     56:                strlcat(cmd, auxp, sizeof(cmd));
                     57:        }
                     58:        if (defp) {
                     59:                strlcat(cmd, " -M ", sizeof(cmd));
                     60:                strlcat(cmd, defp, sizeof(cmd));
                     61:        }
                     62:
                     63:        /* Open manpath(1).  Ignore errors. */
                     64:
                     65:        stream = popen(cmd, "r");
                     66:        if (NULL == stream)
                     67:                return;
                     68:
                     69:        buf = NULL;
                     70:        bsz = 0;
                     71:
                     72:        /* Read in as much output as we can. */
                     73:
                     74:        do {
                     75:                buf = mandoc_realloc(buf, bsz + 1024);
1.9       kristaps   76:                sz = fread(buf + bsz, 1, 1024, stream);
1.6       kristaps   77:                bsz += sz;
                     78:        } while (sz > 0);
                     79:
                     80:        if ( ! ferror(stream) && feof(stream) &&
                     81:                        bsz && '\n' == buf[bsz - 1]) {
                     82:                buf[bsz - 1] = '\0';
1.19      schwarze   83:                manpath_parseline(dirs, buf, 1);
1.6       kristaps   84:        }
1.1       kristaps   85:
1.6       kristaps   86:        free(buf);
                     87:        pclose(stream);
                     88: #else
1.20      schwarze   89:        char             manpath_default[] = MANPATH_DEFAULT;
1.8       kristaps   90:        char            *insert;
1.4       schwarze   91:
1.8       kristaps   92:        /* Always prepend -m. */
1.18      schwarze   93:        manpath_parseline(dirs, auxp, 1);
1.10      schwarze   94:
1.8       kristaps   95:        /* If -M is given, it overrides everything else. */
                     96:        if (NULL != defp) {
1.18      schwarze   97:                manpath_parseline(dirs, defp, 1);
1.8       kristaps   98:                return;
                     99:        }
1.1       kristaps  100:
1.8       kristaps  101:        /* MANPATH and man.conf(5) cooperate. */
                    102:        defp = getenv("MANPATH");
                    103:        if (NULL == file)
                    104:                file = MAN_CONF_FILE;
                    105:
                    106:        /* No MANPATH; use man.conf(5) only. */
                    107:        if (NULL == defp || '\0' == defp[0]) {
                    108:                manpath_manconf(dirs, file);
1.20      schwarze  109:                if (dirs->sz == 0)
                    110:                        manpath_parseline(dirs, manpath_default, 0);
1.8       kristaps  111:                return;
                    112:        }
                    113:
                    114:        /* Prepend man.conf(5) to MANPATH. */
                    115:        if (':' == defp[0]) {
                    116:                manpath_manconf(dirs, file);
1.18      schwarze  117:                manpath_parseline(dirs, defp, 0);
1.8       kristaps  118:                return;
                    119:        }
                    120:
                    121:        /* Append man.conf(5) to MANPATH. */
1.9       kristaps  122:        if (':' == defp[strlen(defp) - 1]) {
1.18      schwarze  123:                manpath_parseline(dirs, defp, 0);
1.8       kristaps  124:                manpath_manconf(dirs, file);
                    125:                return;
                    126:        }
                    127:
                    128:        /* Insert man.conf(5) into MANPATH. */
                    129:        insert = strstr(defp, "::");
                    130:        if (NULL != insert) {
                    131:                *insert++ = '\0';
1.18      schwarze  132:                manpath_parseline(dirs, defp, 0);
1.8       kristaps  133:                manpath_manconf(dirs, file);
1.18      schwarze  134:                manpath_parseline(dirs, insert + 1, 0);
1.8       kristaps  135:                return;
                    136:        }
                    137:
                    138:        /* MANPATH overrides man.conf(5) completely. */
1.18      schwarze  139:        manpath_parseline(dirs, defp, 0);
1.6       kristaps  140: #endif
1.1       kristaps  141: }
                    142:
                    143: /*
                    144:  * Parse a FULL pathname from a colon-separated list of arrays.
                    145:  */
1.6       kristaps  146: static void
1.18      schwarze  147: manpath_parseline(struct manpaths *dirs, char *path, int complain)
1.1       kristaps  148: {
                    149:        char    *dir;
                    150:
                    151:        if (NULL == path)
                    152:                return;
                    153:
                    154:        for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
1.18      schwarze  155:                manpath_add(dirs, dir, complain);
1.1       kristaps  156: }
                    157:
                    158: /*
                    159:  * Add a directory to the array, ignoring bad directories.
                    160:  * Grow the array one-by-one for simplicity's sake.
                    161:  */
                    162: static void
1.18      schwarze  163: manpath_add(struct manpaths *dirs, const char *dir, int complain)
1.1       kristaps  164: {
                    165:        char             buf[PATH_MAX];
1.18      schwarze  166:        struct stat      sb;
1.1       kristaps  167:        char            *cp;
1.9       kristaps  168:        size_t           i;
1.1       kristaps  169:
1.18      schwarze  170:        if (NULL == (cp = realpath(dir, buf))) {
                    171:                if (complain) {
                    172:                        fputs("manpath: ", stderr);
                    173:                        perror(dir);
                    174:                }
1.1       kristaps  175:                return;
1.18      schwarze  176:        }
1.1       kristaps  177:
                    178:        for (i = 0; i < dirs->sz; i++)
                    179:                if (0 == strcmp(dirs->paths[i], dir))
                    180:                        return;
                    181:
1.18      schwarze  182:        if (stat(cp, &sb) == -1) {
                    183:                if (complain) {
                    184:                        fputs("manpath: ", stderr);
                    185:                        perror(dir);
                    186:                }
                    187:                return;
                    188:        }
                    189:
1.15      schwarze  190:        dirs->paths = mandoc_reallocarray(dirs->paths,
                    191:            dirs->sz + 1, sizeof(char *));
1.1       kristaps  192:
                    193:        dirs->paths[dirs->sz++] = mandoc_strdup(cp);
1.2       kristaps  194: }
                    195:
                    196: void
                    197: manpath_free(struct manpaths *p)
                    198: {
1.9       kristaps  199:        size_t           i;
1.2       kristaps  200:
                    201:        for (i = 0; i < p->sz; i++)
                    202:                free(p->paths[i]);
                    203:
                    204:        free(p->paths);
                    205: }
                    206:
                    207: void
1.5       schwarze  208: manpath_manconf(struct manpaths *dirs, const char *file)
1.2       kristaps  209: {
                    210:        FILE            *stream;
1.1       kristaps  211:        char            *p, *q;
1.4       schwarze  212:        size_t           len, keysz;
1.1       kristaps  213:
                    214:        keysz = strlen(MAN_CONF_KEY);
                    215:        assert(keysz > 0);
                    216:
1.3       kristaps  217:        if (NULL == (stream = fopen(file, "r")))
1.1       kristaps  218:                return;
                    219:
                    220:        while (NULL != (p = fgetln(stream, &len))) {
                    221:                if (0 == len || '\n' != p[--len])
                    222:                        break;
                    223:                p[len] = '\0';
                    224:                while (isspace((unsigned char)*p))
                    225:                        p++;
                    226:                if (strncmp(MAN_CONF_KEY, p, keysz))
                    227:                        continue;
                    228:                p += keysz;
1.12      schwarze  229:                while (isspace((unsigned char)*p))
1.1       kristaps  230:                        p++;
                    231:                if ('\0' == *p)
                    232:                        continue;
                    233:                if (NULL == (q = strrchr(p, '/')))
                    234:                        continue;
                    235:                *q = '\0';
1.18      schwarze  236:                manpath_add(dirs, p, 0);
1.1       kristaps  237:        }
                    238:
                    239:        fclose(stream);
                    240: }

CVSweb