@@ -671,19 +671,21 @@ static void __clearBuff(char* buff, int size) {
671671 }
672672}
673673
674- static enum shell_state __obj_runChar (PikaObj * self , char inputChar ) {
674+ static void __obj_runCharBeforeRun (PikaObj * self ) {
675675 struct shell_config * cfg = args_getStruct (self -> list , "__shcfg" );
676- __obj_shellLineHandler_t __lineHandler_fun = obj_getPtr (self , "__shhdl" );
677676 /* create the line buff for the first time */
678- if (! obj_isArgExist ( self , "__shbuf" )){
679- obj_setBytes (self , "__shbuf" , NULL , PIKA_LINE_BUFF_SIZE );
680- /* print out the prefix when first entry */
681- __platform_printf (cfg -> prefix );
682- }
677+ obj_setBytes ( self , "__shbuf" , NULL , PIKA_LINE_BUFF_SIZE );
678+ obj_setInt (self , "__shinb" , 0 );
679+ /* print out the prefix when first entry */
680+ __platform_printf (cfg -> prefix );
681+ }
683682
683+ enum shell_state obj_runChar (PikaObj * self , char inputChar ) {
684+ struct shell_config * cfg = args_getStruct (self -> list , "__shcfg" );
685+ __obj_shellLineHandler_t __lineHandler_fun = obj_getPtr (self , "__shhdl" );
684686 char * rxBuff = (char * )obj_getBytes (self , "__shbuf" );
685687 char * input_line = NULL ;
686- uint8_t is_in_block = 0 ;
688+ int is_in_block = obj_getInt ( self , "__shinb" ) ;
687689#ifndef __linux
688690 __platform_printf ("%c" , inputChar );
689691#endif
@@ -717,10 +719,11 @@ static enum shell_state __obj_runChar(PikaObj* self, char inputChar){
717719 strsDeinit (& buffs );
718720 /* go out from block */
719721 if ((rxBuff [0 ] != ' ' ) && (rxBuff [0 ] != '\t' )) {
720- is_in_block = 0 ;
722+ obj_setInt ( self , "__shinb" , 0 ) ;
721723 input_line = obj_getStr (self , "shell_buff" );
722- return __lineHandler_fun (self , input_line );
724+ enum shell_state state = __lineHandler_fun (self , input_line );
723725 __platform_printf (">>> " );
726+ return state ;
724727 } else {
725728 __platform_printf ("... " );
726729 }
@@ -730,7 +733,7 @@ static enum shell_state __obj_runChar(PikaObj* self, char inputChar){
730733 if (0 != strGetSize (rxBuff )) {
731734 /* go in block */
732735 if (rxBuff [strGetSize (rxBuff ) - 1 ] == ':' ) {
733- is_in_block = 1 ;
736+ obj_setInt ( self , "__shinb" , 1 ) ;
734737 char _n = '\n' ;
735738 strAppendWithSize (rxBuff , & _n , 1 );
736739 obj_setStr (self , "shell_buff" , rxBuff );
@@ -740,32 +743,43 @@ static enum shell_state __obj_runChar(PikaObj* self, char inputChar){
740743 }
741744 }
742745 input_line = rxBuff ;
743- return __lineHandler_fun (self , input_line );
746+ enum shell_state state = __lineHandler_fun (self , input_line );
744747 __platform_printf (cfg -> prefix );
745-
746748 __clearBuff (rxBuff , PIKA_LINE_BUFF_SIZE );
747- return SHELL_STATE_CONTINUE ;
749+ return state ;
748750 }
749751 return SHELL_STATE_CONTINUE ;
750752}
751753
752- void obj_shellLineProcess (PikaObj * self ,
753- __obj_shellLineHandler_t __lineHandler_fun ,
754- struct shell_config * cfg ) {
754+ static void obj_shellConfig (PikaObj * self ,
755+ __obj_shellLineHandler_t __lineHandler_fun ,
756+ struct shell_config * cfg ) {
755757 struct shell_config cfg_stack = {0 };
756758 __platform_memcpy (& cfg_stack , cfg , sizeof (cfg_stack ));
757759 args_setStruct (self -> list , "__shcfg" , cfg_stack );
758760 obj_setPtr (self , "__shhdl" , __lineHandler_fun );
761+ }
762+
763+ void obj_shellLineProcess (PikaObj * self ,
764+ __obj_shellLineHandler_t __lineHandler_fun ,
765+ struct shell_config * cfg ) {
766+ /* config the shell */
767+ obj_shellConfig (self , __lineHandler_fun , cfg );
768+
769+ /* init the shell */
770+ __obj_runCharBeforeRun (self );
771+
772+ /* getchar and run */
759773 while (1 ) {
760774 char inputChar = __platform_getchar ();
761- if (SHELL_STATE_EXIT == __obj_runChar (self , inputChar )){
775+ if (SHELL_STATE_EXIT == obj_runChar (self , inputChar )) {
762776 break ;
763777 }
764778 }
765779}
766780
767- static enum shell_state __obj_shellLineHandler_debuger (PikaObj * self ,
768- char * input_line ) {
781+ static enum shell_state __obj_shellLineHandler_REPL (PikaObj * self ,
782+ char * input_line ) {
769783 /* exit */
770784 if (strEqu ("exit()" , input_line )) {
771785 /* exit pika shell */
@@ -776,11 +790,20 @@ static enum shell_state __obj_shellLineHandler_debuger(PikaObj* self,
776790 return SHELL_STATE_CONTINUE ;
777791}
778792
793+ void obj_runCharInit (PikaObj * self ) {
794+ struct shell_config cfg = {
795+ .prefix = ">>> " ,
796+ };
797+ obj_shellConfig (self , __obj_shellLineHandler_REPL , & cfg );
798+ /* init the shell */
799+ __obj_runCharBeforeRun (self );
800+ }
801+
779802void pikaScriptShell (PikaObj * self ) {
780803 struct shell_config cfg = {
781804 .prefix = ">>> " ,
782805 };
783- obj_shellLineProcess (self , __obj_shellLineHandler_debuger , & cfg );
806+ obj_shellLineProcess (self , __obj_shellLineHandler_REPL , & cfg );
784807}
785808
786809void obj_setErrorCode (PikaObj * self , int32_t errCode ) {
0 commit comments