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

Annotation of mandoc/manpath.c, Revision 1.27

1.27    ! schwarze    1: /*     $Id: manpath.c,v 1.26 2015/06/10 19:26:13 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:  *
1.22      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.22      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       kristaps   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 <ctype.h>
1.27    ! schwarze   24: #include <err.h>
1.1       kristaps   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.23      schwarze   31: #include "manconf.h"
1.1       kristaps   32:
1.26      schwarze   33: #if !HAVE_MANPATH
1.23      schwarze   34: static void     manconf_file(struct manconf *, const char *);
1.26      schwarze   35: #endif
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:
1.23      schwarze   39:
1.1       kristaps   40: void
1.23      schwarze   41: manconf_parse(struct manconf *conf, const char *file,
1.5       schwarze   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.24      schwarze   84:                manpath_parseline(&conf->manpath, buf, 1);
1.6       kristaps   85:        }
1.1       kristaps   86:
1.6       kristaps   87:        free(buf);
                     88:        pclose(stream);
                     89: #else
1.8       kristaps   90:        char            *insert;
1.4       schwarze   91:
1.8       kristaps   92:        /* Always prepend -m. */
1.23      schwarze   93:        manpath_parseline(&conf->manpath, auxp, 1);
1.10      schwarze   94:
1.8       kristaps   95:        /* If -M is given, it overrides everything else. */
                     96:        if (NULL != defp) {
1.23      schwarze   97:                manpath_parseline(&conf->manpath, 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]) {
1.23      schwarze  108:                manconf_file(conf, file);
1.8       kristaps  109:                return;
                    110:        }
                    111:
                    112:        /* Prepend man.conf(5) to MANPATH. */
                    113:        if (':' == defp[0]) {
1.23      schwarze  114:                manconf_file(conf, file);
                    115:                manpath_parseline(&conf->manpath, defp, 0);
1.8       kristaps  116:                return;
                    117:        }
                    118:
                    119:        /* Append man.conf(5) to MANPATH. */
1.9       kristaps  120:        if (':' == defp[strlen(defp) - 1]) {
1.23      schwarze  121:                manpath_parseline(&conf->manpath, defp, 0);
                    122:                manconf_file(conf, file);
1.8       kristaps  123:                return;
                    124:        }
                    125:
                    126:        /* Insert man.conf(5) into MANPATH. */
                    127:        insert = strstr(defp, "::");
                    128:        if (NULL != insert) {
                    129:                *insert++ = '\0';
1.23      schwarze  130:                manpath_parseline(&conf->manpath, defp, 0);
                    131:                manconf_file(conf, file);
                    132:                manpath_parseline(&conf->manpath, insert + 1, 0);
1.8       kristaps  133:                return;
                    134:        }
                    135:
                    136:        /* MANPATH overrides man.conf(5) completely. */
1.23      schwarze  137:        manpath_parseline(&conf->manpath, defp, 0);
1.6       kristaps  138: #endif
1.1       kristaps  139: }
                    140:
                    141: /*
                    142:  * Parse a FULL pathname from a colon-separated list of arrays.
                    143:  */
1.6       kristaps  144: static void
1.18      schwarze  145: manpath_parseline(struct manpaths *dirs, char *path, int complain)
1.1       kristaps  146: {
                    147:        char    *dir;
                    148:
                    149:        if (NULL == path)
                    150:                return;
                    151:
                    152:        for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
1.18      schwarze  153:                manpath_add(dirs, dir, complain);
1.1       kristaps  154: }
                    155:
                    156: /*
                    157:  * Add a directory to the array, ignoring bad directories.
                    158:  * Grow the array one-by-one for simplicity's sake.
                    159:  */
                    160: static void
1.18      schwarze  161: manpath_add(struct manpaths *dirs, const char *dir, int complain)
1.1       kristaps  162: {
                    163:        char             buf[PATH_MAX];
1.18      schwarze  164:        struct stat      sb;
1.1       kristaps  165:        char            *cp;
1.9       kristaps  166:        size_t           i;
1.1       kristaps  167:
1.18      schwarze  168:        if (NULL == (cp = realpath(dir, buf))) {
1.27    ! schwarze  169:                if (complain)
        !           170:                        warn("manpath: %s", dir);
1.1       kristaps  171:                return;
1.18      schwarze  172:        }
1.1       kristaps  173:
                    174:        for (i = 0; i < dirs->sz; i++)
                    175:                if (0 == strcmp(dirs->paths[i], dir))
                    176:                        return;
                    177:
1.18      schwarze  178:        if (stat(cp, &sb) == -1) {
1.27    ! schwarze  179:                if (complain)
        !           180:                        warn("manpath: %s", dir);
1.18      schwarze  181:                return;
                    182:        }
                    183:
1.15      schwarze  184:        dirs->paths = mandoc_reallocarray(dirs->paths,
                    185:            dirs->sz + 1, sizeof(char *));
1.1       kristaps  186:
                    187:        dirs->paths[dirs->sz++] = mandoc_strdup(cp);
1.2       kristaps  188: }
                    189:
                    190: void
1.23      schwarze  191: manconf_free(struct manconf *conf)
1.2       kristaps  192: {
1.9       kristaps  193:        size_t           i;
1.2       kristaps  194:
1.23      schwarze  195:        for (i = 0; i < conf->manpath.sz; i++)
                    196:                free(conf->manpath.paths[i]);
1.2       kristaps  197:
1.23      schwarze  198:        free(conf->manpath.paths);
                    199:        free(conf->output.includes);
                    200:        free(conf->output.man);
                    201:        free(conf->output.paper);
                    202:        free(conf->output.style);
1.2       kristaps  203: }
                    204:
1.26      schwarze  205: #if !HAVE_MANPATH
1.23      schwarze  206: static void
                    207: manconf_file(struct manconf *conf, const char *file)
1.2       kristaps  208: {
1.23      schwarze  209:        const char *const toks[] = { "manpath", "output", "_whatdb" };
1.25      schwarze  210:        char manpath_default[] = MANPATH_DEFAULT;
1.22      schwarze  211:
1.2       kristaps  212:        FILE            *stream;
1.22      schwarze  213:        char            *cp, *ep;
                    214:        size_t           len, tok;
1.1       kristaps  215:
1.22      schwarze  216:        if ((stream = fopen(file, "r")) == NULL)
1.25      schwarze  217:                goto out;
1.1       kristaps  218:
1.22      schwarze  219:        while ((cp = fgetln(stream, &len)) != NULL) {
                    220:                ep = cp + len;
                    221:                if (ep[-1] != '\n')
1.1       kristaps  222:                        break;
1.22      schwarze  223:                *--ep = '\0';
                    224:                while (isspace((unsigned char)*cp))
                    225:                        cp++;
                    226:                if (*cp == '#')
1.1       kristaps  227:                        continue;
1.22      schwarze  228:
                    229:                for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
                    230:                        len = strlen(toks[tok]);
                    231:                        if (cp + len < ep &&
                    232:                            isspace((unsigned char)cp[len]) &&
                    233:                            !strncmp(cp, toks[tok], len)) {
                    234:                                cp += len;
                    235:                                while (isspace((unsigned char)*cp))
                    236:                                        cp++;
                    237:                                break;
                    238:                        }
                    239:                }
                    240:
                    241:                switch (tok) {
1.23      schwarze  242:                case 2:  /* _whatdb */
1.22      schwarze  243:                        while (ep > cp && ep[-1] != '/')
                    244:                                ep--;
                    245:                        if (ep == cp)
                    246:                                continue;
                    247:                        *ep = '\0';
                    248:                        /* FALLTHROUGH */
                    249:                case 0:  /* manpath */
1.23      schwarze  250:                        manpath_add(&conf->manpath, cp, 0);
1.25      schwarze  251:                        *manpath_default = '\0';
1.23      schwarze  252:                        break;
                    253:                case 1:  /* output */
                    254:                        manconf_output(&conf->output, cp);
1.22      schwarze  255:                        break;
                    256:                default:
                    257:                        break;
                    258:                }
1.1       kristaps  259:        }
1.25      schwarze  260:        fclose(stream);
1.1       kristaps  261:
1.25      schwarze  262: out:
                    263:        if (*manpath_default != '\0')
                    264:                manpath_parseline(&conf->manpath, manpath_default, 0);
1.23      schwarze  265: }
1.26      schwarze  266: #endif
1.23      schwarze  267:
                    268: void
                    269: manconf_output(struct manoutput *conf, const char *cp)
                    270: {
                    271:        const char *const toks[] = {
                    272:            "includes", "man", "paper", "style",
                    273:            "indent", "width", "fragment", "mdoc"
                    274:        };
                    275:
                    276:        size_t   len, tok;
                    277:
                    278:        for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
                    279:                len = strlen(toks[tok]);
                    280:                if ( ! strncmp(cp, toks[tok], len) &&
                    281:                    strchr(" =  ", cp[len]) != NULL) {
                    282:                        cp += len;
                    283:                        if (*cp == '=')
                    284:                                cp++;
                    285:                        while (isspace((unsigned char)*cp))
                    286:                                cp++;
                    287:                        break;
                    288:                }
                    289:        }
                    290:
                    291:        if (tok < 6 && *cp == '\0')
                    292:                return;
                    293:
                    294:        switch (tok) {
                    295:        case 0:
                    296:                if (conf->includes == NULL)
                    297:                        conf->includes = mandoc_strdup(cp);
                    298:                break;
                    299:        case 1:
                    300:                if (conf->man == NULL)
                    301:                        conf->man = mandoc_strdup(cp);
                    302:                break;
                    303:        case 2:
                    304:                if (conf->paper == NULL)
                    305:                        conf->paper = mandoc_strdup(cp);
                    306:                break;
                    307:        case 3:
                    308:                if (conf->style == NULL)
                    309:                        conf->style = mandoc_strdup(cp);
                    310:                break;
                    311:        case 4:
                    312:                if (conf->indent == 0)
                    313:                        conf->indent = strtonum(cp, 0, 1000, NULL);
                    314:                break;
                    315:        case 5:
                    316:                if (conf->width == 0)
                    317:                        conf->width = strtonum(cp, 58, 1000, NULL);
                    318:                break;
                    319:        case 6:
                    320:                conf->fragment = 1;
                    321:                break;
                    322:        case 7:
                    323:                conf->mdoc = 1;
                    324:                break;
                    325:        default:
                    326:                break;
                    327:        }
1.1       kristaps  328: }

CVSweb