repo: rxvt-unicode-sixel
action: commit
revision: 
path_from: 
revision_from: 54197cf33e22bcd417eb7d28ec83a86d4025569c:
path_to: 
revision_to: 
git.thebackupbox.net
rxvt-unicode-sixel
git clone git://git.thebackupbox.net/rxvt-unicode-sixel
commit 54197cf33e22bcd417eb7d28ec83a86d4025569c
Author: Marc Lehmann 
Date:   Fri Dec 23 14:46:35 2005 +0000

    *** empty log message ***

diff --git a/Changes b/Changes
index cb217ba459ffcf796fa33ae91d573036623570ab..
index ..2f9265a0b887d28da39e0d65f8a5d342b33b79fb 100644
--- a/Changes
+++ b/Changes
@@ -20,6 +20,7 @@ WISH: just for fun, do shade and tint with XRender.
           partial patch).
         - bump max columns/rows to 10000 each.
         - bump max savelines to 10000000.
+        - -pty-fd now passes the urxvtc fd to the urxvtd process.
         - major code cleanup (still not complete, though).
         - implement -hold option.
         - _major_ rewrite of internal buffer handling:
diff --git a/MANIFEST b/MANIFEST
index 01dfe5a548a3c32b05c96c9bc052e1937c073972..
index ..477042f692276bd1cf4b23c576ea2df4b17845c9 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -68,6 +68,8 @@ src/command.C
 src/command.h
 src/encoding.C
 src/encoding.h
+src/fdpass.h
+src/fdpass.C
 src/feature.h
 src/init.C
 src/init.h
diff --git a/autoconf/config.h.in b/autoconf/config.h.in
index d442d1aad442a317915bab320d2c3f8f732a4592..
index ..b90b4900687f628d8c045577d2d81207b49355f6 100644
--- a/autoconf/config.h.in
+++ b/autoconf/config.h.in
@@ -316,6 +316,9 @@
 /* Define if nl_langinfo(CODESET) works */
 #undef HAVE_NL_LANGINFO

+/* Define if sys/socket.h defines the necessary macros/functions for file handle passing */
+#undef HAVE_UNIX_FDPASS
+
 /* Define if your XIMCallback specifies XIC as first type. */
 #undef XIMCB_PROTO_BROKEN

diff --git a/autoconf/configure.in b/autoconf/configure.in
index 53b6121d1df2e961a4e75c4be64448e950db0283..
index ..a2e96ea99973445c9277232b44655a3a612feca3 100644
--- a/autoconf/configure.in
+++ b/autoconf/configure.in
@@ -1118,6 +1118,39 @@ if test x$rxvt_cv_func_nl_langinfo = xyes; then
   AC_DEFINE(HAVE_NL_LANGINFO, 1, Define if nl_langinfo(CODESET) works)
 fi

+AC_CACHE_CHECK(for unix-compliant filehandle passing ability, rxvt_can_pass_fds,
+[AC_TRY_LINK([
+#include 
+#include 
+],[
+{
+  msghdr msg;
+  iovec iov;
+  char buf [100];
+  char data = 0;
+
+  iov.iov_base = &data;
+  iov.iov_len  = 1;
+
+  msg.msg_iov        = &iov;
+  msg.msg_iovlen     = 1;
+  msg.msg_control    = buf;
+  msg.msg_controllen = sizeof buf;
+
+  cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type  = SCM_RIGHTS;
+  cmsg->cmsg_len   = 100;
+
+  *(int *)CMSG_DATA (cmsg) = 5;
+
+  return sendmsg (3, &msg, 0);
+}
+],[rxvt_can_pass_fds=yes],[rxvt_can_pass_fds=no])])
+if test x$rxvt_can_pass_fds = xyes; then
+   AC_DEFINE(HAVE_UNIX_FDPASS, 1, Define if sys/socket.h defines the necessary macros/functions for file handle passing)
+fi
+
 AC_CACHE_CHECK(for broken XIM callback, rxvt_broken_ximcb,
 [AC_COMPILE_IFELSE([
 #include 
diff --git a/configure b/configure
index 3018338f2937992af0922a52c4f9f4a16bf08533..
index ..c09fccd189dc4e93cf11bc2a04e05d0d8b6d281d 100755
--- a/configure
+++ b/configure
@@ -11340,6 +11340,94 @@ _ACEOF

 fi

+echo "$as_me:$LINENO: checking for unix-compliant filehandle passing ability" >&5
+echo $ECHO_N "checking for unix-compliant filehandle passing ability... $ECHO_C" >&6
+if test "${rxvt_can_pass_fds+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+#include 
+#include 
+
+int
+main ()
+{
+
+{
+  msghdr msg;
+  iovec iov;
+  char buf [100];
+  char data = 0;
+
+  iov.iov_base = &data;
+  iov.iov_len  = 1;
+
+  msg.msg_iov        = &iov;
+  msg.msg_iovlen     = 1;
+  msg.msg_control    = buf;
+  msg.msg_controllen = sizeof buf;
+
+  cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type  = SCM_RIGHTS;
+  cmsg->cmsg_len   = 100;
+
+  *(int *)CMSG_DATA (cmsg) = 5;
+
+  return sendmsg (3, &msg, 0);
+}
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+  (eval $ac_link) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+	 { ac_try='test -z "$ac_cxx_werror_flag"			 || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+	 { ac_try='test -s conftest$ac_exeext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  rxvt_can_pass_fds=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+rxvt_can_pass_fds=no
+fi
+rm -f conftest.err conftest.$ac_objext \
+      conftest$ac_exeext conftest.$ac_ext
+fi
+echo "$as_me:$LINENO: result: $rxvt_can_pass_fds" >&5
+echo "${ECHO_T}$rxvt_can_pass_fds" >&6
+if test x$rxvt_can_pass_fds = xyes; then
+
+cat >>confdefs.h <<\_ACEOF
+#define HAVE_UNIX_FDPASS 1
+_ACEOF
+
+fi
+
 echo "$as_me:$LINENO: checking for broken XIM callback" >&5
 echo $ECHO_N "checking for broken XIM callback... $ECHO_C" >&6
 if test "${rxvt_broken_ximcb+set}" = set; then
diff --git a/src/Makefile.in b/src/Makefile.in
index 91d62ed85ed666298c9d2bb9cf6bebca3831f2d3..
index ..04bb1b4c12670823923e36c50273cf18e8519068 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -27,6 +27,8 @@ COMMON = \
 	scrollbar.o scrollbar-next.o scrollbar-rxvt.o scrollbar-xterm.o scrollbar-plain.o \
 	strings.o xdefaults.o xpm.o encoding.o rxvttoolkit.o rxvtutil.o iom.o keyboard.o

+COMMON_DAEMON = rxvtdaemon.o fdpass.o
+
 RXVT_BASENAME=`$(ECHO) $(RXVTNAME)|$(SED) 's/$(EXEEXT)$$//'|$(SED) '$(transform)'`
 RXVT_BINNAME=$(DESTDIR)$(bindir)/$(RXVT_BASENAME)$(EXEEXT)
 RXVTC_BINNAME=$(DESTDIR)$(bindir)/$(RXVT_BASENAME)c$(EXEEXT)
@@ -61,11 +63,11 @@ rxvt: rxvt.o $(COMMON)
 	$(LINK) -o $@ $^ $(LIBS) $(XLIB) $(DLIB)
 #	$(LIBTOOL) --mode=link $(LINK) rxvt.o librxvt.la $(LIBS) $(XLIB) $(DLIB) -o $@

-rxvtd: rxvtd.o rxvtdaemon.o $(COMMON)
+rxvtd: rxvtd.o $(COMMON) $(COMMON_DAEMON)
 	$(LINK) -o $@ $^ $(LIBS) $(XLIB) $(DLIB)
 #	$(LIBTOOL) --mode=link $(LINK) rxvtd.o rxvtdaemon.o librxvt.la $(LIBS) $(XLIB) $(DLIB) -o $@

-rxvtc: rxvtc.o rxvtdaemon.o
+rxvtc: rxvtc.o $(COMMON_DAEMON)
 	$(LINK) -o $@ $^ $(LIBS) $(DLIB)
 #	$(LIBTOOL) --mode=link $(LINK) rxvtc.o rxvtdaemon.o $(LIBS) $(DLIB) -o $@

@@ -125,6 +127,7 @@ encoding.o: table/cns11643_1992_f.h table/big5_ext.h table/big5_plus.h
 encoding.o: table/viscii.h table/jis0201_1976_0.h table/jis0208_1990_0.h
 encoding.o: table/jis0212_1990_0.h table/jis0213_1.h table/jis0213_2.h
 encoding.o: table/compose.h table/category.h
+fdpass.o: ../config.h fdpass.h
 init.o: ../config.h rxvt.h rxvtlib.h ptytty.h feature.h encoding.h rxvtfont.h
 init.o: rxvtutil.h rxvttoolkit.h iom.h iom_conf.h callback.h salloc.h
 init.o: menubar.h init.h
@@ -214,6 +217,7 @@ encoding.lo: table/cns11643_1992_f.h table/big5_ext.h table/big5_plus.h
 encoding.lo: table/viscii.h table/jis0201_1976_0.h table/jis0208_1990_0.h
 encoding.lo: table/jis0212_1990_0.h table/jis0213_1.h table/jis0213_2.h
 encoding.lo: table/compose.h table/category.h
+fdpass.lo: ../config.h fdpass.h
 init.lo: ../config.h rxvt.h rxvtlib.h ptytty.h feature.h encoding.h
 init.lo: rxvtfont.h rxvtutil.h rxvttoolkit.h iom.h iom_conf.h callback.h
 init.lo: salloc.h menubar.h init.h
diff --git a/src/command.C b/src/command.C
index 489e5a7387b430e717ca9d33ceb3289ddab4a2c1..
index ..76eb690c6641b5bb9ac480a3c18cc5a3234dd743 100644
--- a/src/command.C
+++ b/src/command.C
@@ -1069,8 +1069,13 @@ rxvt_term::pty_fill ()
       cmdbuf_endp += n;
       return true;
     }
-  else if (n < 0 && errno != EAGAIN && errno != EINTR)
-    pty_ev.stop ();
+  else if ((n < 0 && errno != EAGAIN && errno != EINTR) || n == 0)
+    {
+      pty_ev.stop ();
+
+      if (!(options & Opt_hold))
+        destroy ();
+    }

   return false;
 }
diff --git a/src/fdpass.C b/src/fdpass.C
new file mode 100644
index 0000000000000000000000000000000000000000..d57282bc95d2e20f64f92fad08da1cbd9da10206
--- /dev/null
+++ b/src/fdpass.C
@@ -0,0 +1,105 @@
+/*--------------------------------*-C-*---------------------------------*
+ * File:	fdpass.C
+ *----------------------------------------------------------------------*
+ *
+ * All portions of code are copyright by their respective author/s.
+ * Copyright (c) 2005      Marc Lehmann 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *----------------------------------------------------------------------*/
+
+#include "../config.h"		/* NECESSARY */
+
+#if ENABLE_FRILLS && HAVE_UNIX_FDPASS
+
+#include 
+#include 
+
+#include "fdpass.h"
+
+#include  //d
+
+#ifndef CMSG_LEN // CMSG_SPACe && CMSG_LEN are rfc2292 extensions to unix
+# define CMSG_LEN(len) (sizeof (cmsghdr) + len)
+#endif
+
+int
+rxvt_send_fd (int socket, int fd)
+{
+  msghdr msg;
+  iovec iov;
+  char buf [CMSG_LEN (sizeof (int))];
+  char data = 0;
+
+  iov.iov_base = &data;
+  iov.iov_len  = 1;
+
+  msg.msg_name       = 0;
+  msg.msg_namelen    = 0;
+  msg.msg_iov        = &iov;
+  msg.msg_iovlen     = 1;
+  msg.msg_control    = (void *)buf;
+  msg.msg_controllen = sizeof buf;
+
+  cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type  = SCM_RIGHTS;
+  cmsg->cmsg_len   = CMSG_LEN (sizeof (int));
+
+  *(int *)CMSG_DATA (cmsg) = fd;
+
+  msg.msg_controllen = cmsg->cmsg_len;
+
+  return sendmsg (socket, &msg, 0);
+}
+
+int
+rxvt_recv_fd (int socket)
+{
+  msghdr msg;
+  iovec iov;
+  char buf [CMSG_LEN (sizeof (int))];  /* ancillary data buffer */
+  char data = 1;
+
+  iov.iov_base = &data;
+  iov.iov_len  = 1;
+
+  msg.msg_name       = 0;
+  msg.msg_namelen    = 0;
+  msg.msg_iov        = &iov;
+  msg.msg_iovlen     = 1;
+  msg.msg_control    = buf;
+  msg.msg_controllen = sizeof buf;
+
+  cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
+  cmsg->cmsg_level = SOL_SOCKET;
+  cmsg->cmsg_type  = SCM_RIGHTS;
+  cmsg->cmsg_len   = CMSG_LEN (sizeof (int));
+
+  msg.msg_controllen = cmsg->cmsg_len;
+
+  if (recvmsg (socket, &msg, 0) <= 0
+      || data               != 0
+      || msg.msg_controllen < CMSG_LEN (sizeof (int))
+      || cmsg->cmsg_level   != SOL_SOCKET
+      || cmsg->cmsg_type    != SCM_RIGHTS
+      || cmsg->cmsg_len     < CMSG_LEN (sizeof (int)))
+    return -1;
+
+  return *(int *)CMSG_DATA (cmsg);
+}
+
+#endif
+
diff --git a/src/fdpass.h b/src/fdpass.h
new file mode 100644
index 0000000000000000000000000000000000000000..6b02c570926f43f4ad043bdea3a5b4644c0ab02b
--- /dev/null
+++ b/src/fdpass.h
@@ -0,0 +1,31 @@
+/*--------------------------------*-C-*---------------------------------*
+ * File:	fdpass.h
+ *----------------------------------------------------------------------*
+ *
+ * All portions of code are copyright by their respective author/s.
+ * Copyright (c) 2005      Marc Lehmann 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *----------------------------------------------------------------------*/
+
+#include "../config.h"		/* NECESSARY */
+
+#if ENABLE_FRILLS && HAVE_UNIX_FDPASS
+
+int rxvt_send_fd (int socket, int fd);
+int rxvt_recv_fd (int socket);
+
+#endif
+
diff --git a/src/init.C b/src/init.C
index 90d6a3eac65c708701c0b4d88584a149ddf2c847..
index ..70e1b905e95680892ef9d945e53fc784d168ead6 100644
--- a/src/init.C
+++ b/src/init.C
@@ -1412,7 +1412,12 @@ rxvt_term::run_command (const char *const *argv)
   if (rs[Rs_pty_fd])
     {
       pty.pty = atoi (rs[Rs_pty_fd]);
-      fcntl (pty.pty, F_SETFL, O_NONBLOCK);
+
+      if (getfd_hook)
+        pty.pty = (*getfd_hook) (pty.pty);
+
+      if (pty.pty < 0 || fcntl (pty.pty, F_SETFL, O_NONBLOCK))
+        rxvt_fatal ("unusable pty-fd filehandle, aborting.\n");
     }
   else
 #endif
diff --git a/src/rxvt.h b/src/rxvt.h
index 15a5bd6a43546b393e5161ce66d8adb1fddab157..
index ..89c2a1b46481b7b07e39cd41e039cb917292ea8b 100644
--- a/src/rxvt.h
+++ b/src/rxvt.h
@@ -961,6 +961,7 @@ enum {
 extern class rxvt_failure_exception { } rxvt_failure_exception;

 typedef callback1 log_callback;
+typedef callback1 getfd_callback;

 extern void rxvt_vlog (const char *fmt, va_list arg_ptr);
 extern void rxvt_log (const char *fmt, ...);
@@ -1047,6 +1048,7 @@ extern class rxvt_composite_vec rxvt_composite;

 struct rxvt_term : zero_initialized, rxvt_vars {
   log_callback *log_hook;               // log error messages through this hook, if != 0
+  getfd_callback *getfd_hook;           // convert remote to local fd, if != 0

   struct mbstate  mbstate;              // current input multibyte state

diff --git a/src/rxvtc.C b/src/rxvtc.C
index ff9dbd0ed15ef0a52faf2e7cb8d0f46c709d98e2..
index ..e4d5db0e78af27e1af1476616ad137dc4772b510 100644
--- a/src/rxvtc.C
+++ b/src/rxvtc.C
@@ -22,6 +22,7 @@

 #include "../config.h"
 #include "rxvtdaemon.h"
+#include "fdpass.h"

 #include "rxvt.h"

@@ -99,6 +100,7 @@ main (int argc, const char *const *argv)
   c.send ("END");

   auto_str tok;
+  int cint;

   for (;;)
     if (!c.recv (tok))
@@ -108,6 +110,14 @@ main (int argc, const char *const *argv)
       }
     else if (!strcmp (tok, "MSG") && c.recv (tok))
       fprintf (stderr, "%s", (const char *)tok);
+    else if (!strcmp (tok, "GETFD") && c.recv (cint))
+      {
+        if (rxvt_send_fd (c.fd, cint) < 0)
+          {
+            fprintf (stderr, "unable to send fd %d: ", cint); perror (0);
+            exit (EXIT_FAILURE);
+          }
+      }
     else if (!strcmp (tok, "END"))
       {
         int success;
@@ -116,7 +126,7 @@ main (int argc, const char *const *argv)
       }
     else
       {
-        fprintf (stderr, "protocol error: received illegal token '%s'.\n", (const char *)tok);
+        fprintf (stderr, "protocol error: received unsupported token '%s'.\n", (const char *)tok);
         break;
       }

diff --git a/src/rxvtd.C b/src/rxvtd.C
index 7bd89af580cd69d45f6228ef0cdf9b9e1a0bafd3..
index ..4a714e1b66c72a1725298cf204e4de4adf43af90 100644
--- a/src/rxvtd.C
+++ b/src/rxvtd.C
@@ -23,6 +23,7 @@
 #include "../config.h"
 #include "rxvt.h"
 #include "rxvtdaemon.h"
+#include "fdpass.h"
 #include "iom.h"

 #include 
@@ -43,13 +44,16 @@ extern char **environ;

 struct server : rxvt_connection {
   log_callback log_cb;
+  getfd_callback getfd_cb;

   void read_cb (io_watcher &w, short revents); io_watcher read_ev;
   void log_msg (const char *msg);
+  int getfd (int remote_fd);

   server (int fd)
   : read_ev (this, &server::read_cb),
-    log_cb (this, &server::log_msg)
+    log_cb (this, &server::log_msg),
+    getfd_cb (this, &server::getfd)
   {
     this->fd = fd;
     read_ev.start (fd, EVENT_READ);
@@ -114,6 +118,17 @@ void unix_listener::accept_cb (io_watcher &w, short revents)
     }
 }

+int server::getfd (int remote_fd)
+{
+#if ENABLE_FRILLS && HAVE_UNIX_FDPASS
+  send ("GETFD");
+  send (remote_fd);
+  return rxvt_recv_fd (fd);
+#else
+  return -1;
+#endif
+}
+
 void server::log_msg (const char *msg)
 {
   send ("MSG"), send (msg);
@@ -179,6 +194,7 @@ void server::read_cb (io_watcher &w, short revents)
             rxvt_term *term = new rxvt_term;

             term->log_hook = &log_cb;
+            term->getfd_hook = &getfd_cb;
             term->argv = argv;
             term->envv = envv;

-----END OF PAGE-----