finsh_var.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * File : finsh_var.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-03-22 Bernard first version
  13. * 2012-04-27 Bernard fixed finsh_var_delete issue which
  14. * is found by Grissiom.
  15. */
  16. #include <finsh.h>
  17. #include "finsh_var.h"
  18. struct finsh_var global_variable[FINSH_VARIABLE_MAX];
  19. struct finsh_sysvar_item* global_sysvar_list;
  20. int finsh_var_init()
  21. {
  22. memset(global_variable, 0, sizeof(global_variable));
  23. return 0;
  24. }
  25. int finsh_var_insert(const char* name, int type)
  26. {
  27. int i, empty;
  28. empty = -1;
  29. for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
  30. {
  31. /* there is a same name variable exist. */
  32. if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
  33. return -1;
  34. if (global_variable[i].type == finsh_type_unknown && empty == -1)
  35. {
  36. empty = i;
  37. }
  38. }
  39. /* there is no empty entry */
  40. if (empty == -1) return -1;
  41. /* insert entry */
  42. strncpy(global_variable[empty].name, name, FINSH_NAME_MAX);
  43. global_variable[empty].type = type;
  44. /* return the offset */
  45. return empty;
  46. }
  47. int finsh_var_delete(const char* name)
  48. {
  49. int i;
  50. for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
  51. {
  52. if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
  53. break;
  54. }
  55. /* can't find variable */
  56. if (i == FINSH_VARIABLE_MAX) return -1;
  57. memset(&global_variable[i], 0, sizeof(struct finsh_var));
  58. return 0;
  59. }
  60. struct finsh_var* finsh_var_lookup(const char* name)
  61. {
  62. int i;
  63. for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
  64. {
  65. if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
  66. break;
  67. }
  68. /* can't find variable */
  69. if (i == FINSH_VARIABLE_MAX) return NULL;
  70. return &global_variable[i];
  71. }
  72. #ifdef RT_USING_HEAP
  73. extern char *strdup(const char *s);
  74. void finsh_sysvar_append(const char* name, u_char type, void* var_addr)
  75. {
  76. /* create a sysvar */
  77. struct finsh_sysvar_item* item;
  78. item = (struct finsh_sysvar_item*) rt_malloc (sizeof(struct finsh_sysvar_item));
  79. if (item != NULL)
  80. {
  81. item->next = NULL;
  82. item->sysvar.name = strdup(name);
  83. item->sysvar.type = type;
  84. item->sysvar.var = var_addr;
  85. if (global_sysvar_list == NULL)
  86. {
  87. global_sysvar_list = item;
  88. }
  89. else
  90. {
  91. item->next = global_sysvar_list;
  92. global_sysvar_list = item;
  93. }
  94. }
  95. }
  96. #endif
  97. struct finsh_sysvar* finsh_sysvar_lookup(const char* name)
  98. {
  99. struct finsh_sysvar* index;
  100. struct finsh_sysvar_item* item;
  101. for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
  102. {
  103. if (strcmp(index->name, name) == 0)
  104. return index;
  105. }
  106. /* find in sysvar list */
  107. item = global_sysvar_list;
  108. while (item != NULL)
  109. {
  110. if (strncmp(item->sysvar.name, name, strlen(name)) == 0)
  111. {
  112. return &(item->sysvar);
  113. }
  114. /* move to next item */
  115. item = item->next;
  116. }
  117. /* can't find variable */
  118. return NULL;
  119. }