--- /dev/null
+/*
+ * This program 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*
+ * Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_SGCONF_COMPOSER_H__
+#define __STG_SGCONF_COMPOSER_H__
+
+#include "parser_state.h"
+
+namespace SGCONF
+{
+
+class COMPOSER
+{
+ public:
+ typedef PARSER_STATE (* FUNC)(int, char **, CONFIG&);
+ COMPOSER(int argc, char ** argv)
+ : m_done(false), m_result(0)
+ { mstate.argc = argc; m_state.argv = argv; }
+ COMPOSER compose(FUNC func)
+ {
+ if (m_done)
+ return COMPOSER(m_result);
+ try
+ {
+ PARSER_STATE state(func(m_state.argc, m_state.argv, m_state.config));
+ if (state.result)
+ return COMPOSER(0);
+ else
+ return COMPOSER(state);
+ }
+ catch (const PARSER_ERROR& ex)
+ {
+ std::cerr << ex.what() << "\n";
+ return COMPOSER(-1);
+ }
+ }
+ int exec()
+ {
+ if (m_done)
+ return m_result;
+ Usage();
+ return -1;
+ }
+
+ private:
+ bool m_done;
+ int m_result;
+ PARSER_STATE m_state;
+
+ COMPOSER(int result)
+ : m_done(true),
+ m_result(result)
+ {
+ }
+
+ COMPOSER(const PARSER_STATE& state)
+ : m_done(false),
+ m_result(0),
+ m_state(state)
+};
+
+}
--- /dev/null
+/*
+ * This program 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*
+ * Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_SGCONF_CONFIG_H__
+#define __STG_SGCONF_CONFIG_H__
+
+#include "stg/resetable.h"
+#include "stg/os_int.h"
+
+#include <string>
+
+namespace SGCONF
+{
+
+struct CONFIG
+{
+ RESETABLE<std::string> configFile;
+ RESETABLE<std::string> server;
+ RESETABLE<uint16_t> port;
+ RESETABLE<std::string> userName;
+ RESETABLE<std::string> userPass;
+};
+
+}
return ProcessSetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), conf, stat);
}
//-----------------------------------------------------------------------------
+PARSER_STATE TryParse(const PARSERS& parsers, char ** argv, int argc)
+{
+PARSERS::const_iterator it = parsers.find(*argv);
+if (it != parsers.end())
+ return it->second(++argv, --argc);
+PARSER_STATE state;
+state.argc = argc;
+state.argv = argv;
+state.result = false;
+return state;
+}
+//-----------------------------------------------------------------------------
+PARSER_STATE ParseCommon(int argc, char ** argv, CONFIG& config)
+{
+if (pos == 0)
+ ++pos;
+
+PARSERS parsers;
+parsers.add<std::string>("-c", "--config", config.configFile);
+parsers.add<void>("-h", "--help", Usage, false);
+parsers.add<void>("--help-all", Usage, true);
+parsers.add<void>("-v", "--version", Version);
+
+while (true)
+ {
+ PARSER_STATE state(TryParse(parsers, argv, argc, config));
+ if (state.argv == argv)
+ return state; // No-op
+ if (state.argc == 0)
+ return state; // EOF
+ if (state.result)
+ return state; // Done
+ argv = state.argv;
+ argc = state.argc;
+ }
+
+assert(0 && "Can't be here.");
+return PARSER_STATE();
+}
+//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
Usage(true);
exit(0);
+
+// Ok - succesfully parsed
+// Done - don't continue, return 0
+// Error - don't continue, return -1
+// No-op - nothing changed
+
+return COMPOSER(argv).compose(ParseCommon)
+ .compose(ReadConfig)
+ .compose(ParseCommand)
+ .exec();
+
+
+if (argc < 2)
+ {
+ // TODO: no arguments
+ Usage(false);
+ return 1;
+ }
+
if (argc <= 2)
{
UsageConf();
<< "General options:\n"
<< "\t-c, --config <config file>\t\toverride default config file (default: \"~/.config/stg/sgconf.conf\")\n"
<< "\t-h, --help\t\t\t\tshow this help and exit\n"
- << "\t-h, --help-all\t\t\t\tshow full help and exit\n"
+ << "\t--help-all\t\t\t\tshow full help and exit\n"
<< "\t-v, --version\t\t\t\tshow version information and exit\n\n";
UsageConnection();
UsageAdmins(full);
--- /dev/null
+/*
+ * This program 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*
+ * Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_SGCONF_PARSER_STATE_H__
+#define __STG_SGCONF_PARSER_STATE_H__
+
+#include "config.h"
+
+namespace SGCONF
+{
+
+struct PARSER_STATE
+{
+ CONFIG config;
+ bool result;
+ int argc;
+ char ** argv;
+};
+
+}
--- /dev/null
+/*
+ * This program 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*
+ * Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_SGCONF_PARSERS_H__
+#define __STG_SGCONF_PARSERS_H__
+
+namespace SGCONF
+{
+
+typedef void (*FUNC0)();
+
+template <typename T>
+struct FUNC1
+{
+typedef void (*type)(T);
+};
+
+class PARSER
+{
+ public:
+ virtual PARSER_STATE parse(int, char **, CONFIG&) = 0;
+};
+
+template <typename T>
+class PARAM_PARSER : public PARSER
+{
+ public:
+ PARAM_PARSER(T& var) : m_var(var) {}
+ virtual PARSER_STATE parse(int argc, char ** argv, CONFIG& config)
+ {
+ std::istringstream stream(argv[0]);
+ stream >> m_var;
+ PARSER_STATE state;
+ state.argc = argc - 1;
+ state.argv = argv + 1;
+ state.config = config;
+ state.result = false;
+ return state;
+ }
+
+ private:
+ T& m_var;
+};
+
+class FUNC0_PARSER
+{
+ public:
+ FUNC0_PARSER(FUNC0 func) : m_func(func) {}
+ virtual PARSER_STATE parse(int argc, char ** argv, CONFIG& config)
+ {
+ m_func();
+ PARSER_STATE state;
+ state.argc = argc - 1;
+ state.argv = argv + 1;
+ state.config = config;
+ state.result = true;
+ return state;
+ }
+
+ private:
+ FUNC0 m_func;
+};
+
+template <typename T>
+class FUNC1_PARSER
+{
+ public:
+ FUNC1_PARSER(typename FUNC1<T>::type func, const T & arg) : m_func(func), m_arg(arg) {}
+ virtual PARSER_STATE parse(int argc, char ** argv, CONFIG& config)
+ {
+ m_func(m_arg);
+ PARSER_STATE state;
+ state.argc = argc - 1;
+ state.argv = argv + 1;
+ state.config = config;
+ state.result = true;
+ return state;
+ }
+
+ private:
+ typename FUNC1<T>::type m_func;
+ T m_arg;
+}
+
+class PARSERS
+{
+ public:
+ typedef PARSER_STATE (* FUNC)(int, char **, CONFIG&);
+
+ template <typename T>
+ void add(const std::string & shortToken,
+ const std::string & fullToken,
+ T& var);
+
+ template <>
+ void add<void>(const std:string & shortToken,
+ const std::string & fullToken,
+ FUNC0 func);
+ template <typename V>
+ void add<void>(const std:string & shortToken,
+ const std::string & fullToken,
+ FUNC1 func, const V& v);
+
+ private:
+};
+
+}