2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file lyxserver.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
|
* \author Angus Leeming
|
|
|
|
|
* \author John Levon
|
1999-10-02 16:21:10 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
Docu : To use the lyxserver define the name of the pipe in your
|
2002-03-21 17:27:08 +00:00
|
|
|
|
lyxrc:
|
|
|
|
|
\serverpipe "/home/myhome/.lyxpipe"
|
|
|
|
|
Then use .lyxpipe.in and .lyxpipe.out to communicate to LyX.
|
|
|
|
|
Each message consists of a single line in ASCII. Input lines
|
|
|
|
|
(client -> LyX) have the following format:
|
|
|
|
|
"LYXCMD:<clientname>:<functionname>:<argument>"
|
|
|
|
|
Answers from LyX look like this:
|
|
|
|
|
"INFO:<clientname>:<functionname>:<data>"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
[asierra970531] Or like this in case of error:
|
2002-03-21 17:27:08 +00:00
|
|
|
|
"ERROR:<clientname>:<functionname>:<error message>"
|
|
|
|
|
where <clientname> and <functionname> are just echoed.
|
|
|
|
|
If LyX notifies about a user defined extension key-sequence,
|
|
|
|
|
the line looks like this:
|
|
|
|
|
"NOTIFY:<key-sequence>"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
[asierra970531] New server-only messages to implement a simple protocol
|
2002-03-21 17:27:08 +00:00
|
|
|
|
"LYXSRV:<clientname>:<protocol message>"
|
|
|
|
|
where <protocol message> can be "hello" or "bye". If hello is
|
|
|
|
|
received LyX will inform the client that it's listening its
|
|
|
|
|
messages, and 'bye' will inform that lyx is closing.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
See development/server_monitor.c for an example client.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
Purpose: implement a client/server lib for LyX
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "lyxserver.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
|
#include "debug.h"
|
2003-10-13 00:38:09 +00:00
|
|
|
|
#include "funcrequest.h"
|
|
|
|
|
#include "LyXAction.h"
|
2001-04-24 17:33:01 +00:00
|
|
|
|
#include "lyxfunc.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
|
#include "support/lstrings.h"
|
2000-09-26 13:54:57 +00:00
|
|
|
|
#include "support/lyxlib.h"
|
2002-07-14 01:44:15 +00:00
|
|
|
|
#include "frontends/lyx_gui.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2004-07-22 13:44:40 +00:00
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
2003-09-05 22:17:02 +00:00
|
|
|
|
#include <cerrno>
|
2005-04-26 10:30:24 +00:00
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
|
# include <sys/stat.h>
|
|
|
|
|
#endif
|
2003-09-05 22:17:02 +00:00
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#ifdef __EMX__
|
2005-04-26 10:30:24 +00:00
|
|
|
|
# include <cstdlib>
|
|
|
|
|
# include <io.h>
|
|
|
|
|
# define OS2EMX_PLAIN_CHAR
|
|
|
|
|
# define INCL_DOSNMPIPES
|
|
|
|
|
# define INCL_DOSERRORS
|
|
|
|
|
# include <os2.h>
|
|
|
|
|
# include "support/os2_errortable.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::support::compare;
|
|
|
|
|
using lyx::support::rtrim;
|
|
|
|
|
using lyx::support::split;
|
|
|
|
|
using lyx::support::unlink;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
|
using std::endl;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
|
|
2005-05-02 13:35:30 +00:00
|
|
|
|
#if !defined (HAVE_MKFIFO)
|
|
|
|
|
// We provide a stub class that disables the lyxserver.
|
|
|
|
|
|
|
|
|
|
void LyXComm::openConnection()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::closeConnection()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int LyXComm::startPipe(string const & filename, bool write)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2005-05-02 13:35:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::endPipe(int & fd, string const & filename, bool write)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::emergencyCleanup()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void LyXComm::read_ready()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::send(string const & msg)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#else // defined (HAVE_MKFIFO)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2001-09-09 22:02:19 +00:00
|
|
|
|
void LyXComm::openConnection()
|
|
|
|
|
{
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr[Debug::LYXSERVER] << "LyXComm: Opening connection" << endl;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// If we are up, that's an error
|
|
|
|
|
if (ready) {
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr << "LyXComm: Already connected" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// We assume that we don't make it
|
|
|
|
|
ready = false;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-11-14 12:00:51 +00:00
|
|
|
|
if (pipename.empty()) {
|
|
|
|
|
lyxerr[Debug::LYXSERVER]
|
|
|
|
|
<< "LyXComm: server is disabled, nothing to do"
|
|
|
|
|
<< endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-11-12 17:43:21 +00:00
|
|
|
|
if ((infd = startPipe(inPipeName(), false)) == -1)
|
2001-10-19 15:13:49 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2001-11-12 17:43:21 +00:00
|
|
|
|
if ((outfd = startPipe(outPipeName(), true)) == -1) {
|
2002-07-30 22:35:43 +00:00
|
|
|
|
endPipe(infd, inPipeName(), false);
|
2001-10-19 15:13:49 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
if (fcntl(outfd, F_SETFL, O_NONBLOCK) < 0) {
|
2001-11-12 17:43:21 +00:00
|
|
|
|
lyxerr << "LyXComm: Could not set flags on pipe " << outPipeName()
|
2001-10-19 15:13:49 +00:00
|
|
|
|
<< '\n' << strerror(errno) << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
// We made it!
|
|
|
|
|
ready = true;
|
|
|
|
|
lyxerr[Debug::LYXSERVER] << "LyXComm: Connection established" << endl;
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
/// Close pipes
|
|
|
|
|
void LyXComm::closeConnection()
|
|
|
|
|
{
|
2002-03-21 17:27:08 +00:00
|
|
|
|
lyxerr[Debug::LYXSERVER] << "LyXComm: Closing connection" << endl;
|
2001-10-19 15:13:49 +00:00
|
|
|
|
|
|
|
|
|
if (pipename.empty()) {
|
2001-11-14 12:00:51 +00:00
|
|
|
|
lyxerr[Debug::LYXSERVER]
|
|
|
|
|
<< "LyXComm: server is disabled, nothing to do"
|
|
|
|
|
<< endl;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
return;
|
2001-10-19 15:13:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ready) {
|
|
|
|
|
lyxerr << "LyXComm: Already disconnected" << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2002-07-30 22:35:43 +00:00
|
|
|
|
endPipe(infd, inPipeName(), false);
|
|
|
|
|
endPipe(outfd, outPipeName(), true);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
ready = false;
|
|
|
|
|
}
|
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
|
2001-11-12 17:43:21 +00:00
|
|
|
|
int LyXComm::startPipe(string const & filename, bool write)
|
2001-10-19 15:13:49 +00:00
|
|
|
|
{
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#ifdef __EMX__
|
2001-10-19 15:13:49 +00:00
|
|
|
|
HPIPE os2fd;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
APIRET rc;
|
|
|
|
|
int errnum;
|
|
|
|
|
// Try create one instance of named pipe with the mode O_RDONLY|O_NONBLOCK.
|
|
|
|
|
// The current emx implementation of access() won't work with pipes.
|
2001-10-19 15:13:49 +00:00
|
|
|
|
rc = DosCreateNPipe(filename.c_str(), &os2fd, NP_ACCESS_INBOUND,
|
1999-09-27 18:44:28 +00:00
|
|
|
|
NP_NOWAIT|0x01, 0600, 0600, 0);
|
2001-10-19 15:13:49 +00:00
|
|
|
|
if (rc == ERROR_PIPE_BUSY) {
|
|
|
|
|
lyxerr << "LyXComm: Pipe " << filename << " already exists.\n"
|
1999-10-07 18:44:17 +00:00
|
|
|
|
<< "If no other LyX program is active, please delete"
|
|
|
|
|
" the pipe by hand and try again." << endl;
|
2000-05-04 10:57:00 +00:00
|
|
|
|
pipename.erase();
|
2001-10-19 15:13:49 +00:00
|
|
|
|
return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (rc != NO_ERROR) {
|
|
|
|
|
errnum = TranslateOS2Error(rc);
|
2001-10-19 15:13:49 +00:00
|
|
|
|
lyxerr <<"LyXComm: Could not create pipe " << filename
|
1999-10-07 18:44:17 +00:00
|
|
|
|
<< strerror(errnum) << endl;
|
2001-10-19 15:13:49 +00:00
|
|
|
|
return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
};
|
|
|
|
|
// Listen to it.
|
2001-10-19 15:13:49 +00:00
|
|
|
|
rc = DosConnectNPipe(os2fd);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (rc != NO_ERROR && rc != ERROR_PIPE_NOT_CONNECTED) {
|
|
|
|
|
errnum = TranslateOS2Error(rc);
|
2001-10-19 15:13:49 +00:00
|
|
|
|
lyxerr <<"LyXComm: Could not create pipe " << filename
|
1999-11-01 04:22:28 +00:00
|
|
|
|
<< strerror(errnum) << endl;
|
2001-10-19 15:13:49 +00:00
|
|
|
|
return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
};
|
|
|
|
|
// Imported handles can be used both with OS/2 APIs and emx
|
2002-03-21 17:27:08 +00:00
|
|
|
|
// library functions.
|
2005-02-15 11:04:40 +00:00
|
|
|
|
int const fd = _imphandle(os2fd);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#else
|
2001-10-19 15:13:49 +00:00
|
|
|
|
if (::access(filename.c_str(), F_OK) == 0) {
|
|
|
|
|
lyxerr << "LyXComm: Pipe " << filename << " already exists.\n"
|
1999-10-07 18:44:17 +00:00
|
|
|
|
<< "If no other LyX program is active, please delete"
|
|
|
|
|
" the pipe by hand and try again." << endl;
|
2000-05-04 10:57:00 +00:00
|
|
|
|
pipename.erase();
|
2001-10-19 15:13:49 +00:00
|
|
|
|
return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
if (::mkfifo(filename.c_str(), 0600) < 0) {
|
|
|
|
|
lyxerr << "LyXComm: Could not create pipe " << filename << '\n'
|
1999-10-07 18:44:17 +00:00
|
|
|
|
<< strerror(errno) << endl;
|
2001-10-19 15:13:49 +00:00
|
|
|
|
return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
};
|
2005-02-15 11:04:40 +00:00
|
|
|
|
int const fd = ::open(filename.c_str(),
|
|
|
|
|
write ? (O_RDWR) : (O_RDONLY|O_NONBLOCK));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#endif
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
if (fd < 0) {
|
|
|
|
|
lyxerr << "LyXComm: Could not open pipe " << filename << '\n'
|
1999-10-07 18:44:17 +00:00
|
|
|
|
<< strerror(errno) << endl;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
unlink(filename);
|
2001-10-19 15:13:49 +00:00
|
|
|
|
return -1;
|
2001-09-09 22:02:19 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2002-07-14 01:44:15 +00:00
|
|
|
|
if (!write) {
|
2004-07-23 19:01:48 +00:00
|
|
|
|
lyx_gui::register_socket_callback(fd, boost::bind(&LyXComm::read_ready, this));
|
2002-07-14 01:44:15 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
return fd;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2001-09-09 22:02:19 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
|
2002-07-30 22:35:43 +00:00
|
|
|
|
void LyXComm::endPipe(int & fd, string const & filename, bool write)
|
2001-09-09 22:02:19 +00:00
|
|
|
|
{
|
2001-10-19 15:13:49 +00:00
|
|
|
|
if (fd < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2002-07-30 22:35:43 +00:00
|
|
|
|
if (!write) {
|
2004-07-22 13:44:40 +00:00
|
|
|
|
lyx_gui::unregister_socket_callback(fd);
|
2002-07-30 22:35:43 +00:00
|
|
|
|
}
|
2002-11-27 10:30:28 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#ifdef __EMX__
|
|
|
|
|
APIRET rc;
|
|
|
|
|
int errnum;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
rc = DosDisConnectNPipe(fd);
|
|
|
|
|
if (rc != NO_ERROR) {
|
|
|
|
|
errnum = TranslateOS2Error(rc);
|
|
|
|
|
lyxerr << "LyXComm: Could not disconnect pipe " << filename
|
|
|
|
|
<< '\n' << strerror(errnum) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
if (::close(fd) < 0) {
|
|
|
|
|
lyxerr << "LyXComm: Could not close pipe " << filename
|
|
|
|
|
<< '\n' << strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-10-19 15:13:49 +00:00
|
|
|
|
// OS/2 pipes are deleted automatically
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#ifndef __EMX__
|
2003-06-30 23:56:22 +00:00
|
|
|
|
if (unlink(filename) < 0) {
|
2001-10-19 15:13:49 +00:00
|
|
|
|
lyxerr << "LyXComm: Could not remove pipe " << filename
|
|
|
|
|
<< '\n' << strerror(errno) << endl;
|
|
|
|
|
};
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#endif
|
2001-11-08 11:46:06 +00:00
|
|
|
|
|
|
|
|
|
fd = -1;
|
2001-10-19 15:13:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LyXComm::emergencyCleanup()
|
|
|
|
|
{
|
2001-11-14 12:00:51 +00:00
|
|
|
|
if (!pipename.empty()) {
|
2002-07-30 22:35:43 +00:00
|
|
|
|
endPipe(infd, inPipeName(), false);
|
|
|
|
|
endPipe(outfd, outPipeName(), true);
|
2001-11-14 12:00:51 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2001-09-09 22:02:19 +00:00
|
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Receives messages and sends then to client
|
2002-07-14 01:44:15 +00:00
|
|
|
|
void LyXComm::read_ready()
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2002-09-11 16:48:07 +00:00
|
|
|
|
// nb! make read_buffer_ a class-member for multiple sessions
|
|
|
|
|
static string read_buffer_;
|
|
|
|
|
read_buffer_.erase();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2002-09-11 16:48:07 +00:00
|
|
|
|
int const charbuf_size = 100;
|
|
|
|
|
char charbuf[charbuf_size];
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
int status;
|
|
|
|
|
// the single = is intended here.
|
2002-09-11 16:48:07 +00:00
|
|
|
|
while ((status = ::read(infd, charbuf, charbuf_size - 1))) {
|
|
|
|
|
|
2002-07-14 01:44:15 +00:00
|
|
|
|
if (status > 0) {
|
2002-09-11 16:48:07 +00:00
|
|
|
|
charbuf[status] = '\0'; // turn it into a c string
|
|
|
|
|
read_buffer_ += rtrim(charbuf, "\r");
|
2003-02-06 01:18:36 +00:00
|
|
|
|
// commit any commands read
|
|
|
|
|
while (read_buffer_.find('\n') != string::npos) {
|
|
|
|
|
// split() grabs the entire string if
|
|
|
|
|
// the delim /wasn't/ found. ?:-P
|
|
|
|
|
string cmd;
|
|
|
|
|
read_buffer_= split(read_buffer_, cmd,'\n');
|
|
|
|
|
lyxerr[Debug::LYXSERVER]
|
|
|
|
|
<< "LyXComm: status:" << status
|
|
|
|
|
<< ", read_buffer_:" << read_buffer_
|
|
|
|
|
<< ", cmd:" << cmd << endl;
|
|
|
|
|
if (!cmd.empty())
|
|
|
|
|
clientcb(client, cmd);
|
|
|
|
|
//\n or not \n?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
|
errno = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (errno != 0) {
|
|
|
|
|
lyxerr << "LyXComm: " << strerror(errno) << endl;
|
2002-09-11 16:48:07 +00:00
|
|
|
|
if (!read_buffer_.empty()) {
|
2002-08-04 23:11:50 +00:00
|
|
|
|
lyxerr << "LyXComm: truncated command: "
|
2003-02-06 01:18:36 +00:00
|
|
|
|
<< read_buffer_ << endl;
|
2002-09-11 16:48:07 +00:00
|
|
|
|
read_buffer_.erase();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2003-02-06 01:18:36 +00:00
|
|
|
|
break; // reset connection
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-09-11 16:48:07 +00:00
|
|
|
|
|
2003-02-06 01:18:36 +00:00
|
|
|
|
// The connection gets reset in errno != EAGAIN
|
|
|
|
|
// Why does it need to be reset if errno == 0?
|
|
|
|
|
closeConnection();
|
|
|
|
|
openConnection();
|
2002-09-11 16:48:07 +00:00
|
|
|
|
errno = 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
1999-10-25 14:18:30 +00:00
|
|
|
|
|
|
|
|
|
|
2001-09-09 22:02:19 +00:00
|
|
|
|
void LyXComm::send(string const & msg)
|
|
|
|
|
{
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (msg.empty()) {
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr << "LyXComm: Request to send empty string. Ignoring."
|
|
|
|
|
<< endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-07 18:44:17 +00:00
|
|
|
|
if (lyxerr.debugging(Debug::LYXSERVER)) {
|
|
|
|
|
lyxerr << "LyXComm: Sending '" << msg << '\'' << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pipename.empty()) return;
|
|
|
|
|
|
|
|
|
|
if (!ready) {
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr << "LyXComm: Pipes are closed. Could not send "
|
|
|
|
|
<< msg << endl;
|
2000-09-26 13:54:57 +00:00
|
|
|
|
} else if (::write(outfd, msg.c_str(), msg.length()) < 0) {
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr << "LyXComm: Error sending message: " << msg
|
|
|
|
|
<< '\n' << strerror(errno)
|
|
|
|
|
<< "\nLyXComm: Resetting connection" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
closeConnection();
|
|
|
|
|
openConnection();
|
|
|
|
|
}
|
|
|
|
|
#ifdef __EMX__
|
|
|
|
|
APIRET rc;
|
|
|
|
|
int errnum;
|
|
|
|
|
rc = DosResetBuffer(outfd); // To avoid synchronization problems.
|
|
|
|
|
if (rc != NO_ERROR) {
|
|
|
|
|
errnum = TranslateOS2Error(rc);
|
1999-11-01 04:22:28 +00:00
|
|
|
|
lyxerr << "LyXComm: Message could not be flushed: " << msg
|
|
|
|
|
<< '\n' << strerror(errnum) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-02 13:35:30 +00:00
|
|
|
|
#endif // defined (HAVE_MKFIFO)
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2005-01-20 15:07:36 +00:00
|
|
|
|
string const LyXComm::inPipeName() const
|
|
|
|
|
{
|
|
|
|
|
return pipename + string(".in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const LyXComm::outPipeName() const
|
|
|
|
|
{
|
|
|
|
|
return pipename + string(".out");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// LyXServer class
|
|
|
|
|
|
|
|
|
|
LyXServer::~LyXServer()
|
|
|
|
|
{
|
|
|
|
|
// say goodbye to clients so they stop sending messages
|
|
|
|
|
// modified june 1999 by stefano@zool.su.se to send as many bye
|
|
|
|
|
// messages as there are clients, each with client's name.
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string message;
|
2000-01-24 18:34:46 +00:00
|
|
|
|
for (int i= 0; i<numclients; ++i) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
message = "LYXSRV:" + clients[i] + ":bye\n";
|
|
|
|
|
pipes.send(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
|
Function : ServerCallback
|
|
|
|
|
Called by : LyXComm
|
|
|
|
|
Purpose : handle data gotten from communication
|
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
void LyXServer::callback(LyXServer * serv, string const & msg)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr[Debug::LYXSERVER] << "LyXServer: Received: '"
|
|
|
|
|
<< msg << '\'' << endl;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
|
char const * p = msg.c_str();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// --- parse the string --------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Format: LYXCMD:<client>:<func>:<argstring>\n
|
|
|
|
|
//
|
|
|
|
|
bool server_only = false;
|
2001-12-05 08:04:20 +00:00
|
|
|
|
while (*p) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// --- 1. check 'header' ---
|
2000-04-11 16:57:16 +00:00
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
if (compare(p, "LYXSRV:", 7) == 0) {
|
|
|
|
|
server_only = true;
|
2001-02-16 09:25:43 +00:00
|
|
|
|
} else if (0 != compare(p, "LYXCMD:", 7)) {
|
2002-11-27 10:30:28 +00:00
|
|
|
|
lyxerr << "LyXServer: Unknown request \""
|
|
|
|
|
<< p << '"' << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
p += 7;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// --- 2. for the moment ignore the client name ---
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string client;
|
2001-12-05 08:04:20 +00:00
|
|
|
|
while (*p && *p != ':')
|
1999-09-27 18:44:28 +00:00
|
|
|
|
client += char(*p++);
|
2000-11-04 10:00:12 +00:00
|
|
|
|
if (*p == ':') ++p;
|
|
|
|
|
if (!*p) return;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// --- 3. get function name ---
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string cmd;
|
2001-12-05 08:04:20 +00:00
|
|
|
|
while (*p && *p != ':')
|
1999-09-27 18:44:28 +00:00
|
|
|
|
cmd += char(*p++);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// --- 4. parse the argument ---
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string arg;
|
2000-11-04 10:00:12 +00:00
|
|
|
|
if (!server_only && *p == ':' && *(++p)) {
|
2001-12-05 08:04:20 +00:00
|
|
|
|
while (*p && *p != '\n')
|
1999-09-27 18:44:28 +00:00
|
|
|
|
arg += char(*p++);
|
2000-11-04 10:00:12 +00:00
|
|
|
|
if (*p) ++p;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr[Debug::LYXSERVER]
|
|
|
|
|
<< "LyXServer: Client: '" << client
|
|
|
|
|
<< "' Command: '" << cmd
|
|
|
|
|
<< "' Argument: '" << arg << '\'' << endl;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// --- lookup and exec the command ------------------
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (server_only) {
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string buf;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
// return the greeting to inform the client that
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// we are listening.
|
|
|
|
|
if (cmd == "hello") {
|
|
|
|
|
// One more client
|
2002-02-16 15:59:55 +00:00
|
|
|
|
if (serv->numclients == MAX_CLIENTS) { //paranoid check
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr[Debug::LYXSERVER]
|
|
|
|
|
<< "LyXServer: too many clients..."
|
|
|
|
|
<< endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
1999-11-15 12:01:38 +00:00
|
|
|
|
int i= 0; //find place in clients[]
|
2002-03-21 17:27:08 +00:00
|
|
|
|
while (!serv->clients[i].empty()
|
|
|
|
|
&& i<serv->numclients)
|
2000-01-24 18:34:46 +00:00
|
|
|
|
++i;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
serv->clients[i] = client;
|
|
|
|
|
serv->numclients++;
|
|
|
|
|
buf = "LYXSRV:" + client + ":hello\n";
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr[Debug::LYXSERVER]
|
|
|
|
|
<< "LyXServer: Greeting "
|
|
|
|
|
<< client << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
serv->pipes.send(buf);
|
|
|
|
|
} else if (cmd == "bye") {
|
1999-11-15 12:01:38 +00:00
|
|
|
|
// If clients == 0 maybe we should reset the pipes
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// to prevent fake callbacks
|
2000-01-24 18:34:46 +00:00
|
|
|
|
int i = 0; //look if client is registered
|
|
|
|
|
for (; i < serv->numclients; ++i) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (serv->clients[i] == client) break;
|
|
|
|
|
}
|
2000-01-24 18:34:46 +00:00
|
|
|
|
if (i < serv->numclients) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
serv->numclients--;
|
2000-05-04 10:57:00 +00:00
|
|
|
|
serv->clients[i].erase();
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr[Debug::LYXSERVER]
|
|
|
|
|
<< "LyXServer: Client "
|
|
|
|
|
<< client << " said goodbye"
|
|
|
|
|
<< endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
} else {
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr[Debug::LYXSERVER]
|
|
|
|
|
<< "LyXServer: ignoring bye messge from unregistered client"
|
|
|
|
|
<< client << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr <<"LyXServer: Undefined server command "
|
2002-11-27 10:30:28 +00:00
|
|
|
|
<< cmd << '.' << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (!cmd.empty()) {
|
|
|
|
|
// which lyxfunc should we let it connect to?
|
|
|
|
|
// The correct solution would be to have a
|
|
|
|
|
// specialized (non-gui) BufferView. But how do
|
|
|
|
|
// we do it now? Probably we should just let it
|
|
|
|
|
// connect to the lyxfunc in the single LyXView we
|
|
|
|
|
// support currently. (Lgb)
|
|
|
|
|
|
|
|
|
|
|
2003-10-13 00:38:09 +00:00
|
|
|
|
serv->func->dispatch(FuncRequest(lyxaction.lookupFunc(cmd), arg));
|
2002-05-28 23:04:57 +00:00
|
|
|
|
string const rval = serv->func->getMessage();
|
2002-07-28 18:13:51 +00:00
|
|
|
|
|
2002-05-28 23:04:57 +00:00
|
|
|
|
//modified june 1999 stefano@zool.su.se:
|
1999-09-27 18:44:28 +00:00
|
|
|
|
//all commands produce an INFO or ERROR message
|
|
|
|
|
//in the output pipe, even if they do not return
|
|
|
|
|
//anything. See chapter 4 of Customization doc.
|
2002-05-28 23:04:57 +00:00
|
|
|
|
string buf;
|
|
|
|
|
if (serv->func->errorStat())
|
1999-09-27 18:44:28 +00:00
|
|
|
|
buf = "ERROR:";
|
|
|
|
|
else
|
|
|
|
|
buf = "INFO:";
|
2002-11-27 10:30:28 +00:00
|
|
|
|
buf += client + ':' + cmd + ':' + rval + '\n';
|
1999-09-27 18:44:28 +00:00
|
|
|
|
serv->pipes.send(buf);
|
|
|
|
|
|
|
|
|
|
// !!! we don't do any error checking -
|
|
|
|
|
// if the client won't listen, the
|
|
|
|
|
// message is lost and others too
|
|
|
|
|
// maybe; so the client should empty
|
|
|
|
|
// the outpipe before issuing a request.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// not found
|
|
|
|
|
}
|
|
|
|
|
} /* while *p */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
2002-08-04 23:11:50 +00:00
|
|
|
|
Function : LyXNotifyClient
|
1999-09-27 18:44:28 +00:00
|
|
|
|
Called by : WorkAreaKeyPress
|
|
|
|
|
Purpose : send a notify messge to a client
|
|
|
|
|
Parameters: s - string to send
|
|
|
|
|
Returns : nothing
|
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
void LyXServer::notifyClient(string const & s)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string buf = string("NOTIFY:") + s + "\n";
|
1999-09-27 18:44:28 +00:00
|
|
|
|
pipes.send(buf);
|
|
|
|
|
}
|