Skip to content

Commit

Permalink
Parse arg refactoring deskflow#4124
Browse files Browse the repository at this point in the history
Extracted platform specific argument parsing code.
  • Loading branch information
Xinyu Hou committed Oct 22, 2014
1 parent 326901e commit d2814a4
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/lib/base/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdarg.h>

#define CLOG (CLog::getInstance())
#define BYE "\nTry `%s --help' for more information."

class ILogOutputter;
class CThread;
Expand Down
2 changes: 0 additions & 2 deletions src/lib/synergy/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ class CApp : public IApp {
CSocketMultiplexer* m_socketMultiplexer;
};

#define BYE "\nTry `%s --help' for more information."

#if WINAPI_MSWINDOWS
#define DAEMON_RUNNING(running_) CArchMiscWindows::daemonRunning(running_)
#else
Expand Down
87 changes: 87 additions & 0 deletions src/lib/synergy/ArgParser.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#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;
}
38 changes: 38 additions & 0 deletions src/lib/synergy/ArgParser.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#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;
};
3 changes: 2 additions & 1 deletion src/lib/synergy/ArgsBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/synergy/ArgsBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ class CArgsBase {
#if WINAPI_XWINDOWS
bool m_disableXInitThreads;
#endif
bool m_shouldExit;
};

0 comments on commit d2814a4

Please sign in to comment.