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>
44 #include FORMS_H_LOCATION
47 #pragma implementation
50 #include "lyxserver.h"
54 #include "support/lstrings.h"
55 #include "support/lyxlib.h"
56 #include "frontends/lyx_gui.h"
61 #define OS2EMX_PLAIN_CHAR
62 #define INCL_DOSNMPIPES
63 #define INCL_DOSERRORS
65 #include "support/os2_errortable.h"
70 // provide an empty mkfifo() if we do not have one. This disables the
73 int mkfifo(char const * __path, mode_t __mode) {
79 void LyXComm::openConnection()
81 lyxerr[Debug::LYXSERVER] << "LyXComm: Opening connection" << endl;
83 // If we are up, that's an error
85 lyxerr << "LyXComm: Already connected" << endl;
88 // We assume that we don't make it
91 if (pipename.empty()) {
92 lyxerr[Debug::LYXSERVER]
93 << "LyXComm: server is disabled, nothing to do"
98 if ((infd = startPipe(inPipeName(), false)) == -1)
101 if ((outfd = startPipe(outPipeName(), true)) == -1) {
102 endPipe(infd, inPipeName());
106 if (fcntl(outfd, F_SETFL, O_NONBLOCK) < 0) {
107 lyxerr << "LyXComm: Could not set flags on pipe " << outPipeName()
108 << '\n' << strerror(errno) << endl;
114 lyxerr[Debug::LYXSERVER] << "LyXComm: Connection established" << endl;
119 void LyXComm::closeConnection()
121 lyxerr[Debug::LYXSERVER] << "LyXComm: Closing connection" << endl;
123 if (pipename.empty()) {
124 lyxerr[Debug::LYXSERVER]
125 << "LyXComm: server is disabled, nothing to do"
131 lyxerr << "LyXComm: Already disconnected" << endl;
135 endPipe(infd, inPipeName());
136 endPipe(outfd, outPipeName());
141 int LyXComm::startPipe(string const & filename, bool write)
149 // Try create one instance of named pipe with the mode O_RDONLY|O_NONBLOCK.
150 // The current emx implementation of access() won't work with pipes.
151 rc = DosCreateNPipe(filename.c_str(), &os2fd, NP_ACCESS_INBOUND,
152 NP_NOWAIT|0x01, 0600, 0600, 0);
153 if (rc == ERROR_PIPE_BUSY) {
154 lyxerr << "LyXComm: Pipe " << filename << " already exists.\n"
155 << "If no other LyX program is active, please delete"
156 " the pipe by hand and try again." << endl;
161 if (rc != NO_ERROR) {
162 errnum = TranslateOS2Error(rc);
163 lyxerr <<"LyXComm: Could not create pipe " << filename
164 << strerror(errnum) << endl;
168 rc = DosConnectNPipe(os2fd);
169 if (rc != NO_ERROR && rc != ERROR_PIPE_NOT_CONNECTED) {
170 errnum = TranslateOS2Error(rc);
171 lyxerr <<"LyXComm: Could not create pipe " << filename
172 << strerror(errnum) << endl;
175 // Imported handles can be used both with OS/2 APIs and emx
176 // library functions.
177 fd = _imphandle(os2fd);
179 if (::access(filename.c_str(), F_OK) == 0) {
180 lyxerr << "LyXComm: Pipe " << filename << " already exists.\n"
181 << "If no other LyX program is active, please delete"
182 " the pipe by hand and try again." << endl;
187 if (::mkfifo(filename.c_str(), 0600) < 0) {
188 lyxerr << "LyXComm: Could not create pipe " << filename << '\n'
189 << strerror(errno) << endl;
192 fd = ::open(filename.c_str(), write ? (O_RDWR) : (O_RDONLY|O_NONBLOCK));
196 lyxerr << "LyXComm: Could not open pipe " << filename << '\n'
197 << strerror(errno) << endl;
198 lyx::unlink(filename);
203 lyx_gui::set_read_callback(fd, this);
210 void LyXComm::endPipe(int & fd, string const & filename)
219 rc = DosDisConnectNPipe(fd);
220 if (rc != NO_ERROR) {
221 errnum = TranslateOS2Error(rc);
222 lyxerr << "LyXComm: Could not disconnect pipe " << filename
223 << '\n' << strerror(errnum) << endl;
228 if (::close(fd) < 0) {
229 lyxerr << "LyXComm: Could not close pipe " << filename
230 << '\n' << strerror(errno) << endl;
233 // OS/2 pipes are deleted automatically
235 if (lyx::unlink(filename) < 0) {
236 lyxerr << "LyXComm: Could not remove pipe " << filename
237 << '\n' << strerror(errno) << endl;
245 void LyXComm::emergencyCleanup()
247 if (!pipename.empty()) {
248 endPipe(infd, inPipeName());
249 endPipe(outfd, outPipeName());
254 // Receives messages and sends then to client
255 void LyXComm::read_ready()
257 if (lyxerr.debugging(Debug::LYXSERVER)) {
258 lyxerr << "LyXComm: Receiving from fd " << infd << endl;
261 int const CMDBUFLEN = 100;
262 char charbuf[CMDBUFLEN];
264 // nb! make lsbuf a class-member for multiple sessions
269 // the single = is intended here.
270 while ((status = read(infd, charbuf, CMDBUFLEN-1))) {
272 charbuf[status]= '\0'; // turn it into a c string
273 lsbuf += strip(charbuf, '\r');
274 // commit any commands read
275 while (lsbuf.find('\n') != string::npos) {
276 // split() grabs the entire string if
277 // the delim /wasn't/ found. ?:-P
278 lsbuf= split(lsbuf, cmd,'\n');
279 lyxerr[Debug::LYXSERVER]
280 << "LyXComm: status:" << status
281 << ", lsbuf:" << lsbuf
282 << ", cmd:" << cmd << endl;
284 clientcb(client, cmd);
288 if (errno == EAGAIN) {
293 lyxerr << "LyXComm: " << strerror(errno) << endl;
296 lyxerr << "LyxComm: truncated command: "
300 break; // reset connection
309 void LyXComm::send(string const & msg)
312 lyxerr << "LyXComm: Request to send empty string. Ignoring."
317 if (lyxerr.debugging(Debug::LYXSERVER)) {
318 lyxerr << "LyXComm: Sending '" << msg << '\'' << endl;
321 if (pipename.empty()) return;
324 lyxerr << "LyXComm: Pipes are closed. Could not send "
326 } else if (::write(outfd, msg.c_str(), msg.length()) < 0) {
327 lyxerr << "LyXComm: Error sending message: " << msg
328 << '\n' << strerror(errno)
329 << "\nLyXComm: Resetting connection" << endl;
336 rc = DosResetBuffer(outfd); // To avoid synchronization problems.
337 if (rc != NO_ERROR) {
338 errnum = TranslateOS2Error(rc);
339 lyxerr << "LyXComm: Message could not be flushed: " << msg
340 << '\n' << strerror(errnum) << endl;
348 LyXServer::~LyXServer()
350 // say goodbye to clients so they stop sending messages
351 // modified june 1999 by stefano@zool.su.se to send as many bye
352 // messages as there are clients, each with client's name.
354 for (int i= 0; i<numclients; ++i) {
355 message = "LYXSRV:" + clients[i] + ":bye\n";
361 /* ---F+------------------------------------------------------------------ *\
362 Function : ServerCallback
364 Purpose : handle data gotten from communication
365 \* ---F------------------------------------------------------------------- */
367 void LyXServer::callback(LyXServer * serv, string const & msg)
369 lyxerr[Debug::LYXSERVER] << "LyXServer: Received: '"
370 << msg << '\'' << endl;
372 char const * p = msg.c_str();
374 // --- parse the string --------------------------------------------
376 // Format: LYXCMD:<client>:<func>:<argstring>\n
378 bool server_only = false;
380 // --- 1. check 'header' ---
382 if (compare(p, "LYXSRV:", 7) == 0) {
384 } else if (0 != compare(p, "LYXCMD:", 7)) {
385 lyxerr << "LyXServer: Unknown request \"" << p << "\"" << endl;
390 // --- 2. for the moment ignore the client name ---
392 while (*p && *p != ':')
393 client += char(*p++);
397 // --- 3. get function name ---
399 while (*p && *p != ':')
402 // --- 4. parse the argument ---
404 if (!server_only && *p == ':' && *(++p)) {
405 while (*p && *p != '\n')
410 lyxerr[Debug::LYXSERVER]
411 << "LyXServer: Client: '" << client
412 << "' Command: '" << cmd
413 << "' Argument: '" << arg << '\'' << endl;
415 // --- lookup and exec the command ------------------
419 // return the greeting to inform the client that
421 if (cmd == "hello") {
423 if (serv->numclients == MAX_CLIENTS) { //paranoid check
424 lyxerr[Debug::LYXSERVER]
425 << "LyXServer: too many clients..."
429 int i= 0; //find place in clients[]
430 while (!serv->clients[i].empty()
431 && i<serv->numclients)
433 serv->clients[i] = client;
435 buf = "LYXSRV:" + client + ":hello\n";
436 lyxerr[Debug::LYXSERVER]
437 << "LyXServer: Greeting "
439 serv->pipes.send(buf);
440 } else if (cmd == "bye") {
441 // If clients == 0 maybe we should reset the pipes
442 // to prevent fake callbacks
443 int i = 0; //look if client is registered
444 for (; i < serv->numclients; ++i) {
445 if (serv->clients[i] == client) break;
447 if (i < serv->numclients) {
449 serv->clients[i].erase();
450 lyxerr[Debug::LYXSERVER]
451 << "LyXServer: Client "
452 << client << " said goodbye"
455 lyxerr[Debug::LYXSERVER]
456 << "LyXServer: ignoring bye messge from unregistered client"
460 lyxerr <<"LyXServer: Undefined server command "
461 << cmd << "." << endl;
467 // which lyxfunc should we let it connect to?
468 // The correct solution would be to have a
469 // specialized (non-gui) BufferView. But how do
470 // we do it now? Probably we should just let it
471 // connect to the lyxfunc in the single LyXView we
472 // support currently. (Lgb)
475 serv->func->verboseDispatch(cmd + ' ' + arg, false);
476 string const rval = serv->func->getMessage();
478 //modified june 1999 stefano@zool.su.se:
479 //all commands produce an INFO or ERROR message
480 //in the output pipe, even if they do not return
481 //anything. See chapter 4 of Customization doc.
483 if (serv->func->errorStat())
487 buf += client + ":" + cmd + ":" + rval + "\n";
488 serv->pipes.send(buf);
490 // !!! we don't do any error checking -
491 // if the client won't listen, the
492 // message is lost and others too
493 // maybe; so the client should empty
494 // the outpipe before issuing a request.
502 /* ---F+------------------------------------------------------------------ *\
503 Function : LyxNotifyClient
504 Called by : WorkAreaKeyPress
505 Purpose : send a notify messge to a client
506 Parameters: s - string to send
508 \* ---F------------------------------------------------------------------- */
510 void LyXServer::notifyClient(string const & s)
512 string buf = string("NOTIFY:") + s + "\n";