From cee1b1bda213e14b6b29e9c2a8f69f4a5ccf26d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnim=20L=C3=A4uger?= Date: Wed, 2 Apr 2008 09:18:47 +0000 Subject: [PATCH] remove global variables for bsdl subsystem [ 1929235 ] Use less global and static variables git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1148 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- jtag/ChangeLog | 11 +++++ jtag/include/bsdl.h | 16 +++++-- jtag/include/chain.h | 2 + jtag/src/bsdl/Makefile.am | 2 +- jtag/src/bsdl/bsdl.c | 66 +++++++++++++------------- jtag/src/bsdl/bsdl_bison.y | 2 +- jtag/src/bsdl/bsdl_flex.l | 2 +- jtag/src/bsdl/{bsdl.h => bsdl_local.h} | 5 ++ jtag/src/bsdl/bsdl_sem.c | 2 +- jtag/src/cmd/bsdl.c | 18 +++---- jtag/src/tap/chain.c | 3 ++ 11 files changed, 79 insertions(+), 50 deletions(-) rename jtag/src/bsdl/{bsdl.h => bsdl_local.h} (98%) diff --git a/jtag/ChangeLog b/jtag/ChangeLog index 42bbd577..681f01cf 100644 --- a/jtag/ChangeLog +++ b/jtag/ChangeLog @@ -1,3 +1,14 @@ +2008-04-02 Arnim Laeuger + + [ 1929235 ] Use less global and static variables: + * src/bsdl/bsdl.c, src/cmd/bsdl.c: use globals from chain_t + * src/tap/chain.c (chain_alloc): init BSDL globals + * include/chain.h: added bsdl_globs_t to chain_t + * include/bsdl.h: defined structure for BSDL globals + * src/bsdl/Makefile.am, src/bsdl/bsdl_sem.c, src/bsdl/bsdl_flex.l, + src/bsdl/bsdl_bison.y, src/bsdl/bsdl.c, src/bsdl/bsdl_local.h: + renamed bsdl.h to bsdl_local.h + 2008-04-01 Arnim Laeuger [ 1887221 ] Add configure switch to disable all cable&parport drivers: diff --git a/jtag/include/bsdl.h b/jtag/include/bsdl.h index 2e0193bc..1f7d037c 100644 --- a/jtag/include/bsdl.h +++ b/jtag/include/bsdl.h @@ -25,12 +25,20 @@ #ifndef BSDL_H #define BSDL_H -#include "chain.h" +typedef struct { + char **path_list; + int debug; +} bsdl_globs_t; + +#define BSDL_GLOBS_INIT(bsdl) {\ + bsdl.path_list = NULL;\ + bsdl.debug = 0;\ +} -extern int bsdl_debug; +#include "chain.h" -int bsdl_read_file(chain_t *, const char *, int, char *); -void bsdl_set_path(const char *); +int bsdl_read_file(chain_t *, const char *, int, const char *); +void bsdl_set_path(chain_t *, const char *); int bsdl_scan_files(chain_t *, const char *, int); #endif /* BSDL_H */ diff --git a/jtag/include/chain.h b/jtag/include/chain.h index 280edaac..9c46d905 100644 --- a/jtag/include/chain.h +++ b/jtag/include/chain.h @@ -30,6 +30,7 @@ typedef struct chain_t chain_t; #include "cable.h" +#include "bsdl.h" #define EXITMODE_SHIFT 0 #define EXITMODE_IDLE 1 @@ -40,6 +41,7 @@ struct chain_t { parts_t *parts; int active_part; cable_t *cable; + bsdl_globs_t bsdl; }; chain_t *chain_alloc( void ); diff --git a/jtag/src/bsdl/Makefile.am b/jtag/src/bsdl/Makefile.am index 51f98c47..8d4dfe86 100644 --- a/jtag/src/bsdl/Makefile.am +++ b/jtag/src/bsdl/Makefile.am @@ -24,7 +24,7 @@ include $(top_srcdir)/Makefile.rules noinst_LIBRARIES = libbsdl.a noinst_HEADERS = \ - bsdl.h \ + bsdl_local.h \ bsdl_bison.h \ bsdl_sysdep.h diff --git a/jtag/src/bsdl/bsdl.c b/jtag/src/bsdl/bsdl.c index 7cc4aa74..062603a8 100644 --- a/jtag/src/bsdl/bsdl.c +++ b/jtag/src/bsdl/bsdl.c @@ -33,15 +33,10 @@ #include "sysdep.h" -#include -#include +#include "jtag.h" +#include "cmd.h" -#include "bsdl.h" - - -static char **bsdl_path_list = NULL; - -int bsdl_debug = 0; +#include "bsdl_local.h" void bsdl_msg(int type, const char *format, ...) @@ -89,6 +84,7 @@ void bsdl_msg(int type, const char *format, ...) ****************************************************************************/ int bsdl_read_file(chain_t *chain, const char *BSDL_File_Name, int mode, const char *idcode) { + bsdl_globs_t *globs = &(chain->bsdl); FILE *BSDL_File; parser_priv_t *parser_priv; int Compile_Errors = 1; @@ -96,7 +92,7 @@ int bsdl_read_file(chain_t *chain, const char *BSDL_File_Name, int mode, const c BSDL_File = fopen(BSDL_File_Name, "r"); - if (bsdl_debug || (mode == 0)) + if (globs->debug || (mode == 0)) bsdl_msg(BSDL_MSG_NOTE, _("Reading file '%s'\n"), BSDL_File_Name); if (BSDL_File == NULL) { @@ -104,7 +100,7 @@ int bsdl_read_file(chain_t *chain, const char *BSDL_File_Name, int mode, const c return -1; } - if ((parser_priv = bsdl_parser_init(BSDL_File, mode, bsdl_debug))) { + if ((parser_priv = bsdl_parser_init(BSDL_File, mode, globs->debug))) { if (mode >= 0) { if (mode >= 1) { if (chain == NULL) { @@ -141,10 +137,10 @@ int bsdl_read_file(chain_t *chain, const char *BSDL_File_Name, int mode, const c Compile_Errors = bsdl_flex_get_compile_errors(parser_priv->scanner); if (Compile_Errors == 0) { - if (bsdl_debug) + if (globs->debug) bsdl_msg(BSDL_MSG_NOTE, _("BSDL file '%s' compiled correctly\n"), BSDL_File_Name); } else { - if (bsdl_debug || (mode >= 0)) + if (globs->debug || (mode >= 0)) bsdl_msg(BSDL_MSG_ERR, _("BSDL file '%s' contains errors, stopping\n"), BSDL_File_Name); } @@ -152,7 +148,7 @@ int bsdl_read_file(chain_t *chain, const char *BSDL_File_Name, int mode, const c bsdl_ac_finalize(parser_priv); if ((Compile_Errors == 0) && parser_priv->jtag_ctrl.idcode) { - if (bsdl_debug) + if (globs->debug) bsdl_msg(BSDL_MSG_NOTE, _("Got IDCODE: %s\n"), parser_priv->jtag_ctrl.idcode); /* should be compare the idcodes? */ @@ -168,7 +164,7 @@ int bsdl_read_file(chain_t *chain, const char *BSDL_File_Name, int mode, const c if (idcode[idx] != parser_priv->jtag_ctrl.idcode[idx]) idcode_match = 0; - if (bsdl_debug) { + if (globs->debug) { if (idcode_match) bsdl_msg(BSDL_MSG_NOTE, _("IDCODE matched\n") ); else @@ -192,24 +188,25 @@ int bsdl_read_file(chain_t *chain, const char *BSDL_File_Name, int mode, const c * void bsdl_set_path(const char *pathlist) * * Dissects pathlist and enters its elements to the global variable - * bsdl_path_list. + * bsdl.path_list. * * Return value: * void ****************************************************************************/ -void bsdl_set_path(const char *pathlist) +void bsdl_set_path(chain_t *chain, const char *pathlist) { + bsdl_globs_t *globs = &(chain->bsdl); char *delim; char *elem; int num; /* free memory of current path list */ - if (bsdl_path_list) { - for (num = 0; bsdl_path_list[num]; num++) - if (bsdl_path_list[num]) - free(bsdl_path_list[num]); - free(bsdl_path_list); - bsdl_path_list = NULL; + if (globs->path_list) { + for (num = 0; globs->path_list[num]; num++) + if (globs->path_list[num]) + free(globs->path_list[num]); + free(globs->path_list); + globs->path_list = NULL; } /* run through path list and determine number of elements */ @@ -218,17 +215,17 @@ void bsdl_set_path(const char *pathlist) if ((delim - elem > 0) || (delim == NULL)) { num++; /* extend path list array */ - bsdl_path_list = (char **)realloc(bsdl_path_list, (num+1) * sizeof(char *)); + globs->path_list = (char **)realloc(globs->path_list, (num+1) * sizeof(char *)); /* enter path element up to the delimeter */ - bsdl_path_list[num-1] = strndup(elem, (size_t)(delim - elem)); - bsdl_path_list[num] = NULL; + globs->path_list[num-1] = strndup(elem, (size_t)(delim - elem)); + globs->path_list[num] = NULL; } elem = delim ? delim + 1 : elem + strlen(elem); } - if (bsdl_debug) - for (num = 0; bsdl_path_list[num] != NULL; num++) { - bsdl_msg(BSDL_MSG_NOTE, "%s\n", bsdl_path_list[num]); + if (globs->debug) + for (num = 0; globs->path_list[num] != NULL; num++) { + bsdl_msg(BSDL_MSG_NOTE, "%s\n", globs->path_list[num]); } } @@ -257,28 +254,29 @@ void bsdl_set_path(const char *pathlist) ****************************************************************************/ int bsdl_scan_files(chain_t *chain, const char *idcode, int mode) { + bsdl_globs_t *globs = &(chain->bsdl); int idx = 0; int result = 0; /* abort if no path list was specified */ - if (bsdl_path_list == NULL) + if (globs->path_list == NULL) return 0; - while (bsdl_path_list[idx] && (result <= 0)) { + while (globs->path_list[idx] && (result <= 0)) { DIR *dir; - if ((dir = opendir(bsdl_path_list[idx]))) { + if ((dir = opendir(globs->path_list[idx]))) { struct dirent *elem; /* run through all elements in the current directory */ while ((elem = readdir(dir)) && (result <= 0)) { char *name; - name = (char *)malloc(strlen(bsdl_path_list[idx]) + strlen(elem->d_name) + 1 + 1); + name = (char *)malloc(strlen(globs->path_list[idx]) + strlen(elem->d_name) + 1 + 1); if (name) { struct stat buf; - strcpy(name, bsdl_path_list[idx]); + strcpy(name, globs->path_list[idx]); strcat(name, "/"); strcat(name, elem->d_name); @@ -304,7 +302,7 @@ int bsdl_scan_files(chain_t *chain, const char *idcode, int mode) closedir(dir); } else - bsdl_msg(BSDL_MSG_WARN, _("Cannot open directory %s\n"), bsdl_path_list[idx]); + bsdl_msg(BSDL_MSG_WARN, _("Cannot open directory %s\n"), globs->path_list[idx]); idx++; } diff --git a/jtag/src/bsdl/bsdl_bison.y b/jtag/src/bsdl/bsdl_bison.y index 201ff550..9eeb4b10 100644 --- a/jtag/src/bsdl/bsdl_bison.y +++ b/jtag/src/bsdl/bsdl_bison.y @@ -132,7 +132,7 @@ LEGAL NOTICES: /* interface to flex */ #include "bsdl_bison.h" -#include "bsdl.h" +#include "bsdl_local.h" #define YYLEX_PARAM priv_data->scanner int yylex (YYSTYPE *, void *); diff --git a/jtag/src/bsdl/bsdl_flex.l b/jtag/src/bsdl/bsdl_flex.l index 9ccaa33f..7feb387e 100644 --- a/jtag/src/bsdl/bsdl_flex.l +++ b/jtag/src/bsdl/bsdl_flex.l @@ -130,7 +130,7 @@ LEGAL NOTICES: #include "bsdl_sysdep.h" #include "bsdl_bison.h" -#include "bsdl.h" +#include "bsdl_local.h" #define YY_EXTRA_TYPE scan_extra_t * diff --git a/jtag/src/bsdl/bsdl.h b/jtag/src/bsdl/bsdl_local.h similarity index 98% rename from jtag/src/bsdl/bsdl.h rename to jtag/src/bsdl/bsdl_local.h index 92487984..7d1ca4da 100644 --- a/jtag/src/bsdl/bsdl.h +++ b/jtag/src/bsdl/bsdl_local.h @@ -22,6 +22,9 @@ * */ +#ifndef BSDL_LOCAL_H +#define BSDL_LOCAL_H + #include @@ -175,3 +178,5 @@ void bsdl_ac_set_register(parser_priv_t *, char *, int); void bsdl_ac_add_instruction(parser_priv_t *, char *); void bsdl_ac_apply_assoc(parser_priv_t *); void bsdl_ac_finalize(parser_priv_t *); + +#endif /* BSDL_LOCAL_H */ diff --git a/jtag/src/bsdl/bsdl_sem.c b/jtag/src/bsdl/bsdl_sem.c index 23ed211e..6cc7655c 100644 --- a/jtag/src/bsdl/bsdl_sem.c +++ b/jtag/src/bsdl/bsdl_sem.c @@ -35,7 +35,7 @@ #include "bsdl_sysdep.h" #include "bsdl_bison.h" -#include "bsdl.h" +#include "bsdl_local.h" /***************************************************************************** diff --git a/jtag/src/cmd/bsdl.c b/jtag/src/cmd/bsdl.c index 32bcef31..41704064 100644 --- a/jtag/src/cmd/bsdl.c +++ b/jtag/src/cmd/bsdl.c @@ -28,28 +28,30 @@ #include #include -#include -#include +#include "bsdl.h" +#include "chain.h" +#include "cmd.h" static int cmd_bsdl_run( chain_t *chain, char *params[] ) { int num_params, result = -1; + bsdl_globs_t *globs = &(chain->bsdl); num_params = cmd_params(params); if (num_params >= 2) { if (strcmp(params[1], "test") == 0) { int debug_save; - debug_save = bsdl_debug; - bsdl_debug = 1; + debug_save = globs->debug; + globs->debug = 1; if (num_params == 3) { result = bsdl_read_file(chain, params[2], -1, NULL) >= 0 ? 1 : -1; } else if (num_params == 2) { bsdl_scan_files(chain, NULL, -1); result = 1; } - bsdl_debug = debug_save; + globs->debug = debug_save; } if (strcmp(params[1], "dump") == 0) { @@ -63,17 +65,17 @@ cmd_bsdl_run( chain_t *chain, char *params[] ) if (num_params == 3) { if (strcmp(params[1], "path") == 0) { - bsdl_set_path(params[2]); + bsdl_set_path(chain, params[2]); result = 1; } if (strcmp(params[1], "debug") == 0) { if (strcmp(params[2], "on") == 0) { - bsdl_debug = 1; + globs->debug = 1; result = 1; } if (strcmp(params[2], "off") == 0) { - bsdl_debug = 0; + globs->debug = 0; result = 1; } } diff --git a/jtag/src/tap/chain.c b/jtag/src/tap/chain.c index 9347f78b..8c830d76 100644 --- a/jtag/src/tap/chain.c +++ b/jtag/src/tap/chain.c @@ -31,6 +31,8 @@ #include "state.h" #include "tap.h" +#include "bsdl.h" + chain_t * chain_alloc( void ) { @@ -41,6 +43,7 @@ chain_alloc( void ) chain->cable = NULL; chain->parts = NULL; chain->active_part = 0; + BSDL_GLOBS_INIT(chain->bsdl); tap_state_init( chain ); return chain;