Annotation of src/usr.bin/man/config.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: config.c,v 1.7 1995/09/28 06:05:21 tls Exp $ */
! 2:
! 3: /*
! 4: * Copyright (c) 1989, 1993, 1995
! 5: * The Regents of the University of California. All rights reserved.
! 6: *
! 7: * Redistribution and use in source and binary forms, with or without
! 8: * modification, are permitted provided that the following conditions
! 9: * are met:
! 10: * 1. Redistributions of source code must retain the above copyright
! 11: * notice, this list of conditions and the following disclaimer.
! 12: * 2. Redistributions in binary form must reproduce the above copyright
! 13: * notice, this list of conditions and the following disclaimer in the
! 14: * documentation and/or other materials provided with the distribution.
! 15: * 3. All advertising materials mentioning features or use of this software
! 16: * must display the following acknowledgement:
! 17: * This product includes software developed by the University of
! 18: * California, Berkeley and its contributors.
! 19: * 4. Neither the name of the University nor the names of its contributors
! 20: * may be used to endorse or promote products derived from this software
! 21: * without specific prior written permission.
! 22: *
! 23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 33: * SUCH DAMAGE.
! 34: */
! 35:
! 36: #ifndef lint
! 37: #if 0
! 38: static char sccsid[] = "@(#)config.c 8.8 (Berkeley) 1/31/95";
! 39: #else
! 40: static char rcsid[] = "$NetBSD: config.c,v 1.7 1995/09/28 06:05:21 tls Exp $";
! 41: #endif
! 42: #endif /* not lint */
! 43:
! 44: #include <sys/types.h>
! 45: #include <sys/queue.h>
! 46:
! 47: #include <ctype.h>
! 48: #include <err.h>
! 49: #include <errno.h>
! 50: #include <stdio.h>
! 51: #include <stdlib.h>
! 52: #include <string.h>
! 53:
! 54: #include "config.h"
! 55: #include "pathnames.h"
! 56:
! 57: struct _head head;
! 58:
! 59: /*
! 60: * config --
! 61: *
! 62: * Read the configuration file and build a doubly linked
! 63: * list that looks like:
! 64: *
! 65: * tag1 <-> record <-> record <-> record
! 66: * |
! 67: * tag2 <-> record <-> record <-> record
! 68: */
! 69: void
! 70: config(fname)
! 71: char *fname;
! 72: {
! 73: TAG *tp;
! 74: ENTRY *ep;
! 75: FILE *cfp;
! 76: size_t len;
! 77: int lcnt;
! 78: char *p, *t;
! 79:
! 80: if (fname == NULL)
! 81: fname = _PATH_MANCONF;
! 82: if ((cfp = fopen(fname, "r")) == NULL)
! 83: err(1, "%s", fname);
! 84: TAILQ_INIT(&head);
! 85: for (lcnt = 1; (p = fgetln(cfp, &len)) != NULL; ++lcnt) {
! 86: if (len == 1) /* Skip empty lines. */
! 87: continue;
! 88: if (p[len - 1] != '\n') { /* Skip corrupted lines. */
! 89: warnx("%s: line %d corrupted", fname, lcnt);
! 90: continue;
! 91: }
! 92: p[len - 1] = '\0'; /* Terminate the line. */
! 93:
! 94: /* Skip leading space. */
! 95: for (; *p != '\0' && isspace(*p); ++p);
! 96: /* Skip empty/comment lines. */
! 97: if (*p == '\0' || *p == '#')
! 98: continue;
! 99: /* Find first token. */
! 100: for (t = p; *t && !isspace(*t); ++t);
! 101: if (*t == '\0') /* Need more than one token.*/
! 102: continue;
! 103: *t = '\0';
! 104:
! 105: for (tp = head.tqh_first; /* Find any matching tag. */
! 106: tp != NULL && strcmp(p, tp->s); tp = tp->q.tqe_next);
! 107:
! 108: if (tp == NULL) /* Create a new tag. */
! 109: tp = addlist(p);
! 110:
! 111: /*
! 112: * Attach new records. The keyword _build takes the rest of
! 113: * the line as a single entity, everything else is white
! 114: * space separated. The reason we're not just using strtok(3)
! 115: * for all of the parsing is so we don't get caught if a line
! 116: * has only a single token on it.
! 117: */
! 118: if (!strcmp(p, "_build")) {
! 119: while (*++t && isspace(*t));
! 120: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
! 121: (ep->s = strdup(t)) == NULL)
! 122: err(1, NULL);
! 123: TAILQ_INSERT_TAIL(&tp->list, ep, q);
! 124: } else for (++t; (p = strtok(t, " \t\n")) != NULL; t = NULL) {
! 125: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
! 126: (ep->s = strdup(p)) == NULL)
! 127: err(1, NULL);
! 128: TAILQ_INSERT_TAIL(&tp->list, ep, q);
! 129: }
! 130: }
! 131:
! 132: fclose(cfp);
! 133: }
! 134:
! 135: /*
! 136: * addlist --
! 137: * Add a tag to the list.
! 138: */
! 139: TAG *
! 140: addlist(name)
! 141: char *name;
! 142: {
! 143: TAG *tp;
! 144:
! 145: if ((tp = calloc(1, sizeof(TAG))) == NULL ||
! 146: (tp->s = strdup(name)) == NULL)
! 147: err(1, NULL);
! 148: TAILQ_INIT(&tp->list);
! 149: TAILQ_INSERT_TAIL(&head, tp, q);
! 150: return (tp);
! 151: }
! 152:
! 153: /*
! 154: * getlist --
! 155: * Return the linked list of entries for a tag if it exists.
! 156: */
! 157: TAG *
! 158: getlist(name)
! 159: char *name;
! 160: {
! 161: TAG *tp;
! 162:
! 163: for (tp = head.tqh_first; tp != NULL; tp = tp->q.tqe_next)
! 164: if (!strcmp(name, tp->s))
! 165: return (tp);
! 166: return (NULL);
! 167: }
! 168:
! 169: void
! 170: debug(l)
! 171: char *l;
! 172: {
! 173: TAG *tp;
! 174: ENTRY *ep;
! 175:
! 176: (void)printf("%s ===============\n", l);
! 177: for (tp = head.tqh_first; tp != NULL; tp = tp->q.tqe_next) {
! 178: printf("%s\n", tp->s);
! 179: for (ep = tp->list.tqh_first; ep != NULL; ep = ep->q.tqe_next)
! 180: printf("\t%s\n", ep->s);
! 181: }
! 182: }