From d2814a423ca982cfa18eb05c0e5e51d6eff6e486 Mon Sep 17 00:00:00 2001 From: Xinyu Hou Date: Wed, 22 Oct 2014 16:27:38 +0100 Subject: [PATCH] Parse arg refactoring #4124 Extracted platform specific argument parsing code. --- src/lib/base/Log.h | 1 + src/lib/synergy/App.h | 2 - src/lib/synergy/ArgParser.cpp | 87 +++++++++++++++++++++++++++++++++++ src/lib/synergy/ArgParser.h | 38 +++++++++++++++ src/lib/synergy/ArgsBase.cpp | 3 +- src/lib/synergy/ArgsBase.h | 1 + 6 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 src/lib/synergy/ArgParser.cpp create mode 100644 src/lib/synergy/ArgParser.h diff --git a/src/lib/base/Log.h b/src/lib/base/Log.h index bbcc742e..d8cc5eee 100644 --- a/src/lib/base/Log.h +++ b/src/lib/base/Log.h @@ -26,6 +26,7 @@ #include #define CLOG (CLog::getInstance()) +#define BYE "\nTry `%s --help' for more information." class ILogOutputter; class CThread; diff --git a/src/lib/synergy/App.h b/src/lib/synergy/App.h index a3c328e3..2ce02d6e 100644 --- a/src/lib/synergy/App.h +++ b/src/lib/synergy/App.h @@ -125,8 +125,6 @@ private: CSocketMultiplexer* m_socketMultiplexer; }; -#define BYE "\nTry `%s --help' for more information." - #if WINAPI_MSWINDOWS #define DAEMON_RUNNING(running_) CArchMiscWindows::daemonRunning(running_) #else diff --git a/src/lib/synergy/ArgParser.cpp b/src/lib/synergy/ArgParser.cpp new file mode 100644 index 00000000..ad207ec4 --- /dev/null +++ b/src/lib/synergy/ArgParser.cpp @@ -0,0 +1,87 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2014 Synergy Si, inc. + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file COPYING that should have accompanied this file. + * + * This package 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, see . + */ + +#include "synergy/ArgParser.h" + +#include "synergy/ArgsBase.h" +#include "base/Log.h" + +CArgsBase* CArgParser::m_argsBase = NULL; + +bool +CArgParser::parsePlatformArg(CArgsBase& argsBase, const int& argc, const char* const* argv, int& i) +{ +#if WINAPI_MSWINDOWS + if (isArg(i, argc, argv, NULL, "--service")) { + LOG((CLOG_WARN "obsolete argument --service, use synergyd instead.")); + argsBase.m_shouldExit = true; + } + else if (isArg(i, argc, argv, NULL, "--exit-pause")) { + argsBase.m_pauseOnExit = true; + } + else if (isArg(i, argc, argv, NULL, "--stop-on-desk-switch")) { + argsBase.m_stopOnDeskSwitch = true; + } + else { + // option not supported here + return false; + } + + return true; +#elif WINAPI_XWINDOWS + if (CArgumentParser::isArg(i, argc, argv, "-display", "--display", 1)) { + // use alternative display + argsBase.m_display = argv[++i]; + } + + else if (CArgumentParser::isArg(i, argc, argv, NULL, "--no-xinitthreads")) { + argsBase.m_disableXInitThreads = true; + } + + else { + // option not supported here + return false; + } + + return true; +#elif WINAPI_CARBON + // no options for carbon + return false; +#endif +} + +bool +CArgParser::isArg( + int argi, int argc, const char* const* argv, + const char* name1, const char* name2, + int minRequiredParameters) +{ + if ((name1 != NULL && strcmp(argv[argi], name1) == 0) || + (name2 != NULL && strcmp(argv[argi], name2) == 0)) { + // match. check args left. + if (argi + minRequiredParameters >= argc) { + LOG((CLOG_PRINT "%s: missing arguments for `%s'" BYE, + argsBase().m_pname, argv[argi], argsBase().m_pname)); + argsBase().m_shouldExit = true; + return false; + } + return true; + } + + // no match + return false; +} diff --git a/src/lib/synergy/ArgParser.h b/src/lib/synergy/ArgParser.h new file mode 100644 index 00000000..96320e26 --- /dev/null +++ b/src/lib/synergy/ArgParser.h @@ -0,0 +1,38 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2014 Synergy Si, Inc. + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file COPYING that should have accompanied this file. + * + * This package 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, see . + */ + +#pragma once + +#include "base/String.h" +#include "common/stdvector.h" + +class CArgsBase; + +class CArgParser { + +public: + bool parsePlatformArg(CArgsBase& argsBase, const int& argc, const char* const* argv, int& i); + + static bool isArg(int argi, int argc, const char* const* argv, + const char* name1, const char* name2, + int minRequiredParameters = 0); +private: + static CArgsBase& argsBase() { return *m_argsBase; } + +private: + static CArgsBase* m_argsBase; +}; diff --git a/src/lib/synergy/ArgsBase.cpp b/src/lib/synergy/ArgsBase.cpp index 77090fab..e093764c 100644 --- a/src/lib/synergy/ArgsBase.cpp +++ b/src/lib/synergy/ArgsBase.cpp @@ -39,7 +39,8 @@ m_logFile(NULL), m_display(NULL), m_disableTray(false), m_enableIpc(false), -m_enableDragDrop(false) +m_enableDragDrop(false), +m_shouldExit(false) { } diff --git a/src/lib/synergy/ArgsBase.h b/src/lib/synergy/ArgsBase.h index 2bc744bc..ec8f8e86 100644 --- a/src/lib/synergy/ArgsBase.h +++ b/src/lib/synergy/ArgsBase.h @@ -46,4 +46,5 @@ public: #if WINAPI_XWINDOWS bool m_disableXInitThreads; #endif + bool m_shouldExit; };