add OpenHarmony 1.0 baseline

This commit is contained in:
wenjun
2020-09-08 10:21:39 +08:00
parent 94f5f466b4
commit 6df931fc98
736 changed files with 111817 additions and 0 deletions

144
apps/shell/src/main.c Executable file
View File

@@ -0,0 +1,144 @@
/*
* Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "show.h"
#include "shmsg.h"
#include "shcmd.h"
#include "semaphore.h"
#include "securec.h"
#include "unistd.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
ShellCB *g_shellCB = NULL;
ShellCB *OsGetShellCb()
{
return g_shellCB;
}
void ShellDeinit(ShellCB *shellCB)
{
(void)pthread_mutex_destroy(&shellCB->historyMutex);
(void)pthread_mutex_destroy(&shellCB->keyMutex);
OsShellKeyDeInit((CmdKeyLink *)shellCB->cmdKeyLink);
OsShellKeyDeInit((CmdKeyLink *)shellCB->cmdHistoryKeyLink);
(void)free(shellCB);
}
static int OsShellCreateTask(ShellCB *shellCB)
{
struct sched_param param = { 0 };
int ret;
ret = sched_getparam(getpid(), &param);
if (ret != SH_OK) {
goto OUT;
}
param.sched_priority = SHELL_PROCESS_PRIORITY_INIT;
ret = sched_setparam(getpid(), &param);
if (ret != SH_OK) {
goto OUT;
}
ret = ShellTaskInit(shellCB);
if (ret != SH_OK) {
goto OUT;
}
ret = ShellEntryInit(shellCB);
if (ret != SH_OK) {
goto OUT;
}
(void)pthread_join(shellCB->shellTaskHandle, NULL);
(void)pthread_join(shellCB->shellEntryHandle, NULL);
OUT:
ShellDeinit(shellCB);
return ret;
}
int main()
{
int ret = SH_NOK;
ShellCB *shellCB = NULL;
shellCB = (ShellCB *)malloc(sizeof(ShellCB));
if (shellCB == NULL) {
goto ERR_OUT1;
}
ret = memset_s(shellCB, sizeof(ShellCB), 0, sizeof(ShellCB));
if (ret != SH_OK) {
goto ERR_OUT1;
}
ret = pthread_mutex_init(&shellCB->keyMutex, NULL);
if (ret != SH_OK) {
goto ERR_OUT1;
}
ret = pthread_mutex_init(&shellCB->historyMutex, NULL);
if (ret != SH_OK) {
goto ERR_OUT2;
}
ret = (int)OsShellKeyInit(shellCB);
if (ret != SH_OK) {
goto ERR_OUT3;
}
(void)strncpy_s(shellCB->shellWorkingDirectory, PATH_MAX, "/", 2); /* 2:space for "/" */
sem_init(&shellCB->shellSem, 0, 0);
g_shellCB = shellCB;
return OsShellCreateTask(shellCB);
ERR_OUT3:
(void)pthread_mutex_destroy(&shellCB->historyMutex);
ERR_OUT2:
(void)pthread_mutex_destroy(&shellCB->keyMutex);
ERR_OUT1:
(void)free(shellCB);
return ret;
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

611
apps/shell/src/shcmd.c Executable file
View File

@@ -0,0 +1,611 @@
/*
* Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "shcmd.h"
#include "show.h"
#include "stdlib.h"
#include "unistd.h"
#include "dirent.h"
#include "securec.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
#define SHELL_INIT_MAGIC_FLAG 0xABABABAB
#define CTRL_C 0x03 /* 0x03: ctrl+c ASCII */
static void OsFreeCmdPara(CmdParsed *cmdParsed)
{
unsigned int i;
for (i = 0; i < cmdParsed->paramCnt; i++) {
if ((cmdParsed->paramArray[i]) != NULL) {
free((cmdParsed->paramArray[i]));
cmdParsed->paramArray[i] = NULL;
}
}
}
static int OsStrSeparateTabStrGet(const char **tabStr, CmdParsed *parsed, unsigned int tabStrLen)
{
char *shiftStr = NULL;
char *tempStr = (char *)malloc(SHOW_MAX_LEN << 1);
if (tempStr == NULL) {
return (int)SH_ERROR;
}
(void)memset_s(tempStr, SHOW_MAX_LEN << 1, 0, SHOW_MAX_LEN << 1);
shiftStr = tempStr + SHOW_MAX_LEN;
if (strncpy_s(tempStr, SHOW_MAX_LEN - 1, *tabStr, tabStrLen)) {
free(tempStr);
return (int)SH_ERROR;
}
parsed->cmdType = CMD_TYPE_STD;
/* cut useless or repeat space */
if (OsCmdKeyShift(tempStr, shiftStr, SHOW_MAX_LEN - 1)) {
free(tempStr);
return (int)SH_ERROR;
}
/* get exact position of string to complete */
/* situation different if end space lost or still exist */
if ((strlen(shiftStr) == 0) || (tempStr[strlen(tempStr) - 1] != shiftStr[strlen(shiftStr) - 1])) {
*tabStr = "";
} else {
if (OsCmdTokenSplit(shiftStr, ' ', parsed)) {
free(tempStr);
return (int)SH_ERROR;
}
*tabStr = parsed->paramArray[parsed->paramCnt - 1];
}
free(tempStr);
return SH_OK;
}
char *OsShellGetWorkingDirtectory()
{
return OsGetShellCb()->shellWorkingDirectory;
}
int OsShellSetWorkingDirtectory(const char *dir, size_t len)
{
if (dir == NULL) {
return SH_NOK;
}
int ret = strncpy_s(OsGetShellCb()->shellWorkingDirectory, sizeof(OsGetShellCb()->shellWorkingDirectory),
dir, len);
if (ret != SH_OK) {
return SH_NOK;
}
return SH_OK;
}
static int OsStrSeparate(const char *tabStr, char *strPath, char *nameLooking, unsigned int tabStrLen)
{
char *strEnd = NULL;
char *cutPos = NULL;
CmdParsed parsed = {0};
char *shellWorkingDirectory = OsShellGetWorkingDirtectory();
int ret;
ret = OsStrSeparateTabStrGet(&tabStr, &parsed, tabStrLen);
if (ret != SH_OK) {
return ret;
}
/* get fullpath str */
if (*tabStr != '/') {
if (strncpy_s(strPath, CMD_MAX_PATH, shellWorkingDirectory, CMD_MAX_PATH - 1)) {
OsFreeCmdPara(&parsed);
return (int)SH_ERROR;
}
if (strcmp(shellWorkingDirectory, "/")) {
if (strncat_s(strPath, CMD_MAX_PATH - 1, "/", CMD_MAX_PATH - strlen(strPath) - 1)) {
OsFreeCmdPara(&parsed);
return (int)SH_ERROR;
}
}
}
if (strncat_s(strPath, CMD_MAX_PATH - 1, tabStr, CMD_MAX_PATH - strlen(strPath) - 1)) {
OsFreeCmdPara(&parsed);
return (int)SH_ERROR;
}
/* split str by last '/' */
strEnd = strrchr(strPath, '/');
if (strEnd != NULL) {
if (strncpy_s(nameLooking, CMD_MAX_PATH, strEnd + 1, CMD_MAX_PATH - 1)) { /* get cmp str */
OsFreeCmdPara(&parsed);
return (int)SH_ERROR;
}
}
cutPos = strrchr(strPath, '/');
if (cutPos != NULL) {
*(cutPos + 1) = '\0';
}
OsFreeCmdPara(&parsed);
return SH_OK;
}
static int OsShowPageInputControl(void)
{
char readChar;
while (1) {
if (read(STDIN_FILENO, &readChar, 1) != 1) { /* get one char from stdin */
printf("\n");
return (int)SH_ERROR;
}
if ((readChar == 'q') || (readChar == 'Q') || (readChar == CTRL_C)) {
printf("\n");
return 0;
} else if (readChar == '\r') {
printf("\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b");
return 1;
}
}
}
static int OsShowPageControl(unsigned int timesPrint, unsigned int lineCap, unsigned int count)
{
if (NEED_NEW_LINE(timesPrint, lineCap)) {
printf("\n");
if (SCREEN_IS_FULL(timesPrint, lineCap) && (timesPrint < count)) {
printf("--More--");
return OsShowPageInputControl();
}
}
return 1;
}
static int OsSurePrintAll(unsigned int count)
{
char readChar = 0;
printf("\nDisplay all %u possibilities?(y/n)", count);
while (1) {
if (read(0, &readChar, 1) != 1) {
return (int)SH_ERROR;
}
if ((readChar == 'n') || (readChar == 'N') || (readChar == CTRL_C)) {
printf("\n");
return 0;
} else if ((readChar == 'y') || (readChar == 'Y') || (readChar == '\r')) {
return 1;
}
}
}
static int OsPrintMatchList(unsigned int count, const char *strPath, const char *nameLooking, unsigned int printLen)
{
unsigned int timesPrint = 0;
unsigned int lineCap;
int ret;
DIR *openDir = NULL;
struct dirent *readDir = NULL;
char formatChar[10] = {0}; /* 10:for formatChar length */
printLen = (printLen > (DEFAULT_SCREEN_WIDTH - 2)) ? (DEFAULT_SCREEN_WIDTH - 2) : printLen; /* 2:revered 2 bytes */
lineCap = DEFAULT_SCREEN_WIDTH / (printLen + 2); /* 2:DEFAULT_SCREEN_WIDTH revered 2 bytes */
if (snprintf_s(formatChar, sizeof(formatChar) - 1, 7, "%%-%us ", printLen) < 0) { /* 7:format-len */
return (int)SH_ERROR;
}
if (count > (lineCap * DEFAULT_SCREEN_HEIGNT)) {
ret = OsSurePrintAll(count);
if (ret != 1) {
return ret;
}
}
openDir = opendir(strPath);
if (openDir == NULL) {
return (int)SH_ERROR;
}
printf("\n");
for (readDir = readdir(openDir); readDir != NULL; readDir = readdir(openDir)) {
if (strncmp(nameLooking, readDir->d_name, strlen(nameLooking)) != 0) {
continue;
}
printf(formatChar, readDir->d_name);
timesPrint++;
ret = OsShowPageControl(timesPrint, lineCap, count);
if (ret != 1) {
if (closedir(openDir) < 0) {
return (int)SH_ERROR;
}
return ret;
}
}
printf("\n");
if (closedir(openDir) < 0) {
return (int)SH_ERROR;
}
return SH_OK;
}
static void StrncmpCut(const char *s1, char *s2, size_t n)
{
if ((n == 0) || (s1 == NULL) || (s2 == NULL)) {
return;
}
do {
if (*s1 && *s2 && (*s1 == *s2)) {
s1++;
s2++;
} else {
break;
}
} while (--n != 0);
if (n > 0) {
/* NULL pad the remaining n-1 bytes */
while (n-- != 0) {
*s2++ = 0;
}
}
return;
}
static void OsCompleteStr(char *result, const char *target, char *cmdKey, unsigned int *len)
{
unsigned int size = strlen(result) - strlen(target);
char *des = cmdKey + *len;
char *src = result + strlen(target);
while (size-- > 0) {
printf("%c", *src);
if (*len == (SHOW_MAX_LEN - 1)) {
*des = '\0';
break;
}
*des++ = *src++;
(*len)++;
}
}
static int OsExecNameMatch(const char *strPath, const char *nameLooking, char *strObj, unsigned int *maxLen)
{
int count = 0;
DIR *openDir = NULL;
struct dirent *readDir = NULL;
openDir = opendir(strPath);
if (openDir == NULL) {
return (int)SH_ERROR;
}
for (readDir = readdir(openDir); readDir != NULL; readDir = readdir(openDir)) {
if (strncmp(nameLooking, readDir->d_name, strlen(nameLooking)) != 0) {
continue;
}
if (count == 0) {
if (strncpy_s(strObj, CMD_MAX_PATH, readDir->d_name, CMD_MAX_PATH - 1)) {
(void)closedir(openDir);
return (int)SH_ERROR;
}
*maxLen = strlen(readDir->d_name);
} else {
/* strncmp&cut the same strings of name matched */
StrncmpCut(readDir->d_name, strObj, strlen(strObj));
if (strlen(readDir->d_name) > *maxLen) {
*maxLen = strlen(readDir->d_name);
}
}
count++;
}
if (closedir(openDir) < 0) {
return (int)SH_ERROR;
}
return count;
}
static int OsTabMatchFile(char *cmdKey, unsigned int *len)
{
unsigned int maxLen = 0;
int count;
char *strOutput = NULL;
char *strCmp = NULL;
char *dirOpen = (char *)malloc(CMD_MAX_PATH * 3); /* 3:dirOpen\strOutput\strCmp */
if (dirOpen == NULL) {
return (int)SH_ERROR;
}
(void)memset_s(dirOpen, CMD_MAX_PATH * 3, 0, CMD_MAX_PATH * 3); /* 3:dirOpen\strOutput\strCmp */
strOutput = dirOpen + CMD_MAX_PATH;
strCmp = strOutput + CMD_MAX_PATH;
if (OsStrSeparate(cmdKey, dirOpen, strCmp, *len)) {
free(dirOpen);
return (int)SH_ERROR;
}
count = OsExecNameMatch(dirOpen, strCmp, strOutput, &maxLen);
/* one or more matched */
if (count >= 1) {
OsCompleteStr(strOutput, strCmp, cmdKey, len);
if (count == 1) {
free(dirOpen);
return 1;
}
if (OsPrintMatchList((unsigned int)count, dirOpen, strCmp, maxLen) == -1) {
free(dirOpen);
return (int)SH_ERROR;
}
}
free(dirOpen);
return count;
}
/*
* Description: Pass in the string and clear useless space ,which inlcude:
* 1) The overmatch space which is not be marked by Quote's area
* Squeeze the overmatch space into one space
* 2) Clear all space before first vaild charatctor
* Input: cmdKey : Pass in the buff string, which is ready to be operated
* cmdOut : Pass out the buffer string ,which has already been operated
* size : cmdKey length
*/
unsigned int OsCmdKeyShift(const char *cmdKey, char *cmdOut, unsigned int size)
{
char *output = NULL;
char *outputBak = NULL;
unsigned int len;
int ret;
bool quotes = FALSE;
if ((cmdKey == NULL) || (cmdOut == NULL)) {
return (unsigned int)SH_ERROR;
}
len = strlen(cmdKey);
if ((*cmdKey == '\n') || (len >= size)) {
return (unsigned int)SH_ERROR;
}
output = (char *)malloc(len + 1);
if (output == NULL) {
printf("malloc failure in %s[%d]", __FUNCTION__, __LINE__);
return (unsigned int)SH_ERROR;
}
/* Backup the 'output' start address */
outputBak = output;
/* Scan each charactor in 'cmdKey',and squeeze the overmuch space and ignore invaild charactor */
for (; *cmdKey != '\0'; cmdKey++) {
/* Detected a Double Quotes, switch the matching status */
if (*(cmdKey) == '\"') {
SWITCH_QUOTES_STATUS(quotes);
}
/* Ignore the current charactor in following situation */
/* 1) Quotes matching status is FALSE (which said that the space is not been marked by double quotes) */
/* 2) Current charactor is a space */
/* 3) Next charactor is a space too, or the string is been seeked to the end already(\0) */
/* 4) Invaild charactor, such as single quotes */
if ((*cmdKey == ' ') && ((*(cmdKey + 1) == ' ') || (*(cmdKey + 1) == '\0')) && QUOTES_STATUS_CLOSE(quotes)) {
continue;
}
if (*cmdKey == '\'') {
continue;
}
*output = *cmdKey;
output++;
}
*output = '\0';
/* Restore the 'output' start address */
output = outputBak;
len = strlen(output);
/* Clear the space which is located at the first charactor in buffer */
if (*output == ' ') {
output++;
len--;
}
/* Copy out the buffer which is been operated already */
ret = strncpy_s(cmdOut, size, output, len);
if (ret != SH_OK) {
printf("%s,%d strncpy_s failed, err:%d!\n", __FUNCTION__, __LINE__, ret);
free(outputBak);
return SH_ERROR;
}
cmdOut[len] = '\0';
free(outputBak);
return SH_OK;
}
int OsTabCompletion(char *cmdKey, unsigned int *len)
{
int count;
char *cmdMainStr = cmdKey;
if ((cmdKey == NULL) || (len == NULL)) {
return (int)SH_ERROR;
}
/* cut left space */
while (*cmdMainStr == 0x20) {
cmdMainStr++;
}
count = OsTabMatchFile(cmdKey, len);
return count;
}
unsigned int OsShellKeyInit(ShellCB *shellCB)
{
CmdKeyLink *cmdKeyLink = NULL;
CmdKeyLink *cmdHistoryLink = NULL;
if (shellCB == NULL) {
return SH_ERROR;
}
cmdKeyLink = (CmdKeyLink *)malloc(sizeof(CmdKeyLink));
if (cmdKeyLink == NULL) {
printf("Shell CmdKeyLink memory alloc error!\n");
return SH_ERROR;
}
cmdHistoryLink = (CmdKeyLink *)malloc(sizeof(CmdKeyLink));
if (cmdHistoryLink == NULL) {
free(cmdKeyLink);
printf("Shell CmdHistoryLink memory alloc error!\n");
return SH_ERROR;
}
cmdKeyLink->count = 0;
SH_ListInit(&(cmdKeyLink->list));
shellCB->cmdKeyLink = (void *)cmdKeyLink;
cmdHistoryLink->count = 0;
SH_ListInit(&(cmdHistoryLink->list));
shellCB->cmdHistoryKeyLink = (void *)cmdHistoryLink;
shellCB->cmdMaskKeyLink = (void *)cmdHistoryLink;
return SH_OK;
}
void OsShellKeyDeInit(CmdKeyLink *cmdKeyLink)
{
CmdKeyLink *cmdtmp = NULL;
if (cmdKeyLink == NULL) {
return;
}
while (!SH_ListEmpty(&(cmdKeyLink->list))) {
cmdtmp = SH_LIST_ENTRY(cmdKeyLink->list.pstNext, CmdKeyLink, list);
SH_ListDelete(&cmdtmp->list);
free(cmdtmp);
}
cmdKeyLink->count = 0;
free(cmdKeyLink);
}
void OsShellCmdPush(const char *string, CmdKeyLink *cmdKeyLink)
{
CmdKeyLink *cmdNewNode = NULL;
unsigned int len;
if ((string == NULL) || (strlen(string) == 0)) {
return;
}
len = strlen(string);
cmdNewNode = (CmdKeyLink *)malloc(sizeof(CmdKeyLink) + len + 1);
if (cmdNewNode == NULL) {
return;
}
(void)memset_s(cmdNewNode, sizeof(CmdKeyLink) + len + 1, 0, sizeof(CmdKeyLink) + len + 1);
if (strncpy_s(cmdNewNode->cmdString, len + 1, string, len)) {
free(cmdNewNode);
return;
}
SH_ListTailInsert(&(cmdKeyLink->list), &(cmdNewNode->list));
return;
}
void OsShellHistoryShow(unsigned int value, ShellCB *shellCB)
{
CmdKeyLink *cmdtmp = NULL;
CmdKeyLink *cmdNode = shellCB->cmdHistoryKeyLink;
CmdKeyLink *cmdMask = shellCB->cmdMaskKeyLink;
int ret;
(void)pthread_mutex_lock(&shellCB->historyMutex);
if (value == CMD_KEY_DOWN) {
if (cmdMask == cmdNode) {
goto END;
}
cmdtmp = SH_LIST_ENTRY(cmdMask->list.pstNext, CmdKeyLink, list);
if (cmdtmp != cmdNode) {
cmdMask = cmdtmp;
} else {
goto END;
}
} else if (value == CMD_KEY_UP) {
cmdtmp = SH_LIST_ENTRY(cmdMask->list.pstPrev, CmdKeyLink, list);
if (cmdtmp != cmdNode) {
cmdMask = cmdtmp;
} else {
goto END;
}
}
while (shellCB->shellBufOffset--) {
printf("\b \b");
}
printf("%s", cmdMask->cmdString);
shellCB->shellBufOffset = strlen(cmdMask->cmdString);
(void)memset_s(shellCB->shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN);
ret = memcpy_s(shellCB->shellBuf, SHOW_MAX_LEN, cmdMask->cmdString, shellCB->shellBufOffset);
if (ret != SH_OK) {
printf("%s, %d memcpy failed!\n", __FUNCTION__, __LINE__);
goto END;
}
shellCB->cmdMaskKeyLink = (void *)cmdMask;
END:
(void)pthread_mutex_unlock(&shellCB->historyMutex);
return;
}
unsigned int OsCmdExec(CmdParsed *cmdParsed, char *cmdStr)
{
/* TODO: complete the usrspace command */
unsigned int ret = SH_OK;
if (cmdParsed && cmdStr) {
ret = SH_NOK;
}
return ret;
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

164
apps/shell/src/shcmdparse.c Executable file
View File

@@ -0,0 +1,164 @@
/*
* Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "shcmd.h"
#include "sherr.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/*
* Filter out double quote or single-quoted strings at both ends
*/
char *OsCmdParseStrdup(const char *str)
{
char *tempStr = NULL;
char *newStr = NULL;
newStr = (char *)malloc(strlen(str) + 1);
if (newStr == NULL) {
return NULL;
}
tempStr = newStr;
for (; *str != '\0'; str++) {
if ((*str == '\"') || (*str == '\'')) {
continue;
}
*newStr = *str;
newStr++;
}
*newStr = '\0';
return tempStr;
}
unsigned int OsCmdParseParaGet(char **value, const char *paraTokenStr)
{
if ((paraTokenStr == NULL) || (value == NULL)) {
return (unsigned int)SH_ERROR;
}
*value = OsCmdParseStrdup(paraTokenStr);
if (*value == NULL) {
return SH_NOK;
}
return SH_OK;
}
unsigned int OsCmdParseOneToken(CmdParsed *cmdParsed, unsigned int index, const char *token)
{
unsigned int ret = SH_OK;
unsigned int tempLen;
if (cmdParsed == NULL) {
return (unsigned int)SH_ERROR;
}
if (index == 0) {
if (cmdParsed->cmdType != CMD_TYPE_STD) {
return ret;
}
}
if ((token != NULL) && (cmdParsed->paramCnt < CMD_MAX_PARAS)) {
tempLen = cmdParsed->paramCnt;
ret = OsCmdParseParaGet(&(cmdParsed->paramArray[tempLen]), token);
if (ret != SH_OK) {
return ret;
}
cmdParsed->paramCnt++;
}
return ret;
}
unsigned int OsCmdTokenSplit(char *cmdStr, char split, CmdParsed *cmdParsed)
{
enum {
STAT_INIT,
STAT_TOKEN_IN,
STAT_TOKEN_OUT
} state = STAT_INIT;
unsigned int count = 0;
char *p = NULL;
char *token = cmdStr;
unsigned int ret = SH_OK;
bool quotes = FALSE;
if (cmdStr == NULL) {
return (unsigned int)SH_ERROR;
}
for (p = cmdStr; (*p != '\0') && (ret == SH_OK); p++) {
if (*p == '\"') {
SWITCH_QUOTES_STATUS(quotes);
}
switch (state) {
case STAT_INIT:
case STAT_TOKEN_IN:
if ((*p == split) && QUOTES_STATUS_CLOSE(quotes)) {
*p = '\0';
ret = OsCmdParseOneToken(cmdParsed, count++, token);
state = STAT_TOKEN_OUT;
}
break;
case STAT_TOKEN_OUT:
if (*p != split) {
token = p;
state = STAT_TOKEN_IN;
}
break;
default:
break;
}
}
if (((ret == SH_OK) && (state == STAT_TOKEN_IN)) || (state == STAT_INIT)) {
ret = OsCmdParseOneToken(cmdParsed, count, token);
}
return ret;
}
unsigned int OsCmdParse(char *cmdStr, CmdParsed *cmdParsed)
{
if ((cmdStr == NULL) || (cmdParsed == NULL) || (strlen(cmdStr) == 0)) {
return (unsigned int)SH_ERROR;
}
return OsCmdTokenSplit(cmdStr, ' ', cmdParsed);
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

640
apps/shell/src/shmsg.c Executable file
View File

@@ -0,0 +1,640 @@
/*
* Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdlib.h"
#include "stdio.h"
#include "unistd.h"
#include "sys/prctl.h"
#include "sys/ioctl.h"
#include "syscall.h"
#include "sys/wait.h"
#include "pthread.h"
#include "securec.h"
#include "shmsg.h"
#include "shell_pri.h"
#include "shcmd.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
char *GetCmdline(ShellCB *shellCB)
{
CmdKeyLink *cmdkey = shellCB->cmdKeyLink;
CmdKeyLink *cmdNode = NULL;
(void)pthread_mutex_lock(&shellCB->keyMutex);
if ((cmdkey == NULL) || SH_ListEmpty(&cmdkey->list)) {
(void)pthread_mutex_unlock(&shellCB->keyMutex);
return NULL;
}
cmdNode = SH_LIST_ENTRY(cmdkey->list.pstNext, CmdKeyLink, list);
SH_ListDelete(&(cmdNode->list));
(void)pthread_mutex_unlock(&shellCB->keyMutex);
return cmdNode->cmdString;
}
static void ShellSaveHistoryCmd(char *string, ShellCB *shellCB)
{
CmdKeyLink *cmdHistory = shellCB->cmdHistoryKeyLink;
CmdKeyLink *cmdkey = SH_LIST_ENTRY(string, CmdKeyLink, cmdString);
CmdKeyLink *cmdNxt = NULL;
if ((string == NULL) || (*string == '\n') || (strlen(string) == 0)) {
return;
}
(void)pthread_mutex_lock(&shellCB->historyMutex);
if (cmdHistory->count != 0) {
cmdNxt = SH_LIST_ENTRY(cmdHistory->list.pstPrev, CmdKeyLink, list);
if (strcmp(string, cmdNxt->cmdString) == 0) {
free((void *)cmdkey);
(void)pthread_mutex_unlock(&shellCB->historyMutex);
return;
}
}
if (cmdHistory->count == CMD_HISTORY_LEN) {
cmdNxt = SH_LIST_ENTRY(cmdHistory->list.pstNext, CmdKeyLink, list);
SH_ListDelete(&(cmdNxt->list));
SH_ListTailInsert(&(cmdHistory->list), &(cmdkey->list));
free((void *)cmdNxt);
(void)pthread_mutex_unlock(&shellCB->historyMutex);
return;
}
SH_ListTailInsert(&(cmdHistory->list), &(cmdkey->list));
cmdHistory->count++;
(void)pthread_mutex_unlock(&shellCB->historyMutex);
return;
}
int ShellPend(ShellCB *shellCB)
{
if (shellCB == NULL) {
return SH_NOK;
}
return sem_wait(&shellCB->shellSem);
}
int ShellNotify(ShellCB *shellCB)
{
if (shellCB == NULL) {
return SH_NOK;
}
return sem_post(&shellCB->shellSem);
}
enum {
STAT_NOMAL_KEY,
STAT_ESC_KEY,
STAT_MULTI_KEY
};
static int ShellCmdLineCheckUDRL(const char ch, ShellCB *shellCB)
{
int ret = SH_OK;
if (ch == 0x1b) { /* 0x1b: ESC */
shellCB->shellKeyType = STAT_ESC_KEY;
return ret;
} else if (ch == 0x5b) { /* 0x5b: first Key combination */
if (shellCB->shellKeyType == STAT_ESC_KEY) {
shellCB->shellKeyType = STAT_MULTI_KEY;
return ret;
}
} else if (ch == 0x41) { /* up */
if (shellCB->shellKeyType == STAT_MULTI_KEY) {
OsShellHistoryShow(CMD_KEY_UP, shellCB);
shellCB->shellKeyType = STAT_NOMAL_KEY;
return ret;
}
} else if (ch == 0x42) { /* down */
if (shellCB->shellKeyType == STAT_MULTI_KEY) {
shellCB->shellKeyType = STAT_NOMAL_KEY;
OsShellHistoryShow(CMD_KEY_DOWN, shellCB);
return ret;
}
} else if (ch == 0x43) { /* right */
if (shellCB->shellKeyType == STAT_MULTI_KEY) {
shellCB->shellKeyType = STAT_NOMAL_KEY;
return ret;
}
} else if (ch == 0x44) { /* left */
if (shellCB->shellKeyType == STAT_MULTI_KEY) {
shellCB->shellKeyType = STAT_NOMAL_KEY;
return ret;
}
}
return SH_NOK;
}
void ShellTaskNotify(ShellCB *shellCB)
{
int ret;
(void)pthread_mutex_lock(&shellCB->keyMutex);
OsShellCmdPush(shellCB->shellBuf, shellCB->cmdKeyLink);
(void)pthread_mutex_unlock(&shellCB->keyMutex);
ret = ShellNotify(shellCB);
if (ret != SH_OK) {
printf("command execute failed, \"%s\"", shellCB->shellBuf);
}
}
void ParseEnterKey(OutputFunc outputFunc, ShellCB *shellCB)
{
if ((shellCB == NULL) || (outputFunc == NULL)) {
return;
}
if (shellCB->shellBufOffset == 0) {
shellCB->shellBuf[shellCB->shellBufOffset] = '\n';
shellCB->shellBuf[shellCB->shellBufOffset + 1] = '\0';
goto NOTIFY;
}
if (shellCB->shellBufOffset <= (SHOW_MAX_LEN - 1)) {
shellCB->shellBuf[shellCB->shellBufOffset] = '\0';
}
NOTIFY:
outputFunc("\n");
shellCB->shellBufOffset = 0;
ShellTaskNotify(shellCB);
}
void ParseDeleteKey(OutputFunc outputFunc, ShellCB *shellCB)
{
if ((shellCB == NULL) || (outputFunc == NULL)) {
return;
}
if ((shellCB->shellBufOffset > 0) && (shellCB->shellBufOffset <= (SHOW_MAX_LEN - 1))) {
shellCB->shellBuf[shellCB->shellBufOffset - 1] = '\0';
shellCB->shellBufOffset--;
outputFunc("\b \b");
}
}
void ParseTabKey(OutputFunc outputFunc, ShellCB *shellCB)
{
int ret;
if ((shellCB == NULL) || (outputFunc == NULL)) {
return;
}
if ((shellCB->shellBufOffset > 0) && (shellCB->shellBufOffset < (SHOW_MAX_LEN - 1))) {
ret = OsTabCompletion(shellCB->shellBuf, &shellCB->shellBufOffset);
if (ret > 1) {
outputFunc(SHELL_PROMPT"%s", shellCB->shellBuf);
}
}
}
void ParseNormalChar(char ch, OutputFunc outputFunc, ShellCB *shellCB)
{
if ((shellCB == NULL) || (outputFunc == NULL)) {
return;
}
if ((ch != '\0') && (shellCB->shellBufOffset < (SHOW_MAX_LEN - 1))) {
shellCB->shellBuf[shellCB->shellBufOffset] = ch;
shellCB->shellBufOffset++;
outputFunc("%c", ch);
}
shellCB->shellKeyType = STAT_NOMAL_KEY;
}
void ShellCmdLineParse(char c, OutputFunc outputFunc, ShellCB *shellCB)
{
const char ch = c;
int ret;
if ((shellCB->shellBufOffset == 0) && (ch != '\n') && (ch != '\0')) {
(void)memset_s(shellCB->shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN);
}
switch (ch) {
case '\r':
case '\n': /* enter */
ParseEnterKey(outputFunc, shellCB);
break;
case '\b': /* backspace */
case 0x7F: /* delete(0x7F) */
ParseDeleteKey(outputFunc, shellCB);
break;
case '\t': /* tab */
ParseTabKey(outputFunc, shellCB);
break;
default:
/* parse the up/down/right/left key */
ret = ShellCmdLineCheckUDRL(ch, shellCB);
if (ret == SH_OK) {
return;
}
ParseNormalChar(ch, outputFunc, shellCB);
break;
}
return;
}
unsigned int ShellMsgNameGet(CmdParsed *cmdParsed, const char *cmdType)
{
(void)cmdParsed;
(void)cmdType;
return SH_ERROR;
}
char *GetCmdName(const char *cmdline, unsigned int len)
{
unsigned int loop;
const char *tmpStr = NULL;
bool quotes = FALSE;
char *cmdName = NULL;
if (cmdline == NULL) {
return NULL;
}
cmdName = (char *)malloc(len + 1);
if (cmdName == NULL) {
printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
return NULL;
}
/* Scan the 'cmdline' string for command */
/* Notice: Command string must not have any special name */
for (tmpStr = cmdline, loop = 0; (*tmpStr != '\0') && (loop < len); ) {
/* If reach a double quotes, switch the quotes matching status */
if (*tmpStr == '\"') {
SWITCH_QUOTES_STATUS(quotes);
/* Ignore the double quote charactor itself */
tmpStr++;
continue;
}
/* If detected a space which the quotes matching status is false */
/* which said has detected the first space for seperator, finish this scan operation */
if ((*tmpStr == ' ') && (QUOTES_STATUS_CLOSE(quotes))) {
break;
}
cmdName[loop] = *tmpStr++;
loop++;
}
cmdName[loop] = '\0';
return cmdName;
}
static void DoCmdExec(const char *cmdName, const char *cmdline, unsigned int len, const CmdParsed *cmdParsed)
{
int ret;
pid_t forkPid;
if (strncmp(cmdline, SHELL_EXEC_COMMAND, SHELL_EXEC_COMMAND_BYTES) == 0) {
forkPid = fork();
if (forkPid < 0) {
printf("Faild to fork from shell\n");
return;
} else if (forkPid == 0) {
ret = setpgrp();
if (ret == -1) {
exit(1);
}
ret = execve((const char *)cmdParsed->paramArray[0], (char * const *)cmdParsed->paramArray, NULL);
if (ret == -1) {
perror("execve");
exit(-1);
}
}
} else {
(void)syscall(__NR_shellexec, cmdName, cmdline);
}
}
static void ParseAndExecCmdline(CmdParsed *cmdParsed, const char *cmdline, unsigned int len)
{
int i;
unsigned int ret;
char shellWorkingDirectory[PATH_MAX + 1] = { 0 };
char *cmdlineOrigin = NULL;
char *cmdName = NULL;
cmdlineOrigin = strdup(cmdline);
if (cmdlineOrigin == NULL) {
printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
return;
}
cmdName = GetCmdName(cmdline, len);
if (cmdName == NULL) {
free(cmdlineOrigin);
printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
return;
}
ret = OsCmdParse((char *)cmdline, cmdParsed);
if (ret != SH_OK) {
printf("cmd parse failure in %s[%d]\n", __FUNCTION__, __LINE__);
goto OUT;
}
DoCmdExec(cmdName, cmdlineOrigin, len, cmdParsed);
if (getcwd(shellWorkingDirectory, PATH_MAX) != NULL) {
(void)OsShellSetWorkingDirtectory(shellWorkingDirectory, (PATH_MAX + 1));
}
OUT:
for (i = 0; i < cmdParsed->paramCnt; i++) {
if (cmdParsed->paramArray[i] != NULL) {
free(cmdParsed->paramArray[i]);
cmdParsed->paramArray[i] = NULL;
}
}
free(cmdName);
free(cmdlineOrigin);
}
unsigned int PreHandleCmdline(const char *input, char **output, unsigned int *outputlen)
{
unsigned int shiftLen, execLen, newLen;
unsigned int removeLen = strlen("./"); /* "./" needs to be removed if it exists */
unsigned int ret;
char *newCmd = NULL;
char *execCmd = SHELL_EXEC_COMMAND;
const char *cmdBuf = input;
unsigned int cmdBufLen = strlen(cmdBuf);
char *shiftStr = (char *)malloc(cmdBufLen + 1);
errno_t err;
if (shiftStr == NULL) {
printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
return SH_NOK;
}
(void)memset_s(shiftStr, cmdBufLen + 1, 0, cmdBufLen + 1);
/* Call function 'OsCmdKeyShift' to squeeze and clear useless or overmuch space if string buffer */
ret = OsCmdKeyShift(cmdBuf, shiftStr, cmdBufLen + 1);
shiftLen = strlen(shiftStr);
if ((ret != SH_OK) || (shiftLen == 0)) {
ret = SH_NOK;
goto END_FREE_SHIFTSTR;
}
*output = shiftStr;
*outputlen = shiftLen;
/* Check and parse "./", located at the first two charaters of the cmd */
if ((shiftLen > removeLen) && (shiftStr[0] == '.') && (shiftStr[1] == '/')) {
execLen = strlen(execCmd);
newLen = execLen + shiftLen - removeLen; /* i.e., newLen - execLen == shiftLen - removeLen */
newCmd = (char *)malloc(newLen + 1);
if (newCmd == NULL) {
ret = SH_NOK;
printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__);
goto END_FREE_SHIFTSTR;
}
err = memcpy_s(newCmd, newLen, execCmd, execLen);
if (err != EOK) {
printf("memcpy_s failure in %s[%d]\n", __FUNCTION__, __LINE__);
ret = SH_NOK;
goto END_FREE_NEWCMD;
}
err = memcpy_s(newCmd + execLen, newLen - execLen, shiftStr + removeLen, shiftLen - removeLen);
if (err != EOK) {
printf("memcpy_s failure in %s[%d]\n", __FUNCTION__, __LINE__);
ret = SH_NOK;
goto END_FREE_NEWCMD;
}
newCmd[newLen] = '\0';
*output = newCmd;
*outputlen = newLen;
ret = SH_OK;
goto END_FREE_SHIFTSTR;
} else {
ret = SH_OK;
goto END;
}
END_FREE_NEWCMD:
free(newCmd);
END_FREE_SHIFTSTR:
free(shiftStr);
END:
return ret;
}
static void ExecCmdline(const char *cmdline)
{
unsigned int ret;
char *output = NULL;
unsigned int outputlen;
CmdParsed cmdParsed;
if (cmdline == NULL) {
return;
}
/* strip out unnecessary characters */
ret = PreHandleCmdline(cmdline, &output, &outputlen);
if (ret == SH_NOK) {
return;
}
(void)memset_s(&cmdParsed, sizeof(CmdParsed), 0, sizeof(CmdParsed));
ParseAndExecCmdline(&cmdParsed, output, outputlen);
free(output);
}
void RecycleZombieChild(void)
{
while (waitpid(-1, NULL, WNOHANG) > 0) {
continue;
}
}
static void ShellCmdProcess(ShellCB *shellCB)
{
char *buf = NULL;
while (1) {
/* recycle zombine child process */
RecycleZombieChild();
buf = GetCmdline(shellCB);
if (buf == NULL) {
break;
}
ExecCmdline(buf);
ShellSaveHistoryCmd(buf, shellCB);
shellCB->cmdMaskKeyLink = shellCB->cmdHistoryKeyLink;
printf(SHELL_PROMPT);
}
}
void *ShellTask(void *argv)
{
int ret;
ShellCB *shellCB = (ShellCB *)argv;
if (shellCB == NULL) {
return NULL;
}
ret = prctl(PR_SET_NAME, "ShellTask");
if (ret != SH_OK) {
return NULL;
}
printf(SHELL_PROMPT);
while (1) {
ret = ShellPend(shellCB);
if (ret == SH_OK) {
ShellCmdProcess(shellCB);
} else if (ret != SH_OK) {
break;
}
}
return NULL;
}
int ShellTaskInit(ShellCB *shellCB)
{
unsigned int ret;
size_t stackSize = SHELL_TASK_STACKSIZE;
void *arg = NULL;
pthread_attr_t attr;
if (shellCB == NULL) {
return SH_NOK;
}
ret = pthread_attr_init(&attr);
if (ret != SH_OK) {
return SH_NOK;
}
pthread_attr_setstacksize(&attr, stackSize);
arg = (void *)shellCB;
ret = pthread_create(&shellCB->shellTaskHandle, &attr, &ShellTask, arg);
if (ret != SH_OK) {
return SH_NOK;
}
return ret;
}
static int ShellKernelReg(unsigned int shellHandle)
{
return ioctl(0, CONSOLE_CONTROL_REG_USERTASK, shellHandle);
}
void *ShellEntry(void *argv)
{
char ch;
int ret;
int n;
pid_t tid = syscall(__NR_gettid);
ShellCB *shellCB = (ShellCB *)argv;
if (shellCB == NULL) {
return NULL;
}
(void)memset_s(shellCB->shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN);
ret = prctl(PR_SET_NAME, "ShellEntry");
if (ret != SH_OK) {
return NULL;
}
ret = ShellKernelReg((int)tid);
if (ret != 0) {
printf("another shell is already running!\n");
exit(-1);
}
while (1) {
/* is console ready for shell ? */
if (ret != SH_OK)
break;
n = read(0, &ch, 1);
if (n == 1) {
ShellCmdLineParse(ch, (OutputFunc)printf, shellCB);
}
}
return NULL;
}
int ShellEntryInit(ShellCB *shellCB)
{
int ret;
size_t stackSize = SHELL_ENTRY_STACKSIZE;
void *arg = NULL;
pthread_attr_t attr;
if (shellCB == NULL) {
return SH_NOK;
}
ret = pthread_attr_init(&attr);
if (ret != SH_OK) {
return SH_NOK;
}
pthread_attr_setstacksize(&attr, stackSize);
arg = (void *)shellCB;
ret = pthread_create(&shellCB->shellEntryHandle, &attr, &ShellEntry, arg);
if (ret != SH_OK) {
return SH_NOK;
}
return ret;
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif