Module: xenomai-3
Branch: master
Commit: b783d22b219034e0af9f42c87b1b38eee0629bdc
URL:    
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=b783d22b219034e0af9f42c87b1b38eee0629bdc

Author: Philippe Gerum <r...@xenomai.org>
Date:   Mon May 11 21:09:50 2015 +0200

boilerplate/setup, utils, testsuite: enable application help strings

---

 demo/alchemy/altency.c      |   42 ++++++++++++++++++++----------------
 demo/posix/can-rtt.c        |   22 ++++++++-----------
 include/boilerplate/setup.h |    9 +++++++-
 include/xenomai/init.h      |    4 ++++
 lib/boilerplate/setup.c     |   37 ++++++++++++++++++++++----------
 testsuite/latency/latency.c |   43 +++++++++++++++++++++----------------
 utils/autotune/autotune.c   |   50 +++++++++++++++++--------------------------
 utils/corectl/corectl.c     |   25 ++++++++--------------
 utils/hdb/hdb.c             |   17 +++++----------
 9 files changed, 129 insertions(+), 120 deletions(-)

diff --git a/demo/alchemy/altency.c b/demo/alchemy/altency.c
index 5365ab2..83cc806 100644
--- a/demo/alchemy/altency.c
+++ b/demo/alchemy/altency.c
@@ -17,6 +17,7 @@
 #include <alchemy/sem.h>
 #include <rtdm/testing.h>
 #include <boilerplate/trace.h>
+#include <xenomai/init.h>
 
 RT_TASK latency_task, display_task;
 
@@ -490,6 +491,28 @@ static void sigdebug(int sig, siginfo_t *si, void *context)
 
 #endif /* CONFIG_XENO_COBALT */
 
+void application_usage(void)
+{
+        fprintf(stderr, "usage: %s [options]:\n", get_program_name());
+       fprintf(stderr,
+               "-h                              print histograms of min, avg, 
max latencies\n"
+               "-g <file>                       dump histogram to <file> in 
gnuplot format\n"
+               "-s                              print statistics of min, avg, 
max latencies\n"
+               "-H <histogram-size>             default = 200, increase if 
your last bucket is full\n"
+               "-B <bucket-size>                default = 1000ns, decrease for 
more resolution\n"
+               "-p <period_us>                  sampling period\n"
+               "-l <data-lines per header>      default=21, 0 to supress 
headers\n"
+               "-T <test_duration_seconds>      default=0, so ^C to end\n"
+               "-q                              supresses RTD, RTH lines if -T 
is used\n"
+               "-D <testing_device_no>          number of testing device, 
default=0\n"
+               "-t <test_mode>                  0=user task (default), 
1=kernel task, 2=timer IRQ\n"
+               "-f                              freeze trace for each new max 
latency\n"
+               "-c <cpu>                        pin measuring task down to 
given CPU\n"
+               "-P <priority>                   task priority (test mode 0 and 
1 only)\n"
+               "-b                              break upon mode switch\n"
+               );
+}
+
 int main(int argc, char *const *argv)
 {
        struct sigaction sa __attribute__((unused));
@@ -552,24 +575,7 @@ int main(int argc, char *const *argv)
                        stop_upon_switch = 1;
                        break;
                default:
-
-                       fprintf(stderr,
-"usage: altency [options]\n"
-"  [-h]                         # print histograms of min, avg, max 
latencies\n"
-"  [-g <file>]                  # dump histogram to <file> in gnuplot format\n"
-"  [-s]                         # print statistics of min, avg, max 
latencies\n"
-"  [-H <histogram-size>]        # default = 200, increase if your last bucket 
is full\n"
-"  [-B <bucket-size>]           # default = 1000ns, decrease for more 
resolution\n"
-"  [-p <period_us>]             # sampling period\n"
-"  [-l <data-lines per header>] # default=21, 0 to supress headers\n"
-"  [-T <test_duration_seconds>] # default=0, so ^C to end\n"
-"  [-q]                         # supresses RTD, RTH lines if -T is used\n"
-"  [-t <test_mode>]             # 0=user task (default), 1=kernel task, 
2=timer IRQ\n"
-"  [-f]                         # freeze trace for each new max latency\n"
-"  [-c <cpu>]                   # pin measuring task down to given CPU\n"
-"  [-P <priority>]              # task priority (test mode 0 and 1 only)\n"
-"  [-b]                         # break upon mode switch\n"
-);
+                       xenomai_usage();
                        exit(2);
                }
 
diff --git a/demo/posix/can-rtt.c b/demo/posix/can-rtt.c
index da8836d..6c7c54d 100644
--- a/demo/posix/can-rtt.c
+++ b/demo/posix/can-rtt.c
@@ -56,6 +56,7 @@
 #include <linux/can.h>
 #include <linux/can/raw.h>
 #endif
+#include <xenomai/init.h>
 
 #define NSEC_PER_SEC 1000000000
 
@@ -78,16 +79,14 @@ struct rtt_stat {
     int counts_per_sec;
 };
 
-static void print_usage(char *prg)
+void application_usage(void)
 {
+    fprintf(stderr, "usage: %s [options] <tx-can-interface> 
<rx-can-interface>:\n",
+           get_program_name());
     fprintf(stderr,
-           "Usage: %s  [Options] <tx-can-interface> <rx-can-interface>\n"
-           "Options:\n"
-           " -h, --help     This help\n"
-           " -r, --repeater Repeater, send back received messages\n"
-           " -i, --id=ID    CAN Identifier (default = 0x1)\n"
-           " -c, --cycle    Cycle time in us (default = 10000us)\n",
-           prg);
+           " -r, --repeater                    Repeater, send back received 
messages\n"
+           " -i, --id=ID                       CAN Identifier (default = 
0x1)\n"
+           " -c, --cycle                       Cycle time in us (default = 
10000us)\n");
 }
 
 static void *transmitter(void *arg)
@@ -219,11 +218,10 @@ int main(int argc, char *argv[])
        { "id", required_argument, 0, 'i'},
        { "cycle", required_argument, 0, 'c'},
        { "repeater", no_argument, 0, 'r'},
-       { "help", no_argument, 0, 'h'},
        { 0, 0, 0, 0},
     };
 
-    while ((opt = getopt_long(argc, argv, "hri:c:",
+    while ((opt = getopt_long(argc, argv, "ri:c:",
                              long_options, NULL)) != -1) {
        switch (opt) {
        case 'c':
@@ -240,15 +238,13 @@ int main(int argc, char *argv[])
 
        default:
            fprintf(stderr, "Unknown option %c\n", opt);
-       case 'h':
-           print_usage(argv[0]);
            exit(-1);
        }
     }
 
     printf("%d %d\n", optind, argc);
     if (optind + 2 != argc) {
-       print_usage(argv[0]);
+       xenomai_usage();
        exit(0);
     }
 
diff --git a/include/boilerplate/setup.h b/include/boilerplate/setup.h
index 5345c0d..0879618 100644
--- a/include/boilerplate/setup.h
+++ b/include/boilerplate/setup.h
@@ -20,6 +20,7 @@
 
 #include <boilerplate/list.h>
 #include <boilerplate/wrappers.h>
+#include <string.h>
 #include <sched.h>
 
 struct base_setup_data {
@@ -64,7 +65,8 @@ struct setup_descriptor {
  *
  * - The last priority level is used for the bootstrap code
  * (__bootstrap_ctor), which is guaranteed to run after any
- * setup/early code.
+ * setup/early code, provided such code is part of the main
+ * executable.
  *
  * The guarantees on the init order don't go beyond what is stated
  * here, do NOT assume more than this.
@@ -99,4 +101,9 @@ extern struct base_setup_data __base_setup_data;
 }
 #endif
 
+static inline const char *get_program_name(void)
+{
+       return basename(__base_setup_data.arg0 ?: "program");
+}
+
 #endif /* !_BOILERPLATE_SETUP_H */
diff --git a/include/xenomai/init.h b/include/xenomai/init.h
index 7ff469a..bd99da7 100644
--- a/include/xenomai/init.h
+++ b/include/xenomai/init.h
@@ -28,6 +28,10 @@ void xenomai_init(int *argcp, char *const **argvp);
 
 int xenomai_main(int argc, char *const argv[]);
 
+void xenomai_usage(void);
+
+void application_usage(void);
+
 extern const char *xenomai_version_string;
 
 #ifdef __cplusplus
diff --git a/lib/boilerplate/setup.c b/lib/boilerplate/setup.c
index efa1f3d..9dca6a0 100644
--- a/lib/boilerplate/setup.c
+++ b/lib/boilerplate/setup.c
@@ -253,12 +253,35 @@ static struct option *build_option_array(int 
*base_opt_startp)
        return options;
 }
 
-static void usage(void)
+void __weak application_usage(void)
+{
+       /*
+        * Applications can implement this hook for dumping their own
+        * help strings.
+        */
+        fprintf(stderr, "usage: %s <options>:\n", get_program_name());
+}
+
+void xenomai_usage(void)
 {
        struct setup_descriptor *setup;
 
        print_version();
-        fprintf(stderr, "usage: program <options>, where options may be:\n");
+
+       /*
+        * Dump help strings from the highest level code to the
+        * lowest.
+        */
+       application_usage();
+
+       if (!pvlist_empty(&setup_list)) {
+               pvlist_for_each_entry_reverse(setup, &setup_list,
+                                             __reserved.next) {
+                       if (setup->help)
+                               setup->help();
+               }
+       }
+
         fprintf(stderr, "--cpu-affinity=<cpu[,cpu]...> set CPU affinity of 
threads\n");
         fprintf(stderr, "--[no-]sanity                 disable/enable sanity 
checks\n");
         fprintf(stderr, "--quiet[=level]               tame down verbosity to 
desired level\n");
@@ -269,14 +292,6 @@ static void usage(void)
         fprintf(stderr, "--no-mlock                    do not lock memory at 
init\n");
 #endif
         fprintf(stderr, "--help                                display 
help\n");
-
-       if (pvlist_empty(&setup_list))
-               return;
-
-       pvlist_for_each_entry(setup, &setup_list, __reserved.next) {
-               if (setup->help)
-                       setup->help();
-       }
 }
 
 static int parse_base_options(int *argcp, char *const **argvp,
@@ -345,7 +360,7 @@ static int parse_base_options(int *argcp, char *const 
**argvp,
                        dump_configuration();
                        exit(0);
                case help_opt:
-                       usage();
+                       xenomai_usage();
                        exit(0);
                default:
                        /* Skin option, don't process yet. */
diff --git a/testsuite/latency/latency.c b/testsuite/latency/latency.c
index e3dadae..f76e3e8 100644
--- a/testsuite/latency/latency.c
+++ b/testsuite/latency/latency.c
@@ -15,6 +15,7 @@
 #include <xeno_config.h>
 #include <rtdm/testing.h>
 #include <boilerplate/trace.h>
+#include <xenomai/init.h>
 
 pthread_t latency_task, display_task;
 
@@ -573,6 +574,28 @@ static void sigdebug(int sig, siginfo_t *si, void *context)
 
 #endif /* CONFIG_XENO_COBALT */
 
+void application_usage(void)
+{
+        fprintf(stderr, "usage: %s [options]:\n", get_program_name());
+       fprintf(stderr,
+               "-h                              print histograms of min, avg, 
max latencies\n"
+               "-g <file>                       dump histogram to <file> in 
gnuplot format\n"
+               "-s                              print statistics of min, avg, 
max latencies\n"
+               "-H <histogram-size>             default = 200, increase if 
your last bucket is full\n"
+               "-B <bucket-size>                default = 1000ns, decrease for 
more resolution\n"
+               "-p <period_us>                  sampling period\n"
+               "-l <data-lines per header>      default=21, 0 to supress 
headers\n"
+               "-T <test_duration_seconds>      default=0, so ^C to end\n"
+               "-q                              supresses RTD, RTH lines if -T 
is used\n"
+               "-D <testing_device_no>          number of testing device, 
default=0\n"
+               "-t <test_mode>                  0=user task (default), 
1=kernel task, 2=timer IRQ\n"
+               "-f                              freeze trace for each new max 
latency\n"
+               "-c <cpu>                        pin measuring task down to 
given CPU\n"
+               "-P <priority>                   task priority (test mode 0 and 
1 only)\n"
+               "-b                              break upon mode switch\n"
+               );
+}
+
 int main(int argc, char *const *argv)
 {
        struct sigaction sa __attribute__((unused));
@@ -658,25 +681,7 @@ int main(int argc, char *const *argv)
                        break;
 
                default:
-
-                       fprintf(stderr,
-"usage: latency [options]\n"
-"  [-h]                         # print histograms of min, avg, max 
latencies\n"
-"  [-g <file>]                  # dump histogram to <file> in gnuplot format\n"
-"  [-s]                         # print statistics of min, avg, max 
latencies\n"
-"  [-H <histogram-size>]        # default = 200, increase if your last bucket 
is full\n"
-"  [-B <bucket-size>]           # default = 1000ns, decrease for more 
resolution\n"
-"  [-p <period_us>]             # sampling period\n"
-"  [-l <data-lines per header>] # default=21, 0 to supress headers\n"
-"  [-T <test_duration_seconds>] # default=0, so ^C to end\n"
-"  [-q]                         # supresses RTD, RTH lines if -T is used\n"
-"  [-D <testing_device_no>]     # number of testing device, default=0\n"
-"  [-t <test_mode>]             # 0=user task (default), 1=kernel task, 
2=timer IRQ\n"
-"  [-f]                         # freeze trace for each new max latency\n"
-"  [-c <cpu>]                   # pin measuring task down to given CPU\n"
-"  [-P <priority>]              # task priority (test mode 0 and 1 only)\n"
-"  [-b]                         # break upon mode switch\n"
-);
+                       xenomai_usage();
                        exit(2);
                }
 
diff --git a/utils/autotune/autotune.c b/utils/autotune/autotune.c
index de1365b..140355a 100644
--- a/utils/autotune/autotune.c
+++ b/utils/autotune/autotune.c
@@ -27,7 +27,7 @@
 #include <error.h>
 #include <sys/cobalt.h>
 #include <rtdm/autotune.h>
-#include <boilerplate/setup.h>
+#include <xenomai/init.h>
 
 static int tune_irqlat, tune_kernlat, tune_userlat;
 
@@ -38,49 +38,42 @@ static int reset, noload, background;
 
 static const struct option base_options[] = {
        {
-#define help_opt       0
-               .name = "help",
-               .has_arg = 0,
-               .flag = NULL,
-               .val = 0
-       },
-       {
-#define irq_opt                1
+#define irq_opt                0
                .name = "irq",
                .flag = &tune_irqlat,
                .val = 1
        },
        {
-#define kernel_opt     2
+#define kernel_opt     1
                .name = "kernel",
                .flag = &tune_kernlat,
                .val = 1
        },
        {
-#define user_opt       3
+#define user_opt       2
                .name = "user",
                .flag = &tune_userlat,
                .val = 1
        },
        {
-#define reset_opt      4
+#define reset_opt      3
                .name = "reset",
                .flag = &reset,
                .val = 1
        },
        {
-#define noload_opt     5
+#define noload_opt     4
                .name = "noload",
                .flag = &noload,
                .val = 1
        },
        {
-#define period_opt     6
+#define period_opt     5
                .name = "period",
                .has_arg = 1,
        },
        {
-#define background_opt 7
+#define background_opt 6
                .name = "background",
                .flag = &background,
                .val = 1,
@@ -194,18 +187,18 @@ static void create_load(pthread_t *tid)
        pthread_setname_np(*tid, "loadgen");
 }
 
-static void usage(void)
+void application_usage(void)
 {
-       fprintf(stderr, "usage: autotune [options], tuning core timer for:\n");
-       fprintf(stderr, "   --irq               interrupt latency\n");
-       fprintf(stderr, "   --kernel            kernel scheduling latency\n");
-       fprintf(stderr, "   --user              user scheduling latency\n");
-       fprintf(stderr, "   --period            set the sampling period\n");
-       fprintf(stderr, "   --reset             reset core timer gravity to 
factory defaults\n");
-       fprintf(stderr, "   --noload            disable load generation\n");
-       fprintf(stderr, "   --background        run in the background\n");
-       fprintf(stderr, "   --help              print this help\n\n");
-       fprintf(stderr, "if no option is given, tune for all contexts using the 
default period.\n");
+        fprintf(stderr, "usage: %s [options]:\n", get_program_name());
+       fprintf(stderr, "--irq                          tune for interrupt 
latency\n");
+       fprintf(stderr, "--kernel                       tune for kernel 
scheduling latency\n");
+       fprintf(stderr, "--user                         tune for user 
scheduling latency\n");
+       fprintf(stderr, "    [ if none of --irq, --kernel and --user is 
given,\n"
+                       "      tune for all contexts ]\n");
+       fprintf(stderr, "--period                       set the sampling 
period\n");
+       fprintf(stderr, "--reset                        reset core timer 
gravity to factory defaults\n");
+       fprintf(stderr, "--noload                       disable load 
generation\n");
+       fprintf(stderr, "--background                   run in the 
background\n");
 }
 
 static void run_tuner(int fd, unsigned int op, int period, const char *type)
@@ -253,16 +246,13 @@ int main(int argc, char *const argv[])
                if (c == EOF)
                        break;
                if (c == '?') {
-                       usage();
+                       xenomai_usage();
                        return EINVAL;
                }
                if (c > 0)
                        continue;
 
                switch (lindex) {
-               case help_opt:
-                       usage();
-                       exit(0);
                case period_opt:
                        period = atoi(optarg);
                        if (period <= 0)
diff --git a/utils/corectl/corectl.c b/utils/corectl/corectl.c
index 6d0b7b3..7104086 100644
--- a/utils/corectl/corectl.c
+++ b/utils/corectl/corectl.c
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <error.h>
 #include <sys/cobalt.h>
+#include <xenomai/init.h>
 
 int __cobalt_no_shadow = 1;
 
@@ -50,20 +51,15 @@ static const struct option options[] = {
                .flag = &action,
                .val = start_opt,
        },
-       {
-#define help_opt       3
-               .name = "help",
-       },
        { /* Sentinel */ }
 };
 
-static void usage(void)
+void application_usage(void)
 {
-       fprintf(stderr, "usage: corectl [options]:\n");
-       fprintf(stderr, "   --stop [<grace-seconds>]    stop Xenomai/cobalt 
services\n");
-       fprintf(stderr, "   --start                     start Xenomai/cobalt 
services\n");
-       fprintf(stderr, "   --status                    query Xenomai/cobalt 
status\n");
-       fprintf(stderr, "   --help                      print this help\n\n");
+        fprintf(stderr, "usage: %s <option>:\n", get_program_name());
+       fprintf(stderr, "--stop [<grace-seconds>]       stop Xenomai/cobalt 
services\n");
+       fprintf(stderr, "--start                        start Xenomai/cobalt 
services\n");
+       fprintf(stderr, "--status                       query Xenomai/cobalt 
status\n");
 }
 
 static int core_stop(int grace_period)
@@ -117,16 +113,13 @@ int main(int argc, char *const argv[])
                if (c == EOF)
                        break;
                if (c == '?') {
-                       usage();
+                       xenomai_usage();
                        return EINVAL;
                }
                if (c > 0)
                        continue;
 
                switch (lindex) {
-               case help_opt:
-                       usage();
-                       exit(0);
                case stop_opt:
                        grace_period = optarg ? atoi(optarg) : 0;
                case start_opt:
@@ -148,8 +141,8 @@ int main(int argc, char *const argv[])
                ret = core_status();
                break;
        default:
-               usage();
-               exit(0);
+               xenomai_usage();
+               exit(1);
        }
 
        if (ret)
diff --git a/utils/hdb/hdb.c b/utils/hdb/hdb.c
index 233beb5..10ee1a9 100644
--- a/utils/hdb/hdb.c
+++ b/utils/hdb/hdb.c
@@ -25,6 +25,7 @@
 #include <error.h>
 #include <fcntl.h>
 #include <copperplate/cluster.h>
+#include <xenomai/init.h>
 
 static const struct option options[] = {
        {
@@ -32,18 +33,13 @@ static const struct option options[] = {
                .name = "dump-cluster",
                .has_arg = 1,
        },
-       {
-#define help_opt       1
-               .name = "help",
-       },
        { /* Sentinel */ }
 };
 
-static void usage(void)
+void application_usage(void)
 {
-       fprintf(stderr, "usage: hdb [options]:\n");
-       fprintf(stderr, "   --dump-cluster <name>       dump cluster <name>\n");
-       fprintf(stderr, "   --help                      print this help\n\n");
+        fprintf(stderr, "usage: %s <option>:\n", get_program_name());
+       fprintf(stderr, "--dump-cluster <name>          dump cluster <name>\n");
 }
 
 static int check_shared_heap(const char *cmd)
@@ -127,16 +123,13 @@ int main(int argc, char *const argv[])
                if (c == EOF)
                        break;
                if (c == '?') {
-                       usage();
+                       xenomai_usage();
                        return EINVAL;
                }
                if (c > 0)
                        continue;
 
                switch (lindex) {
-               case help_opt:
-                       usage();
-                       exit(0);
                case dump_cluster_opt:
                        cluster_name = optarg;
                        break;


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://xenomai.org/mailman/listinfo/xenomai-git

Reply via email to