From 915ee1c2d7244733726e71126cb7d38bffbf55fa Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Wed, 30 Mar 2011 23:41:06 +0200 Subject: digital/avr/modules/host: get instance from command line, refs #157 --- digital/avr/modules/host/host.h | 5 ++++ digital/avr/modules/host/host.host.c | 50 +++++++++++++++++++++++++++++-- digital/avr/modules/host/test/test_host.c | 6 +++- 3 files changed, 58 insertions(+), 3 deletions(-) (limited to 'digital/avr') diff --git a/digital/avr/modules/host/host.h b/digital/avr/modules/host/host.h index 73cd4a58..03a8f7b8 100644 --- a/digital/avr/modules/host/host.h +++ b/digital/avr/modules/host/host.h @@ -39,6 +39,11 @@ host_init (int argc, char **argv); void host_get_program_arguments (int *argc, char ***argv); +/** Retrieve instance given on command line, or use default. Strip tail + * components if requested. Buffer is valid until next call. */ +const char * +host_get_instance (const char *def, int strip); + /** Host variables are usefull on reset. They are passed in the environment. * This is not optimised for performance. */ diff --git a/digital/avr/modules/host/host.host.c b/digital/avr/modules/host/host.host.c index 7967e9cd..9d039f75 100644 --- a/digital/avr/modules/host/host.host.c +++ b/digital/avr/modules/host/host.host.c @@ -30,18 +30,38 @@ #include #include #include +#include /* Saved arguments. Uninitialised global variables are set to 0 by the * compiler. */ static int host_saved_argc; static char **host_saved_argv; +/* User arguments not used by this module. */ +static int host_user_argc; +static char **host_user_argv; + +/* Instance string, from program arguments. */ +static char *host_instance; + /** Initialise host module. */ void host_init (int argc, char **argv) { host_saved_argc = argc; host_saved_argv = argv; + /* Get instance argument. */ + argv++; + argc--; + if (argc && strncmp (argv[0], "-i", 2) == 0) + { + host_instance = argv[0] + 2; + argv++; + argc--; + } + /* Save other arguments. */ + host_user_argc = argc; + host_user_argv = argv; } /** Retrieve saved program arguments. Program name and used parameters are @@ -50,8 +70,34 @@ void host_get_program_arguments (int *argc, char ***argv) { assert (host_saved_argc); - *argc = host_saved_argc - 1; - *argv = host_saved_argv + 1; + *argc = host_user_argc; + *argv = host_user_argv; +} + +/** Retrieve instance given on command line, or use default. Strip tail + * components if requested. Buffer is valid until next call. */ +const char * +host_get_instance (const char *def, int strip) +{ + if (!host_instance) + return def; + else if (strip == 0) + return host_instance; + else + { + static char stripped[256]; + assert (strlen (host_instance) < sizeof (stripped)); + strcpy (stripped, host_instance); + while (strip--) + { + char *p = strrchr (stripped, ':'); + if (!p) + return ""; + else + *p = '\0'; + } + return stripped; + } } /** Register a host integer. */ diff --git a/digital/avr/modules/host/test/test_host.c b/digital/avr/modules/host/test/test_host.c index 8c98a94a..5df65459 100644 --- a/digital/avr/modules/host/test/test_host.c +++ b/digital/avr/modules/host/test/test_host.c @@ -51,8 +51,12 @@ main (int argc, char **argv) else { printf ("set\n"); - assert_print (argc == 2 && strcmp (argv[1], "ni") == 0, + host_get_program_arguments (&ac, &av); + assert_print (ac == 1 && strcmp (av[0], "ni") == 0, "please provide \"ni\" as the first argument"); + printf ("instance %s\n", host_get_instance ("none", 0)); + printf ("instance -1 %s\n", host_get_instance ("none", 1)); + printf ("instance -2 %s\n", host_get_instance ("none", 2)); host_register_integer ("avr_integer", 42); host_register_string ("avr_string", "Ni!"); host_reset (); -- cgit v1.2.3