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
|
/*
* var.c - Variable handling
*
* Written 2001,2002 by Werner Almesberger
* Copyright 2001 EPFL-ICA
* Copyright 2002 Bivio Networks, Werner Almesberger
*/
#include <string.h>
#include <u128.h>
#include <memutil.h>
#include "tcsim.h"
#include "command.h"
#include "var.h"
static struct var *vars = NULL;
static struct var *set_var(const char *name,enum var_type type)
{
struct var **var;
for (var = &vars; *var; var = &(*var)->next)
if (!strcmp((*var)->name,name)) break;
if (!*var) {
*var = alloc_t(struct var);
(*var)->name = stralloc(name);
(*var)->next = NULL;
}
else {
/*
* Don't free the command ! Somebody might be doing something nasty
* like command $c = command $c
*/
/* if ((*var)->type == vt_cmd) cmd_free((*var)->u.cmd); */
}
(*var)->type = type;
return *var;
}
void set_var_unum(const char *name,U128 value)
{
set_var(name,vt_unum)->u.value = value;
}
void set_var_cmd(const char *name,struct command *cmd)
{
set_var(name,vt_cmd)->u.cmd = cmd;
}
static struct var *get_var(const char *name,enum var_type type)
{
struct var *var;
for (var = vars; var; var = var->next)
if (!strcmp(var->name,name)) break;
if (!var) yyerror("unknown variable");
if (var->type != type) yyerror("invalid variable type");
return var;
}
U128 get_var_unum(const char *name)
{
return get_var(name,vt_unum)->u.value;
}
struct command *get_var_cmd(const char *name)
{
return get_var(name,vt_cmd)->u.cmd;
}
|