1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* 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.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2007 Voltaire. All rights reserved.
* Copyright (c) 2012 Los Alamos National Security, LLC. All rights reserved.
* Copyright (c) 2014-2020 Intel, Inc. All rights reserved.
*
* Copyright (c) 2015 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2021-2022 Nanook Consulting. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "src/include/pmix_config.h"
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#include "pmix_common.h"
#include "src/util/pmix_argv.h"
#define ARGSIZE 128
/*
* Append a string to the end of a new or existing argv array.
*/
pmix_status_t pmix_argv_append(int *argc, char ***argv, const char *arg)
{
pmix_status_t rc;
/* add the new element */
if (PMIX_SUCCESS != (rc = PMIx_Argv_append_nosize(argv, arg))) {
return rc;
}
*argc = PMIx_Argv_count(*argv);
return PMIX_SUCCESS;
}
pmix_status_t pmix_argv_append_unique_idx(int *idx, char ***argv, const char *arg)
{
int i;
pmix_status_t rc;
/* if the provided array is NULL, then the arg cannot be present,
* so just go ahead and append
*/
if (NULL == *argv) {
goto add;
}
/* 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 */
*idx = i;
return PMIX_SUCCESS;
}
}
add:
if (PMIX_SUCCESS != (rc = PMIx_Argv_append_nosize(argv, arg))) {
return rc;
}
*idx = PMIx_Argv_count(*argv) - 1;
return PMIX_SUCCESS;
}
/*
* Join all the elements of an argv array from within a
* specified range into a single newly-allocated string.
*/
char *pmix_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 >= PMIx_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;
}
if (0 == str_len) {
return strdup("");
}
/* 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;
}
/*
* Return the number of bytes consumed by an argv array.
*/
size_t pmix_argv_len(char **argv)
{
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;
}
/*
* Copy a NULL-terminated argv array, stripping any leading/trailing
* quotes from each element
*/
char **pmix_argv_copy_strip(char **argv)
{
char **dupv = NULL;
int n;
char *start;
bool mod;
size_t len;
if (NULL == argv) {
return NULL;
}
/* 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;
for (n=0; NULL != argv[n]; n++) {
mod = false;
start = argv[n];
if ('\"' == argv[n][0]) {
++start;
}
len = strlen(argv[n]);
if ('\"' == argv[n][len-1]) {
argv[n][len-1] = '\0';
mod = true;
}
if (PMIX_SUCCESS != PMIx_Argv_append_nosize(&dupv, start)) {
PMIx_Argv_free(dupv);
if (mod) {
argv[n][len-1] = '\"';
}
return NULL;
}
if (mod) {
argv[n][len-1] = '\"';
}
}
/* All done */
return dupv;
}
pmix_status_t pmix_argv_delete(int *argc, char ***argv, int start, int num_to_delete)
{
int i;
int count;
int suffix_count;
char **tmp;
/* Check for the bozo cases */
if (NULL == argv || NULL == *argv || 0 == num_to_delete) {
return PMIX_SUCCESS;
}
count = PMIx_Argv_count(*argv);
if (start > count) {
return PMIX_SUCCESS;
} else if (start < 0 || num_to_delete < 0) {
return PMIX_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;
/* adjust the argv array */
tmp = (char **) realloc(*argv, sizeof(char *) * (i + 1));
if (NULL != tmp)
*argv = tmp;
/* adjust the argc */
(*argc) -= num_to_delete;
return PMIX_SUCCESS;
}
pmix_status_t pmix_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 PMIX_ERR_BAD_PARAM;
} else if (NULL == source) {
return PMIX_SUCCESS;
}
/* Easy case: appending to the end */
target_count = PMIx_Argv_count(*target);
source_count = PMIx_Argv_count(source);
if (start > target_count) {
for (i = 0; i < source_count; ++i) {
pmix_argv_append(&target_count, target, source[i]);
}
}
/* Harder: insertting into the middle */
else {
/* Alloc out new space */
*target = (char **) realloc(*target, sizeof(char *) * (target_count + source_count + 1));
/* 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 PMIX_SUCCESS;
}
pmix_status_t pmix_argv_insert_element(char ***target, int location, char *source)
{
int i, target_count;
int suffix_count;
/* Check for the bozo cases */
if (NULL == target || NULL == *target || location < 0) {
return PMIX_ERR_BAD_PARAM;
} else if (NULL == source) {
return PMIX_SUCCESS;
}
/* Easy case: appending to the end */
target_count = PMIx_Argv_count(*target);
if (location > target_count) {
pmix_argv_append(&target_count, target, source);
return PMIX_SUCCESS;
}
/* Alloc out new space */
*target = (char **) realloc(*target, sizeof(char *) * (target_count + 2));
/* Move suffix items down to the end */
suffix_count = target_count - location;
for (i = suffix_count - 1; i >= 0; --i) {
(*target)[location + 1 + i] = (*target)[location + i];
}
(*target)[location + suffix_count + 1] = NULL;
/* Strdup in the source */
(*target)[location] = strdup(source);
/* All done */
return PMIX_SUCCESS;
}