dediprog: Revise prefix check for given programmer id
The SF600Plus-G2 uses a new prefix `S6B' for the serial number. Refactor
the code to ease more prefix additions and add this one.
Change-Id: I10a57446041352e391462a3bef42f570720a8d50
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/105
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
diff --git a/dediprog.c b/dediprog.c
index 247f9de..5c9a4c0 100644
--- a/dediprog.c
+++ b/dediprog.c
@@ -1181,7 +1181,7 @@
char *voltage, *id_str, *device, *spispeed, *target_str;
int spispeed_idx = 1;
int millivolt = 3500;
- int id = -1; /* -1 defaults to enumeration order */
+ long id = -1; /* -1 defaults to enumeration order */
int found_id;
long usedevice = 0;
long target = FLASH_TYPE_APPLICATION_FLASH_1;
@@ -1214,20 +1214,30 @@
id_str = extract_programmer_param("id");
if (id_str) {
- char prefix0, prefix1;
- if (sscanf(id_str, "%c%c%d", &prefix0, &prefix1, &id) != 3) {
- msg_perr("Error: Could not parse dediprog 'id'.\n");
- msg_perr("Expected a string like SF012345 or DP012345.\n");
+ const char prefixes[][4] = { "SF", "DP", "S6B", };
+ for (i = 0; i < (int)ARRAY_SIZE(prefixes); ++i) {
+ if (!strncmp(id_str, prefixes[i], strlen(prefixes[i])))
+ break;
+ }
+ if (i >= (int)ARRAY_SIZE(prefixes)) {
+ msg_perr("Error: Could not parse dediprog `id'.\n");
+ msg_perr("Expected a string prefixed with any of ");
+ for (i = 0; i < (int)ARRAY_SIZE(prefixes); ++i)
+ msg_perr("%s`%s'", i > 0 ? ", " : "", prefixes[i]);
+ msg_perr(".\n");
+ free(id_str);
+ return 1;
+ }
+ char *endptr;
+ id = strtol(id_str + strlen(prefixes[i]), &endptr, 10);
+ if (strlen(id_str) <= strlen(prefixes[i]) || *endptr) {
+ msg_perr("Error: Could not parse dediprog `id'.\n");
+ msg_perr("Expected a number after string prefix.\n");
free(id_str);
return 1;
}
if (id < 0 || id >= 0x1000000) {
- msg_perr("Error: id %s is out of range!\n", id_str);
- free(id_str);
- return 1;
- }
- if (!(prefix0 == 'S' && prefix1 == 'F') && !(prefix0 == 'D' && prefix1 == 'P')) {
- msg_perr("Error: %s is an invalid id!\n", id_str);
+ msg_perr("Error: id `%s' is out of range!\n", id_str);
free(id_str);
return 1;
}