finsh_node.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * node routines for finsh shell.
  3. *
  4. * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
  5. *
  6. * This file is part of RT-Thread (http://www.rt-thread.org)
  7. * Maintainer: bernard.xiong <bernard.xiong at gmail.com>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. * Change Logs:
  26. * Date Author Notes
  27. * 2010-03-22 Bernard first version
  28. */
  29. #include <finsh.h>
  30. #include "finsh_node.h"
  31. #include "finsh_error.h"
  32. #include "finsh_var.h"
  33. #include "finsh_heap.h"
  34. struct finsh_node global_node_table[FINSH_NODE_MAX];
  35. int finsh_node_init()
  36. {
  37. memset(global_node_table, 0, sizeof(global_node_table));
  38. return 0;
  39. }
  40. struct finsh_node* finsh_node_allocate(u_char type)
  41. {
  42. int i;
  43. /* find an empty entry */
  44. for (i = 0; i < FINSH_NODE_MAX; i ++)
  45. {
  46. if (global_node_table[i].node_type == FINSH_NODE_UNKNOWN) break;
  47. }
  48. if (i == FINSH_NODE_MAX) return NULL;
  49. /* fill type field */
  50. global_node_table[i].node_type = type;
  51. /* return this allocated node */
  52. return &global_node_table[i];
  53. }
  54. struct finsh_node* finsh_node_new_id(char* id)
  55. {
  56. struct finsh_node* node;
  57. void* symbol;
  58. unsigned char type;
  59. symbol = NULL;
  60. type = 0;
  61. node = NULL;
  62. /* lookup variable firstly */
  63. symbol = (void*)finsh_var_lookup(id);
  64. if (symbol == NULL)
  65. {
  66. /* then lookup system variable */
  67. symbol = (void*)finsh_sysvar_lookup(id);
  68. if (symbol == NULL)
  69. {
  70. /* then lookup system call */
  71. symbol = (void*)finsh_syscall_lookup(id);
  72. if (symbol != NULL) type = FINSH_IDTYPE_SYSCALL;
  73. }
  74. else type = FINSH_IDTYPE_SYSVAR;
  75. }
  76. else type = FINSH_IDTYPE_VAR;
  77. if (symbol != NULL)
  78. {
  79. /* allocate a new node */
  80. node = finsh_node_allocate(FINSH_NODE_ID);
  81. /* allocate node error */
  82. if (node == NULL)
  83. {
  84. finsh_error_set(FINSH_ERROR_MEMORY_FULL);
  85. return NULL;
  86. }
  87. /* fill node value according type */
  88. switch (type)
  89. {
  90. case FINSH_IDTYPE_VAR:
  91. node->id.var = (struct finsh_var*)symbol;
  92. break;
  93. case FINSH_IDTYPE_SYSVAR:
  94. node->id.sysvar = (struct finsh_sysvar*)symbol;
  95. break;
  96. case FINSH_IDTYPE_SYSCALL:
  97. node->id.syscall = (struct finsh_syscall*)symbol;
  98. break;
  99. }
  100. /* fill identifier type */
  101. node->idtype = type;
  102. }
  103. else finsh_error_set(FINSH_ERROR_UNKNOWN_SYMBOL);
  104. return node;
  105. }
  106. struct finsh_node* finsh_node_new_char(char c)
  107. {
  108. struct finsh_node* node;
  109. node = finsh_node_allocate(FINSH_NODE_VALUE_CHAR);
  110. if (node == NULL)
  111. {
  112. finsh_error_set(FINSH_ERROR_MEMORY_FULL);
  113. return NULL;
  114. }
  115. node->value.char_value = c;
  116. return node;
  117. }
  118. struct finsh_node* finsh_node_new_int(int i)
  119. {
  120. struct finsh_node* node;
  121. node = finsh_node_allocate(FINSH_NODE_VALUE_INT);
  122. if (node == NULL)
  123. {
  124. finsh_error_set(FINSH_ERROR_MEMORY_FULL);
  125. return NULL;
  126. }
  127. node->value.int_value = i;
  128. return node;
  129. }
  130. struct finsh_node* finsh_node_new_long(long l)
  131. {
  132. struct finsh_node* node;
  133. node = finsh_node_allocate(FINSH_NODE_VALUE_LONG);
  134. if (node == NULL)
  135. {
  136. finsh_error_set(FINSH_ERROR_MEMORY_FULL);
  137. return NULL;
  138. }
  139. node->value.long_value = l;
  140. return node;
  141. }
  142. struct finsh_node* finsh_node_new_string(char* s)
  143. {
  144. struct finsh_node* node;
  145. node = finsh_node_allocate(FINSH_NODE_VALUE_STRING);
  146. if (node == NULL)
  147. {
  148. finsh_error_set(FINSH_ERROR_MEMORY_FULL);
  149. return NULL;
  150. }
  151. /* make string */
  152. node->value.ptr = finsh_heap_allocate(strlen(s) + 1);
  153. strncpy(node->value.ptr, s, strlen(s));
  154. ((u_char*)node->value.ptr)[strlen(s)] = '\0';
  155. return node;
  156. }
  157. struct finsh_node* finsh_node_new_ptr(void* ptr)
  158. {
  159. struct finsh_node* node;
  160. node = finsh_node_allocate(FINSH_NODE_VALUE_NULL);
  161. if (node == NULL)
  162. {
  163. finsh_error_set(FINSH_ERROR_MEMORY_FULL);
  164. return NULL;
  165. }
  166. node->value.ptr = ptr;
  167. return node;
  168. }