1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "interpreter.h"
26 #include "binarybuffer.h"
30 var_t *variables = NULL;
32 int handle_var_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
33 int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
34 int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
36 int interpreter_register_commands(struct command_context_s *cmd_ctx)
38 register_command(cmd_ctx, NULL, "var", handle_var_command,
39 COMMAND_ANY, "allocate, display or delete variable <name> [num_fields|'del'] [size1] ...");
40 register_command(cmd_ctx, NULL, "field", handle_field_command,
41 COMMAND_ANY, "display/modify variable field <var> <field> [value|'flip']");
42 register_command(cmd_ctx, NULL, "script", handle_script_command,
43 COMMAND_ANY, "execute commands from <file>");
48 var_t* get_var_by_num(int num)
51 var_t *var = variables;
68 var_t* get_var_by_name(char *name)
70 var_t *var = variables;
74 if (strcmp(var->name, name) == 0)
79 if (strcmp(var->name, name) == 0)
86 var_t* get_var_by_namenum(char *namenum)
88 if ((namenum[0] >= '0') && (namenum[0] <= '9'))
89 return get_var_by_num(strtol(namenum, NULL, 0));
91 return get_var_by_name(namenum);
95 int field_le_to_host(u8 *buffer, void *priv)
97 var_field_t *field = priv;
98 field->value = buf_get_u32(buffer, 0, field->num_bits);
103 int handle_var_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
105 var_t **last_var_p = &variables;
112 if (strcmp((*last_var_p)->name, args[0]) == 0)
114 if (strcmp(args[1], "del") == 0)
116 var_t *next = (*last_var_p)->next;
117 free ((*last_var_p)->fields);
120 command_print(cmd_ctx, "variable %s deleted", args[0]);
123 command_print(cmd_ctx, "variable of that name already exists");
126 last_var_p = &((*last_var_p)->next);
129 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
131 command_print(cmd_ctx, "invalid name specified (first character may not be a number)");
135 *last_var_p = malloc(sizeof(var_t));
136 (*last_var_p)->name = strdup(args[0]);
137 (*last_var_p)->num_fields = argc - 1;
138 (*last_var_p)->next = NULL;
140 (*last_var_p)->fields = malloc(sizeof(var_field_t) * (*last_var_p)->num_fields);
141 for (i = 0; i < (*last_var_p)->num_fields; i++)
143 (*last_var_p)->fields[i].num_bits = strtol(args[1+i], NULL, 0);
144 (*last_var_p)->fields[i].value = 0x0;
151 var_t *var = get_var_by_namenum(args[0]);
155 command_print(cmd_ctx, "%s (%i fields):", var->name, var->num_fields);
156 for (i = 0; i < (var->num_fields); i++)
158 command_print(cmd_ctx, "0x%x (/%i)", var->fields[i].value, var->fields[i].num_bits);
163 command_print(cmd_ctx, "variable %s doesn't exist", args[0]);
169 var_t *var = variables;
173 command_print(cmd_ctx, "%i: %s (%i fields)", count, var->name, var->num_fields);
182 int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
186 command_print(cmd_ctx, "usage: field <var> <field> [value|'flip']");
190 var_t *var = get_var_by_namenum(args[0]);
191 int field_num = strtol(args[1], NULL, 0);
194 command_print(cmd_ctx, "variable %s doesn't exist", args[0]);
197 if (field_num >= var->num_fields)
198 command_print(cmd_ctx, "variable field %i is out of bounds (max. %i)", field_num, var->num_fields - 1);
199 if ((var) && (field_num < var->num_fields))
203 if (strcmp(args[2], "flip") == 0)
204 var->fields[field_num].value = flip_u32(var->fields[field_num].value, var->fields[field_num].num_bits);
206 var->fields[field_num].value = strtoul(args[2], NULL, 0);
209 command_print(cmd_ctx, "%s(%i): 0x%x (/%i)", var->name, field_num, var->fields[field_num].value, var->fields[field_num].num_bits);
216 int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
222 command_print(cmd_ctx, "usage: script <file>");
224 script_file = fopen(args[0], "r");
227 command_print(cmd_ctx, "couldn't open script file %s", args[0]);
231 echo = cmd_ctx->echo;
234 command_run_file(cmd_ctx, script_file, cmd_ctx->mode);
236 cmd_ctx->echo = echo;