1999-09-27 18:44:28 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/* This file is part of
|
1999-11-15 12:01:38 +00:00
|
|
|
* ======================================================
|
1999-10-02 16:21:10 +00:00
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
*
|
|
|
|
* Copyright 1995 Matthias Ettrich
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
1999-10-02 16:21:10 +00:00
|
|
|
*
|
1999-11-15 12:01:38 +00:00
|
|
|
* ====================================================== */
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Docu : To use the lyxserver define the name of the pipe in your
|
|
|
|
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>"
|
|
|
|
[asierra970531] Or like this in case of error:
|
|
|
|
"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>"
|
|
|
|
[asierra970531] New server-only messages to implement a simple protocol
|
|
|
|
"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.
|
|
|
|
|
|
|
|
See development/server_monitor.c for an example client.
|
|
|
|
Purpose: implement a client/server lib for LyX
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2001-05-31 02:23:46 +00:00
|
|
|
//#include <cstring>
|
1999-09-27 18:44:28 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <cerrno>
|
1999-09-27 18:44:28 +00:00
|
|
|
#include FORMS_H_LOCATION
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "lyxserver.h"
|
|
|
|
#include "lyx_main.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
#include "debug.h"
|
1999-11-22 16:19:48 +00:00
|
|
|
#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"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __EMX__
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <cstdlib>
|
1999-09-27 18:44:28 +00:00
|
|
|
#include <io.h>
|
|
|
|
#define OS2EMX_PLAIN_CHAR
|
|
|
|
#define INCL_DOSNMPIPES
|
|
|
|
#define INCL_DOSERRORS
|
|
|
|
#include <os2.h>
|
|
|
|
#include "os2_errortable.h"
|
|
|
|
#endif
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::endl;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// provide an empty mkfifo() if we do not have one. This disables the
|
|
|
|
// lyxserver.
|
|
|
|
#ifndef HAVE_MKFIFO
|
2000-11-04 10:00:12 +00:00
|
|
|
int mkfifo(char const * __path, mode_t __mode) {
|
2000-10-12 00:11:06 +00:00
|
|
|
return 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* === variables ========================================================= */
|
|
|
|
|
|
|
|
extern LyXAction lyxaction;
|
|
|
|
|
1999-10-25 14:18:30 +00:00
|
|
|
// C wrapper
|
|
|
|
extern "C" void C_LyXComm_callback(int fd, void *v);
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// LyXComm class
|
|
|
|
|
|
|
|
// Open pipes
|
|
|
|
void LyXComm::openConnection() {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::LYXSERVER] << "LyXComm: Opening connection" << endl;
|
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;
|
|
|
|
|
|
|
|
if (pipename.empty()) return;
|
|
|
|
|
|
|
|
// --- prepare input pipe ---------------------------------------
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmp = pipename + ".in";
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __EMX__
|
|
|
|
HPIPE fd;
|
|
|
|
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.
|
|
|
|
rc = DosCreateNPipe(tmp.c_str(), &fd, NP_ACCESS_INBOUND,
|
|
|
|
NP_NOWAIT|0x01, 0600, 0600, 0);
|
|
|
|
if (rc == ERROR_PIPE_BUSY) {
|
|
|
|
#else
|
2000-09-26 13:54:57 +00:00
|
|
|
if (::access(tmp.c_str(), F_OK) == 0) {
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Pipe " << tmp << " already exists.\n"
|
|
|
|
<< "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();
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifndef __EMX__
|
2000-09-26 13:54:57 +00:00
|
|
|
if (::mkfifo(tmp.c_str(), 0600) < 0) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not create pipe " << tmp << '\n'
|
|
|
|
<< strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
};
|
2000-09-26 13:54:57 +00:00
|
|
|
infd = ::open(tmp.c_str(), O_RDONLY|O_NONBLOCK);
|
1999-09-27 18:44:28 +00:00
|
|
|
#else
|
|
|
|
if (rc != NO_ERROR) {
|
|
|
|
errnum = TranslateOS2Error(rc);
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr <<"LyXComm: Could not create pipe " << tmp
|
|
|
|
<< strerror(errnum) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
// Listen to it.
|
|
|
|
rc = DosConnectNPipe(fd);
|
|
|
|
if (rc != NO_ERROR && rc != ERROR_PIPE_NOT_CONNECTED) {
|
|
|
|
errnum = TranslateOS2Error(rc);
|
1999-11-01 04:22:28 +00:00
|
|
|
lyxerr <<"LyXComm: Could not create pipe " << tmp
|
|
|
|
<< strerror(errnum) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
// Imported handles can be used both with OS/2 APIs and emx
|
|
|
|
// library functions.
|
|
|
|
infd = _imphandle(fd);
|
|
|
|
#endif
|
|
|
|
if (infd < 0) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not open pipe " << tmp << '\n'
|
|
|
|
<< strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
1999-12-13 00:05:34 +00:00
|
|
|
fl_add_io_callback(infd, FL_READ, C_LyXComm_callback, this);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// --- prepare output pipe ---------------------------------------
|
|
|
|
|
|
|
|
tmp = pipename + ".out";
|
|
|
|
|
|
|
|
#ifndef __EMX__
|
2000-09-26 13:54:57 +00:00
|
|
|
if (::access(tmp.c_str(), F_OK) == 0) {
|
1999-09-27 18:44:28 +00:00
|
|
|
#else
|
|
|
|
rc = DosCreateNPipe(tmp.c_str(), &fd, NP_ACCESS_DUPLEX,
|
|
|
|
NP_NOWAIT|0x01, 0600, 0600, 0);
|
|
|
|
|
|
|
|
if (rc == ERROR_PIPE_BUSY) {
|
|
|
|
#endif
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Pipe " << tmp << " already exists.\n"
|
|
|
|
<< "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();
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifndef __EMX__
|
2000-09-26 13:54:57 +00:00
|
|
|
if (::mkfifo(tmp.c_str(), 0600) < 0) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not create pipe " << tmp << '\n'
|
|
|
|
<< strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
};
|
2000-09-26 13:54:57 +00:00
|
|
|
if (::access(tmp.c_str(), F_OK) != 0) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Pipe " << tmp
|
|
|
|
<< " does not exist" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-09-26 13:54:57 +00:00
|
|
|
outfd = ::open(tmp.c_str(), O_RDWR);
|
1999-09-27 18:44:28 +00:00
|
|
|
#else
|
|
|
|
if (rc != NO_ERROR) {
|
|
|
|
errnum = TranslateOS2Error(rc);
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not create pipe " << tmp << '\n'
|
|
|
|
<< strerror(errnum) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
rc = DosConnectNPipe(fd);
|
|
|
|
if (rc == ERROR_BAD_PIPE) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Pipe " << tmp
|
|
|
|
<< " does not exist" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rc != NO_ERROR && rc != ERROR_PIPE_NOT_CONNECTED) {
|
|
|
|
errnum = TranslateOS2Error(rc);
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not create pipe " << tmp << '\n'
|
|
|
|
<< strerror(errnum) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
outfd = _imphandle(fd);
|
|
|
|
#endif
|
|
|
|
if (outfd < 0) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not open pipe " << tmp << '\n'
|
|
|
|
<< strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fcntl(outfd, F_SETFL, O_NONBLOCK) < 0) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not set flags on pipe " << tmp
|
|
|
|
<< '\n' << strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
// We made it!
|
|
|
|
ready = true;
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::LYXSERVER] << "LyXComm: Connection established" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Close pipes
|
|
|
|
void LyXComm::closeConnection() {
|
|
|
|
#ifdef __EMX__
|
|
|
|
APIRET rc;
|
|
|
|
int errnum;
|
|
|
|
#endif
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::LYXSERVER] << "LyXComm: Closing connection" << 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: Already disconnected" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-11-04 10:00:12 +00:00
|
|
|
if (infd > -1) {
|
1999-10-25 14:18:30 +00:00
|
|
|
fl_remove_io_callback(infd, FL_READ, C_LyXComm_callback);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmp = pipename + ".in";
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef __EMX__ // Notify the operating system.
|
|
|
|
rc = DosDisConnectNPipe(infd);
|
|
|
|
if (rc != NO_ERROR) {
|
|
|
|
errnum = TranslateOS2Error(rc);
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not disconnect pipe " << tmp
|
|
|
|
<< '\n' << strerror(errnum) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (close(infd) < 0) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not close pipe " << tmp
|
|
|
|
<< '\n' << strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
#ifndef __EMX__ // OS/2 named pipes will be automatically removed.
|
2000-09-26 13:54:57 +00:00
|
|
|
if (lyx::unlink(tmp) < 0){
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not remove pipe " << tmp
|
|
|
|
<< '\n' << strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
}
|
2000-11-04 10:00:12 +00:00
|
|
|
if (outfd > -1) {
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmp = pipename + ".out";
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef __EMX__
|
|
|
|
rc = DosDisConnectNPipe(outfd);
|
|
|
|
if (rc != NO_ERROR) {
|
|
|
|
errnum = TranslateOS2Error(rc);
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not disconnect pipe " << tmp
|
|
|
|
<< '\n' << strerror(errnum) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2000-09-26 13:54:57 +00:00
|
|
|
if (::close(outfd) < 0) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not close pipe " << tmp
|
|
|
|
<< '\n' << strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
#ifndef __EMX__
|
2000-09-26 13:54:57 +00:00
|
|
|
if (lyx::unlink(tmp) < 0){
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: Could not remove pipe " << tmp
|
|
|
|
<< '\n' << strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
ready = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receives messages and sends then to client
|
|
|
|
void LyXComm::callback(int fd, void *v)
|
|
|
|
{
|
1999-11-22 16:19:48 +00:00
|
|
|
LyXComm * c = static_cast<LyXComm*>(v);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-07 18:44:17 +00:00
|
|
|
if (lyxerr.debugging(Debug::LYXSERVER)) {
|
|
|
|
lyxerr << "LyXComm: Receiving from fd " << fd << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const int CMDBUFLEN = 100;
|
|
|
|
char charbuf[CMDBUFLEN];
|
1999-10-02 16:21:10 +00:00
|
|
|
string cmd;
|
1999-09-27 18:44:28 +00:00
|
|
|
// nb! make lsbuf a class-member for multiple sessions
|
1999-10-02 16:21:10 +00:00
|
|
|
static string lsbuf;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
int status;
|
|
|
|
// the single = is intended here.
|
1999-11-15 12:01:38 +00:00
|
|
|
while((status = read(fd, charbuf, CMDBUFLEN-1)))
|
1999-09-27 18:44:28 +00:00
|
|
|
{// break and return in loop
|
2000-11-04 10:00:12 +00:00
|
|
|
if (status > 0) // got something
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-11-15 12:01:38 +00:00
|
|
|
charbuf[status]= '\0'; // turn it into a c string
|
1999-10-02 16:21:10 +00:00
|
|
|
lsbuf += strip(charbuf, '\r');
|
1999-09-27 18:44:28 +00:00
|
|
|
// commit any commands read
|
1999-10-02 16:21:10 +00:00
|
|
|
while(lsbuf.find('\n') != string::npos) // while still
|
1999-09-27 18:44:28 +00:00
|
|
|
// commands
|
|
|
|
// left
|
|
|
|
{
|
|
|
|
// split() grabs the entire string if
|
|
|
|
// the delim /wasn't/ found. ?:-P
|
1999-11-15 12:01:38 +00:00
|
|
|
lsbuf= split(lsbuf, cmd,'\n');
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::LYXSERVER]
|
|
|
|
<< "LyXComm: status:" << status
|
|
|
|
<< ", lsbuf:" << lsbuf
|
|
|
|
<< ", cmd:" << cmd << endl;
|
2000-11-04 10:00:12 +00:00
|
|
|
if (!cmd.empty())
|
1999-09-27 18:44:28 +00:00
|
|
|
c->clientcb(c->client, cmd);
|
|
|
|
//\n or not \n?
|
|
|
|
}
|
|
|
|
}
|
2000-11-04 10:00:12 +00:00
|
|
|
if (errno == EAGAIN)
|
1999-09-27 18:44:28 +00:00
|
|
|
{ // EAGAIN is not really an error , it means we're
|
|
|
|
// only reading too fast for the writing process on
|
|
|
|
// the other end of the pipe.
|
|
|
|
errno = 0;
|
|
|
|
return; // up to libforms select-loop (*crunch*)
|
|
|
|
}
|
2000-11-04 10:00:12 +00:00
|
|
|
if (errno != 0 )
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: " << strerror(errno) << endl;
|
2000-11-04 10:00:12 +00:00
|
|
|
if (!lsbuf.empty())
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyxComm: truncated command: "
|
|
|
|
<< lsbuf << endl;
|
2000-05-04 10:57:00 +00:00
|
|
|
lsbuf.erase();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
break; // reset connection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->closeConnection();
|
|
|
|
c->openConnection();
|
1999-11-15 12:01:38 +00:00
|
|
|
errno= 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
1999-10-25 14:18:30 +00:00
|
|
|
|
|
|
|
extern "C" void C_LyXComm_callback(int fd, void *v)
|
|
|
|
{
|
|
|
|
LyXComm::callback(fd, v);
|
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-02 16:21:10 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
char const * p = msg.c_str();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// --- parse the string --------------------------------------------
|
|
|
|
//
|
|
|
|
// Format: LYXCMD:<client>:<func>:<argstring>\n
|
|
|
|
//
|
|
|
|
bool server_only = false;
|
|
|
|
while(*p) {
|
|
|
|
// --- 1. check 'header' ---
|
2000-04-11 16:57:16 +00:00
|
|
|
|
2001-02-16 09:25:43 +00:00
|
|
|
if (compare(p, "LYXSRV:", 7) == 0) {
|
1999-09-27 18:44:28 +00:00
|
|
|
server_only = true;
|
2001-02-16 09:25:43 +00:00
|
|
|
} else if (0 != compare(p, "LYXCMD:", 7)) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXServer: Unknown request" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
p += 7;
|
|
|
|
|
|
|
|
// --- 2. for the moment ignore the client name ---
|
1999-10-02 16:21:10 +00:00
|
|
|
string client;
|
1999-09-27 18:44:28 +00:00
|
|
|
while(*p && *p != ':')
|
|
|
|
client += char(*p++);
|
2000-11-04 10:00:12 +00:00
|
|
|
if (*p == ':') ++p;
|
|
|
|
if (!*p) return;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// --- 3. get function name ---
|
1999-10-02 16:21:10 +00:00
|
|
|
string cmd;
|
1999-09-27 18:44:28 +00:00
|
|
|
while(*p && *p != ':')
|
|
|
|
cmd += char(*p++);
|
|
|
|
|
|
|
|
// --- 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)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
while(*p && *p != '\n')
|
|
|
|
arg += char(*p++);
|
2000-11-04 10:00:12 +00:00
|
|
|
if (*p) ++p;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::LYXSERVER]
|
|
|
|
<< "LyXServer: Client: '" << client
|
|
|
|
<< "' Command: '" << cmd
|
|
|
|
<< "' Argument: '" << arg << '\'' << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// --- lookup and exec the command ------------------
|
|
|
|
|
|
|
|
if (server_only) {
|
1999-10-02 16:21:10 +00:00
|
|
|
string buf;
|
1999-09-27 18:44:28 +00:00
|
|
|
// return the greeting to inform the client that
|
|
|
|
// we are listening.
|
|
|
|
if (cmd == "hello") {
|
|
|
|
// One more client
|
2000-11-04 10:00:12 +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[]
|
1999-09-27 18:44:28 +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 "
|
|
|
|
<< cmd << "." << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
int action = lyxaction.LookupFunc(cmd);
|
1999-09-27 18:44:28 +00:00
|
|
|
//int action = -1;
|
1999-10-02 16:21:10 +00:00
|
|
|
string rval, buf;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-11-15 12:01:38 +00:00
|
|
|
if (action>= 0) {
|
2000-09-19 11:18:35 +00:00
|
|
|
rval = serv->func->Dispatch(action, arg);
|
1999-09-27 18:44:28 +00:00
|
|
|
} else {
|
|
|
|
rval = "Unknown command";
|
|
|
|
}
|
|
|
|
|
|
|
|
//modified june 1999 stefano@zool.su.se:
|
|
|
|
//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.
|
|
|
|
if (action<0 || serv->func->errorStat())
|
|
|
|
buf = "ERROR:";
|
|
|
|
else
|
|
|
|
buf = "INFO:";
|
1999-10-02 16:21:10 +00:00
|
|
|
buf += string(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.
|
|
|
|
|
|
|
|
// not found
|
|
|
|
}
|
|
|
|
} /* while *p */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : LyxNotifyClient
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|