]> git.lyx.org Git - lyx.git/blobdiff - src/lyxserver.C
bug 183
[lyx.git] / src / lyxserver.C
index fa0294c5dd921099b1b309161eead5611a935095..ba3e0856713dcda4bff3e9da40c60af9b3295dd4 100644 (file)
@@ -108,18 +108,23 @@ void LyXComm::openConnection()
        // We assume that we don't make it
        ready = false;
  
-       if (pipename.empty()) return;
+       if (pipename.empty()) {
+               lyxerr[Debug::LYXSERVER]
+                       << "LyXComm: server is disabled, nothing to do"
+                       << endl;
+               return;
+       }
 
-       if ((infd = startPipe(pipename + ".in")) == -1)
+       if ((infd = startPipe(inPipeName(), false)) == -1)
                return;
 
-       if ((outfd = startPipe(pipename + ".out")) == -1) {
-               endPipe(infd, pipename + ".in");
+       if ((outfd = startPipe(outPipeName(), true)) == -1) {
+               endPipe(infd, inPipeName());
                return;
        }
  
        if (fcntl(outfd, F_SETFL, O_NONBLOCK) < 0) {
-               lyxerr << "LyXComm: Could not set flags on pipe " << pipename << ".out"
+               lyxerr << "LyXComm: Could not set flags on pipe " << outPipeName()
                       << '\n' << strerror(errno) << endl;
                return;
        }
@@ -136,6 +141,9 @@ void LyXComm::closeConnection()
                lyxerr[Debug::LYXSERVER] << "LyXComm: Closing connection" << endl;
 
        if (pipename.empty()) {
+               lyxerr[Debug::LYXSERVER]
+                       << "LyXComm: server is disabled, nothing to do"
+                       << endl;
                return;
        }
 
@@ -144,13 +152,13 @@ void LyXComm::closeConnection()
                return;
        }
  
-       endPipe(infd, pipename + ".in");
-       endPipe(outfd, pipename + ".out");
+       endPipe(infd, inPipeName());
+       endPipe(outfd, outPipeName());
  
        ready = false;
 }
 
-int LyXComm::startPipe(string const & filename)
+int LyXComm::startPipe(string const & filename, bool write)
 {
        int fd;
  
@@ -201,7 +209,7 @@ int LyXComm::startPipe(string const & filename)
                       << strerror(errno) << endl;
                return -1;
        };
-       fd = ::open(filename.c_str(), O_RDONLY|O_NONBLOCK);
+       fd = ::open(filename.c_str(), write ? (O_RDWR) : (O_RDONLY|O_NONBLOCK));
 #endif
  
        if (fd < 0) {
@@ -210,12 +218,15 @@ int LyXComm::startPipe(string const & filename)
                lyx::unlink(filename);
                return -1;
        }
-       fl_add_io_callback(fd, FL_READ, C_LyXComm_callback, this);
+       if (!write)
+               fl_add_io_callback(fd, FL_READ, C_LyXComm_callback, this);
        return fd;
 }
 
 
-void LyXComm::endPipe(int fd, string const & filename)
+void LyXComm::endPipe(int fd, string const & filename)
 {
        if (fd < 0)
                return;
@@ -245,13 +256,17 @@ void LyXComm::endPipe(int fd, string const & filename)
                       << '\n' << strerror(errno) << endl;
        };
 #endif
+
+       fd = -1;
 }
 
 
 void LyXComm::emergencyCleanup()
 {
-       endPipe(infd, pipename + ".in");
-       endPipe(outfd, pipename + ".out");
+       if (!pipename.empty()) {
+               endPipe(infd, inPipeName());
+               endPipe(outfd, outPipeName());
+       }
 }
 
 
@@ -273,14 +288,14 @@ void LyXComm::callback(int fd, void *v)
        errno = 0;
        int status;
        // the single = is intended here.
-       while((status = read(fd, charbuf, CMDBUFLEN-1)))
+       while ((status = read(fd, charbuf, CMDBUFLEN-1)))
        {// break and return in loop
                if (status > 0) // got something
                {
                        charbuf[status]= '\0'; // turn it into a c string
                        lsbuf += strip(charbuf, '\r');
                        // commit any commands read
-                       while(lsbuf.find('\n') != string::npos) // while still
+                       while (lsbuf.find('\n') != string::npos) // while still
                                                        // commands
                                                        // left 
                        {
@@ -391,33 +406,33 @@ void LyXServer::callback(LyXServer * serv, string const & msg)
        //  Format: LYXCMD:<client>:<func>:<argstring>\n
        //
        bool server_only = false;
-       while(*p) {
+       while (*p) {
                // --- 1. check 'header' ---
 
                if (compare(p, "LYXSRV:", 7) == 0) {
                        server_only = true; 
                } else if (0 != compare(p, "LYXCMD:", 7)) {
-                       lyxerr << "LyXServer: Unknown request" << endl;
+                       lyxerr << "LyXServer: Unknown request \"" << p << "\"" << endl;
                        return;
                }
                p += 7;
                
                // --- 2. for the moment ignore the client name ---
                string client;
-               while(*p && *p != ':')
+               while (*p && *p != ':')
                        client += char(*p++);
                if (*p == ':') ++p;
                if (!*p) return;
                
                // --- 3. get function name ---
                string cmd;
-               while(*p && *p != ':')
+               while (*p && *p != ':')
                        cmd += char(*p++);
                
                // --- 4. parse the argument ---
                string arg;
                if (!server_only && *p == ':' && *(++p)) {
-                       while(*p && *p != '\n')
+                       while (*p && *p != '\n')
                                arg += char(*p++);
                        if (*p) ++p;
                }
@@ -486,7 +501,7 @@ void LyXServer::callback(LyXServer * serv, string const & msg)
                        // connect to the lyxfunc in the single LyXView we
                        // support currently. (Lgb)
 
-                       int action = lyxaction.LookupFunc(cmd);
+                       kb_action action = static_cast<kb_action>(lyxaction.LookupFunc(cmd));
                        //int action = -1;
                        string rval, buf;