root/opal/mca/pmix/pmix4x/pmix/src/util/parse_options.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pmix_util_parse_range_options
  2. pmix_util_get_ranges

   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 (c) 2016      Intel, Inc.  All rights reserved.
  17  * $COPYRIGHT$
  18  *
  19  * Additional copyrights may follow
  20  *
  21  * $HEADER$
  22  */
  23 #include "pmix_config.h"
  24 #include "pmix_common.h"
  25 
  26 #include <stdio.h>
  27 #include <string.h>
  28 #include <stdlib.h>
  29 #ifdef HAVE_UNISTD_H
  30 #include <unistd.h>
  31 #endif
  32 #ifdef HAVE_SYS_TYPES_H
  33 #include <sys/types.h>
  34 #endif
  35 
  36 #include "src/util/argv.h"
  37 #include "src/util/output.h"
  38 
  39 #include "src/util/parse_options.h"
  40 
  41 void pmix_util_parse_range_options(char *inp, char ***output)
  42 {
  43     char **r1=NULL, **r2=NULL;
  44     int i, vint;
  45     int start, end, n;
  46     char nstr[32];
  47     char *input, *bang;
  48     bool bang_option=false;
  49 
  50     /* protect against null input */
  51     if (NULL == inp) {
  52         return;
  53     }
  54 
  55     /* protect the provided input */
  56     input = strdup(inp);
  57 
  58     /* check for the special '!' operator */
  59     if (NULL != (bang = strchr(input, '!'))) {
  60         bang_option = true;
  61         *bang = '\0';
  62     }
  63 
  64     /* split on commas */
  65     r1 = pmix_argv_split(input, ',');
  66     /* for each resulting element, check for range */
  67     for (i=0; i < pmix_argv_count(r1); i++) {
  68         r2 = pmix_argv_split(r1[i], '-');
  69         if (1 < pmix_argv_count(r2)) {
  70             /* given range - get start and end */
  71             start = strtol(r2[0], NULL, 10);
  72             end = strtol(r2[1], NULL, 10);
  73         } else {
  74             /* check for wildcard - have to do this here because
  75              * the -1 would have been caught in the split
  76              */
  77             vint = strtol(r1[i], NULL, 10);
  78             if (-1 == vint) {
  79                 pmix_argv_free(*output);
  80                 *output = NULL;
  81                 pmix_argv_append_nosize(output, "-1");
  82                 pmix_argv_free(r2);
  83                 goto cleanup;
  84             }
  85             start = strtol(r2[0], NULL, 10);
  86             end = start;
  87         }
  88         for (n = start; n <= end; n++) {
  89             snprintf(nstr, 32, "%d", n);
  90             pmix_argv_append_nosize(output, nstr);
  91         }
  92         pmix_argv_free(r2);
  93     }
  94 
  95 cleanup:
  96     if (bang_option) {
  97         pmix_argv_append_nosize(output, "BANG");
  98     }
  99     free(input);
 100     pmix_argv_free(r1);
 101 
 102 }
 103 
 104 void pmix_util_get_ranges(char *inp, char ***startpts, char ***endpts)
 105 {
 106     char **r1=NULL, **r2=NULL;
 107     int i;
 108     char *input;
 109 
 110     /* protect against null input */
 111     if (NULL == inp) {
 112         return;
 113     }
 114 
 115     /* protect the provided input */
 116     input = strdup(inp);
 117 
 118     /* split on commas */
 119     r1 = pmix_argv_split(input, ',');
 120     /* for each resulting element, check for range */
 121     for (i=0; i < pmix_argv_count(r1); i++) {
 122         r2 = pmix_argv_split(r1[i], '-');
 123         if (2 == pmix_argv_count(r2)) {
 124             /* given range - get start and end */
 125             pmix_argv_append_nosize(startpts, r2[0]);
 126             pmix_argv_append_nosize(endpts, r2[1]);
 127         } else if (1 == pmix_argv_count(r2)) {
 128             /* only one value provided, so it is both the start
 129              * and the end
 130              */
 131             pmix_argv_append_nosize(startpts, r2[0]);
 132             pmix_argv_append_nosize(endpts, r2[0]);
 133         } else {
 134             /* no idea how to parse this */
 135             pmix_output(0, "Unknown parse error on string: %s(%s)", inp, r1[i]);
 136         }
 137         pmix_argv_free(r2);
 138     }
 139 
 140     free(input);
 141     pmix_argv_free(r1);
 142 
 143 }

/* [<][>][^][v][top][bottom][index][help] */