| 1 | /* |
| 2 | * ========== Copyright Header Begin ========================================== |
| 3 | * |
| 4 | * Hypervisor Software File: defines.c |
| 5 | * |
| 6 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 7 | * |
| 8 | * - Do no alter or remove copyright notices |
| 9 | * |
| 10 | * - Redistribution and use of this software in source and binary forms, with |
| 11 | * or without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistribution of source code must retain the above copyright notice, |
| 15 | * this list of conditions and the following disclaimer. |
| 16 | * |
| 17 | * - Redistribution in binary form must reproduce the above copyright notice, |
| 18 | * this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * |
| 21 | * Neither the name of Sun Microsystems, Inc. or the names of contributors |
| 22 | * may be used to endorse or promote products derived from this software |
| 23 | * without specific prior written permission. |
| 24 | * |
| 25 | * This software is provided "AS IS," without a warranty of any kind. |
| 26 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, |
| 27 | * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
| 28 | * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN |
| 29 | * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR |
| 30 | * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR |
| 31 | * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN |
| 32 | * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR |
| 33 | * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE |
| 34 | * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, |
| 35 | * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF |
| 36 | * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
| 37 | * |
| 38 | * You acknowledge that this software is not designed, licensed or |
| 39 | * intended for use in the design, construction, operation or maintenance of |
| 40 | * any nuclear facility. |
| 41 | * |
| 42 | * ========== Copyright Header End ============================================ |
| 43 | */ |
| 44 | /* |
| 45 | * @(#)defines.c 1.3 03/08/20 |
| 46 | * Copyright 2001-2003 Sun Microsystems, Inc. All Rights Reserved |
| 47 | * Copyright Use is subject to license terms. |
| 48 | */ |
| 49 | |
| 50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <unistd.h> |
| 53 | #include <string.h> |
| 54 | |
| 55 | #include "defines.h" |
| 56 | |
| 57 | static define_t *symbol_root = NULL; |
| 58 | |
| 59 | /* find a symbol by name and return a pointer to it or NULL if not found */ |
| 60 | static define_t * |
| 61 | find_symbol(char *name) |
| 62 | { |
| 63 | define_t *found = NULL; |
| 64 | define_t *ptr = symbol_root; |
| 65 | |
| 66 | while (ptr != NULL) { |
| 67 | if (strcmp(ptr->name, name) == 0) { |
| 68 | found = ptr; |
| 69 | break; |
| 70 | } |
| 71 | ptr = ptr->next; |
| 72 | } |
| 73 | return (found); |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * define a symbol, the data part follows the optional '=' sign. |
| 78 | * the type is just used to track where the symbol was defined - on the |
| 79 | * command line or by upload from the forth engine. |
| 80 | */ |
| 81 | void |
| 82 | define_symbol(char *name, int type) |
| 83 | { |
| 84 | char *sname, *tname; |
| 85 | char *valuep; |
| 86 | define_t *new; |
| 87 | |
| 88 | tname = strdup(name); |
| 89 | valuep = strchr(tname, '='); |
| 90 | if (valuep != NULL) { |
| 91 | *valuep = 0; |
| 92 | valuep = strdup(valuep+1); |
| 93 | name = strdup(tname); |
| 94 | free(tname); |
| 95 | } else { |
| 96 | name = strdup(name); |
| 97 | } |
| 98 | new = find_symbol(name); |
| 99 | if (new != NULL) { |
| 100 | if ((new->type != FORTH_UNDEF) && (new->type != CMD_UNDEF)) { |
| 101 | fprintf(stderr, "Warning redefining %s\n", name); |
| 102 | } |
| 103 | if (new->value != NULL) { |
| 104 | free(new->value); |
| 105 | } |
| 106 | new->value = valuep; |
| 107 | new->type = type; |
| 108 | free(name); |
| 109 | } else { |
| 110 | new = malloc(sizeof (define_t)); |
| 111 | new->name = name; |
| 112 | new->next = symbol_root; |
| 113 | new->prev = NULL; |
| 114 | new->value = valuep; |
| 115 | new->type = type; |
| 116 | if (symbol_root != NULL) { |
| 117 | symbol_root->prev = new; |
| 118 | } |
| 119 | symbol_root = new; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * 'un'def a symbol, type tracks where the 'undef' was executed from |
| 125 | * either the command line or the forth engine. |
| 126 | */ |
| 127 | void |
| 128 | undef_symbol(char *name, int type) |
| 129 | { |
| 130 | define_t *which, *prev, *next; |
| 131 | |
| 132 | which = find_symbol(name); |
| 133 | if (which != NULL) { |
| 134 | #if 0 |
| 135 | prev = which->prev; |
| 136 | next = which->next; |
| 137 | if (prev != NULL) { |
| 138 | prev->next = which->next; |
| 139 | } else { |
| 140 | symbol_root = next; |
| 141 | } |
| 142 | next->prev = which->prev; |
| 143 | if (which->value != NULL) { |
| 144 | free(which->value); |
| 145 | } |
| 146 | free(which); |
| 147 | #endif |
| 148 | which->type = type; |
| 149 | } else { |
| 150 | define_symbol(name, type); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* return true/false to test for a symbol existence */ |
| 155 | int |
| 156 | symbol_defined(char *name) |
| 157 | { |
| 158 | define_t *found; |
| 159 | |
| 160 | found = find_symbol(name); |
| 161 | |
| 162 | return ((found != NULL) && |
| 163 | (found->type != FORTH_UNDEF) && |
| 164 | (found->type != CMD_UNDEF)); |
| 165 | } |
| 166 | |
| 167 | /* return a pointer to a symbols data, return NULL if there is none. */ |
| 168 | char * |
| 169 | extract_symbol(char *name) |
| 170 | { |
| 171 | char *valuep = NULL; |
| 172 | define_t *which; |
| 173 | |
| 174 | which = find_symbol(name); |
| 175 | if (which != NULL) { |
| 176 | valuep = which->value; |
| 177 | } |
| 178 | return (valuep); |
| 179 | } |
| 180 | |
| 181 | /* a debug routine to show a symbol, its data and where is was defined */ |
| 182 | static void |
| 183 | show_symbol(define_t *ptr) |
| 184 | { |
| 185 | char *value; |
| 186 | char *type; |
| 187 | |
| 188 | switch (ptr->type) { |
| 189 | case CMD_DEFINE: |
| 190 | type = "[cmd line, defined]"; |
| 191 | break; |
| 192 | |
| 193 | case CMD_UNDEF: |
| 194 | type = "[cmd line, undef]"; |
| 195 | break; |
| 196 | |
| 197 | case FORTH_DEFINE: |
| 198 | type = "[Forth defined]"; |
| 199 | break; |
| 200 | |
| 201 | case FORTH_UNDEF: |
| 202 | type = "[Forth undefined]"; |
| 203 | break; |
| 204 | default: |
| 205 | type = "[invalid define type]"; |
| 206 | break; |
| 207 | } |
| 208 | if (ptr->value == NULL) { |
| 209 | value = "[Boolean]"; |
| 210 | } else { |
| 211 | value = ptr->value; |
| 212 | } |
| 213 | fprintf(stderr, "%-20.20s %-20.20s %s\n", ptr->name, type, value); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * release all resources used by symbols, optionally printing the information |
| 218 | * about the symbol use - called when the wrapper exits. |
| 219 | */ |
| 220 | void |
| 221 | finish_symbols(int show) |
| 222 | { |
| 223 | define_t *ptr = symbol_root; |
| 224 | |
| 225 | if (show) { |
| 226 | fprintf(stderr, "%-20.20s %-20.20s %s\n", |
| 227 | "Symbols", "location", "type/value"); |
| 228 | } |
| 229 | while (ptr != NULL) { |
| 230 | define_t *next = ptr->next; |
| 231 | if (show) { |
| 232 | show_symbol(ptr); |
| 233 | } else { |
| 234 | if (ptr->value != NULL) { |
| 235 | free(ptr->value); |
| 236 | } |
| 237 | free(ptr->name); |
| 238 | free(ptr); |
| 239 | } |
| 240 | ptr = next; |
| 241 | } |
| 242 | if (!show) { |
| 243 | symbol_root = NULL; |
| 244 | } |
| 245 | } |