diff --git a/src/PikaObj.c b/src/PikaObj.c index 3d163b7e2..86f0b9e22 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -671,85 +671,99 @@ static void __clearBuff(char* buff, int size) { } } +static void __obj_runChar(PikaObj* self, char inputChar){ + struct shell_config* cfg = args_getStruct(self->list, "__shcfg"); + __obj_shellLineHandler_t __lineHandler_fun = obj_getPtr(self, "__shhdl"); + /* create the line buff for the first time */ + if(!obj_isArgExist(self, "__shbuf")){ + obj_setBytes(self, "__shbuf" , NULL, PIKA_LINE_BUFF_SIZE); + /* print out the prefix when first entry */ + __platform_printf(cfg->prefix); + } + + char* rxBuff = (char*)obj_getBytes(self, "__shbuf"); + char* input_line = NULL; + uint8_t is_in_block = 0; +#ifndef __linux + __platform_printf("%c", inputChar); +#endif + if ((inputChar == '\b') || (inputChar == 127)) { + uint32_t size = strGetSize(rxBuff); + if (size == 0) { + __platform_printf(" "); + return; + } + __platform_printf(" \b"); + rxBuff[size - 1] = 0; + return; + } + if (inputChar != '\r' && inputChar != '\n') { + strAppendWithSize(rxBuff, &inputChar, 1); + return; + } + if ((inputChar == '\r') || (inputChar == '\n')) { +#ifndef __linux + __platform_printf("\r\n"); +#endif + /* still in block */ + if (is_in_block) { + /* load new line into buff */ + Args buffs = {0}; + char _n = '\n'; + strAppendWithSize(rxBuff, &_n, 1); + char* shell_buff_new = + strsAppend(&buffs, obj_getStr(self, "shell_buff"), rxBuff); + obj_setStr(self, "shell_buff", shell_buff_new); + strsDeinit(&buffs); + /* go out from block */ + if ((rxBuff[0] != ' ') && (rxBuff[0] != '\t')) { + is_in_block = 0; + input_line = obj_getStr(self, "shell_buff"); + if (SHELL_STATE_EXIT == + __lineHandler_fun(self, input_line)) { + return; + } + __platform_printf(">>> "); + } else { + __platform_printf("... "); + } + __clearBuff(rxBuff, PIKA_LINE_BUFF_SIZE); + return; + } + if (0 != strGetSize(rxBuff)) { + /* go in block */ + if (rxBuff[strGetSize(rxBuff) - 1] == ':') { + is_in_block = 1; + char _n = '\n'; + strAppendWithSize(rxBuff, &_n, 1); + obj_setStr(self, "shell_buff", rxBuff); + __clearBuff(rxBuff, PIKA_LINE_BUFF_SIZE); + __platform_printf("... "); + return; + } + } + input_line = rxBuff; + if (SHELL_STATE_EXIT == __lineHandler_fun(self, input_line)) { + return; + } + __platform_printf(cfg->prefix); + + __clearBuff(rxBuff, PIKA_LINE_BUFF_SIZE); + return; + } +} + void obj_shellLineProcess(PikaObj* self, __obj_shellLineHandler_t __lineHandler_fun, struct shell_config* cfg) { - Args buffs = {0}; - char* rxBuff = args_getBuff(&buffs, PIKA_LINE_BUFF_SIZE); - char* input_line = NULL; - uint8_t is_in_block = 0; - __platform_printf(cfg->prefix); + struct shell_config cfg_stack = {0}; + __platform_memcpy(&cfg_stack, cfg, sizeof(cfg_stack)); + args_setStruct(self->list, "__shcfg", cfg_stack); + obj_setPtr(self, "__shhdl", __lineHandler_fun); while (1) { char inputChar = __platform_getchar(); -#ifndef __linux - __platform_printf("%c", inputChar); -#endif - if ((inputChar == '\b') || (inputChar == 127)) { - uint32_t size = strGetSize(rxBuff); - if (size == 0) { - __platform_printf(" "); - continue; - } - __platform_printf(" \b"); - rxBuff[size - 1] = 0; - continue; - } - if (inputChar != '\r' && inputChar != '\n') { - strAppendWithSize(rxBuff, &inputChar, 1); - continue; - } - if ((inputChar == '\r') || (inputChar == '\n')) { -#ifndef __linux - __platform_printf("\r\n"); -#endif - /* still in block */ - if (is_in_block) { - /* load new line into buff */ - Args buffs = {0}; - char _n = '\n'; - strAppendWithSize(rxBuff, &_n, 1); - char* shell_buff_new = - strsAppend(&buffs, obj_getStr(self, "shell_buff"), rxBuff); - obj_setStr(self, "shell_buff", shell_buff_new); - strsDeinit(&buffs); - /* go out from block */ - if ((rxBuff[0] != ' ') && (rxBuff[0] != '\t')) { - is_in_block = 0; - input_line = obj_getStr(self, "shell_buff"); - if (SHELL_STATE_EXIT == - __lineHandler_fun(self, input_line)) { - break; - } - __platform_printf(">>> "); - } else { - __platform_printf("... "); - } - __clearBuff(rxBuff, PIKA_LINE_BUFF_SIZE); - continue; - } - if (0 != strGetSize(rxBuff)) { - /* go in block */ - if (rxBuff[strGetSize(rxBuff) - 1] == ':') { - is_in_block = 1; - char _n = '\n'; - strAppendWithSize(rxBuff, &_n, 1); - obj_setStr(self, "shell_buff", rxBuff); - __clearBuff(rxBuff, PIKA_LINE_BUFF_SIZE); - __platform_printf("... "); - continue; - } - } - input_line = rxBuff; - if (SHELL_STATE_EXIT == __lineHandler_fun(self, input_line)) { - break; - } - __platform_printf(cfg->prefix); - - __clearBuff(rxBuff, PIKA_LINE_BUFF_SIZE); - continue; - } + __obj_runChar(self, inputChar); } - strsDeinit(&buffs); } static enum shell_state __obj_shellLineHandler_debuger(PikaObj* self,