1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
12 Docu : To use the lyxserver define the name of the pipe in your
14 \serverpipe "/home/myhome/.lyxpipe"
15 Then use .lyxpipe.in and .lyxpipe.out to communicate to LyX.
16 Each message consists of a single line in ASCII. Input lines
17 (client -> LyX) have the following format:
18 "LYXCMD:<clientname>:<functionname>:<argument>"
19 Answers from LyX look like this:
20 "INFO:<clientname>:<functionname>:<data>"
21 [asierra970531] Or like this in case of error:
22 "ERROR:<clientname>:<functionname>:<error message>"
23 where <clientname> and <functionname> are just echoed.
24 If LyX notifies about a user defined extension key-sequence,
25 the line looks like this:
26 "NOTIFY:<key-sequence>"
27 [asierra970531] New server-only messages to implement a simple protocol
28 "LYXSRV:<clientname>:<protocol message>"
29 where <protocol message> can be "hello" or "bye". If hello is
30 received LyX will inform the client that it's listening its
31 messages, and 'bye' will inform that lyx is closing.
33 See development/server_monitor.c for an example client.
34 Purpose: implement a client/server lib for LyX
39 #include <sys/types.h>
46 #pragma implementation
49 #include "lyxserver.h"
53 #include "support/lstrings.h"
54 #include "support/lyxlib.h"
55 #include "frontends/lyx_gui.h"
60 #define OS2EMX_PLAIN_CHAR
61 #define INCL_DOSNMPIPES
62 #define INCL_DOSERRORS
64 #include "support/os2_errortable.h"
69 // provide an empty mkfifo() if we do not have one. This disables the
72 int mkfifo(char const * __path, mode_t __mode) {
78 void LyXComm::openConnection()
80 lyxerr[Debug::LYXSERVER] << "LyXComm: Opening connection" << endl;
82 // If we are up, that's an error
84 lyxerr << "LyXComm: Already connected" << endl;
87 // We assume that we don't make it
90 if (pipename.empty()) {
91 lyxerr[Debug::LYXSERVER]
92 << "LyXComm: server is disabled, nothing to do"
97 if ((infd = startPipe(inPipeName(), false)) == -1)
100 if ((outfd = startPipe(outPipeName(), true)) == -1) {
101 endPipe(infd, inPipeName(), false);
105 if (fcntl(outfd, F_SETFL, O_NONBLOCK) < 0) {
106 lyxerr << "LyXComm: Could not set flags on pipe " << outPipeName()
107 << '\n' << strerror(errno) << endl;
113 lyxerr[Debug::LYXSERVER] << "LyXComm: Connection established" << endl;
118 void LyXComm::closeConnection()
120 lyxerr[Debug::LYXSERVER] << "LyXComm: Closing connection" << endl;
122 if (pipename.empty()) {
123 lyxerr[Debug::LYXSERVER]
124 << "LyXComm: server is disabled, nothing to do"
130 lyxerr << "LyXComm: Already disconnected" << endl;
134 endPipe(infd, inPipeName(), false);
135 endPipe(outfd, outPipeName(), true);
140 int LyXComm::startPipe(string const & filename, bool write)
148 // Try create one instance of named pipe with the mode O_RDONLY|O_NONBLOCK.
149 // The current emx implementation of access() won't work with pipes.
150 rc = DosCreateNPipe(filename.c_str(), &os2fd, NP_ACCESS_INBOUND,
151 NP_NOWAIT|0x01, 0600, 0600, 0);
152 if (rc == ERROR_PIPE_BUSY) {
153 lyxerr << "LyXComm: Pipe " << filename << " already exists.\n"
154 << "If no other LyX program is active, please delete"
155 " the pipe by hand and try again." << endl;
160 if (rc != NO_ERROR) {
161 errnum = TranslateOS2Error(rc);
162 lyxerr <<"LyXComm: Could not create pipe " << filename
163 << strerror(errnum) << endl;
167 rc = DosConnectNPipe(os2fd);
168 if (rc != NO_ERROR && rc != ERROR_PIPE_NOT_CONNECTED) {
169 errnum = TranslateOS2Error(rc);
170 lyxerr <<"LyXComm: Could not create pipe " << filename
171 << strerror(errnum) << endl;
174 // Imported handles can be used both with OS/2 APIs and emx
175 // library functions.
176 fd = _imphandle(os2fd);
178 if (::access(filename.c_str(), F_OK) == 0) {
179 lyxerr << "LyXComm: Pipe " << filename << " already exists.\n"
180 << "If no other LyX program is active, please delete"
181 " the pipe by hand and try again." << endl;
186 if (::mkfifo(filename.c_str(), 0600) < 0) {
187 lyxerr << "LyXComm: Could not create pipe " << filename << '\n'
188 << strerror(errno) << endl;
191 fd = ::open(filename.c_str(), write ? (O_RDWR) : (O_RDONLY|O_NONBLOCK));
195 lyxerr << "LyXComm: Could not open pipe " << filename << '\n'
196 << strerror(errno) << endl;
197 lyx::unlink(filename);
202 lyx_gui::set_read_callback(fd, this);
209 void LyXComm::endPipe(int & fd, string const & filename, bool write)
215 lyx_gui::remove_read_callback(fd);
222 rc = DosDisConnectNPipe(fd);
223 if (rc != NO_ERROR) {
224 errnum = TranslateOS2Error(rc);
225 lyxerr << "LyXComm: Could not disconnect pipe " << filename
226 << '\n' << strerror(errnum) << endl;
231 if (::close(fd) < 0) {
232 lyxerr << "LyXComm: Could not close pipe " << filename
233 << '\n' << strerror(errno) << endl;
236 // OS/2 pipes are deleted automatically
238 if (lyx::unlink(filename) < 0) {
239 lyxerr << "LyXComm: Could not remove pipe " << filename
240 << '\n' << strerror(errno) << endl;
248 void LyXComm::emergencyCleanup()
250 if (!pipename.empty()) {
251 endPipe(infd, inPipeName(), false);
252 endPipe(outfd, outPipeName(), true);
257 // Receives messages and sends then to client
258 void LyXComm::read_ready()
260 // nb! make read_buffer_ a class-member for multiple sessions
261 static string read_buffer_;
262 read_buffer_.erase();
264 int const charbuf_size = 100;
265 char charbuf[charbuf_size];
269 // the single = is intended here.
270 while ((status = ::read(infd, charbuf, charbuf_size - 1))) {
273 charbuf[status] = '\0'; // turn it into a c string
274 read_buffer_ += rtrim(charbuf, "\r");
276 } else if (errno != EAGAIN) {
277 if (!read_buffer_.empty()) {
278 lyxerr << "LyXComm: truncated command: "
279 << read_buffer_ << '\n'
280 << "Resetting connection" << endl;
281 read_buffer_.erase();
291 // Nothing new has arrived, so now's the time
292 // to tell the outside world if there's anything
293 // in the read buffer.
298 if (!read_buffer_.empty()) {
299 read_buffer_ = rtrim(read_buffer_, "\n");
300 lyxerr[Debug::LYXSERVER]
301 << "LyXComm: Received from fd "
303 << '\"' << read_buffer_ << '\"' << endl;
304 clientcb(client, read_buffer_);
311 void LyXComm::send(string const & msg)
314 lyxerr << "LyXComm: Request to send empty string. Ignoring."
319 if (lyxerr.debugging(Debug::LYXSERVER)) {
320 lyxerr << "LyXComm: Sending '" << msg << '\'' << endl;
323 if (pipename.empty()) return;
326 lyxerr << "LyXComm: Pipes are closed. Could not send "
328 } else if (::write(outfd, msg.c_str(), msg.length()) < 0) {
329 lyxerr << "LyXComm: Error sending message: " << msg
330 << '\n' << strerror(errno)
331 << "\nLyXComm: Resetting connection" << endl;
338 rc = DosResetBuffer(outfd); // To avoid synchronization problems.
339 if (rc != NO_ERROR) {
340 errnum = TranslateOS2Error(rc);
341 lyxerr << "LyXComm: Message could not be flushed: " << msg
342 << '\n' << strerror(errnum) << endl;
350 LyXServer::~LyXServer()
352 // say goodbye to clients so they stop sending messages
353 // modified june 1999 by stefano@zool.su.se to send as many bye
354 // messages as there are clients, each with client's name.
356 for (int i= 0; i<numclients; ++i) {
357 message = "LYXSRV:" + clients[i] + ":bye\n";
363 /* ---F+------------------------------------------------------------------ *\
364 Function : ServerCallback
366 Purpose : handle data gotten from communication
367 \* ---F------------------------------------------------------------------- */
369 void LyXServer::callback(LyXServer * serv, string const & msg)
371 lyxerr[Debug::LYXSERVER] << "LyXServer: Received: '"
372 << msg << '\'' << endl;
374 char const * p = msg.c_str();
376 // --- parse the string --------------------------------------------
378 // Format: LYXCMD:<client>:<func>:<argstring>\n
380 bool server_only = false;
382 // --- 1. check 'header' ---
384 if (compare(p, "LYXSRV:", 7) == 0) {
386 } else if (0 != compare(p, "LYXCMD:", 7)) {
387 lyxerr << "LyXServer: Unknown request \"" << p << "\"" << endl;
392 // --- 2. for the moment ignore the client name ---
394 while (*p && *p != ':')
395 client += char(*p++);
399 // --- 3. get function name ---
401 while (*p && *p != ':')
404 // --- 4. parse the argument ---
406 if (!server_only && *p == ':' && *(++p)) {
407 while (*p && *p != '\n')
412 lyxerr[Debug::LYXSERVER]
413 << "LyXServer: Client: '" << client
414 << "' Command: '" << cmd
415 << "' Argument: '" << arg << '\'' << endl;
417 // --- lookup and exec the command ------------------
421 // return the greeting to inform the client that
423 if (cmd == "hello") {
425 if (serv->numclients == MAX_CLIENTS) { //paranoid check
426 lyxerr[Debug::LYXSERVER]
427 << "LyXServer: too many clients..."
431 int i= 0; //find place in clients[]
432 while (!serv->clients[i].empty()
433 && i<serv->numclients)
435 serv->clients[i] = client;
437 buf = "LYXSRV:" + client + ":hello\n";
438 lyxerr[Debug::LYXSERVER]
439 << "LyXServer: Greeting "
441 serv->pipes.send(buf);
442 } else if (cmd == "bye") {
443 // If clients == 0 maybe we should reset the pipes
444 // to prevent fake callbacks
445 int i = 0; //look if client is registered
446 for (; i < serv->numclients; ++i) {
447 if (serv->clients[i] == client) break;
449 if (i < serv->numclients) {
451 serv->clients[i].erase();
452 lyxerr[Debug::LYXSERVER]
453 << "LyXServer: Client "
454 << client << " said goodbye"
457 lyxerr[Debug::LYXSERVER]
458 << "LyXServer: ignoring bye messge from unregistered client"
462 lyxerr <<"LyXServer: Undefined server command "
463 << cmd << "." << endl;
469 // which lyxfunc should we let it connect to?
470 // The correct solution would be to have a
471 // specialized (non-gui) BufferView. But how do
472 // we do it now? Probably we should just let it
473 // connect to the lyxfunc in the single LyXView we
474 // support currently. (Lgb)
477 serv->func->dispatch(cmd + ' ' + arg);
478 string const rval = serv->func->getMessage();
480 //modified june 1999 stefano@zool.su.se:
481 //all commands produce an INFO or ERROR message
482 //in the output pipe, even if they do not return
483 //anything. See chapter 4 of Customization doc.
485 if (serv->func->errorStat())
489 buf += client + ":" + cmd + ":" + rval + "\n";
490 serv->pipes.send(buf);
492 // !!! we don't do any error checking -
493 // if the client won't listen, the
494 // message is lost and others too
495 // maybe; so the client should empty
496 // the outpipe before issuing a request.
504 /* ---F+------------------------------------------------------------------ *\
505 Function : LyXNotifyClient
506 Called by : WorkAreaKeyPress
507 Purpose : send a notify messge to a client
508 Parameters: s - string to send
510 \* ---F------------------------------------------------------------------- */
512 void LyXServer::notifyClient(string const & s)
514 string buf = string("NOTIFY:") + s + "\n";