1999-09-27 18:44:28 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/* This file is part of
|
|
|
|
* ======================================================
|
1999-10-02 16:21:10 +00:00
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
*
|
|
|
|
* Copyright 1995 Matthias Ettrich
|
1999-10-07 18:44:17 +00:00
|
|
|
* Copyright 1995-1999 The LyX Team.
|
1999-10-02 16:21:10 +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>
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
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 "lyxfunc.h"
|
|
|
|
#include "lyx_main.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
#include "debug.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
#include "support/lstrings.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
|
|
|
|
|
|
|
|
// provide an empty mkfifo() if we do not have one. This disables the
|
|
|
|
// lyxserver.
|
|
|
|
#ifndef HAVE_MKFIFO
|
|
|
|
int mkfifo( char *__path, mode_t __mode ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
if (access(tmp.c_str(), F_OK) == 0) {
|
|
|
|
#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;
|
1999-10-02 16:21:10 +00:00
|
|
|
pipename = string();
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifndef __EMX__
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
infd = open(tmp.c_str(), O_RDONLY|O_NONBLOCK);
|
|
|
|
#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-10-07 18:44:17 +00:00
|
|
|
lyxerr <<"LyXComm: Could not create pipe " + tmp);
|
|
|
|
lyxerr <<strerror(errnum);
|
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-10-25 14:18:30 +00:00
|
|
|
fl_add_io_callback(infd, FL_READ, C_LyXComm_callback, (void*)this);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// --- prepare output pipe ---------------------------------------
|
|
|
|
|
|
|
|
tmp = pipename + ".out";
|
|
|
|
|
|
|
|
#ifndef __EMX__
|
|
|
|
if (access(tmp.c_str(), F_OK) == 0) {
|
|
|
|
#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;
|
1999-10-02 16:21:10 +00:00
|
|
|
pipename = string();
|
1999-09-27 18:44:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifndef __EMX__
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
outfd = open(tmp.c_str(), O_RDWR);
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (unlink(tmp.c_str()) < 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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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__
|
|
|
|
if (unlink(tmp.c_str()) < 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)
|
|
|
|
{
|
|
|
|
LyXComm * c = (LyXComm *) v;
|
|
|
|
|
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.
|
|
|
|
while((status = read(fd,charbuf,CMDBUFLEN-1)))
|
|
|
|
{// break and return in loop
|
|
|
|
if(status > 0) // got something
|
|
|
|
{
|
|
|
|
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-10-02 16:21:10 +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;
|
1999-09-27 18:44:28 +00:00
|
|
|
if(!cmd.empty())
|
|
|
|
c->clientcb(c->client, cmd);
|
|
|
|
//\n or not \n?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(errno == EAGAIN)
|
|
|
|
{ // 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*)
|
|
|
|
}
|
|
|
|
if(errno != 0 )
|
|
|
|
{
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyXComm: " << strerror(errno) << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
if(!lsbuf.empty())
|
|
|
|
{
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyxComm: truncated command: "
|
|
|
|
<< lsbuf << endl;
|
1999-10-19 20:59:27 +00:00
|
|
|
lsbuf.clear();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
break; // reset connection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->closeConnection();
|
|
|
|
c->openConnection();
|
|
|
|
errno=0;
|
|
|
|
}
|
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;
|
1999-09-27 18:44:28 +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-10-07 18:44:17 +00:00
|
|
|
lyxerr <<"LyXComm: Message could not be flushed: " +msg);
|
|
|
|
lyxerr <<strerror(errnum));
|
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;
|
1999-09-27 18:44:28 +00:00
|
|
|
for (int i=0; i<numclients; i++) {
|
|
|
|
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
|
|
|
|
|
|
|
char const *p = msg.c_str();
|
|
|
|
|
|
|
|
// --- parse the string --------------------------------------------
|
|
|
|
//
|
|
|
|
// Format: LYXCMD:<client>:<func>:<argstring>\n
|
|
|
|
//
|
|
|
|
bool server_only = false;
|
|
|
|
while(*p) {
|
|
|
|
// --- 1. check 'header' ---
|
|
|
|
if (strncmp(p, "LYXSRV:", 7)==0) {
|
|
|
|
server_only = true;
|
|
|
|
} else if(0!=strncmp(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++);
|
|
|
|
if(*p == ':') p++;
|
|
|
|
if(!*p) return;
|
|
|
|
|
|
|
|
// --- 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;
|
1999-09-27 18:44:28 +00:00
|
|
|
if(!server_only && *p == ':' && *(++p)) {
|
|
|
|
while(*p && *p != '\n')
|
|
|
|
arg += char(*p++);
|
|
|
|
if(*p) p++;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
int i=0; //find place in clients[]
|
|
|
|
while (!serv->clients[i].empty()
|
|
|
|
&& i<serv->numclients)
|
|
|
|
i++;
|
|
|
|
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") {
|
|
|
|
// If clients==0 maybe we should reset the pipes
|
|
|
|
// to prevent fake callbacks
|
|
|
|
int i; //look if client is registered
|
|
|
|
for (i=0; i<serv->numclients; i++) {
|
|
|
|
if (serv->clients[i] == client) break;
|
|
|
|
}
|
|
|
|
if (i<serv->numclients) {
|
|
|
|
serv->numclients--;
|
1999-10-19 20:59:27 +00:00
|
|
|
serv->clients[i].clear();
|
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)
|
|
|
|
|
|
|
|
int action = lyxaction.LookupFunc(cmd.c_str());
|
|
|
|
//int action = -1;
|
1999-10-02 16:21:10 +00:00
|
|
|
string rval, buf;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
if (action>=0) {
|
|
|
|
rval = serv->func->Dispatch(action, arg.c_str());
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
|