18#include <netlink/cli/utils.h>
41 lval = strtoul(arg, &endptr, 0);
42 if (endptr == arg || lval == ULONG_MAX)
43 nl_cli_fatal(EINVAL,
"Unable to parse \"%s\", not a number.",
46 return (uint32_t) lval;
49void nl_cli_print_version(
void)
51 printf(
"libnl tools version %s\n", LIBNL_VERSION);
53 "Copyright (C) 2003-2010 Thomas Graf <tgraf@redhat.com>\n"
55 "This program comes with ABSOLUTELY NO WARRANTY. This is free \n"
56 "software, and you are welcome to redistribute it under certain\n"
57 "conditions. See the GNU General Public License for details.\n"
75 fprintf(stderr,
"Error: ");
79 vfprintf(stderr, fmt, ap);
81 fprintf(stderr,
"\n");
85 locale_t loc = newlocale(LC_MESSAGES_MASK,
"", (locale_t)0);
86 if (loc == (locale_t)0) {
88 loc = newlocale(LC_MESSAGES_MASK,
89 "POSIX", (locale_t)0);
90 if (loc == (locale_t)0)
91 buf =
"newlocale() failed";
93 if (loc != (locale_t)0)
94 buf = strerror_l(err, loc);
98 fprintf(stderr,
"%s\n", buf);
100 if (loc != (locale_t)0)
108int nl_cli_connect(
struct nl_sock *sk,
int protocol)
113 nl_cli_fatal(err,
"Unable to connect netlink socket: %s",
119struct nl_sock *nl_cli_alloc_socket(
void)
121 struct nl_sock *sock;
124 nl_cli_fatal(ENOBUFS,
"Unable to allocate netlink socket");
129struct nl_addr *nl_cli_addr_parse(
const char *str,
int family)
131 struct nl_addr *addr;
136 str, nl_geterror(err));
141int nl_cli_parse_dumptype(
const char *str)
143 if (!strcasecmp(str,
"brief"))
145 else if (!strcasecmp(str,
"details") || !strcasecmp(str,
"detailed"))
147 else if (!strcasecmp(str,
"stats"))
150 nl_cli_fatal(EINVAL,
"Invalid dump type \"%s\".\n", str);
155int nl_cli_confirm(
struct nl_object *obj,
struct nl_dump_params *params,
161 char buf[32] = { 0 };
164 printf(
"Delete? (%c/%c) ",
165 default_yes ?
'Y' :
'y',
166 default_yes ?
'n' :
'N');
168 if (!fgets(buf,
sizeof(buf), stdin)) {
169 fprintf(stderr,
"Error while reading\n.");
173 switch ((answer = tolower(buf[0]))) {
175 answer = default_yes ?
'y' :
'n';
179 return answer ==
'y';
182 fprintf(stderr,
"Invalid input, try again.\n");
189struct nl_cache *nl_cli_alloc_cache(
struct nl_sock *sock,
const char *name,
190 int (*ac)(
struct nl_sock *,
struct nl_cache **))
192 struct nl_cache *cache;
195 if ((err = ac(sock, &cache)) < 0)
197 name, nl_geterror(err));
204struct nl_cache *nl_cli_alloc_cache_flags(
struct nl_sock *sock,
205 const char *name,
unsigned int flags,
206 int (*ac)(
struct nl_sock *,
struct nl_cache **,
209 struct nl_cache *cache;
212 if ((err = ac(sock, &cache, flags)) < 0)
214 name, nl_geterror(err));
221void nl_cli_load_module(
const char *prefix,
const char *name)
223 char path[FILENAME_MAX+1];
225 snprintf(path,
sizeof(path),
"%s/%s/%s.so",
226 PKGLIBDIR, prefix, name);
232 handle = dlopen(path, RTLD_NOW);
234 nl_cli_fatal(ENOENT,
"Unable to load module \"%s\": %s\n",
241 nl_cli_fatal(ENOTSUP,
"Unable to load module \"%s\": built without dynamic libraries support\n",
int nl_addr_parse(const char *addrstr, int hint, struct nl_addr **result)
Allocate abstract address based on character string.
void nl_cache_mngt_provide(struct nl_cache *cache)
Provide a cache for global use.
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
int nl_connect(struct nl_sock *sk, int protocol)
Create file descriptor and bind socket.
struct nl_sock * nl_socket_alloc(void)
Allocate new netlink socket.
@ NL_DUMP_STATS
Dump all attributes including statistics.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.