LTP GCOV extension - code coverage report
Current view: directory - fcode-utils/toke - strsubvocab.c
Test: FCode suite 1.0.2
Date: 2006-10-30 Instrumented lines: 21
Code covered: 100.0 % Executed lines: 21
Legend: not executed executed converted-only

       1                 : /*
       2                 :  *                     OpenBIOS - free your system!
       3                 :  *                         ( FCode tokenizer )
       4                 :  *
       5                 :  *  This program is part of a free implementation of the IEEE 1275-1994
       6                 :  *  Standard for Boot (Initialization Configuration) Firmware.
       7                 :  *
       8                 :  *  Copyright (C) 2001-2005 Stefan Reinauer, <stepan@openbios.org>
       9                 :  *
      10                 :  *  This program is free software; you can redistribute it and/or modify
      11                 :  *  it under the terms of the GNU General Public License as published by
      12                 :  *  the Free Software Foundation; version 2 of the License.
      13                 :  *
      14                 :  *  This program is distributed in the hope that it will be useful,
      15                 :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17                 :  *  GNU General Public License for more details.
      18                 :  *
      19                 :  *  You should have received a copy of the GNU General Public License
      20                 :  *  along with this program; if not, write to the Free Software
      21                 :  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
      22                 :  *
      23                 :  */
      24                 : 
      25                 : /* **************************************************************************
      26                 :  *
      27                 :  *      General-purpose support functions for
      28                 :  *          String-Substitution-type vocabularies
      29                 :  *
      30                 :  *      (C) Copyright 2005 IBM Corporation.  All Rights Reserved.
      31                 :  *      Module Author:  David L. Paktor    dlpaktor@us.ibm.com
      32                 :  *
      33                 :  **************************************************************************** */
      34                 : 
      35                 : /* **************************************************************************
      36                 :  *
      37                 :  *      A String-Substitution vocabulary, as the name implies, is one in
      38                 :  *          in which each an entry consists of two strings;  one that is
      39                 :  *          sought, and one that is returned as a substitute.  Macros and
      40                 :  *          aliases are implemented this way, as are also user-supplied
      41                 :  *          command-line symbol definitions.
      42                 :  *
      43                 :  **************************************************************************** */
      44                 : 
      45                 : /* **************************************************************************
      46                 :  *
      47                 :  *      Functions Exported:
      48                 :  *          add_str_sub_entry      Add an entry to a Str-Subst vocab
      49                 :  *          lookup_str_sub         Look for a name in a String-Subst'n vocab;
      50                 :  *                                     return a pointer to the structure.
      51                 :  *          exists_in_str_sub      Confirm whether a given name exists in a
      52                 :  *                                     String-Substitution vocabulary
      53                 :  *          reset_str_sub_vocab    Reset a given Str-Subst vocab to its initial
      54                 :  *                                    "Built-In" position.
      55                 :  *
      56                 :  *
      57                 :  **************************************************************************** */
      58                 : 
      59                 : #include <stdio.h>
      60                 : #include <stdlib.h>
      61                 : #if defined(__linux__) && ! defined(__USE_BSD)
      62                 : #define __USE_BSD
      63                 : #endif
      64                 : #include <string.h>
      65                 : 
      66                 : #include "errhandler.h"
      67                 : #include "strsubvocab.h"
      68                 : 
      69                 : 
      70                 : /* **************************************************************************
      71                 :  *
      72                 :  *      Function name:  add_str_sub_entry
      73                 :  *      Synopsis:       Add an entry to the given Str-Subst vocab
      74                 :  *      
      75                 :  *      Inputs:
      76                 :  *         Parameters:         Pointer to:
      77                 :  *             ename               space containing the name of the entry
      78                 :  *             subst_str           space containing the substitution string
      79                 :  *             *str_sub_vocab      the "tail" of the Str-Subst vocab-list 
      80                 :  *
      81                 :  *      Outputs:
      82                 :  *         Returned Value:          NONE
      83                 :  *         Supplied Pointers:
      84                 :  *             *str_sub_vocab       Will point to new entry
      85                 :  *         Memory Allocated:
      86                 :  *             Memory for the new entry will be allocated.
      87                 :  *         When Freed?
      88                 :  *             When reset_str_sub_vocab() is applied to the same vocab-list.
      89                 :  *                 In some instances, the new entry will be freed upon end
      90                 :  *                 of tokenization; in others, only on termination of program.
      91                 :  *
      92                 :  *      Error Detection:
      93                 :  *          Failure to allocate memory is a Fatal Error.
      94                 :  *
      95                 :  *      Process Explanation:
      96                 :  *          The name and substitution-string pointers are presumed to already
      97                 :  *              point to stable memory-spaces.  Memory will be allocated
      98                 :  *              for the entry itself; its pointers will be entered and the
      99                 :  *              given pointer-to-the-tail-of-the-vocabulary will be updated.
     100                 :  *
     101                 :  *      Extraneous Remarks:
     102                 :  *          This might have been where we would have checked for re-aliasing,
     103                 :  *              but the introduction of the approach to aliasing embodied in
     104                 :  *              the various  create_..._alias()  routines neatly bypasses it.
     105                 :  *
     106                 :  **************************************************************************** */
     107                 : 
     108                 : void add_str_sub_entry( char *ename,
     109                 :                             char *subst_str,
     110                 :                                 str_sub_vocab_t **str_sub_vocab )
     111              43 : {
     112                 :     str_sub_vocab_t *new_entry;
     113                 : 
     114              43 :     new_entry = safe_malloc(sizeof(str_sub_vocab_t), "adding str_sub_entry");
     115              43 :     new_entry->name   =  ename;
     116              43 :     new_entry->alias  =  subst_str;
     117              43 :     new_entry->next   = *str_sub_vocab;
     118                 : 
     119              43 :     *str_sub_vocab = new_entry;
     120                 : 
     121              43 : }
     122                 : 
     123                 : 
     124                 : /* **************************************************************************
     125                 :  *
     126                 :  *      Function name:  lookup_str_sub
     127                 :  *      Synopsis:       Look for a name in the given Str-Subst vocabulary.
     128                 :  *                      Return a pointer to the structure if name was valid
     129                 :  *      
     130                 :  *      Inputs:
     131                 :  *         Parameters:
     132                 :  *             tname                The "target" name for which to look
     133                 :  *             str_sub_vocab        The Str-Subst vocab-list
     134                 :  *
     135                 :  *      Outputs:
     136                 :  *         Returned Value:          Pointer to the substitution-string entry
     137                 :  *                                      data-structure.  NULL if not found.
     138                 :  *
     139                 :  **************************************************************************** */
     140                 : 
     141                 : str_sub_vocab_t *lookup_str_sub( char *tname, str_sub_vocab_t *str_sub_vocab )
     142             156 : {
     143                 :     str_sub_vocab_t *curr;
     144             156 :     str_sub_vocab_t *retval = NULL;
     145                 : 
     146             216 :     for (curr = str_sub_vocab ; curr != NULL ; curr=curr->next)
     147                 :     {
     148             149 :         if ( strcasecmp(tname, curr->name) == 0 )
     149                 :         {
     150              89 :             retval = curr;
     151              89 :             break;
     152                 :         }
     153                 :     }
     154             156 :     return ( retval ) ;
     155                 : }
     156                 : 
     157                 : /* **************************************************************************
     158                 :  *
     159                 :  *      Function name:  exists_in_str_sub
     160                 :  *      Synopsis:       Confirm whether a given name exists in a given
     161                 :  *                          String-Substitution vocabulary
     162                 :  *      
     163                 :  *      Inputs:
     164                 :  *         Parameters:
     165                 :  *             tname                The "target" name for which to look
     166                 :  *             str_sub_vocab        Pointer to the Str-Subst vocab-list
     167                 :  *
     168                 :  *      Outputs:
     169                 :  *         Returned Value:          TRUE if the name is found
     170                 :  *
     171                 :  **************************************************************************** */
     172                 : 
     173                 : bool exists_in_str_sub( char *tname, str_sub_vocab_t *str_sub_vocab )
     174             124 : {
     175             124 :     bool retval = FALSE;
     176             124 :     str_sub_vocab_t *found = NULL;
     177                 : 
     178             124 :     found = lookup_str_sub( tname, str_sub_vocab );
     179             124 :     if ( found != NULL )
     180                 :     {
     181              59 :         retval = TRUE;
     182                 :     }
     183             124 :     return ( retval );
     184                 : 
     185                 : }
     186                 : 

Generated by: LTP GCOV extension version 1.5