2004-01-07 11:03:19 +03:00
|
|
|
/*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2004-03-19 00:35:28 +03:00
|
|
|
#include "include/constants.h"
|
2004-03-17 21:45:16 +03:00
|
|
|
#include "util/argv.h"
|
|
|
|
#include "util/strncpy.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
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_argv_append(int *argc, char ***argv, const char *arg)
|
2004-01-07 11:03:19 +03:00
|
|
|
{
|
|
|
|
/* Create new argv. */
|
|
|
|
|
|
|
|
if (NULL == *argv) {
|
2004-10-18 19:38:19 +04:00
|
|
|
*argv = (char**) malloc(2 * sizeof(char *));
|
2004-01-07 11:03:19 +03:00
|
|
|
if (NULL == *argv)
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_ERROR;
|
2004-01-13 05:58:37 +03:00
|
|
|
*argc = 0;
|
2004-01-07 11:03:19 +03:00
|
|
|
(*argv)[0] = NULL;
|
|
|
|
(*argv)[1] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend existing argv. */
|
|
|
|
|
|
|
|
else {
|
2004-10-18 19:38:19 +04:00
|
|
|
*argv = (char**) realloc(*argv, (*argc + 2) * sizeof(char *));
|
2004-01-07 11:03:19 +03:00
|
|
|
if (NULL == *argv)
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_ERROR;
|
2004-01-07 11:03:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the newest element to point to a copy of the arg string */
|
|
|
|
|
2004-10-18 19:38:19 +04:00
|
|
|
(*argv)[*argc] = (char*) malloc(strlen(arg) + 1);
|
2004-01-07 11:03:19 +03:00
|
|
|
if (NULL == (*argv)[*argc])
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_ERROR;
|
2004-01-07 11:03:19 +03:00
|
|
|
|
|
|
|
strcpy((*argv)[*argc], arg);
|
|
|
|
*argc = *argc + 1;
|
|
|
|
(*argv)[*argc] = NULL;
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-01-07 11:03:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-20 16:39:27 +04:00
|
|
|
/*
|
2004-01-07 11:03:19 +03:00
|
|
|
* Free a NULL-terminated argv array.
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
void ompi_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.
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
char **ompi_argv_split(const char *src_string, int delimiter)
|
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) {
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tail argument, add straight from the original string */
|
|
|
|
|
|
|
|
else if ('\0' == *p) {
|
2004-06-07 19:33:53 +04:00
|
|
|
if (OMPI_ERROR == ompi_argv_append(&argc, &argv, src_string))
|
2004-01-07 11:03:19 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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';
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
if (OMPI_ERROR == ompi_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';
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
if (OMPI_ERROR == ompi_argv_append(&argc, &argv, arg))
|
2004-01-07 11:03:19 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
src_string = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All done */
|
|
|
|
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_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.
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
char *ompi_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
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
size_t ompi_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.
|
|
|
|
*/
|
2004-06-07 19:33:53 +04:00
|
|
|
char **ompi_argv_copy(char **argv)
|
2004-01-07 11:03:19 +03:00
|
|
|
{
|
|
|
|
char **dupv = NULL;
|
|
|
|
int dupc = 0;
|
|
|
|
|
|
|
|
if (NULL == argv)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (NULL != *argv) {
|
2004-06-07 19:33:53 +04:00
|
|
|
if (OMPI_ERROR == ompi_argv_append(&dupc, &dupv, *argv)) {
|
|
|
|
ompi_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
|
|
|
|
|
|
|
|
|
|
|
int ompi_argv_delete(char **argv, int start, int num_to_delete)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int count;
|
|
|
|
int suffix_count;
|
|
|
|
|
|
|
|
/* Check for the bozo cases */
|
|
|
|
|
|
|
|
count = ompi_argv_count(argv);
|
|
|
|
if (NULL == argv || start > count || 0 == num_to_delete) {
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
} else if (start < 0 || num_to_delete < 0) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
free(argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the suffix over the deleted items */
|
|
|
|
|
|
|
|
for (i = start; i < start + suffix_count; ++i) {
|
|
|
|
argv[i] = argv[i + num_to_delete];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the trailing NULL */
|
|
|
|
|
|
|
|
argv[i] = NULL;
|
|
|
|
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ompi_argv_insert(char ***target, int start, char **source)
|
|
|
|
{
|
|
|
|
int i, source_count, target_count;
|
|
|
|
int suffix_count;
|
|
|
|
|
|
|
|
/* Check for the bozo cases */
|
|
|
|
|
|
|
|
if (NULL == target || NULL == *target || start < 0) {
|
|
|
|
return OMPI_ERR_BAD_PARAM;
|
|
|
|
} else if (NULL == source) {
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Easy case: appending to the end */
|
|
|
|
|
|
|
|
target_count = ompi_argv_count(*target);
|
|
|
|
source_count = ompi_argv_count(source);
|
|
|
|
if (start > target_count) {
|
|
|
|
for (i = 0; i < source_count; ++i) {
|
|
|
|
ompi_argv_append(&target_count, target, source[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|