[components][finsh] Decouple RT_USING_DEVICE dependency for msh feature

Signed-off-by: chenyong <1521761801@qq.com>
This commit is contained in:
chenyong 2019-09-25 20:08:53 +08:00
parent 676279421f
commit 2b50d3d32c
8 changed files with 328 additions and 351 deletions

View File

@ -4,7 +4,6 @@ from building import *
cwd = GetCurrentDir() cwd = GetCurrentDir()
src = Split(''' src = Split('''
shell.c shell.c
symbol.c
cmd.c cmd.c
''') ''')
@ -21,19 +20,20 @@ finsh_vm.c
finsh_token.c finsh_token.c
''') ''')
msh_src = Split(''' msh_src = Glob('msh.c')
msh.c
msh_cmd.c
msh_file.c
''')
CPPPATH = [cwd] if GetDepend('RT_USING_DFS'):
msh_src += ['msh_file.c']
if not GetDepend('FINSH_USING_SYMTAB'):
src += ['symbol.c']
if GetDepend('FINSH_USING_MSH'): if GetDepend('FINSH_USING_MSH'):
src = src + msh_src src = src + msh_src
if not GetDepend('FINSH_USING_MSH_ONLY'): if not GetDepend('FINSH_USING_MSH_ONLY'):
src = src + fsh_src src = src + fsh_src
CPPPATH = [cwd]
group = DefineGroup('finsh', src, depend = ['RT_USING_FINSH'], CPPPATH = CPPPATH) group = DefineGroup('finsh', src, depend = ['RT_USING_FINSH'], CPPPATH = CPPPATH)
Return('group') Return('group')

View File

@ -89,6 +89,37 @@ 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.);
int cmd_ps(int argc, char **argv)
{
extern long list_thread(void);
extern int list_module(void);
#ifdef RT_USING_MODULE
if ((argc == 2) && (strcmp(argv[1], "-m") == 0))
list_module();
else
#endif
list_thread();
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ps, __cmd_ps, List threads in the system.);
#ifdef RT_USING_HEAP
int cmd_free(int argc, char **argv)
{
extern void list_mem(void);
extern void list_memheap(void);
#ifdef RT_USING_MEMHEAP_AS_HEAP
list_memheap();
#else
list_mem();
#endif
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_free, __cmd_free, Show the memory usage in the system.);
#endif
static int msh_split(char *cmd, rt_size_t length, char *argv[FINSH_ARG_MAX]) static int msh_split(char *cmd, rt_size_t length, char *argv[FINSH_ARG_MAX])
{ {
char *ptr; char *ptr;

View File

@ -1,331 +0,0 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2013-03-30 Bernard the first verion for FinSH
* 2015-08-28 Bernard Add mkfs command.
*/
#include <rtthread.h>
#ifdef FINSH_USING_MSH
#include <finsh.h>
#include "msh.h"
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#ifdef DFS_USING_WORKDIR
extern char working_directory[];
#endif
int cmd_ls(int argc, char **argv)
{
extern void ls(const char *pathname);
if (argc == 1)
{
#ifdef DFS_USING_WORKDIR
ls(working_directory);
#else
ls("/");
#endif
}
else
{
ls(argv[1]);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ls, __cmd_ls, List information about the FILEs.);
int cmd_cp(int argc, char **argv)
{
void copy(const char *src, const char *dst);
if (argc != 3)
{
rt_kprintf("Usage: cp SOURCE DEST\n");
rt_kprintf("Copy SOURCE to DEST.\n");
}
else
{
copy(argv[1], argv[2]);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cp, __cmd_cp, Copy SOURCE to DEST.);
int cmd_mv(int argc, char **argv)
{
if (argc != 3)
{
rt_kprintf("Usage: mv SOURCE DEST\n");
rt_kprintf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n");
}
else
{
int fd;
char *dest = RT_NULL;
rt_kprintf("%s => %s\n", argv[1], argv[2]);
fd = open(argv[2], O_DIRECTORY, 0);
if (fd >= 0)
{
char *src;
close(fd);
/* it's a directory */
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]);
while (src != argv[1])
{
if (*src == '/') break;
src --;
}
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);
unlink(argv[2]);
}
dest = argv[2];
}
rename(argv[1], dest);
if (dest != RT_NULL && dest != argv[2]) rt_free(dest);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mv, __cmd_mv, Rename SOURCE to DEST.);
int cmd_cat(int argc, char **argv)
{
int index;
extern void cat(const char *filename);
if (argc == 1)
{
rt_kprintf("Usage: cat [FILE]...\n");
rt_kprintf("Concatenate FILE(s)\n");
return 0;
}
for (index = 1; index < argc; index ++)
{
cat(argv[index]);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cat, __cmd_cat, Concatenate FILE(s));
int cmd_rm(int argc, char **argv)
{
int index;
if (argc == 1)
{
rt_kprintf("Usage: rm FILE...\n");
rt_kprintf("Remove (unlink) the FILE(s).\n");
return 0;
}
for (index = 1; index < argc; index ++)
{
unlink(argv[index]);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_rm, __cmd_rm, Remove(unlink) the FILE(s).);
#ifdef DFS_USING_WORKDIR
int cmd_cd(int argc, char **argv)
{
if (argc == 1)
{
rt_kprintf("%s\n", working_directory);
}
else if (argc == 2)
{
if (chdir(argv[1]) != 0)
{
rt_kprintf("No such directory: %s\n", argv[1]);
}
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cd, __cmd_cd, Change the shell working directory.);
int cmd_pwd(int argc, char **argv)
{
rt_kprintf("%s\n", working_directory);
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_pwd, __cmd_pwd, Print the name of the current working directory.);
#endif
int cmd_mkdir(int argc, char **argv)
{
if (argc == 1)
{
rt_kprintf("Usage: mkdir [OPTION] DIRECTORY\n");
rt_kprintf("Create the DIRECTORY, if they do not already exist.\n");
}
else
{
mkdir(argv[1], 0);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkdir, __cmd_mkdir, Create the DIRECTORY.);
int cmd_mkfs(int argc, char **argv)
{
int result = 0;
char *type = "elm"; /* use the default file system type as 'fatfs' */
if (argc == 2)
{
result = dfs_mkfs(type, argv[1]);
}
else if (argc == 4)
{
if (strcmp(argv[1], "-t") == 0)
{
type = argv[2];
result = dfs_mkfs(type, argv[3]);
}
}
else
{
rt_kprintf("Usage: mkfs [-t type] device\n");
return 0;
}
if (result != RT_EOK)
{
rt_kprintf("mkfs failed, result=%d\n", result);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkfs, __cmd_mkfs, format disk with file system);
extern int df(const char *path);
int cmd_df(int argc, char** argv)
{
if (argc != 2)
{
df("/");
}
else
{
if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0))
{
rt_kprintf("df [path]\n");
}
else
{
df(argv[1]);
}
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_df, __cmd_df, disk free);
int cmd_echo(int argc, char** argv)
{
if (argc == 2)
{
rt_kprintf("%s\n", argv[1]);
}
else if (argc == 3)
{
int fd;
fd = open(argv[2], O_RDWR | O_APPEND | O_CREAT, 0);
if (fd >= 0)
{
write (fd, argv[1], strlen(argv[1]));
close(fd);
}
else
{
rt_kprintf("open file:%s failed!\n", argv[2]);
}
}
else
{
rt_kprintf("Usage: echo \"string\" [filename]\n");
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_echo, __cmd_echo, echo string to file);
#endif
int cmd_ps(int argc, char **argv)
{
extern long list_thread(void);
extern int list_module(void);
#ifdef RT_USING_MODULE
if ((argc == 2) && (strcmp(argv[1], "-m") == 0))
list_module();
else
#endif
list_thread();
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ps, __cmd_ps, List threads in the system.);
int cmd_time(int argc, char **argv)
{
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_time, __cmd_time, Execute command with time.);
#ifdef RT_USING_HEAP
int cmd_free(int argc, char **argv)
{
extern void list_mem(void);
extern void list_memheap(void);
#ifdef RT_USING_MEMHEAP_AS_HEAP
list_memheap();
#else
list_mem();
#endif
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_free, __cmd_free, Show the memory usage in the system.);
#endif
#endif /* FINSH_USING_MSH */

View File

@ -137,5 +137,276 @@ int msh_exec_script(const char *cmd_line, int size)
return ret; return ret;
} }
#ifdef DFS_USING_WORKDIR
extern char working_directory[];
#endif
int cmd_ls(int argc, char **argv)
{
extern void ls(const char *pathname);
if (argc == 1)
{
#ifdef DFS_USING_WORKDIR
ls(working_directory);
#else
ls("/");
#endif
}
else
{
ls(argv[1]);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ls, __cmd_ls, List information about the FILEs.);
int cmd_cp(int argc, char **argv)
{
void copy(const char *src, const char *dst);
if (argc != 3)
{
rt_kprintf("Usage: cp SOURCE DEST\n");
rt_kprintf("Copy SOURCE to DEST.\n");
}
else
{
copy(argv[1], argv[2]);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cp, __cmd_cp, Copy SOURCE to DEST.);
int cmd_mv(int argc, char **argv)
{
if (argc != 3)
{
rt_kprintf("Usage: mv SOURCE DEST\n");
rt_kprintf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n");
}
else
{
int fd;
char *dest = RT_NULL;
rt_kprintf("%s => %s\n", argv[1], argv[2]);
fd = open(argv[2], O_DIRECTORY, 0);
if (fd >= 0)
{
char *src;
close(fd);
/* it's a directory */
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]);
while (src != argv[1])
{
if (*src == '/') break;
src --;
}
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);
unlink(argv[2]);
}
dest = argv[2];
}
rename(argv[1], dest);
if (dest != RT_NULL && dest != argv[2]) rt_free(dest);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mv, __cmd_mv, Rename SOURCE to DEST.);
int cmd_cat(int argc, char **argv)
{
int index;
extern void cat(const char *filename);
if (argc == 1)
{
rt_kprintf("Usage: cat [FILE]...\n");
rt_kprintf("Concatenate FILE(s)\n");
return 0;
}
for (index = 1; index < argc; index ++)
{
cat(argv[index]);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cat, __cmd_cat, Concatenate FILE(s));
int cmd_rm(int argc, char **argv)
{
int index;
if (argc == 1)
{
rt_kprintf("Usage: rm FILE...\n");
rt_kprintf("Remove (unlink) the FILE(s).\n");
return 0;
}
for (index = 1; index < argc; index ++)
{
unlink(argv[index]);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_rm, __cmd_rm, Remove(unlink) the FILE(s).);
#ifdef DFS_USING_WORKDIR
int cmd_cd(int argc, char **argv)
{
if (argc == 1)
{
rt_kprintf("%s\n", working_directory);
}
else if (argc == 2)
{
if (chdir(argv[1]) != 0)
{
rt_kprintf("No such directory: %s\n", argv[1]);
}
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cd, __cmd_cd, Change the shell working directory.);
int cmd_pwd(int argc, char **argv)
{
rt_kprintf("%s\n", working_directory);
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_pwd, __cmd_pwd, Print the name of the current working directory.);
#endif
int cmd_mkdir(int argc, char **argv)
{
if (argc == 1)
{
rt_kprintf("Usage: mkdir [OPTION] DIRECTORY\n");
rt_kprintf("Create the DIRECTORY, if they do not already exist.\n");
}
else
{
mkdir(argv[1], 0);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkdir, __cmd_mkdir, Create the DIRECTORY.);
int cmd_mkfs(int argc, char **argv)
{
int result = 0;
char *type = "elm"; /* use the default file system type as 'fatfs' */
if (argc == 2)
{
result = dfs_mkfs(type, argv[1]);
}
else if (argc == 4)
{
if (strcmp(argv[1], "-t") == 0)
{
type = argv[2];
result = dfs_mkfs(type, argv[3]);
}
}
else
{
rt_kprintf("Usage: mkfs [-t type] device\n");
return 0;
}
if (result != RT_EOK)
{
rt_kprintf("mkfs failed, result=%d\n", result);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkfs, __cmd_mkfs, format disk with file system);
extern int df(const char *path);
int cmd_df(int argc, char** argv)
{
if (argc != 2)
{
df("/");
}
else
{
if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0))
{
rt_kprintf("df [path]\n");
}
else
{
df(argv[1]);
}
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_df, __cmd_df, disk free);
int cmd_echo(int argc, char** argv)
{
if (argc == 2)
{
rt_kprintf("%s\n", argv[1]);
}
else if (argc == 3)
{
int fd;
fd = open(argv[2], O_RDWR | O_APPEND | O_CREAT, 0);
if (fd >= 0)
{
write (fd, argv[1], strlen(argv[1]));
close(fd);
}
else
{
rt_kprintf("open file:%s failed!\n", argv[2]);
}
}
else
{
rt_kprintf("Usage: echo \"string\" [filename]\n");
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_echo, __cmd_echo, echo string to file);
#endif /* defined(FINSH_USING_MSH) && defined(RT_USING_DFS) */ #endif /* defined(FINSH_USING_MSH) && defined(RT_USING_DFS) */

View File

@ -41,6 +41,14 @@ static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE];
struct finsh_shell _shell; struct finsh_shell _shell;
#endif #endif
/* finsh symtab */
#ifdef FINSH_USING_SYMTAB
struct finsh_syscall *_syscall_table_begin = NULL;
struct finsh_syscall *_syscall_table_end = NULL;
struct finsh_sysvar *_sysvar_table_begin = NULL;
struct finsh_sysvar *_sysvar_table_end = NULL;
#endif
struct finsh_shell *shell; struct finsh_shell *shell;
static char *finsh_prompt_custom = RT_NULL; static char *finsh_prompt_custom = RT_NULL;
@ -158,6 +166,7 @@ void finsh_set_prompt_mode(rt_uint32_t prompt_mode)
static int finsh_getchar(void) static int finsh_getchar(void)
{ {
#ifdef RT_USING_DEVICE
#ifdef RT_USING_POSIX #ifdef RT_USING_POSIX
return getchar(); return getchar();
#else #else
@ -169,9 +178,13 @@ static int finsh_getchar(void)
return (int)ch; return (int)ch;
#endif #endif
#else
extern char rt_hw_console_getchar(void);
return rt_hw_console_getchar();
#endif
} }
#ifndef RT_USING_POSIX #if !defined(RT_USING_POSIX) && defined(RT_USING_DEVICE)
static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size) static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
{ {
RT_ASSERT(shell != RT_NULL); RT_ASSERT(shell != RT_NULL);
@ -499,7 +512,7 @@ void finsh_thread_entry(void *parameter)
finsh_init(&shell->parser); finsh_init(&shell->parser);
#endif #endif
#ifndef RT_USING_POSIX #if !defined(RT_USING_POSIX) && defined(RT_USING_DEVICE)
/* set console device as shell device */ /* set console device as shell device */
if (shell->device == RT_NULL) if (shell->device == RT_NULL)
{ {

View File

@ -82,7 +82,7 @@ struct finsh_shell
rt_uint16_t line_position; rt_uint16_t line_position;
rt_uint16_t line_curpos; rt_uint16_t line_curpos;
#ifndef RT_USING_POSIX #if !defined(RT_USING_POSIX) && defined(RT_USING_DEVICE)
rt_device_t device; rt_device_t device;
#endif #endif

View File

@ -10,7 +10,7 @@
#include <rtthread.h> #include <rtthread.h>
#ifdef RT_USING_FINSH #if defined(RT_USING_FINSH) && !defined(FINSH_USING_SYMTAB)
#include "finsh.h" #include "finsh.h"
@ -27,12 +27,6 @@ long list_msgqueue(void);
long list_mempool(void); long list_mempool(void);
long list_timer(void); long list_timer(void);
#ifdef FINSH_USING_SYMTAB
struct finsh_syscall *_syscall_table_begin = NULL;
struct finsh_syscall *_syscall_table_end = NULL;
struct finsh_sysvar *_sysvar_table_begin = NULL;
struct finsh_sysvar *_sysvar_table_end = NULL;
#else
struct finsh_syscall _syscall_table[] = struct finsh_syscall _syscall_table[] =
{ {
{"hello", hello}, {"hello", hello},
@ -67,7 +61,6 @@ struct finsh_syscall *_syscall_table_end = &_syscall_table[sizeof(_syscall_tab
struct finsh_sysvar *_sysvar_table_begin = NULL; struct finsh_sysvar *_sysvar_table_begin = NULL;
struct finsh_sysvar *_sysvar_table_end = NULL; struct finsh_sysvar *_sysvar_table_end = NULL;
#endif
#endif /* RT_USING_FINSH */ #endif /* RT_USING_FINSH && !FINSH_USING_SYMTAB */

View File

@ -22,9 +22,9 @@ static struct ulog_backend console;
void ulog_console_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, void ulog_console_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw,
const char *log, size_t len) const char *log, size_t len)
{ {
#ifdef RT_USING_DEVICE
rt_device_t dev = rt_console_get_device(); rt_device_t dev = rt_console_get_device();
#ifdef RT_USING_DEVICE
if (dev == RT_NULL) if (dev == RT_NULL)
{ {
rt_hw_console_output(log); rt_hw_console_output(log);