File: symtable.c

package info (click to toggle)
setools 2.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,112 kB
  • ctags: 10,502
  • sloc: ansic: 76,267; tcl: 27,222; yacc: 2,943; makefile: 993; sh: 504; lex: 244
file content (59 lines) | stat: -rw-r--r-- 1,411 bytes parent folder | download | duplicates (3)
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
/* Copyright (C) 2004 Tresys Technology, LLC
 * see file 'COPYING' for use and warranty information */

/* 
 * Author: jtang@tresys.com 
 */

/* A simple symbol table implemented as a linked list.  Nothing to see
 * here, carry on.
 */


#include <stdlib.h>
#include <string.h>

#include "symtable.h"
#include "util.h"

static llist_t *symtable = NULL;


/* Retrieves a symbol from the symbol table.  If it does not exist
   return NULL. */
symbol_t *get_symbol (const char * const symbol_name) {
    llist_node_t *node_ptr;
    if (symtable == NULL) {
        return NULL;
    }
    node_ptr = symtable->head;
    while (node_ptr != NULL) {
        symbol_t *sym = (symbol_t *) node_ptr->data;
        if (sym != NULL && strcmp (sym->name, symbol_name) == 0) {
            return sym;
        }
        node_ptr = node_ptr->next;
    }
    return NULL;
}


/* Adds a symbol to the symbol table without checking for duplicates.
   Afterwards returns a pointer to that symbol.  */
symbol_t *new_symbol (char * const symbol_name) {
    symbol_t *sym;
    if ((sym = malloc (sizeof (*sym))) == NULL) {
        return NULL;
    }
    if ((sym->name = strdup (symbol_name)) == NULL) {
        return NULL;
    }
    sym->value = ll_new ();
    if (symtable == NULL) {
        if ((symtable = ll_new ()) == NULL) {
            return NULL;
        }
    }
    (void) ll_append_data (symtable, sym);
    return sym;
}