Move parts of wpa_cli to a new common file
[mech_eap.git] / src / common / cli.c
1 /*
2  * Common hostapd/wpa_supplicant command line interface functions
3  * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "utils/common.h"
12 #include "common/cli.h"
13
14
15 void cli_txt_list_free(struct cli_txt_entry *e)
16 {
17         dl_list_del(&e->list);
18         os_free(e->txt);
19         os_free(e);
20 }
21
22
23 void cli_txt_list_flush(struct dl_list *list)
24 {
25         struct cli_txt_entry *e;
26
27         while ((e = dl_list_first(list, struct cli_txt_entry, list)))
28                 cli_txt_list_free(e);
29 }
30
31
32 struct cli_txt_entry * cli_txt_list_get(struct dl_list *txt_list,
33                                         const char *txt)
34 {
35         struct cli_txt_entry *e;
36
37         dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
38                 if (os_strcmp(e->txt, txt) == 0)
39                         return e;
40         }
41         return NULL;
42 }
43
44
45 void cli_txt_list_del(struct dl_list *txt_list, const char *txt)
46 {
47         struct cli_txt_entry *e;
48
49         e = cli_txt_list_get(txt_list, txt);
50         if (e)
51                 cli_txt_list_free(e);
52 }
53
54
55 void cli_txt_list_del_addr(struct dl_list *txt_list, const char *txt)
56 {
57         u8 addr[ETH_ALEN];
58         char buf[18];
59
60         if (hwaddr_aton(txt, addr) < 0)
61                 return;
62         os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
63         cli_txt_list_del(txt_list, buf);
64 }
65
66
67 void cli_txt_list_del_word(struct dl_list *txt_list, const char *txt,
68                            int separator)
69 {
70         const char *end;
71         char *buf;
72
73         end = os_strchr(txt, separator);
74         if (end == NULL)
75                 end = txt + os_strlen(txt);
76         buf = dup_binstr(txt, end - txt);
77         if (buf == NULL)
78                 return;
79         cli_txt_list_del(txt_list, buf);
80         os_free(buf);
81 }
82
83
84 int cli_txt_list_add(struct dl_list *txt_list, const char *txt)
85 {
86         struct cli_txt_entry *e;
87
88         e = cli_txt_list_get(txt_list, txt);
89         if (e)
90                 return 0;
91         e = os_zalloc(sizeof(*e));
92         if (e == NULL)
93                 return -1;
94         e->txt = os_strdup(txt);
95         if (e->txt == NULL) {
96                 os_free(e);
97                 return -1;
98         }
99         dl_list_add(txt_list, &e->list);
100         return 0;
101 }
102
103
104 int cli_txt_list_add_addr(struct dl_list *txt_list, const char *txt)
105 {
106         u8 addr[ETH_ALEN];
107         char buf[18];
108
109         if (hwaddr_aton(txt, addr) < 0)
110                 return -1;
111         os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
112         return cli_txt_list_add(txt_list, buf);
113 }
114
115
116 int cli_txt_list_add_word(struct dl_list *txt_list, const char *txt,
117                           int separator)
118 {
119         const char *end;
120         char *buf;
121         int ret;
122
123         end = os_strchr(txt, separator);
124         if (end == NULL)
125                 end = txt + os_strlen(txt);
126         buf = dup_binstr(txt, end - txt);
127         if (buf == NULL)
128                 return -1;
129         ret = cli_txt_list_add(txt_list, buf);
130         os_free(buf);
131         return ret;
132 }
133
134
135 char ** cli_txt_list_array(struct dl_list *txt_list)
136 {
137         unsigned int i, count = dl_list_len(txt_list);
138         char **res;
139         struct cli_txt_entry *e;
140
141         res = os_calloc(count + 1, sizeof(char *));
142         if (res == NULL)
143                 return NULL;
144
145         i = 0;
146         dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
147                 res[i] = os_strdup(e->txt);
148                 if (res[i] == NULL)
149                         break;
150                 i++;
151         }
152
153         return res;
154 }