summaryrefslogtreecommitdiffstats
path: root/dwarflint/option.hh
blob: 5e4eb4877381778bc8a9b41ce3f92ef8eadcfbb5 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Pedantic checker for DWARF files
   Copyright (C) 2010, 2011 Red Hat, Inc.
   This file is part of Red Hat elfutils.

   Red Hat elfutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by the
   Free Software Foundation; version 2 of the License.

   Red Hat elfutils 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with Red Hat elfutils; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.

   Red Hat elfutils is an included package of the Open Invention Network.
   An included package of the Open Invention Network is a package for which
   Open Invention Network licensees cross-license their patents.  No patent
   license is granted, either expressly or impliedly, by designation as an
   included package.  Should you wish to participate in the Open Invention
   Network licensing program, please visit www.openinventionnetwork.com
   <http://www.openinventionnetwork.com>.  */

#ifndef DWARFLINT_OPTION_HH
#define DWARFLINT_OPTION_HH

#include <string>
#include <argp.h>
#include <map>
#include <vector>
#include <cassert>
#include <iostream>

#include "option_i.hh"
#include "checkdescriptor_i.hh"

class options
  : private std::map<int, option_i *>
{
  friend class argppp;
  mutable std::vector<argp_option> _m_opts;

  option_i *find_opt (int key) const;

public:
  option_i const *getopt (int key) const;
  argp build_argp (bool toplev = false) const;
  void add (option_i *opt);
  using std::map<int, option_i *>::empty;
  using std::map<int, option_i *>::begin;
  using std::map<int, option_i *>::end;
  using std::map<int, option_i *>::const_iterator;
};

// Wrapper of argp parsing.  While in general argp does a decent job,
// it's not possible to pass user arguments to the parser function
// from the argp structure itself, and therefore share the same parser
// for all the children.  Since that's what we need to do, we need to
// pass the "input" argument to argp_parse, and carefully setup the
// child_inputs arguments.  That means coordinating the whole parsing
// process from one place.  As a result this is hardly a nice,
// reusable piece of code.
class argppp
{
  std::vector<argp> _m_children_argps;
  std::vector<std::string> _m_children_headers;
  std::vector<argp_child> _m_children;
  std::vector<options const *> _m_children_inputs;
  argp _m_argp;
  bool _m_inited;

  static error_t parse_opt (int key, char *arg, argp_state *state);
  static argppp *instance;

public:
  argppp (options const &global);
  argppp (options const &global,
	  std::vector<checkdescriptor const *> checkdescriptors);

  void parse (int argc, char **argv, unsigned flags, int *remaining);
  void help (FILE *stream, unsigned flags, char *name);
};

class option_i // we cannot call it simply "option", this conflicts
	       // with another global declaration
{
  // last allocated option unique identifier
  static int _m_last_opt;

protected:
  // Answer either opt_short argument if it's non-0.  Otherwise create
  // new unique identifier.
  static int get_short_option (char opt_short);

public:
  virtual ~option_i () {}

  virtual bool seen () const = 0;
  virtual argp_option const &build_option () const = 0;
  virtual error_t parse_opt (char *arg, argp_state *state) = 0;
  virtual int key () const = 0;
};

class option_common
  : public option_i
{
  argp_option _m_opt;

protected:
  bool _m_seen;

  option_common (char const *description,
		 char const *arg_description,
		 char const *opt_long, char opt_short,
		 int flags = 0);

public:
  bool
  seen () const
  {
    return _m_seen;
  }

  argp_option const &
  build_option () const
  {
    return _m_opt;
  }

  int
  key () const
  {
    return _m_opt.key;
  }
};

template<class arg_type>
class value_converter;

template<class arg_type>
class xoption
  : public option_common
{
  arg_type _m_arg;

public:
  xoption (char const *description,
	   char const *arg_description,
	   char const *opt_long, char opt_short = 0,
	   int flags = 0)
    : option_common (description, arg_description, opt_long, opt_short, flags)
  {
  }

  arg_type const &value () const
  {
    return _m_arg;
  }

  arg_type const &value (arg_type arg)
  {
    return seen () ? _m_arg : arg;
  }

  error_t parse_opt (char *arg, __attribute__ ((unused)) argp_state *state)
  {
    _m_seen = true;
    _m_arg = value_converter<arg_type>::convert (arg);
    return 0;
  }
};

template<>
class xoption<void>
  : public option_common
{
public:
  xoption (char const *description,
	   char const *opt_long, char opt_short = 0, int flags = 0)
    : option_common (description, NULL, opt_long, opt_short, flags)
  {
  }

  error_t parse_opt (char *arg, __attribute__ ((unused)) argp_state *state)
  {
    assert (arg == NULL);
    _m_seen = true;
    return 0;
  }

  // This shouldn't be promoted to option_common, as
  // e.g. xoption<bool> will naturally have a different
  // implementation.
  operator bool () { return seen (); }
};

template<>
struct value_converter<std::string>
{
  static std::string convert (char const *arg)
  {
    if (arg == NULL)
      return "";
    else
      return arg;
  }
};

template<>
struct value_converter<unsigned>
{
  static unsigned convert (char const *arg)
  {
    unsigned u;
    if (std::sscanf (arg, "%u", &u) == 1)
      return u;
    else
      return -1;
  }
};

typedef xoption<void> void_option;
typedef xoption<std::string> string_option;
typedef xoption<unsigned> unsigned_option;

options & global_opts ();

template<class OPT>
struct global_opt
  : public OPT
{
  template<typename... Args>
  global_opt (Args const&... args)
    : OPT (args...)
  {
    global_opts ().add (this);
  }
};

#endif//DWARFLINT_OPTION_HH