SmartAudio/lichee/brandy/pack_tools/script/script.c

983 lines
20 KiB
C
Raw Normal View History

2018-07-13 01:31:50 +00:00
// script.cpp : Defines the entry point for the console application.
//
#include <malloc.h>
#include <string.h>
#include "types.h"
#include "script.h"
#include <ctype.h>
#include <unistd.h>
//__asm__(".symver memcpy ,memcpy@GLIBC_2.2.5");
int parser_script(void *pbuf, int script_len, FILE *hfile);
//------------------------------------------------------------------------------------------------------------
//
// <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>
//
//
// <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>
//
//
// <20><><EFBFBD><EFBFBD>ֵ
//
//
// <20><><EFBFBD><EFBFBD>
// <20><>
//
//------------------------------------------------------------------------------------------------------------
int IsFullName(const char *FilePath)
{
if ( FilePath[0] == '/')
{
return 1;
}
else
{
return 0;
}
}
//------------------------------------------------------------------------------------------------------------
//
// <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>
//
//
// <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>
//
//
// <20><><EFBFBD><EFBFBD>ֵ
//
//
// <20><><EFBFBD><EFBFBD>
// <20><>
//
//------------------------------------------------------------------------------------------------------------
void GetFullPath(char *dName, const char *sName)
{
char Buffer[MAX_PATH];
if(IsFullName(sName))
{
strcpy(dName, sName);
return ;
}
/* Get the current working directory: */
if(getcwd(Buffer, MAX_PATH ) == NULL)
{
perror( "_getcwd error" );
return ;
}
sprintf(dName, "%s/%s", Buffer, sName);
}
//------------------------------------------------------------------------------------------------------------
//
// <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>
//
//
// <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>
//
//
// <20><><EFBFBD><EFBFBD>ֵ
//
//
// <20><><EFBFBD><EFBFBD>
// <20><>
//
//------------------------------------------------------------------------------------------------------------
void Usage(void)
{
printf("\n");
printf("Usage:\n");
printf("script.exe script file path para file path\n\n");
}
static int _get_str_length(char *str)
{
int length = 0;
while(str[length])
{
length ++;
}
return length;
}
//------------------------------------------------------------------------------------------------------------
//
// <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>
//
//
// <20><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>
//
//
// <20><><EFBFBD><EFBFBD>ֵ
//
//
// <20><><EFBFBD><EFBFBD>
// <20><>
//
//------------------------------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
//char str1[] = "D:\\winners\\eBase\\eGON\\eGON2\\workspace\\sun_20\\eFex\\sys_config1.fex";
//char str2[] = "D:\\winners\\eBase\\eGON\\eGON2\\workspace\\sun_20\\eFex\\sys_config.bin";
char str1[] = "sys_config.fex";
char str2[] = "sys_config.bin";
char src[MAX_PATH];
char dest[MAX_PATH];
FILE *dst_file = NULL;
FILE *script_file = NULL;
int ret = -1, src_length;
char *script_addr = NULL;
int script_len, i;
printf("argc = %d\n", argc);
for(i=1;i<argc;i++)
{
if(argv[i] == NULL)
{
printf("script error: input source file name is empty\n");
return __LINE__;
}
printf("input name %s\n", argv[i]);
memset(src, 0, sizeof(MAX_PATH));
memset(dest, 0, sizeof(MAX_PATH));
GetFullPath(src, argv[i]);
src_length = _get_str_length(src);
memcpy(dest, src, src_length);
dest[src_length - 0] = NULL;
dest[src_length - 1] = 'n';
dest[src_length - 2] = 'i';
dest[src_length - 3] = 'b';
printf("Script %d source file Path=%s\n", i, src);
printf("Script %d bin file Path=%s\n", i, dest);
//<2F>򿪽ű<F2BFAABD><C5B1>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>򲻿<EFBFBD><F2B2BBBF><EFBFBD>ʧ<EFBFBD><CAA7>
script_file = fopen(src, "rb");
if(!script_file)
{
printf("unable to open script file %s\n", src);
goto _err_out;
}
//<2F><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>򲻿<EFBFBD><F2B2BBBF><EFBFBD>ʧ<EFBFBD><CAA7>
dst_file = fopen(dest, "wb");
if(!dst_file)
{
printf("unable to open dest file\n");
goto _err_out;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD>Ȼ<EFBFBD>ȡ<EFBFBD>ű<EFBFBD><C5B1>ij<EFBFBD><C4B3><EFBFBD>
fseek(script_file, 0, SEEK_END);
script_len = ftell(script_file);
fseek(script_file, 0, SEEK_SET);
//<2F><><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
script_addr = (char *)malloc(script_len+1);
memset(script_addr,0,script_len+1);
if(!script_addr)
{
printf("unable to malloc memory for script\n");
goto _err_out;
}
fread(script_addr, 1, script_len, script_file);
fclose(script_file);
script_file = NULL;
//<2F>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><E4A3AC>Ϊ<EFBFBD><CEAA>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEA3AC>׼<EFBFBD>ǣ<EFBFBD><C7A3>ڻس<DABB><D8B3><EFBFBD><EFBFBD>ź<EFBFBD><C5BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>
ret = parser_script(script_addr, script_len, dst_file);
if(ret)
{
printf("error1\n");
goto _err_out;
}
free(script_addr);
script_addr = NULL;
fclose(dst_file);
dst_file = NULL;
printf("parser %d file ok\n", i);
}
_err_out:
//<2F>˳<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
if(script_addr)
{
free(script_addr);
}
if(script_file)
{
fclose(script_file);
script_file = NULL;
}
if(dst_file)
{
fclose(dst_file);
dst_file = NULL;
}
return ret;
}
#define THIS_LINE_NULL (0)
#define THIS_LINE_MAINKEY (1)
#define THIS_LINE_SUBKEY (2)
#define THIS_LINE_ERROR (-1)
//<2F>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ǰ<EFBFBD>еij<D0B5><C4B3>ȣ<EFBFBD><C8A3><EFBFBD>ָ<EFBFBD><EFBFBD>ص<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/********************************************
* flag = 0 //<2F><>ǰ<EFBFBD><C7B0>ע<EFBFBD><D7A2><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* = 1 //<2F><>ǰ<EFBFBD><C7B0><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>
* = 2 //<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶε<D6B6><CEB5><EFBFBD>һ<EFBFBD><D2BB>
* = -1 //<2F><>ǰ<EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD>Ϲ<CFB9><E6B7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*********************************************/
static int _get_line_status(char *daddr, int *flag, int last_len)
{
char *src;
int len;
char ch;
src = daddr;
ch = *src++;
last_len --;
switch(ch)
{
case ';': //ע<><D7A2><EFBFBD><EFBFBD>
case 0x0D: //<2F>س<EFBFBD><D8B3><EFBFBD>
{
*flag = THIS_LINE_NULL;
}
break;
case '[': //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
*flag = THIS_LINE_MAINKEY;
break;
}
default: //<2F>Ӽ<EFBFBD><D3BC><EFBFBD>
{
*flag = THIS_LINE_SUBKEY;
break;
}
}
len = 1;
ch = *src++;
while((ch != 0x0A) && (last_len > len)) //ֻҪ<D6BB><D2AA><EFBFBD>ǻ<EFBFBD><C7BB>з<EFBFBD><D0B7>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
ch = *src++;
len ++;
if(len >= 512)
{
*flag = THIS_LINE_ERROR;
return 0;
}
}
return len + 1;
}
//<2F><><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static int _fill_line_mainkey(char *pbuf, script_item_t *item)
{
char *src;
char ch, i;
i = 0;
src = pbuf + 1; //<2F><><EFBFBD><EFBFBD> <20><>
ch = *src++;
while(']' != ch)
{
item->item_name[i] = ch;
i++;
ch = *src++;
if(i >= ITEM_MAIN_NAME_MAX)
{
item->item_name[i-1] = 0;
break;
}
}
return 0;
}
static int _get_item_value(char *pbuf, char *name, char *value)
{
char *src, *dstname, *dstvalue;
int len;
src = pbuf;
dstname = name;
dstvalue = value;
len = 0;
//<2F><><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ĺϷ<C4BA><CFB7><EFBFBD>
while(1)
{
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD><D5B8><EFBFBD><EFBFBD><EFBFBD>TAB<41><42><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>ָ<EFBFBD>룬ֱ<EBA3AC><D6B1><EFBFBD>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ϸ<EFBFBD><CFB7>ַ<EFBFBD>Ϊֹ
if((*src == ' ') || (*src == 0x09))
{
src ++;
}
//<2F><><EFBFBD><EFBFBD>ʲô<CAB2><C3B4>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
else if((*src == 0x0D) || (*src == 0x0A))
{
dstname[0] = '\0';
dstvalue[0] = '\0';
return 0;
}
else
{
break;
}
}
//<2F>Ѿ<EFBFBD><D1BE>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ϸ<EFBFBD><CFB7>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD>ֱ<EFBFBD><D6B1>Ѱ<EFBFBD>ҵ<EFBFBD><D2B5>Ⱥţ<C8BA>ȥ<EFBFBD><C8A5>β<EFBFBD><CEB2><EFBFBD>Ŀո<C4BF><D5B8><EFBFBD><EFBFBD><EFBFBD>TAB
while(*src != '=')
{
dstname[len ++] = *src;
src ++;
if(len >= 31)
{
dstname[len] = '\0';
break;
}
}
while(1)
{
len --;
if((dstname[len] == ' ') || (dstname[len] == 0x09))
{
dstname[len] = '\0';
}
else
{
dstname[len + 1] = '\0';
break;
}
}
while(*src != '=')
{
src ++;
}
src++;
len = 0;
//<2F><><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ĺϷ<C4BA><CFB7><EFBFBD>
while(1)
{
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD><D5B8><EFBFBD><EFBFBD><EFBFBD>TAB<41><42><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>ָ<EFBFBD>룬ֱ<EBA3AC><D6B1><EFBFBD>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ϸ<EFBFBD><CFB7>ַ<EFBFBD>Ϊֹ
if((*src == ' ') || (*src == 0x09))
{
src ++;
}
//<2F><><EFBFBD><EFBFBD>ʲô<CAB2><C3B4>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
else if((*src == 0x0D) || (*src == 0x0A))
{
dstvalue[0] = '\0';
return 0;
}
else
{
break;
}
}
//<2F>Ѿ<EFBFBD><D1BE>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ϸ<EFBFBD><CFB7>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD>ֱ<EFBFBD><D6B1>Ѱ<EFBFBD>ҵ<EFBFBD><D2B5>Ⱥţ<C8BA>ȥ<EFBFBD><C8A5>β<EFBFBD><CEB2><EFBFBD>Ŀո<C4BF><D5B8><EFBFBD><EFBFBD><EFBFBD>TAB
while((*src != 0x0D) && (*src != 0x0A))
{
dstvalue[len ++] = *src;
src ++;
if(len >= 127)
{
dstvalue[len] = '\0';
break;
}
}
while(1)
{
len --;
if((dstvalue[len] == ' ') || (dstvalue[len] == 0x09))
{
dstvalue[len] = '\0';
}
else
{
dstvalue[len + 1] = '\0';
break;
}
}
return 0;
}
//<2F>˺<EFBFBD><CBBA><EFBFBD>ת<EFBFBD><D7AA><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ݳ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>10<31><30><EFBFBD>ƺ<EFBFBD>16<31><36><EFBFBD><EFBFBD>
//ת<><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>value<75>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>־ת<D6BE><D7AA><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
static int _get_str2int(char *saddr, int value[] )
{
char *src;
char off, ch;
unsigned int tmp_value = 0;
int data_count, i;
char tmp_str[128];
int sign = 1;
data_count = 2;
src = saddr;
off = 0; //0<><30><EFBFBD><EFBFBD>10<31><30><EFBFBD>ƣ<EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD>16<31><36><EFBFBD><EFBFBD>
if(!strncmp(src, "port:", 5))
{
if((src[5] == 'P') || (src[5] == 'p'))
{
off = 3; //<2F><>ʾ<EFBFBD>Ƕ˿<C7B6><CBBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
src += 6;
}
}
else if(!strncmp(src, "string:", 7))
{
off = 0;
src += 7;
}
else if(src[0] == '"')
{
off = 5;
src += 1;
}
else if((src[0] == '0') && ((src[1] == 'x') || (src[1] == 'X'))) //ʮ<><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
src += 2;
off = 2;
}
else if((src[0] >= '0') && (src[0] <= '9')) //ʮ<><CAAE><EFBFBD><EFBFBD>
{
off = 1;
}
else if(((src[1] >= '0') && (src[1] <= '9')) && (src[0] == '-'))
{
src ++;
off = 1;
sign = -1;
}
else if(src[0] == '\0')
{
src++;
off = 4;
}
//<2F><>ʾ<EFBFBD><CABE><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
if(off == 0)
{
data_count = 0;
while(src[data_count] != '\0')
{
data_count ++;
if(data_count > 127)
{
break;
}
}
if(data_count & 0x03) //Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD>ֽڶ<D6BD><DAB6><EFBFBD>
{
data_count = (data_count & (~0x03)) + 4;
}
else
{
data_count = data_count + 4;
}
value[0] = data_count>>2;
if(saddr != src)
{
value[1] = 0;
}
else
{
value[1] = 1;
}
return DATA_TYPE_STRING;
}
else if(off == 5) //<2F><>ʾ<EFBFBD><CABE><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
{
data_count = 0;
while(src[data_count] != '"')
{
data_count ++;
if(data_count > 127)
{
break;
}
}
src[data_count] = '\0';
if(data_count & 0x03) //Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD>ֽڶ<D6BD><DAB6><EFBFBD>
{
data_count = (data_count & (~0x03)) + 4;
}
else
{
data_count = data_count + 4;
}
value[0] = data_count>>2;
value[1] = 5;
return DATA_TYPE_STRING;
}
else if(off == 1)
{
while(*src != '\0')
{
if((*src >= '0') && (*src <= '9'))
{
tmp_value = tmp_value * 10 + (*src - '0');
src ++;
}
else
{
return -1;
}
}
value[0] = tmp_value * sign;
return DATA_TYPE_SINGLE_WORD;
}
else if(off == 2)
{
while(*src != '\0')
{
if((*src >= '0') && (*src <= '9'))
{
tmp_value = tmp_value * 16 + (*src - '0');
src ++;
}
else if((*src >= 'A') && (*src <= 'F'))
{
tmp_value = tmp_value * 16 + (*src - 'A' + 10);
src ++;
}
else if((*src >= 'a') && (*src <= 'f'))
{
tmp_value = tmp_value * 16 + (*src - 'a' + 10);
src ++;
}
else
{
return -1;
}
}
value[0] = tmp_value;
return DATA_TYPE_SINGLE_WORD;
}
else if(off == 3) //<2F><>ʾ<EFBFBD><CABE>GPIO<49><4F>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ձ<EFBFBD>׼<EFBFBD><D7BC>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> <20>˿ڱ<CBBF><DAB1>ţ<EFBFBD><C5A3>˿<EFBFBD><CBBF><EFBFBD><EFBFBD>ű<EFBFBD><C5B1>ţ<EFBFBD>mult-driving<6E><67>pull<6C><6C>driving-level<65><6C>data<74><61>6<EFBFBD><36>word
{
//<2F><>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int tmp_flag = 0;
ch = *src++;
if((ch == 'o') || (ch == 'O')) //<2F><>1<EFBFBD><31><EFBFBD><EFBFBD>A<EFBFBD>飬2<E9A3AC><32><EFBFBD><EFBFBD>B<EFBFBD><EFBFBD><E9A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD>0xffff<66><66><EFBFBD><EFBFBD>POWER<45><52><EFBFBD><EFBFBD>
{
ch = src[0];
if((ch == 'w') || (ch == 'W'))
{
ch = src[1];
if((ch == 'e') || (ch == 'E'))
{
ch = src[2];
if((ch == 'r') || (ch == 'R'))
{
//ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>POWER<45><52><EFBFBD><EFBFBD>
value[0] = 0xffff;
src += 3;
tmp_flag = 1;
}
}
}
}
if(!tmp_flag)
{
if((ch >= 'A') && (ch <= 'Z'))
{
value[0] = ch - 'A' + 1;
}
else if((ch >= 'a') && (ch <= 'z'))
{
value[0] = ch - 'a' + 1;
}
else
{
return -1;
}
}
//<2F><>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><E6B1BE><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
ch = *src++;
tmp_value = 0;
while(ch != '<')
{
if((ch >= '0') && (ch <= '9'))
{
tmp_value = tmp_value * 10 + (ch - '0');
ch = *src++;
}
else if(ch == 0)
{
src --;
break;
}
else
{
return -1;
}
}
value[1] = tmp_value;
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еļ<D0B5><C4BC><EFBFBD><EFBFBD><EFBFBD>
ch = *src++;
while(ch != '\0')
{
i = 0;
memset(tmp_str, 0, sizeof(tmp_str));
while(ch != '>') //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if((ch >= 'A') && (ch <= 'Z'))
{
ch += 'a' - 'A';
}
tmp_str[i++] = ch;
ch = *src++;
}
tmp_str[i] = '\0';
//<2F>Ƚ<EFBFBD><C8BD>ַ<EFBFBD><D6B7><EFBFBD>
if(!strcmp(tmp_str, "default"))
{
value[data_count] = -1;
}
else if(!strcmp(tmp_str, "none"))
{
value[data_count] = -1;
}
else if(!strcmp(tmp_str, "null"))
{
value[data_count] = -1;
}
else if(!strcmp(tmp_str, "-1"))
{
value[data_count] = -1;
}
else
{
i = 0;
ch = tmp_str[i++];
tmp_value = 0;
if(ch == '-')
{
sign = -1;
ch = tmp_str[i++];
}
while(ch != '\0')
{
if((ch >= '0') && (ch <= '9'))
{
tmp_value = tmp_value * 10 + (ch - '0');
}
else
{
return -1;
}
ch = tmp_str[i++];
}
value[data_count] = tmp_value * sign;
}
data_count ++;
ch = *src++;
if(ch == '<')
{
ch = *src++;
}
else if(ch == '\0')
{
;
}
else
{
return -1;
}
}
switch(data_count)
{
case 3:
value[3] = -1; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case 4:
value[4] = -1; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case 5:
value[5] = -1; // <20><><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>
case 6:
break;
default:
return -1;
}
return DATA_TYPE_GPIO;
}
else if(off == 4)
{
value[0] = 4>>2;
return DATA_EMPTY;
}
else
{
return -1;
}
}
u32 randto1k(u32 num)
{
if(num % 1024)
{
printf("update_fdt: num %d randto1k\n", num);
return ((num+1023)/1024 * 1024);
}
else
{
return num;
}
}
//<2F><EFBFBD><E3B7A8>һ<EFBFBD><D2BB>һ<EFBFBD>еIJ<D0B5><C4B2><EFBFBD>
// <20><><EFBFBD>ҵ<EFBFBD>ð<EFBFBD>ţ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ע<EFBFBD><D7A2><EFBFBD>У<EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD>ҵ<EFBFBD><D2B5>س<EFBFBD><D8B3><EFBFBD><EFBFBD>з<EFBFBD><D0B7>ţ<EFBFBD><C5A3>ҵ<EFBFBD><D2B5><EFBFBD>һ<EFBFBD><D2BB>ȥ (<28><EFBFBD><EFB7A8><EFBFBD><EFBFBD>)
// <20><><EFBFBD>ҵ<EFBFBD>'['(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<29><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><>ΪA<CEAA><41><EFBFBD><EFBFBD>)<29><><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
// ֱ<><D6B1><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(B<><42><EFBFBD><EFBFBD>)<29><>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD>ΪA<CEAA><41><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><>A<EFBFBD><41><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD>մӼ<D5B4><D3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>
// <20><>¼A<C2BC><41><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>(<28>Ӽ<EFBFBD><D3BC>ĸ<EFBFBD><C4B8><EFBFBD>)<29><><EFBFBD><EFBFBD>¼A<C2BC><41><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>е<EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>
#define TIEM_MAIN_MAX 256
int parser_script(void *pbuf, int script_len, FILE *hfile)
{
int ret = -1;
char *src, *dest = NULL, *tmp_dest;
int *dest_data = NULL, *tmp_dest_data, dest_data_index;
int line_len, line_status;
int new_main_key_flag = 0, sub_value_index = 0;
script_item_t item_table[TIEM_MAIN_MAX];
char sub_name[128], sub_value[128];
int value[8];
unsigned int i, main_key_index = 0;
script_head_t script_head;
unsigned int original_len = 0;;
char *align_buf = NULL;
src = (char *)pbuf;
//<2F><><EFBFBD><EFBFBD><EFBFBD>ֶοռ<CEBF>
dest = (char *)malloc(512 * 1024);
if(!dest)
{
printf("fail to get memory for script storage key\n");
goto _err;
}
memset(dest, 0, 512 * 1024);
tmp_dest = dest;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿռ<DDBF>
dest_data = (int *)malloc(512 * 1024);
if(!dest_data)
{
printf("fail to get memory for script storage data\n");
goto _err;
}
memset(dest_data, 0, 512 * 1024);
dest_data_index = 0;
tmp_dest_data = dest_data;
memset(item_table, 0, TIEM_MAIN_MAX * sizeof(script_item_t));
while(script_len)
{
line_len = _get_line_status(src, &line_status, script_len);//<2F>жϵ<D0B6>ǰ<EFBFBD>е<EFBFBD>״̬
script_len -= line_len;
switch(line_status)
{
case THIS_LINE_NULL: //ע<><D7A2><EFBFBD><EFBFBD>
{
break;
}
case THIS_LINE_MAINKEY: //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if(_fill_line_mainkey(src, &item_table[main_key_index])) //<2F><>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̱<EFBFBD><CCB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
{
goto _err;
}
if(!new_main_key_flag)
{
new_main_key_flag = 1;
item_table[main_key_index].item_offset = 0;
}
else
{
item_table[main_key_index].item_offset = item_table[main_key_index - 1].item_offset + item_table[main_key_index - 1].item_length * 10;
}
main_key_index ++;
break;
}
case THIS_LINE_SUBKEY: //<2F>Ӽ<EFBFBD><D3BC>У<EFBFBD><D0A3><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ
{
if(!new_main_key_flag)
{
break;
}
if(_get_item_value(src, sub_name, sub_value)) //ת<><D7AA><EFBFBD><EFBFBD><EFBFBD>ѵȺ<D1B5><C8BA>󷽵Ĵ<F3B7BDB5><C4B4>ڵ<EFBFBD>sub_nam<61><6D><EFBFBD>Ⱥ<EFBFBD><C8BA>ҷ<EFBFBD><D2B7>Ĵ<EFBFBD><C4B4>ŵ<EFBFBD>sub_value<75><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʽ
{
goto _err;
}
//<2F>Ⱥ<EFBFBD><C8BA>󷽵<EFBFBD>ֱ<EFBFBD>ӷŵ<D3B7><C5B5>ڴ<EFBFBD><DAB4>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>32<33><32><EFBFBD>ֽڵĽ<DAB5>ȡ32<33><32><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD><EFBFBD>32<33><32><EFBFBD>ֽڵIJ<DAB5><C4B2><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC>32<33><32><EFBFBD>ֽ<EFBFBD>
//<2F><><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>Ŷ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>ݵ<EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>
strcpy(tmp_dest, sub_name);
tmp_dest += ITEM_MAIN_NAME_MAX;
//<2F>ҷ<EFBFBD><D2B7><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4>ŵ<EFBFBD><C5B5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
ret = _get_str2int(sub_value, value);
if(ret == -1) //<2F><><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
{
goto _err;
}
else if(ret == DATA_TYPE_SINGLE_WORD) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>ݣ<EFBFBD>10<31><30><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD>16<31><36><EFBFBD><EFBFBD>
{
*tmp_dest_data = value[0];
*((unsigned int *)tmp_dest) = dest_data_index;
tmp_dest += 4;
*((unsigned int *)tmp_dest) = (1 << 0) | (DATA_TYPE_SINGLE_WORD << 16); //<2F><><EFBFBD>Ӽ<EFBFBD><D3BC>ij<EFBFBD><C4B3><EFBFBD>1 word<72><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ1<CEAA><31><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
tmp_dest_data ++;
dest_data_index ++;
}
else if(ret == DATA_EMPTY)
{
*((int *)tmp_dest) = dest_data_index;
tmp_dest += 4;
*((unsigned int *)tmp_dest) = (value[0] << 0) | (DATA_EMPTY << 16); //<2F><><EFBFBD>Ӽ<EFBFBD><D3BC>ij<EFBFBD><C4B3><EFBFBD>value word<72><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ2<CEAA><32><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
tmp_dest_data += value[0];
dest_data_index += value[0];
}
else if(ret == DATA_TYPE_STRING)
{
*((int *)tmp_dest) = dest_data_index;
if(value[1] == 0)
{
strncpy((char *)tmp_dest_data, sub_value + sizeof("string:") - 1, value[0]<<2);
}
else if(value[1] == 1)
{
strncpy((char *)tmp_dest_data,sub_value,value[0]<<2);
}
else
{
strncpy((char *)tmp_dest_data, sub_value + 1, value[0]<<2);
}
tmp_dest += 4;
*((unsigned int *)tmp_dest) = (value[0] << 0) | (DATA_TYPE_STRING << 16); //<2F><><EFBFBD>Ӽ<EFBFBD><D3BC>ij<EFBFBD><C4B3><EFBFBD>value word<72><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ2<CEAA><32><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
tmp_dest_data += value[0];
dest_data_index += value[0];
}
else if(ret == DATA_TYPE_GPIO) //ת<><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
{
for(i=0;i<6;i++)
{
*(tmp_dest_data ++) = value[i];
}
*((unsigned int *)tmp_dest) = dest_data_index;
tmp_dest += 4;
*((unsigned int *)tmp_dest) = (6 << 0) | (DATA_TYPE_GPIO << 16); //<2F><><EFBFBD>Ӽ<EFBFBD><D3BC>ij<EFBFBD><C4B3><EFBFBD>ret word<72><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ3<CEAA><33><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
dest_data_index += 6;
}
else if(ret == DATA_TYPE_MULTI_WORD)
{
;
}
sub_value_index ++;
tmp_dest += 4;
item_table[main_key_index - 1].item_length ++;
break;
}
default:
{
goto _err;
}
}
src += line_len;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5>ļ<EFBFBD><C4BC><EFBFBD>
if(!main_key_index)
{
goto _err;
}
//У<><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD>ƫ<EFBFBD>ƣ<EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƫ<EFBFBD>ƣ<EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>λ
for(i=0;i<main_key_index;i++)
{
item_table[i].item_offset += ((sizeof(script_item_t) * main_key_index) >> 2) + (sizeof(script_head_t) >> 2);
}
//У<><D0A3>ÿ<EFBFBD><C3BF><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD>ƫ<EFBFBD>ƣ<EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƫ<EFBFBD>ƺ<EFBFBD><C6BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƫ<EFBFBD>ƣ<EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>λ
{
src = dest;
i = 0;
while(i < sub_value_index * 10 * sizeof(int))
{
src += ITEM_MAIN_NAME_MAX;
*(unsigned int *)src += ((sizeof(script_item_t) * main_key_index) >> 2) + (sub_value_index * 10) + (sizeof(script_head_t)>>2);
i += 10 * sizeof(int);
src += 8;
}
}
script_head.item_num = main_key_index;
original_len = sizeof(script_head_t) + sizeof(script_item_t) * main_key_index +
sub_value_index * 10 * sizeof(int) + dest_data_index * sizeof(int);
script_head.length = randto1k(original_len);
script_head.version[0] = 1;
script_head.version[1] = 2;
fwrite(&script_head, 1, sizeof(script_head_t), hfile);
fwrite(item_table, 1, sizeof(script_item_t) * main_key_index, hfile); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ
fwrite(dest, 1, sub_value_index * 10 * sizeof(int), hfile); //<2F><><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ
fwrite(dest_data, 1, dest_data_index * sizeof(int), hfile); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>Ӽ<EFBFBD><D3BC><EFBFBD>ֵ<EFBFBD><D6B5>Ŀ
if (script_head.length - original_len)
{
align_buf =(char *)malloc(script_head.length - original_len);
if (align_buf == NULL)
{
printf("malloc align_buf fail\n");
goto _err;
}
memset(align_buf,0,script_head.length - original_len);
fwrite(align_buf, 1, script_head.length - original_len, hfile);
}
_err:
if(dest)
{
free(dest);
}
if(dest_data)
{
free(dest_data);
}
if(align_buf)
{
free(align_buf);
}
ret = ((ret >= 0) ? 0: -1);
return ret;
}