diff --git a/Makefile b/Makefile index b9808f1..581a483 100644 --- a/Makefile +++ b/Makefile @@ -39,8 +39,7 @@ LIBIFUPDOWN_SRC = \ libifupdown/lifecycle.c \ libifupdown/config-parser.c \ libifupdown/config-file.c \ - libifupdown/compat.c \ - + libifupdown/compat.c LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o} LIBIFUPDOWN_LIB = libifupdown.a @@ -48,7 +47,8 @@ MULTICALL_SRC = \ cmd/multicall.c \ cmd/multicall-options.c \ cmd/multicall-exec-options.c \ - cmd/multicall-match-options.c + cmd/multicall-match-options.c \ + cmd/pretty-print-iface.c MULTICALL_OBJ = ${MULTICALL_SRC:.c=.o} MULTICALL = ifupdown diff --git a/cmd/ifquery.c b/cmd/ifquery.c index dd17ddc..f5a879e 100644 --- a/cmd/ifquery.c +++ b/cmd/ifquery.c @@ -20,42 +20,7 @@ #include #include "libifupdown/libifupdown.h" #include "cmd/multicall.h" - -void -print_interface(struct lif_interface *iface) -{ - if (!lif_lifecycle_query_dependents(&exec_opts, iface, iface->ifname)) - return; - - if (iface->is_auto) - printf("auto %s\n", iface->ifname); - - printf("%s %s\n", iface->is_template ? "template" : "iface", iface->ifname); - - struct lif_node *iter; - LIF_DICT_FOREACH(iter, &iface->vars) - { - struct lif_dict_entry *entry = iter->data; - - if (!strcmp(entry->key, "address")) - { - struct lif_address *addr = entry->data; - char addr_buf[512]; - - if (!lif_address_unparse(addr, addr_buf, sizeof addr_buf, true)) - { - printf(" # warning: failed to unparse address\n"); - continue; - } - - printf(" %s %s\n", entry->key, addr_buf); - } - else - printf(" %s %s\n", entry->key, (const char *) entry->data); - } - - printf("\n"); -} +#include "cmd/pretty-print-iface.h" void print_interface_dot(struct lif_dict *collection, struct lif_interface *iface, struct lif_interface *parent) @@ -147,7 +112,7 @@ list_interfaces(struct lif_dict *collection, struct match_options *opts) continue; if (opts->pretty_print) - print_interface(iface); + prettyprint_interface_eni(iface); else if (opts->dot) print_interface_dot(collection, iface, NULL); else @@ -330,7 +295,7 @@ ifquery_main(int argc, char *argv[]) if (match_opts.property != NULL) print_interface_property(iface, match_opts.property); else - print_interface(iface); + prettyprint_interface_eni(iface); } return EXIT_SUCCESS; diff --git a/cmd/pretty-print-iface.c b/cmd/pretty-print-iface.c new file mode 100644 index 0000000..710e12a --- /dev/null +++ b/cmd/pretty-print-iface.c @@ -0,0 +1,56 @@ +/* + * cmd/pretty-print-iface.c + * Purpose: interface pretty-printer (/e/n/i style) + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#include +#include +#include "libifupdown/libifupdown.h" +#include "cmd/multicall.h" +#include "cmd/pretty-print-iface.h" + +void +prettyprint_interface_eni(struct lif_interface *iface) +{ + if (!lif_lifecycle_query_dependents(&exec_opts, iface, iface->ifname)) + return; + + if (iface->is_auto) + printf("auto %s\n", iface->ifname); + + printf("%s %s\n", iface->is_template ? "template" : "iface", iface->ifname); + + struct lif_node *iter; + LIF_DICT_FOREACH(iter, &iface->vars) + { + struct lif_dict_entry *entry = iter->data; + + if (!strcmp(entry->key, "address")) + { + struct lif_address *addr = entry->data; + char addr_buf[512]; + + if (!lif_address_unparse(addr, addr_buf, sizeof addr_buf, true)) + { + printf(" # warning: failed to unparse address\n"); + continue; + } + + printf(" %s %s\n", entry->key, addr_buf); + } + else + printf(" %s %s\n", entry->key, (const char *) entry->data); + } + + printf("\n"); +} diff --git a/cmd/pretty-print-iface.h b/cmd/pretty-print-iface.h new file mode 100644 index 0000000..8835ae6 --- /dev/null +++ b/cmd/pretty-print-iface.h @@ -0,0 +1,23 @@ +/* + * cmd/pretty-print-iface.h + * Purpose: interface pretty-printer (/e/n/i style) + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#ifndef IFUPDOWN_CMD_PRETTY_PRINT_IFACE_H__GUARD +#define IFUPDOWN_CMD_PRETTY_PRINT_IFACE_H__GUARD + +#include "libifupdown/libifupdown.h" + +extern void prettyprint_interface_eni(struct lif_interface *iface); + +#endif