[Finsh] Add features to execute module.

This commit is contained in:
bernard 2014-01-03 08:01:24 +08:00
parent bd8fc8b2f9
commit 1f8a0668d2
1 changed files with 334 additions and 260 deletions

View File

@ -24,13 +24,18 @@
*
* Change Logs:
* Date Author Notes
* 2013-03-30 Bernard the first verion for FinSH
* 2013-03-30 Bernard the first verion for finsh
* 2014-01-03 Bernard msh can execute module.
*/
#include "msh.h"
#include <finsh.h>
#include <shell.h>
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif
#define RT_FINSH_ARG_MAX 10
typedef int (*cmd_function_t)(int argc, char** argv);
@ -171,6 +176,72 @@ static cmd_function_t msh_get_cmd(char *cmd)
return cmd_func;
}
#if defined(RT_USING_MODULE) && defined(RT_USING_DFS)
int msh_exec_module(int argc, char** argv)
{
int fd = -1;
char *pg_name;
int length, cmd_length;
if (argc == 0) return -RT_ERROR; /* no command */
/* get name length */
cmd_length = rt_strlen(argv[0]); length = cmd_length + 32;
pg_name = (char*) rt_malloc(length);
if (pg_name == RT_NULL) return -RT_ENOMEM; /* no memory */
if (strstr(argv[0], ".mo") != RT_NULL || strstr(argv[0], ".MO") != RT_NULL)
{
/* try to open program */
if (fd < 0)
{
rt_snprintf(pg_name, length - 1, "%s", argv[0]);
fd = open(pg_name, O_RDONLY, 0);
}
/* search in /bin path */
if (fd < 0)
{
rt_snprintf(pg_name, length - 1, "/bin/%s", argv[0]);
fd = open(pg_name, O_RDONLY, 0);
}
}
else
{
/* add .mo and open program */
/* try to open program */
if (fd < 0)
{
rt_snprintf(pg_name, length - 1, "%s.mo", argv[0]);
fd = open(pg_name, O_RDONLY, 0);
}
/* search in /bin path */
if (fd < 0)
{
rt_snprintf(pg_name, length - 1, "/bin/%s.mo", argv[0]);
fd = open(pg_name, O_RDONLY, 0);
}
}
if (fd >= 0)
{
/* found program */
close(fd);
rt_module_open(pg_name);
}
else
{
rt_kprintf("%s: program not found.\n", argv[0]);
}
rt_free(pg_name);
return 0;
}
#endif
int msh_exec(char* cmd, rt_size_t length)
{
int argc;
@ -186,7 +257,11 @@ int msh_exec(char* cmd, rt_size_t length)
cmd_func = msh_get_cmd(argv[0]);
if (cmd_func == RT_NULL)
{
rt_kprintf("%s: command not found\n", argv[0]);
#ifdef RT_USING_MODULE
msh_exec_module(argc, argv);
#else
rt_kprintf("%s: command not found.\n", argv[0]);
#endif
return -1;
}
@ -208,7 +283,6 @@ static int str_common(const char *str1, const char *str2)
}
#ifdef RT_USING_DFS
#include <dfs_posix.h>
void msh_auto_complete_path(char *path)
{
DIR* dir;