From 4081f1080e469054b366086a1b52e1f9ac307121 Mon Sep 17 00:00:00 2001 From: jukoo Date: Tue, 24 Feb 2026 13:22:05 +0000 Subject: [PATCH] interactive repl: checking for printable ascii char --- main.c | 18 +++++++----------- src/baseconv.c | 23 ++++++++++++++++------- src/baseconv.h | 4 +++- src/bcvrepl.c | 12 +++++++++++- src/bcvrepl.h | 16 ++++++++-------- 5 files changed, 45 insertions(+), 28 deletions(-) diff --git a/main.c b/main.c index 9178ed9..20b41a8 100644 --- a/main.c +++ b/main.c @@ -14,7 +14,8 @@ #include #include "baseconv.h" -#include "bcvrepl.h" + +#define NO_ARGS !(ac &~(1)) void bcv_usage(unsigned char const) ; /* [[noreturn]] */ @@ -23,16 +24,11 @@ int main(int ac , char **av) (void) setvbuf(stdout , (void *)0 , _IONBF ,0) ; //!NOTICE : when no argument was provided the repl is automaticaly launched - //if(bcv_launch_iteractive_repl_on(!(ac ~ &(1)))) - //goto _bcv_end ; - if (!(ac &~(1))) - { - //! launch interactive shell - (void) setlinebuf(stdin) ; - bcrepl_shell(__nptr); - goto _bcv_end; - } - + + if(bcv_launch_interactive_repl_on(NO_ARGS)) + goto _bcv_end ; + + //!NOTICE : long options are note handled ... //!NOTICE : probably -h and -v (help and version) //!TODO : make support for long option diff --git a/src/baseconv.c b/src/baseconv.c index 3d9c4c8..29dd33c 100644 --- a/src/baseconv.c +++ b/src/baseconv.c @@ -6,6 +6,8 @@ #include #include #include "baseconv.h" +#include "bcvrepl.h" + char bc_global_buffer[0xff]={0} ; uf64_t __allbase_enable__ = (BIN|CHR|OCT|HEX|429496729600); @@ -103,7 +105,8 @@ void bcv_guess_base(const char * restrict num) { uf64_t options = __allbase_enable__, - resolve_option=__allbase_enable__, + restore_option=__allbase_enable__, + excluded =0 , value = 0 ; char indicator = tolower(*(num+1) & 0xff ) ; @@ -132,14 +135,20 @@ void bcv_guess_base(const char * restrict num) goto _end; } - - uf64_t exclude= (__allbase_enable__ ^ options) ; - __allbase_enable__^=exclude; - bcv_print(value) ; - - __allbase_enable__ = resolve_option ; + + excluded= (__allbase_enable__ ^ options) ; + __allbase_enable__^=excluded; + bcv_print(value) ; + __allbase_enable__ = restore_option ; _end: return ; } +int bcv_launch_interactive_repl_on(int check_shell_argument) +{ + if(check_shell_argument) + bcrepl_shell(__nptr /*default prompt */) ; + + return 0; +} diff --git a/src/baseconv.h b/src/baseconv.h index 29911b6..b807226 100644 --- a/src/baseconv.h +++ b/src/baseconv.h @@ -56,6 +56,7 @@ }\ } +#define bcvtol(input , base) strtol(input , 00 , base) #define SHOWHEX(__expr, __it ,__out) \ SHOWBASE_EXPR(__expr ,__it, "0x","%c", __out) @@ -95,6 +96,7 @@ struct __bcb_t int _index ; }; +#include "bcvrepl.h" //!TODO : Use memory stream instead extern char bc_global_buffer[0xff] ; extern uf64_t __allbase_enable__; @@ -191,6 +193,6 @@ __BCX(char *) bc_dec(uf64_t value) ; __BCX(char *) bc_chr(uf64_t value) ; __BCX(void) bcv_print(uf64_t value) ; __BCX(void) bcv_guess_base(const char * rawinput) ; - +__BCX(int) bcv_launch_interactive_repl_on(int no_args) ; #endif //!__BASECONV diff --git a/src/bcvrepl.c b/src/bcvrepl.c index a8cc58a..da3f2cc 100644 --- a/src/bcvrepl.c +++ b/src/bcvrepl.c @@ -38,6 +38,7 @@ void bcrepl_shell(const char * prompt) bzero(prompt_buffer , bcrepl_buffer_limit) ; fflush(stdin) ; } + exit(EXIT_SUCCESS) ; } void bcrepl_customize(user_prompt_custom_shell custom_prompt , const char * prompt) @@ -63,6 +64,15 @@ char * bcrepl_token_search(const char * bcrpl_inline_buffer) } +static uf64_t bcrepl_analyse_braw(const char * raw_buffer) +{ + uf64_t value = strtol(raw_buffer , 00 , 10); + if (!value && is_printable(raw_buffer)) + value = *raw_buffer & 0xff ; + + return value ; +} + void bcrepl_compute(const char * buffer) { @@ -78,7 +88,7 @@ void bcrepl_compute(const char * buffer) vtok = bcrepl_process(buffer_clone , *(should_be_tokinezed) & 0xff); else { - value = strtol(buffer_clone , 00 , 10) ; + value = bcrepl_analyse_braw(buffer_clone); value ? bcv_print(value) : bcv_guess_base(buffer_clone) ; bcrepl_show_helper(buffer_clone) ; diff --git a/src/bcvrepl.h b/src/bcvrepl.h index b08c5f7..97a2bd0 100644 --- a/src/bcvrepl.h +++ b/src/bcvrepl.h @@ -10,8 +10,8 @@ # define bcvrepl_export #endif -#include "bcv_conf.h" -#include "baseconv.h" +#include "bcv_conf.h" +#include "baseconv.h" #if defined(__linux__) #include @@ -37,6 +37,8 @@ static inline __attribute__((constructor)) void init_tty(void) #define GREEN __SETAF(COLOR_GREEN) #define RED __SETAF(COLOR_RED) #define YELLOW __SETAF(COLOR_YELLOW) +#define INFO __SETAF(COLOR_CYAN) + #define __reset tputs(reset, 1, putchar ) #define apply(__statement , __color_attr) \ @@ -77,15 +79,14 @@ extern char * program_invocation_short_name ; #endif - typedef typeof(void(const char * __restrict__)) * user_prompt_custom_shell ; /* @fn bcrepl_shell(const char * _Nullable) * @brief define the shell prompt * @param const char * - if Null the default shell prompt is used * */ -bcvrepl_export void -bcrepl_shell(const char * __prompt) ; +bcvrepl_export void __attribute__((noreturn)) +bcrepl_shell(const char * __prompt) ; bcvrepl_export void @@ -112,10 +113,9 @@ bcrepl_listen_special_cmd(const char *__buffer) ; bcvrepl_export void __trimlower(char *__cmd); -bcvrepl_export char * bcrepl_token_search(const char * __restrict__) ; +static uf64_t bcrepl_analyse_braw(const char * __restrict__ raw_input_buffer) ; +bcvrepl_export char * bcrepl_token_search(const char * __restrict__) ; bcvrepl_export uf64_t bcrepl_process(const char * buffer , char founded_token) ; - - static void bcrepl_show_helper(const char buffer [ static 1 ]) ; #endif //!bcv_repl_h