X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fclient%2Fclient.cpp;h=f303c1833efe3466c7bf3a6810235f407fb53075;hb=0c7bd9a57f2a308bb9659200eda3b7e45f8d5d3c;hp=105c1e9c0174e983f4dadd8ad2d12fdaaaba7217;hpb=16fd47ad8f5d78a3b7266520b0868c1e2bd0dcac;p=lyx.git diff --git a/src/client/client.cpp b/src/client/client.cpp index 105c1e9c01..f303c1833e 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -12,14 +12,14 @@ #include +#include "support/ConsoleApplication.h" #include "support/debug.h" #include "support/FileName.h" #include "support/FileNameList.h" #include "support/lstrings.h" #include "support/Messages.h" #include "support/unicode.h" - -#include +#include "support/unique_ptr.h" // getpid(), getppid() #ifdef HAVE_SYS_TYPES_H @@ -54,16 +54,16 @@ #include #include #include +#include #include #include #include #include + using namespace std; using namespace lyx::support; -using ::boost::scoped_ptr; - namespace lyx { // Dummy LyXRC support @@ -424,7 +424,17 @@ bool CmdLineParser::parse(int argc, char * argv[]) namespace cmdline { - docstring mainTmp(from_ascii("/tmp")); +docstring mainTmp(from_ascii("/tmp")); + + +class StopException : public exception +{ +public: + StopException(int status) : status_(status) {} + int status() const { return status_; } +private: + int status_; +}; void usage() @@ -444,8 +454,8 @@ void usage() "lyxclient will connect only to a lyx with the specified pid. Options -c and -g\n" "cannot be set simultaneoulsly. If no -c or -g options are given, lyxclient\n" "will read commands from standard input and disconnect when command read is BYE:\n" - "\n" - "System directory is: " << to_utf8(cmdline::mainTmp) + "\n" + "System directory is: " << to_utf8(cmdline::mainTmp) << endl; } @@ -453,7 +463,7 @@ void usage() int h(vector const &) { usage(); - exit(0); + throw StopException(EXIT_SUCCESS); } @@ -462,7 +472,7 @@ docstring clientName = int n(vector const & arg) { - if (arg.size() < 1) { + if (arg.empty()) { cerr << "lyxclient: The option -n requires 1 argument." << endl; return -1; @@ -477,7 +487,7 @@ docstring singleCommand; int c(vector const & arg) { - if (arg.size() < 1) { + if (arg.empty()) { cerr << "lyxclient: The option -c requires 1 argument." << endl; return -1; @@ -507,7 +517,7 @@ docstring serverAddress; int a(vector const & arg) { - if (arg.size() < 1) { + if (arg.empty()) { cerr << "lyxclient: The option -a requires 1 argument." << endl; return -1; @@ -522,7 +532,7 @@ int a(vector const & arg) int t(vector const & arg) { - if (arg.size() < 1) { + if (arg.empty()) { cerr << "lyxclient: The option -t requires 1 argument." << endl; return -1; @@ -537,7 +547,7 @@ string serverPid; // Init to empty string int p(vector const & arg) { - if (arg.size() < 1) { + if (arg.empty()) { cerr << "lyxclient: The option -p requires 1 argument." << endl; return -1; @@ -548,24 +558,47 @@ int p(vector const & arg) } // namespace cmdline -} // namespace lyx - -int main(int argc, char * argv[]) +/// The main application class +class LyXClientApp : public ConsoleApplication { - using namespace lyx; - lyxerr.setStream(cerr); +public: + LyXClientApp(int & argc, char * argv[]) + : ConsoleApplication("client" PROGRAM_SUFFIX, argc, argv), + argc_(argc), argv_(argv) + { + } + void doExec() + { + try { + int const exit_status = run(); + exit(exit_status); + } + catch (cmdline::StopException & e) { + exit(e.status()); + } + } +private: + int run(); + int & argc_; + char ** argv_; +}; - // Set defaults +int LyXClientApp::run() +{ + // qt changes this, and our numeric conversions require the C locale + setlocale(LC_NUMERIC, "C"); + + // Set defaults char const * const lyxsocket = getenv("LYXSOCKET"); if (lyxsocket) cmdline::serverAddress = from_local8bit(lyxsocket); - // Default temporary - cmdline::mainTmp = FileName::tempPath().absoluteFilePath(); + // Default temporary + cmdline::mainTmp = FileName::tempPath().absoluteFilePath(); - // Command line builder + // Command line builder CmdLineParser args; args.helper["-h"] = cmdline::h; args.helper["-c"] = cmdline::c; @@ -576,14 +609,14 @@ int main(int argc, char * argv[]) args.helper["-p"] = cmdline::p; // Command line failure conditions: - if ((!args.parse(argc, argv)) + if ((!args.parse(argc_, argv_)) || (args.isset["-c"] && args.isset["-g"]) || (args.isset["-a"] && args.isset["-p"])) { cmdline::usage(); - return 1; + return EXIT_FAILURE; } - scoped_ptr server; + unique_ptr server; if (!cmdline::serverAddress.empty()) { server.reset(new LyXDataSocket(FileName(to_utf8(cmdline::serverAddress)))); @@ -677,6 +710,17 @@ int main(int argc, char * argv[]) return EXIT_SUCCESS; } +} // namespace lyx + + +int main(int argc, char * argv[]) +{ + lyx::lyxerr.setStream(cerr); + + lyx::LyXClientApp app(argc, argv); + return app.exec(); +} + namespace boost {