Parse arg refactoring #4124

Used argParser to parse arguments and removed the old code
This commit is contained in:
Xinyu Hou 2014-10-23 11:30:51 +01:00
parent e26ccfdce6
commit 943a355359
14 changed files with 80 additions and 458 deletions

View File

@ -17,6 +17,7 @@
*/
#include "synergy/App.h"
#include "base/Log.h"
#include "common/Version.h"
#include "synergy/protocol_types.h"
@ -51,6 +52,10 @@
CApp* CApp::s_instance = nullptr;
//
// CApp
//
CApp::CApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver, CArgsBase* args) :
m_bye(&exit),
m_taskBarReceiver(NULL),
@ -71,202 +76,6 @@ CApp::~CApp()
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
CApp::version()
{
@ -364,6 +173,18 @@ CApp::initApp(int argc, const char** argv)
// parse command line
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
setupFileLogging();

View File

@ -70,11 +70,6 @@ public:
// TODO: this is old C code - use inheritance to normalize
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; }
// If --log was specified in args, then add a file logger.
@ -105,8 +100,6 @@ private:
void handleIpcMessage(const CEvent&, void*);
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 cleanupIpcClient();
void runEventsLoop(void*);

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
CAppUtil::adoptApp(IApp* app)
{

View File

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

View File

@ -19,9 +19,11 @@
#include "synergy/ClientApp.h"
#include "client/Client.h"
#include "synergy/ArgParser.h"
#include "synergy/protocol_types.h"
#include "synergy/Screen.h"
#include "synergy/XScreen.h"
#include "synergy/ClientArgs.h"
#include "net/NetworkAddress.h"
#include "net/TCPSocketFactory.h"
#include "net/SocketMultiplexer.h"
@ -61,9 +63,10 @@
#define RETRY_TIME 1.0
CClientApp::CClientApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver) :
CApp(events, createTaskBarReceiver, new CArgs()),
CApp(events, createTaskBarReceiver, new CClientArgs()),
m_client(NULL),
m_clientScreen(NULL)
m_clientScreen(NULL),
m_serverAddress(NULL)
{
}
@ -71,97 +74,35 @@ 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
CClientApp::parseArgs(int argc, const char* const* argv)
{
// asserts values, sets defaults, and parses args
int i;
CApp::parseArgs(argc, argv, i);
CArgParser argParser(this);
bool result = argParser.parseClientArgs(args(), argc, argv);
// 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));
if (!result || args().m_shouldExit) {
m_bye(kExitArgs);
}
if (i + 1 != argc) {
LOG((CLOG_PRINT "%s: unrecognized option `%s'" BYE,
args().m_pname, argv[i], args().m_pname));
m_bye(kExitArgs);
}
// save server address
try {
*args().m_serverAddress = CNetworkAddress(argv[i], kDefaultPort);
args().m_serverAddress->resolve();
}
catch (XSocketAddress& e) {
// allow an address that we can't look up if we're restartable.
// we'll try to resolve the address each time we connect to the
// server. a bad port will never get better. patch by Brent
// Priddy.
if (!args().m_restartable || e.getError() == XSocketAddress::kBadPort) {
LOG((CLOG_PRINT "%s: %s" BYE,
args().m_pname, e.what(), args().m_pname));
m_bye(kExitFailed);
else {
// save server address
if (!args().m_synergyAddress.empty()) {
try {
*m_serverAddress = CNetworkAddress(args().m_synergyAddress, kDefaultPort);
m_serverAddress->resolve();
}
catch (XSocketAddress& e) {
// allow an address that we can't look up if we're restartable.
// we'll try to resolve the address each time we connect to the
// server. a bad port will never get better. patch by Brent
// Priddy.
if (!args().m_restartable || e.getError() == XSocketAddress::kBadPort) {
LOG((CLOG_PRINT "%s: %s" BYE,
args().m_pname, e.what(), args().m_pname));
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
@ -460,7 +401,7 @@ CClientApp::startClient()
if (m_clientScreen == NULL) {
clientScreen = openClientScreen();
m_client = openClient(args().m_name,
*args().m_serverAddress, clientScreen, args().m_crypto);
*m_serverAddress, clientScreen, args().m_crypto);
m_clientScreen = clientScreen;
LOG((CLOG_NOTE "started client"));
}
@ -590,7 +531,7 @@ int
CClientApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc startup)
{
// general initialization
args().m_serverAddress = new CNetworkAddress;
m_serverAddress = new CNetworkAddress;
args().m_pname = ARCH->getBasename(argv[0]);
// install caller's output filter
@ -612,7 +553,7 @@ CClientApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFun
delete m_taskBarReceiver;
}
delete args().m_serverAddress;
delete m_serverAddress;
throw;
}

View File

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

View File

@ -32,9 +32,6 @@ class IApp : public IInterface
{
public:
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 int standardStartup(int argc, char** argv) = 0;
virtual int runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc startup) = 0;

View File

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

View File

@ -22,9 +22,11 @@
#include "server/ClientListener.h"
#include "server/ClientProxy.h"
#include "server/PrimaryClient.h"
#include "synergy/ArgParser.h"
#include "synergy/Screen.h"
#include "synergy/XScreen.h"
#include "synergy/ServerTaskBarReceiver.h"
#include "synergy/ServerArgs.h"
#include "net/SocketMultiplexer.h"
#include "net/TCPSocketFactory.h"
#include "net/XSocket.h"
@ -63,13 +65,14 @@
//
CServerApp::CServerApp(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver) :
CApp(events, createTaskBarReceiver, new CArgs()),
CApp(events, createTaskBarReceiver, new CServerArgs()),
m_server(NULL),
m_serverState(kUninitialized),
m_serverScreen(NULL),
m_primaryClient(NULL),
m_listener(NULL),
m_timer(NULL)
m_timer(NULL),
m_synergyAddress(NULL)
{
}
@ -77,82 +80,29 @@ CServerApp::~CServerApp()
{
}
CServerApp::CArgs::CArgs() :
m_synergyAddress(NULL),
m_config(NULL)
{
}
CServerApp::CArgs::~CArgs()
{
}
bool
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 {
*args().m_synergyAddress = CNetworkAddress(argv[i + 1],
kDefaultPort);
args().m_synergyAddress->resolve();
}
catch (XSocketAddress& e) {
LOG((CLOG_PRINT "%s: %s" BYE,
args().m_pname, e.what(), args().m_pname));
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);
CArgParser argParser(this);
bool result = argParser.parseServerArgs(args(), argc, argv);
// 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));
if (!result || args().m_shouldExit) {
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);
else {
if (!args().m_synergyAddress.empty()) {
try {
*m_synergyAddress = CNetworkAddress(args().m_synergyAddress,
kDefaultPort);
m_synergyAddress->resolve();
}
catch (XSocketAddress& e) {
LOG((CLOG_PRINT "%s: %s" BYE,
args().m_pname, e.what(), 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
@ -742,8 +692,8 @@ CServerApp::mainLoop()
// set the contact address, if provided, in the config.
// otherwise, if the config doesn't have an address, use
// the default.
if (args().m_synergyAddress->isValid()) {
args().m_config->setSynergyAddress(*args().m_synergyAddress);
if (m_synergyAddress->isValid()) {
args().m_config->setSynergyAddress(*m_synergyAddress);
}
else if (!args().m_config->getSynergyAddress().isValid()) {
args().m_config->setSynergyAddress(CNetworkAddress(kDefaultPort));
@ -839,7 +789,7 @@ int
CServerApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc startup)
{
// general initialization
args().m_synergyAddress = new CNetworkAddress;
m_synergyAddress = new CNetworkAddress;
args().m_config = new CConfig(m_events);
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_synergyAddress;
delete m_synergyAddress;
return result;
}

View File

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

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
standardStartupStatic(int argc, char** argv)
{

View File

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

View File

@ -58,29 +58,6 @@ BOOL WINAPI CAppUtilWindows::consoleHandler(DWORD)
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
int
mainLoopStatic()

View File

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