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

浏览源代码.

结构体

struct  CmdKeyLink
 

函数

unsigned int OsCmdExec (CmdParsed *cmdParsed, char *cmdStr)
 执行命令,shell是运行程序的程序. 更多...
 
unsigned int OsCmdKeyShift (const char *cmdKey, char *cmdOut, unsigned int size)
 
int OsTabCompletion (char *cmdKey, unsigned int *len)
 tab键 更多...
 
void OsShellCmdPush (const char *string, CmdKeyLink *cmdKeyLink)
 将shell命令 string 以 CmdKeyLink 方式加入链表 更多...
 
void OsShellHistoryShow (unsigned int value, ShellCB *shellCB)
 显示shell命令历史记录,支持上下键方式 更多...
 
unsigned int OsShellKeyInit (ShellCB *shellCB)
 shell 命令初始化 更多...
 
void OsShellKeyDeInit (CmdKeyLink *cmdKeyLink)
 shell的析构函数 更多...
 
int OsShellSetWorkingDirectory (const char *dir, size_t len)
 

函数说明

◆ OsCmdExec()

unsigned int 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}
这是这个函数的调用关系图:

◆ OsCmdKeyShift()

unsigned int OsCmdKeyShift ( const char *  cmdKey,
char *  cmdOut,
unsigned int  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 ( unsigned int  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()

unsigned int 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
函数调用图:
这是这个函数的调用关系图:

◆ OsShellSetWorkingDirectory()

int OsShellSetWorkingDirectory ( const char *  dir,
size_t  len 
)

在文件 shcmd.c99 行定义.

100{
101 if (dir == NULL) {
102 return SH_NOK;
103 }
104
105 int ret = strncpy_s(OsGetShellCb()->shellWorkingDirectory, sizeof(OsGetShellCb()->shellWorkingDirectory),
106 dir, len);
107 if (ret != SH_OK) {
108 return SH_NOK;
109 }
110 return SH_OK;
111}
ShellCB * OsGetShellCb(void)
获取shell控制块
Definition: main.c:47
函数调用图:
这是这个函数的调用关系图:

◆ OsTabCompletion()

int OsTabCompletion ( char *  cmdKey,
unsigned int 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
函数调用图:
这是这个函数的调用关系图: