Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace string comparison functions #1482

Merged
merged 16 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/avr910.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ static int avr910_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRM
{
char cmd[2];

if (strcmp(m->desc, "flash") == 0) {
if (str_eq(m->desc, "flash")) {
if (addr & 0x01) {
cmd[0] = 'C'; /* Write Program Mem high byte */
}
Expand All @@ -413,7 +413,7 @@ static int avr910_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRM

addr >>= 1;
}
else if (strcmp(m->desc, "eeprom") == 0) {
else if (str_eq(m->desc, "eeprom")) {
cmd[0] = 'D';
}
else {
Expand Down Expand Up @@ -468,11 +468,11 @@ static int avr910_read_byte_eeprom(const PROGRAMMER *pgm, const AVRPART *p, cons
static int avr910_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m,
unsigned long addr, unsigned char * value)
{
if (strcmp(m->desc, "flash") == 0) {
if (str_eq(m->desc, "flash")) {
return avr910_read_byte_flash(pgm, p, m, addr, value);
}

if (strcmp(m->desc, "eeprom") == 0) {
if (str_eq(m->desc, "eeprom")) {
return avr910_read_byte_eeprom(pgm, p, m, addr, value);
}

Expand Down Expand Up @@ -574,9 +574,9 @@ static int avr910_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR
{
int rval = 0;
if (PDATA(pgm)->use_blockmode == 0) {
if (strcmp(m->desc, "flash") == 0) {
if (str_eq(m->desc, "flash")) {
rval = avr910_paged_write_flash(pgm, p, m, page_size, addr, n_bytes);
} else if (strcmp(m->desc, "eeprom") == 0) {
} else if (str_eq(m->desc, "eeprom")) {
rval = avr910_paged_write_eeprom(pgm, p, m, page_size, addr, n_bytes);
} else {
rval = -2;
Expand All @@ -589,7 +589,7 @@ static int avr910_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVR
unsigned int blocksize = PDATA(pgm)->buffersize;
int wr_size;

if (strcmp(m->desc, "flash") && strcmp(m->desc, "eeprom"))
if (!str_eq(m->desc, "flash") && !str_eq(m->desc, "eeprom"))
return -2;

if (m->desc[0] == 'e') {
Expand Down Expand Up @@ -640,10 +640,10 @@ static int avr910_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRM

max_addr = addr + n_bytes;

if (strcmp(m->desc, "flash") == 0) {
if (str_eq(m->desc, "flash")) {
cmd[0] = 'R';
rd_size = 2; /* read two bytes per addr */
} else if (strcmp(m->desc, "eeprom") == 0) {
} else if (str_eq(m->desc, "eeprom")) {
cmd[0] = 'd';
rd_size = 1;
} else {
Expand Down
36 changes: 18 additions & 18 deletions src/avrftdi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1253,9 +1253,9 @@ static int avrftdi_flash_read(const PROGRAMMER *pgm, const AVRPART *p, const AVR
static int avrftdi_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m,
unsigned int page_size, unsigned int addr, unsigned int n_bytes)
{
if (strcmp(m->desc, "flash") == 0)
if (str_eq(m->desc, "flash"))
return avrftdi_flash_write(pgm, p, m, page_size, addr, n_bytes);
else if (strcmp(m->desc, "eeprom") == 0)
else if (str_eq(m->desc, "eeprom"))
return avrftdi_eeprom_write(pgm, p, m, page_size, addr, n_bytes);
else
return -2;
Expand All @@ -1264,9 +1264,9 @@ static int avrftdi_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV
static int avrftdi_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m,
unsigned int page_size, unsigned int addr, unsigned int n_bytes)
{
if (strcmp(m->desc, "flash") == 0)
if (str_eq(m->desc, "flash"))
return avrftdi_flash_read(pgm, p, m, page_size, addr, n_bytes);
else if(strcmp(m->desc, "eeprom") == 0)
else if(str_eq(m->desc, "eeprom"))
return avrftdi_eeprom_read(pgm, p, m, page_size, addr, n_bytes);
else
return -2;
Expand Down Expand Up @@ -1541,39 +1541,39 @@ static int avrftdi_jtag_chip_erase(const PROGRAMMER *pgm, const AVRPART *p)
static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p,
const AVRMEM *m, unsigned long addr, unsigned char *value)
{
if (strcmp(m->desc, "lfuse") == 0) {
if (str_eq(m->desc, "lfuse")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_READ, 15);

/* Read fuse low byte */
avrftdi_jtag_dr_out(pgm, 0x3200, 15);
*value = avrftdi_jtag_dr_inout(pgm, 0x3300, 15) & 0xff;

} else if (strcmp(m->desc, "hfuse") == 0) {
} else if (str_eq(m->desc, "hfuse")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_READ, 15);

/* Read fuse high byte */
avrftdi_jtag_dr_out(pgm, 0x3e00, 15);
*value = avrftdi_jtag_dr_inout(pgm, 0x3f00, 15) & 0xff;

} else if (strcmp(m->desc, "efuse") == 0) {
} else if (str_eq(m->desc, "efuse")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_READ, 15);

/* Read fuse extended byte */
avrftdi_jtag_dr_out(pgm, 0x3a00, 15);
*value = avrftdi_jtag_dr_inout(pgm, 0x3b00, 15) & 0xff;

} else if (strcmp(m->desc, "lock") == 0) {
} else if (str_eq(m->desc, "lock")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_READ, 15);

/* Read lock bits byte */
avrftdi_jtag_dr_out(pgm, 0x3600, 15);
*value = avrftdi_jtag_dr_inout(pgm, 0x3700, 15) & 0xff;

} else if (strcmp(m->desc, "signature") == 0) {
} else if (str_eq(m->desc, "signature")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_SIGCAL_READ, 15);
avrftdi_jtag_dr_out(pgm, 0x0300 | (addr & 0xff), 15);
Expand All @@ -1582,7 +1582,7 @@ static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p,
avrftdi_jtag_dr_out(pgm, 0x3200, 15);
*value = avrftdi_jtag_dr_inout(pgm, 0x3300, 15) & 0xff;

} else if (strcmp(m->desc, "calibration") == 0) {
} else if (str_eq(m->desc, "calibration")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_SIGCAL_READ, 15);
avrftdi_jtag_dr_out(pgm, 0x0300 | (addr & 0xff), 15);
Expand All @@ -1601,7 +1601,7 @@ static int avrftdi_jtag_read_byte(const PROGRAMMER *pgm, const AVRPART *p,
static int avrftdi_jtag_write_byte(const PROGRAMMER *pgm, const AVRPART *p,
const AVRMEM *m, unsigned long addr, unsigned char value)
{
if (strcmp(m->desc, "lfuse") == 0) {
if (str_eq(m->desc, "lfuse")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_WRITE, 15);

Expand All @@ -1618,7 +1618,7 @@ static int avrftdi_jtag_write_byte(const PROGRAMMER *pgm, const AVRPART *p,
while (!(avrftdi_jtag_dr_inout(pgm, 0x3300, 15) & 0x0200))
;

} else if (strcmp(m->desc, "hfuse") == 0) {
} else if (str_eq(m->desc, "hfuse")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_WRITE, 15);

Expand All @@ -1635,7 +1635,7 @@ static int avrftdi_jtag_write_byte(const PROGRAMMER *pgm, const AVRPART *p,
while (!(avrftdi_jtag_dr_inout(pgm, 0x3700, 15) & 0x0200))
;

} else if (strcmp(m->desc, "efuse") == 0) {
} else if (str_eq(m->desc, "efuse")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FUSE_WRITE, 15);

Expand All @@ -1652,7 +1652,7 @@ static int avrftdi_jtag_write_byte(const PROGRAMMER *pgm, const AVRPART *p,
while (!(avrftdi_jtag_dr_inout(pgm, 0x3b00, 15) & 0x0200))
;

} else if (strcmp(m->desc, "lock") == 0) {
} else if (str_eq(m->desc, "lock")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_LOCK_WRITE, 15);

Expand Down Expand Up @@ -1683,7 +1683,7 @@ static int avrftdi_jtag_paged_write(const PROGRAMMER *pgm, const AVRPART *p,
unsigned int maxaddr = addr + n_bytes;
unsigned char byte;

if (strcmp(m->desc, "flash") == 0) {
if (str_eq(m->desc, "flash")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FLASH_WRITE, 15);

Expand All @@ -1710,7 +1710,7 @@ static int avrftdi_jtag_paged_write(const PROGRAMMER *pgm, const AVRPART *p,
while (!(avrftdi_jtag_dr_inout(pgm, 0x3700, 15) & 0x0200))
;

} else if (strcmp(m->desc, "eeprom") == 0) {
} else if (str_eq(m->desc, "eeprom")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_EEPROM_WRITE, 15);

Expand Down Expand Up @@ -1758,7 +1758,7 @@ static int avrftdi_jtag_paged_read(const PROGRAMMER *pgm, const AVRPART *p,
buf = alloca(n_bytes * 8 + 1);
ptr = buf;

if (strcmp(m->desc, "flash") == 0) {
if (str_eq(m->desc, "flash")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_FLASH_READ, 15);

Expand Down Expand Up @@ -1798,7 +1798,7 @@ static int avrftdi_jtag_paged_read(const PROGRAMMER *pgm, const AVRPART *p,
m->buf[addr + i] = (buf[i * 2] >> 1) | (buf[(i * 2) + 1] << 2);
}

} else if (strcmp(m->desc, "eeprom") == 0) {
} else if (str_eq(m->desc, "eeprom")) {
avrftdi_jtag_ir_out(pgm, JTAG_IR_PROG_COMMANDS);
avrftdi_jtag_dr_out(pgm, 0x2300 | JTAG_DR_PROG_EEPROM_READ, 15);

Expand Down
8 changes: 4 additions & 4 deletions src/avrpart.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ int avr_set_addr_mem(const AVRMEM *mem, int opnum, unsigned char *cmd, unsigned
if(!(op = mem->op[opnum]))
return -1;

isflash = !strcmp(mem->desc, "flash"); // ISP parts have only one flash-like memory
isflash = str_eq(mem->desc, "flash"); // ISP parts have only one flash-like memory
memsize = mem->size >> isflash; // word addresses for flash
pagesize = mem->page_size >> isflash;

Expand Down Expand Up @@ -434,7 +434,7 @@ AVRMEM_ALIAS *avr_locate_memalias(const AVRPART *p, const char *desc) {
match = NULL;
for (ln=lfirst(p->mem_alias); ln; ln=lnext(ln)) {
m = ldata(ln);
if(l && strncmp(m->desc, desc, l) == 0) { // Partial initial match
if(l && str_starts(m->desc, desc)) { // Partial initial match
match = m;
matches++;
if(m->desc[l] == 0) // Exact match; return straight away
Expand All @@ -459,7 +459,7 @@ AVRMEM *avr_locate_mem_noalias(const AVRPART *p, const char *desc) {
match = NULL;
for (ln=lfirst(p->mem); ln; ln=lnext(ln)) {
m = ldata(ln);
if(l && strncmp(m->desc, desc, l) == 0) { // Partial initial match
if(l && str_starts(m->desc, desc)) { // Partial initial match
match = m;
matches++;
if(m->desc[l] == 0) // Exact match; return straight away
Expand Down Expand Up @@ -511,7 +511,7 @@ void avr_mem_display(const char *prefix, FILE *f, const AVRMEM *m,

if (m != NULL) {
// Only print memory section if the previous section printed isn't identical
if(prev_mem_offset != m->offset || prev_mem_size != m->size || (strcmp(p->family_id, "") == 0)) {
if(prev_mem_offset != m->offset || prev_mem_size != m->size || str_eq(p->family_id, "")) {
prev_mem_offset = m->offset;
prev_mem_size = m->size;
AVRMEM_ALIAS *ap = avr_find_memalias(p, m);
Expand Down
27 changes: 13 additions & 14 deletions src/buspirate.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ static int buspirate_send(const PROGRAMMER *pgm, const char *str) {
if (readline == NULL)
return -1;
/* keep reading until we get what we sent there */
} while (strcmp(readline, str) != 0);
} while (!str_eq(readline, str));

/* by now we should be in sync */
return 0;
Expand All @@ -251,7 +251,6 @@ static int buspirate_expect(const PROGRAMMER *pgm, char *send,
char *expect, int wait_for_prompt)
{
int got_it = 0;
size_t expect_len = strlen(expect);
char *rcvd;

buspirate_send(pgm, send);
Expand All @@ -260,7 +259,7 @@ static int buspirate_expect(const PROGRAMMER *pgm, char *send,
if (rcvd == NULL) {
return -1;
}
if (strncmp(rcvd, expect, expect_len) == 0) {
if (str_starts(rcvd, expect)) {
if (! wait_for_prompt) {
serial_drain(&pgm->fd, 0);
return 1;
Expand Down Expand Up @@ -293,7 +292,7 @@ buspirate_parseextparms(const PROGRAMMER *pgm, const LISTID extparms) {

for (ln = lfirst(extparms); ln; ln = lnext(ln)) {
extended_param = ldata(ln);
if (strcmp(extended_param, "ascii") == 0) {
if (str_eq(extended_param, "ascii")) {
PDATA(pgm)->flag |= BP_FLAG_XPARM_FORCE_ASCII;
continue;
}
Expand Down Expand Up @@ -343,11 +342,11 @@ buspirate_parseextparms(const PROGRAMMER *pgm, const LISTID extparms) {
char *resetpin;
while ((resetpin = strtok(preset, ","))) {
preset = NULL; /* for subsequent strtok() calls */
if (strcasecmp(resetpin, "cs") == 0)
if (str_caseeq(resetpin, "cs"))
PDATA(pgm)->reset |= BP_RESET_CS;
else if (strcasecmp(resetpin, "aux") == 0 || strcasecmp(reset, "aux1") == 0)
else if (str_caseeq(resetpin, "aux") || str_caseeq(reset, "aux1"))
PDATA(pgm)->reset |= BP_RESET_AUX;
else if (strcasecmp(resetpin, "aux2") == 0)
else if (str_caseeq(resetpin, "aux2"))
PDATA(pgm)->reset |= BP_RESET_AUX2;
else {
pmsg_error("reset must be either CS or AUX\n");
Expand Down Expand Up @@ -651,7 +650,7 @@ static int buspirate_start_spi_mode_ascii(const PROGRAMMER *pgm) {
return -1;
}
if (spi_cmd == -1 && sscanf(rcvd, "%2d. %10s", &cmd, mode)) {
if (strcmp(mode, "SPI") == 0)
if (str_eq(mode, "SPI"))
spi_cmd = cmd;
}
if (buspirate_is_prompt(rcvd))
Expand All @@ -670,15 +669,15 @@ static int buspirate_start_spi_mode_ascii(const PROGRAMMER *pgm) {
if (rcvd == NULL) {
return -1;
}
if (strstr(rcvd, "Normal (H=3.3V, L=GND)")) {
if (str_contains(rcvd, "Normal (H=3.3V, L=GND)")) {
/* BP firmware 2.1 defaults to Open-drain output.
* That doesn't work on my board, even with pull-up
* resistors. Select 3.3V output mode instead. */
sscanf(rcvd, " %2d.", &cmd);
snprintf(buf, sizeof(buf), "%d\n", cmd);
}
if (buspirate_is_prompt(rcvd)) {
if (strncmp(rcvd, "SPI>", 4) == 0) {
if (str_starts(rcvd, "SPI>")) {
msg_info("BusPirate is now configured for SPI\n");
break;
}
Expand Down Expand Up @@ -736,10 +735,10 @@ static void buspirate_enable(PROGRAMMER *pgm, const AVRPART *p) {
pmsg_error("programmer is not responding\n");
return;
}
if (strncmp(rcvd, "Are you sure?", 13) == 0) {
if (str_starts(rcvd, "Are you sure?")) {
buspirate_send_bin(pgm, (const unsigned char*)accept_str, strlen(accept_str));
}
if (strncmp(rcvd, "RESET", 5) == 0) {
if (str_starts(rcvd, "RESET")) {
print_banner = 1;
continue;
}
Expand Down Expand Up @@ -903,7 +902,7 @@ static int buspirate_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const A
}

// determine what type of memory to read, only flash is supported
if (strcmp(m->desc, "flash") != 0) {
if (!str_eq(m->desc, "flash")) {
return -1;
}

Expand Down Expand Up @@ -964,7 +963,7 @@ static int buspirate_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const
return -1;
}

if (strcmp(m->desc,"flash") != 0) {
if (!str_eq(m->desc,"flash")) {
/* Only flash memory currently supported. */
return -1;
}
Expand Down
Loading