]> git.stg.codes - stg.git/blob - projects/sgconf/main.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / sgconf / main.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include "xml.h"
23 #include "admins.h"
24 #include "tariffs.h"
25 #include "users.h"
26 #include "services.h"
27 #include "corps.h"
28 #include "info.h"
29
30 #include "api_action.h"
31 #include "options.h"
32 #include "actions.h"
33 #include "config.h"
34
35 #include <string>
36 #include <iostream>
37
38 #include <cstdlib> // getenv
39 #include <cstring> // str*
40
41 #include <unistd.h> // access
42 #include <libgen.h> // basename
43
44 namespace
45 {
46
47 template <typename T>
48 struct nullary_function
49 {
50 typedef T result_type;
51 };
52
53 template <typename F>
54 class binder0 : public nullary_function<typename F::result_type>
55 {
56     public:
57         binder0(const F & func, const typename F::argument_type & arg)
58             : m_func(func), m_arg(arg) {}
59         typename F::result_type operator()() const { return m_func(m_arg); }
60     private:
61         F m_func;
62         typename F::argument_type m_arg;
63 };
64
65 template <typename F>
66 inline
67 binder0<F> bind0(const F & func, const typename F::argument_type & arg)
68 {
69 return binder0<F>(func, arg);
70 }
71
72 template <typename A, typename R>
73 class FUNC1_ADAPTER : public std::unary_function<A, R>
74 {
75     public:
76         explicit FUNC1_ADAPTER(R (*func)(A)) : m_func(func) {}
77         const R operator()(A arg) const { return (m_func)(arg); }
78     private:
79         R (*m_func)(A);
80 };
81
82 template <typename C, typename A, typename R>
83 class METHOD1_ADAPTER : public std::unary_function<A, R>
84 {
85     public:
86         METHOD1_ADAPTER(R (C::* func)(A), C & obj) : m_func(func), m_obj(obj) {}
87         R operator()(A arg) { return (m_obj.*m_func)(arg); }
88     private:
89         R (C::* m_func)(A);
90         C & m_obj;
91 };
92
93 template <typename C, typename A, typename R>
94 class CONST_METHOD1_ADAPTER : public std::unary_function<A, R>
95 {
96     public:
97         CONST_METHOD1_ADAPTER(R (C::* func)(A) const, C & obj) : m_func(func), m_obj(obj) {}
98         R operator()(A arg) const { return (m_obj.*m_func)(arg); }
99     private:
100         R (C::* m_func)(A) const;
101         C & m_obj;
102 };
103
104 template <typename A, typename R>
105 FUNC1_ADAPTER<A, R> Func1Adapt(R (func)(A))
106 {
107 return FUNC1_ADAPTER<A, R>(func);
108 }
109
110 template <typename C, typename A, typename R>
111 METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A), C & obj)
112 {
113 return METHOD1_ADAPTER<C, A, R>(func, obj);
114 }
115
116 template <typename C, typename A, typename R>
117 CONST_METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A) const, C & obj)
118 {
119 return CONST_METHOD1_ADAPTER<C, A, R>(func, obj);
120 }
121
122 void Version(const std::string & self)
123 {
124 std::cout << self << ", version: 2.0.0.\n";
125 }
126
127 void ReadUserConfigFile(SGCONF::OPTION_BLOCK & block)
128 {
129 std::vector<std::string> paths;
130 const char * configHome = getenv("XDG_CONFIG_HOME");
131 if (configHome == NULL)
132     {
133     const char * home = getenv("HOME");
134     if (home == NULL)
135         return;
136     paths.push_back(std::string(home) + "/.config/sgconf/sgconf.conf");
137     paths.push_back(std::string(home) + "/.sgconf/sgconf.conf");
138     }
139 else
140     paths.push_back(std::string(configHome) + "/sgconf/sgconf.conf");
141 for (std::vector<std::string>::const_iterator it = paths.begin(); it != paths.end(); ++it)
142     if (access(it->c_str(), R_OK) == 0)
143         {
144         block.ParseFile(*it);
145         return;
146         }
147 }
148
149 } // namespace anonymous
150
151 namespace SGCONF
152 {
153
154 class CONFIG_ACTION : public ACTION
155 {
156     public:
157         CONFIG_ACTION(SGCONF::CONFIG & config,
158                       const std::string & paramDescription)
159             : m_config(config),
160               m_description(paramDescription)
161         {}
162
163         std::string ParamDescription() const override { return m_description; }
164         std::string DefaultDescription() const override { return ""; }
165         OPTION_BLOCK & Suboptions() override { return m_suboptions; }
166         PARSER_STATE Parse(int argc, char ** argv, void * /*data*/) override;
167
168     private:
169         SGCONF::CONFIG & m_config;
170         std::string m_description;
171         OPTION_BLOCK m_suboptions;
172
173         void ParseCredentials(const std::string & credentials);
174         void ParseHostAndPort(const std::string & hostAndPort);
175 };
176
177
178 PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv, void * /*data*/)
179 {
180 if (argc == 0 ||
181     argv == NULL ||
182     *argv == NULL)
183     throw ERROR("Missing argument.");
184 char * pos = strchr(*argv, '@');
185 if (pos != NULL)
186     {
187     ParseCredentials(std::string(*argv, pos));
188     ParseHostAndPort(std::string(pos + 1));
189     }
190 else
191     {
192     ParseHostAndPort(std::string(*argv));
193     }
194 return PARSER_STATE(false, --argc, ++argv);
195 }
196
197 void CONFIG_ACTION::ParseCredentials(const std::string & credentials)
198 {
199 std::string::size_type pos = credentials.find_first_of(':');
200 if (pos != std::string::npos)
201     {
202     m_config.userName = credentials.substr(0, pos);
203     m_config.userPass = credentials.substr(pos + 1);
204     }
205 else
206     {
207     m_config.userName = credentials;
208     }
209 }
210
211 void CONFIG_ACTION::ParseHostAndPort(const std::string & hostAndPort)
212 {
213 std::string::size_type pos = hostAndPort.find_first_of(':');
214 if (pos != std::string::npos)
215     {
216     m_config.server = hostAndPort.substr(0, pos);
217     uint16_t port = 0;
218     if (str2x(hostAndPort.substr(pos + 1), port))
219         throw ERROR("Invalid port value: '" + hostAndPort.substr(pos + 1) + "'");
220     m_config.port = port;
221     }
222 else
223     {
224     m_config.server = hostAndPort;
225     }
226 }
227
228 std::unique_ptr<SGCONF::ACTION> MakeParamAction(SGCONF::CONFIG & config,
229                                                 const std::string & paramDescription)
230 {
231 return std::make_unique<CONFIG_ACTION>(config, paramDescription);
232 }
233
234 } // namespace SGCONF
235
236 //-----------------------------------------------------------------------------
237 int main(int argc, char **argv)
238 {
239     std::string self(basename(argv[0]));
240     SGCONF::CONFIG config;
241     SGCONF::COMMANDS commands;
242
243     SGCONF::OPTION_BLOCKS blocks;
244     blocks.Add("General options")
245           .Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), "<config file>"), "override default config file")
246           .Add("h", "help", SGCONF::MakeFunc0Action(bind0(Method1Adapt(&SGCONF::OPTION_BLOCKS::Help, blocks), 0)), "\t\tshow this help and exit")
247           //.Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit")
248           .Add("v", "version", SGCONF::MakeFunc0Action(bind0(Func1Adapt(Version), self)), "\t\tshow version information and exit");
249     SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options")
250           .Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "<address>"), "\t\thost to connect")
251           .Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), "<port>"), "\t\tport to connect")
252           .Add("local-address", SGCONF::MakeParamAction(config.localAddress, std::string(""), "<address>"), "\tlocal address to bind")
253           .Add("local-port", SGCONF::MakeParamAction(config.localPort, uint16_t(0), "<port>"), "\t\tlocal port to bind")
254           .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "\tadministrative login")
255           .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, "<password>"), "\tpassword for the administrative login")
256           .Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
257     blocks.Add("Debug options")
258           .Add("show-config", SGCONF::MakeParamAction(config.showConfig), "\tshow config and exit");
259     SGCONF::AppendXMLOptionBlock(commands, blocks);
260     SGCONF::AppendServerInfoBlock(commands, blocks);
261     SGCONF::AppendAdminsOptionBlock(commands, blocks);
262     SGCONF::AppendTariffsOptionBlock(commands, blocks);
263     SGCONF::AppendUsersOptionBlock(commands, blocks);
264     SGCONF::AppendServicesOptionBlock(commands, blocks);
265     SGCONF::AppendCorpsOptionBlock(commands, blocks);
266
267     SGCONF::PARSER_STATE state(false, argc, argv);
268
269     try
270     {
271         state = blocks.Parse(--argc, ++argv); // Skipping self name
272     }
273     catch (const SGCONF::OPTION::ERROR& ex)
274     {
275         std::cerr << ex.what() << "\n";
276         return -1;
277     }
278
279     if (state.stop)
280         return 0;
281
282     if (state.argc > 0)
283     {
284         std::cerr << "Unknown option: '" << *state.argv << "'\n";
285         return -1;
286     }
287
288     try
289     {
290         // Preserve config values parsed from the command line
291         SGCONF::CONFIG configOverride(config);
292
293         if (!config.configFile)
294         {
295             // Read main config file.
296             const char * mainConfigFile = "/etc/sgconf/sgconf.conf";
297             if (access(mainConfigFile, R_OK) == 0)
298                 block.ParseFile(mainConfigFile);
299             // Read XDG-stuff.
300             ReadUserConfigFile(block);
301         }
302         else
303         {
304             // Read user-supplied file.
305             block.ParseFile(config.configFile.value());
306         }
307
308         // Apply overrides from the command line
309         config.splice(configOverride);
310
311         if (config.showConfig && config.showConfig.value())
312         {
313             std::cout << config.Serialize() << std::endl;
314             return 0;
315         }
316         return commands.Execute(config) ? 0 : -1;
317     }
318     catch (const std::exception& ex)
319     {
320         std::cerr << ex.what() << "\n";
321         return -1;
322     }
323 }