[Finsh] Add script feature for msh.

This commit is contained in:
Bernard Xiong 2015-10-11 15:38:08 +08:00
parent 147901c2cb
commit f4248a95c3
6 changed files with 334 additions and 157 deletions

View File

@ -22,8 +22,9 @@ finsh_token.c
''')
msh_src = Split('''
msh_cmd.c
msh.c
msh_cmd.c
msh_file.c
''')
CPPPATH = [cwd]

View File

@ -507,13 +507,13 @@ int list_module(void)
list = &rt_object_container[RT_Object_Class_Module].object_list;
rt_kprintf("module name ref\n");
rt_kprintf("------------ --------\n");
rt_kprintf("module name ref address \n");
rt_kprintf("------------ -------- ------------\n");
for (node = list->next; node != list; node = node->next)
{
module = (struct rt_module *)(rt_list_entry(node, struct rt_object, list));
rt_kprintf("%-16.*s %-04d\n",
RT_NAME_MAX, module->parent.name, module->nref);
rt_kprintf("%-16.*s %-04d 0x%08x\n",
RT_NAME_MAX, module->parent.name, module->nref, module->module_space);
}
return 0;

View File

@ -251,7 +251,16 @@ int msh_exec_module(const char* cmd_line, int size)
int system(const char *command)
{
return msh_exec_module(command, rt_strlen(command));
int ret = -RT_ENOMEM;
char *cmd = rt_strdup(command);
if (cmd)
{
ret = msh_exec(cmd, rt_strlen(cmd));
rt_free(cmd);
}
return ret;
}
RTM_EXPORT(system);
#endif
@ -318,6 +327,11 @@ int msh_exec(char* cmd, rt_size_t length)
#endif
#if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
if (msh_exec_script(cmd, length) == 0)
{
return 0;
}
/* change to this directory */
if (chdir(cmd) == 0)
{

View File

@ -36,4 +36,7 @@ rt_bool_t msh_is_used(void);
int msh_exec(char *cmd, rt_size_t length);
void msh_auto_complete(char *prefix);
int msh_exec_module(const char *cmd_line, int size);
int msh_exec_script(const char *cmd_line, int size);
#endif

159
components/finsh/msh_file.c Normal file
View File

@ -0,0 +1,159 @@
/*
* script for RT-Thread module shell
*
* COPYRIGHT (C) 2013-2015, Shanghai Real-Thread Technology Co., Ltd
*
* 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
* 2015-09-25 Bernard the first verion for FinSH
*/
#include <rtthread.h>
#include <finsh.h>
#include "msh.h"
#if defined(FINSH_USING_MSH) && defined(RT_USING_DFS)
#include <dfs_posix.h>
static int msh_readline(int fd, char *line_buf, int size)
{
char ch;
int index = 0;
do
{
if (read(fd, &ch, 1) != 1)
{
/* nothing in this file */
return 0;
}
}
while (ch == '\n' || ch == '\r');
/* set the first character */
line_buf[index ++] = ch;
while (index < size)
{
if (read(fd, &ch, 1) == 1)
{
if (ch == '\n' || ch == '\r')
{
line_buf[index] = '\0';
break;
}
line_buf[index++] = ch;
}
else
{
line_buf[index] = '\0';
break;
}
}
return index;
}
int msh_exec_script(const char *cmd_line, int size)
{
int ret;
int fd = -1;
char *pg_name;
int length, cmd_length = 0;
if (size == 0) return -RT_ERROR;
/* get the length of command0 */
while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
cmd_length ++;
/* get name length */
length = cmd_length + 32;
/* allocate program name memory */
pg_name = (char *) rt_malloc(length);
if (pg_name == RT_NULL) return -RT_ENOMEM;
/* copy command0 */
memcpy(pg_name, cmd_line, cmd_length);
pg_name[cmd_length] = '\0';
if (strstr(pg_name, ".sh") != RT_NULL || strstr(pg_name, ".SH") != RT_NULL)
{
/* try to open program */
fd = open(pg_name, O_RDONLY, 0);
/* search in /bin path */
if (fd < 0)
{
rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
fd = open(pg_name, O_RDONLY, 0);
}
}
rt_free(pg_name);
if (fd >= 0)
{
/* found script */
char *line_buf;
int length;
line_buf = (char *) rt_malloc(RT_CONSOLEBUF_SIZE);
/* read line by line and then exec it */
do
{
length = msh_readline(fd, line_buf, RT_CONSOLEBUF_SIZE);
if (length > 0)
{
char ch = '\0';
int index;
for (index = 0; index < length; index ++)
{
ch = line_buf[index];
if (ch == ' ' || ch == '\t') continue;
else break;
}
if (ch != '#') /* not a comment */
msh_exec(line_buf, length);
}
}
while (length > 0);
close(fd);
rt_free(line_buf);
ret = 0;
}
else
{
ret = -1;
}
return ret;
}
#endif