Make fileio() and do_op() arguments const where possible

This commit is contained in:
Stefan Rueger 2022-10-29 10:45:27 +01:00
parent a1018999c4
commit 87df3216cb
No known key found for this signature in database
GPG Key ID: B0B4F1FD86B1EC55
3 changed files with 89 additions and 85 deletions

View File

@ -57,48 +57,48 @@ struct ihexrec {
}; };
static int b2ihex(unsigned char * inbuf, int bufsize, static int b2ihex(const unsigned char *inbuf, int bufsize,
int recsize, int startaddr, int recsize, int startaddr,
char * outfile, FILE * outf, FILEFMT ffmt); const char *outfile, FILE *outf, FILEFMT ffmt);
static int ihex2b(char * infile, FILE * inf, static int ihex2b(const char *infile, FILE *inf,
AVRMEM * mem, int bufsize, unsigned int fileoffset, const AVRMEM *mem, int bufsize, unsigned int fileoffset,
FILEFMT ffmt); FILEFMT ffmt);
static int b2srec(unsigned char * inbuf, int bufsize, static int b2srec(const unsigned char *inbuf, int bufsize,
int recsize, int startaddr, int recsize, int startaddr,
char * outfile, FILE * outf); const char *outfile, FILE *outf);
static int srec2b(char * infile, FILE * inf, static int srec2b(const char *infile, FILE *inf,
AVRMEM * mem, int bufsize, unsigned int fileoffset); const AVRMEM *mem, int bufsize, unsigned int fileoffset);
static int ihex_readrec(struct ihexrec * ihex, char * rec); static int ihex_readrec(struct ihexrec *ihex, char *rec);
static int srec_readrec(struct ihexrec * srec, char * rec); static int srec_readrec(struct ihexrec *srec, char *rec);
static int fileio_rbin(struct fioparms * fio, static int fileio_rbin(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, int size); const char *filename, FILE *f, const AVRMEM *mem, int size);
static int fileio_ihex(struct fioparms * fio, static int fileio_ihex(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, int size, const char *filename, FILE *f, const AVRMEM *mem, int size,
FILEFMT ffmt); FILEFMT ffmt);
static int fileio_srec(struct fioparms * fio, static int fileio_srec(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, int size); const char *filename, FILE *f, const AVRMEM *mem, int size);
#ifdef HAVE_LIBELF #ifdef HAVE_LIBELF
static int elf2b(char * infile, FILE * inf, static int elf2b(const char *infile, FILE *inf,
AVRMEM * mem, struct avrpart * p, const AVRMEM *mem, const AVRPART *p,
int bufsize, unsigned int fileoffset); int bufsize, unsigned int fileoffset);
static int fileio_elf(struct fioparms * fio, static int fileio_elf(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, const char *filename, FILE *f, const AVRMEM *mem,
struct avrpart * p, int size); const AVRPART *p, int size);
#endif #endif
static int fileio_num(struct fioparms * fio, static int fileio_num(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, int size, const char *filename, FILE *f, const AVRMEM *mem, int size,
FILEFMT fmt); FILEFMT fmt);
char * fileio_fmtstr(FILEFMT format) char * fileio_fmtstr(FILEFMT format)
@ -115,11 +115,11 @@ char * fileio_fmtstr(FILEFMT format)
} }
static int b2ihex(unsigned char * inbuf, int bufsize, static int b2ihex(const unsigned char *inbuf, int bufsize,
int recsize, int startaddr, int recsize, int startaddr,
char * outfile, FILE * outf, FILEFMT ffmt) const char *outfile, FILE *outf, FILEFMT ffmt)
{ {
unsigned char * buf; const unsigned char *buf;
unsigned int nextaddr; unsigned int nextaddr;
int n, nbytes, n_64k; int n, nbytes, n_64k;
int i; int i;
@ -292,8 +292,8 @@ static int ihex_readrec(struct ihexrec * ihex, char * rec)
* If an error occurs, return -1. * If an error occurs, return -1.
* *
* */ * */
static int ihex2b(char * infile, FILE * inf, static int ihex2b(const char *infile, FILE *inf,
AVRMEM * mem, int bufsize, unsigned int fileoffset, const AVRMEM *mem, int bufsize, unsigned int fileoffset,
FILEFMT ffmt) FILEFMT ffmt)
{ {
char buffer [ MAX_LINE_LEN ]; char buffer [ MAX_LINE_LEN ];
@ -394,11 +394,11 @@ static int ihex2b(char * infile, FILE * inf,
} }
} }
static int b2srec(unsigned char * inbuf, int bufsize, static int b2srec(const unsigned char *inbuf, int bufsize,
int recsize, int startaddr, int recsize, int startaddr,
char * outfile, FILE * outf) const char *outfile, FILE *outf)
{ {
unsigned char * buf; const unsigned char *buf;
unsigned int nextaddr; unsigned int nextaddr;
int n, nbytes, addr_width; int n, nbytes, addr_width;
unsigned char cksum; unsigned char cksum;
@ -516,9 +516,9 @@ static int srec_readrec(struct ihexrec * srec, char * rec)
return -1; return -1;
srec->rectyp = rec[offset++]; srec->rectyp = rec[offset++];
if (srec->rectyp == 0x32 || srec->rectyp == 0x38) if (srec->rectyp == 0x32 || srec->rectyp == 0x38)
addr_width = 3; /* S2,S8-record */ addr_width = 3; /* S2,S8-record */
else if (srec->rectyp == 0x33 || srec->rectyp == 0x37) else if (srec->rectyp == 0x33 || srec->rectyp == 0x37)
addr_width = 4; /* S3,S7-record */ addr_width = 4; /* S3,S7-record */
/* reclen */ /* reclen */
if (offset + 2 > len) if (offset + 2 > len)
@ -573,8 +573,8 @@ static int srec_readrec(struct ihexrec * srec, char * rec)
} }
static int srec2b(char * infile, FILE * inf, static int srec2b(const char *infile, FILE * inf,
AVRMEM * mem, int bufsize, unsigned int fileoffset) const AVRMEM *mem, int bufsize, unsigned int fileoffset)
{ {
char buffer [ MAX_LINE_LEN ]; char buffer [ MAX_LINE_LEN ];
unsigned int nextaddr, maxaddr; unsigned int nextaddr, maxaddr;
@ -741,7 +741,7 @@ static Elf_Scn *elf_get_scn(Elf *e, Elf32_Phdr *ph, Elf32_Shdr **shptr)
return NULL; return NULL;
} }
static int elf_mem_limits(AVRMEM *mem, struct avrpart * p, static int elf_mem_limits(const AVRMEM *mem, const AVRPART *p,
unsigned int *lowbound, unsigned int *lowbound,
unsigned int *highbound, unsigned int *highbound,
unsigned int *fileoff) unsigned int *fileoff)
@ -799,8 +799,8 @@ static int elf_mem_limits(AVRMEM *mem, struct avrpart * p,
} }
static int elf2b(char * infile, FILE * inf, static int elf2b(const char *infile, FILE *inf,
AVRMEM * mem, struct avrpart * p, const AVRMEM *mem, const AVRPART *p,
int bufsize, unsigned int fileoffset) int bufsize, unsigned int fileoffset)
{ {
Elf *e; Elf *e;
@ -1039,9 +1039,8 @@ static char *itoa_simple(int n, char *buf, int base)
static int fileio_rbin(struct fioparms * fio, static int fileio_rbin(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, int size) const char *filename, FILE *f, const AVRMEM *mem, int size) {
{
int rc; int rc;
unsigned char *buf = mem->buf; unsigned char *buf = mem->buf;
@ -1069,26 +1068,29 @@ static int fileio_rbin(struct fioparms * fio,
} }
static int fileio_imm(struct fioparms * fio, static int fileio_imm(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, int size) const char *fname, FILE *f, const AVRMEM *mem, int size)
{ {
int rc = 0; int rc = 0;
char * e, * p; char *e, *p, *filename;
unsigned long b; unsigned long b;
int loc; int loc;
filename = cfg_strdup(__func__, fname);
switch (fio->op) { switch (fio->op) {
case FIO_READ: case FIO_READ:
loc = 0; loc = 0;
p = strtok(filename, " ,"); p = strtok(filename, " ,");
while (p != NULL && loc < size) { while (p != NULL && loc < size) {
b = strtoul(p, &e, 0); b = strtoul(p, &e, 0);
/* check for binary formatted (0b10101001) strings */ /* check for binary formatted (0b10101001) strings */
b = (strncmp (p, "0b", 2))? b = (strncmp (p, "0b", 2))?
strtoul (p, &e, 0): strtoul (p, &e, 0):
strtoul (p + 2, &e, 2); strtoul (p + 2, &e, 2);
if (*e != 0) { if (*e != 0) {
pmsg_error("invalid byte value (%s) specified for immediate mode\n", p); pmsg_error("invalid byte value (%s) specified for immediate mode\n", p);
free(filename);
return -1; return -1;
} }
mem->buf[loc] = b; mem->buf[loc] = b;
@ -1100,26 +1102,30 @@ static int fileio_imm(struct fioparms * fio,
case FIO_WRITE: case FIO_WRITE:
pmsg_error("invalid file format 'immediate' for output\n"); pmsg_error("invalid file format 'immediate' for output\n");
free(filename);
return -1; return -1;
default: default:
pmsg_error("invalid operation=%d\n", fio->op); pmsg_error("invalid operation=%d\n", fio->op);
free(filename);
return -1; return -1;
} }
if (rc < 0 || (fio->op == FIO_WRITE && rc < size)) { if (rc < 0 || (fio->op == FIO_WRITE && rc < size)) {
pmsg_ext_error("%s error %s %s: %s; %s %d of the expected %d bytes\n", pmsg_ext_error("%s error %s %s: %s; %s %d of the expected %d bytes\n",
fio->iodesc, fio->dir, filename, strerror(errno), fio->rw, rc, size); fio->iodesc, fio->dir, filename, strerror(errno), fio->rw, rc, size);
free(filename);
return -1; return -1;
} }
free(filename);
return rc; return rc;
} }
static int fileio_ihex(struct fioparms * fio, static int fileio_ihex(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, int size, const char *filename, FILE *f, const AVRMEM *mem, int size,
FILEFMT ffmt) FILEFMT ffmt)
{ {
int rc; int rc;
@ -1147,8 +1153,8 @@ static int fileio_ihex(struct fioparms * fio,
} }
static int fileio_srec(struct fioparms * fio, static int fileio_srec(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, int size) const char *filename, FILE *f, const AVRMEM *mem, int size)
{ {
int rc; int rc;
@ -1177,9 +1183,9 @@ static int fileio_srec(struct fioparms * fio,
#ifdef HAVE_LIBELF #ifdef HAVE_LIBELF
static int fileio_elf(struct fioparms * fio, static int fileio_elf(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, const char *filename, FILE *f, const AVRMEM *mem,
struct avrpart * p, int size) const AVRPART *p, int size)
{ {
int rc; int rc;
@ -1202,9 +1208,9 @@ static int fileio_elf(struct fioparms * fio,
#endif #endif
static int fileio_num(struct fioparms * fio, static int fileio_num(struct fioparms *fio,
char * filename, FILE * f, AVRMEM * mem, int size, const char *filename, FILE *f, const AVRMEM *mem, int size,
FILEFMT fmt) FILEFMT fmt)
{ {
const char *prefix; const char *prefix;
const char *name; const char *name;
@ -1255,7 +1261,7 @@ static int fileio_num(struct fioparms * fio,
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
if (i > 0) { if (i > 0) {
if (putc(',', f) == EOF) if (putc(',', f) == EOF)
goto writeerr; goto writeerr;
} }
num = (unsigned int)(mem->buf[i]); num = (unsigned int)(mem->buf[i]);
/* /*
@ -1265,7 +1271,7 @@ static int fileio_num(struct fioparms * fio,
*/ */
if (prefix[0] != '\0' && !(base == 8 && num < 8)) { if (prefix[0] != '\0' && !(base == 8 && num < 8)) {
if (fputs(prefix, f) == EOF) if (fputs(prefix, f) == EOF)
goto writeerr; goto writeerr;
} }
itoa_simple(num, cbuf, base); itoa_simple(num, cbuf, base);
if (fputs(cbuf, f) == EOF) if (fputs(cbuf, f) == EOF)
@ -1282,9 +1288,7 @@ static int fileio_num(struct fioparms * fio,
} }
int fileio_setparms(int op, struct fioparms * fp, int fileio_setparms(int op, struct fioparms *fp, const AVRPART *p, const AVRMEM * m) {
struct avrpart * p, AVRMEM * m)
{
fp->op = op; fp->op = op;
switch (op) { switch (op) {
@ -1404,12 +1408,12 @@ int fileio_fmt_autodetect(const char * fname)
int fileio(int oprwv, char * filename, FILEFMT format, int fileio(int oprwv, const char *filename, FILEFMT format,
struct avrpart * p, char * memtype, int size) const AVRPART *p, const char *memtype, int size)
{ {
int op, rc; int op, rc;
FILE * f; FILE * f;
char * fname; const char *fname;
struct fioparms fio; struct fioparms fio;
AVRMEM * mem; AVRMEM * mem;
int using_stdio; int using_stdio;

View File

@ -579,7 +579,8 @@ const char * pinmask_to_str(const pinmask_t * const pinmask);
The target file will be selected at configure time. */ The target file will be selected at configure time. */
extern long serial_recv_timeout; extern long serial_recv_timeout; /* ms */
union filedescriptor union filedescriptor
{ {
int ifd; int ifd;
@ -797,7 +798,6 @@ typedef struct programmer_t {
int (*parseextparams) (const struct programmer_t *pgm, const LISTID xparams); int (*parseextparams) (const struct programmer_t *pgm, const LISTID xparams);
void (*setup) (struct programmer_t *pgm); void (*setup) (struct programmer_t *pgm);
void (*teardown) (struct programmer_t *pgm); void (*teardown) (struct programmer_t *pgm);
// Cached r/w API for terminal reads/writes // Cached r/w API for terminal reads/writes
int (*write_byte_cached)(const struct programmer_t *pgm, const AVRPART *p, const AVRMEM *m, int (*write_byte_cached)(const struct programmer_t *pgm, const AVRPART *p, const AVRMEM *m,
unsigned long addr, unsigned char value); unsigned long addr, unsigned char value);
@ -969,8 +969,8 @@ char * fileio_fmtstr(FILEFMT format);
int fileio_fmt_autodetect(const char * fname); int fileio_fmt_autodetect(const char * fname);
int fileio(int oprwv, char * filename, FILEFMT format, int fileio(int oprwv, const char *filename, FILEFMT format,
struct avrpart * p, char * memtype, int size); const AVRPART *p, const char *memtype, int size);
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -1020,10 +1020,10 @@ extern UPDATE * dup_update(UPDATE * upd);
extern UPDATE * new_update(int op, char * memtype, int filefmt, extern UPDATE * new_update(int op, char * memtype, int filefmt,
char * filename); char * filename);
extern void free_update(UPDATE * upd); extern void free_update(UPDATE * upd);
extern int do_op(PROGRAMMER * pgm, struct avrpart * p, UPDATE * upd, extern int do_op(const PROGRAMMER *pgm, const AVRPART *p, UPDATE *upd,
enum updateflags flags); enum updateflags flags);
extern int memstats(struct avrpart *p, char *memtype, int size, Filestats *fsp); extern int memstats(const AVRPART *p, const char *memtype, int size, Filestats *fsp);
// Convenience functions for printing // Convenience functions for printing
const char *update_plural(int x); const char *update_plural(int x);
@ -1036,7 +1036,7 @@ int update_is_okfile(const char *fn);
int update_is_writeable(const char *fn); int update_is_writeable(const char *fn);
int update_is_readable(const char *fn); int update_is_readable(const char *fn);
int update_dryrun(struct avrpart *p, UPDATE *upd); int update_dryrun(const AVRPART *p, UPDATE *upd);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -188,7 +188,7 @@ void free_update(UPDATE * u)
// Memory statistics considering holes after a file read returned size bytes // Memory statistics considering holes after a file read returned size bytes
int memstats(struct avrpart *p, char *memtype, int size, Filestats *fsp) { int memstats(const AVRPART *p, const char *memtype, int size, Filestats *fsp) {
Filestats ret = { 0 }; Filestats ret = { 0 };
AVRMEM *mem = avr_locate_mem(p, memtype); AVRMEM *mem = avr_locate_mem(p, memtype);
@ -332,7 +332,7 @@ int update_is_readable(const char *fn) {
} }
static void ioerror(const char *iotype, UPDATE *upd) { static void ioerror(const char *iotype, const UPDATE *upd) {
int errnocp = errno; int errnocp = errno;
pmsg_ext_error("file %s is not %s: ", update_outname(upd->filename), iotype); pmsg_ext_error("file %s is not %s: ", update_outname(upd->filename), iotype);
@ -344,7 +344,7 @@ static void ioerror(const char *iotype, UPDATE *upd) {
} }
// Basic checks to reveal serious failure before programming // Basic checks to reveal serious failure before programming
int update_dryrun(struct avrpart *p, UPDATE *upd) { int update_dryrun(const AVRPART *p, UPDATE *upd) {
static char **wrote; static char **wrote;
static int nfwritten; static int nfwritten;
@ -426,10 +426,9 @@ int update_dryrun(struct avrpart *p, UPDATE *upd) {
} }
int do_op(PROGRAMMER * pgm, struct avrpart * p, UPDATE * upd, enum updateflags flags) int do_op(const PROGRAMMER *pgm, const AVRPART *p, UPDATE *upd, enum updateflags flags) {
{ AVRPART *v;
struct avrpart * v; AVRMEM *mem;
AVRMEM * mem;
int size; int size;
int rc; int rc;
Filestats fs; Filestats fs;
@ -486,6 +485,7 @@ int do_op(PROGRAMMER * pgm, struct avrpart * p, UPDATE * upd, enum updateflags f
pmsg_error("read from file %s failed\n", update_inname(upd->filename)); pmsg_error("read from file %s failed\n", update_inname(upd->filename));
return LIBAVRDUDE_GENERAL_FAILURE; return LIBAVRDUDE_GENERAL_FAILURE;
} }
size = rc; size = rc;
pmsg_info("reading input file %s for %s%s\n", pmsg_info("reading input file %s for %s%s\n",
update_inname(upd->filename), mem->desc, alias_mem_desc); update_inname(upd->filename), mem->desc, alias_mem_desc);