1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 * 25 * logadm/lut.c -- simple lookup table module 26 * 27 * this file contains a very simple lookup table (lut) implementation. 28 * the tables only support insert & lookup, no delete, and can 29 * only be walked in lexical order. if the key already exists 30 * for something being inserted, the previous entry is simply 31 * replaced. the left-hand-side (lhs), which is the key, is 32 * copied into malloc'd memory. the right-hand-side (rhs), which 33 * is the datum, is not copied (in fact, the lut routines don't 34 * know the size or type of the datum, just the void * pointer to it). 35 * 36 * yea, this module could be much fancier and do much more, but 37 * the idea was to keep it really simple and just provide what 38 * was needed by logadm for options processing. 39 */ 40 41 #include <stdio.h> 42 #include <strings.h> 43 #include "err.h" 44 #include "lut.h" 45 46 /* forward declarations of functions private to this module */ 47 static void dooper(const char *lhs, void *rhs, void *arg); 48 49 /* info created by lut_add() and lut_dup(), private to this module */ 50 struct lut { 51 struct lut *lut_left; 52 struct lut *lut_right; 53 char *lut_lhs; /* search key */ 54 void *lut_rhs; /* the datum */ 55 }; 56 57 /* 58 * lut_add -- add an entry to the table 59 * 60 * use it like this: 61 * struct lut *root = NULL; 62 * root = lut_add(root, "key", value); 63 * 64 * the key string gets strdup'd by lut_add(), but the memory holding 65 * the *value should not be freed until the lut is freed by lut_free(). 66 */ 67 struct lut * 68 lut_add(struct lut *root, const char *lhs, void *rhs) 69 { 70 int diff = 0; 71 72 if (root == NULL) { 73 /* not in tree, create new node */ 74 root = MALLOC(sizeof (*root)); 75 root->lut_lhs = STRDUP(lhs); 76 root->lut_rhs = rhs; 77 root->lut_left = root->lut_right = NULL; 78 } else if (lhs != NULL && (diff = strcmp(root->lut_lhs, lhs)) == 0) { 79 /* already in tree, replace node */ 80 root->lut_rhs = rhs; 81 } else if (diff > 0) 82 root->lut_left = lut_add(root->lut_left, lhs, rhs); 83 else 84 root->lut_right = lut_add(root->lut_right, lhs, rhs); 85 return (root); 86 } 87 88 /* helper function for lut_dup() */ 89 static void 90 dooper(const char *lhs, void *rhs, void *arg) 91 { 92 struct lut **rootp = (struct lut **)arg; 93 94 *rootp = lut_add(*rootp, lhs, rhs); 95 } 96 97 /* 98 * lut_dup -- duplicate a lookup table 99 * 100 * caller is responsible for keeping track of how many tables are keeping 101 * pointers to the void * datum values. 102 */ 103 struct lut * 104 lut_dup(struct lut *root) 105 { 106 struct lut *ret = NULL; 107 108 lut_walk(root, dooper, &ret); 109 110 return (ret); 111 } 112 113 /* 114 * lut_lookup -- find an entry 115 */ 116 void * 117 lut_lookup(struct lut *root, const char *lhs) 118 { 119 int diff; 120 121 if (root == NULL || lhs == NULL) 122 return (NULL); 123 else if ((diff = strcmp(root->lut_lhs, lhs)) == 0) 124 return (root->lut_rhs); 125 else if (diff > 0) 126 return (lut_lookup(root->lut_left, lhs)); 127 else 128 return (lut_lookup(root->lut_right, lhs)); 129 } 130 131 /* 132 * lut_walk -- walk the table in lexical order 133 */ 134 void 135 lut_walk(struct lut *root, 136 void (*callback)(const char *lhs, void *rhs, void *arg), void *arg) 137 { 138 if (root) { 139 lut_walk(root->lut_left, callback, arg); 140 (*callback)(root->lut_lhs, root->lut_rhs, arg); 141 lut_walk(root->lut_right, callback, arg); 142 } 143 } 144 145 /* 146 * lut_free -- free a lut 147 * 148 * if callback is provided, it is called for each value in the table. 149 * it the values are things that the caller malloc'd, then you can do: 150 * lut_free(root, free); 151 */ 152 void 153 lut_free(struct lut *root, void (*callback)(void *rhs)) 154 { 155 if (root != NULL) { 156 lut_free(root->lut_left, callback); 157 lut_free(root->lut_right, callback); 158 FREE(root->lut_lhs); 159 if (callback) 160 (*callback)(root->lut_rhs); 161 FREE(root); 162 } 163 } 164 165 166 #ifdef TESTMODULE 167 168 void 169 printer(const char *lhs, void *rhs, void *arg) 170 { 171 printf("<%s> <%s> (<%s>)\n", lhs, (char *)rhs, 172 (char *)lut_lookup(arg, lhs)); 173 } 174 175 /* 176 * test main for lut module, usage: a.out [lhs[=rhs]...] 177 */ 178 int 179 main(int argc, char *argv[]) 180 { 181 struct lut *r = NULL; 182 struct lut *dupr = NULL; 183 char *equals; 184 185 err_init(argv[0]); 186 setbuf(stdout, NULL); 187 188 for (argv++; *argv; argv++) 189 if ((equals = strchr(*argv, '=')) != NULL) { 190 *equals++ = '\0'; 191 r = lut_add(r, *argv, equals); 192 } else 193 r = lut_add(r, *argv, "NULL"); 194 195 printf("lut contains:\n"); 196 lut_walk(r, printer, r); 197 198 dupr = lut_dup(r); 199 200 lut_free(r, NULL); 201 202 printf("dup lut contains:\n"); 203 lut_walk(dupr, printer, dupr); 204 205 lut_free(dupr, NULL); 206 207 err_done(0); 208 /* NOTREACHED */ 209 return (0); 210 } 211 212 #endif /* TESTMODULE */ 213