在uboot中添加解析ini配置文件的命令

1、在u-boot-2010.06/include/configs目录的xxx.h(xxx是board,如hi3520d.h)里面添加如下宏定义:
     #define CONFIG_CMD_INI
2、 在u-boot-2010.06/common目录的Makefile里面添加如下定义:
<pre name="code" class="html">     COBJS-$(CONFIG_CMD_INI) += cmd_ini.o
 
3、 在u-boot-2010.06/common目录下添加一个文件cmd_ini.c,代码如下:
#include <common.h>
#include <command.h>
#include <environment.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <netdev.h>

#ifdef CONFIG_INI_MAX_LINE
#define MAX_LINE CONFIG_INI_MAX_LINE
#else
#define MAX_LINE 200
#endif

#ifdef CONFIG_INI_MAX_SECTION
#define MAX_SECTION CONFIG_INI_MAX_SECTION
#else
#define MAX_SECTION 50
#endif

#ifdef CONFIG_INI_MAX_NAME
#define MAX_NAME CONFIG_INI_MAX_NAME
#else
#define MAX_NAME 50
#endif

/* Strip whitespace chars off end of given string, in place. Return s. */
static char *rstrip(char *s)
{
	char *p = s + strlen(s);

	while (p > s && isspace(*--p))
		*p = '\0';
	return s;
}

/* Return pointer to first non-whitespace char in given string. */
static char *lskip(const char *s)
{
	while (*s && isspace(*s))
		s++;
	return (char *)s;
}

/* Return pointer to first char c or ';' comment in given string, or pointer to
   null at end of string if neither found. ';' must be prefixed by a whitespace
   character to register as a comment. */
static char *find_char_or_comment(const char *s, char c)
{
	int was_whitespace = 0;

	while (*s && *s != c && !(was_whitespace && *s == ';')) {
		was_whitespace = isspace(*s);
		s++;
	}
	return (char *)s;
}

/* Version of strncpy that ensures dest (size bytes) is null-terminated. */
static char *strncpy0(char *dest, const char *src, size_t size)
{
	strncpy(dest, src, size);
	dest[size - 1] = '\0';
	return dest;
}

/* Emulate the behavior of fgets but on memory */
static char *memgets(char *str, int num, char **mem, size_t *memsize)
{
	char *end;
	int len;
	int newline = 1;

	end = memchr(*mem, '\n', *memsize);
	if (end == NULL) {
		if (*memsize == 0)
			return NULL;
		end = *mem + *memsize;
		newline = 0;
	}
	len = min((end - *mem) + newline, num);
	memcpy(str, *mem, len);
	if (len < num)
		str[len] = '\0';

	/* prepare the mem vars for the next call */
	*memsize -= (end - *mem) + newline;
	*mem += (end - *mem) + newline;

	return str;
}

/* Parse given INI-style file. May have [section]s, name=value pairs
   (whitespace stripped), and comments starting with ';' (semicolon). Section
   is "" if name=value pair parsed before any section heading. name:value
   pairs are also supported as a concession to Python's ConfigParser.

   For each name=value pair parsed, call handler function with given user
   pointer as well as section, name, and value (data only valid for duration
   of handler call). Handler should return nonzero on success, zero on error.

   Returns 0 on success, line number of first error on parse error (doesn't
   stop on first error).
*/

static int ini_parse(char *filestart, size_t filelen,
	int (*handler)(void *, char *, char *, char *),	void *user)
{
	/* Uses a fair bit of stack (use heap instead if you need to) */
	char line[MAX_LINE];
	char section[MAX_SECTION] = "";
	char prev_name[MAX_NAME] = "";

	char *curmem = filestart;
	char *start;
	char *end;
	char *name;
	char *value;
	size_t memleft = filelen;
	int lineno = 0;
	int error = 0;

	/* Scan through file line by line */
	while (memgets(line, sizeof(line), &curmem, &memleft) != NULL) {
		lineno++;
		start = lskip(rstrip(line));
		
loop:
		if (*start && *start != ';' && *start != '[') {
			/* Not a comment, must be a name[=:]value pair */

			end = find_char_or_comment(start, '=');
			if (*end != '=')
				end = find_char_or_comment(start, ':');
			if (*end == '=' || *end == ':') {
				*end = '\0';
				name = rstrip(start);
				value = lskip(end + 1);
				end = find_char_or_comment(value, ';');
				if(*end == ';'){								//batch cmd
					*end = '\0';
					end = find_char_or_comment(value, '\0');
					rstrip(value);
					/* Strip double-quotes */
					if (value[0] == '"' &&
				   		value[strlen(value)-1] == '"') {
						value[strlen(value)-1] = '\0';
 							value += 1;
 					}

					/*
					 * Valid name[=:]value pair found, call handler
				 	*/
					strncpy0(prev_name, name, sizeof(prev_name));
					if (handler(user, section, name, value) &&
				  	   !error){		
						start = ++end;
						goto loop;
					}
					else
						error = lineno;
				}
				else{
					end = find_char_or_comment(value, '\0');
					if (*end == ';')
						*end = '\0';
					rstrip(value);
					/* Strip double-quotes */
					if (value[0] == '"' &&
				   		value[strlen(value)-1] == '"') {
						value[strlen(value)-1] = '\0';
 							value += 1;
 					}

					/*
					 * Valid name[=:]value pair found, call handler
				 	*/
					strncpy0(prev_name, name, sizeof(prev_name));
					if (!handler(user, section, name, value) &&
				  	   !error)
							error = lineno;
						
				}
			} 
			else if (!error)
				/* No '=' or ':' found on name[=:]value line */
				error = lineno;
		}
		else if (*start == ';' || *start == '#') {
			/*
			 * Per Python ConfigParser, allow '#' comments at start
			 * of line
			 */
		}
#if CONFIG_INI_ALLOW_MULTILINE
		else if (*prev_name && *start && start > line) {
			/*
			 * Non-blank line with leading whitespace, treat as
			 * continuation of previous name's value (as per Python
			 * ConfigParser).
			 */
			if (!handler(user, section, prev_name, start) && !error)
				error = lineno;
		}
#endif
		else if (*start == '[') {
			/* A "[section]" line */
			end = find_char_or_comment(start + 1, ']');
			if (*end == ']') {
				*end = '\0';
				strncpy0(section, start + 1, sizeof(section));
				*prev_name = '\0';
			} else if (!error) {
				/* No ']' found on section line */
				error = lineno;
			}
		} 
		if(error)
			return error;
	}
	return error;
}

static int ini_handler(void *user, char *section, char *name, char *value)
{
	
	char *requested_section = (char *)user;
	int i, j;
	
#ifdef CONFIG_INI_CASE_INSENSITIVE
	
	for (i = 0; i < strlen(requested_section); i++)
		requested_section[i] = tolower(requested_section[i]);
	for (i = 0; i < strlen(section); i++)
		section[i] = tolower(section[i]);
#endif

	if (!strcmp(section, requested_section)) {
#ifdef CONFIG_INI_CASE_INSENSITIVE
		for (i = 0; i < strlen(name); i++)
			name[i] = tolower(name[i]);
		for (i = 0; i < strlen(value); i++)
			value[i] = tolower(value[i]);
#endif
		//setenv(name, value);
		char buf[500];
		sprintf(buf, "%s %s", name, value);
		printf("%s\n", buf);
		for(i=0; i<10; i++){
			if(run_command(buf, 0) == -1){		
				printf("error, try again After 1 seconds\n");
				udelay(1000000);
				if (ctrlc()) {
					puts ("\nAbort\n");
					return 0;
				}
				if(i == 9)
					return 0;
				continue;
			}
			break;
		}
	}

	/* success */
	return 1;
}

static int do_ini(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	const char *section;
	char *file_address;
	size_t file_size;

	if (argc == 1)
		return CMD_RET_USAGE;

	section = argv[1];
	file_address = (char *)simple_strtoul(
		argc < 3 ? getenv("loadaddr") : argv[2], NULL, 16);
	file_size = (size_t)simple_strtoul(
		argc < 4 ? getenv("filesize") : argv[3], NULL, 16);

	return ini_parse(file_address, file_size, ini_handler, (void *)section);
}

U_BOOT_CMD(
	ini, 4, 0, do_ini,
	"parse an ini file in memory and merge the specified section into the env",
	"section [[file-address] file-size]"
);
</span>


 
说明:1、INI文件的格式(举一个例子cmd.ini):
;this is a ini file //;为注释
[burn] //[]里面为段名
tftp=0x82000000 FWHI2104HF_20151106_DVR_R5104-AHD_2_2_8_0A_413221.flash;sf=probe 0;
sf=erase 0x80000 0xF80000
sf=write 0x82000000 0x80000 0xF80000
reset=
[setting]
setenv=serverip 192.168.1.68 //name=value
setenv=ipaddr 192.168.1.10 //"="号可以用":"代替,value值也可以用双引号引起来
save= //只有一个参数时也要带等号,右边空着

2、使用方法:
1)使用tftp命令把ini配置文件现在到板子的虚拟内存0x82000000处
tftp 0x82000000 cmd.ini
2)使用ini命令执行特定段里面的命令
ini setting 0x82000000

3、命令格式错误或是不正确的命令等等原因导致命令不能正常进行时会每隔1秒重新执行一次,持续10次, 10次执行还不能正确执行会异常退出,不会执行下调命令。中途不想继续执行可以ctrl+c终止。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
纯c读写ini配置文件 用c/c++读写ini配置文件有不少第三方的开源库,如iniparser、libini、rwini、UltraLightINIParser等,但都不理想,往往代码较大、功能较弱、 接口使用不方便。尤其在大小写处理、前后空格、各种注释、跨平台换行符支持、带引号字符串处理、无section操作、原格式保持等方面存在问题。 现将本人精心制作的ini读写程序源码奉献给大家,纯c编写,简洁好用。支持windows和linux。 主要特点: 1、支持;和#注释符号,支持行尾注释。 2、支持带引号'或"成对匹配的字符串,提取时自动去引号。引号可带其它引号或;#注释符。 3、支持无section或空section(名称为空)。 4、支持10、16、8进制数,0x开头为16进制数,0开头为8进制。 5、支持section、key或=号前后带空格。 6、支持\n、\r、\r\n或\n\r换行格式。 7、不区分section、key大小写,但写入时以新串为准,并保持其大小写。 8、新增数据时,若section存在则在该节最后一个有效数据后添加,否则在文件尾部添加。 9、支持指定key所在整行删除,即删除该键值,包括注释。 10、可自动跳过格式错误行,修改时仍然保留。 11、修改时保留原注释:包括整行注释、行尾注释(包括前面空格)。 12、修改时保留原空行。以上三点主要是尽量保留原格式。 不足之处: 1、不支持单key多value(逗号分割),只能一次性提取后自行处理。 2、不支持同名重复section和key。(重复section可视为错误,重复key则可能造成分歧) 3、不能提取所有section或key名称。 使用只需两个文件inirw.h、inirw.c,另有测试程序和工程文件,支持windows和linux。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值