Move the error handling for invalid file formats to fileio.c

The checks used to be in update.c, but as they are related to
the intended file operation, they are better placed in fileio.c.

The checks affected are to refuse 'm' on output (file write),
and 'd', 'h', 'o', and 'b' formats on input (file read).
This commit is contained in:
Joerg Wunsch 2022-07-23 22:47:38 +02:00
parent ce1ae41dd6
commit cc93bd2c83
2 changed files with 19 additions and 34 deletions

View File

@ -1135,6 +1135,13 @@ static int fileio_imm(struct fioparms * fio,
rc = loc; rc = loc;
} }
break; break;
case FIO_WRITE:
avrdude_message(MSG_INFO,
"%s: Invalid file format 'immediate' for output\n",
progname);
return -1;
default: default:
avrdude_message(MSG_INFO, "%s: fileio: invalid operation=%d\n", avrdude_message(MSG_INFO, "%s: fileio: invalid operation=%d\n",
progname, fio->op); progname, fio->op);
@ -1248,27 +1255,32 @@ static int fileio_num(struct fioparms * fio,
FILEFMT fmt) FILEFMT fmt)
{ {
const char *prefix; const char *prefix;
const char *name;
char cbuf[20]; char cbuf[20];
int base, i, num; int base, i, num;
switch (fmt) { switch (fmt) {
case FMT_HEX: case FMT_HEX:
name = "hex";
prefix = "0x"; prefix = "0x";
base = 16; base = 16;
break; break;
default: default:
case FMT_DEC: case FMT_DEC:
name = "decimal";
prefix = ""; prefix = "";
base = 10; base = 10;
break; break;
case FMT_OCT: case FMT_OCT:
name = "octal";
prefix = "0"; prefix = "0";
base = 8; base = 8;
break; break;
case FMT_BIN: case FMT_BIN:
name = "binary";
prefix = "0b"; prefix = "0b";
base = 2; base = 2;
break; break;
@ -1278,6 +1290,13 @@ static int fileio_num(struct fioparms * fio,
switch (fio->op) { switch (fio->op) {
case FIO_WRITE: case FIO_WRITE:
break; break;
case FIO_READ:
avrdude_message(MSG_INFO,
"%s: Invalid file format '%s' for input\n",
progname, name);
return -1;
default: default:
avrdude_message(MSG_INFO, "%s: fileio: invalid operation=%d\n", avrdude_message(MSG_INFO, "%s: fileio: invalid operation=%d\n",
progname, fio->op); progname, fio->op);

View File

@ -238,12 +238,6 @@ int do_op(PROGRAMMER * pgm, struct avrpart * p, UPDATE * upd, enum updateflags f
/* /*
* read out the specified device memory and write it to a file * read out the specified device memory and write it to a file
*/ */
if (upd->format == FMT_IMM) {
avrdude_message(MSG_INFO,
"%s: Invalid file format 'immediate' for output\n",
progname, upd->filename);
return -1;
}
if (quell_progress < 2) { if (quell_progress < 2) {
avrdude_message(MSG_INFO, "%s: reading %s%s memory:\n", avrdude_message(MSG_INFO, "%s: reading %s%s memory:\n",
progname, mem->desc, alias_mem_desc); progname, mem->desc, alias_mem_desc);
@ -278,34 +272,6 @@ int do_op(PROGRAMMER * pgm, struct avrpart * p, UPDATE * upd, enum updateflags f
* write the selected device memory using data from a file; first * write the selected device memory using data from a file; first
* read the data from the specified file * read the data from the specified file
*/ */
const char *name = 0;
switch (upd->format) {
case FMT_HEX:
name = "hex";
break;
case FMT_DEC:
name = "decimal";
break;
case FMT_OCT:
name = "octal";
break;
case FMT_BIN:
name = "binary";
break;
default:
// no action needed
break;
}
if (name != 0) {
avrdude_message(MSG_INFO,
"%s: Invalid file format '%s' for input\n",
progname, name, upd->filename);
return -1;
}
if (quell_progress < 2) { if (quell_progress < 2) {
avrdude_message(MSG_INFO, "%s: reading input file \"%s\"\n", avrdude_message(MSG_INFO, "%s: reading input file \"%s\"\n",
progname, progname,