40#define SHELL_INIT_MAGIC_FLAG 0xABABABAB
46 for (i = 0; i < cmdParsed->
paramCnt; i++) {
56 char *shiftStr = NULL;
57 char *tempStr = (
char *)
malloc(SHOW_MAX_LEN << 1);
58 if (tempStr == NULL) {
62 (
void)memset_s(tempStr, SHOW_MAX_LEN << 1, 0, SHOW_MAX_LEN << 1);
63 shiftStr = tempStr + SHOW_MAX_LEN;
65 if (strncpy_s(tempStr, SHOW_MAX_LEN - 1, *tabStr, tabStrLen)) {
80 if ((strlen(shiftStr) == 0) || (tempStr[strlen(tempStr) - 1] != shiftStr[strlen(shiftStr) - 1])) {
113static int OsStrSeparate(
const char *tabStr,
char *strPath,
char *nameLooking,
unsigned int tabStrLen)
127 if (*tabStr !=
'/') {
128 if (strncpy_s(strPath, CMD_MAX_PATH, shellWorkingDirectory, CMD_MAX_PATH - 1)) {
130 return (
int)SH_ERROR;
132 if (strcmp(shellWorkingDirectory,
"/")) {
133 if (strncat_s(strPath, CMD_MAX_PATH - 1,
"/", CMD_MAX_PATH - strlen(strPath) - 1)) {
135 return (
int)SH_ERROR;
140 if (strncat_s(strPath, CMD_MAX_PATH - 1, tabStr, CMD_MAX_PATH - strlen(strPath) - 1)) {
142 return (
int)SH_ERROR;
146 strEnd = strrchr(strPath,
'/');
147 if (strEnd != NULL) {
148 if (strncpy_s(nameLooking, CMD_MAX_PATH, strEnd + 1, CMD_MAX_PATH - 1)) {
150 return (
int)SH_ERROR;
154 cutPos = strrchr(strPath,
'/');
155 if (cutPos != NULL) {
156 *(cutPos + 1) =
'\0';
168 if (read(STDIN_FILENO, &readChar, 1) != 1) {
170 return (
int)SH_ERROR;
172 if ((readChar ==
'q') || (readChar ==
'Q') || (readChar == CTRL_C)) {
175 }
else if (readChar ==
'\r') {
176 printf(
"\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b");
184 if (NEED_NEW_LINE(timesPrint, lineCap)) {
186 if (SCREEN_IS_FULL(timesPrint, lineCap) && (timesPrint < count)) {
197 printf(
"\nDisplay all %u possibilities?(y/n)", count);
199 if (read(STDIN_FILENO, &readChar, 1) != 1) {
200 return (
int)SH_ERROR;
202 if ((readChar ==
'n') || (readChar ==
'N') || (readChar == CTRL_C)) {
205 }
else if ((readChar ==
'y') || (readChar ==
'Y') || (readChar ==
'\r')) {
211static int OsPrintMatchList(
unsigned int count,
const char *strPath,
const char *nameLooking,
unsigned int printLen)
213 unsigned int timesPrint = 0;
214 unsigned int lineCap;
217 struct dirent *readDir = NULL;
218 char formatChar[10] = {0};
220 printLen = (printLen > (DEFAULT_SCREEN_WIDTH - 2)) ? (DEFAULT_SCREEN_WIDTH - 2) : printLen;
221 lineCap = DEFAULT_SCREEN_WIDTH / (printLen + 2);
222 if (snprintf_s(formatChar,
sizeof(formatChar) - 1, 7,
"%%-%us ", printLen) < 0) {
223 return (
int)SH_ERROR;
226 if (count > (lineCap * DEFAULT_SCREEN_HEIGHT)) {
232 openDir = opendir(strPath);
233 if (openDir == NULL) {
234 return (
int)SH_ERROR;
238 for (readDir = readdir(openDir); readDir != NULL; readDir = readdir(openDir)) {
239 if (strncmp(nameLooking, readDir->d_name, strlen(nameLooking)) != 0) {
242 printf(formatChar, readDir->d_name);
246 if (closedir(openDir) < 0) {
247 return (
int)SH_ERROR;
254 if (closedir(openDir) < 0) {
255 return (
int)SH_ERROR;
263 if ((n == 0) || (s1 == NULL) || (s2 == NULL)) {
267 if (*s1 && *s2 && (*s1 == *s2)) {
283static void OsCompleteStr(
char *result,
const char *target,
char *cmdKey,
unsigned int *len)
285 unsigned int size = strlen(result) - strlen(target);
286 char *des = cmdKey + *len;
287 char *src = result + strlen(target);
291 if (*len == (SHOW_MAX_LEN - 1)) {
300static int OsExecNameMatch(
const char *strPath,
const char *nameLooking,
char *strObj,
unsigned int *maxLen)
304 struct dirent *readDir = NULL;
306 openDir = opendir(strPath);
307 if (openDir == NULL) {
308 return (
int)SH_ERROR;
311 for (readDir = readdir(openDir); readDir != NULL; readDir = readdir(openDir)) {
312 if (strncmp(nameLooking, readDir->d_name, strlen(nameLooking)) != 0) {
316 if (strncpy_s(strObj, CMD_MAX_PATH, readDir->d_name, CMD_MAX_PATH - 1)) {
317 (
void)closedir(openDir);
318 return (
int)SH_ERROR;
320 *maxLen = strlen(readDir->d_name);
323 StrncmpCut(readDir->d_name, strObj, strlen(strObj));
324 if (strlen(readDir->d_name) > *maxLen) {
325 *maxLen = strlen(readDir->d_name);
331 if (closedir(openDir) < 0) {
332 return (
int)SH_ERROR;
340 unsigned int maxLen = 0;
342 char *strOutput = NULL;
344 char *dirOpen = (
char *)
malloc(CMD_MAX_PATH * 3);
345 if (dirOpen == NULL) {
346 return (
int)SH_ERROR;
349 (
void)memset_s(dirOpen, CMD_MAX_PATH * 3, 0, CMD_MAX_PATH * 3);
350 strOutput = dirOpen + CMD_MAX_PATH;
351 strCmp = strOutput + CMD_MAX_PATH;
355 return (
int)SH_ERROR;
369 return (
int)SH_ERROR;
386unsigned int OsCmdKeyShift(
const char *cmdKey,
char *cmdOut,
unsigned int size)
389 char *outputBak = NULL;
394 if ((cmdKey == NULL) || (cmdOut == NULL)) {
395 return (
unsigned int)SH_ERROR;
398 len = strlen(cmdKey);
399 if ((*cmdKey ==
'\n') || (len >= size)) {
400 return (
unsigned int)SH_ERROR;
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;
411 for (; *cmdKey !=
'\0'; cmdKey++) {
413 if (*(cmdKey) ==
'\"') {
414 SWITCH_QUOTES_STATUS(quotes);
421 if ((*cmdKey ==
' ') && ((*(cmdKey + 1) ==
' ') || (*(cmdKey + 1) ==
'\0')) && QUOTES_STATUS_CLOSE(quotes)) {
424 if (*cmdKey ==
'\'') {
433 len = strlen(output);
435 if (*output ==
' ') {
440 ret = strncpy_s(cmdOut, size, output, len);
442 printf(
"%s,%d strncpy_s failed, err:%d!\n", __FUNCTION__, __LINE__, ret);
456 if ((cmdKey == NULL) || (len == NULL)) {
457 return (
int)SH_ERROR;
470 if (shellCB == NULL) {
475 if (cmdKeyLink == NULL) {
476 printf(
"Shell CmdKeyLink memory alloc error!\n");
480 if (cmdHistoryLink == NULL) {
482 printf(
"Shell CmdHistoryLink memory alloc error!\n");
486 cmdKeyLink->
count = 0;
490 cmdHistoryLink->
count = 0;
500 if (cmdKeyLink == NULL) {
510 cmdKeyLink->
count = 0;
519 if ((
string == NULL) || (strlen(
string) == 0)) {
523 len = strlen(
string);
525 if (cmdNewNode == NULL) {
530 if (strncpy_s(cmdNewNode->
cmdString, len + 1,
string, len)) {
549 if (cmdMask == cmdNode) {
554 if (cmdtmp != cmdNode) {
561 if (cmdtmp != cmdNode) {
573 (
void)memset_s(shellCB->
shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN);
576 printf(
"%s, %d memcpy failed!\n", __FUNCTION__, __LINE__);
589 unsigned int ret = SH_OK;
590 if (cmdParsed && cmdStr) {
unsigned int OsCmdParse(char *cmdStr, CmdParsed *cmdParsed)
解析cmd命令,将关键字,参数分离出来
@ CMD_TYPE_STD
支持的标准命令参数输入,所有输入的字符都会通过命令解析后被传入。
ShellCB * OsGetShellCb(void)
获取shell控制块
static void OsCompleteStr(char *result, const char *target, char *cmdKey, unsigned int *len)
unsigned int OsCmdExec(CmdParsed *cmdParsed, char *cmdStr)
static void OsFreeCmdPara(CmdParsed *cmdParsed)
int OsShellSetWorkingDirectory(const char *dir, size_t len)
static int OsTabMatchFile(char *cmdKey, unsigned int *len)
static int OsSurePrintAll(unsigned int count)
static int OsShowPageControl(unsigned int timesPrint, unsigned int lineCap, unsigned int count)
static int OsStrSeparate(const char *tabStr, char *strPath, char *nameLooking, unsigned int tabStrLen)
unsigned int OsShellKeyInit(ShellCB *shellCB)
static int OsPrintMatchList(unsigned int count, const char *strPath, const char *nameLooking, unsigned int printLen)
void OsShellHistoryShow(unsigned int value, ShellCB *shellCB)
static int OsStrSeparateTabStrGet(const char **tabStr, CmdParsed *parsed, unsigned int tabStrLen)
void OsShellCmdPush(const char *string, CmdKeyLink *cmdKeyLink)
char * OsShellGetWorkingDirectory(void)
int OsTabCompletion(char *cmdKey, unsigned int *len)
static int OsShowPageInputControl(void)
void OsShellKeyDeInit(CmdKeyLink *cmdKeyLink)
unsigned int OsCmdKeyShift(const char *cmdKey, char *cmdOut, unsigned int size)
static void StrncmpCut(const char *s1, char *s2, size_t n)
static int OsExecNameMatch(const char *strPath, const char *nameLooking, char *strObj, unsigned int *maxLen)
void * malloc(size_t size)
动态分配内存块大小
void free(void *ptr)
释放ptr所指向的内存空间
int pthread_mutex_lock(pthread_mutex_t *mutex)
互斥锁加锁操作
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁互斥锁
static void SH_ListDelete(SH_List *node)
static void SH_ListInit(SH_List *list)
static void SH_ListTailInsert(SH_List *list, SH_List *node)
Insert a node to the tail of a doubly linked list.
static bool SH_ListEmpty(SH_List *list)
Identify whether a specified doubly linked list is empty.
char cmdString[0]
字符串,可变数组的一种实现方式.
char * paramArray[CMD_MAX_PARAS]
char shellBuf[SHOW_MAX_LEN]
接受shell命令 buf大小
void * cmdKeyLink
命令链表,所有敲过的命令链表
void * cmdHistoryKeyLink
命令的历史记录链表,去重,10个
unsigned int shellBufOffset
buf偏移量
pthread_mutex_t historyMutex
操作cmdHistoryKeyLink的互斥量
void * cmdMaskKeyLink
主要用于方向键上下遍历历史命令
char shellWorkingDirectory[PATH_MAX]
shell工作目录
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