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;
251 void LyXComm::emergencyCleanup()
253 endPipe(infd, pipename + ".in");
254 endPipe(outfd, pipename + ".out");
258 // Receives messages and sends then to client
259 void LyXComm::callback(int fd, void *v)
261 LyXComm * c = static_cast<LyXComm*>(v);
263 if (lyxerr.debugging(Debug::LYXSERVER)) {
264 lyxerr << "LyXComm: Receiving from fd " << fd << endl;
267 const int CMDBUFLEN = 100;
268 char charbuf[CMDBUFLEN];
270 // nb! make lsbuf a class-member for multiple sessions
275 // the single = is intended here.
276 while((status = read(fd, charbuf, CMDBUFLEN-1)))
277 {// break and return in loop
278 if (status > 0) // got something
280 charbuf[status]= '\0'; // turn it into a c string
281 lsbuf += strip(charbuf, '\r');
282 // commit any commands read
283 while(lsbuf.find('\n') != string::npos) // while still
287 // split() grabs the entire string if
288 // the delim /wasn't/ found. ?:-P
289 lsbuf= split(lsbuf, cmd,'\n');
290 lyxerr[Debug::LYXSERVER]
291 << "LyXComm: status:" << status
292 << ", lsbuf:" << lsbuf
293 << ", cmd:" << cmd << endl;
295 c->clientcb(c->client, cmd);
300 { // EAGAIN is not really an error , it means we're
301 // only reading too fast for the writing process on
302 // the other end of the pipe.
304 return; // up to libforms select-loop (*crunch*)
308 lyxerr << "LyXComm: " << strerror(errno) << endl;
311 lyxerr << "LyxComm: truncated command: "
315 break; // reset connection
318 c->closeConnection();
324 void LyXComm::send(string const & msg)
327 lyxerr << "LyXComm: Request to send empty string. Ignoring."
332 if (lyxerr.debugging(Debug::LYXSERVER)) {
333 lyxerr << "LyXComm: Sending '" << msg << '\'' << endl;
336 if (pipename.empty()) return;
339 lyxerr << "LyXComm: Pipes are closed. Could not send "
341 } else if (::write(outfd, msg.c_str(), msg.length()) < 0) {
342 lyxerr << "LyXComm: Error sending message: " << msg
343 << '\n' << strerror(errno)
344 << "\nLyXComm: Resetting connection" << endl;
351 rc = DosResetBuffer(outfd); // To avoid synchronization problems.
352 if (rc != NO_ERROR) {
353 errnum = TranslateOS2Error(rc);
354 lyxerr << "LyXComm: Message could not be flushed: " << msg
355 << '\n' << strerror(errnum) << endl;
363 LyXServer::~LyXServer()
365 // say goodbye to clients so they stop sending messages
366 // modified june 1999 by stefano@zool.su.se to send as many bye
367 // messages as there are clients, each with client's name.
369 for (int i= 0; i<numclients; ++i) {
370 message = "LYXSRV:" + clients[i] + ":bye\n";
376 /* ---F+------------------------------------------------------------------ *\
377 Function : ServerCallback
379 Purpose : handle data gotten from communication
380 \* ---F------------------------------------------------------------------- */
382 void LyXServer::callback(LyXServer * serv, string const & msg)
384 lyxerr[Debug::LYXSERVER] << "LyXServer: Received: '"
385 << msg << '\'' << endl;
387 char const * p = msg.c_str();
389 // --- parse the string --------------------------------------------
391 // Format: LYXCMD:<client>:<func>:<argstring>\n
393 bool server_only = false;
395 // --- 1. check 'header' ---
397 if (compare(p, "LYXSRV:", 7) == 0) {
399 } else if (0 != compare(p, "LYXCMD:", 7)) {
400 lyxerr << "LyXServer: Unknown request" << endl;
405 // --- 2. for the moment ignore the client name ---
407 while(*p && *p != ':')
408 client += char(*p++);
412 // --- 3. get function name ---
414 while(*p && *p != ':')
417 // --- 4. parse the argument ---
419 if (!server_only && *p == ':' && *(++p)) {
420 while(*p && *p != '\n')
425 lyxerr[Debug::LYXSERVER]
426 << "LyXServer: Client: '" << client
427 << "' Command: '" << cmd
428 << "' Argument: '" << arg << '\'' << endl;
430 // --- lookup and exec the command ------------------
434 // return the greeting to inform the client that
436 if (cmd == "hello") {
438 if (serv->numclients == MAX_CLIENTS){ //paranoid check
439 lyxerr[Debug::LYXSERVER]
440 << "LyXServer: too many clients..."
444 int i= 0; //find place in clients[]
445 while (!serv->clients[i].empty()
446 && i<serv->numclients)
448 serv->clients[i] = client;
450 buf = "LYXSRV:" + client + ":hello\n";
451 lyxerr[Debug::LYXSERVER]
452 << "LyXServer: Greeting "
454 serv->pipes.send(buf);
455 } else if (cmd == "bye") {
456 // If clients == 0 maybe we should reset the pipes
457 // to prevent fake callbacks
458 int i = 0; //look if client is registered
459 for (; i < serv->numclients; ++i) {
460 if (serv->clients[i] == client) break;
462 if (i < serv->numclients) {
464 serv->clients[i].erase();
465 lyxerr[Debug::LYXSERVER]
466 << "LyXServer: Client "
467 << client << " said goodbye"
470 lyxerr[Debug::LYXSERVER]
471 << "LyXServer: ignoring bye messge from unregistered client"
475 lyxerr <<"LyXServer: Undefined server command "
476 << cmd << "." << endl;
482 // which lyxfunc should we let it connect to?
483 // The correct solution would be to have a
484 // specialized (non-gui) BufferView. But how do
485 // we do it now? Probably we should just let it
486 // connect to the lyxfunc in the single LyXView we
487 // support currently. (Lgb)
489 int action = lyxaction.LookupFunc(cmd);
494 rval = serv->func->dispatch(action, arg);
496 rval = "Unknown command";
499 //modified june 1999 stefano@zool.su.se:
500 //all commands produce an INFO or ERROR message
501 //in the output pipe, even if they do not return
502 //anything. See chapter 4 of Customization doc.
503 if (action<0 || serv->func->errorStat())
507 buf += string(client) + ":" + cmd + ":" + rval + "\n";
508 serv->pipes.send(buf);
510 // !!! we don't do any error checking -
511 // if the client won't listen, the
512 // message is lost and others too
513 // maybe; so the client should empty
514 // the outpipe before issuing a request.
522 /* ---F+------------------------------------------------------------------ *\
523 Function : LyxNotifyClient
524 Called by : WorkAreaKeyPress
525 Purpose : send a notify messge to a client
526 Parameters: s - string to send
528 \* ---F------------------------------------------------------------------- */
530 void LyXServer::notifyClient(string const & s)
532 string buf = string("NOTIFY:") + s + "\n";