1 /*
2  * src/utils.c		Utilities
3  *
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
7  *	of the License.
8  *
9  * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @defgroup cli Command Line Interface API
14  *
15  * @{
16  */
17 
18 #include <netlink/cli/utils.h>
19 
nl_cli_parse_u32(const char * arg)20 uint32_t nl_cli_parse_u32(const char *arg)
21 {
22 	unsigned long lval;
23 	char *endptr;
24 
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.",
28 			     arg);
29 
30 	return (uint32_t) lval;
31 }
32 
nl_cli_print_version(void)33 void nl_cli_print_version(void)
34 {
35 	printf("libnl tools version %s\n", LIBNL_VERSION);
36 	printf(
37 	"Copyright (C) 2003-2009 Thomas Graf <tgraf@redhat.com>\n"
38 	"\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"
42 	);
43 
44 	exit(0);
45 }
46 
nl_cli_fatal(int err,const char * fmt,...)47 void nl_cli_fatal(int err, const char *fmt, ...)
48 {
49 	va_list ap;
50 
51 	fprintf(stderr, "Error: ");
52 
53 	if (fmt) {
54 		va_start(ap, fmt);
55 		vfprintf(stderr, fmt, ap);
56 		va_end(ap);
57 		fprintf(stderr, "\n");
58 	} else
59 		fprintf(stderr, "%s\n", strerror(err));
60 
61 	exit(abs(err));
62 }
63 
nl_cli_connect(struct nl_sock * sk,int protocol)64 int nl_cli_connect(struct nl_sock *sk, int protocol)
65 {
66 	int err;
67 
68 	if ((err = nl_connect(sk, protocol)) < 0)
69 		nl_cli_fatal(err, "Unable to connect netlink socket: %s",
70 			     nl_geterror(err));
71 
72 	return err;
73 }
74 
nl_cli_alloc_socket(void)75 struct nl_sock *nl_cli_alloc_socket(void)
76 {
77 	struct nl_sock *sock;
78 
79 	if (!(sock = nl_socket_alloc()))
80 		nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket");
81 
82 	return sock;
83 }
84 
nl_cli_addr_parse(const char * str,int family)85 struct nl_addr *nl_cli_addr_parse(const char *str, int family)
86 {
87 	struct nl_addr *addr;
88 	int err;
89 
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));
93 
94 	return addr;
95 }
96 
nl_cli_parse_dumptype(const char * str)97 int nl_cli_parse_dumptype(const char *str)
98 {
99 	if (!strcasecmp(str, "brief"))
100 		return NL_DUMP_LINE;
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"))
106 		return NL_DUMP_ENV;
107 	else
108 		nl_cli_fatal(EINVAL, "Invalid dump type \"%s\".\n", str);
109 
110 	return 0;
111 }
112 
nl_cli_confirm(struct nl_object * obj,struct nl_dump_params * params,int default_yes)113 int nl_cli_confirm(struct nl_object *obj, struct nl_dump_params *params,
114 		   int default_yes)
115 {
116 	int answer;
117 
118 	nl_object_dump(obj, params);
119 	printf("Delete? (%c/%c) ",
120 		default_yes ? 'Y' : 'y',
121 		default_yes ? 'n' : 'N');
122 
123 	do {
124 		answer = tolower(getchar());
125 		if (answer == '\n')
126 			answer = default_yes ? 'y' : 'n';
127 	} while (answer != 'y' && answer != 'n');
128 
129 	return answer == 'y';
130 }
131 
nl_cli_alloc_cache(struct nl_sock * sock,const char * name,int (* ac)(struct nl_sock *,struct nl_cache **))132 struct nl_cache *nl_cli_alloc_cache(struct nl_sock *sock, const char *name,
133 			    int (*ac)(struct nl_sock *, struct nl_cache **))
134 {
135 	struct nl_cache *cache;
136 	int err;
137 
138 	if ((err = ac(sock, &cache)) < 0)
139 		nl_cli_fatal(err, "Unable to allocate %s cache: %s",
140 			     name, nl_geterror(err));
141 
142 	nl_cache_mngt_provide(cache);
143 
144 	return cache;
145 }
146 
147 /** @} */
148