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
|
/*
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2008 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) 2008 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) 2008 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2015 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "orte_config.h"
#include "orte/constants.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include "opal/util/argv.h"
#include "opal/util/output.h"
#include "orte/runtime/orte_globals.h"
#include "orte/util/parse_options.h"
void orte_util_parse_range_options(char *inp, char ***output)
{
char **r1=NULL, **r2=NULL;
int i, vint;
int start, end, n;
char nstr[32];
char *input, *bang;
bool bang_option=false;
/* protect against null input */
if (NULL == inp) {
return;
}
/* protect the provided input */
input = strdup(inp);
/* check for the special '!' operator */
if (NULL != (bang = strchr(input, '!'))) {
bang_option = true;
*bang = '\0';
}
/* split on commas */
r1 = opal_argv_split(input, ',');
/* for each resulting element, check for range */
for (i=0; i < opal_argv_count(r1); i++) {
r2 = opal_argv_split(r1[i], '-');
if (1 < opal_argv_count(r2)) {
/* given range - get start and end */
start = strtol(r2[0], NULL, 10);
end = strtol(r2[1], NULL, 10);
} else {
/* check for wildcard - have to do this here because
* the -1 would have been caught in the split
*/
vint = strtol(r1[i], NULL, 10);
if (-1 == vint) {
opal_argv_free(*output);
*output = NULL;
opal_argv_append_nosize(output, "-1");
opal_argv_free(r2);
goto cleanup;
}
start = strtol(r2[0], NULL, 10);
end = start;
}
for (n = start; n <= end; n++) {
snprintf(nstr, 32, "%d", n);
opal_argv_append_nosize(output, nstr);
}
opal_argv_free(r2);
}
cleanup:
if (bang_option) {
opal_argv_append_nosize(output, "BANG");
}
free(input);
opal_argv_free(r1);
}
void orte_util_get_ranges(char *inp, char ***startpts, char ***endpts)
{
char **r1=NULL, **r2=NULL;
int i;
char *input;
/* protect against null input */
if (NULL == inp) {
return;
}
/* protect the provided input */
input = strdup(inp);
/* split on commas */
r1 = opal_argv_split(input, ',');
/* for each resulting element, check for range */
for (i=0; i < opal_argv_count(r1); i++) {
r2 = opal_argv_split(r1[i], '-');
if (2 == opal_argv_count(r2)) {
/* given range - get start and end */
opal_argv_append_nosize(startpts, r2[0]);
opal_argv_append_nosize(endpts, r2[1]);
} else if (1 == opal_argv_count(r2)) {
/* only one value provided, so it is both the start
* and the end
*/
opal_argv_append_nosize(startpts, r2[0]);
opal_argv_append_nosize(endpts, r2[0]);
} else {
/* no idea how to parse this */
opal_output(0, "%s Unknown parse error on string: %s(%s)",
ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), inp, r1[i]);
}
opal_argv_free(r2);
}
free(input);
opal_argv_free(r1);
}
|