summaryrefslogtreecommitdiff
path: root/option.cc
blob: 82128318507d80dbcc62365417b2308b0d45e35c (plain)
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
146
147
148
149
150
151
152
153
154
155
156
#include "option.h"
#include "debug.h"
#include "util.h"

#include <iostream>
#include <cstdarg>

option_handler::option_handler(const std::string& label,
			       const std::string& opt_chars)
  : handled(false)
{
  DEBUG_PRINT("ledger.memory.ctors", "ctor option_handler");

  option_t opt;

  static char buf[128];
  char * p = buf;
  for (const char * q = label.c_str(); *q; q++)
    if (*q == '_')
      *p++ = '-';
    else
      *p++ = *q;
  assert(p < buf + 127);
  *p = '\0';
  opt.long_opt = buf;

  handlers.insert(option_handler_pair(opt.long_opt, this));

  if (! opt_chars.empty()) {
    if (opt_chars[0] != ':')
      opt.short_opt = opt_chars[0];

    if (opt_chars[opt_chars.length() - 1] == ':')
      opt.wants_arg = true;
  }

  opt.handler = this;

  options.push_back(opt);
}

static inline void process_option(const option_t& opt,
				  const char * arg = NULL) {
  if (! opt.handler->handled) {
    opt.handler->handle_option(arg);
    opt.handler->handled = true;
  }
}

bool process_option(const std::string& opt, const char * arg)
{
  option_handler_map::iterator handler = option_handler::handlers.find(opt);
  if (handler != option_handler::handlers.end()) {
    if (! (*handler).second->handled) {
      (*handler).second->handle_option(arg);
      (*handler).second->handled = true;
    }
    return true;
  }
  return false;
}

void process_arguments(int argc, char ** argv, const bool anywhere,
		       std::list<std::string>& args)
{
  int index = 1;
  for (char ** i = argv + 1; index < argc; i++, index++) {
    if ((*i)[0] != '-') {
      if (anywhere) {
	args.push_back(*i);
	continue;
      } else {
	for (; index < argc; i++, index++)
	  args.push_back(*i);
	break;
      }
    }

    // --long-option
   again:
    if ((*i)[1] == '-') {
      if ((*i)[2] == '\0')
	break;

      for (std::vector<option_t>::iterator j
	     = option_handler::options.begin();
	   j != option_handler::options.end();
	   j++)
	if ((*j).wants_arg) {
	  if (const char * p = std::strchr(*i + 2, '=')) {
	    if ((*j).long_opt == std::string(*i + 2, p - (*i + 2))) {
	      process_option(*j, p + 1);
	      goto next;
	    }
	  }
	  else if ((*j).long_opt == *i + 2) {
	    process_option(*j, argv[++index]);
	    i++;
	    goto next;
	  }
	}
	else if ((*j).long_opt == *i + 2) {
	  process_option(*j);
	  goto next;
	}

      std::cerr << "Error: illegal option " << *i << std::endl;
      std::exit(1);
    } else {
      for (std::vector<option_t>::iterator j
	     = option_handler::options.begin();
	   j != option_handler::options.end();
	   j++)
	if ((*i)[1] == (*j).short_opt) {
	  if ((*j).wants_arg) {
	    process_option(*j, argv[++index]);
	    i++;
	    goto next;
	  } else {
	    process_option(*j);
	    if ((*i)[2]) {
	      std::strcpy(*i + 1, *i + 2);
	      goto again;
	    }
	    goto next;
	  }
	}

      std::cerr << "Error: illegal option -- " << (*i)[1] << std::endl;
      std::exit(1);
    }

   next:
    ;
  }
}

void process_environment(char ** envp, const std::string& tag)
{
  for (char ** p = envp; *p; p++)
    if (std::strncmp(*p, tag.c_str(), 7) == 0) {
      char * q;
      static char buf[128];
      char * r = buf;
      for (q = *p + 7; *q && *q != '='; q++)
	if (*q == '_')
	  *r++ += '-';
	else
	  *r++ += std::tolower(*q);
      assert(r < buf + 127);
      *r = '\0';

      if (*q == '=')
	process_option(buf, q + 1);
    }
}