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

Annotation of mandoc/manpath.c, Revision 1.10

1.10    ! schwarze    1: /*     $Id: manpath.c,v 1.9 2012/06/08 10:32:40 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
                      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: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.10    ! schwarze   22: #ifdef USE_MANPATH
1.6       kristaps   23: #include <sys/param.h>
1.10    ! schwarze   24: #endif
1.6       kristaps   25:
1.1       kristaps   26: #include <assert.h>
                     27: #include <ctype.h>
                     28: #include <limits.h>
                     29: #include <stdio.h>
                     30: #include <stdlib.h>
                     31: #include <string.h>
                     32:
                     33: #include "mandoc.h"
                     34: #include "manpath.h"
                     35:
                     36: #define MAN_CONF_FILE  "/etc/man.conf"
                     37: #define MAN_CONF_KEY   "_whatdb"
                     38:
                     39: static void     manpath_add(struct manpaths *, const char *);
1.6       kristaps   40: static void     manpath_parseline(struct manpaths *, char *);
1.1       kristaps   41:
                     42: void
1.5       schwarze   43: manpath_parse(struct manpaths *dirs, const char *file,
                     44:                char *defp, char *auxp)
1.1       kristaps   45: {
1.6       kristaps   46: #ifdef USE_MANPATH
                     47:        char             cmd[(MAXPATHLEN * 3) + 20];
                     48:        FILE            *stream;
                     49:        char            *buf;
                     50:        size_t           sz, bsz;
                     51:
                     52:        strlcpy(cmd, "manpath", sizeof(cmd));
                     53:        if (file) {
                     54:                strlcat(cmd, " -C ", sizeof(cmd));
                     55:                strlcat(cmd, file, sizeof(cmd));
                     56:        }
                     57:        if (auxp) {
                     58:                strlcat(cmd, " -m ", sizeof(cmd));
                     59:                strlcat(cmd, auxp, sizeof(cmd));
                     60:        }
                     61:        if (defp) {
                     62:                strlcat(cmd, " -M ", sizeof(cmd));
                     63:                strlcat(cmd, defp, sizeof(cmd));
                     64:        }
                     65:
                     66:        /* Open manpath(1).  Ignore errors. */
                     67:
                     68:        stream = popen(cmd, "r");
                     69:        if (NULL == stream)
                     70:                return;
                     71:
                     72:        buf = NULL;
                     73:        bsz = 0;
                     74:
                     75:        /* Read in as much output as we can. */
                     76:
                     77:        do {
                     78:                buf = mandoc_realloc(buf, bsz + 1024);
1.9       kristaps   79:                sz = fread(buf + bsz, 1, 1024, stream);
1.6       kristaps   80:                bsz += sz;
                     81:        } while (sz > 0);
                     82:
                     83:        if ( ! ferror(stream) && feof(stream) &&
                     84:                        bsz && '\n' == buf[bsz - 1]) {
                     85:                buf[bsz - 1] = '\0';
                     86:                manpath_parseline(dirs, buf);
                     87:        }
1.1       kristaps   88:
1.6       kristaps   89:        free(buf);
                     90:        pclose(stream);
                     91: #else
1.8       kristaps   92:        char            *insert;
1.4       schwarze   93:
1.8       kristaps   94:        /* Always prepend -m. */
1.10    ! schwarze   95:        manpath_parseline(dirs, auxp);
        !            96:
1.8       kristaps   97:        /* If -M is given, it overrides everything else. */
                     98:        if (NULL != defp) {
                     99:                manpath_parseline(dirs, defp);
                    100:                return;
                    101:        }
1.1       kristaps  102:
1.8       kristaps  103:        /* MANPATH and man.conf(5) cooperate. */
                    104:        defp = getenv("MANPATH");
                    105:        if (NULL == file)
                    106:                file = MAN_CONF_FILE;
                    107:
                    108:        /* No MANPATH; use man.conf(5) only. */
                    109:        if (NULL == defp || '\0' == defp[0]) {
                    110:                manpath_manconf(dirs, file);
                    111:                return;
                    112:        }
                    113:
                    114:        /* Prepend man.conf(5) to MANPATH. */
                    115:        if (':' == defp[0]) {
                    116:                manpath_manconf(dirs, file);
1.1       kristaps  117:                manpath_parseline(dirs, defp);
1.8       kristaps  118:                return;
                    119:        }
                    120:
                    121:        /* Append man.conf(5) to MANPATH. */
1.9       kristaps  122:        if (':' == defp[strlen(defp) - 1]) {
1.8       kristaps  123:                manpath_parseline(dirs, defp);
                    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';
                    132:                manpath_parseline(dirs, defp);
                    133:                manpath_manconf(dirs, file);
                    134:                manpath_parseline(dirs, insert + 1);
                    135:                return;
                    136:        }
                    137:
                    138:        /* MANPATH overrides man.conf(5) completely. */
                    139:        manpath_parseline(dirs, defp);
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.4       schwarze  147: manpath_parseline(struct manpaths *dirs, char *path)
1.1       kristaps  148: {
                    149:        char    *dir;
                    150:
                    151:        if (NULL == path)
                    152:                return;
                    153:
                    154:        for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
                    155:                manpath_add(dirs, dir);
                    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.4       schwarze  163: manpath_add(struct manpaths *dirs, const char *dir)
1.1       kristaps  164: {
                    165:        char             buf[PATH_MAX];
                    166:        char            *cp;
1.9       kristaps  167:        size_t           i;
1.1       kristaps  168:
                    169:        if (NULL == (cp = realpath(dir, buf)))
                    170:                return;
                    171:
                    172:        for (i = 0; i < dirs->sz; i++)
                    173:                if (0 == strcmp(dirs->paths[i], dir))
                    174:                        return;
                    175:
                    176:        dirs->paths = mandoc_realloc
1.4       schwarze  177:                (dirs->paths,
1.9       kristaps  178:                 (dirs->sz + 1) * sizeof(char *));
1.1       kristaps  179:
                    180:        dirs->paths[dirs->sz++] = mandoc_strdup(cp);
1.2       kristaps  181: }
                    182:
                    183: void
                    184: manpath_free(struct manpaths *p)
                    185: {
1.9       kristaps  186:        size_t           i;
1.2       kristaps  187:
                    188:        for (i = 0; i < p->sz; i++)
                    189:                free(p->paths[i]);
                    190:
                    191:        free(p->paths);
                    192: }
                    193:
                    194: void
1.5       schwarze  195: manpath_manconf(struct manpaths *dirs, const char *file)
1.2       kristaps  196: {
                    197:        FILE            *stream;
1.1       kristaps  198:        char            *p, *q;
1.4       schwarze  199:        size_t           len, keysz;
1.1       kristaps  200:
                    201:        keysz = strlen(MAN_CONF_KEY);
                    202:        assert(keysz > 0);
                    203:
1.3       kristaps  204:        if (NULL == (stream = fopen(file, "r")))
1.1       kristaps  205:                return;
                    206:
                    207:        while (NULL != (p = fgetln(stream, &len))) {
                    208:                if (0 == len || '\n' != p[--len])
                    209:                        break;
                    210:                p[len] = '\0';
                    211:                while (isspace((unsigned char)*p))
                    212:                        p++;
                    213:                if (strncmp(MAN_CONF_KEY, p, keysz))
                    214:                        continue;
                    215:                p += keysz;
                    216:                while (isspace(*p))
                    217:                        p++;
                    218:                if ('\0' == *p)
                    219:                        continue;
                    220:                if (NULL == (q = strrchr(p, '/')))
                    221:                        continue;
                    222:                *q = '\0';
                    223:                manpath_add(dirs, p);
                    224:        }
                    225:
                    226:        fclose(stream);
                    227: }

CVSweb