monotone

monotone Mtn Source Tree

Root/cmd.hh

1#ifndef __CMD_HH__
2#define __CMD_HH__
3
4// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
5//
6// This program is made available under the GNU GPL version 2.0 or
7// greater. See the accompanying file COPYING for details.
8//
9// This program is distributed WITHOUT ANY WARRANTY; without even the
10// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11// PURPOSE.
12
13#include <map>
14#include <set>
15
16#include "commands.hh"
17#include "options.hh"
18#include "sanity.hh"
19
20class app_state;
21
22namespace commands
23{
24 class command
25 {
26 public:
27 typedef std::set< utf8 > names_set;
28 typedef std::set< command * > children_set;
29
30 private:
31 // NB: these strings are stored _un_translated, because we cannot
32 // translate them until after main starts, by which time the
33 // command objects have all been constructed.
34 utf8 m_primary_name;
35 names_set m_names;
36 command * m_parent;
37 bool m_hidden;
38 utf8 m_params;
39 utf8 m_abstract;
40 utf8 m_desc;
41 bool m_use_workspace_options;
42 options::options_type m_opts;
43 children_set m_children;
44 bool m_allow_completion;
45
46 std::map< command_id, command * >
47 find_completions(utf8 const & prefix, command_id const & completed,
48 bool completion_ok = true) const;
49 command * find_child_by_name(utf8 const & name) const;
50
51 bool allow_completion() const;
52 public:
53 command(std::string const & primary_name,
54 std::string const & other_names,
55 command * parent,
56 bool hidden,
57 std::string const & params,
58 std::string const & abstract,
59 std::string const & desc,
60 bool use_workspace_options,
61 options::options_type const & opts,
62 bool allow_completion);
63
64 virtual ~command(void);
65
66 command_id ident(void) const;
67
68 utf8 const & primary_name(void) const;
69 names_set const & names(void) const;
70 command * parent(void) const;
71 bool hidden(void) const;
72 virtual std::string params(void) const;
73 virtual std::string abstract(void) const;
74 virtual std::string desc(void) const;
75 options::options_type const & opts(void) const;
76 bool use_workspace_options(void) const;
77 children_set & children(void);
78 children_set const & children(void) const;
79 bool is_leaf(void) const;
80
81 bool operator<(command const & cmd) const;
82
83 virtual void exec(app_state & app,
84 command_id const & execid,
85 args_vector const & args) const = 0;
86
87 bool has_name(utf8 const & name) const;
88 command const * find_command(command_id const & id) const;
89 std::set< command_id >
90 complete_command(command_id const & id,
91 command_id completed = command_id(),
92 bool completion_ok = true) const;
93 };
94
95 class automate : public command
96 {
97 // This function is supposed to be called only after the requirements
98 // for "automate" commands have been fulfilled. This is done by the
99 // "exec" function defined below, which implements code shared among
100 // all automation commands. Also, this is also needed by the "stdio"
101 // automation, as it executes multiple of these commands sharing the
102 // same initialization, hence the friend declaration.
103 virtual void exec_from_automate(args_vector args,
104 command_id const & execid,
105 app_state & app,
106 std::ostream & output) const = 0;
107 friend class automate_stdio;
108
109 public:
110 automate(std::string const & name,
111 std::string const & params,
112 std::string const & abstract,
113 std::string const & desc,
114 options::options_type const & opts);
115
116 void exec(app_state & app,
117 command_id const & execid,
118 args_vector const & args) const;
119 };
120};
121
122inline std::vector<file_path>
123args_to_paths(args_vector const & args)
124{
125 std::vector<file_path> paths;
126 for (args_vector::const_iterator i = args.begin(); i != args.end(); ++i)
127 {
128 if (bookkeeping_path::external_string_is_bookkeeping_path(*i))
129 W(F("ignored bookkeeping path '%s'") % *i);
130 else
131 paths.push_back(file_path_external(*i));
132 }
133 // "it should not be the case that args were passed, but our paths set
134 // ended up empty". This test is because some commands have default
135 // behavior for empty path sets -- in particular, it is the same as having
136 // no restriction at all. "mtn revert _MTN" turning into "mtn revert"
137 // would be bad. (Or substitute diff, etc.)
138 N(!(!args.empty() && paths.empty()),
139 F("all arguments given were bookkeeping paths; aborting"));
140 return paths;
141}
142
143std::string
144describe_revision(app_state & app,
145 revision_id const & id);
146
147void
148complete(app_state & app,
149 std::string const & str,
150 revision_id & completion,
151 bool must_exist=true);
152
153void
154complete(app_state & app,
155 std::string const & str,
156 std::set<revision_id> & completion,
157 bool must_exist=true);
158
159void
160notify_if_multiple_heads(app_state & app);
161
162void
163process_commit_message_args(bool & given,
164 utf8 & log_message,
165 app_state & app,
166 utf8 message_prefix = utf8(""));
167
168#define CMD_FWD_DECL(C) \
169namespace commands { \
170 class cmd_ ## C; \
171 extern cmd_ ## C C ## _cmd; \
172}
173
174#define CMD_REF(C) ((commands::command *)&(commands::C ## _cmd))
175
176#define _CMD2(C, name, aliases, parent, hidden, params, abstract, desc, opts) \
177namespace commands { \
178 class cmd_ ## C : public command \
179 { \
180 public: \
181 cmd_ ## C() : command(name, aliases, parent, hidden, params, \
182 abstract, desc, true, \
183 options::options_type() | opts, true) \
184 {} \
185 virtual void exec(app_state & app, \
186 command_id const & execid, \
187 args_vector const & args) const; \
188 }; \
189 cmd_ ## C C ## _cmd; \
190} \
191void commands::cmd_ ## C::exec(app_state & app, \
192 command_id const & execid, \
193 args_vector const & args) const
194
195#define CMD(C, name, aliases, parent, params, abstract, desc, opts) \
196 _CMD2(C, name, aliases, parent, false, params, abstract, desc, opts)
197
198#define CMD_HIDDEN(C, name, aliases, parent, params, abstract, desc, opts) \
199 _CMD2(C, name, aliases, parent, true, params, abstract, desc, opts)
200
201#define _CMD_GROUP2(C, name, aliases, parent, abstract, desc, cmpl) \
202 namespace commands { \
203 class cmd_ ## C : public command \
204 { \
205 public: \
206 cmd_ ## C() : command(name, aliases, parent, false, "", abstract,\
207 desc, true, \
208 options::options_type(), cmpl) \
209 {} \
210 virtual void exec(app_state & app, \
211 command_id const & execid, \
212 args_vector const & args) const; \
213 }; \
214 cmd_ ## C C ## _cmd; \
215} \
216void commands::cmd_ ## C::exec(app_state & app, \
217 command_id const & execid, \
218 args_vector const & args) const \
219{ \
220 I(false); \
221}
222
223#define CMD_GROUP(C, name, aliases, parent, abstract, desc) \
224 _CMD_GROUP2(C, name, aliases, parent, abstract, desc, true)
225
226#define CMD_GROUP_NO_COMPLETE(C, name, aliases, parent, abstract, desc) \
227 _CMD_GROUP2(C, name, aliases, parent, abstract, desc, false)
228
229// Use this for commands that should specifically _not_ look for an
230// _MTN dir and load options from it.
231
232#define CMD_NO_WORKSPACE(C, name, aliases, parent, params, abstract, \
233 desc, opts) \
234namespace commands { \
235 class cmd_ ## C : public command \
236 { \
237 public: \
238 cmd_ ## C() : command(name, aliases, parent, false, params, \
239 abstract, desc, false, \
240 options::options_type() | opts, true) \
241 {} \
242 virtual void exec(app_state & app, \
243 command_id const & execid, \
244 args_vector const & args) const; \
245 }; \
246 cmd_ ## C C ## _cmd; \
247} \
248void commands::cmd_ ## C::exec(app_state & app, \
249 command_id const & execid, \
250 args_vector const & args) const
251
252// TODO: 'abstract' and 'desc' should be refactored so that the
253// command definition allows the description of input/output format,
254// error conditions, version when added, etc. 'desc' can later be
255// automatically built from these.
256#define CMD_AUTOMATE(C, params, abstract, desc, opts) \
257namespace commands { \
258 class automate_ ## C : public automate \
259 { \
260 void exec_from_automate(args_vector args, \
261 command_id const & execid, \
262 app_state & app, \
263 std::ostream & output) const; \
264 public: \
265 automate_ ## C() : automate(#C, params, abstract, desc, \
266 options::options_type() | opts) \
267 {} \
268 }; \
269 automate_ ## C C ## _automate; \
270} \
271void commands::automate_ ## C :: exec_from_automate \
272 (args_vector args, \
273 command_id const & execid, \
274 app_state & app, \
275 std::ostream & output) const
276
277CMD_FWD_DECL(__root__);
278
279CMD_FWD_DECL(automation);
280CMD_FWD_DECL(database);
281CMD_FWD_DECL(debug);
282CMD_FWD_DECL(informative);
283CMD_FWD_DECL(key_and_cert);
284CMD_FWD_DECL(network);
285CMD_FWD_DECL(packet_io);
286CMD_FWD_DECL(rcs);
287CMD_FWD_DECL(review);
288CMD_FWD_DECL(tree);
289CMD_FWD_DECL(variables);
290CMD_FWD_DECL(workspace);
291
292// Local Variables:
293// mode: C++
294// fill-column: 76
295// c-file-style: "gnu"
296// indent-tabs-mode: nil
297// End:
298// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
299
300#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status