fix #>&- syntax for closing fds
[oweals/busybox.git] / networking / ip.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * ip.c         "ip" utility frontend.
4  *
5  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6  *
7  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8  *
9  *
10  * Changes:
11  *
12  * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
13  * Bernhard Reutner-Fischer rewrote to use index_in_substr_array
14  */
15
16 #include "libbb.h"
17
18 #include "libiproute/utils.h"
19 #include "libiproute/ip_common.h"
20
21 #if ENABLE_FEATURE_IP_ADDRESS \
22  || ENABLE_FEATURE_IP_ROUTE \
23  || ENABLE_FEATURE_IP_LINK \
24  || ENABLE_FEATURE_IP_TUNNEL \
25  || ENABLE_FEATURE_IP_RULE
26
27 static int NORETURN ip_print_help(char **argv UNUSED_PARAM)
28 {
29         bb_show_usage();
30 }
31
32 static int ip_do(int (*ip_func)(char **argv), char **argv)
33 {
34         argv = ip_parse_common_args(argv + 1);
35         return ip_func(argv);
36 }
37
38 #if ENABLE_FEATURE_IP_ADDRESS
39 int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
40 int ipaddr_main(int argc UNUSED_PARAM, char **argv)
41 {
42         return ip_do(do_ipaddr, argv);
43 }
44 #endif
45 #if ENABLE_FEATURE_IP_LINK
46 int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
47 int iplink_main(int argc UNUSED_PARAM, char **argv)
48 {
49         return ip_do(do_iplink, argv);
50 }
51 #endif
52 #if ENABLE_FEATURE_IP_ROUTE
53 int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
54 int iproute_main(int argc UNUSED_PARAM, char **argv)
55 {
56         return ip_do(do_iproute, argv);
57 }
58 #endif
59 #if ENABLE_FEATURE_IP_RULE
60 int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
61 int iprule_main(int argc UNUSED_PARAM, char **argv)
62 {
63         return ip_do(do_iprule, argv);
64 }
65 #endif
66 #if ENABLE_FEATURE_IP_TUNNEL
67 int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
68 int iptunnel_main(int argc UNUSED_PARAM, char **argv)
69 {
70         return ip_do(do_iptunnel, argv);
71 }
72 #endif
73
74
75 int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
76 int ip_main(int argc UNUSED_PARAM, char **argv)
77 {
78         static const char keywords[] ALIGN1 =
79                 USE_FEATURE_IP_ADDRESS("address\0")
80                 USE_FEATURE_IP_ROUTE("route\0")
81                 USE_FEATURE_IP_LINK("link\0")
82                 USE_FEATURE_IP_TUNNEL("tunnel\0" "tunl\0")
83                 USE_FEATURE_IP_RULE("rule\0")
84                 ;
85         enum {
86                 USE_FEATURE_IP_ADDRESS(IP_addr,)
87                 USE_FEATURE_IP_ROUTE(IP_route,)
88                 USE_FEATURE_IP_LINK(IP_link,)
89                 USE_FEATURE_IP_TUNNEL(IP_tunnel, IP_tunl,)
90                 USE_FEATURE_IP_RULE(IP_rule,)
91                 IP_none
92         };
93         int (*ip_func)(char**) = ip_print_help;
94
95         argv = ip_parse_common_args(argv + 1);
96         if (*argv) {
97                 int key = index_in_substrings(keywords, *argv);
98                 argv++;
99 #if ENABLE_FEATURE_IP_ADDRESS
100                 if (key == IP_addr)
101                         ip_func = do_ipaddr;
102 #endif
103 #if ENABLE_FEATURE_IP_ROUTE
104                 if (key == IP_route)
105                         ip_func = do_iproute;
106 #endif
107 #if ENABLE_FEATURE_IP_LINK
108                 if (key == IP_link)
109                         ip_func = do_iplink;
110 #endif
111 #if ENABLE_FEATURE_IP_TUNNEL
112                 if (key == IP_tunnel || key == IP_tunl)
113                         ip_func = do_iptunnel;
114 #endif
115 #if ENABLE_FEATURE_IP_RULE
116                 if (key == IP_rule)
117                         ip_func = do_iprule;
118 #endif
119         }
120         return ip_func(argv);
121 }
122
123 #endif /* any of ENABLE_FEATURE_IP_xxx is 1 */