[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_src = Split('''
msh_cmd.c
msh.c msh.c
msh_cmd.c
msh_file.c
''') ''')
CPPPATH = [cwd] CPPPATH = [cwd]

View File

@ -99,17 +99,17 @@ static long _list_thread(struct rt_list_node *list)
else if (thread->stat == RT_THREAD_INIT) rt_kprintf(" init "); else if (thread->stat == RT_THREAD_INIT) rt_kprintf(" init ");
else if (thread->stat == RT_THREAD_CLOSE) rt_kprintf(" close "); else if (thread->stat == RT_THREAD_CLOSE) rt_kprintf(" close ");
ptr = (rt_uint8_t*)thread->stack_addr; ptr = (rt_uint8_t *)thread->stack_addr;
while (*ptr == '#')ptr ++; while (*ptr == '#')ptr ++;
rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n", rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp), thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
thread->stack_size, thread->stack_size,
thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr), thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
thread->remaining_tick, thread->remaining_tick,
thread->error); thread->error);
} }
return 0; return 0;
} }
@ -148,7 +148,7 @@ static long _list_sem(struct rt_list_node *list)
sem = (struct rt_semaphore *)(rt_list_entry(node, struct rt_object, list)); sem = (struct rt_semaphore *)(rt_list_entry(node, struct rt_object, list));
if (!rt_list_isempty(&sem->parent.suspend_thread)) if (!rt_list_isempty(&sem->parent.suspend_thread))
{ {
rt_kprintf("%-8.*s %03d %d:", rt_kprintf("%-8.*s %03d %d:",
RT_NAME_MAX, RT_NAME_MAX,
sem->parent.parent.name, sem->parent.parent.name,
sem->value, sem->value,
@ -448,7 +448,7 @@ static long _list_device(struct rt_list_node *list)
{ {
struct rt_device *device; struct rt_device *device;
struct rt_list_node *node; struct rt_list_node *node;
char * const device_type_str[] = char *const device_type_str[] =
{ {
"Character Device", "Character Device",
"Block Device", "Block Device",
@ -464,11 +464,11 @@ static long _list_device(struct rt_list_node *list)
"SPI Bus", "SPI Bus",
"SPI Device", "SPI Device",
"SDIO Bus", "SDIO Bus",
"PM Pseudo Device", "PM Pseudo Device",
"Pipe", "Pipe",
"Portal Device", "Portal Device",
"Timer Device", "Timer Device",
"Miscellaneous Device", "Miscellaneous Device",
"Unknown" "Unknown"
}; };
@ -507,13 +507,13 @@ int list_module(void)
list = &rt_object_container[RT_Object_Class_Module].object_list; list = &rt_object_container[RT_Object_Class_Module].object_list;
rt_kprintf("module name ref\n"); rt_kprintf("module name ref address \n");
rt_kprintf("------------ --------\n"); rt_kprintf("------------ -------- ------------\n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
module = (struct rt_module *)(rt_list_entry(node, struct rt_object, list)); module = (struct rt_module *)(rt_list_entry(node, struct rt_object, list));
rt_kprintf("%-16.*s %-04d\n", rt_kprintf("%-16.*s %-04d 0x%08x\n",
RT_NAME_MAX, module->parent.name, module->nref); RT_NAME_MAX, module->parent.name, module->nref, module->module_space);
} }
return 0; return 0;
@ -525,13 +525,13 @@ int list_mod_detail(const char *name)
{ {
int i; int i;
struct rt_module *module; struct rt_module *module;
/* find module */ /* find module */
if ((module = rt_module_find(name)) != RT_NULL) if ((module = rt_module_find(name)) != RT_NULL)
{ {
/* module has entry point */ /* module has entry point */
if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY)) if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
{ {
struct rt_thread *thread; struct rt_thread *thread;
struct rt_list_node *tlist; struct rt_list_node *tlist;
rt_uint8_t *ptr; rt_uint8_t *ptr;
@ -548,15 +548,15 @@ int list_mod_detail(const char *name)
else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend"); else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
else if (thread->stat == RT_THREAD_INIT) rt_kprintf(" init "); else if (thread->stat == RT_THREAD_INIT) rt_kprintf(" init ");
ptr = (rt_uint8_t*)thread->stack_addr; ptr = (rt_uint8_t *)thread->stack_addr;
while (*ptr == '#')ptr ++; while (*ptr == '#')ptr ++;
rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n", rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp), thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
thread->stack_size, thread->stack_size,
thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr), thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
thread->remaining_tick, thread->remaining_tick,
thread->error); thread->error);
} }
/* list sub thread in module */ /* list sub thread in module */
@ -607,18 +607,18 @@ int list_mod_detail(const char *name)
if (!rt_list_isempty(tlist)) _list_timer(tlist); if (!rt_list_isempty(tlist)) _list_timer(tlist);
} }
if (module->nsym > 0) if (module->nsym > 0)
{ {
rt_kprintf("symbol address \n"); rt_kprintf("symbol address \n");
rt_kprintf("-------- ----------\n"); rt_kprintf("-------- ----------\n");
/* list module export symbols */ /* list module export symbols */
for (i=0; i<module->nsym; i++) for (i = 0; i < module->nsym; i++)
{ {
rt_kprintf("%s 0x%x\n", rt_kprintf("%s 0x%x\n",
module->symtab[i].name, module->symtab[i].addr); module->symtab[i].name, module->symtab[i].addr);
} }
} }
} }
return 0; return 0;
@ -632,16 +632,16 @@ long list(void)
struct finsh_syscall_item *syscall_item; struct finsh_syscall_item *syscall_item;
struct finsh_sysvar_item *sysvar_item; struct finsh_sysvar_item *sysvar_item;
#endif #endif
rt_kprintf("--Function List:\n"); rt_kprintf("--Function List:\n");
{ {
struct finsh_syscall *index; struct finsh_syscall *index;
for (index = _syscall_table_begin; for (index = _syscall_table_begin;
index < _syscall_table_end; index < _syscall_table_end;
FINSH_NEXT_SYSCALL(index)) FINSH_NEXT_SYSCALL(index))
{ {
/* skip the internal command */ /* skip the internal command */
if (strncmp((char*)index->name, "__", 2) == 0) continue; if (strncmp((char *)index->name, "__", 2) == 0) continue;
#ifdef FINSH_USING_DESCRIPTION #ifdef FINSH_USING_DESCRIPTION
rt_kprintf("%-16s -- %s\n", index->name, index->desc); rt_kprintf("%-16s -- %s\n", index->name, index->desc);
@ -664,8 +664,8 @@ long list(void)
{ {
struct finsh_sysvar *index; struct finsh_sysvar *index;
for (index = _sysvar_table_begin; for (index = _sysvar_table_begin;
index < _sysvar_table_end; index < _sysvar_table_end;
FINSH_NEXT_SYSVAR(index)) FINSH_NEXT_SYSVAR(index))
{ {
#ifdef FINSH_USING_DESCRIPTION #ifdef FINSH_USING_DESCRIPTION
rt_kprintf("%-16s -- %s\n", index->name, index->desc); rt_kprintf("%-16s -- %s\n", index->name, index->desc);
@ -682,7 +682,7 @@ long list(void)
sysvar_item = sysvar_item->next; sysvar_item = sysvar_item->next;
} }
#endif #endif
return 0; return 0;
} }
FINSH_FUNCTION_EXPORT(list, list all symbol in system) FINSH_FUNCTION_EXPORT(list, list all symbol in system)
@ -732,12 +732,12 @@ void list_prefix(char *prefix)
{ {
struct finsh_syscall *index; struct finsh_syscall *index;
for (index = _syscall_table_begin; for (index = _syscall_table_begin;
index < _syscall_table_end; index < _syscall_table_end;
FINSH_NEXT_SYSCALL(index)) FINSH_NEXT_SYSCALL(index))
{ {
/* skip internal command */ /* skip internal command */
if (str_is_prefix("__", index->name) == 0) continue; if (str_is_prefix("__", index->name) == 0) continue;
if (str_is_prefix(prefix, index->name) == 0) if (str_is_prefix(prefix, index->name) == 0)
{ {
if (func_cnt == 0) if (func_cnt == 0)
@ -807,10 +807,10 @@ void list_prefix(char *prefix)
/* checks in system variable */ /* checks in system variable */
{ {
struct finsh_sysvar* index; struct finsh_sysvar *index;
for (index = _sysvar_table_begin; for (index = _sysvar_table_begin;
index < _sysvar_table_end; index < _sysvar_table_end;
FINSH_NEXT_SYSVAR(index)) FINSH_NEXT_SYSVAR(index))
{ {
if (str_is_prefix(prefix, index->name) == 0) if (str_is_prefix(prefix, index->name) == 0)
{ {

View File

@ -37,7 +37,7 @@
#endif #endif
#define RT_FINSH_ARG_MAX 10 #define RT_FINSH_ARG_MAX 10
typedef int (*cmd_function_t)(int argc, char** argv); typedef int (*cmd_function_t)(int argc, char **argv);
#ifdef FINSH_USING_MSH #ifdef FINSH_USING_MSH
#ifdef FINSH_USING_MSH_ONLY #ifdef FINSH_USING_MSH_ONLY
@ -56,7 +56,7 @@ rt_bool_t msh_is_used(void)
return __msh_state; return __msh_state;
} }
static int msh_exit(int argc, char** argv) static int msh_exit(int argc, char **argv)
{ {
/* return to finsh shell mode */ /* return to finsh shell mode */
__msh_state = RT_FALSE; __msh_state = RT_FALSE;
@ -74,15 +74,15 @@ static int msh_enter(void)
FINSH_FUNCTION_EXPORT_ALIAS(msh_enter, msh, use module shell); FINSH_FUNCTION_EXPORT_ALIAS(msh_enter, msh, use module shell);
#endif #endif
int msh_help(int argc, char** argv) int msh_help(int argc, char **argv)
{ {
rt_kprintf("RT-Thread shell commands:\n"); rt_kprintf("RT-Thread shell commands:\n");
{ {
struct finsh_syscall *index; struct finsh_syscall *index;
for (index = _syscall_table_begin; for (index = _syscall_table_begin;
index < _syscall_table_end; index < _syscall_table_end;
FINSH_NEXT_SYSCALL(index)) FINSH_NEXT_SYSCALL(index))
{ {
if (strncmp(index->name, "__cmd_", 6) != 0) continue; if (strncmp(index->name, "__cmd_", 6) != 0) continue;
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB) #if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
@ -98,7 +98,7 @@ int msh_help(int argc, char** argv)
} }
FINSH_FUNCTION_EXPORT_ALIAS(msh_help, __cmd_help, RT-Thread shell help.); FINSH_FUNCTION_EXPORT_ALIAS(msh_help, __cmd_help, RT-Thread shell help.);
static int msh_split(char* cmd, rt_size_t length, char* argv[RT_FINSH_ARG_MAX]) static int msh_split(char *cmd, rt_size_t length, char *argv[RT_FINSH_ARG_MAX])
{ {
char *ptr; char *ptr;
rt_size_t position; rt_size_t position;
@ -161,13 +161,13 @@ static cmd_function_t msh_get_cmd(char *cmd, int size)
cmd_function_t cmd_func = RT_NULL; cmd_function_t cmd_func = RT_NULL;
for (index = _syscall_table_begin; for (index = _syscall_table_begin;
index < _syscall_table_end; index < _syscall_table_end;
FINSH_NEXT_SYSCALL(index)) FINSH_NEXT_SYSCALL(index))
{ {
if (strncmp(index->name, "__cmd_", 6) != 0) continue; if (strncmp(index->name, "__cmd_", 6) != 0) continue;
if (strncmp(&index->name[6], cmd, size) == 0 && if (strncmp(&index->name[6], cmd, size) == 0 &&
index->name[6 + size] == '\0') index->name[6 + size] == '\0')
{ {
cmd_func = (cmd_function_t)index->func; cmd_func = (cmd_function_t)index->func;
break; break;
@ -180,7 +180,7 @@ 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. /* Return 0 on module executed. Other value indicate error.
*/ */
int msh_exec_module(const char* cmd_line, int size) int msh_exec_module(const char *cmd_line, int size)
{ {
int ret; int ret;
int fd = -1; int fd = -1;
@ -197,7 +197,7 @@ int msh_exec_module(const char* cmd_line, int size)
length = cmd_length + 32; length = cmd_length + 32;
/* 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) if (pg_name == RT_NULL)
return -RT_ENOMEM; return -RT_ENOMEM;
@ -251,12 +251,21 @@ int msh_exec_module(const char* cmd_line, int size)
int system(const char *command) 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); RTM_EXPORT(system);
#endif #endif
static int _msh_exec_cmd(char* cmd, rt_size_t length, int *retp) static int _msh_exec_cmd(char *cmd, rt_size_t length, int *retp)
{ {
int argc; int argc;
int cmd0_size = 0; int cmd0_size = 0;
@ -287,12 +296,12 @@ static int _msh_exec_cmd(char* cmd, rt_size_t length, int *retp)
return 0; return 0;
} }
int msh_exec(char* cmd, rt_size_t length) int msh_exec(char *cmd, rt_size_t length)
{ {
int cmd_ret; int cmd_ret;
/* strim the beginning of command */ /* strim the beginning of command */
while(*cmd == ' ' || *cmd == '\t') while (*cmd == ' ' || *cmd == '\t')
{ {
cmd++; cmd++;
length--; length--;
@ -318,7 +327,12 @@ int msh_exec(char* cmd, rt_size_t length)
#endif #endif
#if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR) #if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
/* change to this directory */ if (msh_exec_script(cmd, length) == 0)
{
return 0;
}
/* change to this directory */
if (chdir(cmd) == 0) if (chdir(cmd) == 0)
{ {
return 0; return 0;
@ -329,7 +343,7 @@ int msh_exec(char* cmd, rt_size_t length)
{ {
char *tcmd; char *tcmd;
tcmd = cmd; tcmd = cmd;
while(*tcmd != ' ' && *tcmd != '\0') while (*tcmd != ' ' && *tcmd != '\0')
{ {
tcmd++; tcmd++;
} }
@ -355,14 +369,14 @@ static int str_common(const char *str1, const char *str2)
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
void msh_auto_complete_path(char *path) void msh_auto_complete_path(char *path)
{ {
DIR* dir = RT_NULL; DIR *dir = RT_NULL;
struct dirent *dirent = RT_NULL; struct dirent *dirent = RT_NULL;
char *full_path, *ptr, *index; char *full_path, *ptr, *index;
if (!path) if (!path)
return; return;
full_path = (char*)rt_malloc(256); full_path = (char *)rt_malloc(256);
if (full_path == RT_NULL) return; /* out of memory */ if (full_path == RT_NULL) return; /* out of memory */
if (*path != '/') if (*path != '/')
@ -434,9 +448,9 @@ void msh_auto_complete_path(char *path)
/* save dirent name */ /* save dirent name */
strcpy(full_path, dirent->d_name); strcpy(full_path, dirent->d_name);
} }
length = str_common(dirent->d_name, full_path); length = str_common(dirent->d_name, full_path);
if (length < min_length) if (length < min_length)
{ {
min_length = length; min_length = length;
@ -460,7 +474,7 @@ void msh_auto_complete_path(char *path)
rt_kprintf("%s\n", dirent->d_name); rt_kprintf("%s\n", dirent->d_name);
} }
} }
length = index - path; length = index - path;
memcpy(index, full_path, min_length); memcpy(index, full_path, min_length);
path[length + min_length] = '\0'; path[length + min_length] = '\0';
@ -481,7 +495,7 @@ void msh_auto_complete(char *prefix)
min_length = 0; min_length = 0;
name_ptr = RT_NULL; name_ptr = RT_NULL;
if (*prefix == '\0') if (*prefix == '\0')
{ {
msh_help(0, RT_NULL); msh_help(0, RT_NULL);
return; return;
@ -522,7 +536,7 @@ void msh_auto_complete(char *prefix)
/* skip finsh shell function */ /* skip finsh shell function */
if (strncmp(index->name, "__cmd_", 6) != 0) continue; if (strncmp(index->name, "__cmd_", 6) != 0) continue;
cmd_name = (const char*) &index->name[6]; cmd_name = (const char *) &index->name[6];
if (strncmp(prefix, cmd_name, strlen(prefix)) == 0) if (strncmp(prefix, cmd_name, strlen(prefix)) == 0)
{ {
if (min_length == 0) if (min_length == 0)

View File

@ -33,7 +33,10 @@
#include <rtthread.h> #include <rtthread.h>
rt_bool_t msh_is_used(void); rt_bool_t msh_is_used(void);
int msh_exec(char* cmd, rt_size_t length); int msh_exec(char *cmd, rt_size_t length);
void msh_auto_complete(char *prefix); 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 #endif

View File

@ -41,7 +41,7 @@
extern char working_directory[]; extern char working_directory[];
#endif #endif
int cmd_ls(int argc, char** argv) int cmd_ls(int argc, char **argv)
{ {
extern void ls(const char *pathname); extern void ls(const char *pathname);
@ -53,7 +53,7 @@ int cmd_ls(int argc, char** argv)
ls("/"); ls("/");
#endif #endif
} }
else else
{ {
ls(argv[1]); ls(argv[1]);
} }
@ -62,7 +62,7 @@ int cmd_ls(int argc, char** argv)
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ls, __cmd_ls, List information about the FILEs.); FINSH_FUNCTION_EXPORT_ALIAS(cmd_ls, __cmd_ls, List information about the FILEs.);
int cmd_cp(int argc, char** argv) int cmd_cp(int argc, char **argv)
{ {
void copy(const char *src, const char *dst); void copy(const char *src, const char *dst);
@ -80,7 +80,7 @@ int cmd_cp(int argc, char** argv)
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cp, __cmd_cp, Copy SOURCE to DEST.); FINSH_FUNCTION_EXPORT_ALIAS(cmd_cp, __cmd_cp, Copy SOURCE to DEST.);
int cmd_mv(int argc, char** argv) int cmd_mv(int argc, char **argv)
{ {
if (argc != 3) if (argc != 3)
{ {
@ -89,66 +89,66 @@ int cmd_mv(int argc, char** argv)
} }
else else
{ {
int fd; int fd;
char *dest = RT_NULL; char *dest = RT_NULL;
rt_kprintf("%s => %s\n", argv[1], argv[2]); rt_kprintf("%s => %s\n", argv[1], argv[2]);
fd = open(argv[2], O_DIRECTORY, 0); fd = open(argv[2], O_DIRECTORY, 0);
if (fd >= 0) if (fd >= 0)
{ {
char *src; char *src;
close(fd);
/* it's a directory */ close(fd);
dest = (char*)rt_malloc(DFS_PATH_MAX);
if (dest == RT_NULL)
{
rt_kprintf("out of memory\n");
return -RT_ENOMEM;
}
src = argv[1] + rt_strlen(argv[1]); /* it's a directory */
while (src != argv[1]) dest = (char *)rt_malloc(DFS_PATH_MAX);
{ if (dest == RT_NULL)
if (*src == '/') break; {
src --; rt_kprintf("out of memory\n");
} return -RT_ENOMEM;
}
rt_snprintf(dest, DFS_PATH_MAX - 1, "%s/%s", argv[2], src); src = argv[1] + rt_strlen(argv[1]);
} while (src != argv[1])
else {
{ if (*src == '/') break;
fd = open(argv[2], O_RDONLY, 0); src --;
if (fd >= 0) }
{
close(fd);
unlink(argv[2]);
}
dest = argv[2]; rt_snprintf(dest, DFS_PATH_MAX - 1, "%s/%s", argv[2], src);
} }
else
{
fd = open(argv[2], O_RDONLY, 0);
if (fd >= 0)
{
close(fd);
rename(argv[1], dest); unlink(argv[2]);
if (dest != RT_NULL && dest != argv[2]) rt_free(dest); }
dest = argv[2];
}
rename(argv[1], dest);
if (dest != RT_NULL && dest != argv[2]) rt_free(dest);
} }
return 0; return 0;
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mv, __cmd_mv, Rename SOURCE to DEST.); FINSH_FUNCTION_EXPORT_ALIAS(cmd_mv, __cmd_mv, Rename SOURCE to DEST.);
int cmd_cat(int argc, char** argv) int cmd_cat(int argc, char **argv)
{ {
int index; int index;
extern void cat(const char* filename); extern void cat(const char *filename);
if (argc == 1) if (argc == 1)
{ {
rt_kprintf("Usage: cat [FILE]...\n"); rt_kprintf("Usage: cat [FILE]...\n");
rt_kprintf("Concatenate FILE(s)\n"); rt_kprintf("Concatenate FILE(s)\n");
return 0; return 0;
} }
for (index = 1; index < argc; index ++) for (index = 1; index < argc; index ++)
@ -160,7 +160,7 @@ int cmd_cat(int argc, char** argv)
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cat, __cmd_cat, Concatenate FILE(s)); FINSH_FUNCTION_EXPORT_ALIAS(cmd_cat, __cmd_cat, Concatenate FILE(s));
int cmd_rm(int argc, char** argv) int cmd_rm(int argc, char **argv)
{ {
int index; int index;
@ -168,7 +168,7 @@ int cmd_rm(int argc, char** argv)
{ {
rt_kprintf("Usage: rm FILE...\n"); rt_kprintf("Usage: rm FILE...\n");
rt_kprintf("Remove (unlink) the FILE(s).\n"); rt_kprintf("Remove (unlink) the FILE(s).\n");
return 0; return 0;
} }
for (index = 1; index < argc; index ++) for (index = 1; index < argc; index ++)
@ -178,10 +178,10 @@ int cmd_rm(int argc, char** argv)
return 0; return 0;
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_rm, __cmd_rm, Remove (unlink) the FILE(s).); FINSH_FUNCTION_EXPORT_ALIAS(cmd_rm, __cmd_rm, Remove(unlink) the FILE(s).);
#ifdef DFS_USING_WORKDIR #ifdef DFS_USING_WORKDIR
int cmd_cd(int argc, char** argv) int cmd_cd(int argc, char **argv)
{ {
if (argc == 1) if (argc == 1)
{ {
@ -196,34 +196,34 @@ int cmd_cd(int argc, char** argv)
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cd, __cmd_cd, Change the shell working directory.); FINSH_FUNCTION_EXPORT_ALIAS(cmd_cd, __cmd_cd, Change the shell working directory.);
int cmd_pwd(int argc, char** argv) int cmd_pwd(int argc, char **argv)
{ {
rt_kprintf("%s\n", working_directory); rt_kprintf("%s\n", working_directory);
return 0; return 0;
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_pwd, __cmd_pwd, Print the name of the current working directory.); FINSH_FUNCTION_EXPORT_ALIAS(cmd_pwd, __cmd_pwd, Print the name of the current working directory.);
#endif #endif
int cmd_mkdir(int argc, char** argv) int cmd_mkdir(int argc, char **argv)
{ {
if (argc == 1) if (argc == 1)
{ {
rt_kprintf("Usage: mkdir [OPTION] DIRECTORY\n"); rt_kprintf("Usage: mkdir [OPTION] DIRECTORY\n");
rt_kprintf("Create the DIRECTORY, if they do not already exist.\n"); rt_kprintf("Create the DIRECTORY, if they do not already exist.\n");
} }
else else
{ {
mkdir(argv[1], 0); mkdir(argv[1], 0);
} }
return 0; return 0;
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkdir, __cmd_mkdir, Create the DIRECTORY.); FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkdir, __cmd_mkdir, Create the DIRECTORY.);
int cmd_mkfs(int argc, char** argv) int cmd_mkfs(int argc, char **argv)
{ {
int result = 0; int result = 0;
char* type="elm"; /* use the default file system type as 'fatfs' */ char *type = "elm"; /* use the default file system type as 'fatfs' */
if (argc == 2) if (argc == 2)
{ {
@ -242,7 +242,7 @@ int cmd_mkfs(int argc, char** argv)
rt_kprintf("Usage: mkfs [-t type] device\n"); rt_kprintf("Usage: mkfs [-t type] device\n");
return 0; return 0;
} }
if (result != RT_EOK) if (result != RT_EOK)
{ {
rt_kprintf("mkfs failed, result=%d\n", result); rt_kprintf("mkfs failed, result=%d\n", result);
@ -255,17 +255,17 @@ FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkfs, __cmd_mkfs, format disk with file system);
#endif #endif
#ifdef RT_USING_LWIP #ifdef RT_USING_LWIP
int cmd_ifconfig(int argc, char** argv) int cmd_ifconfig(int argc, char **argv)
{ {
extern void list_if(void); extern void list_if(void);
extern void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr); extern void set_if(char *netif_name, char *ip_addr, char *gw_addr, char *nm_addr);
if(argc == 1) if (argc == 1)
{ {
list_if(); list_if();
} }
else if(argc == 5) else if (argc == 5)
{ {
rt_kprintf("config : %s\n", argv[1]); rt_kprintf("config : %s\n", argv[1]);
rt_kprintf("IP addr: %s\n", argv[2]); rt_kprintf("IP addr: %s\n", argv[2]);
@ -283,22 +283,22 @@ int cmd_ifconfig(int argc, char** argv)
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ifconfig, __cmd_ifconfig, list the information of network interfaces); FINSH_FUNCTION_EXPORT_ALIAS(cmd_ifconfig, __cmd_ifconfig, list the information of network interfaces);
#ifdef RT_LWIP_TCP #ifdef RT_LWIP_TCP
int cmd_netstat(int argc, char** argv) int cmd_netstat(int argc, char **argv)
{ {
extern void list_tcps(void); extern void list_tcps(void);
list_tcps(); list_tcps();
return 0; return 0;
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_netstat, __cmd_netstat, list the information of TCP/IP); FINSH_FUNCTION_EXPORT_ALIAS(cmd_netstat, __cmd_netstat, list the information of TCP / IP);
#endif #endif
#endif /* RT_USING_LWIP */ #endif /* RT_USING_LWIP */
int cmd_ps(int argc, char** argv) int cmd_ps(int argc, char **argv)
{ {
extern long list_thread(void); extern long list_thread(void);
extern int list_module(void); extern int list_module(void);
#ifdef RT_USING_MODULE #ifdef RT_USING_MODULE
if ((argc == 2) && (strcmp(argv[1], "-m") == 0)) if ((argc == 2) && (strcmp(argv[1], "-m") == 0))
list_module(); list_module();
@ -309,14 +309,14 @@ int cmd_ps(int argc, char** argv)
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ps, __cmd_ps, List threads in the system.); FINSH_FUNCTION_EXPORT_ALIAS(cmd_ps, __cmd_ps, List threads in the system.);
int cmd_time(int argc, char** argv) int cmd_time(int argc, char **argv)
{ {
return 0; return 0;
} }
FINSH_FUNCTION_EXPORT_ALIAS(cmd_time, __cmd_time, Execute command with time.); FINSH_FUNCTION_EXPORT_ALIAS(cmd_time, __cmd_time, Execute command with time.);
#ifdef RT_USING_HEAP #ifdef RT_USING_HEAP
int cmd_free(int argc, char** argv) int cmd_free(int argc, char **argv)
{ {
extern void list_mem(void); extern void list_mem(void);
extern void list_memheap(void); extern void list_memheap(void);

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