public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH][realtime] add support for long options
@ 2009-08-24 22:10 Darren Hart
  2009-08-25  9:58 ` Subrata Modak
  0 siblings, 1 reply; 2+ messages in thread
From: Darren Hart @ 2009-08-24 22:10 UTC (permalink / raw)
  To: ltp-list; +Cc: Vernon Mauery

add support for long options

Incorporating existing tests (like pthread_cond_many) may require support
for long options if the existing arguments are to remain supported.  This
patch adds support for long options, while keeping the default of only
short options in place.  long opts MUST have an equivalent short opt so
as to not require changing (and complicating) the parse_arg signature.
This patch tests for this in the rt_init routine.

Signed-off-by: Darren Hart <dvhltc@us.ibm.com>
Acked-by: Vernon Mauery <vernux@us.ibm.com>

---
 include/librttest.h |   12 ++++++++++--
 lib/librttest.c     |   49 +++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 55 insertions(+), 6 deletions(-)

Index: realtime/include/librttest.h
===================================================================
--- realtime.orig/include/librttest.h
+++ realtime/include/librttest.h
@@ -235,10 +235,12 @@ static volatile int _debug_count = 0;
 /* rt_help: print help for standard args */
 void rt_help();
 
-/* rt_init: initialize librttest
+/* rt_init_long: initialize librttest
  * options: pass in an getopt style string of options -- e.g. "ab:cd::e:"
  *          if this or parse_arg is null, no option parsing will be done
  *          on behalf of the calling program (only internal args will be parsed)
+ * longopts: a pointer to the first element of an array of struct option, the
+ *           last entry must be set to all zeros.
  * parse_arg: a function that will get called when one of the above
  *            options is encountered on the command line.  It will be passed
  *            the option -- e.g. 'b' -- and the value.  Something like:
@@ -267,7 +269,13 @@ void rt_help();
  * argc: passed from main
  * argv: passed from main
  */
-int rt_init(const char *options, int (*parse_arg)(int option, char *value), int argc, char *argv[]);
+int rt_init_long(const char *options, const struct option *longopts,
+		 int (*parse_arg)(int option, char *value),
+		 int argc, char *argv[]);
+
+/* rt_init: same as rt_init_long with no long options */
+int rt_init(const char *options, int (*parse_arg)(int option, char *value),
+	    int argc, char *argv[]);
 
 int create_thread(void*(*func)(void*), void *arg, int prio, int policy);
 
Index: realtime/lib/librttest.c
===================================================================
--- realtime.orig/lib/librttest.c
+++ realtime/lib/librttest.c
@@ -99,9 +99,13 @@ void calibrate_busyloop(void)
 	iters_per_us = (CALIBRATE_LOOPS * NS_PER_US) / (end-start);
 }
 
-int rt_init(const char *options, int (*parse_arg)(int option, char *value), int argc, char *argv[])
+int rt_init_long(const char *options, const struct option *longopts,
+		 int (*parse_arg)(int option, char *value), int argc,
+		 char *argv[])
 {
+	const struct option *cur_opt;
 	int use_buffer = 1;
+	char *longopt_vals;
 	size_t i;
 	int c;
 	opterr = 0;
@@ -126,8 +130,33 @@ int rt_init(const char *options, int (*p
 			exit(1);
 		}
 	}
+
+	/* Ensure each long options has a known unique short option in val. */
+	longopt_vals = "";
+	cur_opt = longopts;
+	while (cur_opt && cur_opt->name) {
+		if (cur_opt->flag) {
+			fprintf(stderr, "Programmer error -- argument --%s flag"
+				" is non-null\n", cur_opt->name);
+			exit(1);
+		}
+		if (!strchr(all_options, cur_opt->val)) {
+			fprintf(stderr, "Progreammer error -- argument --%s "
+				"shortopt -%c wasn't listed in options (%s)\n",
+				cur_opt->name, cur_opt->val, all_options);
+			exit(1);
+		}
+		if (strchr(longopt_vals, cur_opt->val)) {
+			fprintf(stderr, "Programmer error -- argument --%s "
+				"shortopt -%c is used more than once\n",
+				cur_opt->name, cur_opt->val);
+			exit(1);
+		}
+		asprintf(&longopt_vals, "%s%c", longopt_vals, cur_opt->val);
+		cur_opt++;
+	}
 	
-	while ((c = getopt(argc, argv, all_options)) != -1) {
+	while ((c = getopt_long(argc, argv, all_options, longopts, NULL)) != -1) {
 		switch (c) {
 		case 'c':
 			pass_criteria = atof(optarg);
@@ -148,11 +177,17 @@ int rt_init(const char *options, int (*p
 			save_stats = 1;
 			break;
 		case ':':
-			fprintf(stderr, "option -%c: missing arg\n", optopt);
+			if (optopt == '-')
+				fprintf(stderr, "long option missing arg\n");
+			else
+				fprintf(stderr, "option -%c: missing arg\n", optopt);
 			parse_arg('h', optarg); /* Just to display usage */
 			exit (1); /* Just in case. (should normally be done by usage()) */
 		case '?':
-			fprintf(stderr, "option -%c not recognized\n", optopt);
+			if (optopt == '-')
+				fprintf(stderr, "unrecognized long option\n");
+			else
+				fprintf(stderr, "option -%c not recognized\n", optopt);
 			parse_arg('h', optarg); /* Just to display usage */
 			exit (1); /* Just in case. (should normally be done by usage()) */
 		default:
@@ -185,6 +220,12 @@ int rt_init(const char *options, int (*p
 	return 0;
 }
 
+int rt_init(const char *options, int (*parse_arg)(int option, char *value),
+	    int argc, char *argv[])
+{
+	return rt_init_long(options, NULL, parse_arg, argc, argv);
+}
+
 void buffer_init(void)
 {
 	_print_buffer = (char *)malloc(PRINT_BUFFER_SIZE);
-- 
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-08-25  9:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-24 22:10 [LTP] [PATCH][realtime] add support for long options Darren Hart
2009-08-25  9:58 ` Subrata Modak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox