aboutsummaryrefslogtreecommitdiffstats
path: root/src/ptty.c
diff options
context:
space:
mode:
authorjames <>2008-02-09 15:47:28 +0000
committerjames <>2008-02-09 15:47:28 +0000
commit5ee4b614cdc64a13aeeb00bf0e0ecbc234e8852d (patch)
treec422688a5e8a11bd177566121473ba1f0000612b /src/ptty.c
parenta4101322d71ce2f800d741e98ec8f537c70b663f (diff)
downloadsympathy-5ee4b614cdc64a13aeeb00bf0e0ecbc234e8852d.tar.gz
sympathy-5ee4b614cdc64a13aeeb00bf0e0ecbc234e8852d.tar.bz2
sympathy-5ee4b614cdc64a13aeeb00bf0e0ecbc234e8852d.zip
*** empty log message ***
Diffstat (limited to 'src/ptty.c')
-rw-r--r--src/ptty.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/ptty.c b/src/ptty.c
new file mode 100644
index 0000000..dee241b
--- /dev/null
+++ b/src/ptty.c
@@ -0,0 +1,157 @@
+/*
+ * ptty.c:
+ *
+ * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
+ * All rights reserved.
+ *
+ */
+
+static char rcsid[] = "$Id$";
+
+/*
+ * $Log$
+ * Revision 1.1 2008/02/09 15:47:28 james
+ * *** empty log message ***
+ *
+ * Revision 1.2 2008/02/07 11:11:14 staffcvs
+ * *** empty log message ***
+ *
+ * Revision 1.1 2008/02/07 01:02:52 james
+ * *** empty log message ***
+ *
+ * Revision 1.3 2008/02/06 17:53:28 james
+ * *** empty log message ***
+ *
+ * Revision 1.2 2008/02/04 02:05:06 james
+ * *** empty log message ***
+ *
+ * Revision 1.1 2008/02/04 01:32:39 james
+ * *** empty log message ***
+ *
+ */
+
+#include "project.h"
+
+
+typedef struct {
+ TTY_SIGNATURE;
+ int fd;
+ pid_t child;
+} PTTY;
+
+
+void ptty_close(TTY *_t)
+{
+PTTY *t=(PTTY *) _t;
+
+if (!t) return;
+
+close(t->fd);
+free(t);
+}
+
+
+
+static int
+ptty_read (TTY * _t, void *buf, int len)
+{
+PTTY *t=(PTTY *)_t;
+ int red, done = 0;
+
+ do
+ {
+
+ red = wrap_read (t->fd, buf, len);
+ if (red < 0)
+ return -1;
+ if (!red)
+ return done;
+
+ buf += red;
+ len -= red;
+ done += red;
+ }
+ while (len);
+
+
+ return done;
+}
+
+
+static int
+ptty_write (TTY * _t, void *buf, int len)
+{
+ int writ, done = 0;
+ PTTY *t=(PTTY *) _t;
+
+ do
+ {
+
+ writ = wrap_write (t->fd, buf, len);
+ if (writ < 0)
+ return -1;
+ if (!writ)
+ sleep (1);
+
+ buf += writ;
+ len -= writ;
+ done += writ;
+ }
+ while (len);
+
+
+ return done;
+}
+
+TTY * ptty_open(char *path,char *argv[])
+{
+ PTTY *t;
+ pid_t child;
+ char name[1024];
+ struct winsize winsize = { 0 };
+ struct termios termios;
+ int fd;
+ char *default_argv={"-",(char *) 0};
+
+ child = forkpty (&fd, name, &termios, &winsize);
+
+ switch (child)
+ {
+ case -1: /*boo hiss */
+ return -1;
+ case 0: /*waaah */
+ setenv ("TERM", "vt102", 1);
+ setenv ("LANG", "C", 1);
+ if (!path)
+ path="/bin/sh";
+
+ if (!argv)
+ argv=default_argv;
+
+ execv (path,argv);
+ _exit (-1);
+ }
+
+ set_nonblocking (fd);
+
+ t=(PTTY*) malloc(sizeof(PTTY));
+
+ strncpy(t->name,name,sizeof(t->name));
+ t->name[sizeof(t->name)-1]=0;
+
+ t->read=ptty_read;
+ t->write=ptty_write;
+ t->close=ptty_close;
+
+ default_termios (&termios);
+
+ winsize.ws_row = VT102_ROWS;
+ winsize.ws_col = VT102_COLS;
+
+ t->fd = open_fd_to_pty (path,argv);
+ t->pid=child;
+ t->rfd=t->fd;
+ t->wfd=0;
+
+ return (TTY *) t;
+}