2013-03-30 16:14:38 +08:00
|
|
|
/*
|
2022-12-13 08:08:38 +08:00
|
|
|
* Copyright (c) 2006-2022, 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
|
2014-01-03 08:01:24 +08:00
|
|
|
* 2013-03-30 Bernard the first verion for finsh
|
|
|
|
* 2014-01-03 Bernard msh can execute module.
|
2017-07-19 15:21:14 +08:00
|
|
|
* 2017-07-19 Aubr.Cool limit argc to RT_FINSH_ARG_MAX
|
2013-03-30 16:14:38 +08:00
|
|
|
*/
|
2018-11-07 14:31:32 +08:00
|
|
|
#include <rtthread.h>
|
2021-08-23 17:30:05 +08:00
|
|
|
#include <string.h>
|
2018-11-07 14:31:32 +08:00
|
|
|
|
2021-08-23 17:30:05 +08:00
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
|
|
|
|
#ifndef FINSH_ARG_MAX
|
2021-11-16 00:22:49 +08:00
|
|
|
#define FINSH_ARG_MAX 8
|
|
|
|
#endif /* FINSH_ARG_MAX */
|
2013-03-30 16:14:38 +08:00
|
|
|
|
|
|
|
#include "msh.h"
|
2021-08-23 17:30:05 +08:00
|
|
|
#include "shell.h"
|
2021-11-16 00:22:49 +08:00
|
|
|
#ifdef DFS_USING_POSIX
|
2022-01-11 15:20:14 +08:00
|
|
|
#include <dfs_file.h>
|
|
|
|
#include <unistd.h>
|
2022-01-14 00:27:12 +08:00
|
|
|
#include <fcntl.h>
|
2021-11-16 00:22:49 +08:00
|
|
|
#endif /* DFS_USING_POSIX */
|
2018-08-30 20:27:45 +08:00
|
|
|
#ifdef RT_USING_MODULE
|
2021-11-16 00:22:49 +08:00
|
|
|
#include <dlmodule.h>
|
|
|
|
#endif /* RT_USING_MODULE */
|
2018-06-07 11:55:54 +08:00
|
|
|
|
2015-10-11 15:38:08 +08:00
|
|
|
typedef int (*cmd_function_t)(int argc, char **argv);
|
2013-03-30 16:14:38 +08:00
|
|
|
|
2015-10-11 15:38:08 +08:00
|
|
|
int msh_help(int argc, char **argv)
|
2013-03-30 16:14:38 +08:00
|
|
|
{
|
2014-01-03 08:01:24 +08:00
|
|
|
rt_kprintf("RT-Thread shell commands:\n");
|
|
|
|
{
|
|
|
|
struct finsh_syscall *index;
|
|
|
|
|
|
|
|
for (index = _syscall_table_begin;
|
2015-10-11 15:38:08 +08:00
|
|
|
index < _syscall_table_end;
|
|
|
|
FINSH_NEXT_SYSCALL(index))
|
2014-01-03 08:01:24 +08:00
|
|
|
{
|
2014-01-01 21:45:09 +08:00
|
|
|
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
|
2021-08-23 18:37:58 +08:00
|
|
|
rt_kprintf("%-16s - %s\n", index->name, index->desc);
|
2014-01-01 21:45:09 +08:00
|
|
|
#else
|
2021-08-23 18:37:58 +08:00
|
|
|
rt_kprintf("%s ", index->name);
|
2014-01-01 21:45:09 +08:00
|
|
|
#endif
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rt_kprintf("\n");
|
2013-03-30 16:14:38 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
return 0;
|
2013-03-30 16:14:38 +08:00
|
|
|
}
|
2022-03-29 14:41:08 +08:00
|
|
|
MSH_CMD_EXPORT_ALIAS(msh_help, help, RT-Thread shell help.);
|
2013-03-30 16:14:38 +08:00
|
|
|
|
2021-08-29 04:48:08 +08:00
|
|
|
#ifdef MSH_USING_BUILT_IN_COMMANDS
|
2019-09-25 20:08:53 +08:00
|
|
|
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;
|
|
|
|
}
|
2021-08-23 17:30:05 +08:00
|
|
|
MSH_CMD_EXPORT_ALIAS(cmd_ps, ps, List threads in the system.);
|
2019-09-25 20:08:53 +08:00
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
int cmd_free(int argc, char **argv)
|
|
|
|
{
|
2022-06-16 14:19:24 +08:00
|
|
|
#ifdef RT_USING_MEMHEAP_AS_HEAP
|
|
|
|
extern void list_memheap(void);
|
|
|
|
list_memheap();
|
|
|
|
#else
|
2022-01-01 19:13:13 +08:00
|
|
|
rt_size_t total = 0, used = 0, max_used = 0;
|
2019-09-25 20:08:53 +08:00
|
|
|
|
2021-12-16 16:23:58 +08:00
|
|
|
rt_memory_info(&total, &used, &max_used);
|
2022-07-07 00:07:17 +08:00
|
|
|
rt_kprintf("total : %d\n", total);
|
|
|
|
rt_kprintf("used : %d\n", used);
|
|
|
|
rt_kprintf("maximum : %d\n", max_used);
|
|
|
|
rt_kprintf("available: %d\n", total - used);
|
2022-06-16 14:19:24 +08:00
|
|
|
#endif
|
2019-09-25 20:08:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-08-23 17:30:05 +08:00
|
|
|
MSH_CMD_EXPORT_ALIAS(cmd_free, free, Show the memory usage in the system.);
|
2021-08-26 07:10:01 +08:00
|
|
|
#endif /* RT_USING_HEAP */
|
2021-08-29 04:48:08 +08:00
|
|
|
#endif /* MSH_USING_BUILT_IN_COMMANDS */
|
2019-09-25 20:08:53 +08:00
|
|
|
|
2018-06-07 11:55:54 +08:00
|
|
|
static int msh_split(char *cmd, rt_size_t length, char *argv[FINSH_ARG_MAX])
|
2013-03-30 16:14:38 +08:00
|
|
|
{
|
2014-01-03 08:01:24 +08:00
|
|
|
char *ptr;
|
|
|
|
rt_size_t position;
|
|
|
|
rt_size_t argc;
|
2017-07-19 15:21:14 +08:00
|
|
|
rt_size_t i;
|
2014-01-03 08:01:24 +08:00
|
|
|
|
|
|
|
ptr = cmd;
|
2021-08-23 17:30:05 +08:00
|
|
|
position = 0;
|
|
|
|
argc = 0;
|
2014-01-03 08:01:24 +08:00
|
|
|
|
|
|
|
while (position < length)
|
|
|
|
{
|
|
|
|
/* strip bank and tab */
|
|
|
|
while ((*ptr == ' ' || *ptr == '\t') && position < length)
|
|
|
|
{
|
|
|
|
*ptr = '\0';
|
2021-08-23 17:30:05 +08:00
|
|
|
ptr ++;
|
|
|
|
position ++;
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
2017-07-19 15:21:14 +08:00
|
|
|
|
2021-08-23 17:30:05 +08:00
|
|
|
if (argc >= FINSH_ARG_MAX)
|
2017-07-19 15:21:14 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("Too many args ! We only Use:\n");
|
2021-08-23 17:30:05 +08:00
|
|
|
for (i = 0; i < argc; i++)
|
2017-07-19 15:21:14 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("%s ", argv[i]);
|
|
|
|
}
|
|
|
|
rt_kprintf("\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
if (position >= length) break;
|
|
|
|
|
|
|
|
/* handle string */
|
|
|
|
if (*ptr == '"')
|
|
|
|
{
|
2021-08-23 17:30:05 +08:00
|
|
|
ptr ++;
|
|
|
|
position ++;
|
|
|
|
argv[argc] = ptr;
|
|
|
|
argc ++;
|
2014-01-03 08:01:24 +08:00
|
|
|
|
|
|
|
/* skip this string */
|
|
|
|
while (*ptr != '"' && position < length)
|
|
|
|
{
|
|
|
|
if (*ptr == '\\')
|
|
|
|
{
|
|
|
|
if (*(ptr + 1) == '"')
|
|
|
|
{
|
2021-08-23 17:30:05 +08:00
|
|
|
ptr ++;
|
|
|
|
position ++;
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-23 17:30:05 +08:00
|
|
|
ptr ++;
|
|
|
|
position ++;
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
|
|
|
if (position >= length) break;
|
|
|
|
|
|
|
|
/* skip '"' */
|
2021-08-23 17:30:05 +08:00
|
|
|
*ptr = '\0';
|
|
|
|
ptr ++;
|
|
|
|
position ++;
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
argv[argc] = ptr;
|
|
|
|
argc ++;
|
|
|
|
while ((*ptr != ' ' && *ptr != '\t') && position < length)
|
|
|
|
{
|
2021-08-23 17:30:05 +08:00
|
|
|
ptr ++;
|
|
|
|
position ++;
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
|
|
|
if (position >= length) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return argc;
|
2013-03-30 16:14:38 +08:00
|
|
|
}
|
|
|
|
|
2014-01-11 16:33:31 +08:00
|
|
|
static cmd_function_t msh_get_cmd(char *cmd, int size)
|
2013-03-30 16:14:38 +08:00
|
|
|
{
|
2014-01-03 08:01:24 +08:00
|
|
|
struct finsh_syscall *index;
|
|
|
|
cmd_function_t cmd_func = RT_NULL;
|
|
|
|
|
|
|
|
for (index = _syscall_table_begin;
|
2015-10-11 15:38:08 +08:00
|
|
|
index < _syscall_table_end;
|
|
|
|
FINSH_NEXT_SYSCALL(index))
|
2014-01-03 08:01:24 +08:00
|
|
|
{
|
2021-08-23 18:37:58 +08:00
|
|
|
if (strncmp(index->name, cmd, size) == 0 &&
|
|
|
|
index->name[size] == '\0')
|
2014-01-03 08:01:24 +08:00
|
|
|
{
|
|
|
|
cmd_func = (cmd_function_t)index->func;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd_func;
|
2013-03-30 16:14:38 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:22:49 +08:00
|
|
|
#if defined(RT_USING_MODULE) && defined(DFS_USING_POSIX)
|
2014-03-27 16:15:56 +08:00
|
|
|
/* Return 0 on module executed. Other value indicate error.
|
|
|
|
*/
|
2015-10-11 15:38:08 +08:00
|
|
|
int msh_exec_module(const char *cmd_line, int size)
|
2014-01-03 08:01:24 +08:00
|
|
|
{
|
2014-03-27 16:15:56 +08:00
|
|
|
int ret;
|
2014-01-03 08:01:24 +08:00
|
|
|
int fd = -1;
|
|
|
|
char *pg_name;
|
2014-01-11 16:33:31 +08:00
|
|
|
int length, cmd_length = 0;
|
2014-01-03 08:01:24 +08:00
|
|
|
|
2014-03-27 16:15:56 +08:00
|
|
|
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 ++;
|
2014-01-03 08:01:24 +08:00
|
|
|
|
|
|
|
/* get name length */
|
2014-01-11 16:33:31 +08:00
|
|
|
length = cmd_length + 32;
|
2014-01-03 08:01:24 +08:00
|
|
|
|
2014-03-27 16:15:56 +08:00
|
|
|
/* allocate program name memory */
|
2022-12-03 12:07:44 +08:00
|
|
|
pg_name = (char *) rt_malloc(length + 3);
|
2014-03-27 16:15:56 +08:00
|
|
|
if (pg_name == RT_NULL)
|
|
|
|
return -RT_ENOMEM;
|
2014-01-03 08:01:24 +08:00
|
|
|
|
2014-03-27 16:15:56 +08:00
|
|
|
/* copy command0 */
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(pg_name, cmd_line, cmd_length);
|
2014-03-27 16:15:56 +08:00
|
|
|
pg_name[cmd_length] = '\0';
|
2014-01-11 16:33:31 +08:00
|
|
|
|
|
|
|
if (strstr(pg_name, ".mo") != RT_NULL || strstr(pg_name, ".MO") != RT_NULL)
|
2014-01-03 08:01:24 +08:00
|
|
|
{
|
|
|
|
/* try to open program */
|
2014-03-27 16:15:56 +08:00
|
|
|
fd = open(pg_name, O_RDONLY, 0);
|
2014-01-03 08:01:24 +08:00
|
|
|
|
|
|
|
/* search in /bin path */
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2014-01-11 16:33:31 +08:00
|
|
|
rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
|
2014-01-03 08:01:24 +08:00
|
|
|
fd = open(pg_name, O_RDONLY, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* add .mo and open program */
|
|
|
|
|
|
|
|
/* try to open program */
|
2014-03-27 16:15:56 +08:00
|
|
|
strcat(pg_name, ".mo");
|
|
|
|
fd = open(pg_name, O_RDONLY, 0);
|
2014-01-03 08:01:24 +08:00
|
|
|
|
|
|
|
/* search in /bin path */
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2014-03-27 16:15:56 +08:00
|
|
|
rt_snprintf(pg_name, length - 1, "/bin/%.*s.mo", cmd_length, cmd_line);
|
2014-01-03 08:01:24 +08:00
|
|
|
fd = open(pg_name, O_RDONLY, 0);
|
|
|
|
}
|
|
|
|
}
|
2014-01-11 16:33:31 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
/* found program */
|
|
|
|
close(fd);
|
2018-08-30 20:27:45 +08:00
|
|
|
dlmodule_exec(pg_name, cmd_line, size);
|
2014-03-27 16:15:56 +08:00
|
|
|
ret = 0;
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-27 16:15:56 +08:00
|
|
|
ret = -1;
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_free(pg_name);
|
2014-03-27 16:15:56 +08:00
|
|
|
return ret;
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
#endif
|
2014-01-03 08:01:24 +08:00
|
|
|
|
2015-10-11 15:38:08 +08:00
|
|
|
static int _msh_exec_cmd(char *cmd, rt_size_t length, int *retp)
|
2013-03-30 16:14:38 +08:00
|
|
|
{
|
2014-01-03 08:01:24 +08:00
|
|
|
int argc;
|
2018-04-09 11:58:11 +08:00
|
|
|
rt_size_t cmd0_size = 0;
|
2014-01-03 08:01:24 +08:00
|
|
|
cmd_function_t cmd_func;
|
2018-06-07 11:55:54 +08:00
|
|
|
char *argv[FINSH_ARG_MAX];
|
2013-03-30 16:14:38 +08:00
|
|
|
|
2014-03-27 16:15:56 +08:00
|
|
|
RT_ASSERT(cmd);
|
|
|
|
RT_ASSERT(retp);
|
|
|
|
|
|
|
|
/* find the size of first command */
|
2023-06-14 05:33:37 +08:00
|
|
|
while (cmd0_size < length && (cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t'))
|
2014-01-13 15:56:20 +08:00
|
|
|
cmd0_size ++;
|
2014-03-27 16:15:56 +08:00
|
|
|
if (cmd0_size == 0)
|
|
|
|
return -RT_ERROR;
|
2014-01-16 22:22:23 +08:00
|
|
|
|
2014-01-13 15:56:20 +08:00
|
|
|
cmd_func = msh_get_cmd(cmd, cmd0_size);
|
|
|
|
if (cmd_func == RT_NULL)
|
2014-03-27 16:15:56 +08:00
|
|
|
return -RT_ERROR;
|
2014-01-11 16:33:31 +08:00
|
|
|
|
2014-01-13 15:56:20 +08:00
|
|
|
/* split arguments */
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memset(argv, 0x00, sizeof(argv));
|
2014-01-11 16:33:31 +08:00
|
|
|
argc = msh_split(cmd, length, argv);
|
2014-03-27 16:15:56 +08:00
|
|
|
if (argc == 0)
|
|
|
|
return -RT_ERROR;
|
2013-03-30 16:14:38 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
/* exec this command */
|
2014-03-27 16:15:56 +08:00
|
|
|
*retp = cmd_func(argc, argv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-16 18:38:28 +08:00
|
|
|
#if defined(RT_USING_SMART) && defined(DFS_USING_POSIX)
|
2022-12-03 12:07:44 +08:00
|
|
|
pid_t exec(char*, int, int, char**);
|
|
|
|
|
|
|
|
/* check whether a file of the given path exits */
|
|
|
|
static rt_bool_t _msh_lwp_cmd_exists(const char *path)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
fd = open(path, O_RDONLY, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* search for the file named "pg_name" or "pg_name.elf" at the given directory,
|
|
|
|
* and return its path. return NULL when not found.
|
|
|
|
*/
|
|
|
|
static char *_msh_exec_search_path(const char *path, const char *pg_name)
|
|
|
|
{
|
|
|
|
char *path_buffer = RT_NULL;
|
|
|
|
ssize_t pg_len = strlen(pg_name);
|
|
|
|
ssize_t base_len = 0;
|
|
|
|
|
|
|
|
if (path)
|
|
|
|
{
|
|
|
|
base_len = strlen(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
path_buffer = rt_malloc(base_len + pg_len + 6);
|
|
|
|
if (path_buffer == RT_NULL)
|
|
|
|
{
|
|
|
|
return RT_NULL; /* no mem */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base_len > 0)
|
|
|
|
{
|
|
|
|
memcpy(path_buffer, path, base_len);
|
|
|
|
path_buffer[base_len] = '/';
|
|
|
|
path_buffer[base_len + 1] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*path_buffer = '\0';
|
|
|
|
}
|
|
|
|
strcat(path_buffer, pg_name);
|
2022-12-13 08:08:38 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (_msh_lwp_cmd_exists(path_buffer))
|
|
|
|
{
|
|
|
|
return path_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(path_buffer, ".elf") != NULL)
|
|
|
|
{
|
|
|
|
goto not_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat(path_buffer, ".elf");
|
|
|
|
if (_msh_lwp_cmd_exists(path_buffer))
|
|
|
|
{
|
|
|
|
return path_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
not_found:
|
|
|
|
rt_free(path_buffer);
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* search for the file named "pg_name" or "pg_name.elf" at each env path,
|
|
|
|
* and return its path. return NULL when not found.
|
|
|
|
*/
|
|
|
|
static char *_msh_exec_search_env(const char *pg_name)
|
|
|
|
{
|
|
|
|
char *result = RT_NULL;
|
|
|
|
char *exec_path = RT_NULL;
|
|
|
|
char *search_path = RT_NULL;
|
|
|
|
char *pos = RT_NULL;
|
|
|
|
char tmp_ch = '\0';
|
|
|
|
|
|
|
|
if (!(exec_path = getenv("PATH")))
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exec path may need to be modified */
|
|
|
|
if (!(exec_path = strdup(exec_path)))
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = exec_path;
|
|
|
|
search_path = exec_path;
|
|
|
|
|
|
|
|
/* walk through the entire exec_path until finding the program wanted
|
|
|
|
or hitting its end */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* env paths are seperated by ':' */
|
|
|
|
if (*pos == ':' || *pos == '\0')
|
|
|
|
{
|
|
|
|
tmp_ch = *pos;
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
result = _msh_exec_search_path(search_path, pg_name);
|
|
|
|
if (result || tmp_ch == '\0')
|
|
|
|
{
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
search_path = pos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release the duplicated exec_path and return */
|
|
|
|
ret:
|
|
|
|
rt_free(exec_path);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _msh_exec_lwp(int debug, char *cmd, rt_size_t length)
|
2018-07-02 13:47:47 +08:00
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
int cmd0_size = 0;
|
|
|
|
char *argv[FINSH_ARG_MAX];
|
|
|
|
char *pg_name;
|
2022-12-03 12:07:44 +08:00
|
|
|
int ret;
|
2018-07-02 13:47:47 +08:00
|
|
|
|
|
|
|
/* find the size of first command */
|
|
|
|
while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
|
|
|
|
cmd0_size ++;
|
|
|
|
if (cmd0_size == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* split arguments */
|
|
|
|
rt_memset(argv, 0x00, sizeof(argv));
|
|
|
|
argc = msh_split(cmd, length, argv);
|
|
|
|
if (argc == 0)
|
|
|
|
return -1;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* try to find program in working directory */
|
|
|
|
pg_name = _msh_exec_search_path("", argv[0]);
|
|
|
|
if (pg_name)
|
|
|
|
{
|
|
|
|
goto found_program;
|
|
|
|
}
|
2018-07-02 13:47:47 +08:00
|
|
|
|
2022-12-13 08:08:38 +08:00
|
|
|
/* only check these paths when the first argument doesn't contain path
|
2022-12-03 12:07:44 +08:00
|
|
|
seperator */
|
|
|
|
if (strstr(argv[0], "/"))
|
|
|
|
{
|
2018-07-02 13:47:47 +08:00
|
|
|
return -1;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* try to find program in /bin */
|
|
|
|
pg_name = _msh_exec_search_path("/bin", argv[0]);
|
|
|
|
if (pg_name)
|
|
|
|
{
|
|
|
|
goto found_program;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try to find program in dirs registered to env path */
|
|
|
|
pg_name = _msh_exec_search_env(argv[0]);
|
|
|
|
if (pg_name)
|
|
|
|
{
|
|
|
|
goto found_program;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found in anywhere */
|
|
|
|
return -1;
|
2018-07-02 13:47:47 +08:00
|
|
|
|
|
|
|
/* found program */
|
2022-12-03 12:07:44 +08:00
|
|
|
found_program:
|
|
|
|
ret = exec(pg_name, debug, argc, argv);
|
|
|
|
rt_free(pg_name);
|
2018-07-02 13:47:47 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return ret;
|
2018-07-02 13:47:47 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
#endif
|
|
|
|
|
2018-07-02 13:47:47 +08:00
|
|
|
|
2015-10-11 15:38:08 +08:00
|
|
|
int msh_exec(char *cmd, rt_size_t length)
|
2014-03-27 16:15:56 +08:00
|
|
|
{
|
|
|
|
int cmd_ret;
|
|
|
|
|
2015-10-11 15:38:08 +08:00
|
|
|
/* strim the beginning of command */
|
2021-05-22 19:50:49 +08:00
|
|
|
while ((length > 0) && (*cmd == ' ' || *cmd == '\t'))
|
2014-03-27 16:15:56 +08:00
|
|
|
{
|
|
|
|
cmd++;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
|
2014-03-29 11:33:20 +08:00
|
|
|
if (length == 0)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-27 16:15:56 +08:00
|
|
|
/* Exec sequence:
|
|
|
|
* 1. built-in command
|
|
|
|
* 2. module(if enabled)
|
|
|
|
*/
|
|
|
|
if (_msh_exec_cmd(cmd, length, &cmd_ret) == 0)
|
|
|
|
{
|
|
|
|
return cmd_ret;
|
|
|
|
}
|
2021-11-16 00:22:49 +08:00
|
|
|
#ifdef DFS_USING_POSIX
|
2018-09-01 11:00:42 +08:00
|
|
|
#ifdef DFS_USING_WORKDIR
|
|
|
|
if (msh_exec_script(cmd, length) == 0)
|
2014-03-27 16:15:56 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-09-11 12:47:25 +08:00
|
|
|
|
2018-09-01 11:00:42 +08:00
|
|
|
#ifdef RT_USING_MODULE
|
|
|
|
if (msh_exec_module(cmd, length) == 0)
|
2015-10-11 15:38:08 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2021-08-26 07:10:01 +08:00
|
|
|
#endif /* RT_USING_MODULE */
|
2014-09-11 12:47:25 +08:00
|
|
|
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef RT_USING_SMART
|
2022-12-03 12:07:44 +08:00
|
|
|
/* exec from msh_exec , debug = 0*/
|
|
|
|
/* _msh_exec_lwp return is pid , <= 0 means failed */
|
|
|
|
if (_msh_exec_lwp(0, cmd, length) > 0)
|
2018-07-02 13:47:47 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2022-12-16 18:38:28 +08:00
|
|
|
#endif /* RT_USING_SMART */
|
2021-11-16 00:22:49 +08:00
|
|
|
#endif /* DFS_USING_POSIX */
|
2018-07-02 13:47:47 +08:00
|
|
|
|
2014-03-27 16:15:56 +08:00
|
|
|
/* truncate the cmd at the first space. */
|
|
|
|
{
|
|
|
|
char *tcmd;
|
|
|
|
tcmd = cmd;
|
2015-10-11 15:38:08 +08:00
|
|
|
while (*tcmd != ' ' && *tcmd != '\0')
|
2014-03-27 16:15:56 +08:00
|
|
|
{
|
|
|
|
tcmd++;
|
|
|
|
}
|
|
|
|
*tcmd = '\0';
|
|
|
|
}
|
|
|
|
rt_kprintf("%s: command not found.\n", cmd);
|
|
|
|
return -1;
|
2013-03-30 16:14:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int str_common(const char *str1, const char *str2)
|
|
|
|
{
|
2014-01-03 08:01:24 +08:00
|
|
|
const char *str = str1;
|
2013-03-30 16:14:38 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
while ((*str != 0) && (*str2 != 0) && (*str == *str2))
|
|
|
|
{
|
|
|
|
str ++;
|
|
|
|
str2 ++;
|
|
|
|
}
|
2013-03-30 16:14:38 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
return (str - str1);
|
2013-03-30 16:14:38 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:22:49 +08:00
|
|
|
#ifdef DFS_USING_POSIX
|
2014-01-01 21:45:09 +08:00
|
|
|
void msh_auto_complete_path(char *path)
|
|
|
|
{
|
2015-10-11 15:38:08 +08:00
|
|
|
DIR *dir = RT_NULL;
|
2014-04-02 14:05:15 +08:00
|
|
|
struct dirent *dirent = RT_NULL;
|
2014-01-03 08:01:24 +08:00
|
|
|
char *full_path, *ptr, *index;
|
|
|
|
|
2014-10-14 16:07:19 +08:00
|
|
|
if (!path)
|
|
|
|
return;
|
|
|
|
|
2015-10-11 15:38:08 +08:00
|
|
|
full_path = (char *)rt_malloc(256);
|
2014-01-03 08:01:24 +08:00
|
|
|
if (full_path == RT_NULL) return; /* out of memory */
|
|
|
|
|
2014-03-27 15:09:53 +08:00
|
|
|
if (*path != '/')
|
2014-01-03 08:01:24 +08:00
|
|
|
{
|
|
|
|
getcwd(full_path, 256);
|
|
|
|
if (full_path[rt_strlen(full_path) - 1] != '/')
|
|
|
|
strcat(full_path, "/");
|
|
|
|
}
|
|
|
|
else *full_path = '\0';
|
|
|
|
|
2014-04-12 16:57:14 +08:00
|
|
|
index = RT_NULL;
|
|
|
|
ptr = path;
|
2014-01-03 08:01:24 +08:00
|
|
|
for (;;)
|
|
|
|
{
|
2018-09-01 11:00:42 +08:00
|
|
|
if (*ptr == '/') index = ptr + 1;
|
|
|
|
if (!*ptr) break;
|
2018-03-04 12:16:52 +08:00
|
|
|
|
|
|
|
ptr ++;
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
|
|
|
if (index == RT_NULL) index = path;
|
|
|
|
|
|
|
|
if (index != RT_NULL)
|
|
|
|
{
|
|
|
|
char *dest = index;
|
|
|
|
|
|
|
|
/* fill the parent path */
|
2014-03-27 15:09:53 +08:00
|
|
|
ptr = full_path;
|
2014-01-03 08:01:24 +08:00
|
|
|
while (*ptr) ptr ++;
|
|
|
|
|
|
|
|
for (index = path; index != dest;)
|
|
|
|
*ptr++ = *index++;
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
dir = opendir(full_path);
|
|
|
|
if (dir == RT_NULL) /* open directory failed! */
|
|
|
|
{
|
|
|
|
rt_free(full_path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore the index position */
|
|
|
|
index = dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* auto complete the file or directory name */
|
|
|
|
if (*index == '\0') /* display all of files and directories */
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
dirent = readdir(dir);
|
|
|
|
if (dirent == RT_NULL) break;
|
2014-03-27 15:09:53 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
rt_kprintf("%s\n", dirent->d_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
int multi = 0;
|
2018-04-09 11:58:11 +08:00
|
|
|
rt_size_t length, min_length;
|
2014-01-03 08:01:24 +08:00
|
|
|
|
|
|
|
min_length = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
dirent = readdir(dir);
|
|
|
|
if (dirent == RT_NULL) break;
|
|
|
|
|
|
|
|
/* matched the prefix string */
|
|
|
|
if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
multi ++;
|
2014-01-03 08:01:24 +08:00
|
|
|
if (min_length == 0)
|
|
|
|
{
|
|
|
|
min_length = rt_strlen(dirent->d_name);
|
|
|
|
/* save dirent name */
|
|
|
|
strcpy(full_path, dirent->d_name);
|
|
|
|
}
|
2015-10-11 15:38:08 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
length = str_common(dirent->d_name, full_path);
|
2015-10-11 15:38:08 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
if (length < min_length)
|
|
|
|
{
|
|
|
|
min_length = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min_length)
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
if (multi > 1)
|
2014-01-03 08:01:24 +08:00
|
|
|
{
|
|
|
|
/* list the candidate */
|
|
|
|
rewinddir(dir);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
dirent = readdir(dir);
|
|
|
|
if (dirent == RT_NULL) break;
|
|
|
|
|
|
|
|
if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
|
|
|
|
rt_kprintf("%s\n", dirent->d_name);
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 15:38:08 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
length = index - path;
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(index, full_path, min_length);
|
2014-01-03 08:01:24 +08:00
|
|
|
path[length + min_length] = '\0';
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
/* try to locate folder */
|
|
|
|
if (multi == 1)
|
|
|
|
{
|
|
|
|
struct stat buffer = {0};
|
2023-06-10 21:35:25 +08:00
|
|
|
if ((stat(path, &buffer) == 0))
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
if (S_ISDIR(buffer.st_mode))
|
|
|
|
{
|
|
|
|
strcat(path, "/");
|
|
|
|
}
|
|
|
|
else if (S_ISLNK(buffer.st_mode))
|
|
|
|
{
|
|
|
|
DIR *dir = opendir(path);
|
|
|
|
if (dir)
|
|
|
|
{
|
|
|
|
closedir(dir);
|
|
|
|
strcat(path, "/");
|
|
|
|
}
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
rt_free(full_path);
|
2014-01-01 21:45:09 +08:00
|
|
|
}
|
2021-11-16 00:22:49 +08:00
|
|
|
#endif /* DFS_USING_POSIX */
|
2014-01-01 21:45:09 +08:00
|
|
|
|
2013-03-30 16:14:38 +08:00
|
|
|
void msh_auto_complete(char *prefix)
|
|
|
|
{
|
2014-01-03 08:01:24 +08:00
|
|
|
int length, min_length;
|
|
|
|
const char *name_ptr, *cmd_name;
|
|
|
|
struct finsh_syscall *index;
|
2013-03-30 16:14:38 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
min_length = 0;
|
|
|
|
name_ptr = RT_NULL;
|
2013-03-30 16:14:38 +08:00
|
|
|
|
2015-10-11 15:38:08 +08:00
|
|
|
if (*prefix == '\0')
|
2014-01-03 08:01:24 +08:00
|
|
|
{
|
|
|
|
msh_help(0, RT_NULL);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-30 16:14:38 +08:00
|
|
|
|
2021-11-16 00:22:49 +08:00
|
|
|
#ifdef DFS_USING_POSIX
|
2014-01-03 08:01:24 +08:00
|
|
|
/* check whether a spare in the command */
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
ptr = prefix + rt_strlen(prefix);
|
|
|
|
while (ptr != prefix)
|
|
|
|
{
|
|
|
|
if (*ptr == ' ')
|
|
|
|
{
|
|
|
|
msh_auto_complete_path(ptr + 1);
|
|
|
|
break;
|
|
|
|
}
|
2014-03-27 15:09:53 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
ptr --;
|
|
|
|
}
|
2022-12-16 18:38:28 +08:00
|
|
|
#if defined(RT_USING_MODULE) || defined(RT_USING_SMART)
|
2014-03-27 15:09:53 +08:00
|
|
|
/* 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);
|
|
|
|
}
|
2021-08-26 07:10:01 +08:00
|
|
|
#endif /* RT_USING_MODULE */
|
2014-01-03 08:01:24 +08:00
|
|
|
}
|
2021-11-16 00:22:49 +08:00
|
|
|
#endif /* DFS_USING_POSIX */
|
2014-03-27 15:09:53 +08:00
|
|
|
|
2014-01-03 08:01:24 +08:00
|
|
|
/* checks in internal command */
|
|
|
|
{
|
|
|
|
for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
|
|
|
|
{
|
|
|
|
/* skip finsh shell function */
|
2021-08-25 09:31:35 +08:00
|
|
|
cmd_name = (const char *) index->name;
|
2014-01-03 08:01:24 +08:00
|
|
|
if (strncmp(prefix, cmd_name, strlen(prefix)) == 0)
|
|
|
|
{
|
|
|
|
if (min_length == 0)
|
|
|
|
{
|
|
|
|
/* set name_ptr */
|
|
|
|
name_ptr = cmd_name;
|
|
|
|
/* set initial length */
|
|
|
|
min_length = strlen(name_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
length = str_common(name_ptr, cmd_name);
|
|
|
|
if (length < min_length)
|
|
|
|
min_length = length;
|
|
|
|
|
|
|
|
rt_kprintf("%s\n", cmd_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* auto complete string */
|
|
|
|
if (name_ptr != NULL)
|
|
|
|
{
|
|
|
|
rt_strncpy(prefix, name_ptr, min_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ;
|
2013-03-30 16:14:38 +08:00
|
|
|
}
|
2021-08-23 17:30:05 +08:00
|
|
|
#endif /* RT_USING_FINSH */
|