Make developer opts portable: change statement exprs and index(); use size_t

This commit is contained in:
Stefan Rueger 2022-07-22 23:50:22 +01:00
parent a95d169ccc
commit 5a517fb74d
2 changed files with 38 additions and 36 deletions

View File

@ -510,7 +510,7 @@ static const char *mem_order[100] = {
}; };
static void add_mem_order(const char *str) { static void add_mem_order(const char *str) {
for(int i=0; i < sizeof mem_order/sizeof *mem_order; i++) { for(size_t i=0; i < sizeof mem_order/sizeof *mem_order; i++) {
if(mem_order[i] && !strcmp(mem_order[i], str)) if(mem_order[i] && !strcmp(mem_order[i], str))
return; return;
if(!mem_order[i]) { if(!mem_order[i]) {
@ -536,7 +536,7 @@ typedef struct {
} AVRMEMdeep; } AVRMEMdeep;
static int avrmem_deep_copy(AVRMEMdeep *d, AVRMEM *m) { static int avrmem_deep_copy(AVRMEMdeep *d, AVRMEM *m) {
int len; size_t len;
d->base = *m; d->base = *m;
@ -555,7 +555,7 @@ static int avrmem_deep_copy(AVRMEMdeep *d, AVRMEM *m) {
// copy over the SPI operations themselves // copy over the SPI operations themselves
memset(d->base.op, 0, sizeof d->base.op); memset(d->base.op, 0, sizeof d->base.op);
memset(d->ops, 0, sizeof d->ops); memset(d->ops, 0, sizeof d->ops);
for(int i=0; i<sizeof d->ops/sizeof *d->ops; i++) for(size_t i=0; i<sizeof d->ops/sizeof *d->ops; i++)
if(m->op[i]) if(m->op[i])
d->ops[i] = *m->op[i]; d->ops[i] = *m->op[i];
@ -586,7 +586,7 @@ typedef struct {
static int avrpart_deep_copy(AVRPARTdeep *d, AVRPART *p) { static int avrpart_deep_copy(AVRPARTdeep *d, AVRPART *p) {
AVRMEM *m; AVRMEM *m;
int len, di; size_t len, di;
memset(d, 0, sizeof *d); memset(d, 0, sizeof *d);
@ -623,7 +623,7 @@ static int avrpart_deep_copy(AVRPARTdeep *d, AVRPART *p) {
// fill in all memories we got in defined order // fill in all memories we got in defined order
di = 0; di = 0;
for(int mi=0; mi < sizeof mem_order/sizeof *mem_order && mem_order[mi]; mi++) { for(size_t mi=0; mi < sizeof mem_order/sizeof *mem_order && mem_order[mi]; mi++) {
m = p->mem? avr_locate_mem(p, mem_order[mi]): NULL; m = p->mem? avr_locate_mem(p, mem_order[mi]): NULL;
if(m) { if(m) {
if(di >= sizeof d->mems/sizeof *d->mems) { if(di >= sizeof d->mems/sizeof *d->mems) {
@ -773,7 +773,7 @@ static void dev_part_strct(AVRPART *p, bool tsv, AVRPART *base) {
if(!base || opcodecmp(p->op[i], base->op[i], i)) if(!base || opcodecmp(p->op[i], base->op[i], i))
dev_part_strct_entry(tsv, ".ptop", p->desc, "part", opcodename(i), opcode2str(p->op[i], i, !tsv)); dev_part_strct_entry(tsv, ".ptop", p->desc, "part", opcodename(i), opcode2str(p->op[i], i, !tsv));
for(int mi=0; mi < sizeof mem_order/sizeof *mem_order && mem_order[mi]; mi++) { for(size_t mi=0; mi < sizeof mem_order/sizeof *mem_order && mem_order[mi]; mi++) {
AVRMEM *m, *bm; AVRMEM *m, *bm;
m = p->mem? avr_locate_mem(p, mem_order[mi]): NULL; m = p->mem? avr_locate_mem(p, mem_order[mi]): NULL;
@ -840,7 +840,9 @@ static void dev_part_strct(AVRPART *p, bool tsv, AVRPART *base) {
* from the GNU C Library (published under GLP v2). Used for portability. * from the GNU C Library (published under GLP v2). Used for portability.
*/ */
#define FOLD(c) ({ int _c = (unsigned char) (c); isascii(_c)? tolower(_c): _c; }) inline static int fold(int c) {
return (c >= 'A' && c <= 'Z')? c+('a'-'A'): c;
}
int part_match(const char *pattern, const char *string) { int part_match(const char *pattern, const char *string) {
unsigned char c; unsigned char c;
@ -849,7 +851,7 @@ int part_match(const char *pattern, const char *string) {
if(!*n) // AVRDUDE specialty: empty string never matches if(!*n) // AVRDUDE specialty: empty string never matches
return 0; return 0;
while((c = FOLD(*p++))) { while((c = fold(*p++))) {
switch(c) { switch(c) {
case '?': case '?':
if(*n == 0) if(*n == 0)
@ -857,8 +859,8 @@ int part_match(const char *pattern, const char *string) {
break; break;
case '\\': case '\\':
c = FOLD(*p++); c = fold(*p++);
if(FOLD(*n) != c) if(fold(*n) != c)
return 0; return 0;
break; break;
@ -871,10 +873,10 @@ int part_match(const char *pattern, const char *string) {
return 1; return 1;
{ {
unsigned char c1 = FOLD(c == '\\'? *p : c); // This char unsigned char c1 = fold(c == '\\'? *p : c); // This char
for(--p; *n; ++n) // Recursively check reminder of string for * for(--p; *n; ++n) // Recursively check reminder of string for *
if((c == '[' || FOLD(*n) == c1) && part_match(p, n) == 1) if((c == '[' || fold(*n) == c1) && part_match(p, n) == 1)
return 1; return 1;
return 0; return 0;
} }
@ -897,13 +899,13 @@ int part_match(const char *pattern, const char *string) {
if(c == '\\') if(c == '\\')
cstart = cend = *p++; cstart = cend = *p++;
cstart = cend = FOLD(cstart); cstart = cend = fold(cstart);
if(c == 0) // [ (unterminated) if(c == 0) // [ (unterminated)
return 0; return 0;
c = *p++; c = *p++;
c = FOLD(c); c = fold(c);
if(c == '-' && *p != ']') { if(c == '-' && *p != ']') {
cend = *p++; cend = *p++;
@ -911,12 +913,12 @@ int part_match(const char *pattern, const char *string) {
cend = *p++; cend = *p++;
if(cend == 0) if(cend == 0)
return 0; return 0;
cend = FOLD(cend); cend = fold(cend);
c = *p++; c = *p++;
} }
if(FOLD(*n) >= cstart && FOLD(*n) <= cend) if(fold(*n) >= cstart && fold(*n) <= cend)
goto matched; goto matched;
if(c == ']') if(c == ']')
@ -942,7 +944,7 @@ int part_match(const char *pattern, const char *string) {
break; break;
default: default:
if(c != FOLD(*n)) if(c != fold(*n))
return 0; return 0;
} }
@ -1043,12 +1045,12 @@ void dev_output_part_defs(char *partdesc) {
if(p->mem) { if(p->mem) {
for(LNODEID lnm=lfirst(p->mem); lnm; lnm=lnext(lnm)) { for(LNODEID lnm=lfirst(p->mem); lnm; lnm=lnext(lnm)) {
AVRMEM *m = ldata(lnm); AVRMEM *m = ldata(lnm);
if(!flashsize && m->desc && 0==strcmp(m->desc, "flash")) { if(!flashsize && 0==strcmp(m->desc, "flash")) {
flashsize = m->size; flashsize = m->size;
flashpagesize = m->page_size; flashpagesize = m->page_size;
flashoffset = m->offset; flashoffset = m->offset;
} }
if(!eepromsize && m->desc && 0==strcmp(m->desc, "eeprom")) { if(!eepromsize && 0==strcmp(m->desc, "eeprom")) {
eepromsize = m->size; eepromsize = m->size;
eepromoffset = m->offset; eepromoffset = m->offset;
eeprompagesize = m->page_size; eeprompagesize = m->page_size;
@ -1057,7 +1059,7 @@ void dev_output_part_defs(char *partdesc) {
} }
// "real" entries don't seem to have a space in their desc (a bit hackey) // "real" entries don't seem to have a space in their desc (a bit hackey)
if(flashsize && !index(p->desc, ' ')) { if(flashsize && !strchr(p->desc, ' ')) {
int ok, nfuses; int ok, nfuses;
AVRMEM *m; AVRMEM *m;
OPCODE *oc; OPCODE *oc;

View File

@ -54,61 +54,61 @@ static int dev_message(int msglvl, const char *fmt, ...);
#define _partout(fmt, component) \ #define _partout(fmt, component) \
dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, dev_sprintf(fmt, p->component)) dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, dev_sprintf(fmt, p->component))
#define _if_partout(cmp, fmt, component) ({ \ #define _if_partout(cmp, fmt, component) do { \
if(!base || cmp(base->component, p->component)) \ if(!base || cmp(base->component, p->component)) \
dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, dev_sprintf(fmt, p->component)); \ dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, dev_sprintf(fmt, p->component)); \
}) } while(0)
#define _if_n_partout(cmp, n, fmt, component) ({ \ #define _if_n_partout(cmp, n, fmt, component) do { \
if(!base || cmp(base->component, p->component, n)) \ if(!base || cmp(base->component, p->component, n)) \
dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, dev_sprintf(fmt, p->component)); \ dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, dev_sprintf(fmt, p->component)); \
}) } while(0)
#define _partout_str(result, component) \ #define _partout_str(result, component) \
dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, result) dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, result)
#define _if_partout_str(cmp, result, component) ({ \ #define _if_partout_str(cmp, result, component) do { \
if(!base || cmp(base->component, p->component)) \ if(!base || cmp(base->component, p->component)) \
dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, result); \ dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, result); \
}) } while(0)
#define _if_n_partout_str(cmp, n, result, component) ({ \ #define _if_n_partout_str(cmp, n, result, component) do { \
if(!base || cmp(base->component, p->component, n)) \ if(!base || cmp(base->component, p->component, n)) \
dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, result); \ dev_part_strct_entry(tsv, ".pt", p->desc, NULL, #component, result); \
}) } while(0)
#define _memout(fmt, component) \ #define _memout(fmt, component) \
dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, dev_sprintf(fmt, m->component)) dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, dev_sprintf(fmt, m->component))
#define _if_memout(cmp, fmt, component) ({ \ #define _if_memout(cmp, fmt, component) do { \
if(!bm || cmp(bm->component, m->component)) \ if(!bm || cmp(bm->component, m->component)) \
dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, dev_sprintf(fmt, m->component)); \ dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, dev_sprintf(fmt, m->component)); \
}) } while(0)
#define _memout_str(result, component) \ #define _memout_str(result, component) \
dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, result) dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, result)
#define _if_n_memout_str(cmp, n, result, component) ({ \ #define _if_n_memout_str(cmp, n, result, component) do { \
if(!bm || cmp(bm->component, m->component, n)) \ if(!bm || cmp(bm->component, m->component, n)) \
dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, result); \ dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, result); \
}) } while(0)
#define _memout_yn(component) \ #define _memout_yn(component) \
dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, strdup(m->component? "yes": "no")) dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, strdup(m->component? "yes": "no"))
#define _if_memout_yn(component) ({ \ #define _if_memout_yn(component) do { \
if(!bm || bm->component != m->component) \ if(!bm || bm->component != m->component) \
dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, strdup(m->component? "yes": "no")); \ dev_part_strct_entry(tsv, ".ptmm", p->desc, m->desc, #component, strdup(m->component? "yes": "no")); \
}) } while(0)
#define _flagout(mask, name) \ #define _flagout(mask, name) \
_partout_str(strdup(p->flags & (mask)? "yes": "no"), name) _partout_str(strdup(p->flags & (mask)? "yes": "no"), name)
#define _if_flagout(mask, name) ({ \ #define _if_flagout(mask, name) do { \
if(!base || (base->flags & (mask)) != (p->flags & (mask))) \ if(!base || (base->flags & (mask)) != (p->flags & (mask))) \
_partout_str(strdup(p->flags & (mask)? "yes": "no"), name); \ _partout_str(strdup(p->flags & (mask)? "yes": "no"), name); \
}) } while(0)
#define _cmderr(result, component) \ #define _cmderr(result, component) \
dev_part_strct_entry(tsv, ".cmderr", p->desc, m->desc, #component, result) dev_part_strct_entry(tsv, ".cmderr", p->desc, m->desc, #component, result)