]> git.lyx.org Git - lyx.git/blobdiff - src/lyxserver.C
removed a warning from screen and added CFLAGS in lyx.spec.in.
[lyx.git] / src / lyxserver.C
index e5cda8f139790ea23216a96291198ab051acbbd5..719412757e8930080993a5691f35a3dabbed1ee7 100644 (file)
@@ -1,13 +1,13 @@
 // -*- C++ -*-
 /* This file is part of
  * ======================================================
-* 
-*           LyX, The Document Processor
-*        
-*           Copyright (C) 1995 Matthias Ettrich
-*           Copyright (C) 1995-1998 The LyX Team.
-*
-*======================================================*/
+ 
+ *           LyX, The Document Processor
+ *        
+ *           Copyright 1995 Matthias Ettrich
+ *           Copyright 1995-1999 The LyX Team.
+ *
+ * ======================================================*/
 
 /**
   Docu   : To use the lyxserver define the name of the pipe in your
 
 #include <config.h>
 
-#include <string.h>
-#include <stdio.h>
+#include <cstring>
+#include <cstdio>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <errno.h>
+#include <cerrno>
 #include FORMS_H_LOCATION
 
 #ifdef __GNUG__
 #include "lyxserver.h"
 #include "lyxfunc.h"
 #include "lyx_main.h"
-#include "error.h"
+#include "debug.h"
+#include "support/lstrings.h"
 
 #ifdef __EMX__
-#include <stdlib.h>
+#include <cstdlib>
 #include <io.h>
 #define OS2EMX_PLAIN_CHAR
 #define INCL_DOSNMPIPES
@@ -74,25 +75,23 @@ int mkfifo( char *__path, mode_t __mode ) {
 #endif
 
 
-//     $Id: lyxserver.C,v 1.1 1999/09/27 18:44:38 larsbj Exp $ 
-
-#if !defined(lint) && !defined(WITH_WARNINGS)
-static char vcid[] = "$Id: lyxserver.C,v 1.1 1999/09/27 18:44:38 larsbj Exp $";
-#endif /* lint */
-       
 /* === variables ========================================================= */
 
 extern LyXAction lyxaction;
 
+// C wrapper
+extern "C" void C_LyXComm_callback(int fd, void *v);
+
+
 // LyXComm class
  
  // Open pipes
 void LyXComm::openConnection() {
-       lyxerr.debug("LyXComm: Opening connection", Error::LYXSERVER);
+       lyxerr[Debug::LYXSERVER] << "LyXComm: Opening connection" << endl;
        
        // If we are up, that's an error
        if (ready) {
-               lyxerr.print("LyXComm: Already connected");
+               lyxerr << "LyXComm: Already connected" << endl;
                return;
        }
        // We assume that we don't make it
@@ -102,7 +101,7 @@ void LyXComm::openConnection() {
 
        // --- prepare input pipe ---------------------------------------
  
-       LString tmp = pipename + ".in";
+       string tmp = pipename + ".in";
        
 #ifdef __EMX__
        HPIPE fd;
@@ -116,32 +115,32 @@ void LyXComm::openConnection() {
 #else
        if (access(tmp.c_str(), F_OK) == 0) {
 #endif
-               lyxerr.print("LyXComm: Pipe " + tmp + " already exists.");
-               lyxerr.print("If no other LyX program is active, please delete"
-                            " the pipe by hand and try again.");
-               pipename = LString();
+               lyxerr << "LyXComm: Pipe " << tmp << " already exists.\n"
+                      << "If no other LyX program is active, please delete"
+                       " the pipe by hand and try again." << endl;
+               pipename = string();
                return;
        }
 #ifndef __EMX__
        if (mkfifo(tmp.c_str(), 0600) < 0) {
-               lyxerr.print("LyXComm: Could not create pipe " + tmp);
-               lyxerr.print(strerror(errno));
+               lyxerr << "LyXComm: Could not create pipe " << tmp << '\n'
+                      << strerror(errno) << endl;
                return;
        };
        infd = open(tmp.c_str(), O_RDONLY|O_NONBLOCK);
 #else
        if (rc != NO_ERROR) {
                errnum = TranslateOS2Error(rc);
-               lyxerr.print("LyXComm: Could not create pipe " + tmp);
-               lyxerr.print(strerror(errnum));
+               lyxerr <<"LyXComm: Could not create pipe " << tmp
+                      << strerror(errnum) << endl;
                return;
        };
        // Listen to it.
        rc = DosConnectNPipe(fd);
        if (rc != NO_ERROR && rc != ERROR_PIPE_NOT_CONNECTED) {
                errnum = TranslateOS2Error(rc);
-               lyxerr.print("LyXComm: Could not create pipe " + tmp);
-               lyxerr.print(strerror(errnum));
+               lyxerr <<"LyXComm: Could not create pipe " + tmp);
+               lyxerr <<strerror(errnum);
                return;
        };
        // Imported handles can be used both with OS/2 APIs and emx
@@ -149,11 +148,11 @@ void LyXComm::openConnection() {
        infd = _imphandle(fd);
 #endif
        if (infd < 0) {
-               lyxerr.print("LyXComm: Could not open pipe " + tmp);
-               lyxerr.print(strerror(errno));
+               lyxerr << "LyXComm: Could not open pipe " << tmp << '\n'
+                      << strerror(errno) << endl;
                return;
        }
-       fl_add_io_callback(infd, FL_READ, callback, (void*)this);
+       fl_add_io_callback(infd, FL_READ, C_LyXComm_callback, (void*)this);
  
        // --- prepare output pipe ---------------------------------------
  
@@ -167,56 +166,58 @@ void LyXComm::openConnection() {
 
        if (rc == ERROR_PIPE_BUSY) {
 #endif
-               lyxerr.print("LyXComm: Pipe " + tmp + " already exists.");
-               lyxerr.print("If no other LyX program is active, please delete"
-                            " the pipe by hand and try again.");
-               pipename = LString();
+               lyxerr << "LyXComm: Pipe " << tmp << " already exists.\n"
+                      << "If no other LyX program is active, please delete"
+                       " the pipe by hand and try again." << endl;
+               pipename = string();
                return;
        }
 #ifndef __EMX__
        if (mkfifo(tmp.c_str(), 0600) < 0) {
-               lyxerr.print("LyXComm: Could not create pipe " + tmp);
-               lyxerr.print(strerror(errno));
+               lyxerr << "LyXComm: Could not create pipe " << tmp << '\n'
+                      << strerror(errno) << endl;
                return;
        };
        if (access(tmp.c_str(), F_OK) != 0) {
-               lyxerr.print("LyXComm: Pipe " + tmp + " does not exist");
+               lyxerr << "LyXComm: Pipe " << tmp
+                      << " does not exist" << endl;
                return;
        }
        outfd = open(tmp.c_str(), O_RDWR);
 #else
        if (rc != NO_ERROR) {
                errnum = TranslateOS2Error(rc);
-               lyxerr.print("LyXComm: Could not create pipe " + tmp);
-               lyxerr.print(strerror(errnum));
+               lyxerr << "LyXComm: Could not create pipe " << tmp << '\n'
+                      << strerror(errnum) << endl;
                return;
        }
        rc = DosConnectNPipe(fd);
        if (rc == ERROR_BAD_PIPE) {
-               lyxerr.print("LyXComm: Pipe " + tmp + " does not exist");
+               lyxerr << "LyXComm: Pipe " << tmp
+                      << " does not exist" << endl;
                return;
        }
        if (rc != NO_ERROR && rc != ERROR_PIPE_NOT_CONNECTED) {
                errnum = TranslateOS2Error(rc);
-               lyxerr.print("LyXComm: Could not create pipe " + tmp);
-               lyxerr.print(strerror(errnum));
+               lyxerr << "LyXComm: Could not create pipe " << tmp << '\n'
+                      << strerror(errnum) << endl;
                return;
        }
        outfd = _imphandle(fd);
 #endif
        if (outfd < 0) {
-               lyxerr.print("LyXComm: Could not open pipe " + tmp);
-               lyxerr.print(strerror(errno));
+               lyxerr << "LyXComm: Could not open pipe " << tmp << '\n'
+                      << strerror(errno) << endl;
                return;
        }
        if (fcntl(outfd, F_SETFL, O_NONBLOCK) < 0) {
-               lyxerr.print("LyXComm: Could not set flags on pipe " + tmp);
-               lyxerr.print(strerror(errno));
+               lyxerr << "LyXComm: Could not set flags on pipe " << tmp
+                      << '\n' << strerror(errno) << endl;
                return;
        };
        // We made it!
        ready = true;
-       lyxerr.debug("LyXComm: Connection established", Error::LYXSERVER);
+       lyxerr[Debug::LYXSERVER] << "LyXComm: Connection established" << endl;
 }
  
 /// Close pipes
@@ -225,60 +226,60 @@ void LyXComm::closeConnection() {
        APIRET rc;
        int errnum;
 #endif
-               lyxerr.debug("LyXComm: Closing connection", Error::LYXSERVER);
+               lyxerr[Debug::LYXSERVER] << "LyXComm: Closing connection" << endl;
 
        if (pipename.empty()) {
                return;
        }
 
        if (!ready) {
-               lyxerr.print("LyXComm: Already disconnected");
+               lyxerr << "LyXComm: Already disconnected" << endl;
                return;
        }
  
        if(infd > -1) {
-               fl_remove_io_callback(infd, FL_READ, callback);
+               fl_remove_io_callback(infd, FL_READ, C_LyXComm_callback);
  
-               LString tmp = pipename + ".in";
+               string tmp = pipename + ".in";
 #ifdef __EMX__         // Notify the operating system.
                rc = DosDisConnectNPipe(infd);
                if (rc != NO_ERROR) {
                        errnum = TranslateOS2Error(rc);
-                       lyxerr.print("LyXComm: Could not disconnect pipe " + tmp);
-                       lyxerr.print(strerror(errnum));
+                       lyxerr << "LyXComm: Could not disconnect pipe " << tmp
+                              << '\n' << strerror(errnum) << endl;
                        return;
                }
 #endif
                if (close(infd) < 0) {
-                       lyxerr.print("LyXComm: Could not close pipe " + tmp);
-                       lyxerr.print(strerror(errno));
+                       lyxerr << "LyXComm: Could not close pipe " << tmp
+                              << '\n' << strerror(errno) << endl;
                }
 #ifndef __EMX__                // OS/2 named pipes will be automatically removed.
                if (unlink(tmp.c_str()) < 0){
-                       lyxerr.print("LyXComm: Could not remove pipe " + tmp);
-                       lyxerr.print(strerror(errno));
+                       lyxerr << "LyXComm: Could not remove pipe " << tmp
+                              << '\n' << strerror(errno) << endl;
                };
 #endif
        }
        if(outfd > -1) {
-               LString tmp = pipename + ".out";
+               string tmp = pipename + ".out";
 #ifdef __EMX__
                rc = DosDisConnectNPipe(outfd);
                if (rc != NO_ERROR) {
                        errnum = TranslateOS2Error(rc);
-                       lyxerr.print("LyXComm: Could not disconnect pipe " + tmp);
-                       lyxerr.print(strerror(errnum));
+                       lyxerr << "LyXComm: Could not disconnect pipe " << tmp
+                              << '\n' << strerror(errnum) << endl;
                        return;
                }
 #endif
                if (close(outfd) < 0) {
-                       lyxerr.print("LyXComm: Could not close pipe " + tmp);
-                       lyxerr.print(strerror(errno));
+                       lyxerr << "LyXComm: Could not close pipe " << tmp
+                              << '\n' << strerror(errno) << endl;
                }
 #ifndef __EMX__
                if (unlink(tmp.c_str()) < 0){
-                       lyxerr.print("LyXComm: Could not remove pipe " + tmp);
-                       lyxerr.print(strerror(errno));
+                       lyxerr << "LyXComm: Could not remove pipe " << tmp
+                              << '\n' << strerror(errno) << endl;
                };
 #endif
        }
@@ -290,15 +291,15 @@ void LyXComm::callback(int fd, void *v)
 {
        LyXComm * c = (LyXComm *) v;
  
-       if (lyxerr.debugging(Error::LYXSERVER)) {
-               lyxerr.print(LString("LyXComm: Receiving from fd ") + int(fd));
+       if (lyxerr.debugging(Debug::LYXSERVER)) {
+               lyxerr << "LyXComm: Receiving from fd " << fd << endl;
        }
  
         const int CMDBUFLEN = 100;
        char charbuf[CMDBUFLEN];
-       LString cmd;
+       string cmd;
 // nb! make lsbuf a class-member for multiple sessions
-       static LString lsbuf;
+       static string lsbuf;
 
        errno = 0;
        int status;
@@ -308,20 +309,19 @@ void LyXComm::callback(int fd, void *v)
                if(status > 0) // got something
                {
                        charbuf[status]='\0'; // turn it into a c string
-                       lsbuf += charbuf;
-                       lsbuf.strip('\r');
+                       lsbuf += strip(charbuf, '\r');
                        // commit any commands read
-                       while(lsbuf.charPos('\n') >= 0) // while still
+                       while(lsbuf.find('\n') != string::npos) // while still
                                                        // commands
                                                        // left 
                        {
                                // split() grabs the entire string if
                                // the delim /wasn't/ found. ?:-P 
-                               lsbuf.split(cmd,'\n');
-                               lyxerr.debug(LString("LyXComm: status:") 
-                                            + status + ", lsbuf:" + lsbuf 
-                                            + ", cmd:" + cmd, 
-                                            Error::LYXSERVER);
+                               lsbuf=split(lsbuf, cmd,'\n');
+                               lyxerr[Debug::LYXSERVER]
+                                       << "LyXComm: status:" << status
+                                       << ", lsbuf:" << lsbuf 
+                                       << ", cmd:" << cmd << endl;
                                if(!cmd.empty())
                                        c->clientcb(c->client, cmd); 
                                        //\n or not \n?
@@ -336,12 +336,12 @@ void LyXComm::callback(int fd, void *v)
                }
                if(errno != 0 )
                {
-                       lyxerr.print(LString("LyXComm: ") + strerror(errno));
+                       lyxerr << "LyXComm: " << strerror(errno) << endl;
                        if(!lsbuf.empty())
                        {
-                               lyxerr.print("LyxComm: truncated command: " 
-                                            + lsbuf);
-                               lsbuf.clean();
+                               lyxerr << "LyxComm: truncated command: " 
+                                      << lsbuf << endl;
+                               lsbuf.clear();
                        }
                        break; // reset connection
                }
@@ -350,25 +350,33 @@ void LyXComm::callback(int fd, void *v)
        c->openConnection();
        errno=0;
 }
+
+extern "C" void C_LyXComm_callback(int fd, void *v)
+{
+       LyXComm::callback(fd, v);
+}
+
  
-void LyXComm::send(LString const & msg) {
+void LyXComm::send(string const & msg) {
        if (msg.empty()) {
-               lyxerr.print("LyXComm: Request to send empty string. Ignoring.");
+               lyxerr << "LyXComm: Request to send empty string. Ignoring."
+                      << endl;
                return;
        }
 
-       if (lyxerr.debugging(Error::LYXSERVER)) {
-               lyxerr.print("LyXComm: Sending '" + msg + '\'');
+       if (lyxerr.debugging(Debug::LYXSERVER)) {
+               lyxerr << "LyXComm: Sending '" << msg << '\'' << endl;
        }
 
        if (pipename.empty()) return;
 
        if (!ready) {
-               lyxerr.print("LyXComm: Pipes are closed. Could not send "+ msg);
+               lyxerr << "LyXComm: Pipes are closed. Could not send "
+                      << msg << endl;
        } else if (write(outfd, msg.c_str(), msg.length()) < 0) {
-               lyxerr.print("LyXComm: Error sending message: " + msg);
-               lyxerr.print(strerror(errno));
-               lyxerr.print("LyXComm: Resetting connection");
+               lyxerr << "LyXComm: Error sending message: " << msg
+                      << '\n' << strerror(errno)
+                      << "\nLyXComm: Resetting connection" << endl;
                closeConnection();
                openConnection();
        }
@@ -378,8 +386,8 @@ void LyXComm::send(LString const & msg) {
        rc = DosResetBuffer(outfd);     // To avoid synchronization problems.
        if (rc != NO_ERROR) {
                errnum = TranslateOS2Error(rc);
-               lyxerr.print("LyXComm: Message could not be flushed: " +msg);
-               lyxerr.print(strerror(errnum));
+               lyxerr <<"LyXComm: Message could not be flushed: " +msg);
+               lyxerr <<strerror(errnum));
        }
 #endif
 }
@@ -392,7 +400,7 @@ 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.
-       LString message;
+       string message;
        for (int i=0; i<numclients; i++) {
                message = "LYXSRV:" + clients[i] + ":bye\n";
                pipes.send(message);
@@ -406,9 +414,10 @@ LyXServer::~LyXServer()
     Purpose   : handle data gotten from communication
 \* ---F------------------------------------------------------------------- */
 
-void LyXServer::callback(LyXServer * serv, LString const & msg)
+void LyXServer::callback(LyXServer * serv, string const & msg)
 {
-       lyxerr.debug("LyXServer: Received: '" + msg + '\'', Error::LYXSERVER);
+       lyxerr[Debug::LYXSERVER] << "LyXServer: Received: '"
+                                << msg << '\'' << endl;
  
        char const *p = msg.c_str();
  
@@ -422,43 +431,48 @@ void LyXServer::callback(LyXServer * serv, LString const & msg)
                if (strncmp(p, "LYXSRV:", 7)==0) {
                        server_only = true; 
                } else if(0!=strncmp(p, "LYXCMD:", 7)) {
-                       lyxerr.print("LyXServer: Unknown request");
+                       lyxerr << "LyXServer: Unknown request" << endl;
                        return;
                }
                p += 7;
                
                // --- 2. for the moment ignore the client name ---
-               LString client;
+               string client;
                while(*p && *p != ':')
                        client += char(*p++);
                if(*p == ':') p++;
                if(!*p) return;
                
                // --- 3. get function name ---
-               LString cmd;
+               string cmd;
                while(*p && *p != ':')
                        cmd += char(*p++);
                
                // --- 4. parse the argument ---
-               LString arg;
+               string arg;
                if(!server_only && *p == ':' && *(++p)) {
                        while(*p && *p != '\n')
                                arg += char(*p++);
                        if(*p) p++;
                }
  
-               lyxerr.debug("LyXServer: Client: '" + client + "' Command: '" + cmd + "' Argument: '" + arg + '\'', Error::LYXSERVER);
+               lyxerr[Debug::LYXSERVER]
+                       << "LyXServer: Client: '" << client
+                       << "' Command: '" << cmd
+                       << "' Argument: '" << arg << '\'' << endl;
                
                // --- lookup and exec the command ------------------
  
                if (server_only) {
-                       LString buf;
+                       string buf;
                        // return the greeting to inform the client that 
                        // we are listening.
                        if (cmd == "hello") {
                                // One more client
                                if(serv->numclients==MAX_CLIENTS){ //paranoid check
-                                       lyxerr.debug("LyXServer: too many clients...", Error::LYXSERVER);
+                                       lyxerr[Debug::LYXSERVER]
+                                               << "LyXServer: too many clients..."
+                                               << endl;
                                        return;
                                }
                                int i=0; //find place in clients[]
@@ -468,7 +482,9 @@ void LyXServer::callback(LyXServer * serv, LString const & msg)
                                serv->clients[i] = client;
                                serv->numclients++;
                                buf = "LYXSRV:" + client + ":hello\n";
-                               lyxerr.debug("LyXServer: Greeting " + client, Error::LYXSERVER);
+                               lyxerr[Debug::LYXSERVER]
+                                       << "LyXServer: Greeting "
+                                       << client << endl;
                                serv->pipes.send(buf);
                        } else if (cmd == "bye") {
                                // If clients==0 maybe we should reset the pipes
@@ -479,15 +495,19 @@ void LyXServer::callback(LyXServer * serv, LString const & msg)
                                }
                                if (i<serv->numclients) {
                                        serv->numclients--;
-                                       serv->clients[i].clean();
-                                       lyxerr.debug("LyXServer: Client " + client + " said goodbye",
-                                               Error::LYXSERVER);
+                                       serv->clients[i].clear();
+                                       lyxerr[Debug::LYXSERVER]
+                                               << "LyXServer: Client "
+                                               << client << " said goodbye"
+                                               << endl;
                                } else {
-                                       lyxerr.debug("LyXServer: ignoring bye messge from unregistered client" +
-                                               client + "\n", Error::LYXSERVER);
+                                       lyxerr[Debug::LYXSERVER]
+                                               << "LyXServer: ignoring bye messge from unregistered client"
+                                               << client << endl;
                                }
                        } else {
-                               lyxerr.print("LyXServer: Undefined server command " + cmd + ".");
+                               lyxerr <<"LyXServer: Undefined server command "
+                                      << cmd << "." << endl;
                        }
                        return;
                }
@@ -502,7 +522,7 @@ void LyXServer::callback(LyXServer * serv, LString const & msg)
 
                        int action = lyxaction.LookupFunc(cmd.c_str());
                        //int action = -1;
-                       LString rval, buf;
+                       string rval, buf;
                    
                        if (action>=0) {
                                rval = serv->func->Dispatch(action, arg.c_str());
@@ -518,7 +538,7 @@ void LyXServer::callback(LyXServer * serv, LString const & msg)
                                buf = "ERROR:";
                        else
                                buf = "INFO:";
-                       buf += LString(client) + ":" + cmd      + ":" + rval + "\n";
+                       buf += string(client) + ":" + cmd       + ":" + rval + "\n";
                        serv->pipes.send(buf);
 
                        // !!! we don't do any error checking -
@@ -541,9 +561,9 @@ void LyXServer::callback(LyXServer * serv, LString const & msg)
    Returns   : nothing
    \* ---F------------------------------------------------------------------- */
 
-void LyXServer::notifyClient(LString const & s)
+void LyXServer::notifyClient(string const & s)
 {
-       LString buf = LString("NOTIFY:") + s + "\n";
+       string buf = string("NOTIFY:") + s + "\n";
        pipes.send(buf);
 }