ip: stop propagating argc; optimize ip_parse_common_args
[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 family_t preferred_family = AF_UNSPEC;
22 smallint oneline;
23 char _SL_;
24
25 char **ip_parse_common_args(char **argv)
26 {
27         static const char ip_common_commands[] ALIGN1 =
28                 "oneline" "\0"
29                 "family" "\0"
30                 "4" "\0"
31                 "6" "\0"
32                 "0" "\0"
33                 ;
34         enum {
35                 ARG_oneline,
36                 ARG_family,
37                 ARG_IPv4,
38                 ARG_IPv6,
39                 ARG_packet,
40         };
41         static const family_t af_numbers[] = { AF_INET, AF_INET6, AF_PACKET };
42         int arg;
43
44         while (*argv) {
45                 char *opt = *argv;
46
47                 if (opt[0] != '-')
48                         break;
49                 opt++;
50                 if (opt[0] == '-') {
51                         opt++;
52                         if (!opt[0]) { /* "--" */
53                                 argv++;
54                                 break;
55                         }
56                 }
57                 arg = index_in_strings(ip_common_commands, opt);
58                 if (arg < 0)
59                         bb_show_usage();
60                 if (arg == ARG_oneline) {
61                         oneline = 1;
62                         argv++;
63                         continue;
64                 }
65                 if (arg == ARG_family) {
66                         static const char families[] ALIGN1 =
67                                 "inet" "\0" "inet6" "\0" "link" "\0";
68                         argv++;
69                         if (!*argv)
70                                 bb_show_usage();
71                         arg = index_in_strings(families, *argv);
72                         if (arg < 0)
73                                 invarg(*argv, "protocol family");
74                         /* now arg == 0, 1 or 2 */
75                 } else {
76                         arg -= ARG_IPv4;
77                         /* now arg == 0, 1 or 2 */
78                 }
79                 preferred_family = af_numbers[arg];
80                 argv++;
81         }
82         _SL_ = oneline ? '\\' : '\n';
83         return argv;
84 }