/ src / core / options.cc
options.cc
  1  /*
  2   * options.cc
  3   *
  4   * This module handles global options passed on the command-line.
  5   *
  6   */
  7  
  8  #include "version.h"
  9  #include "options.h"
 10  #include "osutils.h"
 11  
 12  #include <set>
 13  #include <vector>
 14  #include <string>
 15  #include <map>
 16  
 17  #include <stdlib.h>
 18  
 19  using namespace std;
 20  
 21  __ID("@(#) $Id$");
 22  
 23  static set < string > disabled_tests;
 24  static set < string > visible_classes;
 25  static map < string, string > aliases;
 26  
 27  void alias(const char * aname, const char * cname)
 28  {
 29    aliases[lowercase(aname)] = lowercase(cname);
 30  }
 31  
 32  
 33  static string getcname(const char * aname)
 34  {
 35    if(aliases.find(lowercase(aname)) != aliases.end())
 36      return lowercase(aliases[lowercase(aname)]);
 37    else
 38      return lowercase(aname);
 39  }
 40  
 41  
 42  static void remove_option_argument(int i,
 43  int &argc,
 44  char *argv[])
 45  {
 46    for (int j = i; j + 2 < argc; j++)
 47      argv[j] = argv[j + 2];
 48  
 49    argc -= 2;
 50  }
 51  
 52  
 53  bool parse_options(int &argc,
 54  char *argv[])
 55  {
 56    int i = 1;
 57    string option = "";
 58  
 59    while (i < argc)
 60    {
 61      option = string(argv[i]);
 62  
 63      if (option == "-disable")
 64      {
 65        if (i + 1 >= argc)
 66          return false;                             // -disable requires an argument
 67  
 68        disable(argv[i + 1]);
 69  
 70        remove_option_argument(i, argc, argv);
 71      }
 72      else if (option == "-enable")
 73      {
 74        if (i + 1 >= argc)
 75          return false;                             // -enable requires an argument
 76  
 77        enable(argv[i + 1]);
 78  
 79        remove_option_argument(i, argc, argv);
 80      }
 81  #ifdef SQLITE
 82      else if (option == "-dump")
 83      {
 84        if (i + 1 >= argc)
 85          return false;                             // -dump requires an argument
 86  
 87        setenv("OUTFILE", argv[i + 1], 1);
 88        enable("output:db");
 89  
 90        remove_option_argument(i, argc, argv);
 91      }
 92  #endif
 93      else if ( (option == "-class") || (option == "-C") || (option == "-c"))
 94      {
 95        vector < string > classes;
 96  
 97        enable("output:list");
 98  
 99        if (i + 1 >= argc)
100          return false;                             // -class requires an argument
101  
102        splitlines(argv[i + 1], classes, ',');
103  
104        for (unsigned int j = 0; j < classes.size(); j++)
105          visible_classes.insert(getcname(classes[j].c_str()));
106  
107        remove_option_argument(i, argc, argv);
108      }
109      else
110        i++;
111    }
112  
113    return true;
114  }
115  
116  
117  bool enabled(const char *option)
118  {
119    return !(disabled(lowercase(option).c_str()));
120  }
121  
122  
123  bool disabled(const char *option)
124  {
125    return disabled_tests.find(lowercase(option)) != disabled_tests.end();
126  }
127  
128  
129  void enable(const char *option)
130  {
131    if (!disabled(option))
132      return;
133  
134    disabled_tests.erase(lowercase(option));
135  }
136  
137  
138  void disable(const char *option)
139  {
140    disabled_tests.insert(lowercase(option));
141  }
142  
143  
144  bool visible(const char *c)
145  {
146    if (visible_classes.size() == 0)
147      return true;
148    return visible_classes.find(getcname(c)) != visible_classes.end();
149  }