[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.17

1.17    ! schwarze    1: /*     $OpenBSD: manpath.c,v 1.16 2015/10/11 21:06:59 schwarze Exp $   */
1.1       schwarze    2: /*
1.12      schwarze    3:  * Copyright (c) 2011, 2014, 2015 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.16      schwarze   22: #include <err.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.14      schwarze   29: #include "manconf.h"
1.1       schwarze   30:
                     31: #define MAN_CONF_FILE  "/etc/man.conf"
1.12      schwarze   32: #define MANPATH_DEFAULT        "/usr/share/man:/usr/X11R6/man:/usr/local/man"
1.1       schwarze   33:
1.14      schwarze   34: static void     manconf_file(struct manconf *, const char *);
1.11      schwarze   35: static void     manpath_add(struct manpaths *, const char *, int);
                     36: static void     manpath_parseline(struct manpaths *, char *, int);
1.1       schwarze   37:
1.14      schwarze   38:
1.1       schwarze   39: void
1.14      schwarze   40: manconf_parse(struct manconf *conf, const char *file,
1.2       schwarze   41:                char *defp, char *auxp)
1.1       schwarze   42: {
1.4       schwarze   43:        char            *insert;
1.1       schwarze   44:
1.4       schwarze   45:        /* Always prepend -m. */
1.14      schwarze   46:        manpath_parseline(&conf->manpath, auxp, 1);
1.1       schwarze   47:
1.4       schwarze   48:        /* If -M is given, it overrides everything else. */
                     49:        if (NULL != defp) {
1.14      schwarze   50:                manpath_parseline(&conf->manpath, defp, 1);
1.4       schwarze   51:                return;
                     52:        }
                     53:
                     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.14      schwarze   61:                manconf_file(conf, file);
1.4       schwarze   62:                return;
                     63:        }
                     64:
                     65:        /* Prepend man.conf(5) to MANPATH. */
                     66:        if (':' == defp[0]) {
1.14      schwarze   67:                manconf_file(conf, file);
                     68:                manpath_parseline(&conf->manpath, defp, 0);
1.4       schwarze   69:                return;
                     70:        }
1.1       schwarze   71:
1.4       schwarze   72:        /* Append man.conf(5) to MANPATH. */
1.6       schwarze   73:        if (':' == defp[strlen(defp) - 1]) {
1.14      schwarze   74:                manpath_parseline(&conf->manpath, defp, 0);
                     75:                manconf_file(conf, file);
1.4       schwarze   76:                return;
                     77:        }
                     78:
                     79:        /* Insert man.conf(5) into MANPATH. */
                     80:        insert = strstr(defp, "::");
                     81:        if (NULL != insert) {
                     82:                *insert++ = '\0';
1.14      schwarze   83:                manpath_parseline(&conf->manpath, defp, 0);
                     84:                manconf_file(conf, file);
                     85:                manpath_parseline(&conf->manpath, insert + 1, 0);
1.4       schwarze   86:                return;
                     87:        }
                     88:
                     89:        /* MANPATH overrides man.conf(5) completely. */
1.14      schwarze   90:        manpath_parseline(&conf->manpath, defp, 0);
1.1       schwarze   91: }
                     92:
                     93: /*
                     94:  * Parse a FULL pathname from a colon-separated list of arrays.
                     95:  */
1.3       schwarze   96: static void
1.11      schwarze   97: manpath_parseline(struct manpaths *dirs, char *path, int complain)
1.1       schwarze   98: {
                     99:        char    *dir;
                    100:
                    101:        if (NULL == path)
                    102:                return;
                    103:
                    104:        for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
1.11      schwarze  105:                manpath_add(dirs, dir, complain);
1.1       schwarze  106: }
                    107:
                    108: /*
                    109:  * Add a directory to the array, ignoring bad directories.
                    110:  * Grow the array one-by-one for simplicity's sake.
                    111:  */
                    112: static void
1.11      schwarze  113: manpath_add(struct manpaths *dirs, const char *dir, int complain)
1.1       schwarze  114: {
                    115:        char             buf[PATH_MAX];
1.11      schwarze  116:        struct stat      sb;
1.1       schwarze  117:        char            *cp;
1.6       schwarze  118:        size_t           i;
1.1       schwarze  119:
1.11      schwarze  120:        if (NULL == (cp = realpath(dir, buf))) {
1.16      schwarze  121:                if (complain)
                    122:                        warn("manpath: %s", dir);
1.1       schwarze  123:                return;
1.11      schwarze  124:        }
1.1       schwarze  125:
                    126:        for (i = 0; i < dirs->sz; i++)
                    127:                if (0 == strcmp(dirs->paths[i], dir))
                    128:                        return;
                    129:
1.11      schwarze  130:        if (stat(cp, &sb) == -1) {
1.16      schwarze  131:                if (complain)
                    132:                        warn("manpath: %s", dir);
1.11      schwarze  133:                return;
                    134:        }
                    135:
1.10      schwarze  136:        dirs->paths = mandoc_reallocarray(dirs->paths,
                    137:            dirs->sz + 1, sizeof(char *));
1.1       schwarze  138:
                    139:        dirs->paths[dirs->sz++] = mandoc_strdup(cp);
                    140: }
                    141:
                    142: void
1.14      schwarze  143: manconf_free(struct manconf *conf)
1.1       schwarze  144: {
1.6       schwarze  145:        size_t           i;
1.1       schwarze  146:
1.14      schwarze  147:        for (i = 0; i < conf->manpath.sz; i++)
                    148:                free(conf->manpath.paths[i]);
1.1       schwarze  149:
1.14      schwarze  150:        free(conf->manpath.paths);
                    151:        free(conf->output.includes);
                    152:        free(conf->output.man);
                    153:        free(conf->output.paper);
                    154:        free(conf->output.style);
1.1       schwarze  155: }
                    156:
1.14      schwarze  157: static void
                    158: manconf_file(struct manconf *conf, const char *file)
1.1       schwarze  159: {
1.14      schwarze  160:        const char *const toks[] = { "manpath", "output", "_whatdb" };
1.15      schwarze  161:        char manpath_default[] = MANPATH_DEFAULT;
1.13      schwarze  162:
1.1       schwarze  163:        FILE            *stream;
1.17    ! schwarze  164:        char            *line, *cp, *ep;
        !           165:        size_t           linesz, tok, toklen;
        !           166:        ssize_t          linelen;
1.1       schwarze  167:
1.13      schwarze  168:        if ((stream = fopen(file, "r")) == NULL)
1.15      schwarze  169:                goto out;
1.1       schwarze  170:
1.17    ! schwarze  171:        line = NULL;
        !           172:        linesz = 0;
        !           173:
        !           174:        while ((linelen = getline(&line, &linesz, stream)) != -1) {
        !           175:                cp = line;
        !           176:                ep = cp + linelen;
1.13      schwarze  177:                if (ep[-1] != '\n')
1.1       schwarze  178:                        break;
1.13      schwarze  179:                *--ep = '\0';
                    180:                while (isspace((unsigned char)*cp))
                    181:                        cp++;
                    182:                if (*cp == '#')
1.1       schwarze  183:                        continue;
1.13      schwarze  184:
                    185:                for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
1.17    ! schwarze  186:                        toklen = strlen(toks[tok]);
        !           187:                        if (cp + toklen < ep &&
        !           188:                            isspace((unsigned char)cp[toklen]) &&
        !           189:                            strncmp(cp, toks[tok], toklen) == 0) {
        !           190:                                cp += toklen;
1.13      schwarze  191:                                while (isspace((unsigned char)*cp))
                    192:                                        cp++;
                    193:                                break;
                    194:                        }
                    195:                }
                    196:
                    197:                switch (tok) {
1.14      schwarze  198:                case 2:  /* _whatdb */
1.13      schwarze  199:                        while (ep > cp && ep[-1] != '/')
                    200:                                ep--;
                    201:                        if (ep == cp)
                    202:                                continue;
                    203:                        *ep = '\0';
                    204:                        /* FALLTHROUGH */
                    205:                case 0:  /* manpath */
1.14      schwarze  206:                        manpath_add(&conf->manpath, cp, 0);
1.15      schwarze  207:                        *manpath_default = '\0';
1.14      schwarze  208:                        break;
                    209:                case 1:  /* output */
                    210:                        manconf_output(&conf->output, cp);
1.13      schwarze  211:                        break;
                    212:                default:
                    213:                        break;
                    214:                }
1.1       schwarze  215:        }
1.17    ! schwarze  216:        free(line);
1.15      schwarze  217:        fclose(stream);
1.1       schwarze  218:
1.15      schwarze  219: out:
                    220:        if (*manpath_default != '\0')
                    221:                manpath_parseline(&conf->manpath, manpath_default, 0);
1.14      schwarze  222: }
                    223:
                    224: void
                    225: manconf_output(struct manoutput *conf, const char *cp)
                    226: {
                    227:        const char *const toks[] = {
                    228:            "includes", "man", "paper", "style",
                    229:            "indent", "width", "fragment", "mdoc"
                    230:        };
                    231:
                    232:        size_t   len, tok;
                    233:
                    234:        for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
                    235:                len = strlen(toks[tok]);
                    236:                if ( ! strncmp(cp, toks[tok], len) &&
                    237:                    strchr(" =  ", cp[len]) != NULL) {
                    238:                        cp += len;
                    239:                        if (*cp == '=')
                    240:                                cp++;
                    241:                        while (isspace((unsigned char)*cp))
                    242:                                cp++;
                    243:                        break;
                    244:                }
                    245:        }
                    246:
                    247:        if (tok < 6 && *cp == '\0')
                    248:                return;
                    249:
                    250:        switch (tok) {
                    251:        case 0:
                    252:                if (conf->includes == NULL)
                    253:                        conf->includes = mandoc_strdup(cp);
                    254:                break;
                    255:        case 1:
                    256:                if (conf->man == NULL)
                    257:                        conf->man = mandoc_strdup(cp);
                    258:                break;
                    259:        case 2:
                    260:                if (conf->paper == NULL)
                    261:                        conf->paper = mandoc_strdup(cp);
                    262:                break;
                    263:        case 3:
                    264:                if (conf->style == NULL)
                    265:                        conf->style = mandoc_strdup(cp);
                    266:                break;
                    267:        case 4:
                    268:                if (conf->indent == 0)
                    269:                        conf->indent = strtonum(cp, 0, 1000, NULL);
                    270:                break;
                    271:        case 5:
                    272:                if (conf->width == 0)
                    273:                        conf->width = strtonum(cp, 58, 1000, NULL);
                    274:                break;
                    275:        case 6:
                    276:                conf->fragment = 1;
                    277:                break;
                    278:        case 7:
                    279:                conf->mdoc = 1;
                    280:                break;
                    281:        default:
                    282:                break;
                    283:        }
1.1       schwarze  284: }