2 * Common hostapd/wpa_supplicant command line interface functions
3 * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
11 #include "utils/common.h"
12 #include "common/cli.h"
15 const char *const cli_license =
16 "This software may be distributed under the terms of the BSD license.\n"
17 "See README for more details.\n";
19 const char *const cli_full_license =
20 "This software may be distributed under the terms of the BSD license.\n"
22 "Redistribution and use in source and binary forms, with or without\n"
23 "modification, are permitted provided that the following conditions are\n"
26 "1. Redistributions of source code must retain the above copyright\n"
27 " notice, this list of conditions and the following disclaimer.\n"
29 "2. Redistributions in binary form must reproduce the above copyright\n"
30 " notice, this list of conditions and the following disclaimer in the\n"
31 " documentation and/or other materials provided with the distribution.\n"
33 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
34 " names of its contributors may be used to endorse or promote products\n"
35 " derived from this software without specific prior written permission.\n"
37 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
38 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
39 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
40 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
41 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
42 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
43 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
44 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
45 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
46 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
47 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
51 void cli_txt_list_free(struct cli_txt_entry *e)
53 dl_list_del(&e->list);
59 void cli_txt_list_flush(struct dl_list *list)
61 struct cli_txt_entry *e;
63 while ((e = dl_list_first(list, struct cli_txt_entry, list)))
68 struct cli_txt_entry * cli_txt_list_get(struct dl_list *txt_list,
71 struct cli_txt_entry *e;
73 dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
74 if (os_strcmp(e->txt, txt) == 0)
81 void cli_txt_list_del(struct dl_list *txt_list, const char *txt)
83 struct cli_txt_entry *e;
85 e = cli_txt_list_get(txt_list, txt);
91 void cli_txt_list_del_addr(struct dl_list *txt_list, const char *txt)
96 if (hwaddr_aton(txt, addr) < 0)
98 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
99 cli_txt_list_del(txt_list, buf);
103 void cli_txt_list_del_word(struct dl_list *txt_list, const char *txt,
109 end = os_strchr(txt, separator);
111 end = txt + os_strlen(txt);
112 buf = dup_binstr(txt, end - txt);
115 cli_txt_list_del(txt_list, buf);
120 int cli_txt_list_add(struct dl_list *txt_list, const char *txt)
122 struct cli_txt_entry *e;
124 e = cli_txt_list_get(txt_list, txt);
127 e = os_zalloc(sizeof(*e));
130 e->txt = os_strdup(txt);
131 if (e->txt == NULL) {
135 dl_list_add(txt_list, &e->list);
140 int cli_txt_list_add_addr(struct dl_list *txt_list, const char *txt)
145 if (hwaddr_aton(txt, addr) < 0)
147 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
148 return cli_txt_list_add(txt_list, buf);
152 int cli_txt_list_add_word(struct dl_list *txt_list, const char *txt,
159 end = os_strchr(txt, separator);
161 end = txt + os_strlen(txt);
162 buf = dup_binstr(txt, end - txt);
165 ret = cli_txt_list_add(txt_list, buf);
171 char ** cli_txt_list_array(struct dl_list *txt_list)
173 unsigned int i, count = dl_list_len(txt_list);
175 struct cli_txt_entry *e;
177 res = os_calloc(count + 1, sizeof(char *));
182 dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
183 res[i] = os_strdup(e->txt);
193 int get_cmd_arg_num(const char *str, int pos)
197 for (i = 0; i <= pos; i++) {
200 while (i <= pos && str[i] != ' ')
211 int write_cmd(char *buf, size_t buflen, const char *cmd, int argc, char *argv[])
219 res = os_snprintf(pos, end - pos, "%s", cmd);
220 if (os_snprintf_error(end - pos, res))
224 for (i = 0; i < argc; i++) {
225 res = os_snprintf(pos, end - pos, " %s", argv[i]);
226 if (os_snprintf_error(end - pos, res))
231 buf[buflen - 1] = '\0';
235 printf("Too long command\n");
240 int tokenize_cmd(char *cmd, char *argv[])
253 if (argc == max_args)
256 char *pos2 = os_strrchr(pos, '"');
260 while (*pos != '\0' && *pos != ' ')