From 26a15215cfad5d0e6f3ec2c9dc8b441fa34d1dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20DEL=20NERO?= Date: Fri, 17 Nov 2023 23:43:02 +0100 Subject: [PATCH] env API modified. --- lib_jtag_core/src/jtag_core.c | 14 ++- lib_jtag_core/src/jtag_core_internal.h | 1 - lib_jtag_core/src/script/env.c | 136 +++++++++++++------------ lib_jtag_core/src/script/env.h | 18 ++-- lib_jtag_core/src/script/script.c | 22 ++-- 5 files changed, 97 insertions(+), 94 deletions(-) diff --git a/lib_jtag_core/src/jtag_core.c b/lib_jtag_core/src/jtag_core.c index 6c5a91f..e4052c7 100644 --- a/lib_jtag_core/src/jtag_core.c +++ b/lib_jtag_core/src/jtag_core.c @@ -52,6 +52,8 @@ jtag_core * jtagcore_init() { memset( jc, 0, sizeof(jtag_core) ); + jc->envvar = (void*)initEnv(NULL, NULL); + jtagcore_setEnvVar( jc, "LIBVERSION", "v"LIB_JTAG_CORE_VERSION); sctx = jtagcore_initScript(jc); @@ -1061,13 +1063,8 @@ int jtagcore_select_and_open_probe(jtag_core * jc, int probe_id) int jtagcore_setEnvVar( jtag_core * jc, char * varname, char * varvalue ) { - envvar_entry * tmp_env; - - tmp_env = setEnvVar( jc->envvar, varname, varvalue ); - - if( tmp_env ) + if( setEnvVarDat( jc->envvar, varname, varvalue ) >= 0 ) { - jc->envvar = tmp_env; return JTAG_CORE_NO_ERROR; } else @@ -1078,12 +1075,12 @@ int jtagcore_setEnvVar( jtag_core * jc, char * varname, char * varvalue ) char * jtagcore_getEnvVar( jtag_core * jc, char * varname, char * varvalue) { - return getEnvVar( jc->envvar, varname, varvalue); + return getEnvVarDat( jc->envvar, varname, varvalue, 512 ); } char * jtagcore_getEnvVarIndex( jtag_core * jc, int index, char * varvalue) { - return getEnvVarIndex( jc->envvar, index, varvalue); + return getEnvVarDatIndex( jc->envvar, index, varvalue, 512 ); } int jtagcore_getEnvVarValue( jtag_core * jc, char * varname) @@ -1095,6 +1092,7 @@ void jtagcore_deinit(jtag_core * jc) { if( jc ) { + deinitEnv( (envvar_entry *)jc->envvar ); free( jc ); } } diff --git a/lib_jtag_core/src/jtag_core_internal.h b/lib_jtag_core/src/jtag_core_internal.h index 1a72bd4..9cd9326 100644 --- a/lib_jtag_core/src/jtag_core_internal.h +++ b/lib_jtag_core/src/jtag_core_internal.h @@ -60,7 +60,6 @@ typedef struct _jtag_core jtag_device devices_list[MAX_NB_JTAG_DEVICE]; int IR_filled; - // I2C over JTAG int i2c_sda_device; int i2c_scl_device; diff --git a/lib_jtag_core/src/script/env.c b/lib_jtag_core/src/script/env.c index 0f6d245..01a543e 100644 --- a/lib_jtag_core/src/script/env.c +++ b/lib_jtag_core/src/script/env.c @@ -37,10 +37,6 @@ if L & H == 0 -> end of buffer */ -#ifdef STATIC_ENV_BUFFER -static envvar_entry static_envvar; -#endif - static int stringcopy(char * dst, char * src, unsigned int maxsize) { int s; @@ -269,20 +265,25 @@ static int pushEnvEntry(envvar_entry * env, char * varname, char * vardata) return -1; } -envvar_entry * setEnvVar( envvar_entry * env, char * varname, char * vardata ) +int setEnvVarDat( envvar_entry * env, char * varname, char * vardata ) { int i,off,ret; unsigned short varname_size, vardata_size; int varname_len, vardata_len; int oldentrysize; + ret = 0; + + if(!env) + return -1; + i = 0; varname_len = 0; vardata_len = 0; if( !varname ) - return env; + return -1; varname_len = strlen(varname); @@ -291,32 +292,7 @@ envvar_entry * setEnvVar( envvar_entry * env, char * varname, char * vardata ) if( varname_len > ENV_MAX_STRING_SIZE || vardata_len > ENV_MAX_STRING_SIZE) { - return env; - } - - if(!env) - { -#ifdef STATIC_ENV_BUFFER - memset(&static_envvar,0,ENV_PAGE_SIZE); - static_envvar.bufsize = ENV_PAGE_SIZE; - - env = &static_envvar; -#else - env = malloc( sizeof(envvar_entry) ); - if(!env) - return NULL; - - memset( env,0,sizeof(envvar_entry)); - - env->bufsize = ENV_PAGE_SIZE; - env->buf = malloc(env->bufsize); - if(!env->buf) - { - free(env); - return NULL; - } - memset(env->buf,0,env->bufsize); -#endif + return -1; } off = getEnvBufOff( env, varname ); @@ -331,7 +307,7 @@ envvar_entry * setEnvVar( envvar_entry * env, char * varname, char * vardata ) { vardata_len = strlen(vardata); if( vardata_len + 1 > 0xFFFF ) - return env; + return -1; if( vardata_len + 1 > vardata_size ) { @@ -379,10 +355,10 @@ envvar_entry * setEnvVar( envvar_entry * env, char * varname, char * vardata ) } } - return env; + return ret; } -char * getEnvVar( envvar_entry * env, char * varname, char * vardata) +char * getEnvVarDat( envvar_entry * env, char * varname, char * vardata, int maxsize) { int off; unsigned short varname_size, vardata_size; @@ -403,7 +379,7 @@ char * getEnvVar( envvar_entry * env, char * varname, char * vardata) { if(vardata) { - stringcopy(vardata, (char*)&env->buf[off + 2 + varname_size + 2], ENV_MAX_STRING_SIZE); + stringcopy(vardata, (char*)&env->buf[off + 2 + varname_size + 2], maxsize); } return (char*)&env->buf[off + 2 + varname_size + 2]; @@ -423,7 +399,7 @@ env_var_value getEnvVarValue( envvar_entry * env, char * varname) if(!varname) return 0; - str_return = getEnvVar( env, varname, NULL); + str_return = getEnvVarDat( env, varname, NULL, 0); if(str_return) { @@ -447,7 +423,7 @@ env_var_value getEnvVarValue( envvar_entry * env, char * varname) return value; } -envvar_entry * setEnvVarValue( envvar_entry * env, char * varname, env_var_value value) +int setEnvVarValue( envvar_entry * env, char * varname, env_var_value value) { char tmp_str[128]; @@ -455,10 +431,10 @@ envvar_entry * setEnvVarValue( envvar_entry * env, char * varname, env_var_value snprintf(tmp_str,sizeof(tmp_str) - 1, "%d",value); - return setEnvVar( env, varname, tmp_str ); + return setEnvVarDat( env, varname, tmp_str ); } -char * getEnvVarIndex( envvar_entry * env, int index, char * vardata) +char * getEnvVarDatIndex( envvar_entry * env, int index, char * vardata, int maxsize) { int str_index, off; unsigned short varname_size, vardata_size; @@ -478,7 +454,7 @@ char * getEnvVarIndex( envvar_entry * env, int index, char * vardata) { if(vardata) { - stringcopy(vardata,(char*)&env->buf[off + 2 + varname_size + 2], ENV_MAX_STRING_SIZE); + stringcopy(vardata,(char*)&env->buf[off + 2 + varname_size + 2], maxsize); } return (char*)&env->buf[off + 2 + varname_size + 2]; @@ -496,48 +472,78 @@ char * getEnvVarIndex( envvar_entry * env, int index, char * vardata) return NULL; // Not found. } -envvar_entry * duplicate_env_vars(envvar_entry * src) +envvar_entry * initEnv(envvar_entry * env, envvar_entry * dst) { -#ifndef STATIC_ENV_BUFFER - envvar_entry * tmp_envvars; -#endif - - if(!src) + if(!env) + { +#ifdef STATIC_ENV_BUFFER return NULL; +#else + env = malloc( sizeof(envvar_entry) ); + if(!env) + return NULL; - if(!src->buf) - return NULL; + memset( env,0,sizeof(envvar_entry)); -#ifndef STATIC_ENV_BUFFER - tmp_envvars = malloc(sizeof(envvar_entry)); - if(tmp_envvars) - { - memset(tmp_envvars,0,sizeof(envvar_entry)); - tmp_envvars->buf = malloc(src->bufsize); - if(!tmp_envvars->buf) + env->bufsize = ENV_PAGE_SIZE; + env->buf = malloc(env->bufsize); + if(!env->buf) { - free(tmp_envvars); + free(env); return NULL; } + memset(env->buf,0,env->bufsize); - memcpy(tmp_envvars->buf,src->buf,src->bufsize); - - return tmp_envvars; + return env; +#endif } + else + { +#ifdef STATIC_ENV_BUFFER + if(!dst) + return NULL; + + memset(&dst,0,sizeof(envvar_entry)); + dst->bufsize = ENV_PAGE_SIZE; +#else + envvar_entry * tmp_envvars; + + if(!env->buf) + return NULL; + + tmp_envvars = malloc(sizeof(envvar_entry)); + if(tmp_envvars) + { + memset(tmp_envvars,0,sizeof(envvar_entry)); + tmp_envvars->buf = malloc(env->bufsize); + if(!tmp_envvars->buf) + { + free(tmp_envvars); + return NULL; + } + + memcpy(tmp_envvars->buf,env->buf,env->bufsize); + + return tmp_envvars; + } #endif + } + return NULL; } -void free_env_vars(envvar_entry * src) +void deinitEnv(envvar_entry * env) { #ifndef STATIC_ENV_BUFFER - if(!src) + if(!env) return; - if(src->buf) - free(src->buf); + if(env->buf) + free(env->buf); - free(src); + free(env); +#else + memset(env,0,sizeof(envvar_entry)); #endif return; } diff --git a/lib_jtag_core/src/script/env.h b/lib_jtag_core/src/script/env.h index ee176f4..f910603 100644 --- a/lib_jtag_core/src/script/env.h +++ b/lib_jtag_core/src/script/env.h @@ -47,10 +47,14 @@ typedef struct envvar_entry_ unsigned int bufsize; }envvar_entry; -envvar_entry * setEnvVar( envvar_entry * env, char * varname, char * vardata); -char * getEnvVar( envvar_entry * env, char * varname, char * vardata); -env_var_value getEnvVarValue( envvar_entry * env, char * varname); -envvar_entry * setEnvVarValue( envvar_entry * env, char * varname, env_var_value value); -char * getEnvVarIndex( envvar_entry * env, int index, char * vardata); -envvar_entry * duplicate_env_vars(envvar_entry * src); -void free_env_vars(envvar_entry * src); +envvar_entry * initEnv( envvar_entry * env, envvar_entry * dst ); + +int setEnvVarDat( envvar_entry * env, char * varname, char * vardata ); +int setEnvVarValue( envvar_entry * env, char * varname, env_var_value value ); + +char * getEnvVarDat( envvar_entry * env, char * varname, char * vardata, int maxsize ); +env_var_value getEnvVarValue( envvar_entry * env, char * varname ); + +char * getEnvVarDatIndex( envvar_entry * env, int index, char * vardata, int maxsize ); + +void deinitEnv( envvar_entry * env ); diff --git a/lib_jtag_core/src/script/script.c b/lib_jtag_core/src/script/script.c index 48cd736..aceabad 100644 --- a/lib_jtag_core/src/script/script.c +++ b/lib_jtag_core/src/script/script.c @@ -225,7 +225,7 @@ static int get_param( script_ctx * ctx, char * line, int param_offset,char * par return 1; } - if( !getEnvVar( *((envvar_entry **)ctx->env), (char*)&var_str[1], param) ) + if( !getEnvVarDat( (envvar_entry *)ctx->env, (char*)&var_str[1], param, DEFAULT_BUFLEN) ) { copy_param(param, line, offs); } @@ -301,7 +301,7 @@ static env_var_value get_script_variable( script_ctx * ctx, char * varname) } if(varname[0] == '$') - value = getEnvVarValue( *((envvar_entry **)ctx->env), (char*)&varname[1]); + value = getEnvVarValue( (envvar_entry *)ctx->env, (char*)&varname[1]); else value = str_to_int((char*)varname); @@ -336,7 +336,7 @@ static void set_script_variable( script_ctx * ctx, char * varname, env_var_value if(varname[0] == '$' && varname[1]) { sprintf(tmp_str,"0x"LONGHEXSTR,value); - *((envvar_entry **)ctx->env) = (void *)setEnvVar( *((envvar_entry **)ctx->env), (char*)&varname[1], tmp_str ); + setEnvVarDat( (envvar_entry *)ctx->env, (char*)&varname[1], tmp_str ); return; } @@ -347,12 +347,11 @@ script_ctx * init_script(void * app_ctx, unsigned int flags, void * env) script_ctx * ctx; ctx = malloc(sizeof(script_ctx)); - if(ctx) { memset(ctx,0,sizeof(script_ctx)); - ctx->env = ((envvar_entry**)env); + ctx->env = env; setOutputFunc_script( ctx, dummy_script_printf ); @@ -942,7 +941,7 @@ static int cmd_print_env_var( script_ctx * ctx, char * line ) if(i>=0) { - ptr = getEnvVar( *((envvar_entry **)ctx->env), (char*)&varname, (char*)&varvalue ); + ptr = getEnvVarDat( (envvar_entry *)ctx->env, (char*)&varname, (char*)&varvalue, sizeof(varvalue) ); if(ptr) { ctx->script_printf( ctx, MSG_INFO_1, "%s = %s", varname, varvalue ); @@ -997,7 +996,7 @@ static int cmd_print( script_ctx * ctx, char * line) } else { - ptr = getEnvVar( *((envvar_entry **)ctx->env), &tmp_str[1], NULL); + ptr = getEnvVarDat( (envvar_entry *)ctx->env, &tmp_str[1], NULL, 0); if( ptr ) { genos_strndstcat((char*)str,ptr,sizeof(str)); @@ -1147,7 +1146,6 @@ static int cmd_set_env_var( script_ctx * ctx, char * line ) int i,j,ret; char varname[DEFAULT_BUFLEN]; char varvalue[DEFAULT_BUFLEN]; - envvar_entry * tmp_env; ret = JTAG_CORE_BAD_PARAMETER; @@ -1156,10 +1154,8 @@ static int cmd_set_env_var( script_ctx * ctx, char * line ) if(i>=0 && j>=0) { - tmp_env = setEnvVar( *((envvar_entry **)ctx->env), (char*)&varname, (char*)&varvalue ); - if(tmp_env) + if( setEnvVarDat( (envvar_entry *)ctx->env, (char*)&varname, (char*)&varvalue ) >= 0 ) { - *((envvar_entry **)ctx->env) = tmp_env; ret = JTAG_CORE_NO_ERROR; } else @@ -1297,7 +1293,7 @@ static int cmd_initarray( script_ctx * ctx, char * line) if(size >= 0 && strlen(varname) ) { - ptr = getEnvVar( *((envvar_entry **)ctx->env),(char*)&varname, NULL); + ptr = getEnvVarDat( (envvar_entry *)ctx->env,(char*)&varname, NULL, 0); if(ptr) { arrayresize(ptr, size, (unsigned char)(str_to_int((char*)&varvalue)&0xFF)); @@ -1313,7 +1309,7 @@ static int cmd_initarray( script_ctx * ctx, char * line) } if(ptr) - *((envvar_entry **)ctx->env) = (void *)setEnvVar( *((envvar_entry **)ctx->env), (char*)&varname[1], ptr ); + setEnvVarDat( (envvar_entry *)ctx->env, (char*)&varname[1], ptr ); } ret = JTAG_CORE_NO_ERROR; }