1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <arpa/inet.h>
32 #ifdef HAVE___PROGNAME
33 extern const char *__progname
;
35 # define __progname "lldpctl"
38 /* Global for completion */
39 static struct cmd_node
*root
= NULL
;
44 fprintf(stderr
, "Usage: %s [OPTIONS ...] [COMMAND ...]\n", __progname
);
45 fprintf(stderr
, "Version: %s\n", PACKAGE_STRING
);
47 fprintf(stderr
, "\n");
49 fprintf(stderr
, "-d Enable more debugging information.\n");
50 fprintf(stderr
, "-f format Choose output format (plain, keyvalue or xml).\n");
52 fprintf(stderr
, "\n");
54 fprintf(stderr
, "see manual page lldpctl(8) for more information\n");
61 return (!(getuid() != geteuid() || getgid() != getegid()));
67 int privileged
= is_privileged();
69 return "[lldpctl] # ";
70 return "[lldpctl] $ ";
73 static int must_exit
= 0;
75 * Exit the interpreter.
78 cmd_exit(struct lldpctl_conn_t
*conn
, struct writer
*w
,
79 struct cmd_env
*env
, void *arg
)
81 log_info("lldpctl", "quit lldpctl");
87 * Send an "update" request.
90 cmd_update(struct lldpctl_conn_t
*conn
, struct writer
*w
,
91 struct cmd_env
*env
, void *arg
)
93 log_info("lldpctl", "ask for global update");
95 lldpctl_atom_t
*config
= lldpctl_get_configuration(conn
);
97 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
98 lldpctl_last_strerror(conn
));
101 if (lldpctl_atom_set_int(config
,
102 lldpctl_k_config_tx_interval
, -1) == NULL
) {
103 log_warnx("lldpctl", "unable to ask lldpd for immediate retransmission. %s",
104 lldpctl_last_strerror(conn
));
105 lldpctl_atom_dec_ref(config
);
108 log_info("lldpctl", "immediate retransmission requested successfuly");
109 lldpctl_atom_dec_ref(config
);
114 _cmd_complete(EditLine
*el
, int ch
, int all
)
118 if ((eltok
= tok_init(NULL
)) == NULL
)
121 const LineInfo
*li
= el_line(el
);
125 int argc
, cursorc
, cursoro
;
126 if (tok_line(eltok
, li
, &argc
, &argv
, &cursorc
, &cursoro
) != 0)
128 compl = commands_complete(root
, argc
, argv
, cursorc
, cursoro
, all
);
130 el_deletestr(el
, cursoro
);
131 if (el_insertstr(el
, compl) == -1) {
139 /* No completion or several completion available. We beep. */
143 if (eltok
) tok_end(eltok
);
148 cmd_complete(EditLine
*el
, int ch
)
150 return _cmd_complete(el
, ch
, 0);
154 cmd_help(EditLine
*el
, int ch
)
156 return _cmd_complete(el
, ch
, 1);
159 static struct cmd_node
*
162 root
= commands_root();
163 register_commands_show(root
);
164 register_commands_watch(root
);
165 if (is_privileged()) {
167 commands_new(root
, "update", "Update information and send LLDPU on all ports",
169 NEWLINE
, "Update information and send LLDPU on all ports",
170 NULL
, cmd_update
, NULL
);
171 register_commands_configure(root
);
174 commands_new(root
, "exit", "Exit interpreter", NULL
, NULL
, NULL
),
175 NEWLINE
, "Exit interpreter", NULL
, cmd_exit
, NULL
);
180 main(int argc
, char *argv
[])
182 int ch
, debug
= 1, rc
= EXIT_FAILURE
;
184 lldpctl_conn_t
*conn
;
192 /* Get and parse command line options */
193 while ((ch
= getopt(argc
, argv
, "hdvf:")) != -1) {
202 fprintf(stdout
, "%s\n", PACKAGE_VERSION
);
213 log_init(debug
, __progname
);
215 /* Register commands */
216 root
= register_commands();
219 log_debug("lldpctl", "init editline");
220 el
= el_init("lldpctl", stdin
, stdout
, stderr
);
222 log_warnx("lldpctl", "unable to setup editline");
225 el_set(el
, EL_PROMPT
, prompt
);
226 el_set(el
, EL_SIGNAL
, 0);
227 el_set(el
, EL_EDITOR
, "emacs");
228 /* If on a TTY, setup completion */
229 if (isatty(STDERR_FILENO
)) {
230 el_set(el
, EL_ADDFN
, "command_complete",
231 "Execute completion", cmd_complete
);
232 el_set(el
, EL_ADDFN
, "command_help",
233 "Show completion", cmd_help
);
234 el_set(el
, EL_BIND
, "^I", "command_complete", NULL
);
235 el_set(el
, EL_BIND
, "?", "command_help", NULL
);
239 elhistory
= history_init();
240 if (elhistory
== NULL
) {
241 log_warnx("lldpctl", "unable to enable history");
243 history(elhistory
, &elhistev
, H_SETSIZE
, 800);
244 el_set(el
, EL_HIST
, history
, elhistory
);
248 eltok
= tok_init(NULL
);
250 log_warnx("lldpctl", "unable to initialize tokenizer");
254 /* Make a connection */
255 log_debug("lldpctl", "connect to lldpd");
256 conn
= lldpctl_new(NULL
, NULL
, NULL
);
265 /* Read a new line. */
266 line
= el_gets(el
, &count
);
267 if (line
== NULL
) break;
270 log_debug("lldpctl", "tokenize command line");
271 n
= tok_str(eltok
, line
, &argc
, &argv
);
274 log_warnx("lldpctl", "internal error while tokenizing");
279 /* TODO: handle multiline statements */
280 log_warnx("lldpctl", "unmatched quotes");
288 if (elhistory
) history(elhistory
, &elhistev
, H_ENTER
, line
);
290 /* Init output formatter */
291 if (strcmp(fmt
, "plain") == 0) w
= txt_init(stdout
);
292 else if (strcmp(fmt
, "keyvalue") == 0) w
= kv_init(stdout
);
294 else if (strcmp(fmt
, "xml") == 0) w
= xml_init(stdout
);
297 else if (strcmp(fmt
, "json") == 0) w
= json_init(stdout
);
299 else w
= txt_init(stdout
);
301 /* Execute command */
302 if (commands_execute(conn
, w
,
303 root
, argc
, argv
) != 0)
304 log_info("lldpctl", "an error occurred while executing last command");
311 if (conn
) lldpctl_release(conn
);
312 if (eltok
) tok_end(eltok
);
313 if (elhistory
) history_end(elhistory
);
315 if (root
) commands_free(root
);