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

Annotation of mandoc/manpath.c, Revision 1.38

1.38    ! schwarze    1: /*     $Id: manpath.c,v 1.37 2018/11/22 11:30:23 schwarze Exp $ */
1.1       kristaps    2: /*
1.36      schwarze    3:  * Copyright (c) 2011,2014,2015,2017,2018 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.28      schwarze   24: #if HAVE_ERR
1.27      schwarze   25: #include <err.h>
1.28      schwarze   26: #endif
1.1       kristaps   27: #include <limits.h>
                     28: #include <stdio.h>
                     29: #include <stdlib.h>
                     30: #include <string.h>
                     31:
1.13      schwarze   32: #include "mandoc_aux.h"
1.23      schwarze   33: #include "manconf.h"
1.1       kristaps   34:
1.23      schwarze   35: static void     manconf_file(struct manconf *, const char *);
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.8       kristaps   44:        char            *insert;
1.4       schwarze   45:
1.8       kristaps   46:        /* Always prepend -m. */
1.23      schwarze   47:        manpath_parseline(&conf->manpath, auxp, 1);
1.10      schwarze   48:
1.8       kristaps   49:        /* If -M is given, it overrides everything else. */
                     50:        if (NULL != defp) {
1.23      schwarze   51:                manpath_parseline(&conf->manpath, defp, 1);
1.8       kristaps   52:                return;
                     53:        }
1.1       kristaps   54:
1.8       kristaps   55:        /* MANPATH and man.conf(5) cooperate. */
                     56:        defp = getenv("MANPATH");
                     57:        if (NULL == file)
                     58:                file = MAN_CONF_FILE;
                     59:
                     60:        /* No MANPATH; use man.conf(5) only. */
                     61:        if (NULL == defp || '\0' == defp[0]) {
1.23      schwarze   62:                manconf_file(conf, file);
1.8       kristaps   63:                return;
                     64:        }
                     65:
                     66:        /* Prepend man.conf(5) to MANPATH. */
                     67:        if (':' == defp[0]) {
1.23      schwarze   68:                manconf_file(conf, file);
                     69:                manpath_parseline(&conf->manpath, defp, 0);
1.8       kristaps   70:                return;
                     71:        }
                     72:
                     73:        /* Append man.conf(5) to MANPATH. */
1.9       kristaps   74:        if (':' == defp[strlen(defp) - 1]) {
1.23      schwarze   75:                manpath_parseline(&conf->manpath, defp, 0);
                     76:                manconf_file(conf, file);
1.8       kristaps   77:                return;
                     78:        }
                     79:
                     80:        /* Insert man.conf(5) into MANPATH. */
                     81:        insert = strstr(defp, "::");
                     82:        if (NULL != insert) {
                     83:                *insert++ = '\0';
1.23      schwarze   84:                manpath_parseline(&conf->manpath, defp, 0);
                     85:                manconf_file(conf, file);
                     86:                manpath_parseline(&conf->manpath, insert + 1, 0);
1.8       kristaps   87:                return;
                     88:        }
                     89:
                     90:        /* MANPATH overrides man.conf(5) completely. */
1.23      schwarze   91:        manpath_parseline(&conf->manpath, defp, 0);
1.35      schwarze   92: }
                     93:
                     94: void
                     95: manpath_base(struct manpaths *dirs)
                     96: {
                     97:        char path_base[] = MANPATH_BASE;
                     98:        manpath_parseline(dirs, path_base, 0);
1.1       kristaps   99: }
                    100:
                    101: /*
                    102:  * Parse a FULL pathname from a colon-separated list of arrays.
                    103:  */
1.6       kristaps  104: static void
1.18      schwarze  105: manpath_parseline(struct manpaths *dirs, char *path, int complain)
1.1       kristaps  106: {
                    107:        char    *dir;
                    108:
                    109:        if (NULL == path)
                    110:                return;
                    111:
                    112:        for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
1.18      schwarze  113:                manpath_add(dirs, dir, complain);
1.1       kristaps  114: }
                    115:
                    116: /*
                    117:  * Add a directory to the array, ignoring bad directories.
                    118:  * Grow the array one-by-one for simplicity's sake.
                    119:  */
                    120: static void
1.18      schwarze  121: manpath_add(struct manpaths *dirs, const char *dir, int complain)
1.1       kristaps  122: {
                    123:        char             buf[PATH_MAX];
1.18      schwarze  124:        struct stat      sb;
1.1       kristaps  125:        char            *cp;
1.9       kristaps  126:        size_t           i;
1.1       kristaps  127:
1.18      schwarze  128:        if (NULL == (cp = realpath(dir, buf))) {
1.27      schwarze  129:                if (complain)
                    130:                        warn("manpath: %s", dir);
1.1       kristaps  131:                return;
1.18      schwarze  132:        }
1.1       kristaps  133:
                    134:        for (i = 0; i < dirs->sz; i++)
                    135:                if (0 == strcmp(dirs->paths[i], dir))
                    136:                        return;
                    137:
1.18      schwarze  138:        if (stat(cp, &sb) == -1) {
1.27      schwarze  139:                if (complain)
                    140:                        warn("manpath: %s", dir);
1.18      schwarze  141:                return;
                    142:        }
                    143:
1.15      schwarze  144:        dirs->paths = mandoc_reallocarray(dirs->paths,
                    145:            dirs->sz + 1, sizeof(char *));
1.1       kristaps  146:
                    147:        dirs->paths[dirs->sz++] = mandoc_strdup(cp);
1.2       kristaps  148: }
                    149:
                    150: void
1.23      schwarze  151: manconf_free(struct manconf *conf)
1.2       kristaps  152: {
1.9       kristaps  153:        size_t           i;
1.2       kristaps  154:
1.23      schwarze  155:        for (i = 0; i < conf->manpath.sz; i++)
                    156:                free(conf->manpath.paths[i]);
1.2       kristaps  157:
1.23      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.2       kristaps  163: }
                    164:
1.23      schwarze  165: static void
                    166: manconf_file(struct manconf *conf, const char *file)
1.2       kristaps  167: {
1.23      schwarze  168:        const char *const toks[] = { "manpath", "output", "_whatdb" };
1.25      schwarze  169:        char manpath_default[] = MANPATH_DEFAULT;
1.22      schwarze  170:
1.2       kristaps  171:        FILE            *stream;
1.29      schwarze  172:        char            *line, *cp, *ep;
                    173:        size_t           linesz, tok, toklen;
                    174:        ssize_t          linelen;
1.1       kristaps  175:
1.22      schwarze  176:        if ((stream = fopen(file, "r")) == NULL)
1.25      schwarze  177:                goto out;
1.1       kristaps  178:
1.29      schwarze  179:        line = NULL;
                    180:        linesz = 0;
                    181:
                    182:        while ((linelen = getline(&line, &linesz, stream)) != -1) {
                    183:                cp = line;
1.30      schwarze  184:                ep = cp + linelen - 1;
                    185:                while (ep > cp && isspace((unsigned char)*ep))
                    186:                        *ep-- = '\0';
1.22      schwarze  187:                while (isspace((unsigned char)*cp))
                    188:                        cp++;
1.30      schwarze  189:                if (cp == ep || *cp == '#')
1.1       kristaps  190:                        continue;
1.22      schwarze  191:
                    192:                for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
1.29      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.22      schwarze  198:                                while (isspace((unsigned char)*cp))
                    199:                                        cp++;
                    200:                                break;
                    201:                        }
                    202:                }
                    203:
                    204:                switch (tok) {
1.23      schwarze  205:                case 2:  /* _whatdb */
1.22      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.23      schwarze  213:                        manpath_add(&conf->manpath, cp, 0);
1.25      schwarze  214:                        *manpath_default = '\0';
1.23      schwarze  215:                        break;
                    216:                case 1:  /* output */
1.32      schwarze  217:                        manconf_output(&conf->output, cp, 1);
1.22      schwarze  218:                        break;
                    219:                default:
                    220:                        break;
                    221:                }
1.1       kristaps  222:        }
1.29      schwarze  223:        free(line);
1.25      schwarze  224:        fclose(stream);
1.1       kristaps  225:
1.25      schwarze  226: out:
                    227:        if (*manpath_default != '\0')
                    228:                manpath_parseline(&conf->manpath, manpath_default, 0);
1.23      schwarze  229: }
                    230:
1.32      schwarze  231: int
                    232: manconf_output(struct manoutput *conf, const char *cp, int fromfile)
1.23      schwarze  233: {
                    234:        const char *const toks[] = {
1.37      schwarze  235:            "includes", "man", "paper", "style", "indent", "width",
                    236:            "tag", "fragment", "mdoc", "noval", "toc"
1.23      schwarze  237:        };
1.38    ! schwarze  238:        const size_t ntoks = sizeof(toks) / sizeof(toks[0]);
1.23      schwarze  239:
1.32      schwarze  240:        const char      *errstr;
                    241:        char            *oldval;
                    242:        size_t           len, tok;
1.23      schwarze  243:
1.38    ! schwarze  244:        for (tok = 0; tok < ntoks; tok++) {
1.23      schwarze  245:                len = strlen(toks[tok]);
                    246:                if ( ! strncmp(cp, toks[tok], len) &&
                    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.32      schwarze  257:        if (tok < 6 && *cp == '\0') {
                    258:                warnx("-O %s=?: Missing argument value", toks[tok]);
                    259:                return -1;
                    260:        }
1.38    ! schwarze  261:        if (tok > 6 && tok < ntoks && *cp != '\0') {
1.32      schwarze  262:                warnx("-O %s: Does not take a value: %s", toks[tok], cp);
                    263:                return -1;
                    264:        }
1.23      schwarze  265:
                    266:        switch (tok) {
                    267:        case 0:
1.32      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.23      schwarze  274:        case 1:
1.32      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.23      schwarze  281:        case 2:
1.32      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.23      schwarze  288:        case 3:
1.32      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.23      schwarze  295:        case 4:
1.32      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;
                    303:                warnx("-O indent=%s is %s", cp, errstr);
                    304:                return -1;
1.23      schwarze  305:        case 5:
1.32      schwarze  306:                if (conf->width) {
                    307:                        mandoc_asprintf(&oldval, "%zu", conf->width);
                    308:                        break;
                    309:                }
1.34      schwarze  310:                conf->width = strtonum(cp, 1, 1000, &errstr);
1.32      schwarze  311:                if (errstr == NULL)
                    312:                        return 0;
                    313:                warnx("-O width=%s is %s", cp, errstr);
                    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.32      schwarze  335:                if (fromfile)
                    336:                        warnx("-O %s: Bad argument", cp);
                    337:                return -1;
1.23      schwarze  338:        }
1.32      schwarze  339:        if (fromfile == 0)
                    340:                warnx("-O %s=%s: Option already set to %s",
                    341:                    toks[tok], cp, oldval);
                    342:        free(oldval);
                    343:        return -1;
1.1       kristaps  344: }

CVSweb