1 /* $OpenBSD: look.c,v 1.18 2006/01/20 23:10:19 espie Exp $ */ 2 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. 18 * 3. Neither the name of the University nor the names of its contributors 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 /* 36 * look.c 37 * Facility: m4 macro processor 38 * by: oz 39 */ 40 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <stdint.h> 44 #include <stddef.h> 45 #include <string.h> 46 #include <ohash.h> 47 #include "mdef.h" 48 #include "stdd.h" 49 #include "extern.h" 50 51 static void *hash_alloc(size_t, void *); 52 static void hash_free(void *, size_t, void *); 53 static void *element_alloc(size_t, void *); 54 static void setup_definition(struct macro_definition *, const char *, 55 const char *); 56 57 static struct ohash_info macro_info = { 58 offsetof(struct ndblock, name), 59 NULL, hash_alloc, hash_free, element_alloc }; 60 61 struct ohash macros; 62 63 /* Support routines for hash tables. */ 64 void * 65 hash_alloc(s, u) 66 size_t s; 67 void *u UNUSED; 68 { 69 void *storage = xalloc(s, "hash alloc"); 70 if (storage) 71 memset(storage, 0, s); 72 return storage; 73 } 74 75 void 76 hash_free(p, s, u) 77 void *p; 78 size_t s UNUSED; 79 void *u UNUSED; 80 { 81 free(p); 82 } 83 84 void * 85 element_alloc(s, u) 86 size_t s; 87 void *u UNUSED; 88 { 89 return xalloc(s, "element alloc"); 90 } 91 92 void 93 init_macros() 94 { 95 ohash_init(¯os, 10, ¯o_info); 96 } 97 98 /* 99 * find name in the hash table 100 */ 101 ndptr 102 lookup(const char *name) 103 { 104 return ohash_find(¯os, ohash_qlookup(¯os, name)); 105 } 106 107 struct macro_definition * 108 lookup_macro_definition(const char *name) 109 { 110 ndptr p; 111 112 p = ohash_find(¯os, ohash_qlookup(¯os, name)); 113 if (p) 114 return p->d; 115 else 116 return NULL; 117 } 118 119 static void 120 setup_definition(struct macro_definition *d, const char *defn, const char *name) 121 { 122 ndptr p; 123 124 if (strncmp(defn, BUILTIN_MARKER, sizeof(BUILTIN_MARKER)-1) == 0 && 125 (p = macro_getbuiltin(defn+sizeof(BUILTIN_MARKER)-1)) != NULL) { 126 d->type = macro_builtin_type(p); 127 d->defn = xstrdup(defn+sizeof(BUILTIN_MARKER)-1); 128 } else { 129 if (!*defn) 130 d->defn = null; 131 else 132 d->defn = xstrdup(defn); 133 d->type = MACRTYPE; 134 } 135 if (STREQ(name, defn)) 136 d->type |= RECDEF; 137 } 138 139 static ndptr 140 create_entry(const char *name) 141 { 142 const char *end = NULL; 143 unsigned int i; 144 ndptr n; 145 146 i = ohash_qlookupi(¯os, name, &end); 147 n = ohash_find(¯os, i); 148 if (n == NULL) { 149 n = ohash_create_entry(¯o_info, name, &end); 150 ohash_insert(¯os, i, n); 151 n->trace_flags = FLAG_NO_TRACE; 152 n->builtin_type = MACRTYPE; 153 n->d = NULL; 154 } 155 return n; 156 } 157 158 void 159 macro_define(const char *name, const char *defn) 160 { 161 ndptr n = create_entry(name); 162 if (n->d != NULL) { 163 if (n->d->defn != null) 164 free(n->d->defn); 165 } else { 166 n->d = xalloc(sizeof(struct macro_definition), NULL); 167 n->d->next = NULL; 168 } 169 setup_definition(n->d, defn, name); 170 } 171 172 void 173 macro_pushdef(const char *name, const char *defn) 174 { 175 ndptr n; 176 struct macro_definition *d; 177 178 n = create_entry(name); 179 d = xalloc(sizeof(struct macro_definition), NULL); 180 d->next = n->d; 181 n->d = d; 182 setup_definition(n->d, defn, name); 183 } 184 185 void 186 macro_undefine(const char *name) 187 { 188 ndptr n = lookup(name); 189 if (n != NULL) { 190 struct macro_definition *r, *r2; 191 192 for (r = n->d; r != NULL; r = r2) { 193 r2 = r->next; 194 if (r->defn != null) 195 free(r->defn); 196 free(r); 197 } 198 n->d = NULL; 199 } 200 } 201 202 void 203 macro_popdef(const char *name) 204 { 205 ndptr n = lookup(name); 206 207 if (n != NULL) { 208 struct macro_definition *r = n->d; 209 if (r != NULL) { 210 n->d = r->next; 211 if (r->defn != null) 212 free(r->defn); 213 free(r); 214 } 215 } 216 } 217 218 void 219 macro_for_all(void (*f)(const char *, struct macro_definition *)) 220 { 221 ndptr n; 222 unsigned int i; 223 224 for (n = ohash_first(¯os, &i); n != NULL; 225 n = ohash_next(¯os, &i)) 226 f(n->name, n->d); 227 } 228 229 void 230 setup_builtin(const char *name, unsigned int type) 231 { 232 ndptr n; 233 234 n = create_entry(name); 235 n->builtin_type = type; 236 n->d = xalloc(sizeof(struct macro_definition), NULL); 237 n->d->defn = xstrdup(name); 238 n->d->type = type; 239 n->d->next = NULL; 240 } 241 242 void 243 mark_traced(const char *name, int on) 244 { 245 ndptr p; 246 unsigned int i; 247 248 if (name == NULL) { 249 if (on) 250 trace_flags |= TRACE_ALL; 251 else 252 trace_flags &= ~TRACE_ALL; 253 for (p = ohash_first(¯os, &i); p != NULL; 254 p = ohash_next(¯os, &i)) 255 p->trace_flags = FLAG_NO_TRACE; 256 } else { 257 p = create_entry(name); 258 p->trace_flags = on; 259 } 260 } 261 262 ndptr 263 macro_getbuiltin(const char *name) 264 { 265 ndptr p; 266 267 p = lookup(name); 268 if (p == NULL || p->builtin_type == MACRTYPE) 269 return NULL; 270 else 271 return p; 272 } 273 274