Move parts of wpa_cli to a new common file

In preparation for adding further command completion support
to hostapd_cli move some cli related utility functions out of
wpa_cli into a new common cli file.

Signed-off-by: Mikael Kanstrup <mikael.kanstrup@sonymobile.com>
This commit is contained in:
Mikael Kanstrup 2016-07-07 14:04:34 +02:00 committed by Jouni Malinen
parent 6cad0bff06
commit 977c0796f9
8 changed files with 203 additions and 145 deletions

View file

@ -947,7 +947,10 @@ ifdef CONFIG_ANDROID_LOG
L_CFLAGS += -DCONFIG_ANDROID_LOG L_CFLAGS += -DCONFIG_ANDROID_LOG
endif endif
OBJS_c = hostapd_cli.c src/common/wpa_ctrl.c src/utils/os_$(CONFIG_OS).c OBJS_c = hostapd_cli.c
OBJS_c += src/common/wpa_ctrl.c
OBJS_c += src/utils/os_$(CONFIG_OS).c
OBJS_c += src/common/cli.c
OBJS_c += src/utils/eloop.c OBJS_c += src/utils/eloop.c
OBJS_c += src/utils/common.c OBJS_c += src/utils/common.c
ifdef CONFIG_WPA_TRACE ifdef CONFIG_WPA_TRACE

View file

@ -87,7 +87,10 @@ OBJS += ../src/ap/bss_load.o
OBJS += ../src/ap/neighbor_db.o OBJS += ../src/ap/neighbor_db.o
OBJS += ../src/ap/rrm.o OBJS += ../src/ap/rrm.o
OBJS_c = hostapd_cli.o ../src/common/wpa_ctrl.o ../src/utils/os_$(CONFIG_OS).o OBJS_c = hostapd_cli.o
OBJS_c += ../src/common/wpa_ctrl.o
OBJS_c += ../src/utils/os_$(CONFIG_OS).o
OBJS_c += ../src/common/cli.o
NEED_RC4=y NEED_RC4=y
NEED_AES=y NEED_AES=y

View file

@ -15,6 +15,7 @@
#include "utils/eloop.h" #include "utils/eloop.h"
#include "utils/edit.h" #include "utils/edit.h"
#include "common/version.h" #include "common/version.h"
#include "common/cli.h"
#ifndef CONFIG_NO_CTRL_IFACE #ifndef CONFIG_NO_CTRL_IFACE

154
src/common/cli.c Normal file
View file

@ -0,0 +1,154 @@
/*
* Common hostapd/wpa_supplicant command line interface functions
* Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#include "includes.h"
#include "utils/common.h"
#include "common/cli.h"
void cli_txt_list_free(struct cli_txt_entry *e)
{
dl_list_del(&e->list);
os_free(e->txt);
os_free(e);
}
void cli_txt_list_flush(struct dl_list *list)
{
struct cli_txt_entry *e;
while ((e = dl_list_first(list, struct cli_txt_entry, list)))
cli_txt_list_free(e);
}
struct cli_txt_entry * cli_txt_list_get(struct dl_list *txt_list,
const char *txt)
{
struct cli_txt_entry *e;
dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
if (os_strcmp(e->txt, txt) == 0)
return e;
}
return NULL;
}
void cli_txt_list_del(struct dl_list *txt_list, const char *txt)
{
struct cli_txt_entry *e;
e = cli_txt_list_get(txt_list, txt);
if (e)
cli_txt_list_free(e);
}
void cli_txt_list_del_addr(struct dl_list *txt_list, const char *txt)
{
u8 addr[ETH_ALEN];
char buf[18];
if (hwaddr_aton(txt, addr) < 0)
return;
os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
cli_txt_list_del(txt_list, buf);
}
void cli_txt_list_del_word(struct dl_list *txt_list, const char *txt,
int separator)
{
const char *end;
char *buf;
end = os_strchr(txt, separator);
if (end == NULL)
end = txt + os_strlen(txt);
buf = dup_binstr(txt, end - txt);
if (buf == NULL)
return;
cli_txt_list_del(txt_list, buf);
os_free(buf);
}
int cli_txt_list_add(struct dl_list *txt_list, const char *txt)
{
struct cli_txt_entry *e;
e = cli_txt_list_get(txt_list, txt);
if (e)
return 0;
e = os_zalloc(sizeof(*e));
if (e == NULL)
return -1;
e->txt = os_strdup(txt);
if (e->txt == NULL) {
os_free(e);
return -1;
}
dl_list_add(txt_list, &e->list);
return 0;
}
int cli_txt_list_add_addr(struct dl_list *txt_list, const char *txt)
{
u8 addr[ETH_ALEN];
char buf[18];
if (hwaddr_aton(txt, addr) < 0)
return -1;
os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
return cli_txt_list_add(txt_list, buf);
}
int cli_txt_list_add_word(struct dl_list *txt_list, const char *txt,
int separator)
{
const char *end;
char *buf;
int ret;
end = os_strchr(txt, separator);
if (end == NULL)
end = txt + os_strlen(txt);
buf = dup_binstr(txt, end - txt);
if (buf == NULL)
return -1;
ret = cli_txt_list_add(txt_list, buf);
os_free(buf);
return ret;
}
char ** cli_txt_list_array(struct dl_list *txt_list)
{
unsigned int i, count = dl_list_len(txt_list);
char **res;
struct cli_txt_entry *e;
res = os_calloc(count + 1, sizeof(char *));
if (res == NULL)
return NULL;
i = 0;
dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
res[i] = os_strdup(e->txt);
if (res[i] == NULL)
break;
i++;
}
return res;
}

37
src/common/cli.h Normal file
View file

@ -0,0 +1,37 @@
/*
* Common hostapd/wpa_supplicant command line interface functionality
* Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#ifndef CLI_H
#define CLI_H
#include "utils/list.h"
struct cli_txt_entry {
struct dl_list list;
char *txt;
};
void cli_txt_list_free(struct cli_txt_entry *e);
void cli_txt_list_flush(struct dl_list *list);
struct cli_txt_entry *
cli_txt_list_get(struct dl_list *txt_list, const char *txt);
void cli_txt_list_del(struct dl_list *txt_list, const char *txt);
void cli_txt_list_del_addr(struct dl_list *txt_list, const char *txt);
void cli_txt_list_del_word(struct dl_list *txt_list, const char *txt,
int separator);
int cli_txt_list_add(struct dl_list *txt_list, const char *txt);
int cli_txt_list_add_addr(struct dl_list *txt_list, const char *txt);
int cli_txt_list_add_word(struct dl_list *txt_list, const char *txt,
int separator);
char ** cli_txt_list_array(struct dl_list *txt_list);
#endif /* CLI_H */

View file

@ -94,6 +94,7 @@ OBJS_p += src/utils/wpabuf.c
OBJS_c = wpa_cli.c src/common/wpa_ctrl.c OBJS_c = wpa_cli.c src/common/wpa_ctrl.c
OBJS_c += src/utils/wpa_debug.c OBJS_c += src/utils/wpa_debug.c
OBJS_c += src/utils/common.c OBJS_c += src/utils/common.c
OBJS_c += src/common/cli.c
OBJS_d = OBJS_d =
OBJS_priv = OBJS_priv =

View file

@ -110,6 +110,7 @@ OBJS_p += ../src/utils/wpabuf.o
OBJS_c = wpa_cli.o ../src/common/wpa_ctrl.o OBJS_c = wpa_cli.o ../src/common/wpa_ctrl.o
OBJS_c += ../src/utils/wpa_debug.o OBJS_c += ../src/utils/wpa_debug.o
OBJS_c += ../src/utils/common.o OBJS_c += ../src/utils/common.o
OBJS_c += ../src/common/cli.o
OBJS += wmm_ac.o OBJS += wmm_ac.o
ifndef CONFIG_OS ifndef CONFIG_OS

View file

@ -14,6 +14,7 @@
#include <dirent.h> #include <dirent.h>
#endif /* CONFIG_CTRL_IFACE_UNIX */ #endif /* CONFIG_CTRL_IFACE_UNIX */
#include "common/cli.h"
#include "common/wpa_ctrl.h" #include "common/wpa_ctrl.h"
#include "utils/common.h" #include "utils/common.h"
#include "utils/eloop.h" #include "utils/eloop.h"
@ -90,11 +91,6 @@ static int ping_interval = 5;
static int interactive = 0; static int interactive = 0;
static char *ifname_prefix = NULL; static char *ifname_prefix = NULL;
struct cli_txt_entry {
struct dl_list list;
char *txt;
};
static DEFINE_DL_LIST(bsses); /* struct cli_txt_entry */ static DEFINE_DL_LIST(bsses); /* struct cli_txt_entry */
static DEFINE_DL_LIST(p2p_peers); /* struct cli_txt_entry */ static DEFINE_DL_LIST(p2p_peers); /* struct cli_txt_entry */
static DEFINE_DL_LIST(p2p_groups); /* struct cli_txt_entry */ static DEFINE_DL_LIST(p2p_groups); /* struct cli_txt_entry */
@ -130,144 +126,6 @@ static void usage(void)
} }
static void cli_txt_list_free(struct cli_txt_entry *e)
{
dl_list_del(&e->list);
os_free(e->txt);
os_free(e);
}
static void cli_txt_list_flush(struct dl_list *list)
{
struct cli_txt_entry *e;
while ((e = dl_list_first(list, struct cli_txt_entry, list)))
cli_txt_list_free(e);
}
static struct cli_txt_entry * cli_txt_list_get(struct dl_list *txt_list,
const char *txt)
{
struct cli_txt_entry *e;
dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
if (os_strcmp(e->txt, txt) == 0)
return e;
}
return NULL;
}
static void cli_txt_list_del(struct dl_list *txt_list, const char *txt)
{
struct cli_txt_entry *e;
e = cli_txt_list_get(txt_list, txt);
if (e)
cli_txt_list_free(e);
}
static void cli_txt_list_del_addr(struct dl_list *txt_list, const char *txt)
{
u8 addr[ETH_ALEN];
char buf[18];
if (hwaddr_aton(txt, addr) < 0)
return;
os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
cli_txt_list_del(txt_list, buf);
}
#ifdef CONFIG_P2P
static void cli_txt_list_del_word(struct dl_list *txt_list, const char *txt,
int separator)
{
const char *end;
char *buf;
end = os_strchr(txt, separator);
if (end == NULL)
end = txt + os_strlen(txt);
buf = dup_binstr(txt, end - txt);
if (buf == NULL)
return;
cli_txt_list_del(txt_list, buf);
os_free(buf);
}
#endif /* CONFIG_P2P */
static int cli_txt_list_add(struct dl_list *txt_list, const char *txt)
{
struct cli_txt_entry *e;
e = cli_txt_list_get(txt_list, txt);
if (e)
return 0;
e = os_zalloc(sizeof(*e));
if (e == NULL)
return -1;
e->txt = os_strdup(txt);
if (e->txt == NULL) {
os_free(e);
return -1;
}
dl_list_add(txt_list, &e->list);
return 0;
}
#ifdef CONFIG_P2P
static int cli_txt_list_add_addr(struct dl_list *txt_list, const char *txt)
{
u8 addr[ETH_ALEN];
char buf[18];
if (hwaddr_aton(txt, addr) < 0)
return -1;
os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
return cli_txt_list_add(txt_list, buf);
}
#endif /* CONFIG_P2P */
static int cli_txt_list_add_word(struct dl_list *txt_list, const char *txt,
int separator)
{
const char *end;
char *buf;
int ret;
end = os_strchr(txt, separator);
if (end == NULL)
end = txt + os_strlen(txt);
buf = dup_binstr(txt, end - txt);
if (buf == NULL)
return -1;
ret = cli_txt_list_add(txt_list, buf);
os_free(buf);
return ret;
}
static char ** cli_txt_list_array(struct dl_list *txt_list)
{
unsigned int i, count = dl_list_len(txt_list);
char **res;
struct cli_txt_entry *e;
res = os_calloc(count + 1, sizeof(char *));
if (res == NULL)
return NULL;
i = 0;
dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
res[i] = os_strdup(e->txt);
if (res[i] == NULL)
break;
i++;
}
return res;
}
static int get_cmd_arg_num(const char *str, int pos) static int get_cmd_arg_num(const char *str, int pos)
{ {
int arg = 0, i; int arg = 0, i;