2 * src/utils.c Utilities
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
13 * @defgroup cli Command Line Interface API
18 #include <netlink/cli/utils.h>
20 uint32_t nl_cli_parse_u32(const char *arg)
25 lval = strtoul(arg, &endptr, 0);
26 if (endptr == arg || lval == ULONG_MAX)
27 nl_cli_fatal(EINVAL, "Unable to parse \"%s\", not a number.",
30 return (uint32_t) lval;
33 void nl_cli_print_version(void)
35 printf("libnl tools version %s\n", LIBNL_VERSION);
37 "Copyright (C) 2003-2009 Thomas Graf <tgraf@redhat.com>\n"
39 "This program comes with ABSOLUTELY NO WARRANTY. This is free \n"
40 "software, and you are welcome to redistribute it under certain\n"
41 "conditions. See the GNU General Public License for details.\n"
47 void nl_cli_fatal(int err, const char *fmt, ...)
51 fprintf(stderr, "Error: ");
55 vfprintf(stderr, fmt, ap);
57 fprintf(stderr, "\n");
59 fprintf(stderr, "%s\n", strerror(err));
64 int nl_cli_connect(struct nl_sock *sk, int protocol)
68 if ((err = nl_connect(sk, protocol)) < 0)
69 nl_cli_fatal(err, "Unable to connect netlink socket: %s",
75 struct nl_sock *nl_cli_alloc_socket(void)
79 if (!(sock = nl_socket_alloc()))
80 nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket");
85 struct nl_addr *nl_cli_addr_parse(const char *str, int family)
90 if ((err = nl_addr_parse(str, family, &addr)) < 0)
91 nl_cli_fatal(err, "Unable to parse address \"%s\": %s",
92 str, nl_geterror(err));
97 int nl_cli_parse_dumptype(const char *str)
99 if (!strcasecmp(str, "brief"))
101 else if (!strcasecmp(str, "details") || !strcasecmp(str, "detailed"))
102 return NL_DUMP_DETAILS;
103 else if (!strcasecmp(str, "stats"))
104 return NL_DUMP_STATS;
105 else if (!strcasecmp(str, "env"))
108 nl_cli_fatal(EINVAL, "Invalid dump type \"%s\".\n", str);
113 int nl_cli_confirm(struct nl_object *obj, struct nl_dump_params *params,
118 nl_object_dump(obj, params);
119 printf("Delete? (%c/%c) ",
120 default_yes ? 'Y' : 'y',
121 default_yes ? 'n' : 'N');
124 answer = tolower(getchar());
126 answer = default_yes ? 'y' : 'n';
127 } while (answer != 'y' && answer != 'n');
129 return answer == 'y';
132 struct nl_cache *nl_cli_alloc_cache(struct nl_sock *sock, const char *name,
133 int (*ac)(struct nl_sock *, struct nl_cache **))
135 struct nl_cache *cache;
138 if ((err = ac(sock, &cache)) < 0)
139 nl_cli_fatal(err, "Unable to allocate %s cache: %s",
140 name, nl_geterror(err));
142 nl_cache_mngt_provide(cache);