]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/cmdparse.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 Inktank Storage, Inc.
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License version 2, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "json_spirit/json_spirit.h"
16 #include "common/debug.h"
21 * Given a cmddesc like "foo baz name=bar,type=CephString",
22 * return the prefix "foo baz".
24 std::string
cmddesc_get_prefix(const std::string
&cmddesc
)
26 stringstream
ss(cmddesc
);
28 std::ostringstream result
;
30 while (std::getline(ss
, word
, ' ')) {
31 if (word
.find_first_of(",=") != string::npos
) {
46 * Read a command description list out of cmd, and dump it to f.
47 * A signature description is a set of space-separated words;
48 * see MonCommands.h for more info.
52 dump_cmd_to_json(Formatter
*f
, const string
& cmd
)
54 // put whole command signature in an already-opened container
55 // elements are: "name", meaning "the typeless name that means a literal"
56 // an object {} with key:value pairs representing an argument
61 while (std::getline(ss
, word
, ' ')) {
62 // if no , or =, must be a plain word to put out
63 if (word
.find_first_of(",=") == string::npos
) {
64 f
->dump_string("arg", word
);
67 // Snarf up all the key=val,key=val pairs, put 'em in a dict.
68 // no '=val' implies '=True'.
69 std::stringstream
argdesc(word
);
71 std::map
<std::string
, std::string
>desckv
;
72 // accumulate descriptor keywords in desckv
74 while (std::getline(argdesc
, keyval
, ',')) {
75 // key=value; key by itself implies value is bool true
76 // name="name" means arg dict will be titled 'name'
77 size_t pos
= keyval
.find('=');
79 if (pos
!= std::string::npos
) {
80 key
= keyval
.substr(0, pos
);
81 val
= keyval
.substr(pos
+1);
86 desckv
.insert(std::pair
<std::string
, std::string
> (key
, val
));
88 // name the individual desc object based on the name key
89 f
->open_object_section(desckv
["name"].c_str());
90 // dump all the keys including name into the array
91 for (std::map
<std::string
, std::string
>::iterator it
= desckv
.begin();
92 it
!= desckv
.end(); ++it
) {
93 f
->dump_string(it
->first
.c_str(), it
->second
);
95 f
->close_section(); // attribute object for individual desc
100 dump_cmd_and_help_to_json(Formatter
*jf
,
101 const string
& secname
,
102 const string
& cmdsig
,
103 const string
& helptext
)
105 jf
->open_object_section(secname
.c_str());
106 jf
->open_array_section("sig");
107 dump_cmd_to_json(jf
, cmdsig
);
108 jf
->close_section(); // sig array
109 jf
->dump_string("help", helptext
.c_str());
110 jf
->close_section(); // cmd
114 dump_cmddesc_to_json(Formatter
*jf
,
115 const string
& secname
,
116 const string
& cmdsig
,
117 const string
& helptext
,
118 const string
& module
,
123 jf
->open_object_section(secname
.c_str());
124 jf
->open_array_section("sig");
125 dump_cmd_to_json(jf
, cmdsig
);
126 jf
->close_section(); // sig array
127 jf
->dump_string("help", helptext
.c_str());
128 jf
->dump_string("module", module
.c_str());
129 jf
->dump_string("perm", perm
.c_str());
130 jf
->dump_string("avail", avail
.c_str());
131 jf
->dump_int("flags", flags
);
132 jf
->close_section(); // cmd
135 void cmdmap_dump(const cmdmap_t
&cmdmap
, Formatter
*f
)
137 assert(f
!= nullptr);
139 class dump_visitor
: public boost::static_visitor
<void>
142 std::string
const &key
;
144 dump_visitor(Formatter
*f_
, std::string
const &key_
)
149 void operator()(const std::string
&operand
) const
151 f
->dump_string(key
.c_str(), operand
);
154 void operator()(const bool &operand
) const
156 f
->dump_bool(key
.c_str(), operand
);
159 void operator()(const int64_t &operand
) const
161 f
->dump_int(key
.c_str(), operand
);
164 void operator()(const double &operand
) const
166 f
->dump_float(key
.c_str(), operand
);
169 void operator()(const std::vector
<std::string
> &operand
) const
171 f
->open_array_section(key
.c_str());
172 for (const auto i
: operand
) {
173 f
->dump_string("item", i
);
178 void operator()(const std::vector
<int64_t> &operand
) const
180 f
->open_array_section(key
.c_str());
181 for (const auto i
: operand
) {
182 f
->dump_int("item", i
);
187 void operator()(const std::vector
<double> &operand
) const
189 f
->open_array_section(key
.c_str());
190 for (const auto i
: operand
) {
191 f
->dump_float("item", i
);
197 //f->open_object_section("cmdmap");
198 for (const auto &i
: cmdmap
) {
199 boost::apply_visitor(dump_visitor(f
, i
.first
), i
.second
);
201 //f->close_section();
205 /** Parse JSON in vector cmd into a map from field to map of values
206 * (use mValue/mObject)
207 * 'cmd' should not disappear over lifetime of map
208 * 'mapp' points to the caller's map
209 * 'ss' captures any errors during JSON parsing; if function returns
210 * false, ss is valid */
213 cmdmap_from_json(vector
<string
> cmd
, map
<string
, cmd_vartype
> *mapp
, stringstream
&ss
)
215 json_spirit::mValue v
;
218 // First, join all cmd strings
219 for (vector
<string
>::iterator it
= cmd
.begin();
220 it
!= cmd
.end(); ++it
)
224 if (!json_spirit::read(fullcmd
, v
))
225 throw runtime_error("unparseable JSON " + fullcmd
);
226 if (v
.type() != json_spirit::obj_type
)
227 throw(runtime_error("not JSON object " + fullcmd
));
229 // allocate new mObject (map) to return
230 // make sure all contents are simple types (not arrays or objects)
231 json_spirit::mObject o
= v
.get_obj();
232 for (map
<string
, json_spirit::mValue
>::iterator it
= o
.begin();
233 it
!= o
.end(); ++it
) {
235 // ok, marshal it into our string->cmd_vartype map, or throw an
236 // exception if it's not a simple datatype. This is kind of
237 // annoying, since json_spirit has a boost::variant inside it
238 // already, but it's not public. Oh well.
240 switch (it
->second
.type()) {
242 case json_spirit::obj_type
:
244 throw(runtime_error("JSON array/object not allowed " + fullcmd
));
247 case json_spirit::array_type
:
249 // array is a vector of values. Unpack it to a vector
250 // of strings, doubles, or int64_t, the only types we handle.
251 const vector
<json_spirit::mValue
>& spvals
= it
->second
.get_array();
252 if (spvals
.empty()) {
253 // if an empty array is acceptable, the caller should always check for
254 // vector<string> if the expected value of "vector<int64_t>" in the
255 // cmdmap is missing.
256 (*mapp
)[it
->first
] = vector
<string
>();
257 } else if (spvals
.front().type() == json_spirit::str_type
) {
259 for (const auto& sv
: spvals
) {
260 if (sv
.type() != json_spirit::str_type
) {
261 throw(runtime_error("Can't handle arrays of multiple types"));
263 outv
.push_back(sv
.get_str());
265 (*mapp
)[it
->first
] = std::move(outv
);
266 } else if (spvals
.front().type() == json_spirit::int_type
) {
267 vector
<int64_t> outv
;
268 for (const auto& sv
: spvals
) {
269 if (spvals
.front().type() != json_spirit::int_type
) {
270 throw(runtime_error("Can't handle arrays of multiple types"));
272 outv
.push_back(sv
.get_int64());
274 (*mapp
)[it
->first
] = std::move(outv
);
275 } else if (spvals
.front().type() == json_spirit::real_type
) {
277 for (const auto& sv
: spvals
) {
278 if (spvals
.front().type() != json_spirit::real_type
) {
279 throw(runtime_error("Can't handle arrays of multiple types"));
281 outv
.push_back(sv
.get_real());
283 (*mapp
)[it
->first
] = std::move(outv
);
285 throw(runtime_error("Can't handle arrays of types other than "
286 "int, string, or double"));
290 case json_spirit::str_type
:
291 (*mapp
)[it
->first
] = it
->second
.get_str();
294 case json_spirit::bool_type
:
295 (*mapp
)[it
->first
] = it
->second
.get_bool();
298 case json_spirit::int_type
:
299 (*mapp
)[it
->first
] = it
->second
.get_int64();
302 case json_spirit::real_type
:
303 (*mapp
)[it
->first
] = it
->second
.get_real();
308 } catch (runtime_error
&e
) {
314 class stringify_visitor
: public boost::static_visitor
<string
>
317 template <typename T
>
318 string
operator()(T
&operand
) const
327 cmd_vartype_stringify(const cmd_vartype
&v
)
329 return boost::apply_visitor(stringify_visitor(), v
);
334 handle_bad_get(CephContext
*cct
, const string
& k
, const char *tname
)
336 ostringstream errstr
;
338 const char *typestr
= abi::__cxa_demangle(tname
, 0, 0, &status
);
341 errstr
<< "bad boost::get: key " << k
<< " is not type " << typestr
;
342 lderr(cct
) << errstr
.str() << dendl
;
346 lderr(cct
) << oss
.rdbuf() << dendl
;
348 free((char *)typestr
);
351 long parse_pos_long(const char *s
, std::ostream
*pss
)
353 if (*s
== '-' || *s
== '+') {
355 *pss
<< "expected numerical value, got: " << s
;
360 long r
= strict_strtol(s
, 10, &err
);
361 if ((r
== 0) && !err
.empty()) {
368 *pss
<< "unable to parse positive integer '" << s
<< "'";
374 int parse_osd_id(const char *s
, std::ostream
*pss
)
377 if (strncmp(s
, "osd.", 4) == 0) {
383 long id
= parse_pos_long(s
, &ss
);
389 *pss
<< "osd id " << id
<< " is too large";