libnl 2.0
|
00001 /* 00002 * src/utils.c Utilities 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Lesser General Public 00006 * License as published by the Free Software Foundation version 2.1 00007 * of the License. 00008 * 00009 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch> 00010 */ 00011 00012 /** 00013 * @defgroup cli Command Line Interface API 00014 * 00015 * @{ 00016 */ 00017 00018 #include <netlink/cli/utils.h> 00019 00020 uint32_t nl_cli_parse_u32(const char *arg) 00021 { 00022 unsigned long lval; 00023 char *endptr; 00024 00025 lval = strtoul(arg, &endptr, 0); 00026 if (endptr == arg || lval == ULONG_MAX) 00027 nl_cli_fatal(EINVAL, "Unable to parse \"%s\", not a number.", 00028 arg); 00029 00030 return (uint32_t) lval; 00031 } 00032 00033 void nl_cli_print_version(void) 00034 { 00035 printf("libnl tools version %s\n", LIBNL_VERSION); 00036 printf( 00037 "Copyright (C) 2003-2009 Thomas Graf <tgraf@redhat.com>\n" 00038 "\n" 00039 "This program comes with ABSOLUTELY NO WARRANTY. This is free \n" 00040 "software, and you are welcome to redistribute it under certain\n" 00041 "conditions. See the GNU General Public License for details.\n" 00042 ); 00043 00044 exit(0); 00045 } 00046 00047 void nl_cli_fatal(int err, const char *fmt, ...) 00048 { 00049 va_list ap; 00050 00051 fprintf(stderr, "Error: "); 00052 00053 if (fmt) { 00054 va_start(ap, fmt); 00055 vfprintf(stderr, fmt, ap); 00056 va_end(ap); 00057 fprintf(stderr, "\n"); 00058 } else 00059 fprintf(stderr, "%s\n", strerror(err)); 00060 00061 exit(abs(err)); 00062 } 00063 00064 int nl_cli_connect(struct nl_sock *sk, int protocol) 00065 { 00066 int err; 00067 00068 if ((err = nl_connect(sk, protocol)) < 0) 00069 nl_cli_fatal(err, "Unable to connect netlink socket: %s", 00070 nl_geterror(err)); 00071 00072 return err; 00073 } 00074 00075 struct nl_sock *nl_cli_alloc_socket(void) 00076 { 00077 struct nl_sock *sock; 00078 00079 if (!(sock = nl_socket_alloc())) 00080 nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket"); 00081 00082 return sock; 00083 } 00084 00085 struct nl_addr *nl_cli_addr_parse(const char *str, int family) 00086 { 00087 struct nl_addr *addr; 00088 int err; 00089 00090 if ((err = nl_addr_parse(str, family, &addr)) < 0) 00091 nl_cli_fatal(err, "Unable to parse address \"%s\": %s", 00092 str, nl_geterror(err)); 00093 00094 return addr; 00095 } 00096 00097 int nl_cli_parse_dumptype(const char *str) 00098 { 00099 if (!strcasecmp(str, "brief")) 00100 return NL_DUMP_LINE; 00101 else if (!strcasecmp(str, "details") || !strcasecmp(str, "detailed")) 00102 return NL_DUMP_DETAILS; 00103 else if (!strcasecmp(str, "stats")) 00104 return NL_DUMP_STATS; 00105 else if (!strcasecmp(str, "env")) 00106 return NL_DUMP_ENV; 00107 else 00108 nl_cli_fatal(EINVAL, "Invalid dump type \"%s\".\n", str); 00109 00110 return 0; 00111 } 00112 00113 int nl_cli_confirm(struct nl_object *obj, struct nl_dump_params *params, 00114 int default_yes) 00115 { 00116 int answer; 00117 00118 nl_object_dump(obj, params); 00119 printf("Delete? (%c/%c) ", 00120 default_yes ? 'Y' : 'y', 00121 default_yes ? 'n' : 'N'); 00122 00123 do { 00124 answer = tolower(getchar()); 00125 if (answer == '\n') 00126 answer = default_yes ? 'y' : 'n'; 00127 } while (answer != 'y' && answer != 'n'); 00128 00129 return answer == 'y'; 00130 } 00131 00132 struct nl_cache *nl_cli_alloc_cache(struct nl_sock *sock, const char *name, 00133 int (*ac)(struct nl_sock *, struct nl_cache **)) 00134 { 00135 struct nl_cache *cache; 00136 int err; 00137 00138 if ((err = ac(sock, &cache)) < 0) 00139 nl_cli_fatal(err, "Unable to allocate %s cache: %s", 00140 name, nl_geterror(err)); 00141 00142 nl_cache_mngt_provide(cache); 00143 00144 return cache; 00145 } 00146 00147 /** @} */