cli: Share a common tokenize_cmd() implementation

wpa_cli and hostapd_cli had identical copies of this function.

Signed-off-by: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2016-08-06 12:46:39 +03:00
parent a6d56a3965
commit ac81b3948b
4 changed files with 33 additions and 63 deletions

View file

@ -1478,38 +1478,6 @@ static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
} }
#define max_args 10
static int tokenize_cmd(char *cmd, char *argv[])
{
char *pos;
int argc = 0;
pos = cmd;
for (;;) {
while (*pos == ' ')
pos++;
if (*pos == '\0')
break;
argv[argc] = pos;
argc++;
if (argc == max_args)
break;
if (*pos == '"') {
char *pos2 = os_strrchr(pos, '"');
if (pos2)
pos = pos2 + 1;
}
while (*pos != '\0' && *pos != ' ')
pos++;
if (*pos == ' ')
*pos++ = '\0';
}
return argc;
}
static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx) static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
{ {
if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) { if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {

View file

@ -235,3 +235,33 @@ fail:
printf("Too long command\n"); printf("Too long command\n");
return -1; return -1;
} }
int tokenize_cmd(char *cmd, char *argv[])
{
char *pos;
int argc = 0;
pos = cmd;
for (;;) {
while (*pos == ' ')
pos++;
if (*pos == '\0')
break;
argv[argc] = pos;
argc++;
if (argc == max_args)
break;
if (*pos == '"') {
char *pos2 = os_strrchr(pos, '"');
if (pos2)
pos = pos2 + 1;
}
while (*pos != '\0' && *pos != ' ')
pos++;
if (*pos == ' ')
*pos++ = '\0';
}
return argc;
}

View file

@ -41,4 +41,7 @@ int get_cmd_arg_num(const char *str, int pos);
int write_cmd(char *buf, size_t buflen, const char *cmd, int argc, int write_cmd(char *buf, size_t buflen, const char *cmd, int argc,
char *argv[]); char *argv[]);
#define max_args 10
int tokenize_cmd(char *cmd, char *argv[]);
#endif /* CLI_H */ #endif /* CLI_H */

View file

@ -3808,37 +3808,6 @@ static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int action_monitor)
} }
} }
#define max_args 10
static int tokenize_cmd(char *cmd, char *argv[])
{
char *pos;
int argc = 0;
pos = cmd;
for (;;) {
while (*pos == ' ')
pos++;
if (*pos == '\0')
break;
argv[argc] = pos;
argc++;
if (argc == max_args)
break;
if (*pos == '"') {
char *pos2 = os_strrchr(pos, '"');
if (pos2)
pos = pos2 + 1;
}
while (*pos != '\0' && *pos != ' ')
pos++;
if (*pos == ' ')
*pos++ = '\0';
}
return argc;
}
static void wpa_cli_ping(void *eloop_ctx, void *timeout_ctx) static void wpa_cli_ping(void *eloop_ctx, void *timeout_ctx)
{ {