|
@@ -16,6 +16,7 @@
|
|
|
#include <curses.h>
|
|
#include <curses.h>
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
|
|
+#include <ctype.h>
|
|
|
#include <limits.h>
|
|
#include <limits.h>
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
@@ -750,23 +751,41 @@ static char *add_serial(char *arg)
|
|
|
|
|
|
|
|
bool get_intrange(const char *arg, int *val1, int *val2)
|
|
bool get_intrange(const char *arg, int *val1, int *val2)
|
|
|
{
|
|
{
|
|
|
- int pos, n;
|
|
|
|
|
- // Is is unclear whether %n is counted in the returned value, so %n is doubled up to make 2 unambiguous
|
|
|
|
|
- n = sscanf(arg, "%d%n%n -%d %n", val1, &pos, &pos, val2, &pos);
|
|
|
|
|
- switch (n)
|
|
|
|
|
|
|
+ // NOTE: This could be done with sscanf, but its %n is broken in strange ways on Windows
|
|
|
|
|
+ char *p, *p2;
|
|
|
|
|
+
|
|
|
|
|
+ *val1 = strtol(arg, &p, 0);
|
|
|
|
|
+ if (arg == p)
|
|
|
|
|
+ // Zero-length ending number, invalid
|
|
|
|
|
+ return false;
|
|
|
|
|
+ while (true)
|
|
|
{
|
|
{
|
|
|
- case 1: // %n not counted (only one number)
|
|
|
|
|
- case 3: // %n counted
|
|
|
|
|
|
|
+ if (!p[0])
|
|
|
|
|
+ {
|
|
|
*val2 = *val1;
|
|
*val2 = *val1;
|
|
|
- case 2: // %n not counted (two numbers)
|
|
|
|
|
- case 5: // %n counted
|
|
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (p[0] == '-')
|
|
|
break;
|
|
break;
|
|
|
- default:
|
|
|
|
|
|
|
+ if (!isspace(p[0]))
|
|
|
|
|
+ // Garbage, invalid
|
|
|
return false;
|
|
return false;
|
|
|
|
|
+ ++p;
|
|
|
}
|
|
}
|
|
|
- if (unlikely(arg[pos]))
|
|
|
|
|
|
|
+ p2 = &p[1];
|
|
|
|
|
+ *val2 = strtol(p2, &p, 0);
|
|
|
|
|
+ if (p2 == p)
|
|
|
|
|
+ // Zero-length ending number, invalid
|
|
|
return false;
|
|
return false;
|
|
|
- return true;
|
|
|
|
|
|
|
+ while (true)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (!p[0])
|
|
|
|
|
+ return true;
|
|
|
|
|
+ if (!isspace(p[0]))
|
|
|
|
|
+ // Garbage, invalid
|
|
|
|
|
+ return false;
|
|
|
|
|
+ ++p;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static
|
|
static
|