--- ../../pis/bluez-2.0-pre6/tools/hcitool.c	Wed Feb 20 19:07:17 2002
+++ hcitool.c	Thu Feb 28 23:21:45 2002
@@ -43,11 +43,17 @@
 #include <hci.h>
 #include <hci_lib.h>
 
+#include <getopt.h>
+#include <ctype.h>
+
 extern int optind,opterr,optopt;
 extern char *optarg;
 
 static int ctl;
 
+/* Global option vars.. Should probably be named something more hinting in that regard... */
+int num_rsp = 0, flags=0, length=10;
+
 static int for_each_dev(int flag, int(*func)(int d, long arg), long arg)
 {
 	struct hci_dev_list_req *dl;
@@ -173,22 +179,13 @@
 static void cmd_inq(int dev_id, char **opt, int nopt)
 {
 	inquiry_info *info;
-	int i, num_rsp = 0, length, flags;
+	int i;
 	bdaddr_t bdaddr;
 	
 	if (dev_id < 0)
 		dev_id = get_route(NULL);
 	
-	if (nopt >= 1)
-		length = atoi(opt[0]);
-	else
-		length = 10; /* 10 seconds */
-
-	flags = 0;
-	if (nopt >= 2)
-		flags |= !strncasecmp("f", opt[1], 1) ? IREQ_CACHE_FLUSH : 0;
-		
-	printf("Inquiring ...\n");
+	printf("Inquiring on device %i...\n", dev_id);
 	info = hci_inquiry(dev_id, length, &num_rsp, NULL, flags);
 
 	if (!info) {
@@ -304,13 +301,21 @@
 	char *doc;
 } command[] = {
 	{ "dev",  cmd_dev,  0,          "Display local devices"      },
-	{ "inq",  cmd_inq,  "[lenght] [flush]", "Inquire remote devices"     },
+	{ "inq",  cmd_inq,  "--length, -l inq timeout   Inquire remote devices\n             --resps, -r stop after n responses\n             --flush, -f flush known device cache", " "},
 	{ "con",  cmd_con,  0,          "Display active connections" },
 	{ "cc",   cmd_cc,   "<bdaddr> [pkt type] [role]", "Create connection to remote device" },
 	{ "dc",	  cmd_dc,   "<bdaddr>", "Disconnect from remote device" },
 	{ NULL, NULL, 0}
 };
 
+struct option option_desc[] = {
+  {"i", 1, 0, 'i'},
+  {"length", 1, 0, 'l'},
+  {"resps", 1, 0, 'r'},
+  {"flush", 0, 0, 'f'},
+  {"help", 0, 0, 'h'}
+};
+
 static void usage(void)
 {
 	int i;
@@ -327,21 +332,55 @@
 
 int main(int argc, char *argv[], char *env[])
 {
-	int opt, i, dev_id = -1;
-	char *dev;
+	int opt, i=0, dev_id = -1;
+	char *dev, *endptr;
 
-	while ((opt=getopt(argc, argv, "i:h")) != EOF) {
-		switch(opt) {
-		case 'i':
-			dev    = strdup(optarg);
-			dev_id = atoi(dev + 3);
-			break;
-
-		case 'h':
-		default:
-			usage();
-			exit(0);
-		}
+	while ((opt=getopt_long(argc, argv, "i:r:l:fh",option_desc, &i))>0) {
+	  switch(opt) {
+	  case 'i':
+	    if (!(strncmp("hci", optarg, 3)) && strlen(optarg)>=4
+		&& isdigit(optarg[3])) {
+	      dev    = strdup(optarg);
+	      dev_id = (int) strtol(dev + 3, &endptr,10);
+	      if (endptr==NULL || *endptr==' ' || *endptr == '\0') {
+		break;
+	      }
+	    }
+	    printf("Wrong argument for option -i\n");
+	    usage();
+	    exit(1);
+	  case 'l':
+	    length = (int) strtol(optarg, &endptr,10);
+	    if (endptr!=NULL && *endptr!=' ' && *endptr!='\0') {
+	      printf("Wrong argument for option -l\n");
+	      usage();
+	      exit(1);
+	    }
+	    break;
+	  case 'r':
+	    num_rsp = (int) strtol(optarg, &endptr, 10);
+	    if (endptr!=NULL && *endptr!=' ' && *endptr!='\0') {
+	      printf("Wrong argument for option -r\n");
+	      usage();
+	      exit(1);
+	    }
+	    break;
+	  case 'f':
+	    flags |= IREQ_CACHE_FLUSH;
+	    break;
+	  case ':':
+	    printf("Argument missing\n");
+	    usage();
+	    exit(0);
+	  case '?':
+	    printf("Unknown syntax\n");
+	    usage();
+	    exit(0);
+	  case 'h':
+	  default:
+	    usage();
+	    exit(0);
+	  }
 	}
 
 	if (argc - optind < 1) {

