/* * 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"//struct arrays, global variables, global variables prefixed with globals, add code readability Struct finsh_var global_ variable[finsh_variable_max];//defines the struct finsh_sysvar_item* global_sysvar_list;//array in finsh.h Global_ Variable initialize Int finsh_var_init () { //use sizeof here (Global_ variable), //instead of finsh_variable_max * sizeof (Finsh_var ) &NBSP;&NBSP;&NBSP;&NBSP;&NBsp; //Add code Readability memset (global_variable, 0, sizeof (global_variable));//general, normal return 0 value, The error returns a negative value, such as-1, but the last use of a macro to define the error code//is not recommended to use the magic number return 0;} Variable var insert function, note the const modifier in front of name, to prevent misoperation, because this series of operations is//based on a unique name value Int finsh_var_insert (const char* Name, int type) {Int i, empty; //empty is used to store the found index value iempty = -1;for ( i = 0; i < finsh_variable_max; i ++) {/* there is a SAME&NBSP;NAME&NBSP;VARIABLE&NBSP;EXIST.&NBSP;*///STRNCMP returns a value of 0 indicating that both strings are identical if (strncmp (global_variable[i].name , name, finsh_name_max) == 0) return -1; //finsh_type_unknown is an enumeration value, defined in the Finsh.h file //&& The purpose of empty is to find the first unused index value to &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; //Why not break here? Break can affect code optimization, especially in the For,while loop //specific optimization knowledge can be consulted the scientific masterpiece "in-depth understanding of computer systems" if (Global_variable[i].type == finsh_type_unknown && empty == -1) {empty = i;}} If the global_variable[i].type != finsh_type_unknown//is an array full, return -1/* there is no empty entry */if (empty == -1) return -1;//insert var to array global_variable/* insert entry *///uses strncpy instead of strcpy to reduce the risk of cross-border arrays strncpy (Global_variable[empty].name, name, finsh_name_max);//Type definition global_variable[empty].type = type;//returns the array subscript/* return the Offset */return empty;} Variable var delete function int finsh_var_delete (const char* name) {int i;for (i = 0; i < finsh_variable_max; i ++) { //strncmp& NBsP;== 0, indicating the name of the two matches, so jump out of the Loop if (strncmp (Global_variable[i].name, name, finsh_name_max) == 0) break;} If I == finsh_variable_max proves that the traversal array cannot find the same name Var, return -1/* can ' t find variable */if (I == finsh_variable_max) return -1;//empty the Var value, all initialized to 0memset (&global_variable[i], 0, sizeof (Struct finsh_var));//Returns a value of 0 indicating okreturn 0;} Find Var function struct finsh_var* finsh_var_lookup (const char* name) {int i;//traversal array 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 ;//returns the struct pointer corresponding to the index value return &global_variable[i];} #ifdef rt_using_heap //If you define the Rt_using_heap//sysvar dependent function void finsh_sysvar_append (const Char* name, u_char type, void* var_addr) {/* create a sysvar *///is defined in FINSH.H struct finsh_sysvar_ item* item;//Request size sizeof (Struct finsh_sysvar_item) memory item = in the memory pool (struct finsh_sysvar_ item*) rt_malloc (sizeof (Struct finsh_sysvar_item));//non-null judgment to prevent pointer error if (item != NULL) { //struct initialization item->next = null; item- >sysvar.name = rt_strdup (name); item->sysvar.type = type;item->sysvar.var = var_addr;//if global_sysvar_list == null, the description is the head pointer if (global_sysvar_list == null) { Global_sysvar_list = item;} else //If non-empty, insert global_sysvar_list, notice that the linked list is the end-to-end {Item->next = global_sysvar_list;global_ Sysvar_list = item;}}} #endif//sysvar Lookup function struct finsh_sysvar* finsh_sysvar_lookup (const char* name) {struct Finsh_sysvar* index;struct finsh_sysvar_item* item;//here _sysvar_table_begin etc in finsh.h defined for (index = _sysvar_table_begin; index < _sysvar_table_end; finsh_next_sysvar (index)) { //the same name, the subscript if (strcmp (index->name, name) == 0) is returned Return index;} /*&NBSP;FIND&NBSP;IN&NBSP;SYSVAR&NBSP;LIST&NBSP;*///operates on the local variable item instead of Global_sysvar_list the global variable directly item = global_sysvar_list;//traversal finds a variable with the same name, and returns 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;}
This article is from the "mountain Ask the Boy" blog, please be sure to keep this source http://linpeng.blog.51cto.com/9779987/1685393
Rt-thread finsh Source Analysis: FINSH_VAR.C