config.cc 2.99 KB
Newer Older
1
/*
2
 * Copyright (c) 2002-2009 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23
 */

24
static const char rcsid[] = "$Id: config.cc,v 1.6 2009-06-16 21:11:05 ricci Exp $";
25

26 27 28 29 30 31 32 33 34 35 36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
  
/*
 * Dumb as a stump config for options that the test program supports
 */

void
37
parse_options(char **argv, struct config_param opts[], int nopt)
38 39 40
{
  extern char **environ;

41 42
  /* get from environment first - just skip unknown environment variables */
  config_parse(environ, opts, nopt, false);
43

44 45
  /* then command line - die if given bad option */
  config_parse(argv, opts, nopt, true);
46 47 48
}

int
49 50
config_parse(char **args, struct config_param cparams[], int nparams, 
        bool exit_on_error)
51 52 53 54
{
  int i, j, len;
  char *arg, *var, *val, buf[256];

55
  for (i = 0, arg = args[i]; arg != NULL; i++, arg = args[i]) {
56 57 58 59
    len = sizeof(buf) - 1;
    strncpy(buf, arg, len);
    buf[len] = 0;
    var = val = buf;
60 61 62 63 64 65 66 67 68
    if (strsep(&val, "=") == 0 || val == 0 || *val == 0) {
        if (exit_on_error) {
            cout << "*** Expected a configuration parameter, got "
                 << arg << endl;
            exit(EXIT_FATAL);
        } else {
            continue;
        }
    }
69 70 71 72 73

    for (j = 0; j < nparams; j++)
      if (strcmp(var, cparams[j].name) == 0)
	break;

74 75 76 77 78 79 80 81 82 83
    if (j == nparams) {
      if (exit_on_error) {
          cout << "*** Bad configuration parameter name: " << var << endl;
          exit(EXIT_FATAL);
      } else {
          continue;
      }
    }

    // Note: Unused
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    if (cparams[j].func) {
      cparams[j].func(&cparams[j], val);
      continue;
    }

    switch (cparams[j].type) {
    case CONFIG_INT:
      *(int *)cparams[j].ptr = atoi(val);
      break;
    case CONFIG_FLOAT:
      *(float *)cparams[j].ptr = (float) atof(val);
      break;
    }
  }

  return 0;
}

void
dump_options(const char *str, struct config_param cparams[], int numparams)
{
  int i;

107
  fprintf(stderr,"%s:\n", str);
108 109 110 111 112 113 114 115 116 117 118 119 120 121
  for (i = 0; i < numparams; i++) {
    fprintf(stderr,"  %s ", cparams[i].name);
    switch (cparams[i].type) {
    case CONFIG_INT:
      fprintf(stderr,"%d\n",
	      *(int *)cparams[i].ptr);
      break;
    case CONFIG_FLOAT:
      fprintf(stderr,"%f\n",
	      *(float *)cparams[i].ptr);
      break;
    }
  }
}