File: strsubvocab.c

package info (click to toggle)
fcode-utils 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 46,768 kB
  • sloc: ansic: 9,717; csh: 241; makefile: 129; sh: 17
file content (186 lines) | stat: -rw-r--r-- 6,815 bytes parent folder | download
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
/*
 *                     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-2010 Stefan Reinauer
 *
 *  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:
 *          add_str_sub_entry      Add an entry to a Str-Subst vocab
 *          lookup_str_sub         Look for a name in a String-Subst'n vocab;
 *                                     return a pointer to the structure.
 *          exists_in_str_sub      Confirm whether a given name exists in a
 *                                     String-Substitution vocabulary
 *          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:  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.
 *                      Return a pointer to the structure if name was valid
 *      
 *      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 entry
 *                                      data-structure.  NULL if not found.
 *
 **************************************************************************** */

str_sub_vocab_t *lookup_str_sub( char *tname, str_sub_vocab_t *str_sub_vocab )
{
    str_sub_vocab_t *curr;
    str_sub_vocab_t *retval = NULL;

    for (curr = str_sub_vocab ; curr != NULL ; curr=curr->next)
    {
        if ( strcasecmp(tname, curr->name) == 0 )
	{
	    retval = curr;
	    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
 *
 **************************************************************************** */

bool exists_in_str_sub( char *tname, str_sub_vocab_t *str_sub_vocab )
{
    bool retval = FALSE;
    str_sub_vocab_t *found = NULL;

    found = lookup_str_sub( tname, str_sub_vocab );
    if ( found != NULL )
    {
        retval = TRUE;
    }
    return ( retval );

}