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

Annotation of src/usr.bin/mandoc/man_conf.c, Revision 1.1

1.1     ! schwarze    1: /*      $Id$ */
        !             2: /*
        !             3:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
        !             4:  *
        !             5:  * Permission to use, copy, modify, and distribute this software for any
        !             6:  * purpose with or without fee is hereby granted, provided that the above
        !             7:  * copyright notice and this permission notice appear in all copies.
        !             8:  *
        !             9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !            10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            16:  */
        !            17:
        !            18: #include <sys/types.h>
        !            19: #include <assert.h>
        !            20: #include <ctype.h>
        !            21: #include <stdio.h>
        !            22: #include <stdlib.h>
        !            23: #include <string.h>
        !            24:
        !            25: #include "mandoc.h"
        !            26: #include "man_conf.h"
        !            27:
        !            28: #define        MAN_CONF_FILE   "/etc/man.conf"
        !            29:
        !            30: static void add_dir(struct man_conf *, const char *);
        !            31:
        !            32: static void
        !            33: add_dir(struct man_conf *dirs, const char *dir) {
        !            34:
        !            35:        if (dirs->maxargs == dirs->argc) {
        !            36:                dirs->maxargs += 8;
        !            37:                assert(0 < dirs->maxargs);
        !            38:                dirs->argv = mandoc_realloc(dirs->argv,
        !            39:                                (size_t)dirs->maxargs);
        !            40:        }
        !            41:        dirs->argv[dirs->argc++] = mandoc_strdup(dir);
        !            42: }
        !            43:
        !            44: void
        !            45: manpath_parse(struct man_conf *dirs, char *path) {
        !            46:        char    *dir;
        !            47:
        !            48:        for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
        !            49:                add_dir(dirs, dir);
        !            50: }
        !            51:
        !            52: void
        !            53: man_conf_parse(struct man_conf *dirs) {
        !            54:        FILE    *stream;
        !            55:        char    *p, *q;
        !            56:        size_t   len;
        !            57:
        !            58:        if (NULL == (stream = fopen(MAN_CONF_FILE, "r")))
        !            59:                return;
        !            60:
        !            61:        while (NULL != (p = fgetln(stream, &len)) && '\n' == p[--len]) {
        !            62:                p[len] = '\0';
        !            63:                while (isspace(*p))
        !            64:                        p++;
        !            65:                if (strncmp("_whatdb", p, 7))
        !            66:                        continue;
        !            67:                p += 7;
        !            68:                while (isspace(*p))
        !            69:                        p++;
        !            70:                if ('\0' == *p)
        !            71:                        continue;
        !            72:                if (NULL == (q = strrchr(p, '/')))
        !            73:                        continue;
        !            74:                *q = '\0';
        !            75:                add_dir(dirs, p);
        !            76:        }
        !            77:
        !            78:        fclose(stream);
        !            79: }
        !            80:
        !            81: void
        !            82: man_conf_free(struct man_conf *dirs) {
        !            83:
        !            84:        while (dirs->argc--)
        !            85:                free(dirs->argv[dirs->argc]);
        !            86:        free(dirs->argv);
        !            87:        dirs->maxargs = 0;
        !            88: }