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

Annotation of src/usr.bin/m4/look.c, Revision 1.15

1.15    ! espie       1: /*     $OpenBSD: look.c,v 1.14 2003/06/30 22:11:38 espie Exp $ */
1.2       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1989, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Ozan Yigit at York University.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
1.11      millert    18:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: #ifndef lint
                     36: static char sccsid[] = "@(#)look.c     8.1 (Berkeley) 6/6/93";
                     37: #endif /* not lint */
                     38:
                     39: /*
                     40:  * look.c
                     41:  * Facility: m4 macro processor
                     42:  * by: oz
                     43:  */
                     44:
                     45: #include <sys/types.h>
                     46: #include <stdio.h>
                     47: #include <stdlib.h>
1.3       espie      48: #include <stddef.h>
1.1       deraadt    49: #include <string.h>
1.13      espie      50: #include <ohash.h>
1.1       deraadt    51: #include "mdef.h"
                     52: #include "stdd.h"
                     53: #include "extern.h"
                     54:
1.15    ! espie      55: static void *hash_alloc(size_t, void *);
        !            56: static void hash_free(void *, size_t, void *);
        !            57: static void *element_alloc(size_t, void *);
1.13      espie      58: static void setup_definition(struct macro_definition *, const char *,
                     59:     const char *);
                     60:
                     61: static struct ohash_info macro_info = {
                     62:        offsetof(struct ndblock, name),
                     63:        NULL, hash_alloc, hash_free, element_alloc };
                     64:
1.15    ! espie      65: struct ohash macros;
        !            66:
        !            67: /* Support routines for hash tables.  */
        !            68: void *
        !            69: hash_alloc(s, u)
        !            70:        size_t s;
        !            71:        void *u         UNUSED;
        !            72: {
        !            73:        void *storage = xalloc(s);
        !            74:        if (storage)
        !            75:                memset(storage, 0, s);
        !            76:        return storage;
        !            77: }
        !            78:
        !            79: void
        !            80: hash_free(p, s, u)
        !            81:        void *p;
        !            82:        size_t s        UNUSED;
        !            83:        void *u         UNUSED;
        !            84: {
        !            85:        free(p);
        !            86: }
        !            87:
        !            88: void *
        !            89: element_alloc(s, u)
        !            90:        size_t s;
        !            91:        void *u         UNUSED;
        !            92: {
        !            93:        return xalloc(s);
        !            94: }
1.13      espie      95:
                     96: void
                     97: init_macros()
                     98: {
1.15    ! espie      99:        ohash_init(&macros, 10, &macro_info);
1.1       deraadt   100: }
                    101:
                    102: /*
                    103:  * find name in the hash table
                    104:  */
                    105: ndptr
1.10      espie     106: lookup(const char *name)
1.1       deraadt   107: {
1.13      espie     108:        return ohash_find(&macros, ohash_qlookup(&macros, name));
1.12      espie     109: }
                    110:
                    111: struct macro_definition *
                    112: lookup_macro_definition(const char *name)
                    113: {
                    114:        ndptr p;
                    115:
1.15    ! espie     116:        p = ohash_find(&macros, ohash_qlookup(&macros, name));
1.12      espie     117:        if (p)
1.13      espie     118:                return p->d;
1.12      espie     119:        else
                    120:                return NULL;
                    121: }
                    122:
                    123: static void
1.13      espie     124: setup_definition(struct macro_definition *d, const char *defn, const char *name)
1.12      espie     125: {
1.14      espie     126:        ndptr p;
1.12      espie     127:
1.13      espie     128:        if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER)-1) == 0 &&
1.14      espie     129:            (p = macro_getbuiltin(defn+sizeof(BUILTIN_MARKER)-1)) != NULL) {
                    130:                d->type = macro_builtin_type(p);
1.13      espie     131:                d->defn = xstrdup(defn+sizeof(BUILTIN_MARKER)-1);
                    132:        } else {
                    133:                if (!*defn)
                    134:                        d->defn = null;
                    135:                else
                    136:                        d->defn = xstrdup(defn);
                    137:                d->type = MACRTYPE;
                    138:        }
                    139:        if (STREQ(name, defn))
                    140:                d->type |= RECDEF;
                    141: }
                    142:
                    143: static ndptr
                    144: create_entry(const char *name)
                    145: {
                    146:        const char *end = NULL;
                    147:        unsigned int i;
                    148:        ndptr n;
                    149:
                    150:        i = ohash_qlookupi(&macros, name, &end);
                    151:        n = ohash_find(&macros, i);
                    152:        if (n == NULL) {
                    153:                n = ohash_create_entry(&macro_info, name, &end);
                    154:                ohash_insert(&macros, i, n);
1.15    ! espie     155:                n->trace_flags = FLAG_NO_TRACE;
1.14      espie     156:                n->builtin_type = MACRTYPE;
1.13      espie     157:                n->d = NULL;
1.12      espie     158:        }
1.13      espie     159:        return n;
1.12      espie     160: }
                    161:
                    162: void
                    163: macro_define(const char *name, const char *defn)
                    164: {
1.13      espie     165:        ndptr n = create_entry(name);
                    166:        if (n->d != NULL) {
                    167:                if (n->d->defn != null)
                    168:                        free(n->d->defn);
                    169:        } else {
                    170:                n->d = xalloc(sizeof(struct macro_definition));
                    171:                n->d->next = NULL;
                    172:        }
                    173:        setup_definition(n->d, defn, name);
1.12      espie     174: }
                    175:
                    176: void
                    177: macro_pushdef(const char *name, const char *defn)
                    178: {
1.13      espie     179:        ndptr n;
                    180:        struct macro_definition *d;
                    181:
                    182:        n = create_entry(name);
                    183:        d = xalloc(sizeof(struct macro_definition));
                    184:        d->next = n->d;
                    185:        n->d = d;
                    186:        setup_definition(n->d, defn, name);
1.12      espie     187: }
                    188:
                    189: void
                    190: macro_undefine(const char *name)
                    191: {
1.13      espie     192:        ndptr n = lookup(name);
                    193:        if (n != NULL) {
                    194:                struct macro_definition *r, *r2;
                    195:
                    196:                for (r = n->d; r != NULL; r = r2) {
                    197:                        r2 = r->next;
                    198:                        if (r->defn != null)
                    199:                                free(r->defn);
                    200:                        free(r);
                    201:                }
                    202:                n->d = NULL;
                    203:        }
1.12      espie     204: }
                    205:
                    206: void
                    207: macro_popdef(const char *name)
                    208: {
1.13      espie     209:        ndptr n = lookup(name);
                    210:
                    211:        if (n != NULL) {
                    212:                struct macro_definition *r = n->d;
                    213:                if (r != NULL) {
                    214:                        n->d = r->next;
                    215:                        if (r->defn != null)
                    216:                                free(r->defn);
                    217:                        free(r);
                    218:                }
                    219:        }
1.12      espie     220: }
                    221:
                    222: void
                    223: macro_for_all(void (*f)(const char *, struct macro_definition *))
                    224: {
1.13      espie     225:        ndptr n;
                    226:        unsigned int i;
1.12      espie     227:
1.13      espie     228:        for (n = ohash_first(&macros, &i); n != NULL;
                    229:            n = ohash_next(&macros, &i))
                    230:                f(n->name, n->d);
1.12      espie     231: }
                    232:
                    233: void
                    234: setup_builtin(const char *name, unsigned int type)
                    235: {
1.13      espie     236:        ndptr n;
1.12      espie     237:
1.13      espie     238:        n = create_entry(name);
1.14      espie     239:        n->builtin_type = type;
1.13      espie     240:        n->d = xalloc(sizeof(struct macro_definition));
                    241:        n->d->defn = xstrdup(name);
                    242:        n->d->type = type;
                    243:        n->d->next = NULL;
1.12      espie     244: }
                    245:
1.15    ! espie     246: void
        !           247: mark_traced(const char *name, int on)
1.12      espie     248: {
1.15    ! espie     249:        ndptr p;
        !           250:        unsigned int i;
1.12      espie     251:
1.15    ! espie     252:        if (name == NULL) {
        !           253:                if (on)
        !           254:                        trace_flags |= TRACE_ALL;
        !           255:                else
        !           256:                        trace_flags &= ~TRACE_ALL;
        !           257:                for (p = ohash_first(&macros, &i); p != NULL;
        !           258:                    p = ohash_next(&macros, &i))
        !           259:                        p->trace_flags = FLAG_NO_TRACE;
        !           260:        } else {
        !           261:                p = create_entry(name);
        !           262:                p->trace_flags = on;
        !           263:        }
1.1       deraadt   264: }
1.14      espie     265:
                    266: ndptr
                    267: macro_getbuiltin(const char *name)
                    268: {
                    269:        ndptr p;
                    270:
                    271:        p = lookup(name);
                    272:        if (p == NULL || p->builtin_type == MACRTYPE)
                    273:                return NULL;
                    274:        else
                    275:                return p;
                    276: }
                    277: