From d144dbf8f0081071fb0abcf748667504281f6eca Mon Sep 17 00:00:00 2001 From: Rutger Hofman Date: Tue, 5 May 2009 20:11:23 +0000 Subject: [PATCH] Refactor svf/ to use urj_log()/urj_error_set() i.s.o. printf() git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1571 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- urjtag/ChangeLog | 3 + urjtag/Makefile.rules | 2 - urjtag/include/urjtag/error.h | 3 + urjtag/include/urjtag/svf.h | 26 +++++- urjtag/src/global/urjtag.c | 3 + urjtag/src/svf/svf.c | 144 ++++++++++++++++++---------------- urjtag/src/svf/svf_bison.y | 11 ++- urjtag/src/svf/svf_flex.l | 13 +-- urjtag/src/tap/chain.c | 6 ++ 9 files changed, 133 insertions(+), 78 deletions(-) diff --git a/urjtag/ChangeLog b/urjtag/ChangeLog index 32237e6b..0c81b601 100644 --- a/urjtag/ChangeLog +++ b/urjtag/ChangeLog @@ -1,5 +1,8 @@ 2009-05-05 Rutger Hofman + * src/svf/svf.c, src/svf/svf_bison.y, src/svf/svf_flex.y: replace calls to + printf() with calls to urj_log() and urj_error_set() + * src/lib/getline.c, src/lib/getdelim.c: remove because unused * src/global/parse.c: replace calls to printf() with calls to urj_log() and diff --git a/urjtag/Makefile.rules b/urjtag/Makefile.rules index efc27cc6..3faa5789 100644 --- a/urjtag/Makefile.rules +++ b/urjtag/Makefile.rules @@ -22,8 +22,6 @@ # Written by Marcel Telka , 2003, 2004. # -# CFLAGS += -Werror - LINT_FLAGS = LINT_FLAGS += -warnposixheaders LINT_FLAGS += +boolint diff --git a/urjtag/include/urjtag/error.h b/urjtag/include/urjtag/error.h index c954a6bd..bb7b7c5a 100644 --- a/urjtag/include/urjtag/error.h +++ b/urjtag/include/urjtag/error.h @@ -34,12 +34,15 @@ typedef enum urj_error { URJ_ERROR_OK = 0, URJ_ERROR_ALREADY, URJ_ERROR_OUT_OF_MEMORY, + URJ_ERROR_NO_CHAIN, URJ_ERROR_NO_ACTIVE_PART, URJ_ERROR_INVALID, URJ_ERROR_NOTFOUND, URJ_ERROR_IO, /**< I/O error from OS */ URJ_ERROR_NO_BUS_DRIVER, URJ_ERROR_BUFFER_EXHAUSTED, + URJ_ERROR_ILLEGAL_STATE, + URJ_ERROR_OUT_OF_BOUNDS, } urj_error_t; /** Max length of message string that can be recorded. */ diff --git a/urjtag/include/urjtag/svf.h b/urjtag/include/urjtag/svf.h index 060abc66..4632830b 100644 --- a/urjtag/include/urjtag/svf.h +++ b/urjtag/include/urjtag/svf.h @@ -32,6 +32,30 @@ #include "types.h" -void urj_svf_run (urj_chain_t *chain, FILE *, int, int, uint32_t); +/** + * *************************************************************************** + * urj_svf_run(chain, SVF_FILE, stop_on_mismatch, ref_freq) + * + * Main entry point for the 'svf' command. Calls the svf parser. + * + * Checks the jtag-environment (availability of SIR instruction and SDR + * register). Initializes all svf-global variables and performs clean-up + * afterwards. + * + * @param chain pointer to global chain + * @param SVF_FILE file handle of SVF file + * @param stop_on_mismatch 1 = stop upon tdo mismatch + * 0 = continue upon mismatch + * @param print_progress 1 = continually print progress status + * 0 = don't print + * @param ref_freq reference frequency for RUNTEST + * + * @return + * 1 : all ok + * 0 : error occurred + * ***************************************************************************/ + +int urj_svf_run (urj_chain_t *chain, FILE *SVF_FILE, int stop_on_mismatch, + int print_progress, uint32_t ref_freq); #endif /* URJ_SVF_H */ diff --git a/urjtag/src/global/urjtag.c b/urjtag/src/global/urjtag.c index 3c425956..3c30d91e 100644 --- a/urjtag/src/global/urjtag.c +++ b/urjtag/src/global/urjtag.c @@ -93,12 +93,15 @@ urj_error_string (urj_error_t err) case URJ_ERROR_OK: return "no error"; case URJ_ERROR_ALREADY: return "already defined"; case URJ_ERROR_OUT_OF_MEMORY: return "out of memory"; + case URJ_ERROR_NO_CHAIN: return "no chain"; case URJ_ERROR_NO_ACTIVE_PART: return "no active part"; case URJ_ERROR_INVALID: return "invalid parameter"; case URJ_ERROR_NOTFOUND: return "not found"; case URJ_ERROR_IO: return "I/O error from OS"; case URJ_ERROR_NO_BUS_DRIVER: return "no bus driver"; case URJ_ERROR_BUFFER_EXHAUSTED: return "buffer exhausted"; + case URJ_ERROR_ILLEGAL_STATE: return "illegal state transition"; + case URJ_ERROR_OUT_OF_BOUNDS: return "out of bounds"; } return "UNDEFINED ERROR"; diff --git a/urjtag/src/svf/svf.c b/urjtag/src/svf/svf.c index b5531d72..cab2a904 100644 --- a/urjtag/src/svf/svf.c +++ b/urjtag/src/svf/svf.c @@ -40,6 +40,7 @@ #define SA_ONESHOT SA_RESETHAND #endif +#include #include #include #include @@ -364,7 +365,8 @@ urj_svf_build_bit_string (char *hex_string, int len) if (!(bit_string = calloc (len + 1, sizeof (char)))) { - printf (_("out of memory")); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + len + 1, sizeof (char)); return (NULL); } @@ -386,9 +388,8 @@ urj_svf_build_bit_string (char *hex_string, int len) nibble++; *bit_string_pos = - urj_svf_hex2dec (hex_string_idx >= - 0 ? *hex_string_pos : '0') & (1 << nibble) ? '1' - : '0'; + urj_svf_hex2dec (hex_string_idx >= 0 ? *hex_string_pos : '0') + & (1 << nibble) ? '1' : '0'; } while (bit_string_pos != bit_string); @@ -472,21 +473,20 @@ urj_svf_compare_tdo (urj_svf_parser_priv_t *priv, char *tdo, char *mask, if (mismatch >= 0) { - printf (_("Error %s: mismatch at position %d for TDO\n"), "svf", + urj_log (URJ_LOG_LEVEL_NORMAL, + _("Error %s: mismatch at position %d for TDO\n"), "svf", mismatch); if (loc != NULL) { - printf - (" in input file between line %d col %d and line %d col %d\n", - loc->first_line + 1, loc->first_column + 1, - loc->last_line + 1, loc->last_column + 1); + urj_log (URJ_LOG_LEVEL_NORMAL, + " in input file between line %d col %d and line %d col %d\n", + loc->first_line + 1, loc->first_column + 1, + loc->last_line + 1, loc->last_column + 1); } -#ifdef DEBUG - printf ("Expected : %s\n", tdo_bit); - printf ("Mask : %s\n", mask_bit); - printf ("TDO data : %s\n", reg->string); -#endif + urj_log (URJ_LOG_LEVEL_DEBUG, "Expected : %s\n", tdo_bit); + urj_log (URJ_LOG_LEVEL_DEBUG, "Mask : %s\n", mask_bit); + urj_log (URJ_LOG_LEVEL_DEBUG, "TDO data : %s\n", reg->string); if (priv->svf_stop_on_mismatch) result = 0; @@ -555,7 +555,8 @@ urj_svf_all_care (char **string, double number) /* build string with all cares */ if (!(ptr = calloc (num + 1, sizeof (char)))) { - printf (_("out of memory")); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("calloc(%zd,%zd) fails"), + num + 1, sizeof (char)); return (0); } memset (ptr, 'F', num); @@ -629,8 +630,9 @@ int urj_svf_hxr (enum generic_irdr_coding ir_dr, struct ths_params *params) { if (params->number != 0.0) - printf (_("Warning %s: command %s not implemented\n"), "svf", - ir_dr == generic_ir ? "HIR" : "HDR"); + urj_log (URJ_LOG_LEVEL_WARNINGS, + _("Warning %s: command %s not implemented\n"), "svf", + ir_dr == generic_ir ? "HIR" : "HDR"); return (1); } @@ -665,23 +667,26 @@ urj_svf_runtest (urj_chain_t *chain, urj_svf_parser_priv_t *priv, /* check for restrictions */ if (params->run_count > 0 && params->run_clk != TCK) { - printf (_("Error %s: only TCK is supported for RUNTEST.\n"), "svf"); + urj_error_set (URJ_ERROR_INVALID, + _("Error %s: only TCK is supported for RUNTEST.\n"), + "svf"); return (0); } if (params->max_time > 0.0 && params->max_time < params->min_time) { - printf (_ - ("Error %s: maximum time must be larger or equal to minimum time.\n"), + urj_error_set (URJ_ERROR_OUT_OF_BOUNDS, + _("Error %s: maximum time must be larger or equal to minimum time.\n"), "svf"); return (0); } if (params->max_time > 0.0) if (!priv->issued_runtest_maxtime) { - printf (_ - ("Warning %s: maximum time for RUNTEST not guaranteed.\n"), + urj_log (URJ_LOG_LEVEL_WARNINGS, + _("Warning %s: maximum time for RUNTEST not guaranteed.\n"), "svf"); - printf (_(" This message is only displayed once.\n")); + urj_log (URJ_LOG_LEVEL_WARNINGS, + _(" This message is only displayed once.\n")); priv->issued_runtest_maxtime = 1; } @@ -713,10 +718,11 @@ urj_svf_runtest (urj_chain_t *chain, urj_svf_parser_priv_t *priv, } else { - printf (_ - ("Error %s: Maximum cable clock frequency required for RUNTEST.\n"), - "svf"); - printf (_(" Set the cable frequency with 'FREQUENCY '.\n")); + urj_error_set (URJ_ERROR_OUT_OF_BOUNDS, + _("Error %s: Maximum cable clock frequency required for RUNTEST.\n"), + "svf"); + urj_log (URJ_LOG_LEVEL_ERRORS, + _(" Set the cable frequency with 'FREQUENCY '.\n")); return 0; } } @@ -880,8 +886,8 @@ urj_svf_sxr (urj_chain_t *chain, urj_svf_parser_priv_t *priv, { if (!params->tdi) { - printf (_ - ("Error %s: first %s command after length change must have a TDI value.\n"), + urj_log (URJ_LOG_LEVEL_ERRORS, + _("Error %s: first %s command after length change must have a TDI value.\n"), "svf", ir_dr == generic_ir ? "SIR" : "SDR"); result = 0; } @@ -908,11 +914,12 @@ urj_svf_sxr (urj_chain_t *chain, urj_svf_parser_priv_t *priv, /* is SIR large enough? */ if (priv->ir->value->len != len) { - printf (_("Error %s: SIR command length inconsistent.\n"), "svf"); + urj_log (URJ_LOG_LEVEL_ERRORS, + _("Error %s: SIR command length inconsistent.\n"), "svf"); if (loc != NULL) { - printf - (" in input file between line %d col %d and line %d col %d\n", + urj_log (URJ_LOG_LEVEL_ERRORS, + _(" in input file between line %d col %d and line %d col %d\n"), loc->first_line + 1, loc->first_column + 1, loc->last_line + 1, loc->last_column + 1); } @@ -931,15 +938,11 @@ urj_svf_sxr (urj_chain_t *chain, urj_svf_parser_priv_t *priv, priv->dr->out = NULL; if (!(priv->dr->in = urj_tap_register_alloc (len))) - { - printf (_("out of memory")); + // retain error state return (0); - } if (!(priv->dr->out = urj_tap_register_alloc (len))) - { - printf (_("out of memory")); + // retain error state return (0); - } } break; @@ -1018,9 +1021,9 @@ urj_svf_trst (urj_chain_t *chain, urj_svf_parser_priv_t *priv, int trst_mode) if (priv->svf_trst_absent) { - printf (_ - ("Error %s: no further TRST command allowed after mode ABSENT\n"), - "svf"); + urj_error_set (URJ_ERROR_ILLEGAL_STATE, + _("Error %s: no further TRST command allowed after mode ABSENT\n"), + "svf"); return (0); } @@ -1041,17 +1044,17 @@ urj_svf_trst (urj_chain_t *chain, urj_svf_parser_priv_t *priv, int trst_mode) if (priv->svf_state_executed) { - printf (_ - ("Error %s: TRST ABSENT must not be issued after a STATE command\n"), - "svf"); + urj_error_set (URJ_ERROR_ILLEGAL_STATE, + _("Error %s: TRST ABSENT must not be issued after a STATE command\n"), + "svf"); return (0); } if (priv->sir_params.params.number > 0.0 || priv->sdr_params.params.number > 0.0) { - printf (_ - ("Error %s: TRST ABSENT must not be issued after an SIR or SDR command\n"), - "svf"); + urj_error_set (URJ_ERROR_ILLEGAL_STATE, + _("Error %s: TRST ABSENT must not be issued after an SIR or SDR command\n"), + "svf"); } break; default: @@ -1060,8 +1063,9 @@ urj_svf_trst (urj_chain_t *chain, urj_svf_parser_priv_t *priv, int trst_mode) } if (trst_cable < 0) - printf (_("Warning %s: unimplemented mode '%s' for TRST\n"), "svf", - unimplemented_mode); + urj_log (URJ_LOG_LEVEL_WARNINGS, + _("Warning %s: unimplemented mode '%s' for TRST\n"), "svf", + unimplemented_mode); else urj_tap_cable_set_signal (chain->cable, URJ_POD_CS_TRST, trst_cable ? URJ_POD_CS_TRST : 0); @@ -1090,7 +1094,8 @@ int urj_svf_txr (enum generic_irdr_coding ir_dr, struct ths_params *params) { if (params->number != 0.0) - printf (_("Warning %s: command %s not implemented\n"), "svf", + urj_log (URJ_LOG_LEVEL_WARNINGS, + _("Warning %s: command %s not implemented\n"), "svf", ir_dr == generic_ir ? "TIR" : "TDR"); return (1); @@ -1119,7 +1124,7 @@ urj_svf_txr (enum generic_irdr_coding ir_dr, struct ths_params *params) * 1 : all ok * 0 : error occurred * ***************************************************************************/ -void +int urj_svf_run (urj_chain_t *chain, FILE *SVF_FILE, int stop_on_mismatch, int print_progress, uint32_t ref_freq) { @@ -1152,13 +1157,15 @@ urj_svf_run (urj_chain_t *chain, FILE *SVF_FILE, int stop_on_mismatch, - data register */ if (chain == NULL) { - printf (_("Error %s: no JTAG chain available\n"), "svf"); - return; + urj_error_set (URJ_ERROR_NO_CHAIN, _("%s: no JTAG chain available\n"), + "svf"); + return 0; } if (chain->parts == NULL) { - printf (_("Error %s: chain without any parts\n"), "svf"); - return; + urj_error_set (URJ_ERROR_NO_ACTIVE_PART, + _("%s: chain without any parts\n"), "svf"); + return 0; } priv.part = chain->parts->parts[chain->active_part]; @@ -1166,12 +1173,14 @@ urj_svf_run (urj_chain_t *chain, FILE *SVF_FILE, int stop_on_mismatch, if (!(priv.dr = urj_part_find_data_register (priv.part, "SDR"))) { if (urj_part_data_register_define(priv.part, "SDR", 32) != URJ_STATUS_OK) - return; + return 0; if (!(priv.dr = urj_part_find_data_register (priv.part, "SDR"))) { - printf (_("Error %s: could not establish SDR register\n"), "svf"); - return; + urj_error_set (URJ_ERROR_NOTFOUND, + _("%s: could not establish SDR register\n"), + "svf"); + return 0; } } @@ -1201,15 +1210,16 @@ urj_svf_run (urj_chain_t *chain, FILE *SVF_FILE, int stop_on_mismatch, free (instruction_string); if (result < 1) - return; + return 0; } } if (!(priv.ir = urj_part_find_instruction (priv.part, "SIR"))) { - printf (_("Error %s: could not establish SIR instruction\n"), - "svf"); - return; + urj_error_set (URJ_ERROR_NOTFOUND, + _("%s: could not establish SIR instruction\n"), + "svf"); + return 0; } } @@ -1245,11 +1255,11 @@ urj_svf_run (urj_chain_t *chain, FILE *SVF_FILE, int stop_on_mismatch, if (print_progress) { if (priv.mismatch_occurred > 0) - printf (_ - ("Mismatches occurred between scanned device output and expected TDO values.\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("Mismatches occurred between scanned device output and expected TDO values.\n")); else - printf (_ - ("Scanned device output matched expected TDO values.\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("Scanned device output matched expected TDO values.\n")); } /* clean up */ @@ -1271,4 +1281,6 @@ urj_svf_run (urj_chain_t *chain, FILE *SVF_FILE, int stop_on_mismatch, /* restore previous frequency setting, required by SVF spec */ if (old_frequency != urj_tap_cable_get_frequency (chain->cable)) urj_tap_cable_set_frequency (chain->cable, old_frequency); + + return 1; } diff --git a/urjtag/src/svf/svf_bison.y b/urjtag/src/svf/svf_bison.y index 28e53a9f..e251dc48 100644 --- a/urjtag/src/svf/svf_bison.y +++ b/urjtag/src/svf/svf_bison.y @@ -34,6 +34,8 @@ #include #include +#include + #include "svf.h" /* interface to flex */ @@ -136,7 +138,7 @@ svf_statement | PIOMAP '(' direction IDENTIFIER piomap_rec ')' ';' { - printf("PIOMAP not implemented\n"); + urj_log (URJ_LOG_LEVEL_ERRORS, "PIOMAP not implemented\n"); yyerror(&@$, priv_data, chain, "PIOMAP"); YYERROR; } @@ -144,7 +146,7 @@ svf_statement | PIO VECTOR_STRING ';' { free($2); - printf("PIO not implemented\n"); + urj_log (URJ_LOG_LEVEL_ERRORS, "PIO not implemented\n"); yyerror(&@$, priv_data, chain, "PIO"); YYERROR; } @@ -382,7 +384,7 @@ path_states ps->states[ps->num_states] = $2; ps->num_states++; } else - printf("Error %s: maximum number of %d path states reached.\n", + urj_log (URJ_LOG_LEVEL_ERRORS, "Error %s: maximum number of %d path states reached.\n", "svf", MAX_PATH_STATES); } ; @@ -409,7 +411,8 @@ direction yyerror (YYLTYPE *locp, urj_svf_parser_priv_t *priv_data, urj_chain_t *chain, const char *error_string) { - printf ("Error occurred for SVF command %s.\n", error_string); + urj_log (URJ_LOG_LEVEL_ERRORS, "Error occurred for SVF command %s.\n", + error_string); } diff --git a/urjtag/src/svf/svf_flex.l b/urjtag/src/svf/svf_flex.l index 228bd8f2..af9f7ecf 100644 --- a/urjtag/src/svf/svf_flex.l +++ b/urjtag/src/svf/svf_flex.l @@ -36,6 +36,7 @@ #include #include +#include #ifdef ENABLE_NLS #include @@ -206,7 +207,8 @@ COMMENT (!.*)|("//".*)[^\n] . { /* print token if interactive parsing enabled and yyin != stdin */ - fprintf(stderr, "Error: \"%s\" is not a legal SVF language token\n", yytext); + urj_log (URJ_LOG_LEVEL_ERRORS, + "Error: \"%s\" is not a legal SVF language token\n", yytext); } /* end of any other character */ @@ -360,8 +362,8 @@ progress_nl (YYLTYPE *mylloc, YY_EXTRA_TYPE extra) percent = ((mylloc->last_line * 100) + 1) / extra->num_lines; if (percent <= 1) return; // dont bother printing < 1 % - printf ("\r"); - printf (_("Parsing %6d/%d (%3.0d%%)"), + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); + urj_log (URJ_LOG_LEVEL_NORMAL, _("Parsing %6d/%d (%3.0d%%)"), mylloc->last_line, extra->num_lines, percent); } } @@ -381,7 +383,8 @@ urj_svf_flex_init (FILE *f, int num_lines, int print_progress) if (!(extra = malloc (sizeof (urj_svf_scanner_extra_t)))) { - printf (_("Out of memory, %s line %i\n"), __FILE__, __LINE__); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("malloc(%zd) fails"), + sizeof (urj_svf_scanner_extra_t)); yylex_destroy (scanner); return NULL; } @@ -409,7 +412,7 @@ urj_svf_flex_deinit (void *scanner) { YY_EXTRA_TYPE extra = yyget_extra (scanner); if (extra->print_progress) - printf ("\n"); + urj_log (URJ_LOG_LEVEL_NORMAL, "\n"); free (extra); yylex_destroy (scanner); } diff --git a/urjtag/src/tap/chain.c b/urjtag/src/tap/chain.c index 46daab44..f973fa5a 100644 --- a/urjtag/src/tap/chain.c +++ b/urjtag/src/tap/chain.c @@ -286,6 +286,12 @@ urj_tap_chain_flush (urj_chain_t *chain) urj_part_t * urj_tap_chain_active_part (urj_chain_t *chain) { + if (chain == NULL) + { + urj_error_set (URJ_ERROR_NO_CHAIN, "no JTAG chain"); + return NULL; + } + if (!chain->parts) { urj_error_set (URJ_ERROR_NO_ACTIVE_PART,