Hash :
fbd83855
Author :
Date :
2006-08-18T12:47:12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
/*
* OpenBIOS - free your system!
* ( FCode tokenizer )
*
* This program is part of a free implementation of the IEEE 1275-1994
* Standard for Boot (Initialization Configuration) Firmware.
*
* Copyright (C) 2001-2005 Stefan Reinauer, <stepan@openbios.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*
*/
/* **************************************************************************
*
* General-purpose support functions for
* String-Substitution-type vocabularies
*
* (C) Copyright 2005 IBM Corporation. All Rights Reserved.
* Module Author: David L. Paktor dlpaktor@us.ibm.com
*
**************************************************************************** */
/* **************************************************************************
*
* A String-Substitution vocabulary, as the name implies, is one in
* in which each an entry consists of two strings; one that is
* sought, and one that is returned as a substitute. Macros and
* aliases are implemented this way, as are also user-supplied
* command-line symbol definitions.
*
**************************************************************************** */
/* **************************************************************************
*
* Functions Exported:
* init_str_sub_vocab Initialize a String-Substitution vocab
* add_str_sub_entry Add an entry to a Str-Subst vocab
* lookup_str_sub Look for a name in a String-Substitution
* vocab, return the substitution string.
* exists_in_str_sub Confirm whether a given name exists in a
* String-Substitution vocabulary
* create_str_sub_alias Duplicate the behavior of one name with
* another name. Return a "success" flag.
* reset_str_sub_vocab Reset a given Str-Subst vocab to its initial
* "Built-In" position.
*
*
**************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#if defined(__linux__) && ! defined(__USE_BSD)
#define __USE_BSD
#endif
#include <string.h>
#include "errhandler.h"
#include "strsubvocab.h"
/* **************************************************************************
*
* Function name: init_str_sub_vocab
* Synopsis: Dynamically initialize the link-pointers
* of the.given String-Substitution vocabulary
*
* Inputs:
* Parameters:
* str_sub_vocab_tbl Pointer to the initial Str-Subst vocab array
* max_indx Maximum Index of the initial array.
*
* Outputs:
* Returned Value: None
* Global Variables:
* The link-fields of the initial Str-Subs vocab array entries
* will be filled in.
*
**************************************************************************** */
void init_str_sub_vocab( str_sub_vocab_t *str_sub_vocab_tbl, int max_indx)
{
int indx;
for ( indx = 1 ; indx < max_indx ; indx++ )
{
str_sub_vocab_tbl[indx].next = &str_sub_vocab_tbl[indx-1];
}
}
/* **************************************************************************
*
* Function name: add_str_sub_entry
* Synopsis: Add an entry to the given Str-Subst vocab
*
* Inputs:
* Parameters: Pointer to:
* ename space containing the name of the entry
* subst_str space containing the substitution string
* *str_sub_vocab the "tail" of the Str-Subst vocab-list
*
* Outputs:
* Returned Value: NONE
* Supplied Pointers:
* *str_sub_vocab Will point to new entry
* Memory Allocated:
* Memory for the new entry will be allocated.
* When Freed?
* When reset_str_sub_vocab() is applied to the same vocab-list.
* In some instances, the new entry will be freed upon end
* of tokenization; in others, only on termination of program.
*
* Error Detection:
* Failure to allocate memory is a Fatal Error.
*
* Process Explanation:
* The name and substitution-string pointers are presumed to already
* point to stable memory-spaces. Memory will be allocated
* for the entry itself; its pointers will be entered and the
* given pointer-to-the-tail-of-the-vocabulary will be updated.
*
* Extraneous Remarks:
* This might have been where we would have checked for re-aliasing,
* but the introduction of the approach to aliasing embodied in
* the various create_..._alias() routines neatly bypasses it.
*
**************************************************************************** */
void add_str_sub_entry( char *ename,
char *subst_str,
str_sub_vocab_t **str_sub_vocab )
{
str_sub_vocab_t *new_entry;
new_entry = safe_malloc(sizeof(str_sub_vocab_t), "adding str_sub_entry");
new_entry->name = ename;
new_entry->alias = subst_str;
new_entry->next = *str_sub_vocab;
*str_sub_vocab = new_entry;
}
/* **************************************************************************
*
* Function name: lookup_str_sub
* Synopsis: Look for a name in the given Str-Subst vocabulary
*
* Inputs:
* Parameters:
* tname The "target" name for which to look
* str_sub_vocab The Str-Subst vocab-list
*
* Outputs:
* Returned Value: Pointer to the substitution string, or
* NULL pointer if name not found.
* May be NULL if subst'n string is NULL.
*
**************************************************************************** */
char *lookup_str_sub( char *tname, str_sub_vocab_t *str_sub_vocab )
{
str_sub_vocab_t *curr;
char *retval = NULL;
for (curr = str_sub_vocab ; curr != NULL ; curr=curr->next)
{
if ( strcasecmp(tname, curr->name) == 0 )
{
retval = curr->alias;
break;
}
}
return ( retval ) ;
}
/* **************************************************************************
*
* Function name: exists_in_str_sub
* Synopsis: Confirm whether a given name exists in a given
* String-Substitution vocabulary
*
* Inputs:
* Parameters:
* tname The "target" name for which to look
* str_sub_vocab Pointer to the Str-Subst vocab-list
*
* Outputs:
* Returned Value: TRUE if the name is found
*
* Extraneous Remarks:
* Because the Returned Value of lookup_str_sub() may be NULL for
* other reasons than that the name was not found, we cannot
* rely on that routine, and must replicate the outer-shell
* of its structure.
*
**************************************************************************** */
bool exists_in_str_sub( char *tname, str_sub_vocab_t *str_sub_vocab )
{
str_sub_vocab_t *curr;
bool retval = FALSE;
for (curr = str_sub_vocab ; curr != NULL ; curr=curr->next)
{
if ( strcasecmp(tname, curr->name) == 0 )
{
retval = TRUE;
break;
}
}
return ( retval );
}
/* **************************************************************************
*
* Function name: create_str_sub_alias
* Synopsis: Create an Alias in a String-Substitution vocabulary
* Return a "success" flag.
*
* Associated FORTH word: ALIAS
*
* Inputs:
* Parameters:
* old_name Name of existing entry
* new_name New name for which to create an entry
* *str_sub_vocab Pointer to "tail" of Str-Subst vocab-list
*
* Outputs:
* Returned Value: TRUE if old_name found in str_sub_vocab
* Supplied Pointers:
* *str_sub_vocab Will be updated to point to the new entry
* Memory Allocated:
* A copy of the "old" name's substitution string.
* When Freed?
* When reset_str_sub_vocab() is applied to the same vocab-list.
* In some instances, the new entry will be freed when the
* device-node in which it was created is "finish"ed; in
* others, only on termination of the program.
*
* Process Explanation:
* The "new" name is presumed to point to a stable memory-space.
* If the given "old" name exists in the given Str-Subst vocab-list,
* duplicate the substitution string into newly-allocated memory
* and pass the duplicated string and the "new" name along to
* the add_str_sub_entry() routine and return TRUE.
* If the given "old" name does not exist in the given vocab-list,
* return FALSE.
*
* Extraneous Remarks:
* This neatly bypasses the question of re-aliasing... ;-)
*
* We can rely on testing for a returned NULL from lookup_str_sub()
* because we won't be applying this routine to any vocabulary
* that permits a NULL in its substitution string.
*
**************************************************************************** */
bool create_str_sub_alias(char *new_name,
char *old_name,
str_sub_vocab_t **str_sub_vocab )
{
bool retval = FALSE;
char *old_subst_str = lookup_str_sub( old_name, *str_sub_vocab );
if ( old_subst_str != NULL )
{
char *new_subst_str = strdup(old_subst_str );
add_str_sub_entry(new_name, new_subst_str, str_sub_vocab );
retval = TRUE ;
}
return ( retval );
}
/* **************************************************************************
*
* Function name: reset_str_sub_vocab
* Synopsis: Reset a given Str-Subst vocab to its initial
* "Built-In" position.
*
* Inputs:
* Parameters:
* *str_sub_vocab Pointer to the Str-Subst vocab-list
* reset_position Position to which to reset the list
*
* Outputs:
* Returned Value: NONE
* Supplied Pointers:
* *str_sub_vocab Reset to given "Built-In" position.
* Memory Freed
* All memory allocated by user-definitions will be freed
*
* Process Explanation:
* The "stable memory-spaces" to which the name and substitution
* string pointers point are presumed to have been acquired
* by allocation of memory, which is reasonable for entries
* created by the user as opposed to the built-in entries,
* which we are, in any case, not releasing.
* The substitution-string pointer may be null; watch out when
* we free() it; not all C implementations forgive that.
*
**************************************************************************** */
void reset_str_sub_vocab(
str_sub_vocab_t **str_sub_vocab ,
str_sub_vocab_t *reset_position )
{
str_sub_vocab_t *next_t;
next_t = *str_sub_vocab;
while ( next_t != reset_position )
{
next_t = (*str_sub_vocab)->next ;
free( (*str_sub_vocab)->name );
if ( !(*str_sub_vocab)->alias )
{
free( (*str_sub_vocab)->alias );
}
free( *str_sub_vocab );
*str_sub_vocab = next_t ;
}
}