finsh_var.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Variable implementation in 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. * 2012-04-27 Bernard fixed finsh_var_delete issue which
  29. * is found by Grissiom.
  30. */
  31. #include <finsh.h>
  32. #include "finsh_var.h"
  33. struct finsh_var global_variable[FINSH_VARIABLE_MAX];
  34. struct finsh_sysvar_item* global_sysvar_list;
  35. int finsh_var_init()
  36. {
  37. memset(global_variable, 0, sizeof(global_variable));
  38. return 0;
  39. }
  40. int finsh_var_insert(const char* name, int type)
  41. {
  42. int i, empty;
  43. empty = -1;
  44. for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
  45. {
  46. /* there is a same name variable exist. */
  47. if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
  48. return -1;
  49. if (global_variable[i].type == finsh_type_unknown && empty == -1)
  50. {
  51. empty = i;
  52. }
  53. }
  54. /* there is no empty entry */
  55. if (empty == -1) return -1;
  56. /* insert entry */
  57. strncpy(global_variable[empty].name, name, FINSH_NAME_MAX);
  58. global_variable[empty].type = type;
  59. /* return the offset */
  60. return empty;
  61. }
  62. int finsh_var_delete(const char* name)
  63. {
  64. int i;
  65. for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
  66. {
  67. if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
  68. break;
  69. }
  70. /* can't find variable */
  71. if (i == FINSH_VARIABLE_MAX) return -1;
  72. memset(&global_variable[i], 0, sizeof(struct finsh_var));
  73. return 0;
  74. }
  75. struct finsh_var* finsh_var_lookup(const char* name)
  76. {
  77. int i;
  78. for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
  79. {
  80. if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
  81. break;
  82. }
  83. /* can't find variable */
  84. if (i == FINSH_VARIABLE_MAX) return NULL;
  85. return &global_variable[i];
  86. }
  87. #ifdef RT_USING_HEAP
  88. void finsh_sysvar_append(const char* name, u_char type, void* var_addr)
  89. {
  90. /* create a sysvar */
  91. struct finsh_sysvar_item* item;
  92. item = (struct finsh_sysvar_item*) rt_malloc (sizeof(struct finsh_sysvar_item));
  93. if (item != NULL)
  94. {
  95. item->next = NULL;
  96. item->sysvar.name = rt_strdup(name);
  97. item->sysvar.type = type;
  98. item->sysvar.var = var_addr;
  99. if (global_sysvar_list == NULL)
  100. {
  101. global_sysvar_list = item;
  102. }
  103. else
  104. {
  105. item->next = global_sysvar_list;
  106. global_sysvar_list = item;
  107. }
  108. }
  109. }
  110. #endif
  111. struct finsh_sysvar* finsh_sysvar_lookup(const char* name)
  112. {
  113. struct finsh_sysvar* index;
  114. struct finsh_sysvar_item* item;
  115. for (index = _sysvar_table_begin;
  116. index < _sysvar_table_end;
  117. FINSH_NEXT_SYSVAR(index))
  118. {
  119. if (strcmp(index->name, name) == 0)
  120. return index;
  121. }
  122. /* find in sysvar list */
  123. item = global_sysvar_list;
  124. while (item != NULL)
  125. {
  126. if (strncmp(item->sysvar.name, name, strlen(name)) == 0)
  127. {
  128. return &(item->sysvar);
  129. }
  130. /* move to next item */
  131. item = item->next;
  132. }
  133. /* can't find variable */
  134. return NULL;
  135. }