44#define SHELL_INIT_MAGIC_FLAG 0xABABABAB
60 for (i = 0; i < cmdParsed->
paramCnt; i++) {
70 CHAR *shiftStr = NULL;
72 if (tempStr == NULL) {
73 return (
INT32)OS_ERROR;
76 (VOID)memset_s(tempStr, SHOW_MAX_LEN << 1, 0, SHOW_MAX_LEN << 1);
77 shiftStr = tempStr + SHOW_MAX_LEN;
79 if (strncpy_s(tempStr, SHOW_MAX_LEN - 1, *tabStr, tabStrLen)) {
81 return (
INT32)OS_ERROR;
89 return (
INT32)OS_ERROR;
94 if ((strlen(shiftStr) == 0) || (tempStr[strlen(tempStr) - 1] != shiftStr[strlen(shiftStr) - 1])) {
99 return (
INT32)OS_ERROR;
122 if (*tabStr !=
'/') {
123 if (strncpy_s(strPath, CMD_MAX_PATH, shellWorkingDirectory, CMD_MAX_PATH - 1)) {
125 return (
INT32)OS_ERROR;
127 if (strcmp(shellWorkingDirectory,
"/")) {
128 if (strncat_s(strPath, CMD_MAX_PATH,
"/", CMD_MAX_PATH - strlen(strPath) - 1)) {
130 return (
INT32)OS_ERROR;
135 if (strncat_s(strPath, CMD_MAX_PATH, tabStr, CMD_MAX_PATH - strlen(strPath) - 1)) {
137 return (
INT32)OS_ERROR;
141 strEnd = strrchr(strPath,
'/');
143 if (strEnd != NULL) {
144 if (strncpy_s(nameLooking, CMD_MAX_PATH, strEnd + 1, CMD_MAX_PATH - 1)) {
146 return (
INT32)OS_ERROR;
148 *(cutPos + 1) =
'\0';
160 if (read(STDIN_FILENO, &readChar, 1) != 1) {
162 return (
INT32)OS_ERROR;
164 if ((readChar ==
'q') || (readChar ==
'Q') || (readChar == CTRL_C)) {
167 }
else if (readChar ==
'\r') {
168 PRINTK(
"\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b");
176 if (NEED_NEW_LINE(timesPrint, lineCap)) {
178 if (SCREEN_IS_FULL(timesPrint, lineCap) && (timesPrint < count)) {
189 PRINTK(
"\nDisplay all %u possibilities?(y/n)", count);
191 if (read(0, &readChar, 1) != 1) {
192 return (
INT32)OS_ERROR;
194 if ((readChar ==
'n') || (readChar ==
'N') || (readChar == CTRL_C)) {
197 }
else if ((readChar ==
'y') || (readChar ==
'Y') || (readChar ==
'\r')) {
209 struct dirent *readDir = NULL;
210 CHAR formatChar[10] = {0};
212 printLen = (printLen > (DEFAULT_SCREEN_WIDTH - 2)) ? (DEFAULT_SCREEN_WIDTH - 2) : printLen;
213 lineCap = DEFAULT_SCREEN_WIDTH / (printLen + 2);
214 if (snprintf_s(formatChar,
sizeof(formatChar) - 1, 7,
"%%-%us ", printLen) < 0) {
215 return (
INT32)OS_ERROR;
218 if (count > (lineCap * DEFAULT_SCREEN_HEIGHT)) {
224 openDir = opendir(strPath);
225 if (openDir == NULL) {
226 return (
INT32)OS_ERROR;
230 for (readDir = readdir(openDir); readDir != NULL; readDir = readdir(openDir)) {
231 if (strncmp(nameLooking, readDir->d_name, strlen(nameLooking)) != 0) {
234 PRINTK(formatChar, readDir->d_name);
238 if (closedir(openDir) < 0) {
239 return (
INT32)OS_ERROR;
246 if (closedir(openDir) < 0) {
247 return (
INT32)OS_ERROR;
255 if ((n == 0) || (s1 == NULL) || (s2 == NULL)) {
259 if (*s1 && *s2 && (*s1 == *s2)) {
279 struct dirent *readDir = NULL;
281 openDir = opendir(strPath);
282 if (openDir == NULL) {
283 return (
INT32)OS_ERROR;
286 for (readDir = readdir(openDir); readDir != NULL; readDir = readdir(openDir)) {
287 if (strncmp(nameLooking, readDir->d_name, strlen(nameLooking)) != 0) {
291 if (strncpy_s(strObj, CMD_MAX_PATH, readDir->d_name, CMD_MAX_PATH - 1)) {
292 (VOID)closedir(openDir);
293 return (
INT32)OS_ERROR;
295 *maxLen = strlen(readDir->d_name);
298 strncmp_cut(readDir->d_name, strObj, strlen(strObj));
299 if (strlen(readDir->d_name) > *maxLen) {
300 *maxLen = strlen(readDir->d_name);
306 if (closedir(openDir) < 0) {
307 return (
INT32)OS_ERROR;
315 UINT32 size = strlen(result) - strlen(target);
316 CHAR *des = cmdKey + *len;
317 CHAR *src = (
CHAR *)result + strlen(target);
321 if (*len == (SHOW_MAX_LEN - 1)) {
341 const CHAR *cmdMajor = (
const CHAR *)cmdKey;
343 while (*cmdMajor == 0x20) {
348 return (
INT32)OS_ERROR;
352 if ((curCmdItem == NULL) || (curCmdItem->
cmd == NULL)) {
356 if (strncmp(cmdMajor, curCmdItem->
cmd->
cmdKey, strlen(cmdMajor)) > 0) {
360 if (strncmp(cmdMajor, curCmdItem->
cmd->
cmdKey, strlen(cmdMajor)) != 0) {
365 cmdItemGuard = curCmdItem;
370 if (cmdItemGuard == NULL) {
382 PRINTK(
"%s ", cmdItemGuard->
cmd->
cmdKey);
395 CHAR *strOutput = NULL;
398 if (dirOpen == NULL) {
399 return (
INT32)OS_ERROR;
402 (VOID)memset_s(dirOpen, CMD_MAX_PATH * 3, 0, CMD_MAX_PATH * 3);
403 strOutput = dirOpen + CMD_MAX_PATH;
404 strCmp = strOutput + CMD_MAX_PATH;
408 return (
INT32)OS_ERROR;
422 return (
INT32)OS_ERROR;
442 CHAR *outputBak = NULL;
447 if ((cmdKey == NULL) || (cmdOut == NULL)) {
451 len = strlen(cmdKey);
456 if (output == NULL) {
457 PRINTK(
"malloc failure in %s[%d]", __FUNCTION__, __LINE__);
463 for (; *cmdKey !=
'\0'; cmdKey++) {
465 if (*(cmdKey) ==
'\"') {
466 SWITCH_QUOTES_STATUS(quotes);
473 if ((*cmdKey ==
' ') && ((*(cmdKey + 1) ==
' ') || (*(cmdKey + 1) ==
'\0')) && QUOTES_STATUS_CLOSE(quotes)) {
476 if (*cmdKey ==
'\'') {
485 len = strlen(output);
487 if (*outputBak ==
' ') {
492 ret = strncpy_s(cmdOut, size, output, len);
494 PRINT_ERR(
"%s,%d strncpy_s failed, err:%d!\n", __FUNCTION__, __LINE__, ret);
507 const CHAR *temp = cmdKey;
514 if (strlen(cmdKey) >= CMD_KEY_LEN) {
518 while (*temp !=
'\0') {
519 if (!((*temp <=
'9') && (*temp >=
'0')) &&
520 !((*temp <=
'z') && (*temp >=
'a')) &&
521 !((*temp <=
'Z') && (*temp >=
'A')) &&
522 (*temp !=
'_') && (*temp !=
'-')) {
526 if ((*temp >=
'0') && (*temp <=
'9')) {
527 if (state == STAT_NONE) {
537 if (state == STAT_DIGIT) {
548 CHAR *cmdMainStr = cmdKey;
550 if ((cmdKey == NULL) || (len == NULL)) {
551 return (
INT32)OS_ERROR;
555 while (*cmdMainStr == 0x20) {
560 space = strrchr(cmdMainStr, 0x20);
561 if ((space == NULL) && (*cmdMainStr !=
'\0')) {
607 if (shellCB == NULL) {
611 if (cmdKeyLink == NULL) {
612 PRINT_ERR(
"Shell CmdKeyLink memory alloc error!\n");
616 if (cmdHistoryLink == NULL) {
618 PRINT_ERR(
"Shell CmdHistoryLink memory alloc error!\n");
622 cmdKeyLink->
count = 0;
626 cmdHistoryLink->
count = 0;
636 if (cmdKeyLink == NULL) {
646 cmdKeyLink->
count = 0;
653 UINT8 *cmdItemGroup = NULL;
658 if (cmdItemGroup == NULL) {
659 PRINT_ERR(
"[%s]System memory allocation failure!\n", __FUNCTION__);
663 for (i = 0; i < index; ++i) {
677 if ((
string == NULL) || (strlen(
string) == 0)) {
681 len = strlen(
string);
683 if (cmdNewNode == NULL) {
688 if (strncpy_s(cmdNewNode->
cmdString, len + 1,
string, len)) {
707 if (cmdMask == cmdNode) {
712 if (cmdtmp != cmdNode) {
719 if (cmdtmp != cmdNode) {
731 (VOID)memset_s(shellCB->
shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN);
734 PRINT_ERR(
"%s, %d memcpy failed!\n", __FUNCTION__, __LINE__);
750 const CHAR *cmdKey = NULL;
752 if ((cmdParsed == NULL) || (cmdStr == NULL) || (strlen(cmdStr) == 0)) {
764 (strlen(cmdKey) == strlen(cmdParsed->
cmdKeyword)) &&
765 (strncmp(cmdKey, (
CHAR *)(cmdParsed->
cmdKeyword), strlen(cmdKey)) == 0)) {
772 if (cmdHook != NULL) {
777 for (i = 0; i < cmdParsed->
paramCnt; i++) {
803 PRINT_ERR(
"Create mutex for shell cmd info failed\n");
815 if (cmdItem == NULL) {
816 return OS_ERRNO_SHELL_CMDREG_MEMALLOC_ERROR;
821 if (cmdItemNode == NULL) {
823 return OS_ERRNO_SHELL_CMDREG_MEMALLOC_ERROR;
826 cmdItemNode->
cmd = cmdItem;
863 PRINT_ERR(
"[%s] shell is not yet initialized!\n", __FUNCTION__);
864 return OS_ERRNO_SHELL_NOT_INIT;
868 if ((cmdProc == NULL) || (cmdKey == NULL) ||
869 (cmdType >=
CMD_TYPE_BUTT) || (strlen(cmdKey) >= CMD_KEY_LEN) || !strlen(cmdKey)) {
870 return OS_ERRNO_SHELL_CMDREG_PARA_ERROR;
873 if (paraNum > CMD_MAX_PARAS) {
874 if (paraNum != XARGS) {
875 return OS_ERRNO_SHELL_CMDREG_PARA_ERROR;
880 return OS_ERRNO_SHELL_CMDREG_CMD_ERROR;
886 ((strlen(cmdKey) == strlen(cmdItemNode->
cmd->
cmdKey)) &&
887 (strncmp((
CHAR *)(cmdItemNode->
cmd->
cmdKey), cmdKey, strlen(cmdKey)) == 0))) {
889 return OS_ERRNO_SHELL_CMDREG_CMD_EXIST;
unsigned int OsCmdParse(char *cmdStr, CmdParsed *cmdParsed)
解析cmd命令,将关键字,参数分离出来
unsigned int OsCmdTokenSplit(char *cmdStr, char split, CmdParsed *cmdParsed)
将shell命令按 ' ' 分开处理
@ CMD_TYPE_STD
支持的标准命令参数输入,所有输入的字符都会通过命令解析后被传入。
unsigned int OsCmdExec(CmdParsed *cmdParsed, char *cmdStr)
unsigned int OsShellKeyInit(ShellCB *shellCB)
void OsShellHistoryShow(unsigned int value, ShellCB *shellCB)
void OsShellCmdPush(const char *string, CmdKeyLink *cmdKeyLink)
char * OsShellGetWorkingDirectory(void)
int OsTabCompletion(char *cmdKey, unsigned int *len)
void OsShellKeyDeInit(CmdKeyLink *cmdKeyLink)
unsigned int OsCmdKeyShift(const char *cmdKey, char *cmdOut, unsigned int size)
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListInit(LOS_DL_LIST *list)
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListTailInsert(LOS_DL_LIST *list, LOS_DL_LIST *node)
Insert a node to the tail of a doubly linked list.
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListDelete(LOS_DL_LIST *node)
LITE_OS_SEC_ALW_INLINE STATIC INLINE BOOL LOS_ListEmpty(LOS_DL_LIST *list)
Identify whether a specified doubly linked list is empty. | 判断链表是否为空
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
从指定内存池中申请size长度的内存,注意这可不是从内核堆空间中申请内存
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
UINT8 * m_aucSysMem0
异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。
LITE_OS_SEC_TEXT UINT32 LOS_MuxInit(LosMux *mutex, const LosMuxAttr *attr)
初始化互斥锁
LITE_OS_SEC_TEXT UINT32 LOS_MuxUnlock(LosMux *mutex)
释放锁
LITE_OS_SEC_TEXT UINT32 LOS_MuxLock(LosMux *mutex, UINT32 timeout)
拿互斥锁,
int pthread_mutex_lock(pthread_mutex_t *mutex)
互斥锁加锁操作
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁互斥锁
LITE_OS_SEC_TEXT_MINOR VOID OsCmdAscendingInsert(CmdItemNode *cmd)
按升序插入到链表中
LOS_HAL_TABLE_BEGIN(g_shellcmd, shellcmd)
LITE_OS_SEC_TEXT_MINOR BOOL OsCmdKeyCheck(const CHAR *cmdKey)
类型变量命名,必须是数字字母下划线,首字母不能是数字
LITE_OS_SEC_TEXT_MINOR UINT32 OsCmdInit(VOID)
STATIC INT32 OsShowPageControl(UINT32 timesPrint, UINT32 lineCap, UINT32 count)
显示页内容控制器
LITE_OS_SEC_TEXT_MINOR UINT32 osCmdReg(CmdType cmdType, const CHAR *cmdKey, UINT32 paraNum, CmdCallBackFunc cmdProc)
osCmdReg 以动态方式注册命令
LOS_HAL_TABLE_END(g_shellcmdEnd, shellcmd)
STATIC INT32 OsShowPageInputControl(VOID)
输出内容
STATIC INT32 OsStrSeparateTabStrGet(CHAR **tabStr, CmdParsed *parsed, UINT32 tabStrLen)
STATIC VOID OsFreeCmdPara(CmdParsed *cmdParsed)
释放命令行参数所占内存
STATIC VOID strncmp_cut(const CHAR *s1, CHAR *s2, size_t n)
STATIC INT32 OsStrSeparate(CHAR *tabStr, CHAR *strPath, CHAR *nameLooking, UINT32 tabStrLen)
STATIC INT32 OsPrintMatchList(UINT32 count, const CHAR *strPath, const CHAR *nameLooking, UINT32 printLen)
打印匹配的列表数据
STATIC UINT32 OsCmdItemCreate(CmdType cmdType, const CHAR *cmdKey, UINT32 paraNum, CmdCallBackFunc cmdProc)
创建一个命令项,例如 chmod
STATIC INT32 OsTabMatchFile(CHAR *cmdKey, UINT32 *len)
使用tab键去匹配关键字文件
STATIC INT32 OsSurePrintAll(UINT32 count)
是否打印所有内容
STATIC VOID OsCompleteStr(const CHAR *result, const CHAR *target, CHAR *cmdKey, UINT32 *len)
STATIC CmdModInfo g_cmdInfo
shell 命令模块信息,上面挂了所有的命令项(ls,cd ,cp ==)
LITE_OS_SEC_TEXT_MINOR UINT32 OsShellSysCmdRegister(VOID)
注册系统自带的shell命令
STATIC INT32 OsExecNameMatch(const CHAR *strPath, const CHAR *nameLooking, CHAR *strObj, UINT32 *maxLen)
CmdModInfo * OsCmdInfoGet(VOID)
获取全局变量
STATIC INT32 OsTabMatchCmd(CHAR *cmdKey, UINT32 *len)
使用tab键去匹配命令
const CHAR * cmdKey
命令关键字,例如:ls 函数在Shell中访问的名称。
CmdCallBackFunc cmdHook
命令执行函数地址,即命令实际执行函数。
UINT32 paraNum
调用的执行函数的入参最大个数,暂不支持。
char cmdString[0]
字符串,可变数组的一种实现方式.
UINT32 initMagicFlag
初始魔法标签 0xABABABAB
char cmdKeyword[CMD_KEY_LEN]
char * paramArray[CMD_MAX_PARAS]
struct LOS_DL_LIST * pstPrev
struct LOS_DL_LIST * pstNext
char shellBuf[SHOW_MAX_LEN]
接受shell命令 buf大小
void * cmdKeyLink
命令链表,所有敲过的命令链表
void * cmdHistoryKeyLink
命令的历史记录链表,去重,10个
unsigned int shellBufOffset
buf偏移量
pthread_mutex_t historyMutex
操作cmdHistoryKeyLink的互斥量
void * cmdMaskKeyLink
主要用于方向键上下遍历历史命令
u32_t(* CmdCallBackFunc)(u32_t argc, const char **argv)