From 161f763507ecb8ca1e16c8c6f92584d15387338b Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Wed, 8 May 2024 22:10:06 +0100 Subject: [PATCH] Consolidate functions returning a prog_modes string --- src/avr.c | 84 ++++++++++++++++++++++++-------------------- src/avrpart.c | 2 +- src/developer_opts.c | 43 ++--------------------- src/libavrdude.h | 3 +- src/term.c | 2 +- 5 files changed, 51 insertions(+), 83 deletions(-) diff --git a/src/avr.c b/src/avr.c index 61c8872e..039e488e 100644 --- a/src/avr.c +++ b/src/avr.c @@ -1443,10 +1443,12 @@ int avr_put_cycle_count(const PROGRAMMER *pgm, const AVRPART *p, int cycles) { /* - * Returns a string in closed-circuit space with a comma-separated list of - * programming modes; variant creates the list in subtly different way - * - 0: PM_SPM prints bootloader, pm == 0 prints "?" - * - 1: PM_SPM prints SPM, pm == 0 prints "0" + * Returns a string in closed-circuit space with a list of programming + * modes encoded in pm; variant creates the list in subtly different ways: + * - variants == 0: PM_SPM prints bootloader + * - variants == 1: PM_SPM prints SPM + * - variants == 2: rather than a comma-separated list it's | PM_... separated + * If pm is 0 (no programming modes) returns "0" */ static char *prog_modes_string(int pm, int variant) { // Return string is overwritten after a few calls @@ -1457,48 +1459,52 @@ static char *prog_modes_string(int pm, int variant) { if((size_t) (type - cx->avr_space) > sizeof cx->avr_space - 128) type = cx->avr_space; - strcpy(type, variant? "0": "?"); - if(variant == 0 && (pm & PM_SPM)) - strcat(type, ", bootloader"); - if(pm & PM_TPI) - strcat(type, ", TPI"); - if(pm & PM_ISP) - strcat(type, ", ISP"); - if(pm & PM_PDI) - strcat(type, ", PDI"); - if(pm & PM_UPDI) - strcat(type, ", UPDI"); - if(pm & PM_HVSP) - strcat(type, ", HVSP"); - if(pm & PM_HVPP) - strcat(type, ", HVPP"); - if(pm & PM_debugWIRE) - strcat(type, ", debugWIRE"); - if(pm & PM_JTAG) - strcat(type, ", JTAG"); - if(pm & PM_JTAGmkI) - strcat(type, ", JTAGmkI"); - if(pm & PM_XMEGAJTAG) - strcat(type, ", XMEGAJTAG"); - if(pm & PM_AVR32JTAG) - strcat(type, ", AVR32JTAG"); - if(pm & PM_aWire) - strcat(type, ", aWire"); - if(variant == 1 && (pm & PM_SPM)) - strcat(type, ", SPM"); + const char *spm = variant? "SPM": "bootloader"; + const char *sep = variant == 2? " | PM_": ", "; + int skip = 3 + (variant == 2); - cx->avr_s = type + (type[1] == 0? 0: 3); + strcpy(type, "0"); + if(pm & PM_SPM) + strcat(strcat(type, sep), spm); + if(pm & PM_TPI) + strcat(strcat(type, sep), "TPI"); + if(pm & PM_ISP) + strcat(strcat(type, sep), "ISP"); + if(pm & PM_PDI) + strcat(strcat(type, sep), "PDI"); + if(pm & PM_UPDI) + strcat(strcat(type, sep), "UPDI"); + if(pm & PM_HVSP) + strcat(strcat(type, sep), "HVSP"); + if(pm & PM_HVPP) + strcat(strcat(type, sep), "HVPP"); + if(pm & PM_debugWIRE) + strcat(strcat(type, sep), "debugWIRE"); + if(pm & PM_JTAG) + strcat(strcat(type, sep), "JTAG"); + if(pm & PM_JTAGmkI) + strcat(strcat(type, sep), "JTAGmkI"); + if(pm & PM_XMEGAJTAG) + strcat(strcat(type, sep), "XMEGAJTAG"); + if(pm & PM_AVR32JTAG) + strcat(strcat(type, sep), "AVR32JTAG"); + if(pm & PM_aWire) + strcat(strcat(type, sep), "aWire"); + + cx->avr_s = type + (type[1] == 0? 0: skip); return cx->avr_s; } -// Returns a string in closed-circuit space with list of programming modes or "0" -char *avr_prog_modes_str(int pm) { +char *avr_prog_modes(int pm) { // PM_SPM prints bootloader + return prog_modes_string(pm, 0); +} + +char *str_prog_modes(int pm) { // PM_SPM prints SPM return prog_modes_string(pm, 1); } -// Returns a string in closed-circuit space with list of programming modes or "?" -char *avr_prog_modes(int pm) { - return prog_modes_string(pm, 0); +char *dev_prog_modes(int pm) { // Symbolic C code of prog_modes + return prog_modes_string(pm, 2); } diff --git a/src/avrpart.c b/src/avrpart.c index c4023843..779a803a 100644 --- a/src/avrpart.c +++ b/src/avrpart.c @@ -1086,7 +1086,7 @@ void sort_avrparts(LISTID avrparts) void avr_display(FILE *f, const AVRPART *p, const char *prefix, int verbose) { fprintf(f, "%sAVR Part : %s\n", prefix, p->desc); - fprintf(f, "%sProgramming modes : %s\n", prefix, avr_prog_modes_str(p->prog_modes)); + fprintf(f, "%sProgramming modes : %s\n", prefix, str_prog_modes(p->prog_modes)); if(verbose > 1) { avr_mem_display(f, p, prefix); diff --git a/src/developer_opts.c b/src/developer_opts.c index 3d28bc61..a155dfd2 100644 --- a/src/developer_opts.c +++ b/src/developer_opts.c @@ -133,43 +133,6 @@ static void printallopcodes(const AVRPART *p, const char *d, OPCODE * const *opa } - -// Programming modes - -static char *prog_modes_str(int pm) { - static char type[1024]; - - strcpy(type, "0"); - if(pm & PM_SPM) - strcat(type, " | PM_SPM"); - if(pm & PM_TPI) - strcat(type, " | PM_TPI"); - if(pm & PM_ISP) - strcat(type, " | PM_ISP"); - if(pm & PM_PDI) - strcat(type, " | PM_PDI"); - if(pm & PM_UPDI) - strcat(type, " | PM_UPDI"); - if(pm & PM_HVSP) - strcat(type, " | PM_HVSP"); - if(pm & PM_HVPP) - strcat(type, " | PM_HVPP"); - if(pm & PM_debugWIRE) - strcat(type, " | PM_debugWIRE"); - if(pm & PM_JTAG) - strcat(type, " | PM_JTAG"); - if(pm & PM_JTAGmkI) - strcat(type, " | PM_JTAGmkI"); - if(pm & PM_XMEGAJTAG) - strcat(type, " | PM_XMEGAJTAG"); - if(pm & PM_AVR32JTAG) - strcat(type, " | PM_AVR32JTAG"); - if(pm & PM_aWire) - strcat(type, " | PM_aWire"); - - return type + (type[1] == 0? 0: 4); -} - static char *extra_features_str(int m) { static char mode[1024]; @@ -620,7 +583,7 @@ static void dev_part_strct(const AVRPART *p, bool tsv, const AVRPART *base, bool } _if_partout_str(strcmp, cfg_escape(p->family_id), family_id); - _if_partout_str(intcmp, mmt_strdup(prog_modes_str(p->prog_modes)), prog_modes); + _if_partout_str(intcmp, mmt_strdup(dev_prog_modes(p->prog_modes)), prog_modes); if(p->mcuid == 21) { _if_partout_str(intcmp, mmt_strdup("XVII + IV"), mcuid); } else { @@ -1124,7 +1087,7 @@ void dev_output_part_defs(char *partdesc) { nfuses, ok, p->flags, - prog_modes_str(p->prog_modes), + dev_prog_modes(p->prog_modes), p->config_file, p->lineno ); } @@ -1318,7 +1281,7 @@ static void dev_pgm_strct(const PROGRAMMER *pgm, bool tsv, const PROGRAMMER *bas _if_pgmout_str(strcmp, cfg_escape(pgm->desc), desc); if(!base || base->initpgm != pgm->initpgm) _pgmout_fmt("type", "\"%s\"", locate_programmer_type_id(pgm->initpgm)); - _if_pgmout_str(intcmp, mmt_strdup(prog_modes_str(pgm->prog_modes)), prog_modes); + _if_pgmout_str(intcmp, mmt_strdup(dev_prog_modes(pgm->prog_modes)), prog_modes); _if_pgmout_str(boolcmp, mmt_strdup(pgm->is_serialadapter? "yes": "no"), is_serialadapter); _if_pgmout_str(intcmp, mmt_strdup(extra_features_str(pgm->extra_features)), extra_features); if(!base || base->conntype != pgm->conntype) diff --git a/src/libavrdude.h b/src/libavrdude.h index c288b14a..bd1f1169 100644 --- a/src/libavrdude.h +++ b/src/libavrdude.h @@ -573,8 +573,7 @@ AVRPART * locate_part(const LISTID parts, const char *partdesc); AVRPART * locate_part_by_avr910_devcode(const LISTID parts, int devcode); AVRPART * locate_part_by_signature(const LISTID parts, unsigned char *sig, int sigsize); AVRPART * locate_part_by_signature_pm(const LISTID parts, unsigned char *sig, int sigsize, int prog_modes); -char *avr_prog_modes(int pm); -char *avr_prog_modes_str(int pm); +char *avr_prog_modes(int pm), *str_prog_modes(int pm), *dev_prog_modes(int pm); void avr_display(FILE *f, const AVRPART *p, const char *prefix, int verbose); int avr_variants_display(FILE *f, const AVRPART *p, const char *prefix); diff --git a/src/term.c b/src/term.c index 4ee0bc73..d39d7850 100644 --- a/src/term.c +++ b/src/term.c @@ -1890,7 +1890,7 @@ static int cmd_part(const PROGRAMMER *pgm, const AVRPART *p, int argc, const cha else if(onlyvariants) avr_variants_display(stdout, p, ""); else { - term_out("%s with programming modes %s\n", p->desc, avr_prog_modes_str(p->prog_modes)); + term_out("%s with programming modes %s\n", p->desc, str_prog_modes(p->prog_modes)); avr_mem_display(stdout, p, ""); avr_variants_display(stdout, p, ""); }