2004-01-11 00:30:26 +03:00
|
|
|
/*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University.
|
|
|
|
* All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
|
|
|
|
* All rights reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-01-11 00:30:26 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
#include "ompi_config.h"
|
2004-01-11 00:30:26 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2005-03-14 23:57:21 +03:00
|
|
|
#include <ctype.h>
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
#include "include/constants.h"
|
2005-07-03 20:06:07 +04:00
|
|
|
#include "opal/class/opal_object.h"
|
2005-07-03 20:22:16 +04:00
|
|
|
#include "opal/class/opal_list.h"
|
2005-07-04 02:45:48 +04:00
|
|
|
#include "opal/threads/mutex.h"
|
2005-07-04 04:13:44 +04:00
|
|
|
#include "opal/util/argv.h"
|
|
|
|
#include "opal/util/cmd_line.h"
|
2004-03-17 21:45:16 +03:00
|
|
|
#include "util/strncpy.h"
|
2005-07-04 03:31:27 +04:00
|
|
|
#include "opal/util/output.h"
|
2005-03-14 23:57:21 +03:00
|
|
|
#include "mca/base/mca_base_param.h"
|
2004-01-11 00:30:26 +03:00
|
|
|
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
/*
|
|
|
|
* Some usage message constants
|
|
|
|
*
|
|
|
|
* Max width for param listings before the description will be listed
|
|
|
|
* on the next line
|
|
|
|
*/
|
|
|
|
#define PARAM_WIDTH 25
|
|
|
|
/*
|
|
|
|
* Max length of any line in the usage message
|
|
|
|
*/
|
|
|
|
#define MAX_WIDTH 76
|
|
|
|
|
2004-01-11 00:30:26 +03:00
|
|
|
/*
|
|
|
|
* Description of a command line option
|
|
|
|
*/
|
2004-01-21 02:58:23 +03:00
|
|
|
struct cmd_line_option_t {
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t super;
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
char clo_short_name;
|
|
|
|
char *clo_single_dash_name;
|
|
|
|
char *clo_long_name;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
int clo_num_params;
|
|
|
|
char *clo_description;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_cmd_line_type_t clo_type;
|
2005-03-19 02:40:08 +03:00
|
|
|
char *clo_mca_param_env_var;
|
2005-03-14 23:57:21 +03:00
|
|
|
void *clo_variable_dest;
|
|
|
|
bool clo_variable_set;
|
2004-01-21 02:58:23 +03:00
|
|
|
};
|
|
|
|
typedef struct cmd_line_option_t cmd_line_option_t;
|
2004-08-27 20:44:12 +04:00
|
|
|
static void option_constructor(cmd_line_option_t *cmd);
|
|
|
|
static void option_destructor(cmd_line_option_t *cmd);
|
2004-09-23 04:00:01 +04:00
|
|
|
static OBJ_CLASS_INSTANCE(cmd_line_option_t,
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t,
|
2004-09-23 04:00:01 +04:00
|
|
|
option_constructor, option_destructor);
|
2004-01-11 00:30:26 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* An option that was used in the argv that was parsed
|
|
|
|
*/
|
2004-01-21 02:58:23 +03:00
|
|
|
struct cmd_line_param_t {
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t super;
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Note that clp_arg points to storage "owned" by someone else; it
|
|
|
|
has the original option string by referene, not by value.
|
|
|
|
Hence, it should not be free()'ed. */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
char *clp_arg;
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Pointer to the existing option. This is also by reference; it
|
|
|
|
should not be free()ed. */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd_line_option_t *clp_option;
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* This argv array is a list of all the parameters of this option.
|
|
|
|
It is owned by this parameter, and should be freed when this
|
|
|
|
param_t is freed. */
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
int clp_argc;
|
|
|
|
char **clp_argv;
|
2004-01-21 02:58:23 +03:00
|
|
|
};
|
|
|
|
typedef struct cmd_line_param_t cmd_line_param_t;
|
2004-08-27 20:44:12 +04:00
|
|
|
static void param_constructor(cmd_line_param_t *cmd);
|
|
|
|
static void param_destructor(cmd_line_param_t *cmd);
|
2004-09-23 04:00:01 +04:00
|
|
|
static OBJ_CLASS_INSTANCE(cmd_line_param_t,
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t,
|
2004-09-23 04:00:01 +04:00
|
|
|
param_constructor, param_destructor);
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/*
|
2005-07-04 04:13:44 +04:00
|
|
|
* Instantiate the opal_cmd_line_t class
|
2004-08-26 11:56:45 +04:00
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
static void cmd_line_constructor(opal_cmd_line_t *cmd);
|
|
|
|
static void cmd_line_destructor(opal_cmd_line_t *cmd);
|
|
|
|
OBJ_CLASS_INSTANCE(opal_cmd_line_t,
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_object_t,
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd_line_constructor,
|
|
|
|
cmd_line_destructor);
|
2004-01-21 02:58:23 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Private variables
|
|
|
|
*/
|
|
|
|
static char special_empty_token[] = {
|
2004-08-26 11:56:45 +04:00
|
|
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '\0'
|
2004-01-21 02:58:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private functions
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
static int make_opt(opal_cmd_line_t *cmd, opal_cmd_line_init_t *e);
|
|
|
|
static void free_parse_results(opal_cmd_line_t *cmd);
|
|
|
|
static int split_shorts(opal_cmd_line_t *cmd,
|
2004-10-01 22:38:16 +04:00
|
|
|
char *token, char **args,
|
|
|
|
int *output_argc, char ***output_argv,
|
|
|
|
int *num_args_used, bool ignore_unknown);
|
2005-07-04 04:13:44 +04:00
|
|
|
static cmd_line_option_t *find_option(opal_cmd_line_t *cmd,
|
2004-01-21 02:58:23 +03:00
|
|
|
const char *option_name);
|
2005-03-14 23:57:21 +03:00
|
|
|
static void set_dest(cmd_line_option_t *option, char *sval);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an entire command line handle from a table
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_cmd_line_create(opal_cmd_line_t *cmd,
|
|
|
|
opal_cmd_line_init_t *table)
|
2005-03-14 23:57:21 +03:00
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
/* Check bozo case */
|
|
|
|
|
|
|
|
if (NULL == cmd) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
2005-07-04 04:13:44 +04:00
|
|
|
OBJ_CONSTRUCT(cmd, opal_cmd_line_t);
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
/* Ensure we got a table */
|
|
|
|
|
|
|
|
if (NULL == table) {
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop through the table */
|
|
|
|
|
|
|
|
for (i = 0; ; ++i) {
|
|
|
|
|
|
|
|
/* Is this the end? */
|
|
|
|
|
|
|
|
if ('\0' == table[i].ocl_cmd_short_name &&
|
|
|
|
NULL == table[i].ocl_cmd_single_dash_name &&
|
|
|
|
NULL == table[i].ocl_cmd_long_name) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nope -- it's an entry. Process it. */
|
|
|
|
|
|
|
|
ret = make_opt(cmd, &table[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
2004-01-11 00:30:26 +03:00
|
|
|
|
|
|
|
|
2004-06-17 18:25:29 +04:00
|
|
|
/*
|
2004-08-26 11:56:45 +04:00
|
|
|
* Create a command line option, --long-name and/or -s (short name).
|
2004-01-11 00:30:26 +03:00
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_cmd_line_make_opt(opal_cmd_line_t *cmd, char short_name,
|
2004-08-26 11:56:45 +04:00
|
|
|
const char *long_name, int num_params,
|
|
|
|
const char *desc)
|
2004-01-11 00:30:26 +03:00
|
|
|
{
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_cmd_line_init_t e;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
e.ocl_mca_type_name = NULL;
|
|
|
|
e.ocl_mca_component_name = NULL;
|
|
|
|
e.ocl_mca_param_name = NULL;
|
|
|
|
|
|
|
|
e.ocl_cmd_short_name = short_name;
|
|
|
|
e.ocl_cmd_single_dash_name = NULL;
|
|
|
|
e.ocl_cmd_long_name = long_name;
|
|
|
|
|
|
|
|
e.ocl_num_params = num_params;
|
|
|
|
|
|
|
|
e.ocl_variable_dest = NULL;
|
2005-07-04 04:13:44 +04:00
|
|
|
e.ocl_variable_type = OPAL_CMD_LINE_TYPE_NULL;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
e.ocl_description = desc;
|
|
|
|
|
|
|
|
return make_opt(cmd, &e);
|
2004-01-11 00:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-17 18:25:29 +04:00
|
|
|
/*
|
2004-08-26 11:56:45 +04:00
|
|
|
* Create a command line option, --long-name and/or -s (short name).
|
2004-01-11 00:30:26 +03:00
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_cmd_line_make_opt3(opal_cmd_line_t *cmd, char short_name,
|
2004-08-26 11:56:45 +04:00
|
|
|
const char *sd_name, const char *long_name,
|
|
|
|
int num_params, const char *desc)
|
2004-01-11 00:30:26 +03:00
|
|
|
{
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_cmd_line_init_t e;
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
e.ocl_mca_type_name = NULL;
|
|
|
|
e.ocl_mca_component_name = NULL;
|
|
|
|
e.ocl_mca_param_name = NULL;
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
e.ocl_cmd_short_name = short_name;
|
|
|
|
e.ocl_cmd_single_dash_name = sd_name;
|
|
|
|
e.ocl_cmd_long_name = long_name;
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
e.ocl_num_params = num_params;
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
e.ocl_variable_dest = NULL;
|
2005-07-04 04:13:44 +04:00
|
|
|
e.ocl_variable_type = OPAL_CMD_LINE_TYPE_NULL;
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
e.ocl_description = desc;
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
return make_opt(cmd, &e);
|
2004-01-11 00:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-17 18:25:29 +04:00
|
|
|
/*
|
2005-07-04 04:13:44 +04:00
|
|
|
* Parse a command line according to a pre-built OPAL command line
|
2004-01-21 02:58:23 +03:00
|
|
|
* handle.
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_cmd_line_parse(opal_cmd_line_t *cmd, bool ignore_unknown,
|
2004-08-26 11:56:45 +04:00
|
|
|
int argc, char **argv)
|
2004-01-11 00:30:26 +03:00
|
|
|
{
|
2004-08-26 11:56:45 +04:00
|
|
|
int i, j, orig, ret;
|
|
|
|
cmd_line_option_t *option;
|
|
|
|
cmd_line_param_t *param;
|
|
|
|
bool is_unknown;
|
|
|
|
bool is_option;
|
2004-10-01 22:38:16 +04:00
|
|
|
char **shortsv;
|
|
|
|
int shortsc;
|
|
|
|
int num_args_used;
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-28 20:30:29 +04:00
|
|
|
/* Bozo check */
|
|
|
|
|
|
|
|
if (0 == argc || NULL == argv) {
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Thread serialization */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(&cmd->lcl_mutex);
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Free any parsed results that are already on this handle */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
free_parse_results(cmd);
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Analyze each token */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd->lcl_argc = argc;
|
2005-07-04 04:13:44 +04:00
|
|
|
cmd->lcl_argv = opal_argv_copy(argv);
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Now traverse the easy-to-parse sequence of tokens. Note that
|
|
|
|
incrementing i must happen elsehwere; it can't be the third
|
|
|
|
clause in the "if" statement. */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
param = NULL;
|
|
|
|
option = NULL;
|
|
|
|
for (i = 1; i < cmd->lcl_argc; ) {
|
|
|
|
is_unknown = false;
|
|
|
|
is_option = false;
|
|
|
|
|
|
|
|
/* Are we done? i.e., did we find the special "--" token? If
|
|
|
|
so, copy everying beyond it into the tail (i.e., don't
|
|
|
|
bother copying the "--" into the tail). */
|
|
|
|
|
|
|
|
if (0 == strcmp(cmd->lcl_argv[i], "--")) {
|
|
|
|
++i;
|
|
|
|
while (i < cmd->lcl_argc) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(&cmd->lcl_tail_argc, &cmd->lcl_tail_argv,
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd->lcl_argv[i]);
|
|
|
|
++i;
|
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
break;
|
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* If it's not an option, then we've found an unrecognized
|
|
|
|
token. */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
else if ('-' != cmd->lcl_argv[i][0]) {
|
|
|
|
is_unknown = true;
|
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Nope, this is supposedly an option. Is it a long name? */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
else if (0 == strncmp(cmd->lcl_argv[i], "--", 2)) {
|
|
|
|
is_option = true;
|
|
|
|
option = find_option(cmd, cmd->lcl_argv[i] + 2);
|
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-10-01 22:38:16 +04:00
|
|
|
/* It could be a short name. Is it? */
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
else {
|
|
|
|
option = find_option(cmd, cmd->lcl_argv[i] + 1);
|
2004-10-01 22:38:16 +04:00
|
|
|
|
|
|
|
/* If we didn't find it, try to split it into shorts. If
|
|
|
|
we find the short option, replace lcl_argv[i] and
|
|
|
|
insert the rest into lcl_argv starting after position
|
|
|
|
i. If we don't find the short option, don't do
|
|
|
|
anything to lcl_argv so that it can fall through to the
|
|
|
|
error condition, below. */
|
|
|
|
|
|
|
|
if (NULL == option) {
|
|
|
|
shortsv = NULL;
|
|
|
|
shortsc = 0;
|
|
|
|
ret = split_shorts(cmd, cmd->lcl_argv[i] + 1,
|
|
|
|
&(cmd->lcl_argv[i + 1]),
|
|
|
|
&shortsc, &shortsv,
|
|
|
|
&num_args_used, ignore_unknown);
|
|
|
|
if (OMPI_SUCCESS == ret) {
|
|
|
|
option = find_option(cmd, shortsv[0] + 1);
|
|
|
|
|
|
|
|
if (NULL != option) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_delete(&cmd->lcl_argc,
|
2004-12-07 05:42:17 +03:00
|
|
|
&cmd->lcl_argv, i,
|
2004-10-01 22:38:16 +04:00
|
|
|
1 + num_args_used);
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_insert(&cmd->lcl_argv, i, shortsv);
|
|
|
|
cmd->lcl_argc = opal_argv_count(cmd->lcl_argv);
|
2004-10-01 22:38:16 +04:00
|
|
|
} else {
|
|
|
|
is_unknown = true;
|
|
|
|
}
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_free(shortsv);
|
2004-10-01 22:38:16 +04:00
|
|
|
} else {
|
|
|
|
is_unknown = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != option) {
|
|
|
|
is_option = true;
|
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
}
|
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* If we figured out above that this is an option, handle it */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
if (is_option) {
|
|
|
|
if (NULL == option) {
|
|
|
|
is_unknown = true;
|
|
|
|
} else {
|
|
|
|
is_unknown = false;
|
|
|
|
orig = i;
|
|
|
|
++i;
|
|
|
|
|
|
|
|
/* Suck down the following parameters that belong to
|
|
|
|
this option. If we run out of parameters, or find
|
|
|
|
that any of them are the special_empty_param
|
|
|
|
(insertted by split_shorts()), then print an error
|
|
|
|
and return. */
|
|
|
|
|
|
|
|
param = OBJ_NEW(cmd_line_param_t);
|
|
|
|
if (NULL == param) {
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(&cmd->lcl_mutex);
|
2004-08-26 11:56:45 +04:00
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
param->clp_arg = cmd->lcl_argv[i];
|
|
|
|
param->clp_option = option;
|
|
|
|
|
|
|
|
/* If we have any parameters to this option, suck down
|
|
|
|
tokens starting one beyond the token that we just
|
|
|
|
recognized */
|
|
|
|
|
|
|
|
for (j = 0; j < option->clo_num_params; ++j, ++i) {
|
|
|
|
|
|
|
|
/* If we run out of parameters, error */
|
|
|
|
|
|
|
|
if (i >= cmd->lcl_argc) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "Error: option \"%s\" did not have "
|
2004-08-26 11:56:45 +04:00
|
|
|
"enough parameters (%d)",
|
|
|
|
cmd->lcl_argv[orig],
|
|
|
|
option->clo_num_params);
|
|
|
|
OBJ_RELEASE(param);
|
|
|
|
i = cmd->lcl_argc;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (0 == strcmp(cmd->lcl_argv[i],
|
|
|
|
special_empty_token)) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "Error: option \"%s\" did not have "
|
2004-08-26 11:56:45 +04:00
|
|
|
"enough parameters (%d)",
|
|
|
|
cmd->lcl_argv[orig],
|
|
|
|
option->clo_num_params);
|
|
|
|
if (NULL != param->clp_argv)
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_free(param->clp_argv);
|
2004-08-26 11:56:45 +04:00
|
|
|
OBJ_RELEASE(param);
|
|
|
|
i = cmd->lcl_argc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
/* Otherwise, save this parameter */
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
else {
|
2005-03-14 23:57:21 +03:00
|
|
|
/* Save in the argv on the param entry */
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(¶m->clp_argc,
|
2004-08-26 11:56:45 +04:00
|
|
|
¶m->clp_argv,
|
|
|
|
cmd->lcl_argv[i]);
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
/* If it's the first, save it in the
|
|
|
|
variable dest and/or MCA parameter */
|
|
|
|
|
|
|
|
if (0 == j &&
|
2005-03-19 02:40:08 +03:00
|
|
|
(NULL != option->clo_mca_param_env_var ||
|
2005-03-14 23:57:21 +03:00
|
|
|
NULL != option->clo_variable_dest)) {
|
|
|
|
set_dest(option, cmd->lcl_argv[i]);
|
|
|
|
}
|
2004-08-26 11:56:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
/* If there are no options to this command, see if we
|
|
|
|
need to set a boolean value to "true". */
|
|
|
|
|
|
|
|
if (0 == option->clo_num_params) {
|
|
|
|
set_dest(option, "1");
|
|
|
|
}
|
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* If we succeeded in all that, save the param to the
|
2005-07-04 04:13:44 +04:00
|
|
|
list on the opal_cmd_line_t handle */
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
if (NULL != param) {
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_append(&cmd->lcl_params, ¶m->super);
|
2004-08-26 11:56:45 +04:00
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* If we figured out above that this was an unknown option,
|
|
|
|
handle it. Copy everything (including the current token)
|
|
|
|
into the tail. If we're not ignoring unknowns, then print
|
|
|
|
an error. */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
if (is_unknown) {
|
|
|
|
if (!ignore_unknown) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "Error: unknown option \"%s\"",
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd->lcl_argv[i]);
|
|
|
|
}
|
|
|
|
while (i < cmd->lcl_argc) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(&cmd->lcl_tail_argc, &cmd->lcl_tail_argv,
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd->lcl_argv[i]);
|
|
|
|
++i;
|
|
|
|
}
|
2004-06-17 18:25:29 +04:00
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
}
|
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Thread serialization */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(&cmd->lcl_mutex);
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* All done */
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-01-11 00:30:26 +03:00
|
|
|
}
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-06-17 18:25:29 +04:00
|
|
|
/*
|
2005-07-04 04:13:44 +04:00
|
|
|
* Return a consolidated "usage" message for a OPAL command line handle.
|
2004-01-21 02:58:23 +03:00
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
char *opal_cmd_line_get_usage_msg(opal_cmd_line_t *cmd)
|
2004-01-11 00:30:26 +03:00
|
|
|
{
|
2005-04-10 20:14:48 +04:00
|
|
|
int i, len=MAX_WIDTH * 2, prev_len;
|
2004-08-26 11:56:45 +04:00
|
|
|
int argc;
|
|
|
|
char **argv;
|
2005-03-14 23:57:21 +03:00
|
|
|
char *ret, temp[MAX_WIDTH * 2], line[MAX_WIDTH * 2];
|
|
|
|
char *start, *desc, *ptr;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t *item;
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd_line_option_t *option;
|
2005-03-14 23:57:21 +03:00
|
|
|
bool filled;
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
/* Thread serialization */
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(&cmd->lcl_mutex);
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
/* Make an argv of all the usage strings */
|
|
|
|
|
|
|
|
prev_len = 0;
|
|
|
|
argc = 0;
|
|
|
|
argv = NULL;
|
|
|
|
ret = NULL;
|
2005-07-03 20:22:16 +04:00
|
|
|
for (item = opal_list_get_first(&cmd->lcl_options);
|
|
|
|
opal_list_get_end(&cmd->lcl_options) != item;
|
|
|
|
item = opal_list_get_next(item)) {
|
2004-08-26 11:56:45 +04:00
|
|
|
option = (cmd_line_option_t *) item;
|
|
|
|
if (NULL != option->clo_description) {
|
|
|
|
|
|
|
|
/* Build up the output line */
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
filled = false;
|
2004-08-26 11:56:45 +04:00
|
|
|
line[0] = '\0';
|
|
|
|
if ('\0' != option->clo_short_name) {
|
2005-03-14 23:57:21 +03:00
|
|
|
line[0] = '-';
|
|
|
|
line[1] = option->clo_short_name;
|
|
|
|
filled = true;
|
|
|
|
} else {
|
|
|
|
line[0] = ' ';
|
|
|
|
line[1] = ' ';
|
|
|
|
}
|
|
|
|
line[2] = '\0';
|
|
|
|
line[3] = '\0';
|
|
|
|
if (NULL != option->clo_single_dash_name) {
|
|
|
|
line[2] = (filled) ? '|' : ' ';
|
|
|
|
strcat(line, "-");
|
|
|
|
strcat(line, option->clo_single_dash_name);
|
|
|
|
filled = true;
|
2004-08-26 11:56:45 +04:00
|
|
|
}
|
|
|
|
if (NULL != option->clo_long_name) {
|
2005-03-14 23:57:21 +03:00
|
|
|
if (filled) {
|
2004-08-26 11:56:45 +04:00
|
|
|
strcat(line, "|");
|
2005-03-14 23:57:21 +03:00
|
|
|
} else {
|
|
|
|
strcat(line, " ");
|
2004-08-26 11:56:45 +04:00
|
|
|
}
|
2005-03-14 23:57:21 +03:00
|
|
|
strcat(line, "--");
|
|
|
|
strcat(line, option->clo_long_name);
|
|
|
|
filled = true;
|
2004-08-26 11:56:45 +04:00
|
|
|
}
|
|
|
|
strcat(line, " ");
|
|
|
|
for (i = 0; i < option->clo_num_params; ++i) {
|
|
|
|
snprintf(temp, len, "<arg%d>", i);
|
|
|
|
strcat(line, temp);
|
|
|
|
}
|
|
|
|
if (option->clo_num_params > 0) {
|
|
|
|
strcat(line, " ");
|
|
|
|
}
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
/* If we're less than param width, then start adding the
|
|
|
|
description to this line. Otherwise, finish this line
|
|
|
|
and start adding the description on the next line. */
|
|
|
|
|
|
|
|
if (strlen(line) > PARAM_WIDTH) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(&argc, &argv, line);
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
/* Now reset the line to be all blanks up to
|
|
|
|
PARAM_WIDTH so that we can start adding the
|
|
|
|
description */
|
|
|
|
|
|
|
|
memset(line, ' ', PARAM_WIDTH);
|
|
|
|
line[PARAM_WIDTH] = '\0';
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Add enough blanks to the end of the line so that we
|
|
|
|
can start adding the description */
|
|
|
|
|
|
|
|
for (i = strlen(line); i < PARAM_WIDTH; ++i) {
|
|
|
|
line[i] = ' ';
|
|
|
|
}
|
|
|
|
line[i] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop over adding the description to the array, breaking
|
|
|
|
the string at most at MAX_WIDTH characters. We need a
|
|
|
|
modifyable description (for simplicity), so strdup the
|
|
|
|
clo_description (because it's likely a compiler
|
|
|
|
constant, and may barf if we write temporary \0's in
|
|
|
|
the middle). */
|
|
|
|
|
|
|
|
desc = strdup(option->clo_description);
|
|
|
|
if (NULL == desc) {
|
|
|
|
return strdup("");
|
|
|
|
}
|
|
|
|
start = desc;
|
|
|
|
len = strlen(desc);
|
|
|
|
do {
|
|
|
|
|
|
|
|
/* Trim off leading whitespace */
|
|
|
|
|
|
|
|
while (isspace(*start) && start < desc + len) {
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
if (start >= desc + len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Last line */
|
|
|
|
|
|
|
|
if (strlen(start) < (MAX_WIDTH - PARAM_WIDTH)) {
|
|
|
|
strcat(line, start);
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(&argc, &argv, line);
|
2005-03-14 23:57:21 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have more than 1 line's worth left -- find this
|
|
|
|
line's worth and add it to the array. Then reset
|
|
|
|
and loop around to get the next line's worth. */
|
|
|
|
|
|
|
|
for (ptr = start + (MAX_WIDTH - PARAM_WIDTH);
|
|
|
|
ptr > start; --ptr) {
|
|
|
|
if (isspace(*ptr)) {
|
|
|
|
*ptr = '\0';
|
|
|
|
strcat(line, start);
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(&argc, &argv, line);
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
start = ptr + 1;
|
|
|
|
memset(line, ' ', PARAM_WIDTH);
|
|
|
|
line[PARAM_WIDTH] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-08-26 11:56:45 +04:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
/* If we got all the way back to the beginning of the
|
|
|
|
string, then go forward looking for a whitespace
|
|
|
|
and break there. */
|
|
|
|
|
|
|
|
if (ptr == start) {
|
|
|
|
for (ptr = start + (MAX_WIDTH - PARAM_WIDTH);
|
|
|
|
ptr < start + len; ++ptr) {
|
|
|
|
if (isspace(*ptr)) {
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
strcat(line, start);
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(&argc, &argv, line);
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
start = ptr + 1;
|
|
|
|
memset(line, ' ', PARAM_WIDTH);
|
|
|
|
line[PARAM_WIDTH] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we reached the end of the string with no
|
|
|
|
whitespace, then just add it on and be done */
|
|
|
|
|
|
|
|
if (ptr >= start + len) {
|
|
|
|
strcat(line, start);
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(&argc, &argv, line);
|
2005-03-14 23:57:21 +03:00
|
|
|
start = desc + len + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (start < desc + len);
|
|
|
|
free(desc);
|
2004-01-21 02:58:23 +03:00
|
|
|
}
|
|
|
|
}
|
2004-08-28 15:56:33 +04:00
|
|
|
if (NULL != argv) {
|
2005-07-04 04:13:44 +04:00
|
|
|
ret = opal_argv_join(argv, '\n');
|
|
|
|
opal_argv_free(argv);
|
2005-03-14 23:57:21 +03:00
|
|
|
} else {
|
|
|
|
ret = strdup("");
|
2004-08-26 11:56:45 +04:00
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Thread serialization */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(&cmd->lcl_mutex);
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* All done */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
return ret;
|
2004-01-21 02:58:23 +03:00
|
|
|
}
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-06-17 18:25:29 +04:00
|
|
|
/*
|
2004-01-21 02:58:23 +03:00
|
|
|
* Test if a given option was taken on the parsed command line.
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
bool opal_cmd_line_is_taken(opal_cmd_line_t *cmd, const char *opt)
|
2004-01-21 02:58:23 +03:00
|
|
|
{
|
2005-07-04 04:13:44 +04:00
|
|
|
return (opal_cmd_line_get_ninsts(cmd, opt) > 0);
|
2004-01-11 00:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-17 18:25:29 +04:00
|
|
|
/*
|
2004-01-21 02:58:23 +03:00
|
|
|
* Return the number of instances of an option found during parsing.
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_cmd_line_get_ninsts(opal_cmd_line_t *cmd, const char *opt)
|
2004-01-11 00:30:26 +03:00
|
|
|
{
|
2004-08-26 11:56:45 +04:00
|
|
|
int ret;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t *item;
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd_line_param_t *param;
|
|
|
|
cmd_line_option_t *option;
|
|
|
|
|
|
|
|
/* Thread serialization */
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(&cmd->lcl_mutex);
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
/* Find the corresponding option. If we find it, look through all
|
|
|
|
the parsed params and see if we have any matches. */
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
option = find_option(cmd, opt);
|
|
|
|
if (NULL != option) {
|
2005-07-03 20:22:16 +04:00
|
|
|
for (item = opal_list_get_first(&cmd->lcl_params);
|
|
|
|
opal_list_get_end(&cmd->lcl_params) != item;
|
|
|
|
item = opal_list_get_next(item)) {
|
2004-08-26 11:56:45 +04:00
|
|
|
param = (cmd_line_param_t *) item;
|
|
|
|
if (param->clp_option == option) {
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
}
|
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Thread serialization */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(&cmd->lcl_mutex);
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* All done */
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
return ret;
|
2004-01-11 00:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/*
|
2004-01-21 02:58:23 +03:00
|
|
|
* Return a specific parameter for a specific instance of a option
|
|
|
|
* from the parsed command line.
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
char *opal_cmd_line_get_param(opal_cmd_line_t *cmd, const char *opt, int inst,
|
2004-08-26 11:56:45 +04:00
|
|
|
int idx)
|
2004-01-11 00:30:26 +03:00
|
|
|
{
|
2004-08-26 11:56:45 +04:00
|
|
|
int num_found;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t *item;
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd_line_param_t *param;
|
|
|
|
cmd_line_option_t *option;
|
|
|
|
|
|
|
|
/* Thread serialization */
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(&cmd->lcl_mutex);
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
/* Find the corresponding option. If we find it, look through all
|
|
|
|
the parsed params and see if we have any matches. */
|
|
|
|
|
|
|
|
num_found = 0;
|
|
|
|
option = find_option(cmd, opt);
|
|
|
|
if (NULL != option) {
|
|
|
|
|
|
|
|
/* Ensure to check for the case where the user has asked for a
|
|
|
|
parameter index greater than we will have */
|
|
|
|
|
|
|
|
if (idx < option->clo_num_params) {
|
2005-07-03 20:22:16 +04:00
|
|
|
for (item = opal_list_get_first(&cmd->lcl_params);
|
|
|
|
opal_list_get_end(&cmd->lcl_params) != item;
|
|
|
|
item = opal_list_get_next(item)) {
|
2004-08-26 11:56:45 +04:00
|
|
|
param = (cmd_line_param_t *) item;
|
|
|
|
if (param->clp_option == option) {
|
|
|
|
if (num_found == inst) {
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(&cmd->lcl_mutex);
|
2004-08-26 11:56:45 +04:00
|
|
|
return param->clp_argv[idx];
|
|
|
|
}
|
|
|
|
++num_found;
|
|
|
|
}
|
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
}
|
|
|
|
}
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
/* Thread serialization */
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(&cmd->lcl_mutex);
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
/* All done */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
|
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/*
|
2005-07-04 04:13:44 +04:00
|
|
|
* Return the number of arguments parsed on a OPAL command line handle.
|
2004-08-26 11:56:45 +04:00
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_cmd_line_get_argc(opal_cmd_line_t *cmd)
|
2004-08-26 11:56:45 +04:00
|
|
|
{
|
|
|
|
return (NULL != cmd) ? cmd->lcl_argc : OMPI_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-07-04 04:13:44 +04:00
|
|
|
* Return a string argument parsed on a OPAL command line handle.
|
2004-08-26 11:56:45 +04:00
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
char *opal_cmd_line_get_argv(opal_cmd_line_t *cmd, int index)
|
2004-08-26 11:56:45 +04:00
|
|
|
{
|
|
|
|
return (NULL == cmd) ? NULL :
|
|
|
|
(index >= cmd->lcl_argc || index < 0) ? NULL : cmd->lcl_argv[index];
|
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/*
|
2005-07-04 04:13:44 +04:00
|
|
|
* Return the entire "tail" of unprocessed argv from a OPAL command
|
2004-08-26 11:56:45 +04:00
|
|
|
* line handle.
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_cmd_line_get_tail(opal_cmd_line_t *cmd, int *tailc, char ***tailv)
|
2004-08-26 11:56:45 +04:00
|
|
|
{
|
|
|
|
if (NULL != cmd) {
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(&cmd->lcl_mutex);
|
2004-08-26 11:56:45 +04:00
|
|
|
*tailc = cmd->lcl_tail_argc;
|
2005-07-04 04:13:44 +04:00
|
|
|
*tailv = opal_argv_copy(cmd->lcl_tail_argv);
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(&cmd->lcl_mutex);
|
2004-08-26 11:56:45 +04:00
|
|
|
return OMPI_SUCCESS;
|
|
|
|
} else {
|
|
|
|
return OMPI_ERROR;
|
|
|
|
}
|
2004-01-11 00:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
/**************************************************************************
|
|
|
|
* Static functions
|
|
|
|
**************************************************************************/
|
|
|
|
|
2004-08-27 20:44:12 +04:00
|
|
|
static void option_constructor(cmd_line_option_t *o)
|
|
|
|
{
|
|
|
|
o->clo_short_name = '\0';
|
|
|
|
o->clo_single_dash_name = NULL;
|
|
|
|
o->clo_long_name = NULL;
|
|
|
|
o->clo_num_params = 0;
|
|
|
|
o->clo_description = NULL;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
o->clo_type = OPAL_CMD_LINE_TYPE_NULL;
|
2005-03-19 02:40:08 +03:00
|
|
|
o->clo_mca_param_env_var = NULL;
|
2005-03-14 23:57:21 +03:00
|
|
|
o->clo_variable_dest = NULL;
|
|
|
|
o->clo_variable_set = false;
|
2004-08-27 20:44:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void option_destructor(cmd_line_option_t *o)
|
|
|
|
{
|
|
|
|
if (NULL != o->clo_single_dash_name) {
|
|
|
|
free(o->clo_single_dash_name);
|
|
|
|
}
|
|
|
|
if (NULL != o->clo_long_name) {
|
|
|
|
free(o->clo_long_name);
|
|
|
|
}
|
|
|
|
if (NULL != o->clo_description) {
|
|
|
|
free(o->clo_description);
|
|
|
|
}
|
2005-03-19 02:40:08 +03:00
|
|
|
if (NULL != o->clo_mca_param_env_var) {
|
|
|
|
free(o->clo_mca_param_env_var);
|
|
|
|
}
|
2004-08-27 20:44:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void param_constructor(cmd_line_param_t *p)
|
|
|
|
{
|
|
|
|
p->clp_arg = NULL;
|
|
|
|
p->clp_option = NULL;
|
|
|
|
p->clp_argc = 0;
|
|
|
|
p->clp_argv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void param_destructor(cmd_line_param_t *p)
|
|
|
|
{
|
|
|
|
if (NULL != p->clp_argv) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_free(p->clp_argv);
|
2004-08-27 20:44:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
static void cmd_line_constructor(opal_cmd_line_t *cmd)
|
2004-08-26 11:56:45 +04:00
|
|
|
{
|
|
|
|
/* Initialize the mutex. Since we're creating (and therefore the
|
|
|
|
only thread that has this instance), there's no need to lock it
|
|
|
|
right now. */
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OBJ_CONSTRUCT(&cmd->lcl_mutex, opal_mutex_t);
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
/* Initialize the lists */
|
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
OBJ_CONSTRUCT(&cmd->lcl_options, opal_list_t);
|
|
|
|
OBJ_CONSTRUCT(&cmd->lcl_params, opal_list_t);
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
/* Initialize the argc/argv pairs */
|
|
|
|
|
|
|
|
cmd->lcl_argc = 0;
|
|
|
|
cmd->lcl_argv = NULL;
|
|
|
|
cmd->lcl_tail_argc = 0;
|
|
|
|
cmd->lcl_tail_argv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
static void cmd_line_destructor(opal_cmd_line_t *cmd)
|
2004-08-26 11:56:45 +04:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t *item;
|
2004-08-26 11:56:45 +04:00
|
|
|
|
|
|
|
/* Free the contents of the options list (do not free the list
|
|
|
|
itself; it was not allocated from the heap) */
|
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
for (item = opal_list_remove_first(&cmd->lcl_options);
|
2004-08-26 11:56:45 +04:00
|
|
|
NULL != item;
|
2005-07-03 20:22:16 +04:00
|
|
|
item = opal_list_remove_first(&cmd->lcl_options)) {
|
2004-08-26 11:56:45 +04:00
|
|
|
OBJ_RELEASE(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free any parsed results */
|
|
|
|
|
|
|
|
free_parse_results(cmd);
|
|
|
|
|
|
|
|
/* Destroy the lists */
|
|
|
|
|
|
|
|
OBJ_DESTRUCT(&cmd->lcl_options);
|
|
|
|
OBJ_DESTRUCT(&cmd->lcl_params);
|
|
|
|
|
|
|
|
/* Destroy the mutex */
|
|
|
|
|
|
|
|
OBJ_DESTRUCT(&cmd->lcl_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
static int make_opt(opal_cmd_line_t *cmd, opal_cmd_line_init_t *e)
|
2005-03-14 23:57:21 +03:00
|
|
|
{
|
|
|
|
cmd_line_option_t *option;
|
|
|
|
|
|
|
|
/* Bozo checks */
|
|
|
|
|
|
|
|
if (NULL == cmd) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
} else if ('\0' == e->ocl_cmd_short_name &&
|
|
|
|
NULL == e->ocl_cmd_single_dash_name &&
|
|
|
|
NULL == e->ocl_cmd_long_name) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
} else if (e->ocl_num_params < 0) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate and fill an option item */
|
|
|
|
|
|
|
|
option = OBJ_NEW(cmd_line_option_t);
|
|
|
|
if (NULL == option) {
|
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
option->clo_short_name = e->ocl_cmd_short_name;
|
|
|
|
if (NULL != e->ocl_cmd_single_dash_name) {
|
|
|
|
option->clo_single_dash_name = strdup(e->ocl_cmd_single_dash_name);
|
|
|
|
}
|
|
|
|
if (NULL != e->ocl_cmd_long_name) {
|
|
|
|
option->clo_long_name = strdup(e->ocl_cmd_long_name);
|
|
|
|
}
|
|
|
|
option->clo_num_params = e->ocl_num_params;
|
|
|
|
if (NULL != e->ocl_description) {
|
|
|
|
option->clo_description = strdup(e->ocl_description);
|
|
|
|
}
|
|
|
|
|
|
|
|
option->clo_type = e->ocl_variable_type;
|
|
|
|
option->clo_variable_dest = e->ocl_variable_dest;
|
|
|
|
if (NULL != e->ocl_mca_type_name) {
|
2005-03-19 02:40:08 +03:00
|
|
|
option->clo_mca_param_env_var =
|
|
|
|
mca_base_param_environ_variable(e->ocl_mca_type_name,
|
|
|
|
e->ocl_mca_component_name,
|
|
|
|
e->ocl_mca_param_name);
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the item, serializing thread access */
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(&cmd->lcl_mutex);
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_append(&cmd->lcl_options, &option->super);
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(&cmd->lcl_mutex);
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
/* All done */
|
|
|
|
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
static void free_parse_results(opal_cmd_line_t *cmd)
|
2004-01-11 00:30:26 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t *item;
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Free the contents of the params list (do not free the list
|
|
|
|
itself; it was not allocated from the heap) */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
for (item = opal_list_remove_first(&cmd->lcl_params);
|
2004-08-26 11:56:45 +04:00
|
|
|
NULL != item;
|
2005-07-03 20:22:16 +04:00
|
|
|
item = opal_list_remove_first(&cmd->lcl_params)) {
|
2004-08-26 11:56:45 +04:00
|
|
|
OBJ_RELEASE(item);
|
|
|
|
}
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Free the argv's */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
if (NULL != cmd->lcl_argv) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_free(cmd->lcl_argv);
|
2004-08-26 11:56:45 +04:00
|
|
|
}
|
|
|
|
cmd->lcl_argv = NULL;
|
|
|
|
cmd->lcl_argc = 0;
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
if (NULL != cmd->lcl_tail_argv) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_free(cmd->lcl_tail_argv);
|
2004-08-26 11:56:45 +04:00
|
|
|
}
|
|
|
|
cmd->lcl_tail_argv = NULL;
|
|
|
|
cmd->lcl_tail_argc = 0;
|
2004-01-11 00:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
/*
|
2004-10-01 22:38:16 +04:00
|
|
|
* Traverse a token and split it into individual letter options (the
|
|
|
|
* token has already been certified to not be a long name and not be a
|
|
|
|
* short name). Ensure to differentiate the resulting options from
|
|
|
|
* "single dash" names.
|
2004-01-21 02:58:23 +03:00
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
static int split_shorts(opal_cmd_line_t *cmd, char *token, char **args,
|
2004-10-01 22:38:16 +04:00
|
|
|
int *output_argc, char ***output_argv,
|
|
|
|
int *num_args_used, bool ignore_unknown)
|
2004-01-11 00:30:26 +03:00
|
|
|
{
|
2004-10-01 22:38:16 +04:00
|
|
|
int i, j, len;
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd_line_option_t *option;
|
2004-10-01 22:38:16 +04:00
|
|
|
char fake_token[3];
|
|
|
|
int num_args;
|
2004-08-26 11:56:45 +04:00
|
|
|
|
2004-10-01 22:38:16 +04:00
|
|
|
/* Setup that we didn't use any of the args */
|
2004-08-26 11:56:45 +04:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
num_args = opal_argv_count(args);
|
2004-10-01 22:38:16 +04:00
|
|
|
*num_args_used = 0;
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2005-05-05 06:02:01 +04:00
|
|
|
/* Traverse the token. If it's empty (e.g., if someone passes a
|
|
|
|
"-" token, which, since the upper level calls this function as
|
|
|
|
(argv[i] + 1), will be empty by the time it gets down here),
|
|
|
|
just return that we didn't find a short option. */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-10-01 22:38:16 +04:00
|
|
|
len = strlen(token);
|
2005-05-05 06:02:01 +04:00
|
|
|
if (0 == len) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
2004-10-01 22:38:16 +04:00
|
|
|
fake_token[0] = '-';
|
|
|
|
fake_token[2] = '\0';
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
fake_token[1] = token[i];
|
|
|
|
option = find_option(cmd, fake_token + 1);
|
2004-08-26 11:56:45 +04:00
|
|
|
|
2004-10-01 22:38:16 +04:00
|
|
|
/* If we don't find the option, either return an error or pass
|
|
|
|
it through unmodified to the new argv */
|
2004-08-26 11:56:45 +04:00
|
|
|
|
2004-10-01 22:38:16 +04:00
|
|
|
if (NULL == option) {
|
2004-08-26 11:56:45 +04:00
|
|
|
if (!ignore_unknown) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
2004-01-21 02:58:23 +03:00
|
|
|
} else {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(output_argc, output_argv, fake_token);
|
2004-01-21 02:58:23 +03:00
|
|
|
}
|
2004-10-01 22:38:16 +04:00
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-10-01 22:38:16 +04:00
|
|
|
/* If we do find the option, copy it and all of its parameters
|
|
|
|
to the output args. If we run out of paramters (i.e., no
|
|
|
|
more tokens in the original argv), that error will be
|
|
|
|
handled at a higher level) */
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-10-01 22:38:16 +04:00
|
|
|
else {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(output_argc, output_argv, fake_token);
|
2004-10-01 22:38:16 +04:00
|
|
|
for (j = 0; j < option->clo_num_params; ++j) {
|
|
|
|
if (*num_args_used < num_args) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(output_argc, output_argv,
|
2004-10-01 22:38:16 +04:00
|
|
|
args[*num_args_used]);
|
|
|
|
++(*num_args_used);
|
|
|
|
} else {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(output_argc, output_argv,
|
2004-10-01 22:38:16 +04:00
|
|
|
special_empty_token);
|
|
|
|
}
|
|
|
|
}
|
2004-08-26 11:56:45 +04:00
|
|
|
}
|
2004-01-22 03:31:52 +03:00
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* All done */
|
2004-01-11 00:30:26 +03:00
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-01-11 00:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
static cmd_line_option_t *find_option(opal_cmd_line_t *cmd,
|
2004-01-21 02:58:23 +03:00
|
|
|
const char *option_name)
|
2004-01-11 00:30:26 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t *item;
|
2004-08-26 11:56:45 +04:00
|
|
|
cmd_line_option_t *option;
|
|
|
|
|
|
|
|
/* Iterate through the list of options hanging off the
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_cmd_line_t and see if we find a match in either the short
|
2004-08-26 11:56:45 +04:00
|
|
|
or long names */
|
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
for (item = opal_list_get_first(&cmd->lcl_options);
|
|
|
|
opal_list_get_end(&cmd->lcl_options) != item;
|
|
|
|
item = opal_list_get_next(item)) {
|
2004-08-26 11:56:45 +04:00
|
|
|
option = (cmd_line_option_t *) item;
|
|
|
|
if ((NULL != option->clo_long_name &&
|
|
|
|
0 == strcmp(option_name, option->clo_long_name)) ||
|
|
|
|
(NULL != option->clo_single_dash_name &&
|
|
|
|
0 == strcmp(option_name, option->clo_single_dash_name)) ||
|
|
|
|
(strlen(option_name) == 1 &&
|
|
|
|
option_name[0] == option->clo_short_name)) {
|
|
|
|
return option;
|
|
|
|
}
|
2004-01-21 02:58:23 +03:00
|
|
|
}
|
|
|
|
|
2004-08-26 11:56:45 +04:00
|
|
|
/* Not found */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
static void set_dest(cmd_line_option_t *option, char *sval)
|
|
|
|
{
|
|
|
|
int ival = atoi(sval);
|
2005-06-25 02:13:29 +04:00
|
|
|
long lval = strtol(sval, NULL, 10);
|
2005-03-19 02:40:08 +03:00
|
|
|
char *str;
|
|
|
|
|
|
|
|
/* Set MCA param. We do this in the environment because the MCA
|
|
|
|
parameter may not have been registered yet -- and if it isn't
|
|
|
|
registered, we don't really want to register a dummy one
|
|
|
|
because we don't know what it's type and default value should
|
|
|
|
be. These are solvable problems (e.g., make a re-registration
|
|
|
|
overwrite everything), but it's far simpler to just leave the
|
|
|
|
registered table alone and set an environment variable with the
|
|
|
|
desired value. The environment variable will get picked up
|
|
|
|
during a nromal parameter lookup, and all will be well. */
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2005-03-19 02:40:08 +03:00
|
|
|
if (NULL != option->clo_mca_param_env_var) {
|
2005-03-14 23:57:21 +03:00
|
|
|
switch(option->clo_type) {
|
2005-07-04 04:13:44 +04:00
|
|
|
case OPAL_CMD_LINE_TYPE_STRING:
|
|
|
|
case OPAL_CMD_LINE_TYPE_INT:
|
|
|
|
case OPAL_CMD_LINE_TYPE_SIZE_T:
|
2005-03-19 02:40:08 +03:00
|
|
|
asprintf(&str, "%s=%s", option->clo_mca_param_env_var, sval);
|
2005-03-14 23:57:21 +03:00
|
|
|
break;
|
2005-07-04 04:13:44 +04:00
|
|
|
case OPAL_CMD_LINE_TYPE_BOOL:
|
2005-03-19 02:40:08 +03:00
|
|
|
asprintf(&str, "%s=1", option->clo_mca_param_env_var);
|
2005-03-14 23:57:21 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2005-03-19 02:40:08 +03:00
|
|
|
if (NULL != str) {
|
|
|
|
putenv(str);
|
|
|
|
}
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set variable */
|
|
|
|
|
|
|
|
if (NULL != option->clo_variable_dest) {
|
|
|
|
switch(option->clo_type) {
|
2005-07-04 04:13:44 +04:00
|
|
|
case OPAL_CMD_LINE_TYPE_STRING:
|
2005-03-14 23:57:21 +03:00
|
|
|
*((char**) option->clo_variable_dest) = strdup(sval);
|
|
|
|
break;
|
2005-07-04 04:13:44 +04:00
|
|
|
case OPAL_CMD_LINE_TYPE_INT:
|
2005-03-14 23:57:21 +03:00
|
|
|
*((int*) option->clo_variable_dest) = ival;
|
|
|
|
break;
|
2005-07-04 04:13:44 +04:00
|
|
|
case OPAL_CMD_LINE_TYPE_SIZE_T:
|
2005-06-25 02:13:29 +04:00
|
|
|
*((size_t*) option->clo_variable_dest) = lval;
|
|
|
|
break;
|
2005-07-04 04:13:44 +04:00
|
|
|
case OPAL_CMD_LINE_TYPE_BOOL:
|
2005-04-14 00:42:55 +04:00
|
|
|
*((bool*) option->clo_variable_dest) = 1;
|
2005-03-14 23:57:21 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|