更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
shcmd.h 文件参考

浏览源代码.

结构体

struct  CmdItem
 
struct  CmdItemNode
 
struct  CmdModInfo
 
struct  CmdKeyLink
 

类型定义

typedef BOOL(* CmdVerifyTransID) (UINT32 transID)
 

函数

UINT32 OsCmdInit (VOID)
 
CmdModInfoOsCmdInfoGet (VOID)
 获取全局变量 更多...
 
UINT32 OsCmdExec (CmdParsed *cmdParsed, CHAR *cmdStr)
 执行命令,shell是运行程序的程序. 更多...
 
UINT32 OsCmdKeyShift (const CHAR *cmdKey, CHAR *cmdOut, UINT32 size)
 
INT32 OsTabCompletion (CHAR *cmdKey, UINT32 *len)
 tab键 更多...
 
VOID OsShellCmdPush (const CHAR *string, CmdKeyLink *cmdKeyLink)
 将shell命令 string 以 CmdKeyLink 方式加入链表 更多...
 
VOID OsShellHistoryShow (UINT32 value, ShellCB *shellCB)
 显示shell命令历史记录,支持上下键方式 更多...
 
UINT32 OsShellKeyInit (ShellCB *shellCB)
 shell 命令初始化 更多...
 
VOID OsShellKeyDeInit (CmdKeyLink *cmdKeyLink)
 shell的析构函数 更多...
 
UINT32 OsShellSysCmdRegister (VOID)
 注册系统自带的shell命令 更多...
 

类型定义说明

◆ CmdVerifyTransID

typedef BOOL(* CmdVerifyTransID) (UINT32 transID)

在文件 shcmd.h51 行定义.

函数说明

◆ OsCmdExec()

UINT32 OsCmdExec ( CmdParsed cmdParsed,
CHAR cmdStr 
)

执行命令,shell是运行程序的程序.

在文件 shcmd.c586 行定义.

587{
588 /* TODO: complete the usrspace command */
589 unsigned int ret = SH_OK;
590 if (cmdParsed && cmdStr) {
591 ret = SH_NOK;
592 }
593
594 return ret;
595}
函数调用图:

◆ OsCmdInfoGet()

CmdModInfo * OsCmdInfoGet ( VOID  )

获取全局变量

在文件 shcmd.c52 行定义.

53{
54 return &g_cmdInfo;
55}
STATIC CmdModInfo g_cmdInfo
shell 命令模块信息,上面挂了所有的命令项(ls,cd ,cp ==)
Definition: shcmd.c:47
这是这个函数的调用关系图:

◆ OsCmdInit()

UINT32 OsCmdInit ( VOID  )

命令初始化,用于存放支持的命令,目前鸿蒙支持如下命令 arp cat cd chgrp chmod chown cp cpup
date dhclient dmesg dns format free help hwi
ifconfig ipdebug kill log ls lsfd memcheck mkdir
mount netstat oom partinfo partition ping ping6 pwd
reset rm rmdir sem statfs su swtmr sync
systeminfo task telnet test tftp touch umount uname
watch writeproc

在文件 shcmd.c795 行定义.

796{
797 UINT32 ret;
798 LOS_ListInit(&(g_cmdInfo.cmdList.list));//初始化双向链表
799 g_cmdInfo.listNum = 0; //命令数量
800 g_cmdInfo.initMagicFlag = SHELL_INIT_MAGIC_FLAG;//魔法数字
801 ret = LOS_MuxInit(&g_cmdInfo.muxLock, NULL);//初始化互斥量,确保链表安全访问
802 if (ret != LOS_OK) {
803 PRINT_ERR("Create mutex for shell cmd info failed\n");
804 return OS_ERROR;
805 }
806 return LOS_OK;
807}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListInit(LOS_DL_LIST *list)
Definition: los_list.h:104
LITE_OS_SEC_TEXT UINT32 LOS_MuxInit(LosMux *mutex, const LosMuxAttr *attr)
初始化互斥锁
Definition: los_mux.c:262
unsigned int UINT32
Definition: los_typedef.h:57
LOS_DL_LIST list
双向链表
Definition: shcmd.h:63
CmdItemNode cmdList
命令项节点
Definition: shcmd.h:69
UINT32 initMagicFlag
初始魔法标签 0xABABABAB
Definition: shcmd.h:71
LosMux muxLock
操作链表互斥锁
Definition: shcmd.h:72
UINT32 listNum
节点数量
Definition: shcmd.h:70
函数调用图:
这是这个函数的调用关系图:

◆ OsCmdKeyShift()

UINT32 OsCmdKeyShift ( const CHAR cmdKey,
CHAR cmdOut,
UINT32  size 
)

在文件 shcmd.c386 行定义.

387{
388 char *output = NULL;
389 char *outputBak = NULL;
390 unsigned int len;
391 int ret;
392 bool quotes = FALSE;
393
394 if ((cmdKey == NULL) || (cmdOut == NULL)) {
395 return (unsigned int)SH_ERROR;
396 }
397
398 len = strlen(cmdKey);
399 if ((*cmdKey == '\n') || (len >= size)) {
400 return (unsigned int)SH_ERROR;
401 }
402 output = (char *)malloc(len + 1);
403 if (output == NULL) {
404 printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
405 return (unsigned int)SH_ERROR;
406 }
407
408 /* Backup the 'output' start address */
409 outputBak = output;
410 /* Scan each charactor in 'cmdKey',and squeeze the overmuch space and ignore invalid charactor */
411 for (; *cmdKey != '\0'; cmdKey++) {
412 /* Detected a Double Quotes, switch the matching status */
413 if (*(cmdKey) == '\"') {
414 SWITCH_QUOTES_STATUS(quotes);
415 }
416 /* Ignore the current charactor in following situation */
417 /* 1) Quotes matching status is FALSE (which said that the space is not been marked by double quotes) */
418 /* 2) Current charactor is a space */
419 /* 3) Next charactor is a space too, or the string is been seeked to the end already(\0) */
420 /* 4) Invalid charactor, such as single quotes */
421 if ((*cmdKey == ' ') && ((*(cmdKey + 1) == ' ') || (*(cmdKey + 1) == '\0')) && QUOTES_STATUS_CLOSE(quotes)) {
422 continue;
423 }
424 if (*cmdKey == '\'') {
425 continue;
426 }
427 *output = *cmdKey;
428 output++;
429 }
430 *output = '\0';
431 /* Restore the 'output' start address */
432 output = outputBak;
433 len = strlen(output);
434 /* Clear the space which is located at the first charactor in buffer */
435 if (*output == ' ') {
436 output++;
437 len--;
438 }
439 /* Copy out the buffer which is been operated already */
440 ret = strncpy_s(cmdOut, size, output, len);
441 if (ret != SH_OK) {
442 printf("%s,%d strncpy_s failed, err:%d!\n", __FUNCTION__, __LINE__, ret);
443 free(outputBak);
444 return SH_ERROR;
445 }
446 cmdOut[len] = '\0';
447
448 free(outputBak);
449 return SH_OK;
450}
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
函数调用图:
这是这个函数的调用关系图:

◆ OsShellCmdPush()

VOID OsShellCmdPush ( const CHAR string,
CmdKeyLink cmdKeyLink 
)

将shell命令 string 以 CmdKeyLink 方式加入链表

在文件 shcmd.c514 行定义.

515{
516 CmdKeyLink *cmdNewNode = NULL;
517 unsigned int len;
518
519 if ((string == NULL) || (strlen(string) == 0)) {
520 return;
521 }
522
523 len = strlen(string);
524 cmdNewNode = (CmdKeyLink *)malloc(sizeof(CmdKeyLink) + len + 1);
525 if (cmdNewNode == NULL) {
526 return;
527 }
528
529 (void)memset_s(cmdNewNode, sizeof(CmdKeyLink) + len + 1, 0, sizeof(CmdKeyLink) + len + 1);
530 if (strncpy_s(cmdNewNode->cmdString, len + 1, string, len)) {
531 free(cmdNewNode);
532 return;
533 }
534
535 SH_ListTailInsert(&(cmdKeyLink->list), &(cmdNewNode->list));
536
537 return;
538}
static void SH_ListTailInsert(SH_List *list, SH_List *node)
Insert a node to the tail of a doubly linked list.
Definition: shell_list.h:168
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
函数调用图:

◆ OsShellHistoryShow()

VOID OsShellHistoryShow ( UINT32  value,
ShellCB shellCB 
)

显示shell命令历史记录,支持上下键方式

在文件 shcmd.c540 行定义.

541{
542 CmdKeyLink *cmdtmp = NULL;
543 CmdKeyLink *cmdNode = shellCB->cmdHistoryKeyLink;
544 CmdKeyLink *cmdMask = shellCB->cmdMaskKeyLink;
545 int ret;
546
548 if (value == CMD_KEY_DOWN) {
549 if (cmdMask == cmdNode) {
550 goto END;
551 }
552
553 cmdtmp = SH_LIST_ENTRY(cmdMask->list.pstNext, CmdKeyLink, list);
554 if (cmdtmp != cmdNode) {
555 cmdMask = cmdtmp;
556 } else {
557 goto END;
558 }
559 } else if (value == CMD_KEY_UP) {
560 cmdtmp = SH_LIST_ENTRY(cmdMask->list.pstPrev, CmdKeyLink, list);
561 if (cmdtmp != cmdNode) {
562 cmdMask = cmdtmp;
563 } else {
564 goto END;
565 }
566 }
567
568 while (shellCB->shellBufOffset--) {
569 printf("\b \b");
570 }
571 printf("%s", cmdMask->cmdString);
572 shellCB->shellBufOffset = strlen(cmdMask->cmdString);
573 (void)memset_s(shellCB->shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN);
574 ret = memcpy_s(shellCB->shellBuf, SHOW_MAX_LEN, cmdMask->cmdString, shellCB->shellBufOffset);
575 if (ret != SH_OK) {
576 printf("%s, %d memcpy failed!\n", __FUNCTION__, __LINE__);
577 goto END;
578 }
579 shellCB->cmdMaskKeyLink = (void *)cmdMask;
580
581END:
583 return;
584}
@ CMD_KEY_DOWN
方向下键
Definition: shell.h:97
@ CMD_KEY_UP
方向上键
Definition: shell.h:96
int pthread_mutex_lock(pthread_mutex_t *mutex)
互斥锁加锁操作
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁互斥锁
struct SH_List * pstPrev
Definition: shell_list.h:51
struct SH_List * pstNext
Definition: shell_list.h:52
char shellBuf[SHOW_MAX_LEN]
接受shell命令 buf大小
Definition: shell.h:83
void * cmdHistoryKeyLink
命令的历史记录链表,去重,10个
Definition: shell.h:76
unsigned int shellBufOffset
buf偏移量
Definition: shell.h:78
pthread_mutex_t historyMutex
操作cmdHistoryKeyLink的互斥量
Definition: shell.h:82
void * cmdMaskKeyLink
主要用于方向键上下遍历历史命令
Definition: shell.h:77
函数调用图:

◆ OsShellKeyDeInit()

VOID OsShellKeyDeInit ( CmdKeyLink cmdKeyLink)

shell的析构函数

在文件 shcmd.c497 行定义.

498{
499 CmdKeyLink *cmdtmp = NULL;
500 if (cmdKeyLink == NULL) {
501 return;
502 }
503
504 while (!SH_ListEmpty(&(cmdKeyLink->list))) {
505 cmdtmp = SH_LIST_ENTRY(cmdKeyLink->list.pstNext, CmdKeyLink, list);
506 SH_ListDelete(&cmdtmp->list);
507 free(cmdtmp);
508 }
509
510 cmdKeyLink->count = 0;
511 free(cmdKeyLink);
512}
static void SH_ListDelete(SH_List *node)
Definition: shell_list.h:224
static bool SH_ListEmpty(SH_List *list)
Identify whether a specified doubly linked list is empty.
Definition: shell_list.h:253
函数调用图:

◆ OsShellKeyInit()

UINT32 OsShellKeyInit ( ShellCB shellCB)

shell 命令初始化

在文件 shcmd.c465 行定义.

466{
467 CmdKeyLink *cmdKeyLink = NULL;
468 CmdKeyLink *cmdHistoryLink = NULL;
469
470 if (shellCB == NULL) {
471 return SH_ERROR;
472 }
473
474 cmdKeyLink = (CmdKeyLink *)malloc(sizeof(CmdKeyLink));
475 if (cmdKeyLink == NULL) {
476 printf("Shell CmdKeyLink memory alloc error!\n");
477 return SH_ERROR;
478 }
479 cmdHistoryLink = (CmdKeyLink *)malloc(sizeof(CmdKeyLink));
480 if (cmdHistoryLink == NULL) {
481 free(cmdKeyLink);
482 printf("Shell CmdHistoryLink memory alloc error!\n");
483 return SH_ERROR;
484 }
485
486 cmdKeyLink->count = 0;
487 SH_ListInit(&(cmdKeyLink->list));
488 shellCB->cmdKeyLink = (void *)cmdKeyLink;
489
490 cmdHistoryLink->count = 0;
491 SH_ListInit(&(cmdHistoryLink->list));
492 shellCB->cmdHistoryKeyLink = (void *)cmdHistoryLink;
493 shellCB->cmdMaskKeyLink = (void *)cmdHistoryLink;
494 return SH_OK;
495}
static void SH_ListInit(SH_List *list)
Definition: shell_list.h:72
void * cmdKeyLink
命令链表,所有敲过的命令链表
Definition: shell.h:75
函数调用图:

◆ OsShellSysCmdRegister()

UINT32 OsShellSysCmdRegister ( VOID  )

注册系统自带的shell命令

在文件 shcmd.c650 行定义.

651{
652 UINT32 i;
653 UINT8 *cmdItemGroup = NULL;
654 UINT32 index = ((UINTPTR)(&g_shellcmdEnd) - (UINTPTR)(&g_shellcmd[0])) / sizeof(CmdItem);//获取个数
655 CmdItemNode *cmdItem = NULL;
656
657 cmdItemGroup = (UINT8 *)LOS_MemAlloc(m_aucSysMem0, index * sizeof(CmdItemNode));//分配命令项
658 if (cmdItemGroup == NULL) {
659 PRINT_ERR("[%s]System memory allocation failure!\n", __FUNCTION__);
660 return (UINT32)OS_ERROR;
661 }
662
663 for (i = 0; i < index; ++i) {//循环插入
664 cmdItem = (CmdItemNode *)(cmdItemGroup + i * sizeof(CmdItemNode));
665 cmdItem->cmd = &g_shellcmd[i];//一个个取
666 OsCmdAscendingInsert(cmdItem);//按升序插入到链表中
667 }
668 g_cmdInfo.listNum += index;//命令数量叠加
669 return LOS_OK;
670}
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
从指定内存池中申请size长度的内存,注意这可不是从内核堆空间中申请内存
Definition: los_memory.c:1123
UINT8 * m_aucSysMem0
异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。
Definition: los_memory.c:107
unsigned char UINT8
Definition: los_typedef.h:55
unsigned long UINTPTR
Definition: los_typedef.h:68
CmdItem g_shellcmdEnd
CmdItem g_shellcmd[]
LITE_OS_SEC_TEXT_MINOR VOID OsCmdAscendingInsert(CmdItemNode *cmd)
按升序插入到链表中
Definition: shcmd.c:572
Definition: shcmd.h:53
CmdItem * cmd
命令项
Definition: shcmd.h:64
函数调用图:
这是这个函数的调用关系图:

◆ OsTabCompletion()

INT32 OsTabCompletion ( CHAR cmdKey,
UINT32 len 
)

tab键

在文件 shcmd.c452 行定义.

453{
454 int count;
455
456 if ((cmdKey == NULL) || (len == NULL)) {
457 return (int)SH_ERROR;
458 }
459
460 count = OsTabMatchFile(cmdKey, len);
461
462 return count;
463}
static int OsTabMatchFile(char *cmdKey, unsigned int *len)
Definition: shcmd.c:338
函数调用图: