標籤:rt-thread finsh shell
/* * Variable implementation in finsh shell. * * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team * * This file is part of RT-Thread (http://www.rt-thread.org) * Maintainer: bernard.xiong <bernard.xiong at gmail.com> * * All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * Change Logs: * Date Author Notes * 2010-03-22 Bernard first version * 2012-04-27 Bernard fixed finsh_var_delete issue which * is found by Grissiom. */#include <finsh.h>#include "finsh_var.h"//結構體數組,全域變數,全域變數加首碼global,增加代碼可讀性struct finsh_var global_variable[FINSH_VARIABLE_MAX];//在finsh.h中定義struct finsh_sysvar_item* global_sysvar_list;//數組global_variable初始化int finsh_var_init(){ //此處使用sizeof(global_variable), //而不是FINSH_VARIABLE_MAX * sizeof(finsh_var) //增加代碼可讀性memset(global_variable, 0, sizeof(global_variable));//一般情況,正常返回0值,錯誤返回負值,如-1,但最後使用宏定義錯誤碼//不推薦使用魔數return 0;}//變數var插入函數,注意name前面const修飾符,用於防止誤操作,因為這一系列操作都是//根據唯一的name值進行int finsh_var_insert(const char* name, int type){int i, empty; //empty用於儲存找到的索引值iempty = -1;for (i = 0; i < FINSH_VARIABLE_MAX; i ++){/* there is a same name variable exist. *///strncmp返回0值表示雙方字串相同if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)return -1; //finsh_type_unknown是枚舉值,在finsh.h檔案中定義 //&&empty的目的是找到第一個未使用的索引值即可 //此處為什麼不用break?break會影響代碼最佳化,特別是在for,while迴圈中 //具體最佳化知識可以查閱科學巨著《深入理解電腦系統》if (global_variable[i].type == finsh_type_unknown && empty == -1){empty = i;}}//如果 global_variable[i].type != finsh_type_unknown//即數組已滿,返回-1/* there is no empty entry */if (empty == -1) return -1;//插入var到數組global_variable/* insert entry *///使用strncpy而不是strcpy,減少數組越界風險strncpy(global_variable[empty].name, name, FINSH_NAME_MAX);//類型定義global_variable[empty].type = type;//返回數組下標/* return the offset */return empty;}//變數var刪除函數int finsh_var_delete(const char* name){int i;for (i = 0; i < FINSH_VARIABLE_MAX; i ++){ //strncmp == 0,表示雙方name匹配,故跳出迴圈if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)break;}//如果i == FINSH_VARIABLE_MAX,證明遍曆數組都找不到同名var,返回-1/* can‘t find variable */if (i == FINSH_VARIABLE_MAX) return -1;//清空該var值,全部初始化為0memset(&global_variable[i], 0, sizeof(struct finsh_var));//返回0值,表示OKreturn 0;}//尋找var函數struct finsh_var* finsh_var_lookup(const char* name){int i;//遍曆數組for (i = 0; i < FINSH_VARIABLE_MAX; i ++){if (strncmp(global_variable[i].name, name, FINSH_NAME_MAX) == 0)break;}/* can‘t find variable */if (i == FINSH_VARIABLE_MAX) return NULL;//返回索引值對應的結構體指標return &global_variable[i];}#ifdef RT_USING_HEAP //如果定義RT_USING_HEAP//sysvar附屬函數void finsh_sysvar_append(const char* name, u_char type, void* var_addr){/* create a sysvar *///在finsh.h中定義struct finsh_sysvar_item* item;//在記憶體池中申請大小sizeof(struct finsh_sysvar_item)的記憶體item = (struct finsh_sysvar_item*) rt_malloc (sizeof(struct finsh_sysvar_item));//非空判斷,防止指標錯誤if (item != NULL){ //結構體初始化item->next = NULL; item->sysvar.name = rt_strdup(name);item->sysvar.type = type;item->sysvar.var = var_addr;//如果global_sysvar_list == NULL,說明是頭指標if (global_sysvar_list == NULL){global_sysvar_list = item;}else //如果非空,插入global_sysvar_list,注意該鏈表是首尾相連的{item->next = global_sysvar_list;global_sysvar_list = item;}}}#endif//sysvar尋找函數struct finsh_sysvar* finsh_sysvar_lookup(const char* name){struct finsh_sysvar* index;struct finsh_sysvar_item* item;//此處_sysvar_table_begin等在finsh.h中定義for (index = _sysvar_table_begin; index < _sysvar_table_end; FINSH_NEXT_SYSVAR(index)){ //同名,則返回下標if (strcmp(index->name, name) == 0)return index;}/* find in sysvar list *///對局部變數item進行操作,而不是對全域變數global_sysvar_list直接操作item = global_sysvar_list;//遍曆尋找同名變數,找到則返回while (item != NULL){if (strncmp(item->sysvar.name, name, strlen(name)) == 0){return &(item->sysvar);}/* move to next item */item = item->next;}/* can‘t find variable */return NULL;}
本文出自 “山下問童子” 部落格,請務必保留此出處http://linpeng.blog.51cto.com/9779987/1685393
RT-Thread finsh源碼分析: finsh_var.c