1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2010-03-22 Bernard first version 9 * 2012-04-27 Bernard fixed finsh_var_delete issue which 10 * is found by Grissiom. 11 */ 12 #include <finsh.h> 13 #include "finsh_var.h" 14 15 struct finsh_var global_variable[FINSH_VARIABLE_MAX]; 16 struct finsh_sysvar_item* global_sysvar_list; 17 18 int finsh_var_init() 19 { 20 memset(global_variable, 0, sizeof(global_variable)); 21 22 return 0; 23 } 24 25 int finsh_var_insert(const char* name, int type) 26 { 27 int i, empty; 28 29 empty = -1; 30 for (i = 0; i < FINSH_VARIABLE_MAX; i ++) 31 { 32 /* there is a same name variable exist. */ 33 if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0) 34 return -1; 35 36 if (global_variable[i].type == finsh_type_unknown && empty == -1) 37 { 38 empty = i; 39 } 40 } 41 42 /* there is no empty entry */ 43 if (empty == -1) return -1; 44 45 /* insert entry */ 46 strncpy(global_variable[empty].name, name, FINSH_NAME_MAX); 47 global_variable[empty].type = type; 48 49 /* return the offset */ 50 return empty; 51 } 52 53 int finsh_var_delete(const char* name) 54 { 55 int i; 56 57 for (i = 0; i < FINSH_VARIABLE_MAX; i ++) 58 { 59 if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0) 60 break; 61 } 62 63 /* can't find variable */ 64 if (i == FINSH_VARIABLE_MAX) return -1; 65 66 memset(&global_variable[i], 0, sizeof(struct finsh_var)); 67 68 return 0; 69 } 70 71 struct finsh_var* finsh_var_lookup(const char* name) 72 { 73 int i; 74 75 for (i = 0; i < FINSH_VARIABLE_MAX; i ++) 76 { 77 if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0) 78 break; 79 } 80 81 /* can't find variable */ 82 if (i == FINSH_VARIABLE_MAX) return NULL; 83 84 return &global_variable[i]; 85 } 86 87 #ifdef RT_USING_HEAP 88 void finsh_sysvar_append(const char* name, uint8_t type, void* var_addr) 89 { 90 /* create a sysvar */ 91 struct finsh_sysvar_item* item; 92 93 item = (struct finsh_sysvar_item*) rt_malloc (sizeof(struct finsh_sysvar_item)); 94 if (item != NULL) 95 { 96 item->next = NULL; 97 item->sysvar.name = rt_strdup(name); 98 item->sysvar.type = type; 99 item->sysvar.var = var_addr; 100 101 if (global_sysvar_list == NULL) 102 { 103 global_sysvar_list = item; 104 } 105 else 106 { 107 item->next = global_sysvar_list; 108 global_sysvar_list = item; 109 } 110 } 111 } 112 #endif 113 114 struct finsh_sysvar* finsh_sysvar_lookup(const char* name) 115 { 116 struct finsh_sysvar* index; 117 struct finsh_sysvar_item* item; 118 119 for (index = _sysvar_table_begin; 120 index < _sysvar_table_end; 121 FINSH_NEXT_SYSVAR(index)) 122 { 123 if (strcmp(index->name, name) == 0) 124 return index; 125 } 126 127 /* find in sysvar list */ 128 item = global_sysvar_list; 129 while (item != NULL) 130 { 131 if (strncmp(item->sysvar.name, name, strlen(name)) == 0) 132 { 133 return &(item->sysvar); 134 } 135 136 /* move to next item */ 137 item = item->next; 138 } 139 140 /* can't find variable */ 141 return NULL; 142 } 143