rt-thread/components/finsh/msh_cmd.c

423 lines
8.7 KiB
C
Raw Normal View History

2013-03-30 16:14:38 +08:00
/*
2018-10-14 19:28:18 +08:00
* Copyright (c) 2006-2018, RT-Thread Development Team
2013-03-30 16:14:38 +08:00
*
2018-10-14 19:28:18 +08:00
* SPDX-License-Identifier: Apache-2.0
2013-03-30 16:14:38 +08:00
*
* Change Logs:
* Date Author Notes
* 2013-03-30 Bernard the first verion for FinSH
2015-08-28 15:13:04 +08:00
* 2015-08-28 Bernard Add mkfs command.
2013-03-30 16:14:38 +08:00
*/
#include <rtthread.h>
#ifdef FINSH_USING_MSH
#include <finsh.h>
2013-03-30 16:14:38 +08:00
#include "msh.h"
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#ifdef DFS_USING_WORKDIR
extern char working_directory[];
#endif
2015-10-11 15:38:08 +08:00
int cmd_ls(int argc, char **argv)
2013-03-30 16:14:38 +08:00
{
extern void ls(const char *pathname);
if (argc == 1)
{
#ifdef DFS_USING_WORKDIR
ls(working_directory);
#else
ls("/");
#endif
}
2015-10-11 15:38:08 +08:00
else
2013-03-30 16:14:38 +08:00
{
ls(argv[1]);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ls, __cmd_ls, List information about the FILEs.);
2013-03-30 16:14:38 +08:00
2015-10-11 15:38:08 +08:00
int cmd_cp(int argc, char **argv)
2013-03-30 16:14:38 +08:00
{
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.);
2013-03-30 16:14:38 +08:00
2015-10-11 15:38:08 +08:00
int cmd_mv(int argc, char **argv)
2013-03-30 16:14:38 +08:00
{
if (argc != 3)
{
rt_kprintf("Usage: mv SOURCE DEST\n");
rt_kprintf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n");
}
else
{
2015-10-11 15:38:08 +08:00
int fd;
char *dest = RT_NULL;
2014-06-25 17:23:28 +08:00
2013-03-30 16:14:38 +08:00
rt_kprintf("%s => %s\n", argv[1], argv[2]);
2014-06-25 17:23:28 +08:00
2015-10-11 15:38:08 +08:00
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);
2013-03-30 16:14:38 +08:00
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mv, __cmd_mv, Rename SOURCE to DEST.);
2013-03-30 16:14:38 +08:00
2015-10-11 15:38:08 +08:00
int cmd_cat(int argc, char **argv)
2013-03-30 16:14:38 +08:00
{
int index;
2015-10-11 15:38:08 +08:00
extern void cat(const char *filename);
2013-03-30 16:14:38 +08:00
if (argc == 1)
{
rt_kprintf("Usage: cat [FILE]...\n");
rt_kprintf("Concatenate FILE(s)\n");
2015-10-11 15:38:08 +08:00
return 0;
2013-03-30 16:14:38 +08:00
}
for (index = 1; index < argc; index ++)
{
cat(argv[index]);
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cat, __cmd_cat, Concatenate FILE(s));
2013-03-30 16:14:38 +08:00
2015-10-11 15:38:08 +08:00
int cmd_rm(int argc, char **argv)
2013-03-30 16:14:38 +08:00
{
int index;
if (argc == 1)
{
rt_kprintf("Usage: rm FILE...\n");
rt_kprintf("Remove (unlink) the FILE(s).\n");
2015-10-11 15:38:08 +08:00
return 0;
2013-03-30 16:14:38 +08:00
}
for (index = 1; index < argc; index ++)
{
unlink(argv[index]);
}
return 0;
}
2015-10-11 15:38:08 +08:00
FINSH_FUNCTION_EXPORT_ALIAS(cmd_rm, __cmd_rm, Remove(unlink) the FILE(s).);
2013-03-30 16:14:38 +08:00
#ifdef DFS_USING_WORKDIR
2015-10-11 15:38:08 +08:00
int cmd_cd(int argc, char **argv)
2013-03-30 16:14:38 +08:00
{
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]);
}
2013-03-30 16:14:38 +08:00
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cd, __cmd_cd, Change the shell working directory.);
2015-10-11 15:38:08 +08:00
int cmd_pwd(int argc, char **argv)
2013-03-30 16:14:38 +08:00
{
2015-10-11 15:38:08 +08:00
rt_kprintf("%s\n", working_directory);
return 0;
2013-03-30 16:14:38 +08:00
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_pwd, __cmd_pwd, Print the name of the current working directory.);
#endif
2013-03-30 16:14:38 +08:00
2015-10-11 15:38:08 +08:00
int cmd_mkdir(int argc, char **argv)
2013-03-30 16:14:38 +08:00
{
2015-10-11 15:38:08 +08:00
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;
2013-03-30 16:14:38 +08:00
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkdir, __cmd_mkdir, Create the DIRECTORY.);
2015-10-11 15:38:08 +08:00
int cmd_mkfs(int argc, char **argv)
2015-08-28 15:13:04 +08:00
{
int result = 0;
2015-10-11 15:38:08 +08:00
char *type = "elm"; /* use the default file system type as 'fatfs' */
2015-08-28 15:13:04 +08:00
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]);
2015-08-28 15:13:04 +08:00
}
}
else
{
rt_kprintf("Usage: mkfs [-t type] device\n");
return 0;
}
2015-10-11 15:38:08 +08:00
2015-09-02 14:05:38 +08:00
if (result != RT_EOK)
{
rt_kprintf("mkfs failed, result=%d\n", result);
}
2015-08-28 15:13:04 +08:00
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);
2016-04-18 13:53:32 +08:00
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;
2016-04-18 13:53:32 +08:00
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_echo, __cmd_echo, echo string to file);
2013-03-30 16:14:38 +08:00
#endif
2013-07-14 14:36:48 +08:00
#ifdef RT_USING_LWIP
2015-10-11 15:38:08 +08:00
int cmd_ifconfig(int argc, char **argv)
{
2014-04-09 13:16:02 +08:00
extern void list_if(void);
2015-10-11 15:38:08 +08:00
extern void set_if(char *netif_name, char *ip_addr, char *gw_addr, char *nm_addr);
2014-04-09 13:16:02 +08:00
2015-10-11 15:38:08 +08:00
if (argc == 1)
2014-04-09 13:16:02 +08:00
{
list_if();
}
2015-10-11 15:38:08 +08:00
else if (argc == 5)
2014-04-09 13:16:02 +08:00
{
rt_kprintf("config : %s\n", argv[1]);
rt_kprintf("IP addr: %s\n", argv[2]);
rt_kprintf("Gateway: %s\n", argv[3]);
rt_kprintf("netmask: %s\n", argv[4]);
set_if(argv[1], argv[2], argv[3], argv[4]);
}
else
{
rt_kprintf("bad parameter! e.g: ifconfig e0 192.168.1.30 192.168.1.1 255.255.255.0\n");
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ifconfig, __cmd_ifconfig, list the information of network interfaces);
2015-11-04 09:37:38 +08:00
#ifdef RT_LWIP_DNS
#include <lwip/api.h>
#include <lwip/dns.h>
2017-01-31 13:17:34 +08:00
#include <lwip/ip_addr.h>
#include <lwip/init.h>
2015-11-04 09:37:38 +08:00
int cmd_dns(int argc, char **argv)
{
extern void set_dns(char* dns_server);
if (argc == 1)
{
int index;
2017-01-31 13:17:34 +08:00
#if (LWIP_VERSION) < 0x02000000U
ip_addr_t ip_addr;
for(index=0; index<DNS_MAX_SERVERS; index++)
{
ip_addr = dns_getserver(index);
rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(&ip_addr));
}
#else
const ip_addr_t *ip_addr;
2015-11-04 09:37:38 +08:00
for(index=0; index<DNS_MAX_SERVERS; index++)
{
ip_addr = dns_getserver(index);
2017-01-31 13:17:34 +08:00
rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(ip_addr));
2015-11-04 09:37:38 +08:00
}
2017-01-31 13:17:34 +08:00
#endif
2015-11-04 09:37:38 +08:00
}
else if (argc == 2)
{
rt_kprintf("dns : %s\n", argv[1]);
set_dns(argv[1]);
}
else
{
rt_kprintf("bad parameter! e.g: dns 114.114.114.114\n");
}
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_dns, __cmd_dns, list the information of dns);
#endif
#if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
2015-10-11 15:38:08 +08:00
int cmd_netstat(int argc, char **argv)
2014-04-09 13:17:21 +08:00
{
2015-10-11 15:38:08 +08:00
extern void list_tcps(void);
2018-05-22 23:58:16 +08:00
extern void list_udps(void);
2014-04-09 13:17:21 +08:00
#ifdef RT_LWIP_TCP
2015-10-11 15:38:08 +08:00
list_tcps();
#endif
#ifdef RT_LWIP_UDP
2018-05-22 23:58:16 +08:00
list_udps();
#endif
2015-10-11 15:38:08 +08:00
return 0;
2014-04-09 13:17:21 +08:00
}
2015-10-11 15:38:08 +08:00
FINSH_FUNCTION_EXPORT_ALIAS(cmd_netstat, __cmd_netstat, list the information of TCP / IP);
#endif
2014-04-09 13:16:02 +08:00
#endif /* RT_USING_LWIP */
2015-10-11 15:38:08 +08:00
int cmd_ps(int argc, char **argv)
2013-07-14 14:36:48 +08:00
{
extern long list_thread(void);
2015-09-24 20:58:40 +08:00
extern int list_module(void);
2015-10-11 15:38:08 +08:00
2015-09-24 20:58:40 +08:00
#ifdef RT_USING_MODULE
if ((argc == 2) && (strcmp(argv[1], "-m") == 0))
list_module();
else
#endif
list_thread();
2013-07-14 14:36:48 +08:00
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ps, __cmd_ps, List threads in the system.);
2013-07-14 14:36:48 +08:00
2015-10-11 15:38:08 +08:00
int cmd_time(int argc, char **argv)
2013-07-14 14:36:48 +08:00
{
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_time, __cmd_time, Execute command with time.);
2013-07-14 14:36:48 +08:00
#ifdef RT_USING_HEAP
2015-10-11 15:38:08 +08:00
int cmd_free(int argc, char **argv)
2013-07-14 14:36:48 +08:00
{
extern void list_mem(void);
2015-04-24 11:41:52 +08:00
extern void list_memheap(void);
2013-07-14 14:36:48 +08:00
#ifdef RT_USING_MEMHEAP_AS_HEAP
list_memheap();
#else
2013-07-14 14:36:48 +08:00
list_mem();
#endif
2013-07-14 14:36:48 +08:00
return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_free, __cmd_free, Show the memory usage in the system.);
#endif
2013-07-14 14:36:48 +08:00
#endif /* FINSH_USING_MSH */