[openib-general] [patch] userspace/management: ARGBEGIN() -> getopt() conversion for diags

Sasha Khapyorsky sashak at voltaire.com
Wed Jan 18 06:28:53 PST 2006


Hi Hal,

Diag utils are converted to getopt(). It is just basically tested,
so please report bugs (if any).

Sasha.


This converts diag utils to more standard getopt() using instead of
AGRBEGIN() buggy macros. Unused now ARGBEGIN() related code is
removed from libibcommon.

Signed-off-by: Sasha Khapyorsky <sashak at voltaire.com>

Index: diags/src/ibtracert.c
===================================================================
--- diags/src/ibtracert.c	(revision 5057)
+++ diags/src/ibtracert.c	(working copy)
@@ -41,6 +41,7 @@
 #include <unistd.h>
 #include <stdarg.h>
 #include <ctype.h>
+#include <getopt.h>
 #include <netinet/in.h>
 
 #include <common.h>
@@ -54,6 +55,8 @@
 int force;
 FILE *f;
 
+static char *argv0 = "ibtracert";
+
 #undef DEBUG
 #define	DEBUG	if (ibdebug || verbose) IBWARN
 #define	VERBOSE	if (ibdebug || verbose > 1) IBWARN
@@ -726,7 +729,7 @@
 		basename++;
 
 	fprintf(stderr, "Usage: %s [-d(ebug) -v(erbose) -D(irect_path_addrs) -G(uid_addrs) -v(erbose) -n(o_info) -C ca_name -P hca_port "
-			"-s smlid -t timeout_ms -m mlid] <src-addr> <dest-addr>\n",
+			"-s smlid -t(imeout) timeout_ms -m mlid] <src-addr> <dest-addr>\n",
 			basename);
 	fprintf(stderr, "\n\tUnicast examples:\n");
 	fprintf(stderr, "\t\t%s 4 16\t\t\t# show path between lids 4 and 16\n", basename);
@@ -753,57 +756,85 @@
 	char *ca = 0;
 	int ca_port = 0;
 
+	static char const str_opts[] = "C:P:t:s:m:dvfDGnVhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "verbose", 0, 0, 'v'},
+		{ "force", 0, 0, 'f'},
+		{ "Direct_path_addrs", 0, 0, 'D'},
+		{ "Guid_addrs", 0, 0, 'G'},
+		{ "no_info", 0, 0, 'n'},
+		{ "timeout", 1, 0, 't'},
+		{ "s", 1, 0, 's'},
+		{ "m", 1, 0, 'm'},
+		{ "Version", 0, 0, 'V'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
+
+	argv0 = argv[0];
+
 	f = stderr;
 
-	ARGBEGIN {
-	case 'C':
-		ca = ARGF();
-		break;
-	case 'P':
-		ca_port = strtoul(ARGF(), 0, 0);
-		break;
-	case 'd':
-		ibdebug++;
-		madrpc_show_errors(1);
-		umad_debug(udebug);
-		udebug++;
-		break;
-	case 'D':
-		dest_type = IB_DEST_DRPATH;
-		break;
-	case 'G':
-		dest_type = IB_DEST_GUID;
-		break;
-	case 'm':
-		multicast++;
-		mlid = strtoul(ARGF(), 0, 0);
-		break;
-	case 'f':
-		force++;
-		break;
-	case 'n':
-		dumplevel = 1;
-		break;
-	case 's':
-		if (ib_resolve_portid_str(&sm_portid, ARGF(), IB_DEST_LID, 0) < 0)
-			IBERROR("can't resolve SM destination port %s", ARGF());
-		sm_id = &sm_portid;
-		break;
-	case 't':
-		timeout = strtoul(ARGF(), 0, 0);
-		madrpc_set_timeout(timeout);
-		break;
-	case 'v':
-		madrpc_show_errors(1);
-		verbose++;
-		break;
-	case 'V':
-		fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-		exit(-1);
-	case 'h':
-	default:
-		usage();
-	} ARGEND;
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'C':
+			ca = optarg;
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 'd':
+			ibdebug++;
+			madrpc_show_errors(1);
+			umad_debug(udebug);
+			udebug++;
+			break;
+		case 'D':
+			dest_type = IB_DEST_DRPATH;
+			break;
+		case 'G':
+			dest_type = IB_DEST_GUID;
+			break;
+		case 'm':
+			multicast++;
+			mlid = strtoul(optarg, 0, 0);
+			break;
+		case 'f':
+			force++;
+			break;
+		case 'n':
+			dumplevel = 1;
+			break;
+		case 's':
+			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
+				IBERROR("can't resolve SM destination port %s", optarg);
+			sm_id = &sm_portid;
+			break;
+		case 't':
+			timeout = strtoul(optarg, 0, 0);
+			madrpc_set_timeout(timeout);
+			break;
+		case 'v':
+			madrpc_show_errors(1);
+			verbose++;
+			break;
+		case 'V':
+			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+			exit(-1);
+		case 'h':
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
 
 	if (argc < 2)
 		usage();
Index: diags/src/ibnetdiscover.c
===================================================================
--- diags/src/ibnetdiscover.c	(revision 5057)
+++ diags/src/ibnetdiscover.c	(working copy)
@@ -42,6 +42,7 @@
 #include <stdarg.h>
 #include <time.h>
 #include <string.h>
+#include <getopt.h>
 
 #include <common.h>
 #include <umad.h>
@@ -60,6 +61,8 @@
 #define	DEBUG	if (verbose>1) IBWARN
 #define IBERROR(fmt, args...)	iberror(__FUNCTION__, fmt, ## args)
 
+static char *argv0 = "ibnetdiscover";
+
 void
 iberror(const char *fn, char *msg, ...)
 {
@@ -553,8 +556,8 @@
 void
 usage(void)
 {
-	fprintf(stderr, "Usage: %s [-d(ebug)] -e(err_show) -v(erbose) -s(how) -l(ist) -H(ca_list) -S(witch_list) -V(ersion) -C ca_name -P hca_port "
-			"-t timeout_ms] [<netfile>]\n",
+	fprintf(stderr, "Usage: %s [-d(ebug)] -e(rr_show) -v(erbose) -s(how) -l(ist) -H(ca_list) -S(witch_list) -V(ersion) -C ca_name -P hca_port "
+			"-t(imeout) timeout_ms] [<netfile>]\n",
 			argv0);
 	fprintf(stderr, "%s %s\n", argv0, get_build_version() );
 	exit(-1);
@@ -570,50 +573,77 @@
 	char *ca = 0;
 	int ca_port = 0;
 
+	static char const str_opts[] = "C:P:t:devslHSVhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "err_show", 0, 0, 'e'},
+		{ "verbose", 0, 0, 'v'},
+		{ "show", 0, 0, 's'},
+		{ "list", 0, 0, 'l'},
+		{ "Hca_list", 0, 0, 'H'},
+		{ "Switch_list", 0, 0, 'S'},
+		{ "timeout", 1, 0, 't'},
+		{ "Version", 0, 0, 'V'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
+
 	f = stdout;
 
-	ARGBEGIN {
-	case 'C':
-		ca = ARGF();
-		break;
-	case 'P':
-		ca_port = strtoul(ARGF(), 0, 0);
-		break;
-	case 'd':
-		ibdebug++;
-		madrpc_show_errors(1);
-		umad_debug(udebug);
-		udebug++;
-		break;
-	case 't':
-		timeout = strtoul(ARGF(), 0, 0);
-		break;
-	case 'v':
-		verbose++;
-		dumplevel++;
-		break;
-	case 's':
-		dumplevel = 1;
-		break;
-	case 'e':
-		madrpc_show_errors(1);
-		break;
-	case 'l':
-		list = HCA_NODE | SWITCH_NODE;
-		break;
-	case 'S':
-		list = SWITCH_NODE;
-		break;
-	case 'H':
-		list = HCA_NODE;
-		break;
-	case 'V':
-		fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-		exit(-1);
-	default:
-		usage();
-	} ARGEND;
+	argv0 = argv[0];
 
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'C':
+			ca = optarg;
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 'd':
+			ibdebug++;
+			madrpc_show_errors(1);
+			umad_debug(udebug);
+			udebug++;
+			break;
+		case 't':
+			timeout = strtoul(optarg, 0, 0);
+			break;
+		case 'v':
+			verbose++;
+			dumplevel++;
+			break;
+		case 's':
+			dumplevel = 1;
+			break;
+		case 'e':
+			madrpc_show_errors(1);
+			break;
+		case 'l':
+			list = HCA_NODE | SWITCH_NODE;
+			break;
+		case 'S':
+			list = SWITCH_NODE;
+			break;
+		case 'H':
+			list = HCA_NODE;
+			break;
+		case 'V':
+			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+			exit(-1);
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (argc)
 		if (!(f = fopen(argv[0], "w")))
 			IBERROR("can't open file %s for writing", argv[0]);
Index: diags/src/ibportstate.c
===================================================================
--- diags/src/ibportstate.c	(revision 5057)
+++ diags/src/ibportstate.c	(working copy)
@@ -42,6 +42,7 @@
 #include <stdarg.h>
 #include <time.h>
 #include <string.h>
+#include <getopt.h>
 
 #include <common.h>
 #include <umad.h>
@@ -54,6 +55,8 @@
 static int dest_type = IB_DEST_LID;
 static int verbose;
 
+static char *argv0 = "ibportstate";
+
 static void
 iberror(const char *fn, char *msg, ...)
 {
@@ -142,7 +145,7 @@
 		basename++;
 
 	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -D(irect) -G(uid) -s smlid -V(ersion) -C ca_name -P hca_port "
-			"-t timeout_ms] <dest dr_path|lid|guid> <portnum> [<op>]\n",
+			"-t(imeout) timeout_ms] <dest dr_path|lid|guid> <portnum> [<op>]\n",
 			basename);
 	fprintf(stderr, "\tsupported ops: enable, disable, query\n");
 	fprintf(stderr, "\n\texamples:\n");
@@ -166,48 +169,74 @@
 	char *err;
 	char data[IB_SMP_DATA_SIZE];
 
-	ARGBEGIN {
-	case 'd':
-		ibdebug++;
-		madrpc_show_errors(1);
-		umad_debug(udebug);
-		udebug++;
-		break;
-	case 'e':
-		madrpc_show_errors(1);
-		break;
-	case 'D':
-		dest_type = IB_DEST_DRPATH;
-		break;
-	case 'G':
-		dest_type = IB_DEST_GUID;
-		break;
-	case 'C':
-		ca = ARGF();
-		break;
-	case 'P':
-		ca_port = strtoul(ARGF(), 0, 0);
-		break;
-	case 's':
-		if (ib_resolve_portid_str(&sm_portid, ARGF(), IB_DEST_LID, 0) < 0)
-			IBERROR("can't resolve SM destination port %s", ARGF());
-		sm_id = &sm_portid;
-		break;
-	case 't':
-		timeout = strtoul(ARGF(), 0, 0);
-		madrpc_set_timeout(timeout);
-		break;
-	case 'v':
-		verbose++;
-		break;
-	case 'V':
-		fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-		exit(-1);
-	case 'h':
-	default:
-		usage();
-	} ARGEND;
+	static char const str_opts[] = "C:P:t:s:devDGVhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "err_show", 0, 0, 'e'},
+		{ "verbose", 0, 0, 'v'},
+		{ "Direct", 0, 0, 'D'},
+		{ "Guid", 0, 0, 'G'},
+		{ "timeout", 1, 0, 't'},
+		{ "s", 1, 0, 's'},
+		{ "Version", 0, 0, 'V'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
 
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'd':
+			ibdebug++;
+			madrpc_show_errors(1);
+			umad_debug(udebug);
+			udebug++;
+			break;
+		case 'e':
+			madrpc_show_errors(1);
+			break;
+		case 'D':
+			dest_type = IB_DEST_DRPATH;
+			break;
+		case 'G':
+			dest_type = IB_DEST_GUID;
+			break;
+		case 'C':
+			ca = optarg;
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 's':
+			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
+				IBERROR("can't resolve SM destination port %s", optarg);
+			sm_id = &sm_portid;
+			break;
+		case 't':
+			timeout = strtoul(optarg, 0, 0);
+			madrpc_set_timeout(timeout);
+			break;
+		case 'v':
+			verbose++;
+			break;
+		case 'V':
+			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+			exit(-1);
+		case 'h':
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (argc < 2)
 		usage();
 
Index: diags/src/perfquery.c
===================================================================
--- diags/src/perfquery.c	(revision 5057)
+++ diags/src/perfquery.c	(working copy)
@@ -40,6 +40,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <getopt.h>
 
 #include <common.h>
 #include <umad.h>
@@ -47,6 +48,8 @@
 
 static uint8_t pc[1024];
 
+static char *argv0 = "perfquery";
+
 #define IBERROR(fmt, args...)	iberror(__FUNCTION__, fmt, ## args)
 
 static void
@@ -80,7 +83,7 @@
 		basename++;
 
 	fprintf(stderr, "Usage: %s [-d(ebug) -G(uid_addr) -a(ll_ports) -r(reset_after_read) -C ca_name -P hca_port "
-			"-R(eset_only) -t timeout_ms -V(ersion) -h(elp)] [<lid|guid> [[port] [reset_mask]]]\n",
+			"-R(eset_only) -t(imeout) timeout_ms -V(ersion) -h(elp)] [<lid|guid> [[port] [reset_mask]]]\n",
 			basename);
 	fprintf(stderr, "\tExamples:\n");
 	fprintf(stderr, "\t\t%s\t\t# read local port's performance counters\n", basename);
@@ -110,49 +113,75 @@
 	char *ca = 0;
 	int ca_port = 0;
 
-	ARGBEGIN {
-	case 'C':
-		ca = ARGF();
-		break;
-	case 'P':
-		ca_port = strtoul(ARGF(), 0, 0);
-		break;
-	case 'a':
-		all++;
-		port = 0xff;
-		break;
-	case 'd':
-		ibdebug++;
-		madrpc_show_errors(1);
-		umad_debug(udebug);
-		udebug++;
-		break;
-	case 'G':
-		dest_type = IB_DEST_GUID;
-		break;
-	case 's':
-		if (ib_resolve_portid_str(&sm_portid, ARGF(), IB_DEST_LID, 0) < 0)
-			IBERROR("can't resolve SM destination port %s", ARGF());
-		sm_id = &sm_portid;
-		break;
-	case 'r':
-		reset++;
-		break;
-	case 'R':
-		reset_only++;
-		break;
-	case 't':
-		timeout = strtoul(ARGF(), 0, 0);
-		madrpc_set_timeout(timeout);
-		break;
-	case 'V':
-		fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-		exit(-1);
-	case 'h':
-	default:
-		usage();
-	} ARGEND;
+	static char const str_opts[] = "C:P:s:t:dGarRVhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "Guid_addr", 0, 0, 'G'},
+		{ "all_ports", 0, 0, 'a'},
+		{ "reset_after_read", 0, 0, 'r'},
+		{ "Reset_only", 0, 0, 'R'},
+		{ "sm_portid", 1, 0, 's'},
+		{ "timeout", 1, 0, 't'},
+		{ "Version", 0, 0, 'V'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
 
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'C':
+			ca = optarg;
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 'a':
+			all++;
+			port = 0xff;
+			break;
+		case 'd':
+			ibdebug++;
+			madrpc_show_errors(1);
+			umad_debug(udebug);
+			udebug++;
+			break;
+		case 'G':
+			dest_type = IB_DEST_GUID;
+			break;
+		case 's':
+			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
+				IBERROR("can't resolve SM destination port %s", optarg);
+			sm_id = &sm_portid;
+			break;
+		case 'r':
+			reset++;
+			break;
+		case 'R':
+			reset_only++;
+			break;
+		case 't':
+			timeout = strtoul(optarg, 0, 0);
+			madrpc_set_timeout(timeout);
+			break;
+		case 'V':
+			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+			exit(-1);
+		case 'h':
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (argc > 1)
 		port = strtoul(argv[1], 0, 0);
 	if (argc > 2)
Index: diags/src/smpdump.c
===================================================================
--- diags/src/smpdump.c	(revision 5057)
+++ diags/src/smpdump.c	(working copy)
@@ -51,6 +51,7 @@
 #include <sys/ioctl.h>
 #include <unistd.h>
 #include <string.h>
+#include <getopt.h>
 #include <endian.h>
 #include <byteswap.h>
 #include <sys/poll.h>
@@ -75,6 +76,8 @@
 
 static int debug;
 
+static char *argv0 = "smpdump";
+
 typedef struct {
 	char path[64];
 	int hop_cnt;
@@ -238,31 +241,54 @@
 	uint8_t *desc;
 	int length;
 
-	ARGBEGIN {
-	case 's':
-		dump_char++;
-		break;
-	case 'd':
-		debug++;
-		if (debug > 1)
-			umad_debug(debug-1);
-		break;
-	case 'D':
-		mgmt_class = CLASS_SUBN_DIRECTED_ROUTE;
-		break;
-	case 'C':
-		dev_name = ARGF();
-		break;
-	case 'P':
-		dev_port = atoi(ARGF());
-		break;
-	case 't':
-		timeout_ms = strtoul(ARGF(), 0, 0);
-		break;
-	default:
-		usage();
-	} ARGEND;
+	fprintf(stderr, "Usage: %s [-s(ring) -C ca_name -P ca_port] <dlid|dr_path> <attr> [mod]\n", argv0);
+	static char const str_opts[] = "C:P:t:dsDhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "sring", 0, 0, 's'},
+		{ "Direct", 0, 0, 'D'},
+		{ "timeout", 1, 0, 't'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
 
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 's':
+			dump_char++;
+			break;
+		case 'd':
+			debug++;
+			if (debug > 1)
+				umad_debug(debug-1);
+			break;
+		case 'D':
+			mgmt_class = CLASS_SUBN_DIRECTED_ROUTE;
+			break;
+		case 'C':
+			dev_name = optarg;
+			break;
+		case 'P':
+			dev_port = atoi(optarg);
+			break;
+		case 't':
+			timeout_ms = strtoul(optarg, 0, 0);
+			break;
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (argc < 2)
 		usage();
 
Index: diags/src/ibsysstat.c
===================================================================
--- diags/src/ibsysstat.c	(revision 5057)
+++ diags/src/ibsysstat.c	(working copy)
@@ -42,6 +42,7 @@
 #include <stdarg.h>
 #include <time.h>
 #include <string.h>
+#include <getopt.h>
 
 #include <common.h>
 #include <umad.h>
@@ -75,6 +76,8 @@
 static char ipinfo[IB_VENDOR_RANGE2_DATA_SIZE] = "ipinfo";
 static char ibinfo[IB_VENDOR_RANGE2_DATA_SIZE] = "ibinfo";
 
+static char *argv0 = "ibsysstat";
+
 static void
 iberror(const char *fn, char *msg, ...)
 {
@@ -258,8 +261,8 @@
 	else
 		basename++;
 
-	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -D(irect) -G(uid) -s smlid -V(ersion) -C ca_name -P hca_port "
-			"-t timeout_ms] <op> <dest dr_path|lid|guid> [op params]\n",
+	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -D(irect) -G(uid) -s smlid -o oui -V(ersion) -C ca_name -P hca_port "
+			"-t(imeout) timeout_ms] <op> <dest dr_path|lid|guid> [op params]\n",
 			basename);
 	exit(-1);
 }
@@ -278,51 +281,78 @@
 	char *ca = 0;
 	int ca_port = 0;
 
-	ARGBEGIN {
-	case 'C':
-		ca = ARGF();
-		break;
-	case 'P':
-		ca_port = strtoul(ARGF(), 0, 0);
-		break;
-	case 'd':
-		ibdebug++;
-		madrpc_show_errors(1);
-		umad_debug(udebug);
-		udebug++;
-		break;
-	case 'e':
-		madrpc_show_errors(1);
-		break;
-	case 'G':
-		dest_type = IB_DEST_GUID;
-		break;
-	case 'o':
-		oui = strtoul(ARGF(), 0, 0);
-		break;
-	case 's':
-		if (ib_resolve_portid_str(&sm_portid, ARGF(), IB_DEST_LID, 0) < 0)
-			IBERROR("can't resolve SM destination port %s", ARGF());
-		sm_id = &sm_portid;
-		break;
-	case 'S':
-		server++;
-		break;
-	case 't':
-		timeout = strtoul(ARGF(), 0, 0);
-		madrpc_set_timeout(timeout);
-		break;
-	case 'v':
-		verbose++;
-		break;
-	case 'V':
-		fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-		exit(-1);
-	case 'h':
-	default:
-		usage();
-	} ARGEND;
+	static char const str_opts[] = "C:P:t:s:o:devDGVhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "err_show", 0, 0, 'e'},
+		{ "verbose", 0, 0, 'v'},
+		{ "Direct", 0, 0, 'D'},
+		{ "Guid", 0, 0, 'G'},
+		{ "timeout", 1, 0, 't'},
+		{ "s", 1, 0, 's'},
+		{ "o", 1, 0, 'o'},
+		{ "Version", 0, 0, 'V'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
 
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'C':
+			ca = optarg;
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 'd':
+			ibdebug++;
+			madrpc_show_errors(1);
+			umad_debug(udebug);
+			udebug++;
+			break;
+		case 'e':
+			madrpc_show_errors(1);
+			break;
+		case 'G':
+			dest_type = IB_DEST_GUID;
+			break;
+		case 'o':
+			oui = strtoul(optarg, 0, 0);
+			break;
+		case 's':
+			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
+				IBERROR("can't resolve SM destination port %s", optarg);
+			sm_id = &sm_portid;
+			break;
+		case 'S':
+			server++;
+			break;
+		case 't':
+			timeout = strtoul(optarg, 0, 0);
+			madrpc_set_timeout(timeout);
+			break;
+		case 'v':
+			verbose++;
+			break;
+		case 'V':
+			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+			exit(-1);
+		case 'h':
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (!argc && !server)
 		usage();
 
Index: diags/src/ibaddr.c
===================================================================
--- diags/src/ibaddr.c	(revision 5057)
+++ diags/src/ibaddr.c	(working copy)
@@ -40,6 +40,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <getopt.h>
 
 #include <common.h>
 #include <umad.h>
@@ -47,6 +48,8 @@
 
 #define IBERROR(fmt, args...)	iberror(__FUNCTION__, fmt, ## args)
 
+static char *argv0 = "ibaddr";
+
 static void
 iberror(const char *fn, char *msg, ...)
 {
@@ -114,7 +117,7 @@
 		basename++;
 
 	fprintf(stderr, "Usage: %s [-d(ebug) -D(irect_path_addr) -G(uid_addr) -l(id_show) -g(id_show) -C ca_name -P hca_port "
-			"-t timeout_ms -V(ersion) -h(elp)] [<lid|dr_path|guid>]\n",
+			"-t(imeout) timeout_ms -V(ersion) -h(elp)] [<lid|dr_path|guid>]\n",
 			basename);
 	fprintf(stderr, "\tExamples:\n");
 	fprintf(stderr, "\t\t%s\t\t\t# local port's address\n", basename);
@@ -140,48 +143,75 @@
 	char *ca = 0;
 	int ca_port = 0;
 
-	ARGBEGIN {
-	case 'C':
-		ca = ARGF();
-		break;
-	case 'P':
-		ca_port = strtoul(ARGF(), 0, 0);
-		break;
-	case 'd':
-		ibdebug++;
-		break;
-	case 'D':
-		dest_type = IB_DEST_DRPATH;
-		break;
-	case 'g':
-		show_gid++;
-		break;
-	case 'G':
-		dest_type = IB_DEST_GUID;
-		break;
-	case 'l':
-		show_lid++;
-		break;
-	case 'L':
-		show_lid = -100;
-		break;
-	case 's':
-		if (ib_resolve_portid_str(&sm_portid, ARGF(), IB_DEST_LID, 0) < 0)
-			IBERROR("can't resolve SM destination port %s", ARGF());
-		sm_id = &sm_portid;
-		break;
-	case 't':
-		timeout = strtoul(ARGF(), 0, 0);
-		madrpc_set_timeout(timeout);
-		break;
-	case 'V':
-		fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-		exit(-1);
-	case 'h':
-	default:
-		usage();
-	} ARGEND;
+	static char const str_opts[] = "C:P:t:s:dDGglLVhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "Direct_path_addr", 0, 0, 'D'},
+		{ "Guid_addr", 0, 0, 'G'},
+		{ "gid_show", 0, 0, 'g'},
+		{ "lid_show", 0, 0, 'l'},
+		{ "Lid_show", 0, 0, 'L'},
+		{ "timeout", 1, 0, 't'},
+		{ "sm_port", 1, 0, 's'},
+		{ "Version", 0, 0, 'V'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
 
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'C':
+			ca = optarg;
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 'd':
+			ibdebug++;
+			break;
+		case 'D':
+			dest_type = IB_DEST_DRPATH;
+			break;
+		case 'g':
+			show_gid++;
+			break;
+		case 'G':
+			dest_type = IB_DEST_GUID;
+			break;
+		case 'l':
+			show_lid++;
+			break;
+		case 'L':
+			show_lid = -100;
+			break;
+		case 's':
+			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
+				IBERROR("can't resolve SM destination port %s", optarg);
+			sm_id = &sm_portid;
+			break;
+		case 't':
+			timeout = strtoul(optarg, 0, 0);
+			madrpc_set_timeout(timeout);
+			break;
+		case 'V':
+			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+			exit(-1);
+		case 'h':
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (argc > 1)
 		port = strtoul(argv[1], 0, 0);
 
Index: diags/src/smpquery.c
===================================================================
--- diags/src/smpquery.c	(revision 5057)
+++ diags/src/smpquery.c	(working copy)
@@ -42,6 +42,7 @@
 #include <stdarg.h>
 #include <time.h>
 #include <string.h>
+#include <getopt.h>
 #include <netinet/in.h>
 
 #include <common.h>
@@ -73,6 +74,8 @@
 	{0}
 };
 
+static char *argv0 = "smpquery";
+
 static void
 iberror(const char *fn, char *msg, ...)
 {
@@ -234,7 +237,7 @@
 		basename++;
 
 	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -D(irect) -G(uid) -s smlid -V(ersion) -C ca_name -P hca_port "
-			"-t timeout_ms] <op> <dest dr_path|lid|guid> [op params]\n",
+			"-t(imeout) timeout_ms] <op> <dest dr_path|lid|guid> [op params]\n",
 			basename);
 	fprintf(stderr, "\tsupported ops:\n");
 	fprintf(stderr, "\t\tnodeinfo <addr>\n");
@@ -262,48 +265,74 @@
 	char *err;
 	op_fn_t *fn;
 
-	ARGBEGIN {
-	case 'd':
-		ibdebug++;
-		madrpc_show_errors(1);
-		umad_debug(udebug);
-		udebug++;
-		break;
-	case 'e':
-		madrpc_show_errors(1);
-		break;
-	case 'D':
-		dest_type = IB_DEST_DRPATH;
-		break;
-	case 'G':
-		dest_type = IB_DEST_GUID;
-		break;
-	case 'C':
-		ca = ARGF();
-		break;
-	case 'P':
-		ca_port = strtoul(ARGF(), 0, 0);
-		break;
-	case 's':
-		if (ib_resolve_portid_str(&sm_portid, ARGF(), IB_DEST_LID, 0) < 0)
-			IBERROR("can't resolve SM destination port %s", ARGF());
-		sm_id = &sm_portid;
-		break;
-	case 't':
-		timeout = strtoul(ARGF(), 0, 0);
-		madrpc_set_timeout(timeout);
-		break;
-	case 'v':
-		verbose++;
-		break;
-	case 'V':
-		fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-		exit(-1);
-	case 'h':
-	default:
-		usage();
-	} ARGEND;
+	static char const str_opts[] = "C:P:t:s:devDGVhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "err_show", 0, 0, 'e'},
+		{ "verbose", 0, 0, 'v'},
+		{ "Direct", 0, 0, 'D'},
+		{ "Guid", 0, 0, 'G'},
+		{ "smlid", 1, 0, 's'},
+		{ "timeout", 1, 0, 't'},
+		{ "Version", 0, 0, 'V'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
 
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'd':
+			ibdebug++;
+			madrpc_show_errors(1);
+			umad_debug(udebug);
+			udebug++;
+			break;
+		case 'e':
+			madrpc_show_errors(1);
+			break;
+		case 'D':
+			dest_type = IB_DEST_DRPATH;
+			break;
+		case 'G':
+			dest_type = IB_DEST_GUID;
+			break;
+		case 'C':
+			ca = optarg;
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 's':
+			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
+				IBERROR("can't resolve SM destination port %s", optarg);
+			sm_id = &sm_portid;
+			break;
+		case 't':
+			timeout = strtoul(optarg, 0, 0);
+			madrpc_set_timeout(timeout);
+			break;
+		case 'v':
+			verbose++;
+			break;
+		case 'V':
+			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+			exit(-1);
+		case 'h':
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (argc < 2)
 		usage();
 
Index: diags/src/ibstat.c
===================================================================
--- diags/src/ibstat.c	(revision 5057)
+++ diags/src/ibstat.c	(working copy)
@@ -51,6 +51,7 @@
 #include <sys/ioctl.h>
 #include <unistd.h>
 #include <string.h>
+#include <getopt.h>
 #include <endian.h>
 #include <byteswap.h>
 #include <sys/poll.h>
@@ -64,6 +65,8 @@
 
 static int debug;
 
+static char *argv0 = "ibstat";
+
 static char *node_type_str[] = {
 	"???",
 	"CA",
@@ -202,23 +205,43 @@
 	int list_only = 0, short_format = 0, list_ports = 0;
 	int n, i;
 
-	ARGBEGIN {
-	case 'd':
-		debug++;
-		break;
-	case 'l':
-		list_only++;
-		break;
-	case 's':
-		short_format++;
-		break;
-	case 'p':
-		list_ports++;
-		break;
-	default:
-		usage();
-	} ARGEND;
+	static char const str_opts[] = "dlsphu";
+	static const struct option long_opts[] = {
+		{ "debug", 0, 0, 'd'},
+		{ "list_of_cas", 0, 0, 'l'},
+		{ "short", 0, 0, 's'},
+		{ "port_list", 0, 0, 'p'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
 
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'd':
+			debug++;
+			break;
+		case 'l':
+			list_only++;
+			break;
+		case 's':
+			short_format++;
+			break;
+		case 'p':
+			list_ports++;
+			break;
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (argc > 1)
 		dev_port = strtol(argv[1], 0, 0);
 
Index: diags/src/ibping.c
===================================================================
--- diags/src/ibping.c	(revision 5057)
+++ diags/src/ibping.c	(working copy)
@@ -43,6 +43,7 @@
 #include <time.h>
 #include <string.h>
 #include <signal.h>
+#include <getopt.h>
 
 #include <common.h>
 #include <umad.h>
@@ -57,6 +58,8 @@
 static char host_and_domain[IB_VENDOR_RANGE2_DATA_SIZE];
 static char last_host[IB_VENDOR_RANGE2_DATA_SIZE];
 
+static char *argv0 = "ibping";
+
 static void
 iberror(const char *fn, char *msg, ...)
 {
@@ -180,7 +183,7 @@
 		basename++;
 
 	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -G(uid) -s smlid -V(ersion) -C ca_name -P hca_port"
-			"-t timeout_ms -c ping_count -f(lood) -o oui -S(erver)] <dest lid|guid>\n",
+			"-t(imeout) timeout_ms -c ping_count -f(lood) -o oui -S(erver)] <dest lid|guid>\n",
 			basename);
 	exit(-1);
 }
@@ -237,57 +240,86 @@
 	char *ca = 0;
 	int ca_port = 0;
 
-	ARGBEGIN {
-	case 'C':
-		ca = ARGF();
-		break;
-	case 'P':
-		ca_port = strtoul(ARGF(), 0, 0);
-		break;
-	case 'c':
-		count = strtoul(ARGF(), 0, 0);
-		break;
-	case 'd':
-		ibdebug++;
-		madrpc_show_errors(1);
-		umad_debug(udebug);
-		udebug++;
-		break;
-	case 'e':
-		madrpc_show_errors(1);
-		break;
-	case 'f':
-		flood++;
-		break;
-	case 'G':
-		dest_type = IB_DEST_GUID;
-		break;
-	case 'o':
-		oui = strtoul(ARGF(), 0, 0);
-		break;
-	case 's':
-		if (ib_resolve_portid_str(&sm_portid, ARGF(), IB_DEST_LID, 0) < 0)
-			IBERROR("can't resolve SM destination port %s", ARGF());
-		sm_id = &sm_portid;
-		break;
-	case 'S':
-		server++;
-		break;
-	case 't':
-		timeout = strtoul(ARGF(), 0, 0);
-		madrpc_set_timeout(timeout);
-		break;
-	case 'v':
-		verbose++;
-		break;
-	case 'V':
-		fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-		exit(-1);
-	case 'h':
-	default:
-		usage();
-	} ARGEND;
+	static char const str_opts[] = "C:P:t:s:c:o:devGfSVhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "err_show", 0, 0, 'e'},
+		{ "verbose", 0, 0, 'v'},
+		{ "Guid", 0, 0, 'G'},
+		{ "s", 1, 0, 's'},
+		{ "timeout", 1, 0, 't'},
+		{ "c", 1, 0, 'c'},
+		{ "flood", 0, 0, 'f'},
+		{ "o", 1, 0, 'o'},
+		{ "Server", 0, 0, 'S'},
+		{ "Version", 0, 0, 'V'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
 
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'C':
+			ca = optarg;
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 'c':
+			count = strtoul(optarg, 0, 0);
+			break;
+		case 'd':
+			ibdebug++;
+			madrpc_show_errors(1);
+			umad_debug(udebug);
+			udebug++;
+			break;
+		case 'e':
+			madrpc_show_errors(1);
+			break;
+		case 'f':
+			flood++;
+			break;
+		case 'G':
+			dest_type = IB_DEST_GUID;
+			break;
+		case 'o':
+			oui = strtoul(optarg, 0, 0);
+			break;
+		case 's':
+			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
+				IBERROR("can't resolve SM destination port %s", optarg);
+			sm_id = &sm_portid;
+			break;
+		case 'S':
+			server++;
+			break;
+		case 't':
+			timeout = strtoul(optarg, 0, 0);
+			madrpc_set_timeout(timeout);
+			break;
+		case 'v':
+			verbose++;
+			break;
+		case 'V':
+			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+			exit(-1);
+		case 'h':
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (!argc && !server)
 		usage();
 	
Index: diags/src/ibroute.c
===================================================================
--- diags/src/ibroute.c	(revision 5057)
+++ diags/src/ibroute.c	(working copy)
@@ -42,6 +42,7 @@
 #include <stdarg.h>
 #include <time.h>
 #include <string.h>
+#include <getopt.h>
 #include <netinet/in.h>
 
 #include <common.h>
@@ -57,6 +58,8 @@
 static int verbose;
 static int dump_all;
 
+static char *argv0 = "ibroute";
+
 static void
 iberror(const char *fn, char *msg, ...)
 {
@@ -377,7 +380,7 @@
 		basename++;
 
 	fprintf(stderr, "Usage: %s [-d(ebug)] -a(ll) -n(o_dests) -v(erbose) -D(irect) -G(uid) -M(ulticast) -s smlid -V(ersion) -C ca_name -P hca_port "
-			"-t timeout_ms] [<dest dr_path|lid|guid> [<startlid> [<endlid>]]]\n",
+			"-t(imeout) timeout_ms] [<dest dr_path|lid|guid> [<startlid> [<endlid>]]]\n",
 			basename);
 	fprintf(stderr, "\n\tUnicast examples:\n");
 	fprintf(stderr, "\t\t%s 4\t# dump all lids with valid out ports of switch with lid 4\n", basename);
@@ -408,52 +411,80 @@
 	char *ca = 0;
 	int ca_port = 0;
 
-	ARGBEGIN {
-	case 'C':
-		ca = ARGF();
-		break;
-	case 'P':
-		ca_port = strtoul(ARGF(), 0, 0);
-		break;
-	case 'a':
-		dump_all++;
-		break;
-	case 'd':
-		ibdebug++;
-		break;
-	case 'D':
-		dest_type = IB_DEST_DRPATH;
-		break;
-	case 'G':
-		dest_type = IB_DEST_GUID;
-		break;
-	case 'M':
-		multicast++;
-		break;
-	case 'n':
-		brief++;
-		break;
-	case 's':
-		if (ib_resolve_portid_str(&sm_portid, ARGF(), IB_DEST_LID, 0) < 0)
-			IBERROR("can't resolve SM destination port %s", ARGF());
-		sm_id = &sm_portid;
-		break;
-	case 't':
-		timeout = strtoul(ARGF(), 0, 0);
-		madrpc_set_timeout(timeout);
-		break;
-	case 'v':
-		madrpc_show_errors(1);
-		verbose++;
-		break;
-	case 'V':
-		fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-		exit(-1);
-	case 'h':
-	default:
-		usage();
-	} ARGEND;
+	static char const str_opts[] = "C:P:t:s:danvDGMVhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "all", 0, 0, 'a'},
+		{ "no_dests", 0, 0, 'n'},
+		{ "verbose", 0, 0, 'v'},
+		{ "Direct", 0, 0, 'D'},
+		{ "Guid", 0, 0, 'G'},
+		{ "Multicast", 0, 0, 'M'},
+		{ "timeout", 1, 0, 't'},
+		{ "s", 1, 0, 's'},
+		{ "Version", 0, 0, 'V'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
 
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'C':
+			ca = optarg;
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 'a':
+			dump_all++;
+			break;
+		case 'd':
+			ibdebug++;
+			break;
+		case 'D':
+			dest_type = IB_DEST_DRPATH;
+			break;
+		case 'G':
+			dest_type = IB_DEST_GUID;
+			break;
+		case 'M':
+			multicast++;
+			break;
+		case 'n':
+			brief++;
+			break;
+		case 's':
+			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
+				IBERROR("can't resolve SM destination port %s", optarg);
+			sm_id = &sm_portid;
+			break;
+		case 't':
+			timeout = strtoul(optarg, 0, 0);
+			madrpc_set_timeout(timeout);
+			break;
+		case 'v':
+			madrpc_show_errors(1);
+			verbose++;
+			break;
+		case 'V':
+			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+			exit(-1);
+		case 'h':
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (!argc)
 		usage();
 
Index: diags/src/sminfo.c
===================================================================
--- diags/src/sminfo.c	(revision 5057)
+++ diags/src/sminfo.c	(working copy)
@@ -40,6 +40,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <getopt.h>
 
 #include <common.h>
 #include <umad.h>
@@ -47,10 +48,10 @@
 
 static uint8_t sminfo[1024];
 
+static char *argv0 = "sminfo";
+
 #define IBERROR(fmt, args...)	iberror(__FUNCTION__, fmt, ## args)
 
-#define SAFE_ARGF() (*(argv+1) ? ARGF() : ( usage(), NULL ) )
-
 static void
 iberror(const char *fn, char *msg, ...)
 {
@@ -74,8 +75,8 @@
 void
 usage(void)
 {
-	fprintf(stderr, "Usage: %s [-d(ebug) -s state -p prio -a activity -D(irect) -G(uid) -V(ersion) -C ca_name -P hca_port "
-			"-t timeout_ms] <sm lid|sm path> [modifier]\n",
+	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -s state -p prio -a activity -D(irect) -G(uid) -V(ersion) -C ca_name -P hca_port "
+			"-t(imeout) timeout_ms] <sm lid|sm path> [modifier]\n",
 			argv0);
 	fprintf(stderr, "%s %s\n", argv0, get_build_version() );
 	exit(-1);
@@ -116,48 +117,75 @@
 	char *ca = 0;
 	int ca_port = 0;
 
-	ARGBEGIN {
-	case 'C':
-		ca = SAFE_ARGF();
-		break;
-	case 'P':
-		ca_port = strtoul(SAFE_ARGF(), 0, 0);
-		break;
-	case 'd':
-		ibdebug++;
-		madrpc_show_errors(1);
-		umad_debug(udebug);
-		udebug++;
-		break;
-	case 'e':
-		madrpc_show_errors(1);
-		break;
-	case 'D':
-		dest_type = IB_DEST_DRPATH;
-		break;
-	case 'G':
-		dest_type = IB_DEST_GUID;
-		break;
-	case 't':
-		timeout = strtoul(SAFE_ARGF(), 0, 0);
-		madrpc_set_timeout(timeout);
-		break;
-	case 'a':
-		act = strtoul(SAFE_ARGF(), 0, 0);
-		break;
-	case 's':
-		state = strtoul(SAFE_ARGF(), 0, 0);
-		break;
-	case 'p':
-		prio = strtoul(SAFE_ARGF(), 0, 0);
-		break;
-	case 'V':
-		fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-		exit(-1);
-	default:
-		usage();
-	} ARGEND;
+	static char const str_opts[] = "C:P:t:s:p:a:deDGVhu";
+	static const struct option long_opts[] = {
+		{ "C", 1, 0, 'C'},
+		{ "P", 1, 0, 'P'},
+		{ "debug", 0, 0, 'd'},
+		{ "err_show", 0, 0, 'e'},
+		{ "s", 1, 0, 's'},
+		{ "p", 1, 0, 'p'},
+		{ "a", 1, 0, 'a'},
+		{ "Direct", 0, 0, 'D'},
+		{ "Guid", 0, 0, 'G'},
+		{ "Version", 0, 0, 'V'},
+		{ "timeout", 1, 0, 't'},
+		{ "help", 0, 0, 'h'},
+		{ "usage", 0, 0, 'u'},
+		{ }
+	};
 
+	argv0 = argv[0];
+
+	while (1) {
+		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
+		if ( ch == -1 )
+			break;
+		switch(ch) {
+		case 'C':
+			ca = optarg;
+			break;
+		case 'P':
+			ca_port = strtoul(optarg, 0, 0);
+			break;
+		case 'd':
+			ibdebug++;
+			madrpc_show_errors(1);
+			umad_debug(udebug);
+			udebug++;
+			break;
+		case 'e':
+			madrpc_show_errors(1);
+			break;
+		case 'D':
+			dest_type = IB_DEST_DRPATH;
+			break;
+		case 'G':
+			dest_type = IB_DEST_GUID;
+			break;
+		case 't':
+			timeout = strtoul(optarg, 0, 0);
+			madrpc_set_timeout(timeout);
+			break;
+		case 'a':
+			act = strtoul(optarg, 0, 0);
+			break;
+		case 's':
+			state = strtoul(optarg, 0, 0);
+			break;
+		case 'p':
+			prio = strtoul(optarg, 0, 0);
+			break;
+		case 'V':
+			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+			exit(-1);
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
 	if (argc > 1)
 		mod = atoi(argv[1]);
 
Index: libibcommon/include/infiniband/common.h
===================================================================
--- libibcommon/include/infiniband/common.h	(revision 5057)
+++ libibcommon/include/infiniband/common.h	(working copy)
@@ -83,25 +83,6 @@
  * COMMON MACHINE INDEPENDENT
  */
 
-/* argc, argv parsing */
-
-/** Begin arguments parsing block */
-#define ARGBEGIN	{char *_ss;\
-				for (argv0 = *argv++; *argv && *argv[0] == '-'; argv++, argc--)\
-				for (_ss = *argv + 1; *_ss; _ss++) switch (*_ss)
-
-/** End arguments parsing block */
-#define ARGEND		argc--;};
-
-/** Return current option argument */
-#define ARGF()		(argc--, *++argv)
-
-/** Return current option character */
-#define ARGC()		(*_ss)
-
-/** global application names (was argv[0]) */
-extern char *argv0;
-
 /* Misc. macros: */
 /** align value \a l to \a size (ceil) */
 #define ALIGN(l, size) (((l) + ((size) - 1)) / (size) * (size))
Index: libibcommon/src/vars.c
===================================================================
--- libibcommon/src/vars.c	(revision 5057)
+++ libibcommon/src/vars.c	(working copy)
@@ -1,35 +0,0 @@
-/*
- * Copyright (c) 2004,2005 Voltaire Inc.  All rights reserved.
- *
- * This software is available to you under a choice of one of two
- * licenses.  You may choose to be licensed under the terms of the GNU
- * General Public License (GPL) Version 2, available from the file
- * COPYING in the main directory of this source tree, or the
- * OpenIB.org BSD license below:
- *
- *     Redistribution and use in source and binary forms, with or
- *     without modification, are permitted provided that the following
- *     conditions are met:
- *
- *      - Redistributions of source code must retain the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer.
- *
- *      - Redistributions in binary form must reproduce the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer in the documentation and/or other materials
- *        provided with the distribution.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- *
- * $Id$
- */
-
-char *argv0;
Index: libibcommon/Makefile.am
===================================================================
--- libibcommon/Makefile.am	(revision 5057)
+++ libibcommon/Makefile.am	(working copy)
@@ -13,7 +13,7 @@
     libibcommon_version_script =
 endif
 
-libibcommon_la_SOURCES = src/stack.c src/sysfs.c src/util.c src/vars.c src/time.c src/hash.c
+libibcommon_la_SOURCES = src/stack.c src/sysfs.c src/util.c src/time.c src/hash.c
 libibcommon_la_LDFLAGS = -version-info 1 -export-dynamic \
     $(libibcommon_version_script)
 libibcommon_la_DEPENDENCIES = $(srcdir)/src/libibcommon.map



More information about the general mailing list