wpa_cli: Split wpa_cli_interactive() into two versions

Instead of multiple #ifdef blocks for readline within the function,
use two copies of the functions, one for readline, one without any
readline functionality.
This commit is contained in:
Jouni Malinen 2010-11-14 11:30:19 +02:00
parent 644fb8c8a0
commit 6f1c6549ed

View file

@ -2734,8 +2734,53 @@ static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
} }
} }
#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 trunc_nl(char *str)
{
char *pos = str;
while (*pos != '\0') {
if (*pos == '\n') {
*pos = '\0';
break;
}
pos++;
}
}
#ifdef CONFIG_READLINE #ifdef CONFIG_READLINE
static char * wpa_cli_cmd_gen(const char *text, int state) static char * wpa_cli_cmd_gen(const char *text, int state)
{ {
static int i, len; static int i, len;
@ -2812,21 +2857,16 @@ static char ** wpa_cli_completion(const char *text, int start, int end)
func = wpa_cli_dummy_gen; func = wpa_cli_dummy_gen;
return rl_completion_matches(text, func); return rl_completion_matches(text, func);
} }
#endif /* CONFIG_READLINE */
static void wpa_cli_interactive(void) static void wpa_cli_interactive(void)
{ {
#define max_args 10 char cmdbuf[256], *cmd, *argv[max_args];
char cmdbuf[256], *cmd, *argv[max_args], *pos;
int argc; int argc;
#ifdef CONFIG_READLINE
char *home, *hfile = NULL; char *home, *hfile = NULL;
#endif /* CONFIG_READLINE */
printf("\nInteractive mode\n\n"); printf("\nInteractive mode\n\n");
#ifdef CONFIG_READLINE
rl_attempted_completion_function = wpa_cli_completion; rl_attempted_completion_function = wpa_cli_completion;
home = getenv("HOME"); home = getenv("HOME");
if (home) { if (home) {
@ -2844,7 +2884,6 @@ static void wpa_cli_interactive(void)
} }
} }
} }
#endif /* CONFIG_READLINE */
do { do {
wpa_cli_recv_pending(mon_conn, 0, 0); wpa_cli_recv_pending(mon_conn, 0, 0);
@ -2855,7 +2894,6 @@ static void wpa_cli_interactive(void)
if (mon_pid) if (mon_pid)
kill(mon_pid, SIGUSR1); kill(mon_pid, SIGUSR1);
#endif /* CONFIG_WPA_CLI_FORK */ #endif /* CONFIG_WPA_CLI_FORK */
#ifdef CONFIG_READLINE
cmd = readline("> "); cmd = readline("> ");
if (cmd && *cmd) { if (cmd && *cmd) {
HIST_ENTRY *h; HIST_ENTRY *h;
@ -2866,45 +2904,14 @@ static void wpa_cli_interactive(void)
add_history(cmd); add_history(cmd);
next_history(); next_history();
} }
#else /* CONFIG_READLINE */
printf("> ");
cmd = fgets(cmdbuf, sizeof(cmdbuf), stdin);
#endif /* CONFIG_READLINE */
#ifndef CONFIG_NATIVE_WINDOWS #ifndef CONFIG_NATIVE_WINDOWS
alarm(0); alarm(0);
#endif /* CONFIG_NATIVE_WINDOWS */ #endif /* CONFIG_NATIVE_WINDOWS */
if (cmd == NULL) if (cmd == NULL)
break; break;
wpa_cli_recv_pending(mon_conn, 0, 0); wpa_cli_recv_pending(mon_conn, 0, 0);
pos = cmd; trunc_nl(cmd);
while (*pos != '\0') { argc = tokenize_cmd(cmd, argv);
if (*pos == '\n') {
*pos = '\0';
break;
}
pos++;
}
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';
}
if (argc) if (argc)
wpa_request(ctrl_conn, argc, argv); wpa_request(ctrl_conn, argc, argv);
@ -2916,7 +2923,6 @@ static void wpa_cli_interactive(void)
#endif /* CONFIG_WPA_CLI_FORK */ #endif /* CONFIG_WPA_CLI_FORK */
} while (!wpa_cli_quit); } while (!wpa_cli_quit);
#ifdef CONFIG_READLINE
if (hfile) { if (hfile) {
/* Save command history, excluding lines that may contain /* Save command history, excluding lines that may contain
* passwords. */ * passwords. */
@ -2940,9 +2946,50 @@ static void wpa_cli_interactive(void)
write_history(hfile); write_history(hfile);
os_free(hfile); os_free(hfile);
} }
#endif /* CONFIG_READLINE */
} }
#else /* CONFIG_READLINE */
static void wpa_cli_interactive(void)
{
char cmdbuf[256], *cmd, *argv[max_args];
int argc;
printf("\nInteractive mode\n\n");
do {
wpa_cli_recv_pending(mon_conn, 0, 0);
#ifndef CONFIG_NATIVE_WINDOWS
alarm(ping_interval);
#endif /* CONFIG_NATIVE_WINDOWS */
#ifdef CONFIG_WPA_CLI_FORK
if (mon_pid)
kill(mon_pid, SIGUSR1);
#endif /* CONFIG_WPA_CLI_FORK */
printf("> ");
cmd = fgets(cmdbuf, sizeof(cmdbuf), stdin);
#ifndef CONFIG_NATIVE_WINDOWS
alarm(0);
#endif /* CONFIG_NATIVE_WINDOWS */
if (cmd == NULL)
break;
wpa_cli_recv_pending(mon_conn, 0, 0);
trunc_nl(cmd);
argc = tokenize_cmd(cmd, argv);
if (argc)
wpa_request(ctrl_conn, argc, argv);
if (cmd != cmdbuf)
free(cmd);
#ifdef CONFIG_WPA_CLI_FORK
if (mon_pid)
kill(mon_pid, SIGUSR2);
#endif /* CONFIG_WPA_CLI_FORK */
} while (!wpa_cli_quit);
}
#endif /* CONFIG_READLINE */
static void wpa_cli_action(struct wpa_ctrl *ctrl) static void wpa_cli_action(struct wpa_ctrl *ctrl)
{ {