pubdir.c
来自「MPC5200 BSP 支持ATA,USB, I2C,扩展网口」· C语言 代码 · 共 2,219 行 · 第 1/5 页
C
2,219 行
int i;
for (i = 0; i < 10; i++)
tokens[i] = 0;
if ( ISNULL(strCmd) ) return;
/* find out if the input string have only spaces, skip the spaces on head */
flag_HaveOnlySpace = 0;
for( i = 0; 0 != *strCmd; i++, strCmd++ )
{
if( ' ' != *strCmd )
{
flag_HaveOnlySpace = 1; /* have something except spaces */
break;
}
}
if( 0 == flag_HaveOnlySpace ) /* the input string have only spaces */
{
return;
}
/* delete the space(s) on the tail */
for( i = strlen(strCmd) - 1; i > 0; i-- )
{
if( ' ' == strCmd[i] )
strCmd[i] = 0;
else
break;
}
/* parse the command line into tokens */
/* now support quotation mark, 03-3-21 */
pCursor = strCmd; /* strCmd have no space(s) at the front part */
for ( i = 0; i < 10; i++)
{
if (*pCursor == '\"')
{
*pCursor = 0;
pCursor++;
tokens[i] = pCursor;
while ((*pCursor != 0) && (*pCursor != '\"')) pCursor++;
if (*pCursor == 0)
{
Print ("syntax error\n"); /* ending quotation mark not found */
return;
}
*pCursor = 0;
}
else
{
tokens[i] = pCursor;
while ((*pCursor != 0) && (*pCursor != ' ')) pCursor++;
if (*pCursor == 0) break;
*pCursor = 0;
}
pCursor++;
while (*pCursor == ' ') pCursor++;
if (*pCursor == 0) break;
}
BEGIN_DBG (SWITCH_CMD_EXECCMD)
Print("token number = %d\n", i);
for (i = 0; i < 10; i++)
{
if ( !ISNULL(tokens[i]) )
Print("token %d: %s\n", i, tokens[i]);
}
END_DBG
_strLower( strCmd );
/* is bat file? */
if (strlen(tokens[0]) > 4)
{
if (0 == strcmp(tokens[0] + strlen(tokens[0]) - 4, ".bat"))
{
Print("execute bat file\n");
cmd_bat(tokens[0]);
return;
}
}
/* look up function table, find the function to call */
funCmdEntry = getCmdFunc(tokens[0]);
if (NULL == funCmdEntry)
{
Print( "command syntax error\n" ); /* command not found */
Print ("%s\n", tokens[0]); /* 把不正确的命令再打印一遍 */
return;
}
funCmdEntry();
}
int switchShell ()
{
if ((g_null_fd = open ("/null", O_RDWR, 0)) == ERROR)
{
logMsg ("Fail to open %s.\n",(int)"/null",2,3,4,5,6);
return ERROR;
}
globalInFd = ioGlobalStdGet (STD_IN);
globalOutFd = ioGlobalStdGet (STD_OUT);
globalErrFd = ioGlobalStdGet (STD_ERR);
g_global_std_out = globalOutFd;
ioGlobalStdSet (STD_OUT, g_null_fd);
ioGlobalStdSet (STD_ERR, g_null_fd);
bshellOutFd = globalOutFd;
bshellErrFd = globalErrFd;
return 0;
}
#if (!defined(__VER) && defined(_MKBOOT))
int print (char *format, ...)
{
int count;
int str_len;
int write_len;
char err_msg[256];
va_list arg_pt;
va_start(arg_pt, format);
count = vsprintf (s_s, format, arg_pt);
va_end(arg_pt);
#ifdef __VER
printf ("%s", s_s);
#else
str_len = strlen(s_s);
write_len = write (bshellOutFd, s_s, str_len);
if (str_len != write_len)
{
sprintf (err_msg, "write %d out of %d\n", write_len, str_len);
write (bshellOutFd, err_msg, strlen (err_msg));
}
#endif
return(count);
}
#endif
/*
* turn string to lower case
* input: v_strCmd, output: v_strCmd
*/
void _strLower( unsigned char *v_strCmd )
{
unsigned int i;
for( i = 0; 0 != v_strCmd[i]; i++ )
{
if( v_strCmd[i] >= 'A' && v_strCmd[i] <= 'Z' )
{
v_strCmd[i] -= ('A' - 'a');
}
}
}
/*
* batfile = "a.bat"
* batfile的内容中前三个字母肯定是小写的
* copy .bat file content to bat buffer
*/
static
void cmd_bat(char *batfile)
{
unsigned long fId;
unsigned long fileLength;
unsigned long bytesRead;
if (NULL == batfile || strlen(batfile) <= 4)
return;
if (0 != strcmp(batfile + strlen(batfile) - 4, ".bat"))
return;
if (ERROR == (fId = my_open(batfile, O_RDONLY, 0)))
{
Print("Bat file not found\n");
return;
}
fileLength = lseek(fId, 0, SEEK_END);
lseek(fId, 0, SEEK_SET);
if (fileLength > BAT_BUF_SIZE -1) /* 最后一个字节留给'\0',用作结束标记 */
{
Print("The bat file length is too big, it should less than %d\n", BAT_BUF_SIZE);
my_close(fId);
return;
}
/* read file content */
memset(s_acBatBuf, 0, BAT_BUF_SIZE);
if (ERROR == (bytesRead = read(fId, s_acBatBuf, fileLength)))
{
Print("Read file error\n");
my_close(fId);
return;
}
if (bytesRead != fileLength)
{
Print("Read file error\n");
my_close(fId);
return;
}
s_acBatBuf[BAT_BUF_SIZE-1] = '\0'; /* */
/* check magic mark */
if (0 != strncmp(s_acBatBuf, "bat", 3))
{
Print("This is not a valid bat file\n");
memset(s_acBatBuf, 0, BAT_BUF_SIZE);
my_close(fId);
return;
}
memset(s_acBatBuf, 0x20, 3); /* erase "bat" */
my_close(fId);
SUC;
}
/*============================================================================*\
|* F U N C T I O N S *|
\*============================================================================*/
/*
* 描述:
* 得到一个命令的处理函数的指针,如果指定的命令没有找到,返回NULL
* function called:
* getCmdNumber
* global variable used:
* g_CmdDescList
*/
CMD_FUNC_PTR getCmdFunc(char *v_pcCmdStr)
{
CMD_DESC *pCmdDesc;
int i;
pCmdDesc = NULL;
for (i = 0; i < getCmdNumber(); i++ )
{
if( (strlen(g_CmdDescList[i].strCmd) == strlen(v_pcCmdStr)) &&
(0 == _stricmp(g_CmdDescList[i].strCmd, v_pcCmdStr)) )
{
pCmdDesc = &g_CmdDescList[i];
break;
}
}
if (NULL == pCmdDesc)
return NULL;
else
return pCmdDesc->funCmdEntry;
}
/*
* 对文件打开函数进行了封装,为了统计打开和关闭的匹配信息,用于调试
* 注意,只有打开成功,才将统计量加1
*/
int my_open (
char * name,
int flags,
int mode)
{
int iFD;
iFD = open (name, flags, mode);
if (iFD == ERROR) return ERROR;
g_openTimes++;
return iFD;
}
/*
* 登记在命令表中的命令数目
*/
int getCmdNumber()
{
int i = 0;
int cmdNumber = 0;
while (EOS != g_CmdDescList[i].strCmd[0])
{
i++;
cmdNumber++;
}
return cmdNumber;
}
/*
* 大写小不敏感的strcmp
* when one string is a initial substring of the other, we don't think them equal
* 相等就等于0
* -100 == _stricmp("abc", "abcd")
* 100 == _stricmp("abcd", "abc")
*/
int _stricmp(char *v_str1, char *v_str2)
{
#define MY_STRCMP_NCS /* Non Case Sensitive */
int iStrLen;
char x, y;
int i;
int iStrLen1, iStrLen2;
iStrLen1 = strlen(v_str1);
iStrLen2 = strlen(v_str2);
iStrLen = MIN(iStrLen1, iStrLen2);
for (i = 0; i < iStrLen; i++)
{
x = v_str1[i];
y = v_str2[i];
#ifdef MY_STRCMP_NCS
if (('a' <= x) && ('z' >= x))
x -= 'a' - 'A';
if (('a' <= y) && ('z' >= y))
y -= 'a' - 'A';
#endif
if (x < y)
return (x-y);
else if (x > y)
return (x-y);
}
if (iStrLen1 == iStrLen2)
return 0;
else if (iStrLen1 > iStrLen2)
return 100;
else
return -100;
}
/*
* cmd_printAllHelp
* 显示帮助信息
* 实现这个函数有两种方法,独立实现,或和命令函数表放在一起
* 遍历命令函数表,如果某个命令的帮助信息不为空,就把它打印出来
* 不能打印空行
*/
static
void cmd_printAllHelp( void )
{
CMD_DESC *pCmdDesc = NULL;
int i = 0;
i = 0;
pCmdDesc = &g_CmdDescList[i];
while (EOS != pCmdDesc->strCmd[0])
{
if (EOS != pCmdDesc->strHelp[0])
Print("%s", pCmdDesc->strHelp);
i++;
pCmdDesc = &g_CmdDescList[i];
}
/* 一些缩写的注解 */
Print ("note:\n");
Print ("lf --- local file name\n");
Print ("hf --- host file name\n");
}
/*
* 打印某一个指定的命令的帮助信息
* function called:
* _stricmp()
*/
static
void printHelp( char *cmdstr )
{
CMD_DESC *pCmdDesc;
int i;
i = 0;
pCmdDesc = &g_CmdDescList[i];
while (EOS != pCmdDesc->strCmd[0])
{
if ((0 == _stricmp(pCmdDesc->strCmd, cmdstr)) &&
(EOS != pCmdDesc->strHelp[0]))
Print("%s", pCmdDesc->strHelp);
i++;
pCmdDesc = &g_CmdDescList[i];
}
}
static void _loadBin()
{
if (ISNULL(tokens[1]))
{
printHelp ("!");
}
else
{
cmd_loadBin (tokens[1]);
}
}
/*
* refer cmd_burn
*/
static void cmd_loadBin (char *v_filename)
{
int iHostFileSize = 0;
int iRealSize = 0;
UCHAR *pcBuf = NULL;
char acUser[FTP_USER_LEN];
char acPasswd[FTP_PASSWD_LEN];
FUNCPTR pfunc = NULL;
getFtpUser (acUser);
getFtpPasswd (acPasswd);
iHostFileSize = getHostFileSize(v_filename, getFtpHostIp(), acUser, acPasswd);
if (iHostFileSize < 0)
{
printf ("getHostFileSize() failed.\n");
return;
}
pcBuf = my_malloc(iHostFileSize);
if (NULL == pcBuf)
{
printf ("no enough memory\n");
return;
}
if (ERROR == downloadData(
FLAG_DOWN_WITH_PARAM,
pcBuf,
iHostFileSize,
&iRealSize,
v_filename,
getFtpHostIp(),
acUser,
acPasswd))
{
printf ("downloadData() fail\n");
my_free (pcBuf);
return;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?