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