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"
25 #include "configuration.h"
27 #include "binarybuffer.h"
31 var_t *variables = NULL;
33 int handle_var_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
34 int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
35 int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
37 int interpreter_register_commands(struct command_context_s *cmd_ctx)
39 register_command(cmd_ctx, NULL, "var", handle_var_command,
40 COMMAND_ANY, "allocate, display or delete variable <name> [num_fields|'del'] [size1] ...");
41 register_command(cmd_ctx, NULL, "field", handle_field_command,
42 COMMAND_ANY, "display/modify variable field <var> <field> [value|'flip']");
43 register_command(cmd_ctx, NULL, "script", handle_script_command,
44 COMMAND_ANY, "execute commands from <file>");
49 var_t* get_var_by_num(int num)
52 var_t *var = variables;
69 var_t* get_var_by_name(char *name)
71 var_t *var = variables;
75 if (strcmp(var->name, name) == 0)
80 if (strcmp(var->name, name) == 0)
87 var_t* get_var_by_namenum(char *namenum)
89 if ((namenum[0] >= '0') && (namenum[0] <= '9'))
90 return get_var_by_num(strtol(namenum, NULL, 0));
92 return get_var_by_name(namenum);
96 int field_le_to_host(u8 *buffer, void *priv, struct scan_field_s *dummy)
98 var_field_t *field = priv;
99 field->value = buf_get_u32(buffer, 0, field->num_bits);
104 int handle_var_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
106 var_t **last_var_p = &variables;
113 if (strcmp((*last_var_p)->name, args[0]) == 0)
115 if (strcmp(args[1], "del") == 0)
117 var_t *next = (*last_var_p)->next;
118 free ((*last_var_p)->fields);
121 command_print(cmd_ctx, "variable %s deleted", args[0]);
124 command_print(cmd_ctx, "variable of that name already exists");
127 last_var_p = &((*last_var_p)->next);
130 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
132 command_print(cmd_ctx, "invalid name specified (first character may not be a number)");
136 *last_var_p = malloc(sizeof(var_t));
137 (*last_var_p)->name = strdup(args[0]);
138 (*last_var_p)->num_fields = argc - 1;
139 (*last_var_p)->next = NULL;
141 (*last_var_p)->fields = malloc(sizeof(var_field_t) * (*last_var_p)->num_fields);
142 for (i = 0; i < (*last_var_p)->num_fields; i++)
144 (*last_var_p)->fields[i].num_bits = strtol(args[1+i], NULL, 0);
145 (*last_var_p)->fields[i].value = 0x0;
152 var_t *var = get_var_by_namenum(args[0]);
156 command_print(cmd_ctx, "%s (%i fields):", var->name, var->num_fields);
157 for (i = 0; i < (var->num_fields); i++)
159 command_print(cmd_ctx, "0x%x (/%i)", var->fields[i].value, var->fields[i].num_bits);
164 command_print(cmd_ctx, "variable %s doesn't exist", args[0]);
170 var_t *var = variables;
174 command_print(cmd_ctx, "%i: %s (%i fields)", count, var->name, var->num_fields);
183 int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
187 command_print(cmd_ctx, "usage: field <var> <field> [value|'flip']");
191 var_t *var = get_var_by_namenum(args[0]);
192 int field_num = strtol(args[1], NULL, 0);
195 command_print(cmd_ctx, "variable %s doesn't exist", args[0]);
198 if (field_num >= var->num_fields)
199 command_print(cmd_ctx, "variable field %i is out of bounds (max. %i)", field_num, var->num_fields - 1);
200 if ((var) && (field_num < var->num_fields))
204 if (strcmp(args[2], "flip") == 0)
205 var->fields[field_num].value = flip_u32(var->fields[field_num].value, var->fields[field_num].num_bits);
207 var->fields[field_num].value = strtoul(args[2], NULL, 0);
210 command_print(cmd_ctx, "%s(%i): 0x%x (/%i)", var->name, field_num, var->fields[field_num].value, var->fields[field_num].num_bits);
217 int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
223 command_print(cmd_ctx, "usage: script <file>");
225 script_file = open_file_from_path(cmd_ctx, args[0], "r");
229 command_print(cmd_ctx, "couldn't open script file %s", args[0]);
233 echo = cmd_ctx->echo;
236 command_run_file(cmd_ctx, script_file, cmd_ctx->mode);
238 cmd_ctx->echo = echo;