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

1.1     ! schwarze    1: /*     $Id: manpath.c,v 1.1 2011/11/17 14:52:32 schwarze Exp $ */
        !             2: /*
        !             3:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
        !             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:  *
        !            10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !            11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            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:
        !            19: #include <sys/types.h>
        !            20: #include <assert.h>
        !            21: #include <ctype.h>
        !            22: #include <limits.h>
        !            23: #include <stdio.h>
        !            24: #include <stdlib.h>
        !            25: #include <string.h>
        !            26:
        !            27: #include "mandoc.h"
        !            28: #include "manpath.h"
        !            29:
        !            30: #define MAN_CONF_FILE  "/etc/man.conf"
        !            31: #define MAN_CONF_KEY   "_whatdb"
        !            32:
        !            33: static void     manpath_add(struct manpaths *, const char *);
        !            34:
        !            35: void
        !            36: manpath_parse(struct manpaths *dirs, char *defp, char *auxp)
        !            37: {
        !            38:
        !            39:        manpath_parseline(dirs, auxp);
        !            40:
        !            41:        if (NULL == defp)
        !            42:                defp = getenv("MANPATH");
        !            43:
        !            44:        if (NULL == defp)
        !            45:                manpath_parseconf(dirs);
        !            46:        else
        !            47:                manpath_parseline(dirs, defp);
        !            48: }
        !            49:
        !            50: /*
        !            51:  * Parse a FULL pathname from a colon-separated list of arrays.
        !            52:  */
        !            53: void
        !            54: manpath_parseline(struct manpaths *dirs, char *path)
        !            55: {
        !            56:        char    *dir;
        !            57:
        !            58:        if (NULL == path)
        !            59:                return;
        !            60:
        !            61:        for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
        !            62:                manpath_add(dirs, dir);
        !            63: }
        !            64:
        !            65: /*
        !            66:  * Add a directory to the array, ignoring bad directories.
        !            67:  * Grow the array one-by-one for simplicity's sake.
        !            68:  */
        !            69: static void
        !            70: manpath_add(struct manpaths *dirs, const char *dir)
        !            71: {
        !            72:        char             buf[PATH_MAX];
        !            73:        char            *cp;
        !            74:        int              i;
        !            75:
        !            76:        if (NULL == (cp = realpath(dir, buf)))
        !            77:                return;
        !            78:
        !            79:        for (i = 0; i < dirs->sz; i++)
        !            80:                if (0 == strcmp(dirs->paths[i], dir))
        !            81:                        return;
        !            82:
        !            83:        dirs->paths = mandoc_realloc
        !            84:                (dirs->paths,
        !            85:                 ((size_t)dirs->sz + 1) * sizeof(char *));
        !            86:
        !            87:        dirs->paths[dirs->sz++] = mandoc_strdup(cp);
        !            88: }
        !            89:
        !            90: void
        !            91: manpath_parseconf(struct manpaths *dirs)
        !            92: {
        !            93:
        !            94:        manpath_manconf(MAN_CONF_FILE, dirs);
        !            95: }
        !            96:
        !            97: void
        !            98: manpath_free(struct manpaths *p)
        !            99: {
        !           100:        int              i;
        !           101:
        !           102:        for (i = 0; i < p->sz; i++)
        !           103:                free(p->paths[i]);
        !           104:
        !           105:        free(p->paths);
        !           106: }
        !           107:
        !           108: void
        !           109: manpath_manconf(const char *file, struct manpaths *dirs)
        !           110: {
        !           111:        FILE            *stream;
        !           112:        char            *p, *q;
        !           113:        size_t           len, keysz;
        !           114:
        !           115:        keysz = strlen(MAN_CONF_KEY);
        !           116:        assert(keysz > 0);
        !           117:
        !           118:        if (NULL == (stream = fopen(file, "r")))
        !           119:                return;
        !           120:
        !           121:        while (NULL != (p = fgetln(stream, &len))) {
        !           122:                if (0 == len || '\n' != p[--len])
        !           123:                        break;
        !           124:                p[len] = '\0';
        !           125:                while (isspace((unsigned char)*p))
        !           126:                        p++;
        !           127:                if (strncmp(MAN_CONF_KEY, p, keysz))
        !           128:                        continue;
        !           129:                p += keysz;
        !           130:                while (isspace(*p))
        !           131:                        p++;
        !           132:                if ('\0' == *p)
        !           133:                        continue;
        !           134:                if (NULL == (q = strrchr(p, '/')))
        !           135:                        continue;
        !           136:                *q = '\0';
        !           137:                manpath_add(dirs, p);
        !           138:        }
        !           139:
        !           140:        fclose(stream);
        !           141: }