* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
- * \author João Luis M. Assirati
- * \author Lars Gullik Bjønnes
+ * \author João Luis M. Assirati
+ * \author Lars Gullik Bjønnes
*
* Full author contact details are available in file CREDITS.
*/
#include "support/debug.h"
#include "support/FileName.h"
-#include "support/unicode.h"
+#include "support/FileNameList.h"
#include "support/lstrings.h"
+#include "support/Messages.h"
+#include "support/unicode.h"
-#include <boost/filesystem/operations.hpp>
#include <boost/scoped_ptr.hpp>
// getpid(), getppid()
// fcntl()
#include <fcntl.h>
+// strerror()
+#include <string.h>
+
#include <cerrno>
+#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <iostream>
+using namespace std;
+using namespace lyx::support;
+
+using ::boost::scoped_ptr;
namespace lyx {
-using support::FileName;
-using support::prefixIs;
+// Dummy LyXRC support
+struct LyXRC {
+ string icon_set;
+} lyxrc;
+
+// Keep the linker happy on Windows
+void lyx_exit(int)
+{}
+
+// Dummy language support
+Messages const & getGuiMessages()
+{
+ static Messages lyx_messages;
+
+ return lyx_messages;
+}
-using ::boost::scoped_ptr;
-namespace fs = ::boost::filesystem;
-using std::string;
-using std::vector;
-using std::cout;
-using std::cerr;
-using std::cin;
-using std::endl;
+Messages const & getMessages(string const &)
+{
+ return getGuiMessages();
+}
namespace support {
/// Returns the absolute pathnames of all lyx local sockets in
/// file system encoding.
/// Parts stolen from lyx::support::DirList().
-vector<FileName> lyxSockets(string const & dir, string const & pid)
+FileNameList lyxSockets(string const & dir, string const & pid)
{
- FileName dirpath(dir);
+ FileNameList dirlist;
+
+ FileName dirpath(dir + "/");
- vector<FileName> dirlist = dirpath.dirList();
- vector<FileName>::iterator it = dirlist.begin();
- vector<FileName>::iterator const end = dirlist.end();
+ if (!dirpath.exists() || !dirpath.isDirectory()) {
+ lyxerr << dir << " does not exist or is not a directory."
+ << endl;
+ return dirlist;
+ }
+
+ FileNameList dirs = dirpath.dirList("");
+ FileNameList::const_iterator it = dirs.begin();
+ FileNameList::const_iterator end = dirs.end();
for (; it != end; ++it) {
- if (prefixIs(it->baseName(), "lyx_tmpdir" + pid)) {
- FileName lyxsocket(it->abs() + "lyxsocket";
- if (fs::exists(lyxsocket)) {
- dirlist.push_back(lyxsocket);
- }
- }
+ if (!it->isDirectory())
+ continue;
+ string const tmpdir = it->absFileName();
+ if (!contains(tmpdir, "lyx_tmpdir" + pid))
+ continue;
+
+ FileName lyxsocket(tmpdir + "/lyxsocket");
+ if (lyxsocket.exists())
+ dirlist.push_back(lyxsocket);
}
return dirlist;
if (::connect(fd,
reinterpret_cast<struct sockaddr *>(&addr),
sizeof(addr)) == -1) {
- cerr << "lyxclient: Could not connect to socket " << name.absFilename()
+ cerr << "lyxclient: Could not connect to socket " << name.absFileName()
<< ": " << strerror(errno) << endl;
::close(fd);
return -1;
LyXDataSocket::LyXDataSocket(FileName const & address)
{
- if ((fd_ = support::socktools::connect(address)) == -1) {
+ if ((fd_ = socktools::connect(address)) == -1) {
connected_ = false;
} else {
connected_ = true;
// Returns true if there was a complete line to input
// A line is of the form <key>:<value>
// A line not of this form will not be passed
-// The line read is splitted and stored in 'key' and 'value'
+// The line read is split and stored in 'key' and 'value'
bool LyXDataSocket::readln(string & line)
{
int const charbuf_size = 100;
class CmdLineParser {
public:
typedef int (*optfunc)(vector<docstring> const & args);
- std::map<string, optfunc> helper;
- std::map<string, bool> isset;
+ map<string, optfunc> helper;
+ map<string, bool> isset;
bool parse(int, char * []);
vector<char *> nonopt;
};
namespace cmdline {
+ docstring mainTmp(from_ascii("/tmp"));
+
+
void usage()
{
cerr <<
"Usage: lyxclient [options]\n"
"Options are:\n"
" -a address set address of the lyx socket\n"
- " -t directory set system temporary directory\n"
+ " -t directory set system temporary directory (for detecting sockets)\n"
" -p pid select a running lyx by pidi\n"
- " -c command send a single command and quit\n"
+ " -c command send a single command and quit (LYXCMD prefix needed)\n"
" -g file row send a command to go to file and row\n"
" -n name set client name\n"
" -h name display this help end exit\n"
"If -a is not used, lyxclient will use the arguments of -t and -p to look for\n"
- "a running lyx. If -t is not set, 'directory' defaults to /tmp. If -p is set,\n"
+ "a running lyx. If -t is not set, 'directory' defaults to the system directory. If -p is set,\n"
"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:"
+ "will read commands from standard input and disconnect when command read is BYE:\n"
+ "\n"
+ "System directory is: " << to_utf8(cmdline::mainTmp)
<< endl;
}
docstring clientName =
- from_ascii(support::itoa(::getppid()) + ">" + support::itoa(::getpid()));
+ from_ascii(itoa(::getppid()) + ">" + itoa(::getpid()));
int n(vector<docstring> const & arg)
{
- if (arg.size() < 1) {
+ if (arg.empty()) {
cerr << "lyxclient: The option -n requires 1 argument."
<< endl;
return -1;
int c(vector<docstring> const & arg)
{
- if (arg.size() < 1) {
+ if (arg.empty()) {
cerr << "lyxclient: The option -c requires 1 argument."
<< endl;
return -1;
int a(vector<docstring> const & arg)
{
- if (arg.size() < 1) {
+ if (arg.empty()) {
cerr << "lyxclient: The option -a requires 1 argument."
<< endl;
return -1;
}
-docstring mainTmp(from_ascii("/tmp"));
int t(vector<docstring> const & arg)
{
- if (arg.size() < 1) {
+ if (arg.empty()) {
cerr << "lyxclient: The option -t requires 1 argument."
<< endl;
return -1;
int p(vector<docstring> const & arg)
{
- if (arg.size() < 1) {
+ if (arg.empty()) {
cerr << "lyxclient: The option -p requires 1 argument."
<< endl;
return -1;
using namespace lyx;
lyxerr.setStream(cerr);
+
+ // Set defaults
char const * const lyxsocket = getenv("LYXSOCKET");
if (lyxsocket)
cmdline::serverAddress = from_local8bit(lyxsocket);
+ // Default temporary
+ cmdline::mainTmp = FileName::tempPath().absoluteFilePath();
+
+ // Command line builder
CmdLineParser args;
args.helper["-h"] = cmdline::h;
args.helper["-c"] = cmdline::c;
} else {
// We have to look for an address.
// serverPid can be empty.
- vector<FileName> addrs = support::lyxSockets(to_filesystem8bit(cmdline::mainTmp), cmdline::serverPid);
- vector<FileName>::const_iterator addr = addrs.begin();
- vector<FileName>::const_iterator end = addrs.end();
+ FileNameList addrs = lyxSockets(to_filesystem8bit(cmdline::mainTmp), cmdline::serverPid);
+ FileNameList::const_iterator addr = addrs.begin();
+ FileNameList::const_iterator end = addrs.end();
for (; addr != end; ++addr) {
// Caution: addr->string() is in filesystem encoding
server.reset(new LyXDataSocket(*addr));
<< endl;
return EXIT_FAILURE;
}
- cerr << "lyxclient: " << "Connected to " << addr->string() << endl;
+ cerr << "lyxclient: " << "Connected to " << addr->absFileName() << endl;
}
int const serverfd = server->fd();
iowatch.wait();
if (iowatch.isset(0)) {
string command;
- cin >> command;
+ getline(cin, command);
+ if (command.empty())
+ continue;
if (command == "BYE:") {
server->writeln("BYE:");
saidbye = true;