You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

244 lines
6.5 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  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. *
  9. * *
  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. *
  14. * *
  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. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "interpreter.h"
  24. #include "configuration.h"
  25. #include "binarybuffer.h"
  26. #include <stdlib.h>
  27. #include <string.h>
  28. var_t *variables = NULL;
  29. int handle_var_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  30. int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  31. int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  32. int interpreter_register_commands(struct command_context_s *cmd_ctx)
  33. {
  34. register_command(cmd_ctx, NULL, "var", handle_var_command,
  35. COMMAND_ANY, "allocate, display or delete variable <name> [num_fields|'del'] [size1] ...");
  36. register_command(cmd_ctx, NULL, "field", handle_field_command,
  37. COMMAND_ANY, "display/modify variable field <var> <field> [value|'flip']");
  38. register_command(cmd_ctx, NULL, "script", handle_script_command,
  39. COMMAND_ANY, "execute commands from <file>");
  40. return ERROR_OK;
  41. }
  42. var_t* get_var_by_num(int num)
  43. {
  44. int count = 0;
  45. var_t *var = variables;
  46. if (var)
  47. {
  48. if (num == count)
  49. return var;
  50. while (var->next)
  51. {
  52. var = var->next;
  53. count++;
  54. if (num == count)
  55. return var;
  56. }
  57. }
  58. return NULL;
  59. }
  60. var_t* get_var_by_name(char *name)
  61. {
  62. var_t *var = variables;
  63. if (var)
  64. {
  65. if (strcmp(var->name, name) == 0)
  66. return var;
  67. while (var->next)
  68. {
  69. var = var->next;
  70. if (strcmp(var->name, name) == 0)
  71. return var;
  72. }
  73. }
  74. return NULL;
  75. }
  76. var_t* get_var_by_namenum(char *namenum)
  77. {
  78. if ((namenum[0] >= '0') && (namenum[0] <= '9'))
  79. return get_var_by_num(strtol(namenum, NULL, 0));
  80. else
  81. return get_var_by_name(namenum);
  82. }
  83. int field_le_to_host(u8 *buffer, void *priv, struct scan_field_s *dummy)
  84. {
  85. var_field_t *field = priv;
  86. field->value = buf_get_u32(buffer, 0, field->num_bits);
  87. return ERROR_OK;
  88. }
  89. int handle_var_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  90. {
  91. var_t **last_var_p = &variables;
  92. int i;
  93. if (argc >= 2)
  94. {
  95. while (*last_var_p)
  96. {
  97. if (strcmp((*last_var_p)->name, args[0]) == 0)
  98. {
  99. if (strcmp(args[1], "del") == 0)
  100. {
  101. var_t *next = (*last_var_p)->next;
  102. free ((*last_var_p)->fields);
  103. free (*last_var_p);
  104. *last_var_p = next;
  105. command_print(cmd_ctx, "variable %s deleted", args[0]);
  106. }
  107. else
  108. command_print(cmd_ctx, "variable of that name already exists");
  109. return ERROR_OK;
  110. }
  111. last_var_p = &((*last_var_p)->next);
  112. }
  113. if ((args[0][0] >= '0') && (args[0][0] <= '9'))
  114. {
  115. command_print(cmd_ctx, "invalid name specified (first character may not be a number)");
  116. return ERROR_OK;
  117. }
  118. *last_var_p = malloc(sizeof(var_t));
  119. (*last_var_p)->name = strdup(args[0]);
  120. (*last_var_p)->num_fields = argc - 1;
  121. (*last_var_p)->next = NULL;
  122. (*last_var_p)->fields = malloc(sizeof(var_field_t) * (*last_var_p)->num_fields);
  123. for (i = 0; i < (*last_var_p)->num_fields; i++)
  124. {
  125. (*last_var_p)->fields[i].num_bits = strtol(args[1+i], NULL, 0);
  126. (*last_var_p)->fields[i].value = 0x0;
  127. }
  128. return ERROR_OK;
  129. }
  130. if (argc == 1)
  131. {
  132. var_t *var = get_var_by_namenum(args[0]);
  133. if (var)
  134. {
  135. int i;
  136. command_print(cmd_ctx, "%s (%i fields):", var->name, var->num_fields);
  137. for (i = 0; i < (var->num_fields); i++)
  138. {
  139. command_print(cmd_ctx, "0x%x (/%i)", var->fields[i].value, var->fields[i].num_bits);
  140. }
  141. }
  142. else
  143. {
  144. command_print(cmd_ctx, "variable %s doesn't exist", args[0]);
  145. }
  146. }
  147. if (argc == 0)
  148. {
  149. var_t *var = variables;
  150. int count = 0;
  151. while (var)
  152. {
  153. command_print(cmd_ctx, "%i: %s (%i fields)", count, var->name, var->num_fields);
  154. var = var->next;
  155. count++;
  156. }
  157. }
  158. return ERROR_OK;
  159. }
  160. int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  161. {
  162. if (argc < 2)
  163. command_print(cmd_ctx, "usage: field <var> <field> [value|'flip']");
  164. if (argc >= 2)
  165. {
  166. var_t *var = get_var_by_namenum(args[0]);
  167. int field_num = strtol(args[1], NULL, 0);
  168. if (!var)
  169. {
  170. command_print(cmd_ctx, "variable %s doesn't exist", args[0]);
  171. return ERROR_OK;
  172. }
  173. if (field_num >= var->num_fields)
  174. command_print(cmd_ctx, "variable field %i is out of bounds (max. %i)", field_num, var->num_fields - 1);
  175. if ((var) && (field_num < var->num_fields))
  176. {
  177. if (argc > 2)
  178. {
  179. if (strcmp(args[2], "flip") == 0)
  180. var->fields[field_num].value = flip_u32(var->fields[field_num].value, var->fields[field_num].num_bits);
  181. else
  182. var->fields[field_num].value = strtoul(args[2], NULL, 0);
  183. }
  184. command_print(cmd_ctx, "%s(%i): 0x%x (/%i)", var->name, field_num, var->fields[field_num].value, var->fields[field_num].num_bits);
  185. }
  186. }
  187. return ERROR_OK;
  188. }
  189. int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  190. {
  191. FILE *script_file;
  192. int echo;
  193. if (argc != 1)
  194. command_print(cmd_ctx, "usage: script <file>");
  195. script_file = open_file_from_path(cmd_ctx, args[0], "r");
  196. if (!script_file)
  197. {
  198. command_print(cmd_ctx, "couldn't open script file %s", args[0]);
  199. return ERROR_OK;
  200. }
  201. echo = cmd_ctx->echo;
  202. cmd_ctx->echo = 1;
  203. command_run_file(cmd_ctx, script_file, cmd_ctx->mode);
  204. cmd_ctx->echo = echo;
  205. fclose(script_file);
  206. return ERROR_OK;
  207. }