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

Annotation of mandoc/manpath.c, Revision 1.40

1.40    ! schwarze    1: /*     $Id: manpath.c,v 1.39 2019/05/03 18:39:34 schwarze Exp $ */
1.1       kristaps    2: /*
1.40    ! schwarze    3:  * Copyright (c) 2011,2014,2015,2017-2019 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.40    ! schwarze   24: #include <errno.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.40    ! schwarze   31: #include "mandoc.h"
1.23      schwarze   32: #include "manconf.h"
1.1       kristaps   33:
1.23      schwarze   34: static void     manconf_file(struct manconf *, const char *);
1.40    ! schwarze   35: static void     manpath_add(struct manpaths *, const char *, char);
        !            36: static void     manpath_parseline(struct manpaths *, char *, char);
1.1       kristaps   37:
1.23      schwarze   38:
1.1       kristaps   39: void
1.23      schwarze   40: manconf_parse(struct manconf *conf, const char *file,
1.5       schwarze   41:                char *defp, char *auxp)
1.1       kristaps   42: {
1.8       kristaps   43:        char            *insert;
1.4       schwarze   44:
1.8       kristaps   45:        /* Always prepend -m. */
1.40    ! schwarze   46:        manpath_parseline(&conf->manpath, auxp, 'm');
1.10      schwarze   47:
1.8       kristaps   48:        /* If -M is given, it overrides everything else. */
                     49:        if (NULL != defp) {
1.40    ! schwarze   50:                manpath_parseline(&conf->manpath, defp, 'M');
1.8       kristaps   51:                return;
                     52:        }
1.1       kristaps   53:
1.8       kristaps   54:        /* MANPATH and man.conf(5) cooperate. */
                     55:        defp = getenv("MANPATH");
                     56:        if (NULL == file)
                     57:                file = MAN_CONF_FILE;
                     58:
                     59:        /* No MANPATH; use man.conf(5) only. */
                     60:        if (NULL == defp || '\0' == defp[0]) {
1.23      schwarze   61:                manconf_file(conf, file);
1.8       kristaps   62:                return;
                     63:        }
                     64:
                     65:        /* Prepend man.conf(5) to MANPATH. */
                     66:        if (':' == defp[0]) {
1.23      schwarze   67:                manconf_file(conf, file);
1.40    ! schwarze   68:                manpath_parseline(&conf->manpath, defp, '\0');
1.8       kristaps   69:                return;
                     70:        }
                     71:
                     72:        /* Append man.conf(5) to MANPATH. */
1.9       kristaps   73:        if (':' == defp[strlen(defp) - 1]) {
1.40    ! schwarze   74:                manpath_parseline(&conf->manpath, defp, '\0');
1.23      schwarze   75:                manconf_file(conf, file);
1.8       kristaps   76:                return;
                     77:        }
                     78:
                     79:        /* Insert man.conf(5) into MANPATH. */
                     80:        insert = strstr(defp, "::");
                     81:        if (NULL != insert) {
                     82:                *insert++ = '\0';
1.40    ! schwarze   83:                manpath_parseline(&conf->manpath, defp, '\0');
1.23      schwarze   84:                manconf_file(conf, file);
1.40    ! schwarze   85:                manpath_parseline(&conf->manpath, insert + 1, '\0');
1.8       kristaps   86:                return;
                     87:        }
                     88:
                     89:        /* MANPATH overrides man.conf(5) completely. */
1.40    ! schwarze   90:        manpath_parseline(&conf->manpath, defp, '\0');
1.35      schwarze   91: }
                     92:
                     93: void
                     94: manpath_base(struct manpaths *dirs)
                     95: {
                     96:        char path_base[] = MANPATH_BASE;
1.40    ! schwarze   97:        manpath_parseline(dirs, path_base, '\0');
1.1       kristaps   98: }
                     99:
                    100: /*
                    101:  * Parse a FULL pathname from a colon-separated list of arrays.
                    102:  */
1.6       kristaps  103: static void
1.40    ! schwarze  104: manpath_parseline(struct manpaths *dirs, char *path, char option)
1.1       kristaps  105: {
                    106:        char    *dir;
                    107:
                    108:        if (NULL == path)
                    109:                return;
                    110:
                    111:        for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
1.40    ! schwarze  112:                manpath_add(dirs, dir, option);
1.1       kristaps  113: }
                    114:
                    115: /*
                    116:  * Add a directory to the array, ignoring bad directories.
                    117:  * Grow the array one-by-one for simplicity's sake.
                    118:  */
                    119: static void
1.40    ! schwarze  120: manpath_add(struct manpaths *dirs, const char *dir, char option)
1.1       kristaps  121: {
                    122:        char             buf[PATH_MAX];
1.18      schwarze  123:        struct stat      sb;
1.1       kristaps  124:        char            *cp;
1.9       kristaps  125:        size_t           i;
1.1       kristaps  126:
1.40    ! schwarze  127:        if ((cp = realpath(dir, buf)) == NULL)
        !           128:                goto fail;
1.1       kristaps  129:
                    130:        for (i = 0; i < dirs->sz; i++)
1.40    ! schwarze  131:                if (strcmp(dirs->paths[i], dir) == 0)
1.1       kristaps  132:                        return;
                    133:
1.40    ! schwarze  134:        if (stat(cp, &sb) == -1)
        !           135:                goto fail;
1.18      schwarze  136:
1.15      schwarze  137:        dirs->paths = mandoc_reallocarray(dirs->paths,
1.40    ! schwarze  138:            dirs->sz + 1, sizeof(*dirs->paths));
        !           139:        dirs->paths[dirs->sz++] = mandoc_strdup(cp);
        !           140:        return;
1.1       kristaps  141:
1.40    ! schwarze  142: fail:
        !           143:        if (option != '\0')
        !           144:                mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0,
        !           145:                    "-%c %s: %s", option, dir, strerror(errno));
1.2       kristaps  146: }
                    147:
                    148: void
1.23      schwarze  149: manconf_free(struct manconf *conf)
1.2       kristaps  150: {
1.9       kristaps  151:        size_t           i;
1.2       kristaps  152:
1.23      schwarze  153:        for (i = 0; i < conf->manpath.sz; i++)
                    154:                free(conf->manpath.paths[i]);
1.2       kristaps  155:
1.23      schwarze  156:        free(conf->manpath.paths);
                    157:        free(conf->output.includes);
                    158:        free(conf->output.man);
                    159:        free(conf->output.paper);
                    160:        free(conf->output.style);
1.2       kristaps  161: }
                    162:
1.23      schwarze  163: static void
                    164: manconf_file(struct manconf *conf, const char *file)
1.2       kristaps  165: {
1.23      schwarze  166:        const char *const toks[] = { "manpath", "output", "_whatdb" };
1.25      schwarze  167:        char manpath_default[] = MANPATH_DEFAULT;
1.22      schwarze  168:
1.2       kristaps  169:        FILE            *stream;
1.29      schwarze  170:        char            *line, *cp, *ep;
                    171:        size_t           linesz, tok, toklen;
                    172:        ssize_t          linelen;
1.1       kristaps  173:
1.22      schwarze  174:        if ((stream = fopen(file, "r")) == NULL)
1.25      schwarze  175:                goto out;
1.1       kristaps  176:
1.29      schwarze  177:        line = NULL;
                    178:        linesz = 0;
                    179:
                    180:        while ((linelen = getline(&line, &linesz, stream)) != -1) {
                    181:                cp = line;
1.30      schwarze  182:                ep = cp + linelen - 1;
                    183:                while (ep > cp && isspace((unsigned char)*ep))
                    184:                        *ep-- = '\0';
1.22      schwarze  185:                while (isspace((unsigned char)*cp))
                    186:                        cp++;
1.30      schwarze  187:                if (cp == ep || *cp == '#')
1.1       kristaps  188:                        continue;
1.22      schwarze  189:
                    190:                for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
1.29      schwarze  191:                        toklen = strlen(toks[tok]);
                    192:                        if (cp + toklen < ep &&
                    193:                            isspace((unsigned char)cp[toklen]) &&
                    194:                            strncmp(cp, toks[tok], toklen) == 0) {
                    195:                                cp += toklen;
1.22      schwarze  196:                                while (isspace((unsigned char)*cp))
                    197:                                        cp++;
                    198:                                break;
                    199:                        }
                    200:                }
                    201:
                    202:                switch (tok) {
1.23      schwarze  203:                case 2:  /* _whatdb */
1.22      schwarze  204:                        while (ep > cp && ep[-1] != '/')
                    205:                                ep--;
                    206:                        if (ep == cp)
                    207:                                continue;
                    208:                        *ep = '\0';
                    209:                        /* FALLTHROUGH */
                    210:                case 0:  /* manpath */
1.40    ! schwarze  211:                        manpath_add(&conf->manpath, cp, '\0');
1.25      schwarze  212:                        *manpath_default = '\0';
1.23      schwarze  213:                        break;
                    214:                case 1:  /* output */
1.32      schwarze  215:                        manconf_output(&conf->output, cp, 1);
1.22      schwarze  216:                        break;
                    217:                default:
                    218:                        break;
                    219:                }
1.1       kristaps  220:        }
1.29      schwarze  221:        free(line);
1.25      schwarze  222:        fclose(stream);
1.1       kristaps  223:
1.25      schwarze  224: out:
                    225:        if (*manpath_default != '\0')
1.40    ! schwarze  226:                manpath_parseline(&conf->manpath, manpath_default, '\0');
1.23      schwarze  227: }
                    228:
1.32      schwarze  229: int
                    230: manconf_output(struct manoutput *conf, const char *cp, int fromfile)
1.23      schwarze  231: {
                    232:        const char *const toks[] = {
1.37      schwarze  233:            "includes", "man", "paper", "style", "indent", "width",
                    234:            "tag", "fragment", "mdoc", "noval", "toc"
1.23      schwarze  235:        };
1.38      schwarze  236:        const size_t ntoks = sizeof(toks) / sizeof(toks[0]);
1.23      schwarze  237:
1.32      schwarze  238:        const char      *errstr;
                    239:        char            *oldval;
                    240:        size_t           len, tok;
1.23      schwarze  241:
1.38      schwarze  242:        for (tok = 0; tok < ntoks; tok++) {
1.23      schwarze  243:                len = strlen(toks[tok]);
1.40    ! schwarze  244:                if (strncmp(cp, toks[tok], len) == 0 &&
1.23      schwarze  245:                    strchr(" =  ", cp[len]) != NULL) {
                    246:                        cp += len;
                    247:                        if (*cp == '=')
                    248:                                cp++;
                    249:                        while (isspace((unsigned char)*cp))
                    250:                                cp++;
                    251:                        break;
                    252:                }
                    253:        }
                    254:
1.32      schwarze  255:        if (tok < 6 && *cp == '\0') {
1.40    ! schwarze  256:                mandoc_msg(MANDOCERR_BADVAL_MISS, 0, 0, "-O %s=?", toks[tok]);
1.32      schwarze  257:                return -1;
                    258:        }
1.38      schwarze  259:        if (tok > 6 && tok < ntoks && *cp != '\0') {
1.40    ! schwarze  260:                mandoc_msg(MANDOCERR_BADVAL, 0, 0, "-O %s=%s", toks[tok], cp);
1.32      schwarze  261:                return -1;
                    262:        }
1.23      schwarze  263:
                    264:        switch (tok) {
                    265:        case 0:
1.32      schwarze  266:                if (conf->includes != NULL) {
                    267:                        oldval = mandoc_strdup(conf->includes);
                    268:                        break;
                    269:                }
                    270:                conf->includes = mandoc_strdup(cp);
                    271:                return 0;
1.23      schwarze  272:        case 1:
1.32      schwarze  273:                if (conf->man != NULL) {
                    274:                        oldval = mandoc_strdup(conf->man);
                    275:                        break;
                    276:                }
                    277:                conf->man = mandoc_strdup(cp);
                    278:                return 0;
1.23      schwarze  279:        case 2:
1.32      schwarze  280:                if (conf->paper != NULL) {
                    281:                        oldval = mandoc_strdup(conf->paper);
                    282:                        break;
                    283:                }
                    284:                conf->paper = mandoc_strdup(cp);
                    285:                return 0;
1.23      schwarze  286:        case 3:
1.32      schwarze  287:                if (conf->style != NULL) {
                    288:                        oldval = mandoc_strdup(conf->style);
                    289:                        break;
                    290:                }
                    291:                conf->style = mandoc_strdup(cp);
                    292:                return 0;
1.23      schwarze  293:        case 4:
1.32      schwarze  294:                if (conf->indent) {
                    295:                        mandoc_asprintf(&oldval, "%zu", conf->indent);
                    296:                        break;
                    297:                }
                    298:                conf->indent = strtonum(cp, 0, 1000, &errstr);
                    299:                if (errstr == NULL)
                    300:                        return 0;
1.40    ! schwarze  301:                mandoc_msg(MANDOCERR_BADVAL_BAD, 0, 0,
        !           302:                    "-O indent=%s is %s", cp, errstr);
1.32      schwarze  303:                return -1;
1.23      schwarze  304:        case 5:
1.32      schwarze  305:                if (conf->width) {
                    306:                        mandoc_asprintf(&oldval, "%zu", conf->width);
                    307:                        break;
                    308:                }
1.34      schwarze  309:                conf->width = strtonum(cp, 1, 1000, &errstr);
1.32      schwarze  310:                if (errstr == NULL)
                    311:                        return 0;
1.40    ! schwarze  312:                mandoc_msg(MANDOCERR_BADVAL_BAD, 0, 0,
        !           313:                    "-O width=%s is %s", cp, errstr);
1.32      schwarze  314:                return -1;
1.23      schwarze  315:        case 6:
1.37      schwarze  316:                if (conf->tag != NULL) {
                    317:                        oldval = mandoc_strdup(conf->tag);
                    318:                        break;
                    319:                }
                    320:                conf->tag = mandoc_strdup(cp);
                    321:                return 0;
                    322:        case 7:
1.23      schwarze  323:                conf->fragment = 1;
1.32      schwarze  324:                return 0;
1.37      schwarze  325:        case 8:
1.23      schwarze  326:                conf->mdoc = 1;
1.33      schwarze  327:                return 0;
1.37      schwarze  328:        case 9:
1.33      schwarze  329:                conf->noval = 1;
1.36      schwarze  330:                return 0;
1.37      schwarze  331:        case 10:
1.36      schwarze  332:                conf->toc = 1;
1.32      schwarze  333:                return 0;
1.23      schwarze  334:        default:
1.40    ! schwarze  335:                mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, "-O %s", cp);
        !           336:                return -1;
        !           337:        }
        !           338:        if (fromfile) {
        !           339:                free(oldval);
        !           340:                return 0;
        !           341:        } else {
        !           342:                mandoc_msg(MANDOCERR_BADVAL_DUPE, 0, 0,
        !           343:                    "-O %s=%s: already set to %s", toks[tok], cp, oldval);
        !           344:                free(oldval);
1.32      schwarze  345:                return -1;
1.23      schwarze  346:        }
1.1       kristaps  347: }

CVSweb