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

Annotation of mandoc/manpath.c, Revision 1.28

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

CVSweb