2013-03-28 01:09:41 +04:00
|
|
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2004-2008 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2012 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2015-06-24 06:59:57 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
2013-03-28 01:09:41 +04:00
|
|
|
* University of Stuttgart. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2013-05-18 01:45:49 +04:00
|
|
|
* Copyright (c) 2008-2013 Cisco Systems, Inc. All rights reserved.
|
2015-07-28 01:59:58 +03:00
|
|
|
* Copyright (c) 2012-2015 Los Alamos National Security, LLC. All rights
|
2013-03-28 01:09:41 +04:00
|
|
|
* reserved.
|
|
|
|
* $COPYRIGHT$
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2013-03-28 01:09:41 +04:00
|
|
|
* Additional copyrights may follow
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2013-03-28 01:09:41 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opal_config.h"
|
|
|
|
|
|
|
|
#include "opal/mca/base/mca_base_var_enum.h"
|
2015-07-28 01:59:58 +03:00
|
|
|
#include "opal/mca/base/base.h"
|
2016-03-22 00:20:56 +03:00
|
|
|
#include "opal/util/argv.h"
|
2013-03-28 01:09:41 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
static void mca_base_var_enum_constructor (mca_base_var_enum_t *enumerator);
|
|
|
|
static void mca_base_var_enum_destructor (mca_base_var_enum_t *enumerator);
|
2013-07-25 19:42:01 +04:00
|
|
|
OBJ_CLASS_INSTANCE(mca_base_var_enum_t, opal_object_t, mca_base_var_enum_constructor,
|
2013-03-28 01:09:41 +04:00
|
|
|
mca_base_var_enum_destructor);
|
|
|
|
|
2016-03-22 00:20:56 +03:00
|
|
|
static void mca_base_var_enum_flag_constructor (mca_base_var_enum_flag_t *enumerator);
|
|
|
|
static void mca_base_var_enum_flag_destructor (mca_base_var_enum_flag_t *enumerator);
|
|
|
|
OBJ_CLASS_INSTANCE(mca_base_var_enum_flag_t, opal_object_t, mca_base_var_enum_flag_constructor,
|
|
|
|
mca_base_var_enum_flag_destructor);
|
|
|
|
|
2015-07-28 01:59:58 +03:00
|
|
|
static int enum_dump (mca_base_var_enum_t *self, char **out);
|
|
|
|
static int enum_get_count (mca_base_var_enum_t *self, int *count);
|
|
|
|
static int enum_get_value (mca_base_var_enum_t *self, int index, int *value, const char **string_value);
|
|
|
|
|
2013-07-25 19:42:01 +04:00
|
|
|
static int mca_base_var_enum_bool_get_count (mca_base_var_enum_t *enumerator, int *count)
|
|
|
|
{
|
|
|
|
*count = 2;
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mca_base_var_enum_bool_get_value (mca_base_var_enum_t *self, int index,
|
|
|
|
int *value, const char **string_value)
|
|
|
|
{
|
|
|
|
if (1 < index) {
|
|
|
|
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = index ? 1 : 0;
|
|
|
|
*string_value = index ? "true" : "false";
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mca_base_var_enum_bool_vfs (mca_base_var_enum_t *self, const char *string_value,
|
|
|
|
int *value)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
int v;
|
|
|
|
|
2015-05-29 17:41:51 +03:00
|
|
|
/* skip whitespace */
|
|
|
|
string_value += strspn (string_value, " \t\n\v\f\r");
|
|
|
|
|
2013-07-25 19:42:01 +04:00
|
|
|
v = strtol (string_value, &tmp, 10);
|
|
|
|
if (*tmp != '\0') {
|
|
|
|
if (0 == strcmp (string_value, "true") || 0 == strcmp (string_value, "t") ||
|
2015-05-29 17:41:51 +03:00
|
|
|
0 == strcmp (string_value, "enabled") || 0 == strcmp (string_value, "yes")) {
|
2013-07-25 19:42:01 +04:00
|
|
|
v = 1;
|
|
|
|
} else if (0 == strcmp (string_value, "false") || 0 == strcmp (string_value, "f") ||
|
2015-05-29 17:41:51 +03:00
|
|
|
0 == strcmp (string_value, "disabled") || 0 == strcmp (string_value, "no")) {
|
2013-07-25 19:42:01 +04:00
|
|
|
v = 0;
|
|
|
|
} else {
|
|
|
|
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = !!v;
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mca_base_var_enum_bool_sfv (mca_base_var_enum_t *self, const int value,
|
2016-03-22 00:20:56 +03:00
|
|
|
char **string_value)
|
2013-07-25 19:42:01 +04:00
|
|
|
{
|
2014-03-17 18:58:30 +04:00
|
|
|
if (string_value) {
|
2016-03-22 00:20:56 +03:00
|
|
|
*string_value = strdup (value ? "true" : "false");
|
2014-03-17 18:58:30 +04:00
|
|
|
}
|
2013-07-25 19:42:01 +04:00
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mca_base_var_enum_bool_dump (mca_base_var_enum_t *self, char **out)
|
|
|
|
{
|
2015-05-29 17:41:51 +03:00
|
|
|
*out = strdup ("0: f|false|disabled|no, 1: t|true|enabled|yes");
|
2013-07-25 19:42:01 +04:00
|
|
|
return *out ? OPAL_SUCCESS : OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mca_base_var_enum_t mca_base_var_enum_bool = {
|
|
|
|
.super = OPAL_OBJ_STATIC_INIT(opal_object_t),
|
2015-07-28 01:59:58 +03:00
|
|
|
.enum_is_static = true,
|
2013-07-25 19:42:01 +04:00
|
|
|
.enum_name = "boolean",
|
|
|
|
.get_count = mca_base_var_enum_bool_get_count,
|
|
|
|
.get_value = mca_base_var_enum_bool_get_value,
|
|
|
|
.value_from_string = mca_base_var_enum_bool_vfs,
|
|
|
|
.string_from_value = mca_base_var_enum_bool_sfv,
|
|
|
|
.dump = mca_base_var_enum_bool_dump
|
|
|
|
};
|
|
|
|
|
2015-07-28 01:59:58 +03:00
|
|
|
/* verbosity enumerator */
|
|
|
|
static mca_base_var_enum_value_t verbose_values[] = {
|
|
|
|
{MCA_BASE_VERBOSE_NONE, "none"},
|
|
|
|
{MCA_BASE_VERBOSE_ERROR, "error"},
|
|
|
|
{MCA_BASE_VERBOSE_COMPONENT, "component"},
|
|
|
|
{MCA_BASE_VERBOSE_WARN, "warn"},
|
|
|
|
{MCA_BASE_VERBOSE_INFO, "info"},
|
|
|
|
{MCA_BASE_VERBOSE_TRACE, "trace"},
|
|
|
|
{MCA_BASE_VERBOSE_DEBUG, "debug"},
|
|
|
|
{MCA_BASE_VERBOSE_MAX, "max"},
|
|
|
|
{-1, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int mca_base_var_enum_verbose_vfs (mca_base_var_enum_t *self, const char *string_value,
|
|
|
|
int *value)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
int v;
|
|
|
|
|
|
|
|
/* skip whitespace */
|
|
|
|
string_value += strspn (string_value, " \t\n\v\f\r");
|
|
|
|
|
|
|
|
v = strtol (string_value, &tmp, 10);
|
|
|
|
if (*tmp != '\0') {
|
|
|
|
for (int i = 0 ; verbose_values[i].string ; ++i) {
|
|
|
|
if (0 == strcmp (verbose_values[i].string, string_value)) {
|
|
|
|
*value = verbose_values[i].value;
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_ERR_NOT_FOUND;
|
|
|
|
} else if (v < MCA_BASE_VERBOSE_NONE) {
|
|
|
|
v = MCA_BASE_VERBOSE_NONE;
|
|
|
|
} else if (v > MCA_BASE_VERBOSE_MAX) {
|
|
|
|
v = MCA_BASE_VERBOSE_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = v;
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mca_base_var_enum_verbose_sfv (mca_base_var_enum_t *self, const int value,
|
2016-03-22 00:20:56 +03:00
|
|
|
char **string_value)
|
2015-07-28 01:59:58 +03:00
|
|
|
{
|
2016-03-22 00:20:56 +03:00
|
|
|
int ret;
|
2015-07-28 01:59:58 +03:00
|
|
|
|
|
|
|
if (value < 0 || value > 100) {
|
|
|
|
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0 ; verbose_values[i].string ; ++i) {
|
|
|
|
if (verbose_values[i].value == value) {
|
2016-03-22 00:20:56 +03:00
|
|
|
*string_value = strdup (verbose_values[i].string);
|
2015-07-28 01:59:58 +03:00
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string_value) {
|
2016-03-22 00:20:56 +03:00
|
|
|
ret = asprintf (string_value, "%d", value);
|
|
|
|
if (0 > ret) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
2015-07-28 01:59:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mca_base_var_enum_verbose_dump (mca_base_var_enum_t *self, char **out)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = enum_dump (self, out);
|
|
|
|
if (OPAL_SUCCESS != ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = asprintf (&tmp, "%s, 0 - 100", *out);
|
|
|
|
free (*out);
|
|
|
|
if (0 > ret) {
|
|
|
|
*out = NULL;
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = tmp;
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
mca_base_var_enum_t mca_base_var_enum_verbose = {
|
|
|
|
.super = OPAL_OBJ_STATIC_INIT(opal_object_t),
|
|
|
|
.enum_is_static = true,
|
|
|
|
.enum_name = "verbosity",
|
|
|
|
.get_count = enum_get_count,
|
|
|
|
.get_value = enum_get_value,
|
|
|
|
.value_from_string = mca_base_var_enum_verbose_vfs,
|
|
|
|
.string_from_value = mca_base_var_enum_verbose_sfv,
|
|
|
|
.dump = mca_base_var_enum_verbose_dump,
|
|
|
|
.enum_value_count = 8,
|
|
|
|
.enum_values = verbose_values,
|
|
|
|
};
|
|
|
|
|
2013-07-25 19:42:01 +04:00
|
|
|
|
2013-10-24 23:27:06 +04:00
|
|
|
int mca_base_var_enum_create (const char *name, const mca_base_var_enum_value_t *values, mca_base_var_enum_t **enumerator)
|
2013-03-28 01:09:41 +04:00
|
|
|
{
|
|
|
|
mca_base_var_enum_t *new_enum;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*enumerator = NULL;
|
|
|
|
|
|
|
|
new_enum = OBJ_NEW(mca_base_var_enum_t);
|
|
|
|
if (NULL == new_enum) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_enum->enum_name = strdup (name);
|
|
|
|
if (NULL == new_enum->enum_name) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
2013-10-24 23:27:06 +04:00
|
|
|
for (i = 0 ; values[i].string ; ++i);
|
2013-03-28 01:09:41 +04:00
|
|
|
new_enum->enum_value_count = i;
|
|
|
|
|
2013-10-24 23:27:06 +04:00
|
|
|
/* make a copy of the values */
|
|
|
|
new_enum->enum_values = calloc (new_enum->enum_value_count + 1, sizeof (*new_enum->enum_values));
|
|
|
|
if (NULL == new_enum->enum_values) {
|
2015-04-16 21:07:17 +03:00
|
|
|
OBJ_RELEASE(new_enum);
|
2013-10-24 23:27:06 +04:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0 ; i < new_enum->enum_value_count ; ++i) {
|
|
|
|
new_enum->enum_values[i].value = values[i].value;
|
|
|
|
new_enum->enum_values[i].string = strdup (values[i].string);
|
|
|
|
}
|
|
|
|
|
2013-03-28 01:09:41 +04:00
|
|
|
*enumerator = new_enum;
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-03-22 00:20:56 +03:00
|
|
|
int mca_base_var_enum_create_flag (const char *name, const mca_base_var_enum_value_flag_t *flags, mca_base_var_enum_flag_t **enumerator)
|
|
|
|
{
|
|
|
|
mca_base_var_enum_flag_t *new_enum;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*enumerator = NULL;
|
|
|
|
|
|
|
|
new_enum = OBJ_NEW(mca_base_var_enum_flag_t);
|
|
|
|
if (NULL == new_enum) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_enum->super.enum_name = strdup (name);
|
|
|
|
if (NULL == new_enum->super.enum_name) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0 ; flags[i].string ; ++i);
|
|
|
|
new_enum->super.enum_value_count = i;
|
|
|
|
|
|
|
|
/* make a copy of the values */
|
|
|
|
new_enum->enum_flags = calloc (new_enum->super.enum_value_count + 1, sizeof (*new_enum->enum_flags));
|
|
|
|
if (NULL == new_enum->enum_flags) {
|
|
|
|
OBJ_RELEASE(new_enum);
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int all_flags = 0;
|
|
|
|
for (i = 0 ; i < new_enum->super.enum_value_count ; ++i) {
|
|
|
|
new_enum->enum_flags[i].flag = flags[i].flag;
|
|
|
|
new_enum->enum_flags[i].string = strdup (flags[i].string);
|
|
|
|
new_enum->enum_flags[i].conflicting_flag = flags[i].conflicting_flag;
|
|
|
|
/* ensure flags are only set a single bit, doesn't conflict with itself, and
|
|
|
|
* hasn't already been specified. */
|
|
|
|
assert (!(flags[i].flag & (flags[i].flag - 1)));
|
|
|
|
assert (!(flags[i].flag & flags[i].conflicting_flag));
|
|
|
|
assert (!(all_flags & flags[i].flag));
|
|
|
|
assert (flags[i].flag);
|
|
|
|
all_flags |= flags[i].flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
*enumerator = new_enum;
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-03-28 01:09:41 +04:00
|
|
|
static int enum_dump (mca_base_var_enum_t *self, char **out)
|
|
|
|
{
|
2013-10-26 23:41:35 +04:00
|
|
|
int i;
|
2013-03-28 01:09:41 +04:00
|
|
|
char *tmp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*out = NULL;
|
|
|
|
|
|
|
|
if (NULL == self) {
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = NULL;
|
2013-10-24 23:27:06 +04:00
|
|
|
for (i = 0; i < self->enum_value_count && self->enum_values[i].string ; ++i) {
|
2013-03-28 01:09:41 +04:00
|
|
|
ret = asprintf (out, "%s%s%d:\"%s\"", tmp ? tmp : "", tmp ? ", " : "", self->enum_values[i].value,
|
|
|
|
self->enum_values[i].string);
|
|
|
|
if (tmp) free (tmp);
|
|
|
|
if (0 > ret) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
tmp = *out;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int enum_get_count (mca_base_var_enum_t *self, int *count)
|
|
|
|
{
|
|
|
|
*count = self->enum_value_count;
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int enum_get_value (mca_base_var_enum_t *self, int index, int *value, const char **string_value)
|
|
|
|
{
|
|
|
|
int count, ret;
|
|
|
|
|
|
|
|
ret = self->get_count(self, &count);
|
|
|
|
if (OPAL_SUCCESS != ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= count) {
|
|
|
|
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
*value = self->enum_values[index].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string_value) {
|
2016-03-22 00:20:56 +03:00
|
|
|
*string_value = strdup (self->enum_values[index].string);
|
2013-03-28 01:09:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int enum_value_from_string(mca_base_var_enum_t *self, const char *string_value, int *value_out) {
|
|
|
|
int value, count, ret, i;
|
|
|
|
bool is_int;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
ret = self->get_count(self, &count);
|
|
|
|
if (OPAL_SUCCESS != ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = strtol(string_value, &tmp, 0);
|
|
|
|
|
|
|
|
/* Check if the string is an integer */
|
|
|
|
is_int = tmp[0] == '\0';
|
|
|
|
|
|
|
|
for (i = 0 ; i < count ; ++i) {
|
|
|
|
if ((is_int && value == self->enum_values[i].value) ||
|
|
|
|
0 == strcasecmp (string_value, self->enum_values[i].string)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == count) {
|
|
|
|
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value_out = self->enum_values[i].value;
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-03-22 00:20:56 +03:00
|
|
|
static int enum_string_from_value(mca_base_var_enum_t *self, const int value, char **string_value) {
|
2013-03-28 01:09:41 +04:00
|
|
|
int count, ret, i;
|
|
|
|
|
|
|
|
ret = self->get_count(self, &count);
|
|
|
|
if (OPAL_SUCCESS != ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0 ; i < count ; ++i) {
|
|
|
|
if (value == self->enum_values[i].value) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == count) {
|
|
|
|
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string_value) {
|
2016-03-22 00:20:56 +03:00
|
|
|
*string_value = strdup (self->enum_values[i].string);
|
2013-03-28 01:09:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mca_base_var_enum_constructor (mca_base_var_enum_t *enumerator)
|
|
|
|
{
|
|
|
|
memset ((char *) enumerator + sizeof (enumerator->super), 0 , sizeof(*enumerator) - sizeof(enumerator->super));
|
|
|
|
|
|
|
|
enumerator->get_value = enum_get_value;
|
|
|
|
enumerator->get_count = enum_get_count;
|
|
|
|
enumerator->value_from_string = enum_value_from_string;
|
|
|
|
enumerator->string_from_value = enum_string_from_value;
|
|
|
|
enumerator->dump = enum_dump;
|
2015-07-28 01:59:58 +03:00
|
|
|
enumerator->enum_is_static = false;
|
2013-03-28 01:09:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mca_base_var_enum_destructor (mca_base_var_enum_t *enumerator)
|
|
|
|
{
|
2013-07-25 19:42:01 +04:00
|
|
|
if (enumerator->enum_name) {
|
|
|
|
free (enumerator->enum_name);
|
|
|
|
}
|
2013-10-24 23:27:06 +04:00
|
|
|
|
|
|
|
/* release the copy of the values */
|
|
|
|
if (enumerator->enum_values) {
|
|
|
|
for (int i = 0 ; i < enumerator->enum_value_count ; ++i) {
|
|
|
|
free ((void *) enumerator->enum_values[i].string);
|
|
|
|
}
|
|
|
|
free (enumerator->enum_values);
|
|
|
|
}
|
2013-03-28 01:09:41 +04:00
|
|
|
}
|
2016-03-22 00:20:56 +03:00
|
|
|
|
|
|
|
static int enum_get_value_flag (mca_base_var_enum_t *self, int index, int *value, const char **string_value)
|
|
|
|
{
|
|
|
|
mca_base_var_enum_flag_t *flag_enum = (mca_base_var_enum_flag_t *) self;
|
|
|
|
int count, ret;
|
|
|
|
|
|
|
|
ret = self->get_count(self, &count);
|
|
|
|
if (OPAL_SUCCESS != ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= count) {
|
|
|
|
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
*value = flag_enum->enum_flags[index].flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string_value) {
|
|
|
|
*string_value = strdup (flag_enum->enum_flags[index].string);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int enum_value_from_string_flag (mca_base_var_enum_t *self, const char *string_value, int *value_out) {
|
|
|
|
mca_base_var_enum_flag_t *flag_enum = (mca_base_var_enum_flag_t *) self;
|
|
|
|
int value, count, ret, flag;
|
|
|
|
char **flags;
|
|
|
|
bool is_int;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
ret = self->get_count(self, &count);
|
|
|
|
if (OPAL_SUCCESS != ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = opal_argv_split (string_value, ',');
|
|
|
|
if (NULL == flags) {
|
|
|
|
return OPAL_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
flag = 0;
|
|
|
|
|
|
|
|
for (int i = 0 ; flags[i] ; ++i) {
|
|
|
|
value = strtol (flags[i], &tmp, 0);
|
|
|
|
is_int = tmp[0] == '\0';
|
|
|
|
|
|
|
|
bool found = false, conflict = false;
|
|
|
|
for (int j = 0 ; j < count ; ++j) {
|
|
|
|
if ((is_int && (value == flag_enum->enum_flags[i].flag)) ||
|
|
|
|
0 == strcasecmp (flags[i], flag_enum->enum_flags[i].string)) {
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
if (flag & flag_enum->enum_flags[i].conflicting_flag) {
|
|
|
|
conflict = true;
|
|
|
|
} else {
|
|
|
|
flag |= flag_enum->enum_flags[i].flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found || conflict) {
|
|
|
|
opal_argv_free (flags);
|
|
|
|
return !found ? OPAL_ERR_VALUE_OUT_OF_BOUNDS : OPAL_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 21:49:37 +03:00
|
|
|
opal_argv_free (flags);
|
|
|
|
|
2016-03-22 00:20:56 +03:00
|
|
|
*value_out = flag;
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int enum_string_from_value_flag (mca_base_var_enum_t *self, const int value, char **string_value) {
|
|
|
|
mca_base_var_enum_flag_t *flag_enum = (mca_base_var_enum_flag_t *) self;
|
|
|
|
int count, ret, current;
|
|
|
|
char *out = NULL, *tmp;
|
|
|
|
|
|
|
|
ret = self->get_count(self, &count);
|
|
|
|
if (OPAL_SUCCESS != ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = value;
|
|
|
|
for (int i = 0 ; i < count ; ++i) {
|
|
|
|
if (!(flag_enum->enum_flags[i].flag & current)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = out;
|
|
|
|
|
|
|
|
ret = asprintf (&out, "%s%s%s", tmp ? tmp : "", tmp ? "," : "", flag_enum->enum_flags[i].string);
|
|
|
|
free (tmp);
|
|
|
|
|
|
|
|
if (0 > ret) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value & flag_enum->enum_flags[i].conflicting_flag) {
|
|
|
|
free (out);
|
|
|
|
return OPAL_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
current &= ~flag_enum->enum_flags[i].flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current) {
|
|
|
|
free (out);
|
|
|
|
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string_value) {
|
|
|
|
*string_value = out ? out : strdup ("");
|
|
|
|
} else {
|
|
|
|
free (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int enum_dump_flag (mca_base_var_enum_t *self, char **out)
|
|
|
|
{
|
|
|
|
mca_base_var_enum_flag_t *flag_enum = (mca_base_var_enum_flag_t *) self;
|
|
|
|
char *tmp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*out = NULL;
|
|
|
|
|
|
|
|
if (NULL == self) {
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = strdup ("Comma-delimited list of: ");
|
|
|
|
if (NULL == *out) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < self->enum_value_count ; ++i) {
|
|
|
|
tmp = *out;
|
|
|
|
|
|
|
|
ret = asprintf (out, "%s%s0x%x:\"%s\"", tmp, i ? ", " : " ", flag_enum->enum_flags[i].flag,
|
|
|
|
flag_enum->enum_flags[i].string);
|
|
|
|
free (tmp);
|
|
|
|
if (0 > ret) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mca_base_var_enum_flag_constructor (mca_base_var_enum_flag_t *enumerator)
|
|
|
|
{
|
|
|
|
enumerator->enum_flags = NULL;
|
|
|
|
enumerator->super.get_value = enum_get_value_flag;
|
|
|
|
enumerator->super.get_count = enum_get_count;
|
|
|
|
enumerator->super.value_from_string = enum_value_from_string_flag;
|
|
|
|
enumerator->super.string_from_value = enum_string_from_value_flag;
|
|
|
|
enumerator->super.dump = enum_dump_flag;
|
|
|
|
enumerator->super.enum_is_static = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mca_base_var_enum_flag_destructor (mca_base_var_enum_flag_t *enumerator)
|
|
|
|
{
|
|
|
|
/* release the copy of the values */
|
|
|
|
if (enumerator->enum_flags) {
|
|
|
|
for (int i = 0 ; i < enumerator->super.enum_value_count ; ++i) {
|
|
|
|
free ((void *) enumerator->enum_flags[i].string);
|
|
|
|
}
|
|
|
|
free (enumerator->enum_flags);
|
|
|
|
}
|
|
|
|
}
|