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

浏览源代码.

枚举

enum  { STAT_NORMAL_KEY , STAT_ESC_KEY , STAT_MULTI_KEY }
 

函数

char * GetCmdline (ShellCB *shellCB)
 
static void ShellSaveHistoryCmd (char *string, ShellCB *shellCB)
 保存shell命令历史 更多...
 
int ShellPend (ShellCB *shellCB)
 
int ShellNotify (ShellCB *shellCB)
 
static int ShellCmdLineCheckUDRL (const char ch, ShellCB *shellCB)
 检查上下左右键 更多...
 
void ShellTaskNotify (ShellCB *shellCB)
 
void ParseEnterKey (OutputFunc outputFunc, ShellCB *shellCB)
 
void ParseCancelKey (OutputFunc outputFunc, ShellCB *shellCB)
 
void ParseDeleteKey (OutputFunc outputFunc, ShellCB *shellCB)
 解析删除键 更多...
 
void ParseTabKey (OutputFunc outputFunc, ShellCB *shellCB)
 
void ParseNormalChar (char ch, OutputFunc outputFunc, ShellCB *shellCB)
 
void ShellCmdLineParse (char c, OutputFunc outputFunc, ShellCB *shellCB)
 
unsigned int ShellMsgNameGet (CmdParsed *cmdParsed, const char *cmdType)
 
char * GetCmdName (const char *cmdline, unsigned int len)
 
void ChildExec (const char *cmdName, char *const paramArray[], bool foreground)
 
int CheckExit (const char *cmdName, const CmdParsed *cmdParsed)
 
static void DoCmdExec (const char *cmdName, const char *cmdline, unsigned int len, CmdParsed *cmdParsed)
 
static void ParseAndExecCmdline (CmdParsed *cmdParsed, const char *cmdline, unsigned int len)
 
unsigned int PreHandleCmdline (const char *input, char **output, unsigned int *outputlen)
 
static void ExecCmdline (const char *cmdline)
 
static void ShellCmdProcess (ShellCB *shellCB)
 
voidShellTask (void *argv)
 
int ShellTaskInit (ShellCB *shellCB)
 给控制台注册一个shell客户端任务 更多...
 
static int ShellKernelReg (unsigned int shellHandle)
 
void ShellEntry (ShellCB *shellCB)
 

枚举类型说明

◆ anonymous enum

anonymous enum
枚举值
STAT_NORMAL_KEY 

普通按键

STAT_ESC_KEY 

<ESC>键,支持VT规范

STAT_MULTI_KEY 

组合键 例如: <ESC>[30m

在文件 shmsg.c135 行定义.

135 {
136 STAT_NORMAL_KEY, ///< 普通按键
137 STAT_ESC_KEY, ///< <ESC>键,支持VT规范
138 STAT_MULTI_KEY ///< 组合键 例如: <ESC>[30m
139};
@ STAT_ESC_KEY
<ESC>键,支持VT规范
Definition: shmsg.c:137
@ STAT_NORMAL_KEY
普通按键
Definition: shmsg.c:136
@ STAT_MULTI_KEY
组合键 例如: <ESC>[30m
Definition: shmsg.c:138

函数说明

◆ CheckExit()

int CheckExit ( const char *  cmdName,
const CmdParsed cmdParsed 
)

在文件 shmsg.c385 行定义.

386{
387 int ret = 0;
388
389 if (strlen(cmdName) != CMD_EXIT_COMMAND_BYTES || strncmp(cmdName, CMD_EXIT_COMMAND, CMD_EXIT_COMMAND_BYTES) != 0) {
390 return 0;
391 }
392
393 if (cmdParsed->paramCnt > 1) {
394 printf("exit: too many arguments\n");
395 return -1;
396 }
397 if (cmdParsed->paramCnt == 1) {
398 char *p = NULL;
399 ret = strtol(cmdParsed->paramArray[0], &p, CMD_EXIT_CODE_BASE_DEC);
400 if (*p != '\0') {
401 printf("exit: bad number: %s\n", cmdParsed->paramArray[0]);
402 return -1;
403 }
404 }
405
406 exit(ret);
407}
void exit(int status)
Definition: stdlib.c:60
char * paramArray[CMD_MAX_PARAS]
Definition: shcmdparse.h:51
unsigned int paramCnt
Definition: shcmdparse.h:48
函数调用图:
这是这个函数的调用关系图:

◆ ChildExec()

void ChildExec ( const char *  cmdName,
char *const  paramArray[],
bool  foreground 
)

在文件 shmsg.c354 行定义.

355{
356 int ret;
357 pid_t gid;
358
359 ret = setpgrp();
360 if (ret == -1) {
361 exit(1);
362 }
363
364 gid = getpgrp();
365 if (gid < 0) {
366 printf("get group id failed, pgrpid %d, errno %d\n", gid, errno);
367 exit(1);
368 }
369
370 if (!foreground) {
371 ret = tcsetpgrp(STDIN_FILENO, gid);
372 if (ret != 0) {
373 printf("tcsetpgrp failed, errno %d\n", errno);
374 exit(1);
375 }
376 }
377
378 ret = execve(cmdName, paramArray, NULL);
379 if (ret == -1) {
380 perror("execve");
381 exit(-1);
382 }
383}
函数调用图:
这是这个函数的调用关系图:

◆ DoCmdExec()

static void DoCmdExec ( const char *  cmdName,
const char *  cmdline,
unsigned int  len,
CmdParsed cmdParsed 
)
static

在文件 shmsg.c409 行定义.

410{
411 bool foreground = FALSE;
412 int ret;
413 pid_t forkPid;
414
415 if (strncmp(cmdline, CMD_EXEC_COMMAND, CMD_EXEC_COMMAND_BYTES) == 0) {
416 if ((cmdParsed->paramCnt > 1) && (strcmp(cmdParsed->paramArray[cmdParsed->paramCnt - 1], "&") == 0)) {
417 free(cmdParsed->paramArray[cmdParsed->paramCnt - 1]);
418 cmdParsed->paramArray[cmdParsed->paramCnt - 1] = NULL;
419 cmdParsed->paramCnt--;
420 foreground = TRUE;
421 }
422
423 forkPid = fork();
424 if (forkPid < 0) {
425 printf("Faild to fork from shell\n");
426 return;
427 } else if (forkPid == 0) {
428 ChildExec(cmdParsed->paramArray[0], cmdParsed->paramArray, foreground);
429 } else {
430 if (!foreground) {
431 (void)waitpid(forkPid, 0, 0);
432 }
433 ret = tcsetpgrp(STDIN_FILENO, getpid());
434 if (ret != 0) {
435 printf("tcsetpgrp failed, errno %d\n", errno);
436 }
437 }
438 } else {
439 if (CheckExit(cmdName, cmdParsed) < 0) {
440 return;
441 }
442 (void)syscall(__NR_shellexec, cmdName, cmdline);
443 }
444}
void ChildExec(const char *cmdName, char *const paramArray[], bool foreground)
Definition: shmsg.c:354
int CheckExit(const char *cmdName, const CmdParsed *cmdParsed)
Definition: shmsg.c:385
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
pid_t getpid(void)
获取当前任务ID
Definition: misc.c:150
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
函数调用图:
这是这个函数的调用关系图:

◆ ExecCmdline()

static void ExecCmdline ( const char *  cmdline)
static

在文件 shmsg.c560 行定义.

561{
562 unsigned int ret;
563 char *output = NULL;
564 unsigned int outputlen;
565 CmdParsed cmdParsed;
566
567 if (cmdline == NULL) {
568 return;
569 }
570
571 /* strip out unnecessary characters */
572 ret = PreHandleCmdline(cmdline, &output, &outputlen);
573 if (ret == SH_NOK) {
574 return;
575 }
576
577 (void)memset_s(&cmdParsed, sizeof(CmdParsed), 0, sizeof(CmdParsed));
578 ParseAndExecCmdline(&cmdParsed, output, outputlen);
579 free(output);
580}
unsigned int PreHandleCmdline(const char *input, char **output, unsigned int *outputlen)
Definition: shmsg.c:490
static void ParseAndExecCmdline(CmdParsed *cmdParsed, const char *cmdline, unsigned int len)
Definition: shmsg.c:446
函数调用图:
这是这个函数的调用关系图:

◆ GetCmdline()

char * GetCmdline ( ShellCB shellCB)

在文件 shmsg.c52 行定义.

53{
54 CmdKeyLink *cmdkey = shellCB->cmdKeyLink;
55 CmdKeyLink *cmdNode = NULL;
56
58 if ((cmdkey == NULL) || SH_ListEmpty(&cmdkey->list)) {
60 return NULL;
61 }
62
63 cmdNode = SH_LIST_ENTRY(cmdkey->list.pstNext, CmdKeyLink, list);
64 if (cmdNode == NULL) {
66 return NULL;
67 }
68
69 SH_ListDelete(&(cmdNode->list));
71
72 if (strlen(cmdNode->cmdString) == 0) {
73 free(cmdNode);
74 return NULL;
75 }
76
77 return cmdNode->cmdString;
78}
int pthread_mutex_lock(pthread_mutex_t *mutex)
互斥锁加锁操作
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁互斥锁
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
struct SH_List * pstNext
Definition: shell_list.h:52
void * cmdKeyLink
命令链表,所有敲过的命令链表
Definition: shell.h:75
pthread_mutex_t keyMutex
操作cmdKeyLink的互斥量
Definition: shell.h:81
函数调用图:
这是这个函数的调用关系图:

◆ GetCmdName()

char * GetCmdName ( const char *  cmdline,
unsigned int  len 
)

在文件 shmsg.c315 行定义.

316{
317 unsigned int loop;
318 const char *tmpStr = NULL;
319 bool quotes = FALSE;
320 char *cmdName = NULL;
321 if (cmdline == NULL) {
322 return NULL;
323 }
324
325 cmdName = (char *)malloc(len + 1);
326 if (cmdName == NULL) {
327 printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
328 return NULL;
329 }
330
331 /* Scan the 'cmdline' string for command */
332 /* Notice: Command string must not have any special name */
333 for (tmpStr = cmdline, loop = 0; (*tmpStr != '\0') && (loop < len); ) {
334 /* If reach a double quotes, switch the quotes matching status */
335 if (*tmpStr == '\"') {
336 SWITCH_QUOTES_STATUS(quotes);
337 /* Ignore the double quote charactor itself */
338 tmpStr++;
339 continue;
340 }
341 /* If detected a space which the quotes matching status is false */
342 /* which said has detected the first space for seperator, finish this scan operation */
343 if ((*tmpStr == ' ') && (QUOTES_STATUS_CLOSE(quotes))) {
344 break;
345 }
346 cmdName[loop] = *tmpStr++;
347 loop++;
348 }
349 cmdName[loop] = '\0';
350
351 return cmdName;
352}
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
函数调用图:
这是这个函数的调用关系图:

◆ ParseAndExecCmdline()

static void ParseAndExecCmdline ( CmdParsed cmdParsed,
const char *  cmdline,
unsigned int  len 
)
static

在文件 shmsg.c446 行定义.

447{
448 int i;
449 unsigned int ret;
450 char shellWorkingDirectory[PATH_MAX + 1] = { 0 };
451 char *cmdlineOrigin = NULL;
452 char *cmdName = NULL;
453
454 cmdlineOrigin = strdup(cmdline);
455 if (cmdlineOrigin == NULL) {
456 printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
457 return;
458 }
459
460 cmdName = GetCmdName(cmdline, len);
461 if (cmdName == NULL) {
462 free(cmdlineOrigin);
463 printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
464 return;
465 }
466
467 ret = OsCmdParse((char *)cmdline, cmdParsed);
468 if (ret != SH_OK) {
469 printf("cmd parse failure in %s[%d]\n", __FUNCTION__, __LINE__);
470 goto OUT;
471 }
472
473 DoCmdExec(cmdName, cmdlineOrigin, len, cmdParsed);
474
475 if (getcwd(shellWorkingDirectory, PATH_MAX) != NULL) {
476 (void)OsShellSetWorkingDirectory(shellWorkingDirectory, (PATH_MAX + 1));
477 }
478
479OUT:
480 for (i = 0; i < cmdParsed->paramCnt; i++) {
481 if (cmdParsed->paramArray[i] != NULL) {
482 free(cmdParsed->paramArray[i]);
483 cmdParsed->paramArray[i] = NULL;
484 }
485 }
486 free(cmdName);
487 free(cmdlineOrigin);
488}
int OsShellSetWorkingDirectory(const char *dir, size_t len)
Definition: shcmd.c:99
unsigned int OsCmdParse(char *cmdStr, CmdParsed *cmdParsed)
解析cmd命令,将关键字,参数分离出来
Definition: shcmdparse.c:147
static void DoCmdExec(const char *cmdName, const char *cmdline, unsigned int len, CmdParsed *cmdParsed)
Definition: shmsg.c:409
char * GetCmdName(const char *cmdline, unsigned int len)
Definition: shmsg.c:315
char * getcwd(char *buf, size_t n)
Definition: vfs_other.c:222
函数调用图:
这是这个函数的调用关系图:

◆ ParseCancelKey()

void ParseCancelKey ( OutputFunc  outputFunc,
ShellCB shellCB 
)

在文件 shmsg.c212 行定义.

213{
214 if ((shellCB == NULL) || (outputFunc == NULL)) {
215 return;
216 }
217
218 if (shellCB->shellBufOffset <= (SHOW_MAX_LEN - 1)) {
219 shellCB->shellBuf[0] = CHAR_CTRL_C;
220 shellCB->shellBuf[1] = '\0';
221 }
222
223 shellCB->shellBufOffset = 0;
224 ShellTaskNotify(shellCB);
225}
void ShellTaskNotify(ShellCB *shellCB)
Definition: shmsg.c:178
char shellBuf[SHOW_MAX_LEN]
接受shell命令 buf大小
Definition: shell.h:83
unsigned int shellBufOffset
buf偏移量
Definition: shell.h:78
函数调用图:
这是这个函数的调用关系图:

◆ ParseDeleteKey()

void ParseDeleteKey ( OutputFunc  outputFunc,
ShellCB shellCB 
)

解析删除键

在文件 shmsg.c227 行定义.

228{
229 if ((shellCB == NULL) || (outputFunc == NULL)) {
230 return;
231 }
232
233 if ((shellCB->shellBufOffset > 0) && (shellCB->shellBufOffset <= (SHOW_MAX_LEN - 1))) {
234 shellCB->shellBuf[shellCB->shellBufOffset - 1] = '\0';
235 shellCB->shellBufOffset--;
236 outputFunc("\b \b");
237 }
238}
这是这个函数的调用关系图:

◆ ParseEnterKey()

void ParseEnterKey ( OutputFunc  outputFunc,
ShellCB shellCB 
)

在文件 shmsg.c192 行定义.

193{
194 if ((shellCB == NULL) || (outputFunc == NULL)) {
195 return;
196 }
197
198 if (shellCB->shellBufOffset == 0) {
199 shellCB->shellBuf[shellCB->shellBufOffset] = '\n';
200 shellCB->shellBuf[shellCB->shellBufOffset + 1] = '\0';
201 goto NOTIFY;
202 }
203
204 if (shellCB->shellBufOffset <= (SHOW_MAX_LEN - 1)) {
205 shellCB->shellBuf[shellCB->shellBufOffset] = '\0';
206 }
207NOTIFY:
208 shellCB->shellBufOffset = 0;
209 ShellTaskNotify(shellCB);
210}
@ NOTIFY
通知信息 即发送 TraceNotifyFrame
函数调用图:
这是这个函数的调用关系图:

◆ ParseNormalChar()

void ParseNormalChar ( char  ch,
OutputFunc  outputFunc,
ShellCB shellCB 
)

在文件 shmsg.c256 行定义.

257{
258 if ((shellCB == NULL) || (outputFunc == NULL) || !VISIABLE_CHAR(ch)) {
259 return;
260 }
261
262 if ((ch != '\0') && (shellCB->shellBufOffset < (SHOW_MAX_LEN - 1))) {
263 shellCB->shellBuf[shellCB->shellBufOffset] = ch;
264 shellCB->shellBufOffset++;
265 outputFunc("%c", ch);
266 }
267
268 shellCB->shellKeyType = STAT_NORMAL_KEY;
269}
unsigned int shellKeyType
按键类型
Definition: shell.h:79
这是这个函数的调用关系图:

◆ ParseTabKey()

void ParseTabKey ( OutputFunc  outputFunc,
ShellCB shellCB 
)

在文件 shmsg.c240 行定义.

241{
242 int ret;
243
244 if ((shellCB == NULL) || (outputFunc == NULL)) {
245 return;
246 }
247
248 if ((shellCB->shellBufOffset > 0) && (shellCB->shellBufOffset < (SHOW_MAX_LEN - 1))) {
249 ret = OsTabCompletion(shellCB->shellBuf, &shellCB->shellBufOffset);
250 if (ret > 1) {
251 outputFunc(SHELL_PROMPT"%s", shellCB->shellBuf);
252 }
253 }
254}
int OsTabCompletion(char *cmdKey, unsigned int *len)
tab键
Definition: shcmd.c:452
函数调用图:
这是这个函数的调用关系图:

◆ PreHandleCmdline()

unsigned int PreHandleCmdline ( const char *  input,
char **  output,
unsigned int outputlen 
)

在文件 shmsg.c490 行定义.

491{
492 unsigned int shiftLen, execLen, newLen;
493 unsigned int removeLen = strlen("./"); /* "./" needs to be removed if it exists */
494 unsigned int ret;
495 char *newCmd = NULL;
496 char *execCmd = CMD_EXEC_COMMAND;
497 const char *cmdBuf = input;
498 unsigned int cmdBufLen = strlen(cmdBuf);
499 char *shiftStr = (char *)malloc(cmdBufLen + 1);
500 errno_t err;
501
502 if (shiftStr == NULL) {
503 printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
504 return SH_NOK;
505 }
506 (void)memset_s(shiftStr, cmdBufLen + 1, 0, cmdBufLen + 1);
507
508 /* Call function 'OsCmdKeyShift' to squeeze and clear useless or overmuch space if string buffer */
509 ret = OsCmdKeyShift(cmdBuf, shiftStr, cmdBufLen + 1);
510 shiftLen = strlen(shiftStr);
511 if ((ret != SH_OK) || (shiftLen == 0)) {
512 ret = SH_NOK;
513 goto END_FREE_SHIFTSTR;
514 }
515 *output = shiftStr;
516 *outputlen = shiftLen;
517
518 /* Check and parse "./", located at the first two charaters of the cmd */
519 if ((shiftLen > removeLen) && (shiftStr[0] == '.') && (shiftStr[1] == '/')) {
520 execLen = strlen(execCmd);
521 newLen = execLen + shiftLen - removeLen; /* i.e., newLen - execLen == shiftLen - removeLen */
522 newCmd = (char *)malloc(newLen + 1);
523 if (newCmd == NULL) {
524 ret = SH_NOK;
525 printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
526 goto END_FREE_SHIFTSTR;
527 }
528
529 err = memcpy_s(newCmd, newLen, execCmd, execLen);
530 if (err != EOK) {
531 printf("memcpy_s failure in %s[%d]\n", __FUNCTION__, __LINE__);
532 ret = SH_NOK;
533 goto END_FREE_NEWCMD;
534 }
535
536 err = memcpy_s(newCmd + execLen, newLen - execLen, shiftStr + removeLen, shiftLen - removeLen);
537 if (err != EOK) {
538 printf("memcpy_s failure in %s[%d]\n", __FUNCTION__, __LINE__);
539 ret = SH_NOK;
540 goto END_FREE_NEWCMD;
541 }
542 newCmd[newLen] = '\0';
543
544 *output = newCmd;
545 *outputlen = newLen;
546 ret = SH_OK;
547 goto END_FREE_SHIFTSTR;
548 } else {
549 ret = SH_OK;
550 goto END;
551 }
552END_FREE_NEWCMD:
553 free(newCmd);
554END_FREE_SHIFTSTR:
555 free(shiftStr);
556END:
557 return ret;
558}
unsigned int OsCmdKeyShift(const char *cmdKey, char *cmdOut, unsigned int size)
Definition: shcmd.c:386
函数调用图:
这是这个函数的调用关系图:

◆ ShellCmdLineCheckUDRL()

static int ShellCmdLineCheckUDRL ( const char  ch,
ShellCB shellCB 
)
static

检查上下左右键

在文件 shmsg.c141 行定义.

142{
143 int ret = SH_OK;
144 if (ch == 0x1b) { /* 0x1b: ESC */
145 shellCB->shellKeyType = STAT_ESC_KEY;
146 return ret;
147 } else if (ch == 0x5b) { /* 0x5b: first Key combination */
148 if (shellCB->shellKeyType == STAT_ESC_KEY) {
149 shellCB->shellKeyType = STAT_MULTI_KEY;
150 return ret;
151 }
152 } else if (ch == 0x41) { /* up */
153 if (shellCB->shellKeyType == STAT_MULTI_KEY) {
155 shellCB->shellKeyType = STAT_NORMAL_KEY;
156 return ret;
157 }
158 } else if (ch == 0x42) { /* down */
159 if (shellCB->shellKeyType == STAT_MULTI_KEY) {
160 shellCB->shellKeyType = STAT_NORMAL_KEY;
162 return ret;
163 }
164 } else if (ch == 0x43) { /* right */
165 if (shellCB->shellKeyType == STAT_MULTI_KEY) {
166 shellCB->shellKeyType = STAT_NORMAL_KEY;
167 return ret;
168 }
169 } else if (ch == 0x44) { /* left */
170 if (shellCB->shellKeyType == STAT_MULTI_KEY) {
171 shellCB->shellKeyType = STAT_NORMAL_KEY;
172 return ret;
173 }
174 }
175 return SH_NOK;
176}
void OsShellHistoryShow(unsigned int value, ShellCB *shellCB)
显示shell命令历史记录,支持上下键方式
Definition: shcmd.c:540
@ CMD_KEY_DOWN
方向下键
Definition: shell.h:97
@ CMD_KEY_UP
方向上键
Definition: shell.h:96
函数调用图:
这是这个函数的调用关系图:

◆ ShellCmdLineParse()

void ShellCmdLineParse ( char  c,
OutputFunc  outputFunc,
ShellCB shellCB 
)

在文件 shmsg.c271 行定义.

272{
273 const char ch = c;
274 int ret;
275
276 if ((shellCB->shellBufOffset == 0) && (ch != '\n') && (ch != CHAR_CTRL_C) && (ch != '\0')) {
277 (void)memset_s(shellCB->shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN);
278 }
279
280 switch (ch) {
281 case '\r':
282 case '\n': /* enter */
283 ParseEnterKey(outputFunc, shellCB);
284 break;
285 case CHAR_CTRL_C: /* ctrl + c */
286 ParseCancelKey(outputFunc, shellCB);
287 break;
288 case '\b': /* backspace */
289 case CHAR_CTRL_DEL: /* delete(0x7F) */
290 ParseDeleteKey(outputFunc, shellCB);
291 break;
292 case '\t': /* tab */
293 ParseTabKey(outputFunc, shellCB);
294 break;
295 default:
296 /* parse the up/down/right/left key */
297 ret = ShellCmdLineCheckUDRL(ch, shellCB);
298 if (ret == SH_OK) {
299 return;
300 }
301 ParseNormalChar(ch, outputFunc, shellCB);
302 break;
303 }
304
305 return;
306}
void ParseEnterKey(OutputFunc outputFunc, ShellCB *shellCB)
Definition: shmsg.c:192
void ParseCancelKey(OutputFunc outputFunc, ShellCB *shellCB)
Definition: shmsg.c:212
void ParseNormalChar(char ch, OutputFunc outputFunc, ShellCB *shellCB)
Definition: shmsg.c:256
void ParseTabKey(OutputFunc outputFunc, ShellCB *shellCB)
Definition: shmsg.c:240
void ParseDeleteKey(OutputFunc outputFunc, ShellCB *shellCB)
解析删除键
Definition: shmsg.c:227
static int ShellCmdLineCheckUDRL(const char ch, ShellCB *shellCB)
检查上下左右键
Definition: shmsg.c:141
函数调用图:
这是这个函数的调用关系图:

◆ ShellCmdProcess()

static void ShellCmdProcess ( ShellCB shellCB)
static

在文件 shmsg.c582 行定义.

583{
584 while (1) {
585 char *buf = GetCmdline(shellCB);
586 if (buf == NULL) {
587 break;
588 }
589 if (buf[0] == CHAR_CTRL_C) {
590 printf("^C");
591 buf[0] = '\n';
592 }
593 printf("\n");
594 ExecCmdline(buf);
595 ShellSaveHistoryCmd(buf, shellCB);
596 shellCB->cmdMaskKeyLink = shellCB->cmdHistoryKeyLink;
597 printf(SHELL_PROMPT);
598 }
599}
static void ExecCmdline(const char *cmdline)
Definition: shmsg.c:560
static void ShellSaveHistoryCmd(char *string, ShellCB *shellCB)
保存shell命令历史
Definition: shmsg.c:80
char * GetCmdline(ShellCB *shellCB)
Definition: shmsg.c:52
void * cmdHistoryKeyLink
命令的历史记录链表,去重,10个
Definition: shell.h:76
void * cmdMaskKeyLink
主要用于方向键上下遍历历史命令
Definition: shell.h:77
函数调用图:
这是这个函数的调用关系图:

◆ ShellEntry()

void ShellEntry ( ShellCB shellCB)

在文件 shmsg.c659 行定义.

660{
661 char ch;
662 int ret;
663 int n;
664 pid_t tid = syscall(__NR_gettid);//获取当前任务/线程ID, 即 "Shell Entry" 任务的ID
665
666 if (shellCB == NULL) {
667 return;
668 }
669
670 (void)memset_s(shellCB->shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN);
671
672 ret = ShellKernelReg((int)tid);//向内核注册shell,和控制台捆绑在一块
673 if (ret != 0) {
674 printf("another shell is already running!\n");
675 exit(-1);
676 }
677
678 while (1) {
679 n = read(0, &ch, 1);//读取输入字符
680 if (n == 1) {
681 ShellCmdLineParse(ch, (OutputFunc)printf, shellCB);//对命令行内容进行解析
682 }
683 }
684 return;
685}
void(* OutputFunc)(const char *fmt,...)
Definition: shmsg.h:63
static int ShellKernelReg(unsigned int shellHandle)
Definition: shmsg.c:654
void ShellCmdLineParse(char c, OutputFunc outputFunc, ShellCB *shellCB)
Definition: shmsg.c:271
函数调用图:
这是这个函数的调用关系图:

◆ ShellKernelReg()

static int ShellKernelReg ( unsigned int  shellHandle)
static

在文件 shmsg.c654 行定义.

655{
656 return ioctl(STDIN_FILENO, CONSOLE_CONTROL_REG_USERTASK, shellHandle);//Shell Entry 任务将从标准输入中读取数据
657}
这是这个函数的调用关系图:

◆ ShellMsgNameGet()

unsigned int ShellMsgNameGet ( CmdParsed cmdParsed,
const char *  cmdType 
)

在文件 shmsg.c308 行定义.

309{
310 (void)cmdParsed;
311 (void)cmdType;
312 return SH_ERROR;
313}

◆ ShellNotify()

int ShellNotify ( ShellCB shellCB)

在文件 shmsg.c126 行定义.

127{
128 if (shellCB == NULL) {
129 return SH_NOK;
130 }
131
132 return sem_post(&shellCB->shellSem);
133}
int sem_post(sem_t *sem)
增加信号量计数
Definition: semaphore.c:139
sem_t shellSem
shell信号量
Definition: shell.h:80
这是这个函数的调用关系图:

◆ ShellPend()

int ShellPend ( ShellCB shellCB)

在文件 shmsg.c117 行定义.

118{
119 if (shellCB == NULL) {
120 return SH_NOK;
121 }
122
123 return sem_wait(&shellCB->shellSem);
124}
int sem_wait(sem_t *sem)
获取信号量
Definition: semaphore.c:77
函数调用图:
这是这个函数的调用关系图:

◆ ShellSaveHistoryCmd()

static void ShellSaveHistoryCmd ( char *  string,
ShellCB shellCB 
)
static

保存shell命令历史

在文件 shmsg.c80 行定义.

81{
82 CmdKeyLink *cmdHistory = shellCB->cmdHistoryKeyLink;
83 CmdKeyLink *cmdkey = SH_LIST_ENTRY(string, CmdKeyLink, cmdString);
84 CmdKeyLink *cmdNxt = NULL;
85
86 if (*string == '\n') {
87 free(cmdkey);
88 return;
89 }
90
92 if (cmdHistory->count != 0) {//这个分支的目的是去重,又不遍历整个链表,感觉没多大意义.
93 cmdNxt = SH_LIST_ENTRY(cmdHistory->list.pstPrev, CmdKeyLink, list);
94 if (strcmp(string, cmdNxt->cmdString) == 0) {
95 free((void *)cmdkey);
97 return;
98 }
99 }
100
101 if (cmdHistory->count >= CMD_HISTORY_LEN) {//已经满了的处理,则需要删除一条再插入新的,数量不变,所以不存在 count的 +-
102 cmdNxt = SH_LIST_ENTRY(cmdHistory->list.pstNext, CmdKeyLink, list);//拿到下一条命令行记录
103 SH_ListDelete(&(cmdNxt->list));//将自己摘出去
104 SH_ListTailInsert(&(cmdHistory->list), &(cmdkey->list));//尾部插入新的命令
105 free((void *)cmdNxt);//释放旧数据
107 return;
108 }
109
110 SH_ListTailInsert(&(cmdHistory->list), &(cmdkey->list));//从尾部插入
111 cmdHistory->count++;//历史记录的数量增加
112
114 return;
115}
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
struct SH_List * pstPrev
Definition: shell_list.h:51
pthread_mutex_t historyMutex
操作cmdHistoryKeyLink的互斥量
Definition: shell.h:82
函数调用图:
这是这个函数的调用关系图:

◆ ShellTask()

void * ShellTask ( void argv)

在文件 shmsg.c601 行定义.

602{
603 int ret;
604 ShellCB *shellCB = (ShellCB *)argv;
605
606 if (shellCB == NULL) {
607 return NULL;
608 }
609
610 ret = prctl(PR_SET_NAME, "ShellTask");
611 if (ret != SH_OK) {
612 return NULL;
613 }
614
615 printf(SHELL_PROMPT);
616 while (1) {
617 ret = ShellPend(shellCB);
618 if (ret == SH_OK) {
619 ShellCmdProcess(shellCB);
620 } else if (ret != SH_OK) {
621 break;
622 }
623 }
624
625 return NULL;
626}
static void ShellCmdProcess(ShellCB *shellCB)
Definition: shmsg.c:582
int ShellPend(ShellCB *shellCB)
Definition: shmsg.c:117
Definition: shell.h:71
函数调用图:
这是这个函数的调用关系图:

◆ ShellTaskInit()

int ShellTaskInit ( ShellCB shellCB)

给控制台注册一个shell客户端任务

在文件 shmsg.c628 行定义.

◆ ShellTaskNotify()

void ShellTaskNotify ( ShellCB shellCB)

在文件 shmsg.c178 行定义.

179{
180 int ret;
181
182 (void)pthread_mutex_lock(&shellCB->keyMutex);
183 OsShellCmdPush(shellCB->shellBuf, shellCB->cmdKeyLink);
185
186 ret = ShellNotify(shellCB);//通知解析shell命令的任务
187 if (ret != SH_OK) {
188 printf("command execute failed, \"%s\"", shellCB->shellBuf);
189 }
190}
void OsShellCmdPush(const char *string, CmdKeyLink *cmdKeyLink)
将shell命令 string 以 CmdKeyLink 方式加入链表
Definition: shcmd.c:514
int ShellNotify(ShellCB *shellCB)
Definition: shmsg.c:126
函数调用图:
这是这个函数的调用关系图: