1 /*
2 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
3 * University Research and Technology
4 * Corporation. All rights reserved.
5 * Copyright (c) 2004-2008 The University of Tennessee and The University
6 * of Tennessee Research Foundation. All rights
7 * reserved.
8 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9 * University of Stuttgart. All rights reserved.
10 * Copyright (c) 2004-2005 The Regents of the University of California.
11 * All rights reserved.
12 * Copyright (c) 2008 Sun Microsystems, Inc. All rights reserved.
13 * Copyright (c) 2008 Cisco Systems, Inc. All rights reserved.
14 * Copyright (c) 2015 Research Organization for Information Science
15 * and Technology (RIST). All rights reserved.
16 * $COPYRIGHT$
17 *
18 * Additional copyrights may follow
19 *
20 * $HEADER$
21 */
22 #include "orte_config.h"
23 #include "orte/constants.h"
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34
35 #include "opal/util/argv.h"
36 #include "opal/util/output.h"
37
38 #include "orte/runtime/orte_globals.h"
39
40 #include "orte/util/parse_options.h"
41
orte_util_parse_range_options(char * inp,char *** output)42 void orte_util_parse_range_options(char *inp, char ***output)
43 {
44 char **r1=NULL, **r2=NULL;
45 int i, vint;
46 int start, end, n;
47 char nstr[32];
48 char *input, *bang;
49 bool bang_option=false;
50
51 /* protect against null input */
52 if (NULL == inp) {
53 return;
54 }
55
56 /* protect the provided input */
57 input = strdup(inp);
58
59 /* check for the special '!' operator */
60 if (NULL != (bang = strchr(input, '!'))) {
61 bang_option = true;
62 *bang = '\0';
63 }
64
65 /* split on commas */
66 r1 = opal_argv_split(input, ',');
67 /* for each resulting element, check for range */
68 for (i=0; i < opal_argv_count(r1); i++) {
69 r2 = opal_argv_split(r1[i], '-');
70 if (1 < opal_argv_count(r2)) {
71 /* given range - get start and end */
72 start = strtol(r2[0], NULL, 10);
73 end = strtol(r2[1], NULL, 10);
74 } else {
75 /* check for wildcard - have to do this here because
76 * the -1 would have been caught in the split
77 */
78 vint = strtol(r1[i], NULL, 10);
79 if (-1 == vint) {
80 opal_argv_free(*output);
81 *output = NULL;
82 opal_argv_append_nosize(output, "-1");
83 opal_argv_free(r2);
84 goto cleanup;
85 }
86 start = strtol(r2[0], NULL, 10);
87 end = start;
88 }
89 for (n = start; n <= end; n++) {
90 snprintf(nstr, 32, "%d", n);
91 opal_argv_append_nosize(output, nstr);
92 }
93 opal_argv_free(r2);
94 }
95
96 cleanup:
97 if (bang_option) {
98 opal_argv_append_nosize(output, "BANG");
99 }
100 free(input);
101 opal_argv_free(r1);
102
103 }
104
orte_util_get_ranges(char * inp,char *** startpts,char *** endpts)105 void orte_util_get_ranges(char *inp, char ***startpts, char ***endpts)
106 {
107 char **r1=NULL, **r2=NULL;
108 int i;
109 char *input;
110
111 /* protect against null input */
112 if (NULL == inp) {
113 return;
114 }
115
116 /* protect the provided input */
117 input = strdup(inp);
118
119 /* split on commas */
120 r1 = opal_argv_split(input, ',');
121 /* for each resulting element, check for range */
122 for (i=0; i < opal_argv_count(r1); i++) {
123 r2 = opal_argv_split(r1[i], '-');
124 if (2 == opal_argv_count(r2)) {
125 /* given range - get start and end */
126 opal_argv_append_nosize(startpts, r2[0]);
127 opal_argv_append_nosize(endpts, r2[1]);
128 } else if (1 == opal_argv_count(r2)) {
129 /* only one value provided, so it is both the start
130 * and the end
131 */
132 opal_argv_append_nosize(startpts, r2[0]);
133 opal_argv_append_nosize(endpts, r2[0]);
134 } else {
135 /* no idea how to parse this */
136 opal_output(0, "%s Unknown parse error on string: %s(%s)",
137 ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), inp, r1[i]);
138 }
139 opal_argv_free(r2);
140 }
141
142 free(input);
143 opal_argv_free(r1);
144
145 }
146