[BACK]Return to manpath.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/manpath.c, Revision 1.27

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