2004-01-07 11:03:19 +03:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. 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.
|
2007-08-01 16:08:11 +04:00
|
|
|
* Copyright (c) 2007 Voltaire. All rights reserved.
|
|
|
|
*
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-01-07 11:03:19 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2005-12-13 01:01:51 +03:00
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-01-07 11:03:19 +03:00
|
|
|
#include <stdlib.h>
|
2005-12-13 01:01:51 +03:00
|
|
|
#endif /* HAVE_STDLIB_H */
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-01-07 11:03:19 +03:00
|
|
|
#include <string.h>
|
2005-12-13 01:01:51 +03:00
|
|
|
#endif /* HAVE_STRING_H */
|
2004-01-07 11:03:19 +03:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
#include "opal/util/argv.h"
|
2005-07-04 06:38:44 +04:00
|
|
|
#include "opal/util/strncpy.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
2004-01-07 11:35:06 +03:00
|
|
|
|
2004-01-07 18:21:25 +03:00
|
|
|
#define ARGSIZE 128
|
2004-01-07 11:03:19 +03:00
|
|
|
|
2004-05-20 16:39:27 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Append a string to the end of a new or existing argv array.
|
2004-01-07 11:03:19 +03:00
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_argv_append(int *argc, char ***argv, const char *arg)
|
2004-01-07 11:03:19 +03:00
|
|
|
{
|
2005-05-01 04:47:35 +04:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* add the new element */
|
2006-02-12 04:33:29 +03:00
|
|
|
if (OPAL_SUCCESS != (rc = opal_argv_append_nosize(argv, arg))) {
|
2005-05-01 04:47:35 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
*argc = opal_argv_count(*argv);
|
2005-05-01 04:47:35 +04:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2005-05-01 04:47:35 +04:00
|
|
|
}
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_argv_append_nosize(char ***argv, const char *arg)
|
2005-05-01 04:47:35 +04:00
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
|
2004-01-07 11:03:19 +03:00
|
|
|
/* Create new argv. */
|
|
|
|
|
|
|
|
if (NULL == *argv) {
|
2005-05-01 04:47:35 +04:00
|
|
|
*argv = (char**) malloc(2 * sizeof(char *));
|
2005-09-03 04:06:20 +04:00
|
|
|
if (NULL == *argv) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2005-09-03 04:06:20 +04:00
|
|
|
}
|
2005-05-01 04:47:35 +04:00
|
|
|
argc = 0;
|
2004-01-07 11:03:19 +03:00
|
|
|
(*argv)[0] = NULL;
|
|
|
|
(*argv)[1] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend existing argv. */
|
|
|
|
else {
|
2005-05-01 04:47:35 +04:00
|
|
|
/* count how many entries currently exist */
|
2005-07-04 04:13:44 +04:00
|
|
|
argc = opal_argv_count(*argv);
|
2005-05-01 04:47:35 +04:00
|
|
|
|
|
|
|
*argv = (char**) realloc(*argv, (argc + 2) * sizeof(char *));
|
2005-09-03 04:06:20 +04:00
|
|
|
if (NULL == *argv) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2005-09-03 04:06:20 +04:00
|
|
|
}
|
2005-05-01 04:47:35 +04:00
|
|
|
}
|
2004-01-07 11:03:19 +03:00
|
|
|
|
2005-05-01 04:47:35 +04:00
|
|
|
/* Set the newest element to point to a copy of the arg string */
|
2004-01-07 11:03:19 +03:00
|
|
|
|
2005-09-03 04:06:20 +04:00
|
|
|
(*argv)[argc] = strdup(arg);
|
|
|
|
if (NULL == (*argv)[argc]) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2005-09-03 04:06:20 +04:00
|
|
|
}
|
2004-01-07 11:03:19 +03:00
|
|
|
|
2005-05-01 04:47:35 +04:00
|
|
|
argc = argc + 1;
|
|
|
|
(*argv)[argc] = NULL;
|
2004-01-07 11:03:19 +03:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-01-07 11:03:19 +03:00
|
|
|
}
|
|
|
|
|
2009-01-15 21:11:50 +03:00
|
|
|
int opal_argv_append_unique_nosize(char ***argv, const char *arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* if the provided array is NULL, then the arg cannot be present,
|
|
|
|
* so just go ahead and append
|
|
|
|
*/
|
|
|
|
if (NULL == *argv) {
|
|
|
|
return opal_argv_append_nosize(argv, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see if this arg is already present in the array */
|
|
|
|
for (i=0; NULL != (*argv)[i]; i++) {
|
|
|
|
if (0 == strcmp(arg, (*argv)[i])) {
|
|
|
|
/* already exists - nothing to do */
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we get here if the arg is not in the array - so add it */
|
|
|
|
return opal_argv_append_nosize(argv, arg);
|
|
|
|
}
|
|
|
|
|
2004-05-20 16:39:27 +04:00
|
|
|
/*
|
2004-01-07 11:03:19 +03:00
|
|
|
* Free a NULL-terminated argv array.
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
void opal_argv_free(char **argv)
|
2004-01-07 11:03:19 +03:00
|
|
|
{
|
|
|
|
char **p;
|
|
|
|
|
|
|
|
if (NULL == argv)
|
|
|
|
return;
|
|
|
|
|
2004-01-13 05:58:37 +03:00
|
|
|
for (p = argv; NULL != *p; ++p) {
|
2004-02-10 03:09:36 +03:00
|
|
|
free(*p);
|
2004-01-07 11:03:19 +03:00
|
|
|
}
|
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
free(argv);
|
2004-01-07 11:03:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-20 16:39:27 +04:00
|
|
|
/*
|
2004-01-07 11:03:19 +03:00
|
|
|
* Split a string into a NULL-terminated argv array.
|
|
|
|
*/
|
2007-08-01 16:08:11 +04:00
|
|
|
static char **opal_argv_split_inter(const char *src_string, int delimiter,
|
|
|
|
int include_empty)
|
2004-01-07 11:03:19 +03:00
|
|
|
{
|
|
|
|
char arg[ARGSIZE];
|
2004-01-16 04:33:12 +03:00
|
|
|
char **argv = NULL;
|
|
|
|
const char *p;
|
2004-01-07 11:03:19 +03:00
|
|
|
char *argtemp;
|
|
|
|
int argc = 0;
|
|
|
|
size_t arglen;
|
|
|
|
|
2004-08-17 20:10:42 +04:00
|
|
|
while (src_string && *src_string) {
|
2004-01-07 11:03:19 +03:00
|
|
|
p = src_string;
|
|
|
|
arglen = 0;
|
|
|
|
|
|
|
|
while (('\0' != *p) && (*p != delimiter)) {
|
|
|
|
++p;
|
|
|
|
++arglen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* zero length argument, skip */
|
|
|
|
|
|
|
|
if (src_string == p) {
|
2007-08-01 16:08:11 +04:00
|
|
|
if (include_empty) {
|
|
|
|
arg[0] = '\0';
|
|
|
|
if (OPAL_ERROR == opal_argv_append(&argc, &argv, arg))
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-01-07 11:03:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* tail argument, add straight from the original string */
|
|
|
|
|
|
|
|
else if ('\0' == *p) {
|
2006-02-12 04:33:29 +03:00
|
|
|
if (OPAL_ERROR == opal_argv_append(&argc, &argv, src_string))
|
2004-01-07 11:03:19 +03:00
|
|
|
return NULL;
|
2007-08-01 16:08:11 +04:00
|
|
|
src_string = p;
|
|
|
|
continue;
|
2004-01-07 11:03:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* long argument, malloc buffer, copy and add */
|
|
|
|
|
|
|
|
else if (arglen > (ARGSIZE - 1)) {
|
2004-10-18 19:38:19 +04:00
|
|
|
argtemp = (char*) malloc(arglen + 1);
|
2004-01-07 11:03:19 +03:00
|
|
|
if (NULL == argtemp)
|
|
|
|
return NULL;
|
|
|
|
|
2004-01-19 20:48:41 +03:00
|
|
|
strncpy(argtemp, src_string, arglen);
|
2004-01-07 11:03:19 +03:00
|
|
|
argtemp[arglen] = '\0';
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
if (OPAL_ERROR == opal_argv_append(&argc, &argv, argtemp)) {
|
2004-02-10 03:09:36 +03:00
|
|
|
free(argtemp);
|
2004-01-07 11:03:19 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-02-10 03:09:36 +03:00
|
|
|
free(argtemp);
|
2004-01-07 11:03:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* short argument, copy to buffer and add */
|
|
|
|
|
|
|
|
else {
|
2004-01-19 20:48:41 +03:00
|
|
|
strncpy(arg, src_string, arglen);
|
2004-01-07 11:03:19 +03:00
|
|
|
arg[arglen] = '\0';
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
if (OPAL_ERROR == opal_argv_append(&argc, &argv, arg))
|
2004-01-07 11:03:19 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-08-01 16:08:11 +04:00
|
|
|
src_string = p + 1;
|
2004-01-07 11:03:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* All done */
|
|
|
|
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
2007-08-01 16:08:11 +04:00
|
|
|
char **opal_argv_split(const char *src_string, int delimiter)
|
|
|
|
{
|
|
|
|
return opal_argv_split_inter(src_string, delimiter, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char **opal_argv_split_with_empty(const char *src_string, int delimiter)
|
|
|
|
{
|
|
|
|
return opal_argv_split_inter(src_string, delimiter, 1);
|
|
|
|
}
|
2004-01-07 11:03:19 +03:00
|
|
|
|
2004-05-20 16:39:27 +04:00
|
|
|
/*
|
2004-01-07 11:03:19 +03:00
|
|
|
* Return the length of a NULL-terminated argv array.
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_argv_count(char **argv)
|
2004-01-07 11:03:19 +03:00
|
|
|
{
|
|
|
|
char **p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (NULL == argv)
|
2004-01-13 05:58:37 +03:00
|
|
|
return 0;
|
2004-01-07 11:03:19 +03:00
|
|
|
|
|
|
|
for (i = 0, p = argv; *p; i++, p++)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-20 16:39:27 +04:00
|
|
|
/*
|
2004-01-07 11:03:19 +03:00
|
|
|
* Join all the elements of an argv array into a single
|
|
|
|
* newly-allocated string.
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
char *opal_argv_join(char **argv, int delimiter)
|
2004-01-07 11:03:19 +03:00
|
|
|
{
|
|
|
|
char **p;
|
|
|
|
char *pp;
|
|
|
|
char *str;
|
2004-01-07 11:22:37 +03:00
|
|
|
size_t str_len = 0;
|
|
|
|
size_t i;
|
2004-01-07 11:03:19 +03:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
/* Bozo case */
|
|
|
|
|
|
|
|
if (NULL == argv || NULL == argv[0]) {
|
|
|
|
return strdup("");
|
|
|
|
}
|
|
|
|
|
2004-01-07 11:03:19 +03:00
|
|
|
/* Find the total string length in argv including delimiters. The
|
|
|
|
last delimiter is replaced by the NULL character. */
|
|
|
|
|
|
|
|
for (p = argv; *p; ++p) {
|
|
|
|
str_len += strlen(*p) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the string. */
|
|
|
|
|
2004-10-18 19:38:19 +04:00
|
|
|
if (NULL == (str = (char*) malloc(str_len)))
|
2004-01-07 11:03:19 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Loop filling in the string. */
|
|
|
|
|
|
|
|
str[--str_len] = '\0';
|
|
|
|
p = argv;
|
|
|
|
pp = *p;
|
|
|
|
|
|
|
|
for (i = 0; i < str_len; ++i) {
|
|
|
|
if ('\0' == *pp) {
|
|
|
|
|
|
|
|
/* End of a string, fill in a delimiter and go to the next
|
|
|
|
string. */
|
|
|
|
|
|
|
|
str[i] = (char) delimiter;
|
|
|
|
++p;
|
|
|
|
pp = *p;
|
|
|
|
} else {
|
|
|
|
str[i] = *pp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All done */
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Per the July technical meeting:
Standardize the handling of the orte launch agent option across PLMs. This has been a consistent complaint I have received - each PLM would register its own MCA param to get input on the launch agent for remote nodes (in fact, one or two didn't, but most did). This would then get handled in various and contradictory ways.
Some PLMs would accept only a one-word input. Others accepted multi-word args such as "valgrind orted", but then some would error by putting any prefix specified on the cmd line in front of the incorrect argument.
For example, while using the rsh launcher, if you specified "valgrind orted" as your launch agent and had "--prefix foo" on you cmd line, you would attempt to execute "ssh foo/valgrind orted" - which obviously wouldn't work.
This was all -very- confusing to users, who had to know which PLM was being used so they could even set the right mca param in the first place! And since we don't warn about non-recognized or non-used mca params, half of the time they would wind up not doing what they thought they were telling us to do.
To solve this problem, we did the following:
1. removed all mca params from the individual plms for the launch agent
2. added a new mca param "orte_launch_agent" for this purpose. To further simplify for users, this comes with a new cmd line option "--launch-agent" that can take a multi-word string argument. The value of the param defaults to "orted".
3. added a PLM base function that processes the orte_launch_agent value and adds the contents to a provided argv array. This can subsequently be harvested at-will to handle multi-word values
4. modified the PLMs to use this new function. All the PLMs except for the rsh PLM required very minor change - just called the function and moved on. The rsh PLM required much larger changes as - because of the rsh/ssh cmd line limitations - we had to correctly prepend any provided prefix to the correct argv entry.
5. added a new opal_argv_join_range function that allows the caller to "join" argv entries between two specified indices
Please let me know of any problems. I tried to make this as clean as possible, but cannot compile all PLMs to ensure all is correct.
This commit was SVN r19097.
2008-07-30 22:26:24 +04:00
|
|
|
/*
|
|
|
|
* Join all the elements of an argv array from within a
|
|
|
|
* specified range into a single newly-allocated string.
|
|
|
|
*/
|
|
|
|
char *opal_argv_join_range(char **argv, size_t start, size_t end, int delimiter)
|
|
|
|
{
|
|
|
|
char **p;
|
|
|
|
char *pp;
|
|
|
|
char *str;
|
|
|
|
size_t str_len = 0;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* Bozo case */
|
|
|
|
|
|
|
|
if (NULL == argv || NULL == argv[0] || (int)start > opal_argv_count(argv)) {
|
|
|
|
return strdup("");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the total string length in argv including delimiters. The
|
|
|
|
last delimiter is replaced by the NULL character. */
|
|
|
|
|
|
|
|
for (p = &argv[start], i=start; *p && i < end; ++p, ++i) {
|
|
|
|
str_len += strlen(*p) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the string. */
|
|
|
|
|
|
|
|
if (NULL == (str = (char*) malloc(str_len)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Loop filling in the string. */
|
|
|
|
|
|
|
|
str[--str_len] = '\0';
|
|
|
|
p = &argv[start];
|
|
|
|
pp = *p;
|
|
|
|
|
|
|
|
for (i = 0; i < str_len; ++i) {
|
|
|
|
if ('\0' == *pp) {
|
|
|
|
|
|
|
|
/* End of a string, fill in a delimiter and go to the next
|
|
|
|
string. */
|
|
|
|
|
|
|
|
str[i] = (char) delimiter;
|
|
|
|
++p;
|
|
|
|
pp = *p;
|
|
|
|
} else {
|
|
|
|
str[i] = *pp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All done */
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-20 16:39:27 +04:00
|
|
|
/*
|
2004-01-07 11:03:19 +03:00
|
|
|
* Return the number of bytes consumed by an argv array.
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
size_t opal_argv_len(char **argv)
|
2004-01-07 11:03:19 +03:00
|
|
|
{
|
|
|
|
char **p;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
if (NULL == argv)
|
|
|
|
return (size_t) 0;
|
|
|
|
|
|
|
|
length = sizeof(char *);
|
|
|
|
|
|
|
|
for (p = argv; *p; ++p) {
|
|
|
|
length += strlen(*p) + 1 + sizeof(char *);
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-20 16:39:27 +04:00
|
|
|
/*
|
2004-01-07 11:03:19 +03:00
|
|
|
* Copy a NULL-terminated argv array.
|
|
|
|
*/
|
2005-07-04 04:13:44 +04:00
|
|
|
char **opal_argv_copy(char **argv)
|
2004-01-07 11:03:19 +03:00
|
|
|
{
|
|
|
|
char **dupv = NULL;
|
|
|
|
int dupc = 0;
|
|
|
|
|
|
|
|
if (NULL == argv)
|
|
|
|
return NULL;
|
|
|
|
|
2005-08-23 02:49:27 +04:00
|
|
|
/* create an "empty" list, so that we return something valid if we
|
|
|
|
were passed a valid list with no contained elements */
|
|
|
|
dupv = (char**) malloc(sizeof(char*));
|
|
|
|
dupv[0] = NULL;
|
|
|
|
|
2004-01-07 11:03:19 +03:00
|
|
|
while (NULL != *argv) {
|
2006-02-12 04:33:29 +03:00
|
|
|
if (OPAL_ERROR == opal_argv_append(&dupc, &dupv, *argv)) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_free(dupv);
|
2004-01-07 11:03:19 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
++argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All done */
|
|
|
|
|
|
|
|
return dupv;
|
|
|
|
}
|
2004-10-01 22:38:16 +04:00
|
|
|
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_argv_delete(int *argc, char ***argv, int start, int num_to_delete)
|
2004-10-01 22:38:16 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int count;
|
|
|
|
int suffix_count;
|
2004-12-07 05:42:17 +03:00
|
|
|
char **tmp;
|
2004-10-01 22:38:16 +04:00
|
|
|
|
|
|
|
/* Check for the bozo cases */
|
2005-03-22 17:41:45 +03:00
|
|
|
if (NULL == argv || NULL == *argv || 0 == num_to_delete) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2005-03-22 17:41:45 +03:00
|
|
|
}
|
2005-07-04 04:13:44 +04:00
|
|
|
count = opal_argv_count(*argv);
|
2005-03-22 17:41:45 +03:00
|
|
|
if (start > count) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-10-01 22:38:16 +04:00
|
|
|
} else if (start < 0 || num_to_delete < 0) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-10-01 22:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ok, we have some tokens to delete. Calculate the new length of
|
|
|
|
the argv array. */
|
|
|
|
|
|
|
|
suffix_count = count - (start + num_to_delete);
|
|
|
|
if (suffix_count < 0) {
|
|
|
|
suffix_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free all items that are being deleted */
|
|
|
|
|
|
|
|
for (i = start; i < count && i < start + num_to_delete; ++i) {
|
2004-12-07 06:17:22 +03:00
|
|
|
free((*argv)[i]);
|
2004-10-01 22:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the suffix over the deleted items */
|
|
|
|
|
|
|
|
for (i = start; i < start + suffix_count; ++i) {
|
2004-12-07 06:17:22 +03:00
|
|
|
(*argv)[i] = (*argv)[i + num_to_delete];
|
2004-10-01 22:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the trailing NULL */
|
|
|
|
|
2004-12-07 06:17:22 +03:00
|
|
|
(*argv)[i] = NULL;
|
2004-12-07 05:42:17 +03:00
|
|
|
|
|
|
|
/* adjust the argv array */
|
2006-08-23 00:07:42 +04:00
|
|
|
tmp = (char**)realloc(*argv, sizeof(char**) * (i + 1));
|
2004-12-07 05:42:17 +03:00
|
|
|
if (NULL != tmp) *argv = tmp;
|
2004-10-01 22:38:16 +04:00
|
|
|
|
2005-08-23 02:49:27 +04:00
|
|
|
/* adjust the argc */
|
|
|
|
(*argc) -= num_to_delete;
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-10-01 22:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_argv_insert(char ***target, int start, char **source)
|
2004-10-01 22:38:16 +04:00
|
|
|
{
|
|
|
|
int i, source_count, target_count;
|
|
|
|
int suffix_count;
|
|
|
|
|
|
|
|
/* Check for the bozo cases */
|
|
|
|
|
|
|
|
if (NULL == target || NULL == *target || start < 0) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-10-01 22:38:16 +04:00
|
|
|
} else if (NULL == source) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-10-01 22:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Easy case: appending to the end */
|
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
target_count = opal_argv_count(*target);
|
|
|
|
source_count = opal_argv_count(source);
|
2004-10-01 22:38:16 +04:00
|
|
|
if (start > target_count) {
|
|
|
|
for (i = 0; i < source_count; ++i) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(&target_count, target, source[i]);
|
2004-10-01 22:38:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Harder: insertting into the middle */
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
/* Alloc out new space */
|
|
|
|
|
2004-10-18 19:38:19 +04:00
|
|
|
*target = (char**) realloc(*target,
|
|
|
|
sizeof(char *) * (target_count + source_count + 1));
|
2004-10-01 22:38:16 +04:00
|
|
|
|
|
|
|
/* Move suffix items down to the end */
|
|
|
|
|
|
|
|
suffix_count = target_count - start;
|
|
|
|
for (i = suffix_count - 1; i >= 0; --i) {
|
|
|
|
(*target)[start + source_count + i] =
|
|
|
|
(*target)[start + i];
|
|
|
|
}
|
|
|
|
(*target)[start + suffix_count + source_count] = NULL;
|
|
|
|
|
|
|
|
/* Strdup in the source argv */
|
|
|
|
|
|
|
|
for (i = start; i < start + source_count; ++i) {
|
|
|
|
(*target)[i] = strdup(source[i - start]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All done */
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-10-01 22:38:16 +04:00
|
|
|
}
|