Browse Source

* backend test util : add parse_input()

master
Alex 'AdUser' Z 8 years ago
parent
commit
9003ce9110
  1. 46
      src/backend-test.c

46
src/backend-test.c

@ -14,6 +14,52 @@ void usage() {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* returns -1 on error, 0 for 'ban', 1 for 'check' and 3 for 'unban'
* also fills buf with second arg after command name
*/
int
parse_input(char *line, char *buf, size_t bufsize) {
const char *ops[] = { "ban", "check", "unban", NULL };
int code = 0;
assert(line != NULL);
assert(buf != NULL);
while (isspace(*line))
line++;
if (*line == '\0')
return -1; /* empty line */
for (size_t i = 0; ops[i] != NULL; i++) {
size_t len = strlen(ops[i]);
if (strncmp(line, ops[i], len) != 0)
continue;
code = i;
line += len;
break;
}
while (isspace(*line))
line++;
if (*line == '\0')
return -1; /* missing second arg */
for (char *p = line; *p != '\0'; p++) {
if ((*p >= 'a' && *p <= 'f') || *p == '.' || *p == ':' ||
(*p >= 'A' && *p <= 'F') || isdigit(*p))
continue; /* valid ipaddr char */
/* everything else */
*p = '\0';
break;
}
if (*line == '\0')
return -1; /* garbled second arg */
strlcpy(buf, line, bufsize);
return code;
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
const char *ip = "127.0.0.17"; const char *ip = "127.0.0.17";
f2b_config_t config; f2b_config_t config;

Loading…
Cancel
Save