PERFORCE change 64115 for review

Sam Leffler sam at FreeBSD.org
Tue Nov 2 13:38:15 PST 2004


http://perforce.freebsd.org/chv.cgi?CH=64115

Change 64115 by sam at sam_ebb on 2004/11/02 21:37:23

	o add private command line option support and use it for -L
	  and -C options
	o purge some dead variables
	o minor whitespace cleanups
	o static'ize some stuff
	o give next member in struct cmd a consistent name
	
	This eliminates the INET6 build-time dependency.

Affected files ...

.. //depot/projects/wifi/sbin/ifconfig/af_inet6.c#2 edit
.. //depot/projects/wifi/sbin/ifconfig/ifclone.c#2 edit
.. //depot/projects/wifi/sbin/ifconfig/ifconfig.c#3 edit
.. //depot/projects/wifi/sbin/ifconfig/ifconfig.h#3 edit

Differences ...

==== //depot/projects/wifi/sbin/ifconfig/af_inet6.c#2 (text+ko) ====

@@ -526,6 +526,13 @@
 	.af_addreq	= &in6_addreq,
 };
 
+static void
+in6_Lopt_cb(const char *optarg __unused)
+{
+	ip6lifetime++;	/* print IPv6 address lifetime */
+}
+static struct option in6_Lopt = { "L", "[-L]", in6_Lopt_cb };
+
 static __constructor void
 inet6_ctor(void)
 {
@@ -535,5 +542,6 @@
 	for (i = 0; i < N(inet6_cmds);  i++)
 		cmd_register(&inet6_cmds[i]);
 	af_register(&af_inet6);
+	opt_register(&in6_Lopt);
 #undef N
 }

==== //depot/projects/wifi/sbin/ifconfig/ifclone.c#2 (text+ko) ====

@@ -45,7 +45,7 @@
 
 #include "ifconfig.h"
 
-void
+static void
 list_cloners(void)
 {
 	struct if_clonereq ifcr;
@@ -134,6 +134,14 @@
 	{ "unplumb",	0,	clone_destroy },
 };
 
+static void
+clone_Copt_cb(const char *optarg __unused)
+{
+	list_cloners();
+	exit(0);
+}
+static struct option clone_Copt = { "C", "[-C]", clone_Copt_cb };
+
 static __constructor void
 clone_ctor(void)
 {
@@ -142,5 +150,6 @@
 
 	for (i = 0; i < N(clone_cmds);  i++)
 		cmd_register(&clone_cmds[i]);
+	opt_register(&clone_Copt);
 #undef N
 }

==== //depot/projects/wifi/sbin/ifconfig/ifconfig.c#3 (text+ko) ====

@@ -79,9 +79,6 @@
  * (.e.g. little/big endian difference in the structure.)
  */
 struct	ifreq ifr;
-struct	ifreq ridreq;
-struct	ifaliasreq addreq;
-struct	sockaddr_in netmask;
 
 char	name[IFNAMSIZ];
 int	flags;
@@ -93,69 +90,76 @@
 int	newaddr = 1;
 int	verbose;
 
-int supmedia = 0;
-int listcloners = 0;
-int printname = 0;		/* Print the name of the created interface. */
+int	supmedia = 0;
+int	printname = 0;		/* Print the name of the created interface. */
 
-int	ifconfig(int argc, char *const *argv, const struct afswtch *afp);
-void	status(const struct afswtch *afp, int addrcount,
+static	int ifconfig(int argc, char *const *argv, const struct afswtch *afp);
+static	void status(const struct afswtch *afp, int addrcount,
 		    struct sockaddr_dl *sdl, struct if_msghdr *ifm,
 		    struct ifa_msghdr *ifam);
-void	tunnel_status(int s);
-void	usage(void);
-void	ifmaybeload(char *name);
+static	void tunnel_status(int s);
+static	void usage(void);
 
 static struct afswtch *af_getbyname(const char *name);
 static struct afswtch *af_getbyfamily(int af);
 static void af_all_status(int, const struct rt_addrinfo *sdl);
 
+static struct option *opts = NULL;
+
 void
+opt_register(struct option *p)
+{
+	p->next = opts;
+	opts = p;
+}
+
+static void
 usage(void)
 {
-#ifndef INET6
-	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
-	"usage: ifconfig interface address_family [address [dest_address]]",
-	"                [parameters]",
-	"       ifconfig -C",
-	"       ifconfig interface create",
-	"       ifconfig -a [-d] [-m] [-u] [address_family]",
-	"       ifconfig -l [-d] [-u] [address_family]",
-	"       ifconfig [-d] [-m] [-u]");
-#else
-	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
-	"usage: ifconfig [-L] interface address_family [address [dest_address]]",
-	"                [parameters]",
-	"       ifconfig -C",
-	"       ifconfig interface create",
-	"       ifconfig -a [-L] [-d] [-m] [-u] [address_family]",
-	"       ifconfig -l [-d] [-u] [address_family]",
-	"       ifconfig [-L] [-d] [-m] [-u]");
-#endif
+	char options[1024];
+	struct option *p;
+
+	/* XXX not right but close enough for now */
+	options[0] = '\0';
+	for (p = opts; p != NULL; p = p->next) {
+		strlcat(options, p->opt_usage, sizeof(options));
+		strlcat(options, " ", sizeof(options));
+	}
+
+	fprintf(stderr,
+	"usage: ifconfig %sinterface address_family [address [dest_address]]\n"
+	"                [parameters]\n"
+	"       ifconfig interface create\n"
+	"       ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
+	"       ifconfig -l [-d] [-u] [address_family]\n"
+	"       ifconfig %s[-d] [-m] [-u] [-v]\n",
+		options, options, options);
 	exit(1);
 }
 
 int
 main(int argc, char *argv[])
 {
-	int c;
-	int all, namesonly, downonly, uponly;
+	int c, all, namesonly, downonly, uponly;
 	int need_nl = 0, count = 0;
-	const struct afswtch *afp = 0;
+	const struct afswtch *afp = NULL;
 	int addrcount, ifindex;
-	struct	if_msghdr *ifm, *nextifm;
-	struct	ifa_msghdr *ifam;
-	struct	sockaddr_dl *sdl;
-	char	*buf, *lim, *next;
+	struct if_msghdr *ifm, *nextifm;
+	struct ifa_msghdr *ifam;
+	struct sockaddr_dl *sdl;
+	char *buf, *lim, *next;
 	size_t needed;
 	int mib[6];
+	char options[1024];
+	struct option *p;
+
+	all = downonly = uponly = namesonly = verbose = 0;
 
 	/* Parse leading line options */
-	all = downonly = uponly = namesonly = verbose = 0;
-	while ((c = getopt(argc, argv, "adlmuvC"
-#ifdef INET6
-					"L"
-#endif
-			)) != -1) {
+	strlcpy(options, "adlmuv", sizeof(options));
+	for (p = opts; p != NULL; p = p->next)
+		strlcat(options, p->opt, sizeof(options));
+	while ((c = getopt(argc, argv, options)) != -1) {
 		switch (c) {
 		case 'a':	/* scan all interfaces */
 			all++;
@@ -172,35 +176,23 @@
 		case 'u':	/* restrict scan to "up" interfaces */
 			uponly++;
 			break;
-		case 'C':
-			listcloners = 1;
-			break;
-#ifdef INET6
-		case 'L':
-			ip6lifetime++;	/* print IPv6 address lifetime */
-			break;
-#endif
 		case 'v':
 			verbose++;
 			break;
 		default:
-			usage();
+			for (p = opts; p != NULL; p = p->next)
+				if (p->opt[0] == c) {
+					p->cb(optarg);
+					break;
+				}
+			if (p == NULL)
+				usage();
 			break;
 		}
 	}
 	argc -= optind;
 	argv += optind;
 
-	if (listcloners) {
-		/* -C must be solitary */
-		if (all || supmedia || uponly || downonly || namesonly ||
-		    argc > 0)
-			usage();
-		
-		list_cloners();
-		exit(0);
-	}
-
 	/* -l cannot be used with -a or -m */
 	if (namesonly && (all || supmedia))
 		usage();
@@ -271,7 +263,7 @@
 	mib[5] = ifindex;		/* interface index */
 
 	/* if particular family specified, only ask about it */
-	if (afp)
+	if (afp != NULL)
 		mib[3] = afp->af_af;
 
 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
@@ -437,7 +429,7 @@
 void
 cmd_register(struct cmd *p)
 {
-	p->next = cmds;
+	p->c_next = cmds;
 	cmds = p;
 }
 
@@ -447,7 +439,7 @@
 #define	N(a)	(sizeof(a)/sizeof(a[0]))
 	const struct cmd *p;
 
-	for (p = cmds; p != NULL; p = p->next)
+	for (p = cmds; p != NULL; p = p->c_next)
 		if (strcmp(name, p->c_name) == 0)
 			return p;
 	return NULL;
@@ -461,7 +453,7 @@
 static void setifdstaddr(const char *, int, int, const struct afswtch *);
 static const struct cmd setifdstaddr_cmd = { "ifdstaddr", 0, setifdstaddr };
 
-int
+static int
 ifconfig(int argc, char *const *argv, const struct afswtch *afp)
 {
 	int s;
@@ -793,7 +785,7 @@
  * Print the status of the interface.  If an address family was
  * specified, show only it; otherwise, show them all.
  */
-void
+static void
 status(const struct afswtch *afp, int addrcount, struct	sockaddr_dl *sdl,
     struct if_msghdr *ifm, struct ifa_msghdr *ifam)
 {
@@ -864,7 +856,7 @@
 	return;
 }
 
-void
+static void
 tunnel_status(int s)
 {
 	af_all_tunnel_status(s);

==== //depot/projects/wifi/sbin/ifconfig/ifconfig.h#3 (text+ko) ====

@@ -43,14 +43,14 @@
 typedef	void c_func2(const char *arg, const char *arg2, int s, const struct afswtch *afp);
 
 struct cmd {
-	const	char *c_name;
+	const char *c_name;
 	int	c_parameter;
 #define	NEXTARG		0xffffff	/* has following arg */
 #define	NEXTARG2	0xfffffe	/* has 2 following args */
 #define	OPTARG		0xfffffd	/* has optional following arg */
 	c_func	*c_func;
 	c_func2	*c_func2;
-	struct cmd *next;
+	struct cmd *c_next;
 };
 void	cmd_register(struct cmd *);
 
@@ -87,6 +87,14 @@
 };
 void	af_register(struct afswtch *);
 
+struct option {
+	const char *opt;
+	const char *opt_usage;
+	void	(*cb)(const char *arg);
+	struct option *next;
+};
+void	opt_register(struct option *);
+
 extern	struct ifreq ifr;
 extern	char name[IFNAMSIZ];	/* name of interface */
 extern	int allmedia;
@@ -102,5 +110,6 @@
 void	Perror(const char *cmd);
 void	printb(const char *s, unsigned value, const char *bits);
 
-void	list_cloners(void);
+void	ifmaybeload(char *name);
+
 void	clone_create(void);


More information about the p4-projects mailing list