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"
53 #include "LyXAction.h"
55 #include "support/lstrings.h"
56 #include "support/lyxlib.h"
61 #define OS2EMX_PLAIN_CHAR
62 #define INCL_DOSNMPIPES
63 #define INCL_DOSERRORS
65 #include "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 /* === variables ========================================================= */
81 extern LyXAction lyxaction;
88 void C_LyXComm_callback(int fd, void *v)
90 LyXComm::callback(fd, v);
99 void LyXComm::openConnection()
101 lyxerr[Debug::LYXSERVER] << "LyXComm: Opening connection" << endl;
103 // If we are up, that's an error
105 lyxerr << "LyXComm: Already connected" << endl;
108 // We assume that we don't make it
111 if (pipename.empty()) return;
113 if ((infd = startPipe(pipename + ".in")) == -1)
116 if ((outfd = startPipe(pipename + ".out")) == -1) {
117 endPipe(infd, pipename + ".in");
121 if (fcntl(outfd, F_SETFL, O_NONBLOCK) < 0) {
122 lyxerr << "LyXComm: Could not set flags on pipe " << pipename << ".out"
123 << '\n' << strerror(errno) << endl;
129 lyxerr[Debug::LYXSERVER] << "LyXComm: Connection established" << endl;
134 void LyXComm::closeConnection()
136 lyxerr[Debug::LYXSERVER] << "LyXComm: Closing connection" << endl;
138 if (pipename.empty()) {
143 lyxerr << "LyXComm: Already disconnected" << endl;
147 endPipe(infd, pipename + ".in");
148 endPipe(outfd, pipename + ".out");
153 int LyXComm::startPipe(string const & filename)
161 // Try create one instance of named pipe with the mode O_RDONLY|O_NONBLOCK.
162 // The current emx implementation of access() won't work with pipes.
163 rc = DosCreateNPipe(filename.c_str(), &os2fd, NP_ACCESS_INBOUND,
164 NP_NOWAIT|0x01, 0600, 0600, 0);
165 if (rc == ERROR_PIPE_BUSY) {
166 lyxerr << "LyXComm: Pipe " << filename << " already exists.\n"
167 << "If no other LyX program is active, please delete"
168 " the pipe by hand and try again." << endl;
173 if (rc != NO_ERROR) {
174 errnum = TranslateOS2Error(rc);
175 lyxerr <<"LyXComm: Could not create pipe " << filename
176 << strerror(errnum) << endl;
180 rc = DosConnectNPipe(os2fd);
181 if (rc != NO_ERROR && rc != ERROR_PIPE_NOT_CONNECTED) {
182 errnum = TranslateOS2Error(rc);
183 lyxerr <<"LyXComm: Could not create pipe " << filename
184 << strerror(errnum) << endl;
187 // Imported handles can be used both with OS/2 APIs and emx
188 // library functions.
189 fd = _imphandle(os2fd);
191 if (::access(filename.c_str(), F_OK) == 0) {
192 lyxerr << "LyXComm: Pipe " << filename << " already exists.\n"
193 << "If no other LyX program is active, please delete"
194 " the pipe by hand and try again." << endl;
199 if (::mkfifo(filename.c_str(), 0600) < 0) {
200 lyxerr << "LyXComm: Could not create pipe " << filename << '\n'
201 << strerror(errno) << endl;
204 fd = ::open(filename.c_str(), O_RDONLY|O_NONBLOCK);
208 lyxerr << "LyXComm: Could not open pipe " << filename << '\n'
209 << strerror(errno) << endl;
210 lyx::unlink(filename);
213 fl_add_io_callback(fd, FL_READ, C_LyXComm_callback, this);
218 void LyXComm::endPipe(int & fd, string const & filename)
227 rc = DosDisConnectNPipe(fd);
228 if (rc != NO_ERROR) {
229 errnum = TranslateOS2Error(rc);
230 lyxerr << "LyXComm: Could not disconnect pipe " << filename
231 << '\n' << strerror(errnum) << endl;
236 if (::close(fd) < 0) {
237 lyxerr << "LyXComm: Could not close pipe " << filename
238 << '\n' << strerror(errno) << endl;
241 // OS/2 pipes are deleted automatically
243 if (lyx::unlink(filename) < 0){
244 lyxerr << "LyXComm: Could not remove pipe " << filename
245 << '\n' << strerror(errno) << endl;
253 void LyXComm::emergencyCleanup()
255 endPipe(infd, pipename + ".in");
256 endPipe(outfd, pipename + ".out");
260 // Receives messages and sends then to client
261 void LyXComm::callback(int fd, void *v)
263 LyXComm * c = static_cast<LyXComm*>(v);
265 if (lyxerr.debugging(Debug::LYXSERVER)) {
266 lyxerr << "LyXComm: Receiving from fd " << fd << endl;
269 const int CMDBUFLEN = 100;
270 char charbuf[CMDBUFLEN];
272 // nb! make lsbuf a class-member for multiple sessions
277 // the single = is intended here.
278 while((status = read(fd, charbuf, CMDBUFLEN-1)))
279 {// break and return in loop
280 if (status > 0) // got something
282 charbuf[status]= '\0'; // turn it into a c string
283 lsbuf += strip(charbuf, '\r');
284 // commit any commands read
285 while(lsbuf.find('\n') != string::npos) // while still
289 // split() grabs the entire string if
290 // the delim /wasn't/ found. ?:-P
291 lsbuf= split(lsbuf, cmd,'\n');
292 lyxerr[Debug::LYXSERVER]
293 << "LyXComm: status:" << status
294 << ", lsbuf:" << lsbuf
295 << ", cmd:" << cmd << endl;
297 c->clientcb(c->client, cmd);
302 { // EAGAIN is not really an error , it means we're
303 // only reading too fast for the writing process on
304 // the other end of the pipe.
306 return; // up to libforms select-loop (*crunch*)
310 lyxerr << "LyXComm: " << strerror(errno) << endl;
313 lyxerr << "LyxComm: truncated command: "
317 break; // reset connection
320 c->closeConnection();
326 void LyXComm::send(string const & msg)
329 lyxerr << "LyXComm: Request to send empty string. Ignoring."
334 if (lyxerr.debugging(Debug::LYXSERVER)) {
335 lyxerr << "LyXComm: Sending '" << msg << '\'' << endl;
338 if (pipename.empty()) return;
341 lyxerr << "LyXComm: Pipes are closed. Could not send "
343 } else if (::write(outfd, msg.c_str(), msg.length()) < 0) {
344 lyxerr << "LyXComm: Error sending message: " << msg
345 << '\n' << strerror(errno)
346 << "\nLyXComm: Resetting connection" << endl;
353 rc = DosResetBuffer(outfd); // To avoid synchronization problems.
354 if (rc != NO_ERROR) {
355 errnum = TranslateOS2Error(rc);
356 lyxerr << "LyXComm: Message could not be flushed: " << msg
357 << '\n' << strerror(errnum) << endl;
365 LyXServer::~LyXServer()
367 // say goodbye to clients so they stop sending messages
368 // modified june 1999 by stefano@zool.su.se to send as many bye
369 // messages as there are clients, each with client's name.
371 for (int i= 0; i<numclients; ++i) {
372 message = "LYXSRV:" + clients[i] + ":bye\n";
378 /* ---F+------------------------------------------------------------------ *\
379 Function : ServerCallback
381 Purpose : handle data gotten from communication
382 \* ---F------------------------------------------------------------------- */
384 void LyXServer::callback(LyXServer * serv, string const & msg)
386 lyxerr[Debug::LYXSERVER] << "LyXServer: Received: '"
387 << msg << '\'' << endl;
389 char const * p = msg.c_str();
391 // --- parse the string --------------------------------------------
393 // Format: LYXCMD:<client>:<func>:<argstring>\n
395 bool server_only = false;
397 // --- 1. check 'header' ---
399 if (compare(p, "LYXSRV:", 7) == 0) {
401 } else if (0 != compare(p, "LYXCMD:", 7)) {
402 lyxerr << "LyXServer: Unknown request" << endl;
407 // --- 2. for the moment ignore the client name ---
409 while(*p && *p != ':')
410 client += char(*p++);
414 // --- 3. get function name ---
416 while(*p && *p != ':')
419 // --- 4. parse the argument ---
421 if (!server_only && *p == ':' && *(++p)) {
422 while(*p && *p != '\n')
427 lyxerr[Debug::LYXSERVER]
428 << "LyXServer: Client: '" << client
429 << "' Command: '" << cmd
430 << "' Argument: '" << arg << '\'' << endl;
432 // --- lookup and exec the command ------------------
436 // return the greeting to inform the client that
438 if (cmd == "hello") {
440 if (serv->numclients == MAX_CLIENTS){ //paranoid check
441 lyxerr[Debug::LYXSERVER]
442 << "LyXServer: too many clients..."
446 int i= 0; //find place in clients[]
447 while (!serv->clients[i].empty()
448 && i<serv->numclients)
450 serv->clients[i] = client;
452 buf = "LYXSRV:" + client + ":hello\n";
453 lyxerr[Debug::LYXSERVER]
454 << "LyXServer: Greeting "
456 serv->pipes.send(buf);
457 } else if (cmd == "bye") {
458 // If clients == 0 maybe we should reset the pipes
459 // to prevent fake callbacks
460 int i = 0; //look if client is registered
461 for (; i < serv->numclients; ++i) {
462 if (serv->clients[i] == client) break;
464 if (i < serv->numclients) {
466 serv->clients[i].erase();
467 lyxerr[Debug::LYXSERVER]
468 << "LyXServer: Client "
469 << client << " said goodbye"
472 lyxerr[Debug::LYXSERVER]
473 << "LyXServer: ignoring bye messge from unregistered client"
477 lyxerr <<"LyXServer: Undefined server command "
478 << cmd << "." << endl;
484 // which lyxfunc should we let it connect to?
485 // The correct solution would be to have a
486 // specialized (non-gui) BufferView. But how do
487 // we do it now? Probably we should just let it
488 // connect to the lyxfunc in the single LyXView we
489 // support currently. (Lgb)
491 int action = lyxaction.LookupFunc(cmd);
496 rval = serv->func->dispatch(action, arg);
498 rval = "Unknown command";
501 //modified june 1999 stefano@zool.su.se:
502 //all commands produce an INFO or ERROR message
503 //in the output pipe, even if they do not return
504 //anything. See chapter 4 of Customization doc.
505 if (action<0 || serv->func->errorStat())
509 buf += string(client) + ":" + cmd + ":" + rval + "\n";
510 serv->pipes.send(buf);
512 // !!! we don't do any error checking -
513 // if the client won't listen, the
514 // message is lost and others too
515 // maybe; so the client should empty
516 // the outpipe before issuing a request.
524 /* ---F+------------------------------------------------------------------ *\
525 Function : LyxNotifyClient
526 Called by : WorkAreaKeyPress
527 Purpose : send a notify messge to a client
528 Parameters: s - string to send
530 \* ---F------------------------------------------------------------------- */
532 void LyXServer::notifyClient(string const & s)
534 string buf = string("NOTIFY:") + s + "\n";