Merge pull request #249 from grissiom/msh-next

Msh next
This commit is contained in:
Bernard Xiong 2014-03-27 22:49:43 +08:00
commit 96d9fd8715
1 changed files with 94 additions and 50 deletions

View File

@ -178,13 +178,17 @@ static cmd_function_t msh_get_cmd(char *cmd, int size)
} }
#if defined(RT_USING_MODULE) && defined(RT_USING_DFS) #if defined(RT_USING_MODULE) && defined(RT_USING_DFS)
/* Return 0 on module executed. Other value indicate error.
*/
int msh_exec_module(char* cmd_line, int size) int msh_exec_module(char* cmd_line, int size)
{ {
int ret;
int fd = -1; int fd = -1;
char *pg_name; char *pg_name;
int length, cmd_length = 0; int length, cmd_length = 0;
if (size == 0) return -RT_ERROR; /* no command */ if (size == 0)
return -RT_ERROR;
/* get the length of command0 */ /* get the length of command0 */
while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size) while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
cmd_length ++; cmd_length ++;
@ -194,7 +198,8 @@ int msh_exec_module(char* cmd_line, int size)
/* allocate program name memory */ /* allocate program name memory */
pg_name = (char*) rt_malloc(length); pg_name = (char*) rt_malloc(length);
if (pg_name == RT_NULL) return -RT_ENOMEM; /* no memory */ if (pg_name == RT_NULL)
return -RT_ENOMEM;
/* copy command0 */ /* copy command0 */
memcpy(pg_name, cmd_line, cmd_length); memcpy(pg_name, cmd_line, cmd_length);
@ -203,10 +208,7 @@ int msh_exec_module(char* cmd_line, int size)
if (strstr(pg_name, ".mo") != RT_NULL || strstr(pg_name, ".MO") != RT_NULL) if (strstr(pg_name, ".mo") != RT_NULL || strstr(pg_name, ".MO") != RT_NULL)
{ {
/* try to open program */ /* try to open program */
if (fd < 0)
{
fd = open(pg_name, O_RDONLY, 0); fd = open(pg_name, O_RDONLY, 0);
}
/* search in /bin path */ /* search in /bin path */
if (fd < 0) if (fd < 0)
@ -220,11 +222,8 @@ int msh_exec_module(char* cmd_line, int size)
/* add .mo and open program */ /* add .mo and open program */
/* try to open program */ /* try to open program */
if (fd < 0)
{
strcat(pg_name, ".mo"); strcat(pg_name, ".mo");
fd = open(pg_name, O_RDONLY, 0); fd = open(pg_name, O_RDONLY, 0);
}
/* search in /bin path */ /* search in /bin path */
if (fd < 0) if (fd < 0)
@ -239,57 +238,93 @@ int msh_exec_module(char* cmd_line, int size)
/* found program */ /* found program */
close(fd); close(fd);
rt_module_exec_cmd(pg_name, cmd_line, size); rt_module_exec_cmd(pg_name, cmd_line, size);
ret = 0;
} }
else else
{ {
rt_kprintf("%s: program not found.\n", cmd_line); ret = -1;
} }
rt_free(pg_name); rt_free(pg_name);
return 0; return ret;
} }
#endif #endif
int msh_exec(char* cmd, rt_size_t length) static int _msh_exec_cmd(char* cmd, rt_size_t length, int *retp)
{ {
int argc; int argc;
char *argv[RT_FINSH_ARG_MAX];
int cmd0_size = 0; int cmd0_size = 0;
cmd_function_t cmd_func; cmd_function_t cmd_func;
char *argv[RT_FINSH_ARG_MAX];
RT_ASSERT(cmd);
RT_ASSERT(retp);
/* strim the beginning of command */
while(*cmd == ' ' || *cmd == '\t'){cmd++; length--;}
/* find the size of first command */ /* find the size of first command */
while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length) while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
cmd0_size ++; cmd0_size ++;
if (cmd0_size == 0) return -1; /* no command found */ if (cmd0_size == 0)
return -RT_ERROR;
/* try to get built-in command */
cmd_func = msh_get_cmd(cmd, cmd0_size); cmd_func = msh_get_cmd(cmd, cmd0_size);
if (cmd_func == RT_NULL) if (cmd_func == RT_NULL)
{ return -RT_ERROR;
#ifdef RT_USING_MODULE
msh_exec_module(cmd, length);
#else
argv[0] = cmd;
while(*cmd != ' ')
{
if (*cmd == 0) break;
cmd++;
}
if (*cmd == ' ') *cmd = 0;
rt_kprintf("%s: command not found.\n", argv[0]);
#endif
return -1;
}
/* split arguments */ /* split arguments */
memset(argv, 0x00, sizeof(argv)); memset(argv, 0x00, sizeof(argv));
argc = msh_split(cmd, length, argv); argc = msh_split(cmd, length, argv);
if (argc == 0) return -1; if (argc == 0)
return -RT_ERROR;
/* exec this command */ /* exec this command */
return cmd_func(argc, argv); *retp = cmd_func(argc, argv);
return 0;
}
int msh_exec(char* cmd, rt_size_t length)
{
int cmd_ret;
/* strim the beginning of command */
while(*cmd == ' ' || *cmd == '\t')
{
cmd++;
length--;
}
/* Exec sequence:
* 1. built-in command
* 2. module(if enabled)
* 3. chdir to the directry(if possible)
*/
if (_msh_exec_cmd(cmd, length, &cmd_ret) == 0)
{
return cmd_ret;
}
#ifdef RT_USING_MODULE
if (msh_exec_module(cmd, length) == 0)
{
return 0;
}
#endif
#ifdef DFS_USING_WORKDIR
if (chdir(cmd) == 0)
{
return 0;
}
#endif
/* truncate the cmd at the first space. */
{
char *tcmd;
tcmd = cmd;
while(*tcmd != ' ' && *tcmd != '\0')
{
tcmd++;
}
*tcmd = '\0';
}
rt_kprintf("%s: command not found.\n", cmd);
return -1;
} }
static int str_common(const char *str1, const char *str2) static int str_common(const char *str1, const char *str2)
@ -453,6 +488,15 @@ void msh_auto_complete(char *prefix)
ptr --; ptr --;
} }
#ifdef RT_USING_MODULE
/* There is a chance that the user want to run the module directly. So
* try to complete the file names. If the completed path is not a
* module, the system won't crash anyway. */
if (ptr == prefix)
{
msh_auto_complete_path(ptr);
}
#endif
} }
#endif #endif