[Zrouter-src-freebsd] ZRouter.org: push to FreeBSD HEAD tree

zrouter-src-freebsd at zrouter.org zrouter-src-freebsd at zrouter.org
Tue Feb 28 11:41:37 UTC 2012


details:   http://zrouter.org/hg/FreeBSD/head//rev/3914c6e62521
changeset: 346:3914c6e62521
user:      ray at terran.dlink.ua
date:      Tue Feb 28 13:43:57 2012 +0200
description:
Tidy up, and add more methods.

diffstat:

 head/sbin/devctl/devctl.c |  118 ++++++++++++++++++++++++++++++---------------
 1 files changed, 79 insertions(+), 39 deletions(-)

diffs (152 lines):

diff -r 392322935f5b -r 3914c6e62521 head/sbin/devctl/devctl.c
--- a/head/sbin/devctl/devctl.c	Tue Feb 28 13:43:20 2012 +0200
+++ b/head/sbin/devctl/devctl.c	Tue Feb 28 13:43:57 2012 +0200
@@ -48,69 +48,109 @@
 
 static void	usage(void);
 
+struct cmd {
+	const char 	*name;
+	unsigned long 	ioctl;
+	int 		bargpos;
+	int 		dargpos;
+	int 		argc_min;
+	int		optnl;	/* Optional args count */
+} cmd[] = {
+	{ "attach",	DEVCTLADEV,	2,	4,	5,	1},
+	{ "hinted",	DEVCTLAHDEV,	2,	4,	5,	1},
+	{ "detach",	DEVCTLDDEV,	-1,	2,	4,	0},
+	{ "suspend",	DEVCTLSUSDEV,	-1,	2,	4,	0},
+	{ "resume",	DEVCTLRSMDEV,	-1,	2,	4,	0},
+	{ "shutdown",	DEVCTLSHTDEV,	-1,	2,	4,	0},
+	{ NULL, 0, 0, 0, 0, 0},
+};
+
+
 static void
-usage()
+usage(void)
 {
-	printf("devctl attach bus_name bus_unit dev_name [dev_unit]\n");
-	printf("devctl hinted bus_name bus_unit dev_name [dev_unit]\n");
-	printf("devctl detach bus_name bus_unit dev_name dev_unit\n");
+	printf("Usage:\n");
+	printf("\tdevctl attach bus_name bus_unit dev_name [dev_unit]\n");
+	printf("\tdevctl hinted bus_name bus_unit dev_name [dev_unit]\n");
+	printf("\tdevctl detach dev_name dev_unit\n");
 }
 
 int
 main(int argc, char **argv)
 {
-	struct devctl_attach_args	args;
+	struct devctl_attach_args args;
 	int fd, mode, error;
 
+	args.bus_name = NULL;
+	args.bus_unit = 0;
 	error = 0;
-	if (argc < 5) {
+	if (argc < 3) {
 		usage();
 		return (1);
 	}
 
-	if (strcmp(argv[1], "attach") == 0)
-		mode = MODE_ATTACH;
-	if (strcmp(argv[1], "hinted") == 0)
-		mode = MODE_HINTED;
-	if (strcmp(argv[1], "detach") == 0)
-		mode = MODE_DETACH;
-	if ((mode != MODE_ATTACH) &&
-	    (mode != MODE_HINTED) &&
-	    (mode != MODE_DETACH))
-		err(1, "can only attach/hinted/detach device");
+	for (mode = 0; cmd[mode].name; mode ++) {
+		if (strcmp(argv[1], cmd[mode].name) == 0)
+			break;
+	}
+	if (cmd[mode].name == NULL) {
+		printf("Unsupported command \"%s\".\n", argv[1]);
+		usage();
+		/* support embedd into sh(1) */
+		return (1);
+	}
 
-	if ((mode == MODE_DETACH) && argc < 6)
-		err(1, "explicit device unit number required for detach");
+	if (argc < cmd[mode].argc_min) {
+		printf("Arguments list to short.\n");
+		usage();
+		return (1);
+	}
 
-	args.bus_name = argv[2];
-	args.bus_unit = strtoul(argv[3], 0, 0);
-	args.dev_name = argv[4];
-	if (argc == 6) {
-		args.dev_unit = strtoul(argv[5], 0, 0);
+	if (argc > (cmd[mode].argc_min + cmd[mode].optnl)) {
+		printf("Too much arguments, check command-line please.\n");
+		usage();
+		return (1);
+	}
+
+	/* Get bus class name and bus unit */
+	if (cmd[mode].bargpos != -1) {
+		args.bus_name = argv[cmd[mode].bargpos];
+		args.bus_unit = strtoul(argv[cmd[mode].bargpos + 1], 0, 0);
+	}
+
+	/* Get device class name */
+	args.dev_name = argv[cmd[mode].dargpos];
+
+	/* Get device unit */
+	if (argc > cmd[mode].argc_min) {
+		args.dev_unit = strtoul(argv[cmd[mode].dargpos + 1], 0, 0);
 	} else {
 		args.dev_unit = -1;
 	}
 
+#ifdef DEBUG
+	printf("Mode \"%s\"\n"
+		    "\tbus = %s%d\n"
+		    "\tdevice = %s%d\n",
+		cmd[mode].name,
+		(cmd[mode].bargpos != -1)?args.bus_name:"any",
+		(cmd[mode].bargpos != -1)?args.bus_unit:0,
+		args.dev_name,
+		args.dev_unit
+	);
+	exit(0);
+#endif
+
 	fd = open(DEVCTL, O_RDONLY);
-	if (fd == -1)
-		err(1, "can't open " DEVCTL " file");
-
-
-	switch (mode) {
-	case MODE_ATTACH:
-		error = ioctl(fd, DEVCTLADEV, &args);
-		break;
-	case MODE_HINTED:
-		error = ioctl(fd, DEVCTLAHDEV, &args);
-		break;
-	case MODE_DETACH:
-		error = ioctl(fd, DEVCTLDDEV, &args);
-		break;
+	if (fd == -1) {
+		warn("can't open " DEVCTL " file");
+		return (1);
 	}
-
+	error = ioctl(fd, cmd[mode].ioctl, &args);
 	if (error == -1) {
 		close(fd);
-		err(1, "ioctl failed");
+		warn("ioctl failed");
+		return (1);
 	}
 
 	close(fd);


More information about the Zrouter-src-freebsd mailing list