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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2013 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) 2012-2017 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2012-2020 Cisco Systems, Inc. All rights reserved
* Copyright (c) 2015-2017 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2016-2020 Intel, Inc. All rights reserved.
* Copyright (c) 2017 IBM Corporation. All rights reserved.
* Copyright (c) 2021-2025 Nanook Consulting All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "src/include/pmix_config.h"
#include "src/class/pmix_list.h"
#include "src/class/pmix_object.h"
#include "src/include/pmix_globals.h"
#include "src/util/pmix_argv.h"
#include "src/util/pmix_cmd_line.h"
#include "src/util/pmix_output.h"
#include "src/util/pmix_printf.h"
#include "src/util/pmix_show_help.h"
// Local functions
static int endswith(const char *str, const char *suffix)
{
size_t lenstr, lensuffix;
if (NULL == str || NULL == suffix) {
return PMIX_ERR_BAD_PARAM;
}
lenstr = strlen(str);
lensuffix = strlen(suffix);
if (lensuffix > lenstr) {
return PMIX_ERR_BAD_PARAM;
}
if (0 == strncmp(str + lenstr - lensuffix, suffix, lensuffix)) {
return PMIX_SUCCESS;
}
return PMIX_ERR_BAD_PARAM;
}
static void check_store(const char *name, const char *option,
pmix_cli_result_t *results)
{
pmix_cli_item_t *opt;
PMIX_LIST_FOREACH(opt, &results->instances, pmix_cli_item_t) {
if (0 == strcmp(opt->key, name)) {
/* if the name is NULL, then this is just setting
* a boolean value - the presence of the option in
* the results is considered "true" */
if (NULL != option) {
PMIx_Argv_append_nosize(&opt->values, option);
}
return;
}
}
// get here if this is new option
opt = PMIX_NEW(pmix_cli_item_t);
opt->key = strdup(name);
pmix_list_append(&results->instances, &opt->super);
/* if the name is NULL, then this is just setting
* a boolean value - the presence of the option in
* the results is considered "true" */
if (NULL != option) {
PMIx_Argv_append_nosize(&opt->values, option);
}
return;
}
int pmix_cmd_line_parse(char **pargv, char *shorts,
struct option myoptions[],
pmix_cmd_line_store_fn_t storefn,
pmix_cli_result_t *results,
char *helpfile)
{
int option_index = 0; /* getopt_long stores the option index here. */
int n, m, opt, argc, argind;
bool found;
char *ptr, *str, **argv;
pmix_cmd_line_store_fn_t mystore;
/* the getopt_long parser reorders the input argv array, so
* we have to protect it here */
argv = PMIx_Argv_copy(pargv);
argc = PMIx_Argv_count(argv);
// assign a default store_fn if one isn't provided
if (NULL == storefn) {
mystore = check_store;
} else {
mystore = storefn;
}
/* reset the parser - must be done each time we use it
* to avoid hysteresis */
optind = 0;
opterr = 0;
optopt = 0;
optarg = NULL;
if (1 == argc) {
// nothing to parse
goto done;
}
// run the parser
while (1) {
argind = optind;
if (optind == argc || (optind > 0 && '-' != argv[optind][0])) {
// This is the executable, or we are at the last argument.
// Don't process any further.
break;
}
opt = getopt_long(argc, argv, shorts, myoptions, &option_index);
switch (opt) {
case 0:
/* if this is an MCA param of some type, store it */
if (0 == endswith(myoptions[option_index].name, "mca")) {
/* format mca params as param:value - the optind value
* will have been incremented since the MCA param options
* require an argument */
pmix_asprintf(&str, "%s=%s", argv[optind-1], argv[optind]);
mystore(myoptions[option_index].name, str, results);
free(str);
++optind;
break;
}
// if this is a "-env" option, then there can be three entries
// that describe what to do
if (0 == strcmp(myoptions[option_index].name, PMIX_CLI_PREPEND_ENVAR) ||
0 == strcmp(myoptions[option_index].name, PMIX_CLI_APPEND_ENVAR)) {
mystore(myoptions[option_index].name, argv[optind-1], results);
mystore(myoptions[option_index].name, argv[optind], results);
++optind;
break;
}
if (0 == strcmp(myoptions[option_index].name, PMIX_CLI_UNSET_ENVAR)) {
// unset-env option only takes one argument
mystore(myoptions[option_index].name, optarg, results);
break;
}
/* otherwise, store actual option */
mystore(myoptions[option_index].name, optarg, results);
break;
case 'h':
/* the "help" option can optionally take an argument. Since
* the argument _is_ optional, getopt will _NOT_ increment
* optind, so argv[optind] is the potential argument */
if (NULL == optarg &&
NULL != argv[optind]) {
/* strip any leading dashes */
ptr = argv[optind];
while ('-' == *ptr) {
++ptr;
}
// check for standard options
if (0 == strcmp(ptr, "version") || 0 == strcmp(ptr, "V")) {
str = pmix_show_help_string("help-cli.txt", "version", false);
pmix_output(0, "HELP VERSION");
if (NULL != str) {
printf("%s\n", str);
fflush(stdout);
free(str);
}
PMIx_Argv_free(argv);
return PMIX_OPERATION_SUCCEEDED;
}
if (0 == strcmp(ptr, "verbose") || 0 == strcmp(ptr, "v")) {
str = pmix_show_help_string("help-cli.txt", "verbose", false);
if (NULL != str) {
printf("%s\n", str);
fflush(stdout);
free(str);
}
PMIx_Argv_free(argv);
return PMIX_OPERATION_SUCCEEDED;
}
if (0 == strcmp(ptr, "help") || 0 == strcmp(ptr, "h")) {
// they requested help on the "help" option itself
str = pmix_show_help_string("help-cli.txt", "help", false,
pmix_tool_basename, pmix_tool_basename,
pmix_tool_basename, pmix_tool_basename,
pmix_tool_basename, pmix_tool_basename,
pmix_tool_basename, pmix_tool_basename);
if (NULL != str) {
printf("%s\n", str);
fflush(stdout);
free(str);
}
PMIx_Argv_free(argv);
return PMIX_OPERATION_SUCCEEDED;
}
/* see if we have help on that subject */
str = pmix_show_help_string(helpfile, ptr, false);
if (NULL == str) {
// let the user know we don't recognize that topic
str = pmix_show_help_string("help-cli.txt", "unknown-option", true,
ptr, pmix_tool_basename);
if (NULL != str) {
fprintf(stderr, "%s", str);
fflush(stderr);
free(str);
}
} else {
printf("%s\n", str);
fflush(stdout);
free(str);
}
PMIx_Argv_free(argv);
return PMIX_OPERATION_SUCCEEDED;
} else if (NULL == optarg) {
// high-level help request
str = pmix_show_help_string(helpfile, "usage", false,
pmix_tool_basename, pmix_tool_org,
pmix_tool_version,
pmix_tool_basename,
pmix_tool_msg);
if (NULL != str) {
printf("%s\n", str);
fflush(stdout);
free(str);
}
PMIx_Argv_free(argv);
return PMIX_OPERATION_SUCCEEDED;
} else { // unrecognized option
str = pmix_show_help_string("help-cli.txt", "unrecognized-option", true,
pmix_tool_basename, optarg);
if (NULL != str) {
fprintf(stderr, "%s", str);
fflush(stderr);
free(str);
}
}
PMIx_Argv_free(argv);
return PMIX_ERR_SILENT;
case 'V':
str = pmix_show_help_string(helpfile, "version", false,
pmix_tool_basename, pmix_tool_org,
pmix_tool_version,
pmix_tool_msg);
if (NULL != str) {
printf("%s\n", str);
fflush(stdout);
free(str);
}
// if they ask for the version, that is all we do
PMIx_Argv_free(argv);
return PMIX_OPERATION_SUCCEEDED;
case 'v':
/* if the argv at this point is not pointing at a string
* starting with "-v", then we just ignore it - the user
* has passed a string with multiple 'v's in it and we
* need to wait until the end */
if (0 != strncmp(argv[optind-1], "-v", 2)) {
break;
}
/* we store the 'v' option with a value equal to the
* number of 'v's the user provided */
n = strlen(&argv[optind-1][1]);
pmix_asprintf(&str, "%d", n);
mystore(myoptions[option_index].name, str, results);
free(str);
break;
default:
found = false;
for (n=0; '\0' != shorts[n]; n++) {
int ascii = shorts[n];
if (opt == ascii) {
/* found it - now search for matching option. The
* getopt fn will have already incremented optind
* to point at the next argument.
* If this short option required an argument, then
* it will be indicated by a ':' in the next shorts
* spot and the argument will be in optarg.
*
* If the short option takes an optional argument, then
* it will be indicated by two ':' after the option - in
* this case optarg will contain the argument if given.
* Note that the proper form of the optional argument
* option is "-zfoo", where 'z' is the option and "foo"
* is the argument. Putting a space between the option
* and the argument is forbidden and results in reporting
* of 'z' without an argument - usually followed by
* incorrectly marking "foo" as the beginning of the
* command "tail" */
if (':' == shorts[n+1]) {
// could be an optional arg
if (':' == shorts[n+2]) {
/* in this case, the argument (if given) must be immediately
* attached to the option */
ptr = argv[optind-1];
ptr += 2; // step over the '-' and option
} else {
ptr = optarg;
}
} else {
ptr = NULL;
}
for (m=0; NULL != myoptions[m].name; m++) {
if (ascii == myoptions[m].val) {
if (PMIX_ARG_NONE == myoptions[m].has_arg) {
/* if ptr isn't NULL, then that means we were given
* an argument to an option that doesn't take one.
* Report the error */
if (NULL != ptr) {
str = pmix_show_help_string("help-cli.txt", "short-arg-error", true,
pmix_tool_basename, shorts[n], ptr);
if (NULL != str) {
fprintf(stderr, "%s", str);
fflush(stderr);
free(str);
}
PMIx_Argv_free(argv);
return PMIX_ERR_SILENT;
}
ptr = NULL;
} else if (0 == strcmp(myoptions[m].name, "np") &&
0 == strcmp(optarg, "p")) {
/* we special-case the very common "-np" option */
ptr = argv[optind];
++optind;
}
mystore(myoptions[m].name, ptr, results);
found = true;
break;
}
}
if (found) {
break;
}
/* this could be one of the short options other than 'h' or 'V', so
* we have to check */
if (0 != argind && '-' != argv[argind][0]) {
// this was not an option
goto done;
}
if (0 == strcmp(argv[optind-1], "--")) {
// double-dash indicates separator between launcher
// directives and the application
results->tail = PMIx_Argv_copy(&argv[optind]);
PMIx_Argv_free(argv);
return PMIX_SUCCESS;
}
str = pmix_show_help_string("help-cli.txt", "short-no-long", true,
pmix_tool_basename, shorts[n]);
if (NULL != str) {
fprintf(stderr, "%s", str);
fflush(stderr);
free(str);
}
PMIx_Argv_free(argv);
return PMIX_ERR_SILENT;
}
}
if (found) {
break;
}
/* see if the option is in the list - if it is, then it is a
* "recognized" option but may be missing an argument. The
* getopt_long function declares these as "unrecognized", but
* we would like to provide a more user-friendly error message */
for (n=0; NULL != myoptions[n].name; n++) {
/* skip the "--" prefix */
if (0 == strcmp(&argv[optind-1][2], myoptions[n].name)) {
/* the option is recognized - probably misssing
* an argument */
str = pmix_show_help_string("help-cli.txt", "missing-argument", true,
pmix_tool_basename, argv[optind-1],
pmix_tool_basename, &argv[optind-1][2]);
if (NULL != str) {
fprintf(stderr, "%s", str);
fflush(stderr);
free(str);
}
PMIx_Argv_free(argv);
return PMIX_ERR_SILENT;
}
}
if (0 == strcmp(argv[optind-1], "--")) {
// double-dash indicates separator between launcher
// directives and the application
goto done;
}
if (1 == optind) {
// command without any options
goto done;
}
str = pmix_show_help_string("help-cli.txt", "unregistered-option", true,
pmix_tool_basename, argv[optind-1], pmix_tool_basename);
if (NULL != str) {
fprintf(stderr, "%s", str);
fflush(stderr);
free(str);
}
PMIx_Argv_free(argv);
return PMIX_ERR_SILENT;
}
}
done:
if (optind < argc) {
/* if this is an '&', it simply indicates that the executable
* was being pushed into the background - ignore it */
if (0 != strcmp(argv[optind], "&")) {
results->tail = PMIx_Argv_copy(&argv[optind]);
}
}
PMIx_Argv_free(argv);
return PMIX_SUCCESS;
}
static void icon(pmix_cli_item_t *p)
{
p->key = NULL;
p->values = NULL;
}
static void ides(pmix_cli_item_t *p)
{
if (NULL != p->key) {
free(p->key);
}
if (NULL != p->values) {
PMIx_Argv_free(p->values);
}
}
PMIX_EXPORT PMIX_CLASS_INSTANCE(pmix_cli_item_t,
pmix_list_item_t,
icon, ides);
static void ocon(pmix_cli_result_t *p)
{
PMIX_CONSTRUCT(&p->instances, pmix_list_t);
p->tail = NULL;
}
static void odes(pmix_cli_result_t *p)
{
PMIX_LIST_DESTRUCT(&p->instances);
if (NULL != p->tail) {
PMIx_Argv_free(p->tail);
}
}
PMIX_EXPORT PMIX_CLASS_INSTANCE(pmix_cli_result_t,
pmix_object_t,
ocon, odes);