- move iprule and ipaddress from matches() to index_in_str_array
[oweals/busybox.git] / networking / libiproute / ip_parse_common_args.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * ip.c         "ip" utility frontend.
4  *
5  *              This program is free software; you can redistribute it and/or
6  *              modify it under the terms of the GNU General Public License
7  *              as published by the Free Software Foundation; either version
8  *              2 of the License, or (at your option) any later version.
9  *
10  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11  *
12  *
13  * Changes:
14  *
15  * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
16  */
17
18 #include "ip_common.h"  /* #include "libbb.h" is inside */
19 #include "utils.h"
20
21 int preferred_family = AF_UNSPEC;
22 smallint oneline;
23 char _SL_;
24
25 void ip_parse_common_args(int *argcp, char ***argvp)
26 {
27         int argc = *argcp;
28         char **argv = *argvp;
29         static const char * const ip_common_commands[] =
30                 {"-family", "inet", "inet6", "link",
31                  "-4", "-6", "-0", "-oneline", 0};
32         enum {
33                 ARG_family = 1,
34                 ARG_inet,
35                 ARG_inet6,
36                 ARG_link,
37                 ARG_IPv4,
38                 ARG_IPv6,
39                 ARG_packet,
40                 ARG_oneline
41         };
42         smalluint arg;
43
44         while (argc > 1) {
45                 char *opt = argv[1];
46
47                 if (strcmp(opt,"--") == 0) {
48                         argc--;
49                         argv++;
50                         break;
51                 }
52                 if (opt[0] != '-')
53                         break;
54                 if (opt[1] == '-')
55                         opt++;
56                 arg = index_in_str_array(ip_common_commands, opt) + 1;
57                 if (arg == ARG_family) {
58                         argc--;
59                         argv++;
60                         if (!argv[1])
61                                 bb_show_usage();
62                         arg = index_in_str_array(ip_common_commands, argv[1]) + 1;
63                         if (arg == ARG_inet)
64                                 preferred_family = AF_INET;
65                         else if (arg == ARG_inet6)
66                                 preferred_family = AF_INET6;
67                         else if (arg == ARG_link)
68                                 preferred_family = AF_PACKET;
69                         else
70                                 invarg(argv[1], "protocol family");
71                 } else if (arg == ARG_IPv4) {
72                         preferred_family = AF_INET;
73                 } else if (arg == ARG_IPv6) {
74                         preferred_family = AF_INET6;
75                 } else if (arg == ARG_packet) {
76                         preferred_family = AF_PACKET;
77                 } else if (arg == ARG_oneline) {
78                         ++oneline;
79                 } else {
80                         bb_show_usage();
81                 }
82                 argc--;
83                 argv++;
84         }
85         _SL_ = oneline ? '\\' : '\n' ;
86         *argcp = argc;
87         *argvp = argv;
88 }