Browse Source

Added basic http-specific option parsing

tags/v0.9.3
Michael Brown 19 years ago
parent
commit
911486fe41
1 changed files with 84 additions and 5 deletions
  1. 84
    5
      src/util/prototester.c

+ 84
- 5
src/util/prototester.c View File

@@ -323,21 +323,100 @@ static int http_get ( struct http_request *http ) {
323 323
 	return tcp_connect ( &http->sin );
324 324
 }
325 325
 
326
+
327
+
328
+
329
+struct http_options {
330
+	struct sockaddr_in server;
331
+	char *filename;
332
+};
333
+
334
+static void http_usage ( char **argv ) {
335
+	fprintf ( stderr,
336
+		  "Usage: %s [global options] http [http-specific options]\n"
337
+		  "\n"
338
+		  "http-specific options:\n"
339
+		  "  -h|--host              Host IP address\n"
340
+		  "  -f|--file              Filename\n",
341
+		  argv[0] );
342
+}
343
+
344
+static int http_parse_options ( int argc, char **argv,
345
+				struct http_options *options ) {
346
+	static struct option long_options[] = {
347
+		{ "host", 1, NULL, 'h' },
348
+		{ "file", 1, NULL, 'f' },
349
+		{ },
350
+	};
351
+	int c;
352
+
353
+	/* Set default options */
354
+	memset ( options, 0, sizeof ( *options ) );
355
+	inet_aton ( "192.168.0.1", &options->server.sin_addr );
356
+	options->server.sin_port = htons ( 80 );
357
+	options->filename = "index.html";
358
+
359
+	/* Parse command-line options */
360
+	while ( 1 ) {
361
+		int option_index = 0;
362
+		
363
+		c = getopt_long ( argc, argv, "h:f:", long_options,
364
+				  &option_index );
365
+		if ( c < 0 )
366
+			break;
367
+
368
+		switch ( c ) {
369
+		case 'h':
370
+			if ( inet_aton ( optarg,
371
+					 &options->server.sin_addr ) == 0 ) {
372
+				fprintf ( stderr, "Invalid IP address %s\n",
373
+					  optarg );
374
+				return -1;
375
+			}
376
+			break;
377
+		case 'f':
378
+			options->filename = optarg;
379
+			break;
380
+		case '?':
381
+			/* Unrecognised option */
382
+			return -1;
383
+		default:
384
+			fprintf ( stderr, "Unrecognised option '-%c'\n", c );
385
+			return -1;
386
+		}
387
+	}
388
+
389
+	/* Check there are no remaining arguments */
390
+	if ( optind != argc ) {
391
+		http_usage ( argv );
392
+		return -1;
393
+	}
394
+	
395
+	return optind;
396
+}
397
+
326 398
 static void test_http_callback ( struct http_request *http ) {
327 399
 	
328 400
 }
329 401
 
330 402
 static int test_http ( int argc, char **argv ) {
403
+	struct http_options options;
331 404
 	struct http_request http;
332 405
 
406
+	/* Parse http-specific options */
407
+	if ( http_parse_options ( argc, argv, &options ) < 0 )
408
+		return -1;
409
+
410
+	/* Construct http request */
333 411
 	memset ( &http, 0, sizeof ( http ) );
334
-	http.filename = "/";
412
+	http.filename = options.filename;
413
+	http.sin = options.server;
335 414
 	http.callback = test_http_callback;
336
-	inet_aton ( "192.168.0.1", &http.sin.sin_addr );
337
-	http.sin.sin_port = htons ( 80 );
415
+	fprintf ( stderr, "http fetching http://%s/%s\n",
416
+		  inet_ntoa ( http.sin.sin_addr ), http.filename );
338 417
 
418
+	/* Issue http request and run to completion */
339 419
 	http_get ( &http );
340
-	
341 420
 	while ( ! http.complete ) {
342 421
 		run_tcpip ();
343 422
 	}
@@ -475,7 +554,7 @@ int main ( int argc, char **argv ) {
475 554
 	test = get_test_from_name ( argv[optind] );
476 555
 	if ( ! test ) {
477 556
 		fprintf ( stderr, "Unrecognised test \"%s\"\n", argv[optind] );
478
-		return -1;
557
+		exit ( 1 );
479 558
 	}
480 559
 	optind++;
481 560
 

Loading…
Cancel
Save