Fix coverity warnings in VOM and VAPI
[vpp.git] / src / vpp-api / vom / inspect.cpp
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <boost/algorithm/string.hpp>
17 #include <string>
18 #include <vector>
19
20 #include "vom/inspect.hpp"
21 #include "vom/logger.hpp"
22 #include "vom/om.hpp"
23
24 namespace VOM {
25 std::unique_ptr<std::map<std::string, inspect::command_handler*>>
26   inspect::m_cmd_handlers;
27
28 std::unique_ptr<std::deque<std::pair<std::vector<std::string>, std::string>>>
29   inspect::m_help_handlers;
30
31 void
32 inspect::handle_input(const std::string& message, std::ostream& output)
33 {
34   if (message.length()) {
35     if (message.find("help") != std::string::npos) {
36       output << "Command Options: " << std::endl;
37       output << "  keys              - Show all keys owning objects"
38              << std::endl;
39       output << "  key:XXX           - Show all object referenced by "
40                 "key XXX"
41              << std::endl;
42       output << "  all               - Show All objects" << std::endl;
43       output << "Individual object_base Types:" << std::endl;
44
45       for (auto h : *m_help_handlers) {
46         output << "  {";
47
48         for (auto s : h.first) {
49           output << s << " ";
50         }
51         output << "} - \t";
52         output << h.second;
53         output << std::endl;
54       }
55     } else if (message.find("keys") != std::string::npos) {
56       OM::dump(output);
57     } else if (message.find("key") != std::string::npos) {
58       std::vector<std::string> results;
59       boost::split(results, message, boost::is_any_of(":\n"));
60
61       OM::dump(results[1], output);
62     } else if (message.find("all") != std::string::npos) {
63       /*
64  * get the unique set of handlers, then invoke each
65  */
66       std::set<command_handler*> hdlrs;
67       for (auto h : *m_cmd_handlers) {
68         hdlrs.insert(h.second);
69       }
70       for (auto h : hdlrs) {
71         h->show(output);
72       }
73     } else {
74       auto it = m_cmd_handlers->find(message);
75
76       if (it != m_cmd_handlers->end()) {
77         it->second->show(output);
78       } else {
79         output << "Unknown Command: " << message << std::endl;
80       }
81     }
82   }
83 }
84
85 void
86 inspect::register_handler(const std::vector<std::string>& cmds,
87                           const std::string& help,
88                           command_handler* handler)
89 {
90   if (!m_cmd_handlers) {
91     m_cmd_handlers.reset(new std::map<std::string, command_handler*>);
92     m_help_handlers.reset(
93       new std::deque<std::pair<std::vector<std::string>, std::string>>);
94   }
95
96   for (auto cmd : cmds) {
97     (*m_cmd_handlers)[cmd] = handler;
98   }
99   m_help_handlers->push_front(std::make_pair(cmds, help));
100 }
101 }
102 /*
103  * fd.io coding-style-patch-verification: ON
104  *
105  * Local Variables:
106  * eval: (c-set-style "mozilla")
107  * End:
108  */