finsh_var.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. */
  14. #include <finsh.h>
  15. #include "finsh_var.h"
  16. struct finsh_var global_variable[FINSH_VARIABLE_MAX];
  17. struct finsh_sysvar_item* global_sysvar_list;
  18. int finsh_var_init()
  19. {
  20. memset(global_variable, 0, sizeof(global_variable));
  21. return 0;
  22. }
  23. int finsh_var_insert(const char* name, int type)
  24. {
  25. int i, empty;
  26. empty = -1;
  27. for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
  28. {
  29. /* there is a same name variable exist. */
  30. if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
  31. return -1;
  32. if (global_variable[i].type == finsh_type_unknown && empty == -1)
  33. {
  34. empty = i;
  35. }
  36. }
  37. /* there is no empty entry */
  38. if (empty == -1) return -1;
  39. /* insert entry */
  40. strncpy(global_variable[empty].name, name, FINSH_NAME_MAX);
  41. global_variable[empty].type = type;
  42. /* return the offset */
  43. return empty;
  44. }
  45. int finsh_var_delete(const char* name)
  46. {
  47. int i;
  48. for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
  49. {
  50. if (strncpy(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
  51. break;
  52. }
  53. /* can't find variable */
  54. if (i == FINSH_VARIABLE_MAX) return -1;
  55. memset(&global_variable[i], 0, sizeof(struct finsh_var));
  56. return 0;
  57. }
  58. struct finsh_var* finsh_var_lookup(const char* name)
  59. {
  60. int i;
  61. for (i = 0; i < FINSH_VARIABLE_MAX; i ++)
  62. {
  63. if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)
  64. break;
  65. }
  66. /* can't find variable */
  67. if (i == FINSH_VARIABLE_MAX) return NULL;
  68. return &global_variable[i];
  69. }
  70. #ifdef RT_USING_HEAP
  71. extern char *strdup(const char *s);
  72. void finsh_sysvar_append(const char* name, u_char type, void* var_addr)
  73. {
  74. /* create a sysvar */
  75. struct finsh_sysvar_item* item;
  76. item = (struct finsh_sysvar_item*) rt_malloc (sizeof(struct finsh_sysvar_item));
  77. if (item != NULL)
  78. {
  79. item->next = NULL;
  80. item->sysvar.name = strdup(name);
  81. item->sysvar.type = type;
  82. item->sysvar.var = var_addr;
  83. if (global_sysvar_list == NULL)
  84. {
  85. global_sysvar_list = item;
  86. }
  87. else
  88. {
  89. item->next = global_sysvar_list;
  90. global_sysvar_list->next = item;
  91. }
  92. }
  93. }
  94. #endif
  95. struct finsh_sysvar* finsh_sysvar_lookup(const char* name)
  96. {
  97. struct finsh_sysvar* index;
  98. struct finsh_sysvar_item* item;
  99. for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
  100. {
  101. if (strcmp(index->name, name) == 0)
  102. return index;
  103. }
  104. /* find in sysvar list */
  105. item = global_sysvar_list;
  106. while (item != NULL)
  107. {
  108. if (strncmp(item->sysvar.name, name, strlen(name)) == 0)
  109. {
  110. return &(item->sysvar);
  111. }
  112. /* move to next item */
  113. item = item->next;
  114. }
  115. /* can't find variable */
  116. return NULL;
  117. }