| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013 |
- /*
- * Copyright (c) 2006-2022, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2013-03-30 Bernard the first verion for finsh
- * 2014-01-03 Bernard msh can execute module.
- * 2017-07-19 Aubr.Cool limit argc to RT_FINSH_ARG_MAX
- */
- #include <rtthread.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #ifdef RT_USING_FINSH
- #ifndef FINSH_ARG_MAX
- #define FINSH_ARG_MAX 8
- #endif /* FINSH_ARG_MAX */
- #include "msh.h"
- #include "shell.h"
- #ifdef DFS_USING_POSIX
- #include <dfs_file.h>
- #include <unistd.h>
- #include <fcntl.h>
- #endif /* DFS_USING_POSIX */
- #ifdef RT_USING_MODULE
- #include <dlmodule.h>
- #endif /* RT_USING_MODULE */
- typedef int (*cmd_function_t)(int argc, char **argv);
- static int msh_help(int argc, char **argv)
- {
- rt_kprintf("RT-Thread shell commands:\n");
- {
- struct finsh_syscall *index;
- #if defined(FINSH_USING_SYMTAB)
- for (index = _syscall_table_begin;
- index < _syscall_table_end;
- FINSH_NEXT_SYSCALL(index))
- {
- #if defined(FINSH_USING_DESCRIPTION)
- rt_kprintf("%-16s - %s\n", index->name, index->desc);
- #else
- rt_kprintf("%s ", index->name);
- #endif /* FINSH_USING_DESCRIPTION */
- }
- #endif /* FINSH_USING_SYMTAB */
- }
- rt_kprintf("\n");
- return 0;
- }
- MSH_CMD_EXPORT_ALIAS(msh_help, help, RT-Thread shell help);
- #ifdef MSH_USING_BUILT_IN_COMMANDS
- static int cmd_ps(int argc, char **argv)
- {
- extern long list_thread(void);
- extern int list_module(void);
- #ifdef RT_USING_MODULE
- if ((argc == 2) && (rt_strcmp(argv[1], "-m") == 0))
- list_module();
- else
- #endif
- list_thread();
- return 0;
- }
- MSH_CMD_EXPORT_ALIAS(cmd_ps, ps, List threads in the system);
- #ifdef RT_USING_HEAP
- static int cmd_free(int argc, char **argv)
- {
- #ifdef RT_USING_MEMHEAP_AS_HEAP
- extern void list_memheap(void);
- list_memheap();
- #else
- rt_size_t total = 0, used = 0, max_used = 0;
- rt_memory_info(&total, &used, &max_used);
- 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);
- #endif
- return 0;
- }
- MSH_CMD_EXPORT_ALIAS(cmd_free, free, Show the memory usage in the system);
- #endif /* RT_USING_HEAP */
- #if RT_CPUS_NR > 1
- static int cmd_bind(int argc, char **argv)
- {
- rt_err_t result;
- rt_ubase_t thread_id;
- rt_ubase_t core_id;
- rt_thread_t thread;
- char *endptr;
- if (argc != 3)
- {
- rt_kprintf("Usage: bind <thread_id> <core_id>\n");
- return 0;
- }
- /* Parse thread_id */
- thread_id = (rt_ubase_t)strtoul(argv[1], &endptr, 0);
- if (*endptr != '\0')
- {
- rt_kprintf("Error: Invalid thread ID '%s'\n", argv[1]);
- return 0;
- }
- /* Parse core_id */
- core_id = (rt_uint8_t)strtoul(argv[2], &endptr, 0);
- if (*endptr != '\0')
- {
- rt_kprintf("Error: Invalid core ID '%s'\n", argv[2]);
- return 0;
- }
- thread = (rt_thread_t)thread_id;
- if (rt_object_get_type(&thread->parent) != RT_Object_Class_Thread)
- {
- rt_kprintf("Error: Invalid thread ID %#lx\n", thread_id);
- return 0;
- }
- result = rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void *)core_id);
- if (result == RT_EOK)
- {
- rt_kprintf("Thread 0x%lx bound to core %d successfully\n",
- thread_id, core_id);
- }
- else
- {
- rt_kprintf("Failed to bind thread 0x%lx to core %d\n",
- thread_id, core_id);
- }
- return 0;
- }
- MSH_CMD_EXPORT_ALIAS(cmd_bind, bind, Binding thread to core);
- #endif /* RT_CPUS_NR > 1 */
- #endif /* MSH_USING_BUILT_IN_COMMANDS */
- static int msh_split(char *cmd, rt_size_t length, char *argv[FINSH_ARG_MAX])
- {
- char *ptr;
- rt_size_t position;
- rt_size_t argc;
- rt_size_t i;
- ptr = cmd;
- position = 0;
- argc = 0;
- while (position < length)
- {
- /* strip bank and tab */
- while ((*ptr == ' ' || *ptr == '\t') && position < length)
- {
- *ptr = '\0';
- ptr ++;
- position ++;
- }
- if (argc >= FINSH_ARG_MAX)
- {
- rt_kprintf("Too many args ! We only Use:\n");
- for (i = 0; i < argc; i++)
- {
- rt_kprintf("%s ", argv[i]);
- }
- rt_kprintf("\n");
- break;
- }
- if (position >= length) break;
- /* handle string */
- if (*ptr == '"')
- {
- ptr ++;
- position ++;
- argv[argc] = ptr;
- argc ++;
- /* skip this string */
- while (*ptr != '"' && position < length)
- {
- if (*ptr == '\\')
- {
- if (*(ptr + 1) == '"')
- {
- ptr ++;
- position ++;
- }
- }
- ptr ++;
- position ++;
- }
- if (position >= length) break;
- /* skip '"' */
- *ptr = '\0';
- ptr ++;
- position ++;
- }
- else
- {
- argv[argc] = ptr;
- argc ++;
- while ((*ptr != ' ' && *ptr != '\t') && position < length)
- {
- ptr ++;
- position ++;
- }
- if (position >= length) break;
- }
- }
- return argc;
- }
- static cmd_function_t msh_get_cmd(char *cmd, int size)
- {
- struct finsh_syscall *index;
- cmd_function_t cmd_func = RT_NULL;
- #if defined(FINSH_USING_SYMTAB)
- for (index = _syscall_table_begin;
- index < _syscall_table_end;
- FINSH_NEXT_SYSCALL(index))
- {
- if (rt_strncmp(index->name, cmd, size) == 0 &&
- index->name[size] == '\0')
- {
- cmd_func = (cmd_function_t)index->func;
- break;
- }
- }
- #endif /* FINSH_USING_SYMTAB */
- return cmd_func;
- }
- #if defined(RT_USING_MODULE) && defined(DFS_USING_POSIX)
- /* Return 0 on module executed. Other value indicate error.
- */
- int msh_exec_module(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 + 3);
- if (pg_name == RT_NULL)
- return -RT_ENOMEM;
- /* copy command0 */
- rt_memcpy(pg_name, cmd_line, cmd_length);
- pg_name[cmd_length] = '\0';
- if (rt_strstr(pg_name, ".mo") != RT_NULL || rt_strstr(pg_name, ".MO") != 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);
- }
- }
- else
- {
- /* add .mo and open program */
- /* try to open program */
- strcat(pg_name, ".mo");
- fd = open(pg_name, O_RDONLY, 0);
- /* search in /bin path */
- if (fd < 0)
- {
- rt_snprintf(pg_name, length - 1, "/bin/%.*s.mo", cmd_length, cmd_line);
- fd = open(pg_name, O_RDONLY, 0);
- }
- }
- if (fd >= 0)
- {
- /* found program */
- close(fd);
- dlmodule_exec(pg_name, cmd_line, size);
- ret = 0;
- }
- else
- {
- ret = -1;
- }
- rt_free(pg_name);
- return ret;
- }
- #endif
- static int _msh_exec_cmd(char *cmd, rt_size_t length, int *retp)
- {
- int argc;
- rt_size_t cmd0_size = 0;
- cmd_function_t cmd_func;
- char *argv[FINSH_ARG_MAX];
- RT_ASSERT(cmd);
- RT_ASSERT(retp);
- /* find the size of first command */
- while (cmd0_size < length && (cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t'))
- cmd0_size ++;
- if (cmd0_size == 0)
- return -RT_ERROR;
- cmd_func = msh_get_cmd(cmd, cmd0_size);
- if (cmd_func == RT_NULL)
- return -RT_ERROR;
- /* split arguments */
- rt_memset(argv, 0x00, sizeof(argv));
- argc = msh_split(cmd, length, argv);
- if (argc == 0)
- return -RT_ERROR;
- /* exec this command */
- *retp = cmd_func(argc, argv);
- return 0;
- }
- #if defined(RT_USING_SMART) && defined(DFS_USING_POSIX)
- #include <lwp.h>
- /* 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 = rt_strlen(pg_name);
- ssize_t base_len = 0;
- if (path)
- {
- base_len = rt_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)
- {
- rt_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);
- if (_msh_lwp_cmd_exists(path_buffer))
- {
- return path_buffer;
- }
- if (rt_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 separated 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)
- {
- int argc;
- int cmd0_size = 0;
- char *argv[FINSH_ARG_MAX];
- char *pg_name;
- int ret;
- /* 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;
- /* try to find program in working directory */
- pg_name = _msh_exec_search_path("", argv[0]);
- if (pg_name)
- {
- goto found_program;
- }
- /* only check these paths when the first argument doesn't contain path
- seperator */
- if (rt_strstr(argv[0], "/"))
- {
- return -1;
- }
- /* 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;
- /* found program */
- found_program:
- ret = exec(pg_name, debug, argc, argv);
- rt_free(pg_name);
- return ret;
- }
- #endif
- int msh_exec(char *cmd, rt_size_t length)
- {
- int cmd_ret = 0;
- /* strim the beginning of command */
- while ((length > 0) && (*cmd == ' ' || *cmd == '\t'))
- {
- cmd++;
- length--;
- }
- if (length == 0)
- return 0;
- /* Exec sequence:
- * 1. built-in command
- * 2. module(if enabled)
- */
- if (_msh_exec_cmd(cmd, length, &cmd_ret) == 0)
- {
- if(cmd_ret < 0)
- {
- rt_kprintf("%s: command failed %d.\n", cmd, cmd_ret);
- }
- return cmd_ret;
- }
- #ifdef DFS_USING_POSIX
- #ifdef DFS_USING_WORKDIR
- if (msh_exec_script(cmd, length) == 0)
- {
- return 0;
- }
- #endif
- #ifdef RT_USING_MODULE
- if (msh_exec_module(cmd, length) == 0)
- {
- return 0;
- }
- #endif /* RT_USING_MODULE */
- #ifdef RT_USING_SMART
- /* exec from msh_exec , debug = 0*/
- /* _msh_exec_lwp return is pid , <= 0 means failed */
- cmd_ret = _msh_exec_lwp(0, cmd, length);
- if (cmd_ret > 0)
- {
- return 0;
- }
- #endif /* RT_USING_SMART */
- #endif /* DFS_USING_POSIX */
- /* truncate the cmd at the first space. */
- {
- char *tcmd;
- tcmd = cmd;
- while (*tcmd != ' ' && *tcmd != '\0')
- {
- tcmd++;
- }
- *tcmd = '\0';
- }
- #ifdef RT_USING_SMART
- if (cmd_ret == -EACCES)
- {
- rt_kprintf("%s: Permission denied.\n", cmd);
- }
- else
- #endif
- {
- rt_kprintf("%s: command not found.\n", cmd);
- }
- return -1;
- }
- static int str_common(const char *str1, const char *str2)
- {
- const char *str = str1;
- while ((*str != 0) && (*str2 != 0) && (*str == *str2))
- {
- str ++;
- str2 ++;
- }
- return (str - str1);
- }
- #ifdef DFS_USING_POSIX
- void msh_auto_complete_path(char *path)
- {
- DIR *dir = RT_NULL;
- struct dirent *dirent = RT_NULL;
- char *full_path, *ptr, *index;
- if (!path)
- return;
- full_path = (char *)rt_malloc(256);
- if (full_path == RT_NULL) return; /* out of memory */
- if (*path != '/')
- {
- getcwd(full_path, 256);
- if (full_path[rt_strlen(full_path) - 1] != '/')
- strcat(full_path, "/");
- }
- else *full_path = '\0';
- index = RT_NULL;
- ptr = path;
- for (;;)
- {
- if (*ptr == '/') index = ptr + 1;
- if (!*ptr) break;
- ptr ++;
- }
- if (index == RT_NULL) index = path;
- if (index != RT_NULL)
- {
- char *dest = index;
- /* fill the parent path */
- ptr = full_path;
- 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;
- rt_kprintf("%s\n", dirent->d_name);
- }
- }
- else
- {
- int multi = 0;
- rt_size_t length, min_length;
- min_length = 0;
- for (;;)
- {
- dirent = readdir(dir);
- if (dirent == RT_NULL) break;
- /* matched the prefix string */
- if (rt_strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
- {
- multi ++;
- if (min_length == 0)
- {
- min_length = rt_strlen(dirent->d_name);
- /* save dirent name */
- rt_strcpy(full_path, dirent->d_name);
- }
- length = str_common(dirent->d_name, full_path);
- if (length < min_length)
- {
- min_length = length;
- }
- }
- }
- if (min_length)
- {
- if (multi > 1)
- {
- /* list the candidate */
- rewinddir(dir);
- for (;;)
- {
- dirent = readdir(dir);
- if (dirent == RT_NULL) break;
- if (rt_strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
- rt_kprintf("%s\n", dirent->d_name);
- }
- }
- length = index - path;
- rt_memcpy(index, full_path, min_length);
- path[length + min_length] = '\0';
- /* try to locate folder */
- if (multi == 1)
- {
- struct stat buffer = {0};
- if ((stat(path, &buffer) == 0))
- {
- if (S_ISDIR(buffer.st_mode))
- {
- strcat(path, "/");
- }
- else if (S_ISLNK(buffer.st_mode))
- {
- DIR *link_dir = opendir(path);
- if (link_dir)
- {
- closedir(link_dir);
- strcat(path, "/");
- }
- }
- }
- }
- }
- }
- closedir(dir);
- rt_free(full_path);
- }
- #endif /* DFS_USING_POSIX */
- void msh_auto_complete(char *prefix)
- {
- int length, min_length;
- const char *name_ptr, *cmd_name;
- struct finsh_syscall *index;
- min_length = 0;
- name_ptr = RT_NULL;
- if (*prefix == '\0')
- {
- msh_help(0, RT_NULL);
- return;
- }
- #ifdef DFS_USING_POSIX
- /* 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;
- }
- ptr --;
- }
- #if defined(RT_USING_MODULE) || defined(RT_USING_SMART)
- /* 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 /* RT_USING_MODULE */
- }
- #endif /* DFS_USING_POSIX */
- #if defined(FINSH_USING_SYMTAB)
- /* checks in internal command */
- {
- for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
- {
- /* skip finsh shell function */
- cmd_name = (const char *) index->name;
- if (rt_strncmp(prefix, cmd_name, rt_strlen(prefix)) == 0)
- {
- if (min_length == 0)
- {
- /* set name_ptr */
- name_ptr = cmd_name;
- /* set initial length */
- min_length = rt_strlen(name_ptr);
- }
- length = str_common(name_ptr, cmd_name);
- if (length < min_length)
- min_length = length;
- rt_kprintf("%s\n", cmd_name);
- }
- }
- }
- #endif /* FINSH_USING_SYMTAB */
- /* auto complete string */
- if (name_ptr != NULL)
- {
- rt_strncpy(prefix, name_ptr, min_length);
- }
- return ;
- }
- #ifdef FINSH_USING_OPTION_COMPLETION
- static msh_cmd_opt_t *msh_get_cmd_opt(char *opt_str)
- {
- struct finsh_syscall *index;
- msh_cmd_opt_t *opt = RT_NULL;
- char *ptr;
- int len;
- ptr = strchr(opt_str, ' ');
- if (ptr)
- {
- len = ptr - opt_str;
- }
- else
- {
- len = rt_strlen(opt_str);
- }
- #if defined(FINSH_USING_SYMTAB)
- for (index = _syscall_table_begin;
- index < _syscall_table_end;
- FINSH_NEXT_SYSCALL(index))
- {
- if (rt_strncmp(index->name, opt_str, len) == 0 && index->name[len] == '\0')
- {
- opt = index->opt;
- break;
- }
- }
- #endif /* FINSH_USING_SYMTAB */
- return opt;
- }
- static int msh_get_argc(char *prefix, char **last_argv)
- {
- int argc = 0;
- char *ch = prefix;
- while (*ch)
- {
- if ((*ch == ' ') && *(ch + 1) && (*(ch + 1) != ' '))
- {
- *last_argv = ch + 1;
- argc++;
- }
- ch++;
- }
- return argc;
- }
- static void msh_opt_complete(char *opts_str, struct msh_cmd_opt *cmd_opt)
- {
- struct msh_cmd_opt *opt = cmd_opt;
- const char *name_ptr = RT_NULL;
- int min_length = 0, length, opts_str_len;
- opts_str_len = rt_strlen(opts_str);
- for (opt = cmd_opt; opt->id; opt++)
- {
- if (!rt_strncmp(opt->name, opts_str, opts_str_len))
- {
- if (min_length == 0)
- {
- /* set name_ptr */
- name_ptr = opt->name;
- /* set initial length */
- min_length = rt_strlen(name_ptr);
- }
- length = str_common(name_ptr, opt->name);
- if (length < min_length)
- {
- min_length = length;
- }
- rt_kprintf("%s\n", opt->name);
- }
- }
- rt_kprintf("\n");
- if (name_ptr != NULL)
- {
- rt_strncpy(opts_str, name_ptr, min_length);
- }
- }
- static void msh_opt_help(msh_cmd_opt_t *cmd_opt)
- {
- msh_cmd_opt_t *opt = cmd_opt;
- for (; opt->id; opt++)
- {
- rt_kprintf("%-16s - %s\n", opt->name, opt->des);
- }
- rt_kprintf("\n");
- }
- void msh_opt_auto_complete(char *prefix)
- {
- int argc;
- char *opt_str = RT_NULL;
- msh_cmd_opt_t *opt = RT_NULL;
- argc = msh_get_argc(prefix, &opt_str);
- if (argc)
- {
- opt = msh_get_cmd_opt(prefix);
- }
- else if (!msh_get_cmd(prefix, rt_strlen(prefix)) && (' ' == prefix[rt_strlen(prefix) - 1]))
- {
- opt = msh_get_cmd_opt(prefix);
- }
- if (opt && opt->id)
- {
- switch (argc)
- {
- case 0:
- msh_opt_help(opt);
- break;
- case 1:
- msh_opt_complete(opt_str, opt);
- break;
- default:
- break;
- }
- }
- }
- int msh_cmd_opt_id_get(int argc, char *argv[], void *options)
- {
- msh_cmd_opt_t *opt = (msh_cmd_opt_t *) options;
- int opt_id;
- for (opt_id = 0; (argc >= 2) && opt && opt->id; opt++)
- {
- if (!rt_strcmp(opt->name, argv[1]))
- {
- opt_id = opt->id;
- break;
- }
- }
- return opt_id;
- }
- void msh_opt_list_dump(void *options)
- {
- msh_cmd_opt_t *opt = (msh_cmd_opt_t *) options;
- for (; opt && opt->id; opt++)
- {
- rt_kprintf(" %-16s - %s\n", opt->name, opt->des);
- }
- }
- #endif /* FINSH_USING_OPTION_COMPLETION */
- #endif /* RT_USING_FINSH */
|