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

Annotation of mandoc/manpath.c, Revision 1.20

1.20    ! schwarze    1: /*     $Id: manpath.c,v 1.19 2014/11/27 00:30:40 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"
1.20    ! schwarze   35: #define MANPATH_DEFAULT        "/usr/share/man:/usr/X11R6/man:/usr/local/man"
1.1       kristaps   36:
1.18      schwarze   37: static void     manpath_add(struct manpaths *, const char *, int);
                     38: static void     manpath_parseline(struct manpaths *, char *, int);
1.1       kristaps   39:
                     40: void
1.5       schwarze   41: manpath_parse(struct manpaths *dirs, const char *file,
                     42:                char *defp, char *auxp)
1.1       kristaps   43: {
1.17      schwarze   44: #if HAVE_MANPATH
1.11      schwarze   45:        char             cmd[(PATH_MAX * 3) + 20];
1.6       kristaps   46:        FILE            *stream;
                     47:        char            *buf;
                     48:        size_t           sz, bsz;
                     49:
                     50:        strlcpy(cmd, "manpath", sizeof(cmd));
                     51:        if (file) {
                     52:                strlcat(cmd, " -C ", sizeof(cmd));
                     53:                strlcat(cmd, file, sizeof(cmd));
                     54:        }
                     55:        if (auxp) {
                     56:                strlcat(cmd, " -m ", sizeof(cmd));
                     57:                strlcat(cmd, auxp, sizeof(cmd));
                     58:        }
                     59:        if (defp) {
                     60:                strlcat(cmd, " -M ", sizeof(cmd));
                     61:                strlcat(cmd, defp, sizeof(cmd));
                     62:        }
                     63:
                     64:        /* Open manpath(1).  Ignore errors. */
                     65:
                     66:        stream = popen(cmd, "r");
                     67:        if (NULL == stream)
                     68:                return;
                     69:
                     70:        buf = NULL;
                     71:        bsz = 0;
                     72:
                     73:        /* Read in as much output as we can. */
                     74:
                     75:        do {
                     76:                buf = mandoc_realloc(buf, bsz + 1024);
1.9       kristaps   77:                sz = fread(buf + bsz, 1, 1024, stream);
1.6       kristaps   78:                bsz += sz;
                     79:        } while (sz > 0);
                     80:
                     81:        if ( ! ferror(stream) && feof(stream) &&
                     82:                        bsz && '\n' == buf[bsz - 1]) {
                     83:                buf[bsz - 1] = '\0';
1.19      schwarze   84:                manpath_parseline(dirs, buf, 1);
1.6       kristaps   85:        }
1.1       kristaps   86:
1.6       kristaps   87:        free(buf);
                     88:        pclose(stream);
                     89: #else
1.20    ! schwarze   90:        char             manpath_default[] = MANPATH_DEFAULT;
1.8       kristaps   91:        char            *insert;
1.4       schwarze   92:
1.8       kristaps   93:        /* Always prepend -m. */
1.18      schwarze   94:        manpath_parseline(dirs, auxp, 1);
1.10      schwarze   95:
1.8       kristaps   96:        /* If -M is given, it overrides everything else. */
                     97:        if (NULL != defp) {
1.18      schwarze   98:                manpath_parseline(dirs, defp, 1);
1.8       kristaps   99:                return;
                    100:        }
1.1       kristaps  101:
1.8       kristaps  102:        /* MANPATH and man.conf(5) cooperate. */
                    103:        defp = getenv("MANPATH");
                    104:        if (NULL == file)
                    105:                file = MAN_CONF_FILE;
                    106:
                    107:        /* No MANPATH; use man.conf(5) only. */
                    108:        if (NULL == defp || '\0' == defp[0]) {
                    109:                manpath_manconf(dirs, file);
1.20    ! schwarze  110:                if (dirs->sz == 0)
        !           111:                        manpath_parseline(dirs, manpath_default, 0);
1.8       kristaps  112:                return;
                    113:        }
                    114:
                    115:        /* Prepend man.conf(5) to MANPATH. */
                    116:        if (':' == defp[0]) {
                    117:                manpath_manconf(dirs, file);
1.18      schwarze  118:                manpath_parseline(dirs, defp, 0);
1.8       kristaps  119:                return;
                    120:        }
                    121:
                    122:        /* Append man.conf(5) to MANPATH. */
1.9       kristaps  123:        if (':' == defp[strlen(defp) - 1]) {
1.18      schwarze  124:                manpath_parseline(dirs, defp, 0);
1.8       kristaps  125:                manpath_manconf(dirs, file);
                    126:                return;
                    127:        }
                    128:
                    129:        /* Insert man.conf(5) into MANPATH. */
                    130:        insert = strstr(defp, "::");
                    131:        if (NULL != insert) {
                    132:                *insert++ = '\0';
1.18      schwarze  133:                manpath_parseline(dirs, defp, 0);
1.8       kristaps  134:                manpath_manconf(dirs, file);
1.18      schwarze  135:                manpath_parseline(dirs, insert + 1, 0);
1.8       kristaps  136:                return;
                    137:        }
                    138:
                    139:        /* MANPATH overrides man.conf(5) completely. */
1.18      schwarze  140:        manpath_parseline(dirs, defp, 0);
1.6       kristaps  141: #endif
1.1       kristaps  142: }
                    143:
                    144: /*
                    145:  * Parse a FULL pathname from a colon-separated list of arrays.
                    146:  */
1.6       kristaps  147: static void
1.18      schwarze  148: manpath_parseline(struct manpaths *dirs, char *path, int complain)
1.1       kristaps  149: {
                    150:        char    *dir;
                    151:
                    152:        if (NULL == path)
                    153:                return;
                    154:
                    155:        for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
1.18      schwarze  156:                manpath_add(dirs, dir, complain);
1.1       kristaps  157: }
                    158:
                    159: /*
                    160:  * Add a directory to the array, ignoring bad directories.
                    161:  * Grow the array one-by-one for simplicity's sake.
                    162:  */
                    163: static void
1.18      schwarze  164: manpath_add(struct manpaths *dirs, const char *dir, int complain)
1.1       kristaps  165: {
                    166:        char             buf[PATH_MAX];
1.18      schwarze  167:        struct stat      sb;
1.1       kristaps  168:        char            *cp;
1.9       kristaps  169:        size_t           i;
1.1       kristaps  170:
1.18      schwarze  171:        if (NULL == (cp = realpath(dir, buf))) {
                    172:                if (complain) {
                    173:                        fputs("manpath: ", stderr);
                    174:                        perror(dir);
                    175:                }
1.1       kristaps  176:                return;
1.18      schwarze  177:        }
1.1       kristaps  178:
                    179:        for (i = 0; i < dirs->sz; i++)
                    180:                if (0 == strcmp(dirs->paths[i], dir))
                    181:                        return;
                    182:
1.18      schwarze  183:        if (stat(cp, &sb) == -1) {
                    184:                if (complain) {
                    185:                        fputs("manpath: ", stderr);
                    186:                        perror(dir);
                    187:                }
                    188:                return;
                    189:        }
                    190:
1.15      schwarze  191:        dirs->paths = mandoc_reallocarray(dirs->paths,
                    192:            dirs->sz + 1, sizeof(char *));
1.1       kristaps  193:
                    194:        dirs->paths[dirs->sz++] = mandoc_strdup(cp);
1.2       kristaps  195: }
                    196:
                    197: void
                    198: manpath_free(struct manpaths *p)
                    199: {
1.9       kristaps  200:        size_t           i;
1.2       kristaps  201:
                    202:        for (i = 0; i < p->sz; i++)
                    203:                free(p->paths[i]);
                    204:
                    205:        free(p->paths);
                    206: }
                    207:
                    208: void
1.5       schwarze  209: manpath_manconf(struct manpaths *dirs, const char *file)
1.2       kristaps  210: {
                    211:        FILE            *stream;
1.1       kristaps  212:        char            *p, *q;
1.4       schwarze  213:        size_t           len, keysz;
1.1       kristaps  214:
                    215:        keysz = strlen(MAN_CONF_KEY);
                    216:        assert(keysz > 0);
                    217:
1.3       kristaps  218:        if (NULL == (stream = fopen(file, "r")))
1.1       kristaps  219:                return;
                    220:
                    221:        while (NULL != (p = fgetln(stream, &len))) {
                    222:                if (0 == len || '\n' != p[--len])
                    223:                        break;
                    224:                p[len] = '\0';
                    225:                while (isspace((unsigned char)*p))
                    226:                        p++;
                    227:                if (strncmp(MAN_CONF_KEY, p, keysz))
                    228:                        continue;
                    229:                p += keysz;
1.12      schwarze  230:                while (isspace((unsigned char)*p))
1.1       kristaps  231:                        p++;
                    232:                if ('\0' == *p)
                    233:                        continue;
                    234:                if (NULL == (q = strrchr(p, '/')))
                    235:                        continue;
                    236:                *q = '\0';
1.18      schwarze  237:                manpath_add(dirs, p, 0);
1.1       kristaps  238:        }
                    239:
                    240:        fclose(stream);
                    241: }

CVSweb