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

Annotation of src/usr.bin/man/config.c, Revision 1.9

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