Merged argsParse to master

This commit is contained in:
Xinyu Hou 2014-10-23 13:21:41 +01:00
commit 5c6eba8790
31 changed files with 1492 additions and 468 deletions

View File

@ -26,6 +26,7 @@
#include <stdarg.h> #include <stdarg.h>
#define CLOG (CLog::getInstance()) #define CLOG (CLog::getInstance())
#define BYE "\nTry `%s --help' for more information."
class ILogOutputter; class ILogOutputter;
class CThread; class CThread;

View File

@ -17,6 +17,7 @@
*/ */
#include "synergy/App.h" #include "synergy/App.h"
#include "base/Log.h" #include "base/Log.h"
#include "common/Version.h" #include "common/Version.h"
#include "synergy/protocol_types.h" #include "synergy/protocol_types.h"
@ -51,6 +52,10 @@
CApp* CApp::s_instance = nullptr; CApp* CApp::s_instance = nullptr;
//
// CApp
//
CApp::CApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver, CArgsBase* args) : CApp::CApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver, CArgsBase* args) :
m_bye(&exit), m_bye(&exit),
m_taskBarReceiver(NULL), m_taskBarReceiver(NULL),
@ -67,205 +72,10 @@ CApp::CApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver,
CApp::~CApp() CApp::~CApp()
{ {
s_instance = nullptr;
delete m_args; delete m_args;
} }
bool
CApp::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));
m_bye(kExitArgs);
}
return true;
}
// no match
return false;
}
bool
CApp::parseArg(const int& argc, const char* const* argv, int& i)
{
if (appUtil().parseArg(argc, argv, i)) {
// handled by platform util
return true;
}
else if (isArg(i, argc, argv, "-d", "--debug", 1)) {
// change logging level
argsBase().m_logFilter = argv[++i];
}
else if (isArg(i, argc, argv, "-l", "--log", 1)) {
argsBase().m_logFile = argv[++i];
}
else if (isArg(i, argc, argv, "-f", "--no-daemon")) {
// not a daemon
argsBase().m_daemon = false;
}
else if (isArg(i, argc, argv, NULL, "--daemon")) {
// daemonize
argsBase().m_daemon = true;
}
else if (isArg(i, argc, argv, "-n", "--name", 1)) {
// save screen name
argsBase().m_name = argv[++i];
}
else if (isArg(i, argc, argv, "-1", "--no-restart")) {
// don't try to restart
argsBase().m_restartable = false;
}
else if (isArg(i, argc, argv, NULL, "--restart")) {
// try to restart
argsBase().m_restartable = true;
}
else if (isArg(i, argc, argv, "-z", NULL)) {
argsBase().m_backend = true;
}
else if (isArg(i, argc, argv, NULL, "--no-hooks")) {
argsBase().m_noHooks = true;
}
else if (isArg(i, argc, argv, "-h", "--help")) {
help();
m_bye(kExitSuccess);
}
else if (isArg(i, argc, argv, NULL, "--version")) {
version();
m_bye(kExitSuccess);
}
else if (isArg(i, argc, argv, NULL, "--no-tray")) {
argsBase().m_disableTray = true;
}
else if (isArg(i, argc, argv, NULL, "--ipc")) {
argsBase().m_enableIpc = true;
}
else if (isArg(i, argc, argv, NULL, "--server")) {
// HACK: stop error happening when using portable (synergyp)
}
else if (isArg(i, argc, argv, NULL, "--client")) {
// HACK: stop error happening when using portable (synergyp)
}
else if (isArg(i, argc, argv, NULL, "--crypto-pass")) {
argsBase().m_crypto.m_pass = argv[++i];
argsBase().m_crypto.setMode("cfb");
}
else if (isArg(i, argc, argv, NULL, "--enable-drag-drop")) {
bool useDragDrop = true;
#ifdef WINAPI_XWINDOWS
useDragDrop = false;
LOG((CLOG_INFO "ignoring --enable-drag-drop, not supported on linux."));
#endif
#ifdef WINAPI_MSWINDOWS
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
if (osvi.dwMajorVersion < 6) {
useDragDrop = false;
LOG((CLOG_INFO "ignoring --enable-drag-drop, not supported below vista."));
}
#endif
if (useDragDrop) {
argsBase().m_enableDragDrop = true;
}
}
else {
// option not supported here
return false;
}
return true;
}
void
CApp::parseArgs(int argc, const char* const* argv, int& i)
{
// about these use of assert() here:
// previously an /analyze warning was displayed if we only used assert and
// did not return on failure. however, this warning does not appear to show
// any more (could be because new compiler args have been added).
// the asserts are programmer benefit only; the os should never pass 0 args,
// because the first is always the binary name. the only way assert would
// evaluate to true, is if this parse function were implemented incorrectly,
// which is unlikely because it's old code and has a specific use.
// we should avoid using anything other than assert here, because it will
// look like important code, which it's not really.
assert(argsBase().m_pname != NULL);
assert(argv != NULL);
assert(argc >= 1);
// set defaults
argsBase().m_name = ARCH->getHostName();
// parse options
for (i = 1; i < argc; ++i) {
if (parseArg(argc, argv, i)) {
continue;
}
else if (isArg(i, argc, argv, "--", NULL)) {
// remaining arguments are not options
++i;
break;
}
else if (argv[i][0] == '-') {
std::cerr << "Unrecognized option: " << argv[i] << std::endl;
m_bye(kExitArgs);
}
else {
// this and remaining arguments are not options
break;
}
}
#if SYSAPI_WIN32
// suggest that user installs as a windows service. when launched as
// service, process should automatically detect that it should run in
// daemon mode.
if (argsBase().m_daemon) {
LOG((CLOG_ERR
"the --daemon argument is not supported on windows. "
"instead, install %s as a service (--service install)",
argsBase().m_pname));
m_bye(kExitArgs);
}
#endif
}
void void
CApp::version() CApp::version()
{ {
@ -363,6 +173,18 @@ CApp::initApp(int argc, const char** argv)
// parse command line // parse command line
parseArgs(argc, argv); parseArgs(argc, argv);
// set log filter
if (!CLOG->setFilter(argsBase().m_logFilter)) {
LOG((CLOG_PRINT "%s: unrecognized log level `%s'" BYE,
argsBase().m_pname, argsBase().m_logFilter, argsBase().m_pname));
m_bye(kExitArgs);
}
loggingFilterWarning();
if (argsBase().m_enableDragDrop) {
LOG((CLOG_INFO "drag and drop enabled"));
}
// setup file logging after parsing args // setup file logging after parsing args
setupFileLogging(); setupFileLogging();

View File

@ -70,11 +70,6 @@ public:
// TODO: this is old C code - use inheritance to normalize // TODO: this is old C code - use inheritance to normalize
void (*m_bye)(int); void (*m_bye)(int);
// Returns true if argv[argi] is equal to name1 or name2.
bool isArg(int argi, int argc, const char* const* argv,
const char* name1, const char* name2,
int minRequiredParameters = 0);
static CApp& instance() { assert(s_instance != nullptr); return *s_instance; } static CApp& instance() { assert(s_instance != nullptr); return *s_instance; }
// If --log was specified in args, then add a file logger. // If --log was specified in args, then add a file logger.
@ -105,8 +100,6 @@ private:
void handleIpcMessage(const CEvent&, void*); void handleIpcMessage(const CEvent&, void*);
protected: protected:
virtual void parseArgs(int argc, const char* const* argv, int &i);
virtual bool parseArg(const int& argc, const char* const* argv, int& i);
void initIpcClient(); void initIpcClient();
void cleanupIpcClient(); void cleanupIpcClient();
void runEventsLoop(void*); void runEventsLoop(void*);
@ -125,8 +118,6 @@ private:
CSocketMultiplexer* m_socketMultiplexer; CSocketMultiplexer* m_socketMultiplexer;
}; };
#define BYE "\nTry `%s --help' for more information."
#if WINAPI_MSWINDOWS #if WINAPI_MSWINDOWS
#define DAEMON_RUNNING(running_) CArchMiscWindows::daemonRunning(running_) #define DAEMON_RUNNING(running_) CArchMiscWindows::daemonRunning(running_)
#else #else

View File

@ -30,13 +30,6 @@ CAppUtil::~CAppUtil()
{ {
} }
bool
CAppUtil::parseArg(const int& argc, const char* const* argv, int& i)
{
// no common platform args (yet)
return false;
}
void void
CAppUtil::adoptApp(IApp* app) CAppUtil::adoptApp(IApp* app)
{ {

View File

@ -26,7 +26,6 @@ public:
CAppUtil(); CAppUtil();
virtual ~CAppUtil(); virtual ~CAppUtil();
virtual bool parseArg(const int& argc, const char* const* argv, int& i);
virtual void adoptApp(IApp* app); virtual void adoptApp(IApp* app);
IApp& app() const; IApp& app() const;
virtual void exitApp(int code) { throw XExitApp(code); } virtual void exitApp(int code) { throw XExitApp(code); }

View File

@ -0,0 +1,431 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "synergy/ArgParser.h"
#include "synergy/App.h"
#include "synergy/ServerArgs.h"
#include "synergy/ClientArgs.h"
#include "synergy/ArgsBase.h"
#include "base/Log.h"
CArgsBase* CArgParser::m_argsBase = NULL;
CArgParser::CArgParser(CApp* app) :
m_app(app)
{
}
bool
CArgParser::parseServerArgs(CServerArgs& args, int argc, const char* const* argv)
{
setArgsBase(args);
updateCommonArgs(argv);
for (int i = 1; i < argc; ++i) {
if (parsePlatformArg(args, argc, argv, i)) {
continue;
}
else if (parseGenericArgs(argc, argv, i)) {
continue;
}
else if (isArg(i, argc, argv, "-a", "--address", 1)) {
// save listen address
args.m_synergyAddress = argv[++i];
}
else if (isArg(i, argc, argv, "-c", "--config", 1)) {
// save configuration file path
args.m_configFile = argv[++i];
}
else {
LOG((CLOG_PRINT "%s: unrecognized option `%s'" BYE, args.m_pname, argv[i], args.m_pname));
return false;
}
}
if (checkUnexpectedArgs()) {
return false;
}
return true;
}
bool
CArgParser::parseClientArgs(CClientArgs& args, int argc, const char* const* argv)
{
setArgsBase(args);
updateCommonArgs(argv);
int i;
for (i = 1; i < argc; ++i) {
if (parsePlatformArg(args, argc, argv, i)) {
continue;
}
else if (parseGenericArgs(argc, argv, i)) {
continue;
}
else if (isArg(i, argc, argv, NULL, "--camp")) {
// ignore -- included for backwards compatibility
}
else if (isArg(i, argc, argv, NULL, "--no-camp")) {
// ignore -- included for backwards compatibility
}
else if (isArg(i, argc, argv, NULL, "--yscroll", 1)) {
// define scroll
args.m_yscroll = atoi(argv[++i]);
}
else {
if (i + 1 == argc) {
args.m_synergyAddress = argv[i];
return true;
}
LOG((CLOG_PRINT "%s: unrecognized option `%s'" BYE, args.m_pname, argv[i], args.m_pname));
return false;
}
}
// exactly one non-option argument (server-address)
if (i == argc) {
LOG((CLOG_PRINT "%s: a server address or name is required" BYE,
args.m_pname, args.m_pname));
return false;
}
if (checkUnexpectedArgs()) {
return false;
}
return true;
}
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 (isArg(i, argc, argv, "-display", "--display", 1)) {
// use alternative display
argsBase.m_display = argv[++i];
}
else if (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::parseGenericArgs(int argc, const char* const* argv, int& i)
{
if (isArg(i, argc, argv, "-d", "--debug", 1)) {
// change logging level
argsBase().m_logFilter = argv[++i];
}
else if (isArg(i, argc, argv, "-l", "--log", 1)) {
argsBase().m_logFile = argv[++i];
}
else if (isArg(i, argc, argv, "-f", "--no-daemon")) {
// not a daemon
argsBase().m_daemon = false;
}
else if (isArg(i, argc, argv, NULL, "--daemon")) {
// daemonize
argsBase().m_daemon = true;
}
else if (isArg(i, argc, argv, "-n", "--name", 1)) {
// save screen name
argsBase().m_name = argv[++i];
}
else if (isArg(i, argc, argv, "-1", "--no-restart")) {
// don't try to restart
argsBase().m_restartable = false;
}
else if (isArg(i, argc, argv, NULL, "--restart")) {
// try to restart
argsBase().m_restartable = true;
}
else if (isArg(i, argc, argv, "-z", NULL)) {
argsBase().m_backend = true;
}
else if (isArg(i, argc, argv, NULL, "--no-hooks")) {
argsBase().m_noHooks = true;
}
else if (isArg(i, argc, argv, "-h", "--help")) {
if (m_app) {
m_app->help();
}
argsBase().m_shouldExit = true;
}
else if (isArg(i, argc, argv, NULL, "--version")) {
if (m_app) {
m_app->version();
}
argsBase().m_shouldExit = true;
}
else if (isArg(i, argc, argv, NULL, "--no-tray")) {
argsBase().m_disableTray = true;
}
else if (isArg(i, argc, argv, NULL, "--ipc")) {
argsBase().m_enableIpc = true;
}
else if (isArg(i, argc, argv, NULL, "--server")) {
// HACK: stop error happening when using portable (synergyp)
}
else if (isArg(i, argc, argv, NULL, "--client")) {
// HACK: stop error happening when using portable (synergyp)
}
else if (isArg(i, argc, argv, NULL, "--crypto-pass")) {
argsBase().m_crypto.m_pass = argv[++i];
argsBase().m_crypto.setMode("cfb");
}
else if (isArg(i, argc, argv, NULL, "--enable-drag-drop")) {
bool useDragDrop = true;
#ifdef WINAPI_XWINDOWS
useDragDrop = false;
LOG((CLOG_INFO "ignoring --enable-drag-drop, not supported on linux."));
#endif
#ifdef WINAPI_MSWINDOWS
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
if (osvi.dwMajorVersion < 6) {
useDragDrop = false;
LOG((CLOG_INFO "ignoring --enable-drag-drop, not supported below vista."));
}
#endif
if (useDragDrop) {
argsBase().m_enableDragDrop = true;
}
}
else {
// option not supported here
return false;
}
return true;
}
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;
}
void
CArgParser::splitCommandString(CString& command, std::vector<CString>& argv)
{
if (command.empty()) {
return ;
}
size_t leftDoubleQuote = 0;
size_t rightDoubleQuote = 0;
searchDoubleQuotes(command, leftDoubleQuote, rightDoubleQuote);
size_t startPos = 0;
size_t space = command.find(" ", startPos);
while (space != CString::npos) {
bool ignoreThisSpace = false;
// check if the space is between two double quotes
if (space > leftDoubleQuote && space < rightDoubleQuote) {
ignoreThisSpace = true;
}
else if (space > rightDoubleQuote){
searchDoubleQuotes(command, leftDoubleQuote, rightDoubleQuote, rightDoubleQuote + 1);
}
if (!ignoreThisSpace) {
CString subString = command.substr(startPos, space - startPos);
removeDoubleQuotes(subString);
argv.push_back(subString);
}
// find next space
if (ignoreThisSpace) {
space = command.find(" ", rightDoubleQuote + 1);
}
else {
startPos = space + 1;
space = command.find(" ", startPos);
}
}
CString subString = command.substr(startPos, command.size());
removeDoubleQuotes(subString);
argv.push_back(subString);
}
bool
CArgParser::searchDoubleQuotes(CString& command, size_t& left, size_t& right, size_t startPos)
{
bool result = false;
left = CString::npos;
right = CString::npos;
left = command.find("\"", startPos);
if (left != CString::npos) {
right = command.find("\"", left + 1);
if (right != CString::npos) {
result = true;
}
}
if (!result) {
left = 0;
right = 0;
}
return result;
}
void
CArgParser::removeDoubleQuotes(CString& arg)
{
// if string is surrounded by double quotes, remove them
if (arg[0] == '\"' &&
arg[arg.size() - 1] == '\"') {
arg = arg.substr(1, arg.size() - 2);
}
}
const char**
CArgParser::getArgv(std::vector<CString>& argsArray)
{
size_t argc = argsArray.size();
// caller is responsible for deleting the outer array only
// we use the c string pointers from argsArray and assign
// them to the inner array. So caller only need to use
// delete[] to delete the outer array
const char** argv = new const char*[argc];
for (size_t i = 0; i < argc; i++) {
argv[i] = argsArray[i].c_str();
}
return argv;
}
CString
CArgParser::assembleCommand(std::vector<CString>& argsArray, CString ignoreArg, int parametersRequired)
{
CString result;
for (std::vector<CString>::iterator it = argsArray.begin(); it != argsArray.end(); ++it) {
if (it->compare(ignoreArg) == 0) {
it = it + parametersRequired;
continue;
}
// if there is a space in this arg, use double quotes surround it
if ((*it).find(" ") != CString::npos) {
(*it).insert(0, "\"");
(*it).push_back('\"');
}
result.append(*it);
// add space to saperate args
result.append(" ");
}
if (!result.empty()) {
// remove the tail space
result = result.substr(0, result.size() - 1);
}
return result;
}
void
CArgParser::updateCommonArgs(const char* const* argv)
{
argsBase().m_name = ARCH->getHostName();
argsBase().m_pname = ARCH->getBasename(argv[0]);
}
bool
CArgParser::checkUnexpectedArgs()
{
#if SYSAPI_WIN32
// suggest that user installs as a windows service. when launched as
// service, process should automatically detect that it should run in
// daemon mode.
if (argsBase().m_daemon) {
LOG((CLOG_ERR
"the --daemon argument is not supported on windows. "
"instead, install %s as a service (--service install)",
argsBase().m_pname));
return true;
}
#endif
return false;
}

View File

@ -0,0 +1,61 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "base/String.h"
#include "common/stdvector.h"
class CServerArgs;
class CClientArgs;
class CToolArgs;
class CArgsBase;
class CApp;
class CArgParser {
public:
CArgParser(CApp* app);
bool parseServerArgs(CServerArgs& args, int argc, const char* const* argv);
bool parseClientArgs(CClientArgs& args, int argc, const char* const* argv);
bool parsePlatformArg(CArgsBase& argsBase, const int& argc, const char* const* argv, int& i);
bool parseGenericArgs(int argc, const char* const* argv, int& i);
void setArgsBase(CArgsBase& argsBase) { m_argsBase = &argsBase; }
static bool isArg(int argi, int argc, const char* const* argv,
const char* name1, const char* name2,
int minRequiredParameters = 0);
static void splitCommandString(CString& command, std::vector<CString>& argv);
static bool searchDoubleQuotes(CString& command, size_t& left,
size_t& right, size_t startPos = 0);
static void removeDoubleQuotes(CString& arg);
static const char** getArgv(std::vector<CString>& argsArray);
static CString assembleCommand(std::vector<CString>& argsArray,
CString ignoreArg = "", int parametersRequired = 0);
private:
void updateCommonArgs(const char* const* argv);
bool checkUnexpectedArgs();
static CArgsBase& argsBase() { return *m_argsBase; }
private:
CApp* m_app;
static CArgsBase* m_argsBase;
};

View File

@ -39,7 +39,9 @@ m_logFile(NULL),
m_display(NULL), m_display(NULL),
m_disableTray(false), m_disableTray(false),
m_enableIpc(false), m_enableIpc(false),
m_enableDragDrop(false) m_enableDragDrop(false),
m_shouldExit(false),
m_synergyAddress()
{ {
} }

View File

@ -46,4 +46,6 @@ public:
#if WINAPI_XWINDOWS #if WINAPI_XWINDOWS
bool m_disableXInitThreads; bool m_disableXInitThreads;
#endif #endif
bool m_shouldExit;
CString m_synergyAddress;
}; };

View File

@ -19,9 +19,11 @@
#include "synergy/ClientApp.h" #include "synergy/ClientApp.h"
#include "client/Client.h" #include "client/Client.h"
#include "synergy/ArgParser.h"
#include "synergy/protocol_types.h" #include "synergy/protocol_types.h"
#include "synergy/Screen.h" #include "synergy/Screen.h"
#include "synergy/XScreen.h" #include "synergy/XScreen.h"
#include "synergy/ClientArgs.h"
#include "net/NetworkAddress.h" #include "net/NetworkAddress.h"
#include "net/TCPSocketFactory.h" #include "net/TCPSocketFactory.h"
#include "net/SocketMultiplexer.h" #include "net/SocketMultiplexer.h"
@ -61,9 +63,10 @@
#define RETRY_TIME 1.0 #define RETRY_TIME 1.0
CClientApp::CClientApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver) : CClientApp::CClientApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver) :
CApp(events, createTaskBarReceiver, new CArgs()), CApp(events, createTaskBarReceiver, new CClientArgs()),
m_client(NULL), m_client(NULL),
m_clientScreen(NULL) m_clientScreen(NULL),
m_serverAddress(NULL)
{ {
} }
@ -71,69 +74,21 @@ CClientApp::~CClientApp()
{ {
} }
CClientApp::CArgs::CArgs() :
m_yscroll(0),
m_serverAddress(NULL)
{
}
CClientApp::CArgs::~CArgs()
{
}
bool
CClientApp::parseArg(const int& argc, const char* const* argv, int& i)
{
if (CApp::parseArg(argc, argv, i)) {
// found common arg
return true;
}
else if (isArg(i, argc, argv, NULL, "--camp")) {
// ignore -- included for backwards compatibility
}
else if (isArg(i, argc, argv, NULL, "--no-camp")) {
// ignore -- included for backwards compatibility
}
else if (isArg(i, argc, argv, NULL, "--yscroll", 1)) {
// define scroll
args().m_yscroll = atoi(argv[++i]);
}
else {
// option not supported here
return false;
}
// argument was valid
return true;
}
void void
CClientApp::parseArgs(int argc, const char* const* argv) CClientApp::parseArgs(int argc, const char* const* argv)
{ {
// asserts values, sets defaults, and parses args CArgParser argParser(this);
int i; bool result = argParser.parseClientArgs(args(), argc, argv);
CApp::parseArgs(argc, argv, i);
// exactly one non-option argument (server-address) if (!result || args().m_shouldExit) {
if (i == argc) {
LOG((CLOG_PRINT "%s: a server address or name is required" BYE,
args().m_pname, args().m_pname));
m_bye(kExitArgs); m_bye(kExitArgs);
} }
if (i + 1 != argc) { else {
LOG((CLOG_PRINT "%s: unrecognized option `%s'" BYE,
args().m_pname, argv[i], args().m_pname));
m_bye(kExitArgs);
}
// save server address // save server address
if (!args().m_synergyAddress.empty()) {
try { try {
*args().m_serverAddress = CNetworkAddress(argv[i], kDefaultPort); *m_serverAddress = CNetworkAddress(args().m_synergyAddress, kDefaultPort);
args().m_serverAddress->resolve(); m_serverAddress->resolve();
} }
catch (XSocketAddress& e) { catch (XSocketAddress& e) {
// allow an address that we can't look up if we're restartable. // allow an address that we can't look up if we're restartable.
@ -146,22 +101,8 @@ CClientApp::parseArgs(int argc, const char* const* argv)
m_bye(kExitFailed); m_bye(kExitFailed);
} }
} }
// set log filter
if (!CLOG->setFilter(args().m_logFilter)) {
LOG((CLOG_PRINT "%s: unrecognized log level `%s'" BYE,
args().m_pname, args().m_logFilter, args().m_pname));
m_bye(kExitArgs);
} }
// identify system
LOG((CLOG_INFO "%s Client on %s %s", kAppVersion, ARCH->getOSName().c_str(), ARCH->getPlatformName().c_str()));
if (args().m_enableDragDrop) {
LOG((CLOG_INFO "drag and drop enabled"));
} }
loggingFilterWarning();
} }
void void
@ -460,7 +401,7 @@ CClientApp::startClient()
if (m_clientScreen == NULL) { if (m_clientScreen == NULL) {
clientScreen = openClientScreen(); clientScreen = openClientScreen();
m_client = openClient(args().m_name, m_client = openClient(args().m_name,
*args().m_serverAddress, clientScreen, args().m_crypto); *m_serverAddress, clientScreen, args().m_crypto);
m_clientScreen = clientScreen; m_clientScreen = clientScreen;
LOG((CLOG_NOTE "started client")); LOG((CLOG_NOTE "started client"));
} }
@ -590,7 +531,7 @@ int
CClientApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc startup) CClientApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc startup)
{ {
// general initialization // general initialization
args().m_serverAddress = new CNetworkAddress; m_serverAddress = new CNetworkAddress;
args().m_pname = ARCH->getBasename(argv[0]); args().m_pname = ARCH->getBasename(argv[0]);
// install caller's output filter // install caller's output filter
@ -612,7 +553,7 @@ CClientApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFun
delete m_taskBarReceiver; delete m_taskBarReceiver;
} }
delete args().m_serverAddress; delete m_serverAddress;
throw; throw;
} }

View File

@ -19,26 +19,17 @@
#pragma once #pragma once
#include "synergy/App.h" #include "synergy/App.h"
#include "synergy/ArgsBase.h"
class CScreen; class CScreen;
class CEvent; class CEvent;
class CClient; class CClient;
class CNetworkAddress; class CNetworkAddress;
class CThread; class CThread;
class CClientArgs;
class CCryptoOptions;
class CClientApp : public CApp { class CClientApp : public CApp {
public: public:
class CArgs : public CArgsBase {
public:
CArgs();
~CArgs();
public:
int m_yscroll;
CNetworkAddress* m_serverAddress;
};
CClientApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver); CClientApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver);
virtual ~CClientApp(); virtual ~CClientApp();
@ -49,7 +40,7 @@ public:
void help(); void help();
// Returns arguments that are common and for client. // Returns arguments that are common and for client.
CArgs& args() const { return (CArgs&)argsBase(); } CClientArgs& args() const { return (CClientArgs&)argsBase(); }
const char* daemonName() const; const char* daemonName() const;
const char* daemonInfo() const; const char* daemonInfo() const;
@ -85,10 +76,8 @@ public:
CClient* getClientPtr() { return m_client; } CClient* getClientPtr() { return m_client; }
private:
virtual bool parseArg(const int& argc, const char* const* argv, int& i);
private: private:
CClient* m_client; CClient* m_client;
CScreen* m_clientScreen; CScreen* m_clientScreen;
CNetworkAddress* m_serverAddress;
}; };

View File

@ -0,0 +1,23 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "synergy/ClientArgs.h"
CClientArgs::CClientArgs() :
m_yscroll(0)
{
}

View File

@ -0,0 +1,30 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "synergy/ArgsBase.h"
class CNetworkAddress;
class CClientArgs : public CArgsBase {
public:
CClientArgs();
public:
int m_yscroll;
};

View File

@ -22,6 +22,9 @@
#include "synergy/DaemonApp.h" #include "synergy/DaemonApp.h"
#include "synergy/App.h" #include "synergy/App.h"
#include "synergy/ArgParser.h"
#include "synergy/ServerArgs.h"
#include "synergy/ClientArgs.h"
#include "ipc/IpcClientProxy.h" #include "ipc/IpcClientProxy.h"
#include "ipc/IpcMessage.h" #include "ipc/IpcMessage.h"
#include "ipc/IpcLogOutputter.h" #include "ipc/IpcLogOutputter.h"
@ -301,13 +304,29 @@ CDaemonApp::handleIpcMessage(const CEvent& e, void*)
if (!command.empty()) { if (!command.empty()) {
LOG((CLOG_DEBUG "new command, elevate=%d command=%s", cm->elevate(), command.c_str())); LOG((CLOG_DEBUG "new command, elevate=%d command=%s", cm->elevate(), command.c_str()));
CString debugArg("--debug"); std::vector<CString> argsArray;
size_t debugArgPos = command.find(debugArg); CArgParser::splitCommandString(command, argsArray);
if (debugArgPos != CString::npos) { CArgParser argParser(NULL);
UInt32 from = static_cast<UInt32>(debugArgPos) + static_cast<UInt32>(debugArg.size()) + 1; const char** argv = argParser.getArgv(argsArray);
UInt32 nextSpace = static_cast<UInt32>(command.find(" ", from)); CServerArgs serverArgs;
CString logLevel(command.substr(from, nextSpace - from)); CClientArgs clientArgs;
int argc = static_cast<int>(argsArray.size());
bool server = argsArray[0].find("synergys") != CString::npos ? true : false;
CArgsBase* argBase = NULL;
if (server) {
argParser.parseServerArgs(serverArgs, argc, argv);
argBase = &serverArgs;
}
else {
argParser.parseClientArgs(clientArgs, argc, argv);
argBase = &clientArgs;
}
delete[] argv;
CString logLevel(argBase->m_logFilter);
if (!logLevel.empty()) {
try { try {
// change log level based on that in the command string // change log level based on that in the command string
// and change to that log level now. // and change to that log level now.

View File

@ -32,9 +32,6 @@ class IApp : public IInterface
{ {
public: public:
virtual void setByeFunc(void(*bye)(int)) = 0; virtual void setByeFunc(void(*bye)(int)) = 0;
virtual bool isArg(int argi, int argc, const char* const* argv,
const char* name1, const char* name2,
int minRequiredParameters = 0) = 0;
virtual CArgsBase& argsBase() const = 0; virtual CArgsBase& argsBase() const = 0;
virtual int standardStartup(int argc, char** argv) = 0; virtual int standardStartup(int argc, char** argv) = 0;
virtual int runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc startup) = 0; virtual int runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc startup) = 0;

View File

@ -23,7 +23,6 @@
class IAppUtil : public IInterface { class IAppUtil : public IInterface {
public: public:
virtual bool parseArg(const int& argc, const char* const* argv, int& i) = 0;
virtual void adoptApp(IApp* app) = 0; virtual void adoptApp(IApp* app) = 0;
virtual IApp& app() const = 0; virtual IApp& app() const = 0;
virtual int run(int argc, char** argv) = 0; virtual int run(int argc, char** argv) = 0;

View File

@ -22,9 +22,11 @@
#include "server/ClientListener.h" #include "server/ClientListener.h"
#include "server/ClientProxy.h" #include "server/ClientProxy.h"
#include "server/PrimaryClient.h" #include "server/PrimaryClient.h"
#include "synergy/ArgParser.h"
#include "synergy/Screen.h" #include "synergy/Screen.h"
#include "synergy/XScreen.h" #include "synergy/XScreen.h"
#include "synergy/ServerTaskBarReceiver.h" #include "synergy/ServerTaskBarReceiver.h"
#include "synergy/ServerArgs.h"
#include "net/SocketMultiplexer.h" #include "net/SocketMultiplexer.h"
#include "net/TCPSocketFactory.h" #include "net/TCPSocketFactory.h"
#include "net/XSocket.h" #include "net/XSocket.h"
@ -63,13 +65,14 @@
// //
CServerApp::CServerApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver) : CServerApp::CServerApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver) :
CApp(events, createTaskBarReceiver, new CArgs()), CApp(events, createTaskBarReceiver, new CServerArgs()),
m_server(NULL), m_server(NULL),
m_serverState(kUninitialized), m_serverState(kUninitialized),
m_serverScreen(NULL), m_serverScreen(NULL),
m_primaryClient(NULL), m_primaryClient(NULL),
m_listener(NULL), m_listener(NULL),
m_timer(NULL) m_timer(NULL),
m_synergyAddress(NULL)
{ {
} }
@ -77,82 +80,29 @@ CServerApp::~CServerApp()
{ {
} }
CServerApp::CArgs::CArgs() : void
m_synergyAddress(NULL), CServerApp::parseArgs(int argc, const char* const* argv)
m_config(NULL)
{ {
} CArgParser argParser(this);
bool result = argParser.parseServerArgs(args(), argc, argv);
CServerApp::CArgs::~CArgs() if (!result || args().m_shouldExit) {
{ m_bye(kExitArgs);
} }
else {
bool if (!args().m_synergyAddress.empty()) {
CServerApp::parseArg(const int& argc, const char* const* argv, int& i)
{
if (CApp::parseArg(argc, argv, i)) {
// found common arg
return true;
}
else if (isArg(i, argc, argv, "-a", "--address", 1)) {
// save listen address
try { try {
*args().m_synergyAddress = CNetworkAddress(argv[i + 1], *m_synergyAddress = CNetworkAddress(args().m_synergyAddress,
kDefaultPort); kDefaultPort);
args().m_synergyAddress->resolve(); m_synergyAddress->resolve();
} }
catch (XSocketAddress& e) { catch (XSocketAddress& e) {
LOG((CLOG_PRINT "%s: %s" BYE, LOG((CLOG_PRINT "%s: %s" BYE,
args().m_pname, e.what(), args().m_pname)); args().m_pname, e.what(), args().m_pname));
m_bye(kExitArgs); m_bye(kExitArgs);
} }
++i;
} }
else if (isArg(i, argc, argv, "-c", "--config", 1)) {
// save configuration file path
args().m_configFile = argv[++i];
} }
else {
// option not supported here
return false;
}
// argument was valid
return true;
}
void
CServerApp::parseArgs(int argc, const char* const* argv)
{
// asserts values, sets defaults, and parses args
int i;
CApp::parseArgs(argc, argv, i);
// no non-option arguments are allowed
if (i != argc) {
LOG((CLOG_PRINT "%s: unrecognized option `%s'" BYE,
args().m_pname, argv[i], args().m_pname));
m_bye(kExitArgs);
}
// set log filter
if (!CLOG->setFilter(args().m_logFilter)) {
LOG((CLOG_PRINT "%s: unrecognized log level `%s'" BYE,
args().m_pname, args().m_logFilter, args().m_pname));
m_bye(kExitArgs);
}
// identify system
LOG((CLOG_INFO "%s Server on %s %s", kAppVersion, ARCH->getOSName().c_str(), ARCH->getPlatformName().c_str()));
if (args().m_enableDragDrop) {
LOG((CLOG_INFO "drag and drop enabled"));
}
loggingFilterWarning();
} }
void void
@ -742,8 +692,8 @@ CServerApp::mainLoop()
// set the contact address, if provided, in the config. // set the contact address, if provided, in the config.
// otherwise, if the config doesn't have an address, use // otherwise, if the config doesn't have an address, use
// the default. // the default.
if (args().m_synergyAddress->isValid()) { if (m_synergyAddress->isValid()) {
args().m_config->setSynergyAddress(*args().m_synergyAddress); args().m_config->setSynergyAddress(*m_synergyAddress);
} }
else if (!args().m_config->getSynergyAddress().isValid()) { else if (!args().m_config->getSynergyAddress().isValid()) {
args().m_config->setSynergyAddress(CNetworkAddress(kDefaultPort)); args().m_config->setSynergyAddress(CNetworkAddress(kDefaultPort));
@ -839,7 +789,7 @@ int
CServerApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc startup) CServerApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc startup)
{ {
// general initialization // general initialization
args().m_synergyAddress = new CNetworkAddress; m_synergyAddress = new CNetworkAddress;
args().m_config = new CConfig(m_events); args().m_config = new CConfig(m_events);
args().m_pname = ARCH->getBasename(argv[0]); args().m_pname = ARCH->getBasename(argv[0]);
@ -858,7 +808,7 @@ CServerApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFun
} }
delete args().m_config; delete args().m_config;
delete args().m_synergyAddress; delete m_synergyAddress;
return result; return result;
} }

View File

@ -18,6 +18,7 @@
#pragma once #pragma once
#include "synergy/ArgsBase.h"
#include "synergy/App.h" #include "synergy/App.h"
#include "base/String.h" #include "base/String.h"
#include "server/Config.h" #include "server/Config.h"
@ -44,20 +45,10 @@ class CClientListener;
class CEventQueueTimer; class CEventQueueTimer;
class ILogOutputter; class ILogOutputter;
class IEventQueue; class IEventQueue;
class CServerArgs;
class CServerApp : public CApp { class CServerApp : public CApp {
public: public:
class CArgs : public CArgsBase {
public:
CArgs();
~CArgs();
public:
CString m_configFile;
CNetworkAddress* m_synergyAddress;
CConfig* m_config;
};
CServerApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver); CServerApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver);
virtual ~CServerApp(); virtual ~CServerApp();
@ -68,7 +59,7 @@ public:
void help(); void help();
// Returns arguments that are common and for server. // Returns arguments that are common and for server.
CArgs& args() const { return (CArgs&)argsBase(); } CServerArgs& args() const { return (CServerArgs&)argsBase(); }
const char* daemonName() const; const char* daemonName() const;
const char* daemonInfo() const; const char* daemonInfo() const;
@ -120,9 +111,9 @@ public:
CPrimaryClient* m_primaryClient; CPrimaryClient* m_primaryClient;
CClientListener* m_listener; CClientListener* m_listener;
CEventQueueTimer* m_timer; CEventQueueTimer* m_timer;
CNetworkAddress* m_synergyAddress;
private: private:
virtual bool parseArg(const int& argc, const char* const* argv, int& i);
void handleScreenSwitched(const CEvent&, void* data); void handleScreenSwitched(const CEvent&, void* data);
}; };

View File

@ -0,0 +1,25 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "synergy/ServerArgs.h"
CServerArgs::CServerArgs() :
m_configFile(),
m_config(NULL)
{
}

View File

@ -0,0 +1,32 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "synergy/ArgsBase.h"
class CNetworkAddress;
class CConfig;
class CServerArgs : public CArgsBase {
public:
CServerArgs();
public:
CString m_configFile;
CConfig* m_config;
};

View File

@ -27,31 +27,6 @@ CAppUtilUnix::~CAppUtilUnix()
{ {
} }
bool
CAppUtilUnix::parseArg(const int& argc, const char* const* argv, int& i)
{
#if WINAPI_XWINDOWS
if (app().isArg(i, argc, argv, "-display", "--display", 1)) {
// use alternative display
app().argsBase().m_display = argv[++i];
}
else if (app().isArg(i, argc, argv, NULL, "--no-xinitthreads")) {
app().argsBase().m_disableXInitThreads = true;
}
else {
// option not supported here
return false;
}
return true;
#else
// no options for carbon
return false;
#endif
}
int int
standardStartupStatic(int argc, char** argv) standardStartupStatic(int argc, char** argv)
{ {

View File

@ -29,7 +29,6 @@ public:
CAppUtilUnix(IEventQueue* events); CAppUtilUnix(IEventQueue* events);
virtual ~CAppUtilUnix(); virtual ~CAppUtilUnix();
bool parseArg(const int& argc, const char* const* argv, int& i);
int run(int argc, char** argv); int run(int argc, char** argv);
void startNode(); void startNode();
}; };

View File

@ -58,29 +58,6 @@ BOOL WINAPI CAppUtilWindows::consoleHandler(DWORD)
return TRUE; return TRUE;
} }
bool
CAppUtilWindows::parseArg(const int& argc, const char* const* argv, int& i)
{
if (app().isArg(i, argc, argv, NULL, "--service")) {
LOG((CLOG_WARN "obsolete argument --service, use synergyd instead."));
app().bye(kExitFailed);
}
else if (app().isArg(i, argc, argv, NULL, "--exit-pause")) {
app().argsBase().m_pauseOnExit = true;
}
else if (app().isArg(i, argc, argv, NULL, "--stop-on-desk-switch")) {
app().argsBase().m_stopOnDeskSwitch = true;
}
else {
// option not supported here
return false;
}
return true;
}
static static
int int
mainLoopStatic() mainLoopStatic()

View File

@ -38,8 +38,6 @@ public:
CAppUtilWindows(IEventQueue* events); CAppUtilWindows(IEventQueue* events);
virtual ~CAppUtilWindows(); virtual ~CAppUtilWindows();
bool parseArg(const int& argc, const char* const* argv, int& i);
int daemonNTStartup(int, char**); int daemonNTStartup(int, char**);
int daemonNTMainLoop(int argc, const char** argv); int daemonNTMainLoop(int argc, const char** argv);

View File

@ -0,0 +1,44 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#define TEST_ENV
#include "synergy/App.h"
#include "test/global/gmock.h"
class CMockApp : public CApp
{
public:
CMockApp() : CApp(NULL, NULL, NULL) { }
MOCK_METHOD0(help, void());
MOCK_METHOD0(loadConfig, void());
MOCK_METHOD1(loadConfig, bool(const CString&));
MOCK_CONST_METHOD0(daemonInfo, const char*());
MOCK_CONST_METHOD0(daemonName, const char*());
MOCK_METHOD2(parseArgs, void(int, const char* const*));
MOCK_METHOD0(version, void());
MOCK_METHOD2(standardStartup, int(int, char**));
MOCK_METHOD4(runInner, int(int, char**, ILogOutputter*, StartupFunc));
MOCK_METHOD0(startNode, void());
MOCK_METHOD0(mainLoop, int());
MOCK_METHOD2(foregroundStartup, int(int, char**));
MOCK_METHOD0(createScreen, CScreen*());
};

View File

@ -0,0 +1,33 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#define TEST_ENV
#include "synergy/ArgParser.h"
#include "test/global/gmock.h"
class CMockArgParser : public CArgParser
{
public:
CMockArgParser() : CArgParser(NULL) { }
MOCK_METHOD3(parseGenericArgs, bool(int, const char* const*, int&));
MOCK_METHOD0(checkUnexpectedArgs, bool());
};

View File

@ -49,4 +49,4 @@ endif()
add_executable(unittests ${sources}) add_executable(unittests ${sources})
target_link_libraries(unittests target_link_libraries(unittests
arch base client server common io net platform server synergy mt gtest gmock cryptopp ${libs}) arch base client server common io net platform server synergy mt ipc gtest gmock cryptopp ${libs})

View File

@ -0,0 +1,207 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "synergy/ArgParser.h"
#include "synergy/ArgsBase.h"
#include "test/global/gtest.h"
TEST(CArgParserTests, isArg_abbreviationsArg_returnTrue)
{
int i = 1;
const int argc = 2;
const char* argv[argc] = { "stub", "-t" };
bool result = CArgParser::isArg(i, argc, argv, "-t", NULL);
EXPECT_EQ(true, result);
}
TEST(CArgParserTests, isArg_fullArg_returnTrue)
{
int i = 1;
const int argc = 2;
const char* argv[argc] = { "stub", "--test" };
bool result = CArgParser::isArg(i, argc, argv, NULL, "--test");
EXPECT_EQ(true, result);
}
TEST(CArgParserTests, isArg_missingArgs_returnFalse)
{
int i = 1;
const int argc = 2;
const char* argv[argc] = { "stub", "-t" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
bool result = CArgParser::isArg(i, argc, argv, "-t", NULL, 1);
EXPECT_FALSE(result);
EXPECT_EQ(true, argsBase.m_shouldExit);
}
TEST(CArgParserTests, searchDoubleQuotes_doubleQuotedArg_returnTrue)
{
CString command("\"stub\"");
size_t left = 0;
size_t right = 0;
bool result = CArgParser::searchDoubleQuotes(command, left, right);
EXPECT_EQ(true, result);
EXPECT_EQ(0, left);
EXPECT_EQ(5, right);
}
TEST(CArgParserTests, searchDoubleQuotes_noDoubleQuotedArg_returnfalse)
{
CString command("stub");
size_t left = 0;
size_t right = 0;
bool result = CArgParser::searchDoubleQuotes(command, left, right);
EXPECT_FALSE(result);
EXPECT_EQ(0, left);
EXPECT_EQ(0, right);
}
TEST(CArgParserTests, searchDoubleQuotes_oneDoubleQuoteArg_returnfalse)
{
CString command("\"stub");
size_t left = 0;
size_t right = 0;
bool result = CArgParser::searchDoubleQuotes(command, left, right);
EXPECT_FALSE(result);
EXPECT_EQ(0, left);
EXPECT_EQ(0, right);
}
TEST(CArgParserTests, splitCommandString_oneArg_returnArgv)
{
CString command("stub");
std::vector<CString> argv;
CArgParser::splitCommandString(command, argv);
EXPECT_EQ(1, argv.size());
EXPECT_EQ("stub", argv.at(0));
}
TEST(CArgParserTests, splitCommandString_twoArgs_returnArgv)
{
CString command("stub1 stub2");
std::vector<CString> argv;
CArgParser::splitCommandString(command, argv);
EXPECT_EQ(2, argv.size());
EXPECT_EQ("stub1", argv.at(0));
EXPECT_EQ("stub2", argv.at(1));
}
TEST(CArgParserTests, splitCommandString_doubleQuotedArgs_returnArgv)
{
CString command("\"stub1\" stub2 \"stub3\"");
std::vector<CString> argv;
CArgParser::splitCommandString(command, argv);
EXPECT_EQ(3, argv.size());
EXPECT_EQ("stub1", argv.at(0));
EXPECT_EQ("stub2", argv.at(1));
EXPECT_EQ("stub3", argv.at(2));
}
TEST(CArgParserTests, splitCommandString_spaceDoubleQuotedArgs_returnArgv)
{
CString command("\"stub1\" stub2 \"stub3 space\"");
std::vector<CString> argv;
CArgParser::splitCommandString(command, argv);
EXPECT_EQ(3, argv.size());
EXPECT_EQ("stub1", argv.at(0));
EXPECT_EQ("stub2", argv.at(1));
EXPECT_EQ("stub3 space", argv.at(2));
}
TEST(CArgParserTests, getArgv_stringArray_return2DArray)
{
std::vector<CString> argArray;
argArray.push_back("stub1");
argArray.push_back("stub2");
argArray.push_back("stub3 space");
const char** argv = CArgParser::getArgv(argArray);
CString row1(argv[0]);
CString row2(argv[1]);
CString row3(argv[2]);
EXPECT_EQ("stub1", row1);
EXPECT_EQ("stub2", row2);
EXPECT_EQ("stub3 space", row3);
delete[] argv;
}
TEST(CArgParserTests, assembleCommand_stringArray_returnCommand)
{
std::vector<CString> argArray;
argArray.push_back("stub1");
argArray.push_back("stub2");
CString command = CArgParser::assembleCommand(argArray);
EXPECT_EQ("stub1 stub2", command);
}
TEST(CArgParserTests, assembleCommand_ignoreSecondArg_returnCommand)
{
std::vector<CString> argArray;
argArray.push_back("stub1");
argArray.push_back("stub2");
CString command = CArgParser::assembleCommand(argArray, "stub2");
EXPECT_EQ("stub1", command);
}
TEST(CArgParserTests, assembleCommand_ignoreSecondArgWithOneParameter_returnCommand)
{
std::vector<CString> argArray;
argArray.push_back("stub1");
argArray.push_back("stub2");
argArray.push_back("stub3");
argArray.push_back("stub4");
CString command = CArgParser::assembleCommand(argArray, "stub2", 1);
EXPECT_EQ("stub1 stub4", command);
}
TEST(CArgParserTests, assembleCommand_stringArrayWithSpace_returnCommand)
{
std::vector<CString> argArray;
argArray.push_back("stub1 space");
argArray.push_back("stub2");
argArray.push_back("stub3 space");
CString command = CArgParser::assembleCommand(argArray);
EXPECT_EQ("\"stub1 space\" stub2 \"stub3 space\"", command);
}

View File

@ -0,0 +1,95 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "synergy/ArgParser.h"
#include "synergy/ClientArgs.h"
#include "test/mock/synergy/MockArgParser.h"
#include "test/global/gtest.h"
using ::testing::_;
using ::testing::Invoke;
using ::testing::NiceMock;
bool
client_stubParseGenericArgs(int, const char* const*, int&)
{
return false;
}
bool
client_stubCheckUnexpectedArgs()
{
return false;
}
TEST(CClientArgsParsingTests, parseClientArgs_yScrollArg_setYScroll)
{
NiceMock<CMockArgParser> argParser;
ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(client_stubParseGenericArgs));
ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(client_stubCheckUnexpectedArgs));
CClientArgs clientArgs;
const int argc = 3;
const char* kYScrollCmd[argc] = { "stub", "--yscroll", "1" };
argParser.parseClientArgs(clientArgs, argc, kYScrollCmd);
EXPECT_EQ(1, clientArgs.m_yscroll);
}
TEST(CClientArgsParsingTests, parseClientArgs_addressArg_setSynergyAddress)
{
NiceMock<CMockArgParser> argParser;
ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(client_stubParseGenericArgs));
ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(client_stubCheckUnexpectedArgs));
CClientArgs clientArgs;
const int argc = 2;
const char* kAddressCmd[argc] = { "stub", "mock_address" };
bool result = argParser.parseClientArgs(clientArgs, argc, kAddressCmd);
EXPECT_EQ("mock_address", clientArgs.m_synergyAddress);
EXPECT_EQ(true, result);
}
TEST(CClientArgsParsingTests, parseClientArgs_noAddressArg_returnFalse)
{
NiceMock<CMockArgParser> argParser;
ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(client_stubParseGenericArgs));
ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(client_stubCheckUnexpectedArgs));
CClientArgs clientArgs;
const int argc = 1;
const char* kNoAddressCmd[argc] = { "stub" };
bool result = argParser.parseClientArgs(clientArgs, argc, kNoAddressCmd);
EXPECT_FALSE(result);
}
TEST(CClientArgsParsingTests, parseClientArgs_unrecognizedArg_returnFalse)
{
NiceMock<CMockArgParser> argParser;
ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(client_stubParseGenericArgs));
ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(client_stubCheckUnexpectedArgs));
CClientArgs clientArgs;
const int argc = 3;
const char* kUnrecognizedCmd[argc] = { "stub", "mock_arg", "mock_address"};
bool result = argParser.parseClientArgs(clientArgs, argc, kUnrecognizedCmd);
EXPECT_FALSE(result);
}

View File

@ -0,0 +1,332 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "synergy/ArgParser.h"
#include "synergy/ArgsBase.h"
#include "test/mock/synergy/MockApp.h"
#include "test/global/gtest.h"
using namespace synergy;
using ::testing::_;
using ::testing::Invoke;
using ::testing::NiceMock;
bool g_helpShowed = false;
bool g_versionShowed = false;
void
showMockHelp()
{
g_helpShowed = true;
}
void
showMockVersion()
{
g_versionShowed = true;
}
TEST(CGenericArgsParsingTests, parseGenericArgs_logLevelCmd_setLogLevel)
{
int i = 1;
const int argc = 3;
const char* kLogLevelCmd[argc] = { "stub", "--debug", "DEBUG" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kLogLevelCmd, i);
CString logFilter(argsBase.m_logFilter);
EXPECT_EQ("DEBUG", logFilter);
EXPECT_EQ(2, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_logFileCmd_saveLogFilename)
{
int i = 1;
const int argc = 3;
const char* kLogFileCmd[argc] = { "stub", "--log", "mock_filename" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kLogFileCmd, i);
CString logFile(argsBase.m_logFile);
EXPECT_EQ("mock_filename", logFile);
EXPECT_EQ(2, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_logFileCmdWithSpace_saveLogFilename)
{
int i = 1;
const int argc = 3;
const char* kLogFileCmdWithSpace[argc] = { "stub", "--log", "mo ck_filename" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kLogFileCmdWithSpace, i);
CString logFile(argsBase.m_logFile);
EXPECT_EQ("mo ck_filename", logFile);
EXPECT_EQ(2, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_noDeamonCmd_daemonFalse)
{
int i = 1;
const int argc = 2;
const char* kNoDeamonCmd[argc] = { "stub", "-f" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kNoDeamonCmd, i);
EXPECT_FALSE(argsBase.m_daemon);
EXPECT_EQ(1, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_deamonCmd_daemonTrue)
{
int i = 1;
const int argc = 2;
const char* kDeamonCmd[argc] = { "stub", "--daemon" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kDeamonCmd, i);
EXPECT_EQ(true, argsBase.m_daemon);
EXPECT_EQ(1, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_nameCmd_saveName)
{
int i = 1;
const int argc = 3;
const char* kNameCmd[argc] = { "stub", "--name", "mock" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kNameCmd, i);
EXPECT_EQ("mock", argsBase.m_name);
EXPECT_EQ(2, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_noRestartCmd_restartFalse)
{
int i = 1;
const int argc = 2;
const char* kNoRestartCmd[argc] = { "stub", "--no-restart" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kNoRestartCmd, i);
EXPECT_FALSE(argsBase.m_restartable);
EXPECT_EQ(1, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_restartCmd_restartTrue)
{
int i = 1;
const int argc = 2;
const char* kRestartCmd[argc] = { "stub", "--restart" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kRestartCmd, i);
EXPECT_EQ(true, argsBase.m_restartable);
EXPECT_EQ(1, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_backendCmd_backendTrue)
{
int i = 1;
const int argc = 2;
const char* kBackendCmd[argc] = { "stub", "-z" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kBackendCmd, i);
EXPECT_EQ(true, argsBase.m_backend);
EXPECT_EQ(1, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_noHookCmd_noHookTrue)
{
int i = 1;
const int argc = 2;
const char* kNoHookCmd[argc] = { "stub", "--no-hooks" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kNoHookCmd, i);
EXPECT_EQ(true, argsBase.m_noHooks);
EXPECT_EQ(1, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_helpCmd_showHelp)
{
g_helpShowed = false;
int i = 1;
const int argc = 2;
const char* kHelpCmd[argc] = { "stub", "--help" };
NiceMock<CMockApp> app;
CArgParser argParser(&app);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
ON_CALL(app, help()).WillByDefault(Invoke(showMockHelp));
argParser.parseGenericArgs(argc, kHelpCmd, i);
EXPECT_EQ(true, g_helpShowed);
EXPECT_EQ(1, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_versionCmd_showVersion)
{
g_versionShowed = false;
int i = 1;
const int argc = 2;
const char* kVersionCmd[argc] = { "stub", "--version" };
NiceMock<CMockApp> app;
CArgParser argParser(&app);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
ON_CALL(app, version()).WillByDefault(Invoke(showMockVersion));
argParser.parseGenericArgs(argc, kVersionCmd, i);
EXPECT_EQ(true, g_versionShowed);
EXPECT_EQ(1, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_noTrayCmd_disableTrayTrue)
{
int i = 1;
const int argc = 2;
const char* kNoTrayCmd[argc] = { "stub", "--no-tray" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kNoTrayCmd, i);
EXPECT_EQ(true, argsBase.m_disableTray);
EXPECT_EQ(1, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_ipcCmd_enableIpcTrue)
{
int i = 1;
const int argc = 2;
const char* kIpcCmd[argc] = { "stub", "--ipc" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kIpcCmd, i);
EXPECT_EQ(true, argsBase.m_enableIpc);
EXPECT_EQ(1, i);
}
TEST(CGenericArgsParsingTests, parseGenericArgs_cryptoPassCmd_savePassword)
{
int i = 1;
const int argc = 3;
const char* kCryptoPassCmd[argc] = { "stub", "--crypto-pass", "mock_password" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kCryptoPassCmd, i);
EXPECT_EQ("mock_password", argsBase.m_crypto.m_pass);
EXPECT_EQ(1, argsBase.m_crypto.m_mode); // 1 == kCfb
EXPECT_EQ(2, i);
}
#ifndef WINAPI_XWINDOWS
TEST(CGenericArgsParsingTests, parseGenericArgs_dragDropCmdOnNonLinux_enableDragDropTrue)
{
int i = 1;
const int argc = 2;
const char* kDragDropCmd[argc] = { "stub", "--enable-drag-drop" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kDragDropCmd, i);
EXPECT_EQ(true, argsBase.m_enableDragDrop);
EXPECT_EQ(1, i);
}
#endif
#ifdef WINAPI_XWINDOWS
TEST(CGenericArgsParsingTests, parseGenericArgs_dragDropCmdOnLinux_enableDragDropFalse)
{
int i = 1;
const int argc = 2;
const char* kDragDropCmd[argc] = { "stub", "--enable-drag-drop" };
CArgParser argParser(NULL);
CArgsBase argsBase;
argParser.setArgsBase(argsBase);
argParser.parseGenericArgs(argc, kDragDropCmd, i);
EXPECT_FALSE(argsBase.m_enableDragDrop);
EXPECT_EQ(1, i);
}
#endif

View File

@ -0,0 +1,66 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "synergy/ArgParser.h"
#include "synergy/ServerArgs.h"
#include "test/mock/synergy/MockArgParser.h"
#include "test/global/gtest.h"
using ::testing::_;
using ::testing::Invoke;
using ::testing::NiceMock;
bool
server_stubParseGenericArgs(int, const char* const*, int&)
{
return false;
}
bool
server_stubCheckUnexpectedArgs()
{
return false;
}
TEST(CServerArgsParsingTests, parseServerArgs_addressArg_setSynergyAddress)
{
NiceMock<CMockArgParser> argParser;
ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(server_stubParseGenericArgs));
ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(server_stubCheckUnexpectedArgs));
CServerArgs serverArgs;
const int argc = 3;
const char* kAddressCmd[argc] = { "stub", "--address", "mock_address" };
argParser.parseServerArgs(serverArgs, argc, kAddressCmd);
EXPECT_EQ("mock_address", serverArgs.m_synergyAddress);
}
TEST(CServerArgsParsingTests, parseServerArgs_configArg_setConfigFile)
{
NiceMock<CMockArgParser> argParser;
ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(server_stubParseGenericArgs));
ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(server_stubCheckUnexpectedArgs));
CServerArgs serverArgs;
const int argc = 3;
const char* kConfigCmd[argc] = { "stub", "--config", "mock_configFile" };
argParser.parseServerArgs(serverArgs, argc, kConfigCmd);
EXPECT_EQ("mock_configFile", serverArgs.m_configFile);
}