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.host.c | 50 ++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'digital/avr/modules/host/host.host.c') 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. */ -- cgit v1.2.3