And that's it! pty.c is now a real pty backend rather than a
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Thu, 10 Oct 2002 12:40:05 +0000 (12:40 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Thu, 10 Oct 2002 12:40:05 +0000 (12:40 +0000)
loopback interface; pterm now runs $SHELL and gives every impression
of being not a bad terminal emulator. I'm quite pleased with that. :-)

git-svn-id: svn://svn.tartarus.org/sgt/putty@2015 cda61777-01e9-0310-a592-d414129be87e

unix/pterm.c
unix/pty.c

index 97fe6c4..1cf6e6e 100644 (file)
@@ -8,6 +8,9 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 
@@ -593,6 +596,32 @@ gint timer_func(gpointer data)
     return TRUE;
 }
 
+void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
+{
+    /* struct gui_data *inst = (struct gui_data *)data; */
+    char buf[4096];
+    int ret;
+
+    ret = read(sourcefd, buf, sizeof(buf));
+
+    /*
+     * Clean termination condition is that either ret == 0, or ret
+     * < 0 and errno == EIO. Not sure why the latter, but it seems
+     * to happen. Boo.
+     */
+    if (ret == 0 || (ret < 0 && errno == EIO)) {
+       exit(0);
+    }
+
+    if (ret < 0) {
+       perror("read pty master");
+       exit(1);
+    }
+    if (ret > 0)
+       from_backend(0, buf, ret);
+    term_out();
+}
+
 void destroy(GtkWidget *widget, gpointer data)
 {
     gtk_main_quit();
@@ -793,6 +822,7 @@ void modalfatalbox(char *p, ...)
 int main(int argc, char **argv)
 {
     GtkWidget *window;
+    extern int pty_master_fd;         /* declared in pty.c */
 
     gtk_init(&argc, &argv);
 
@@ -825,6 +855,7 @@ int main(int argc, char **argv)
     gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
                       GTK_SIGNAL_FUNC(expose_area), inst);
     gtk_timeout_add(20, timer_func, inst);
+    gdk_input_add(pty_master_fd, GDK_INPUT_READ, pty_input_func, inst);
     gtk_widget_add_events(GTK_WIDGET(inst->area),
                          GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
 
index 291247e..56d7d89 100644 (file)
@@ -1,5 +1,11 @@
+#define _XOPEN_SOURCE
+#include <features.h>
+
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
 
 #include "putty.h"
 
@@ -10,6 +16,8 @@
 #define TRUE 1
 #endif
 
+int pty_master_fd;
+
 static void pty_size(void);
 
 static void c_write(char *buf, int len)
@@ -27,7 +35,74 @@ static void c_write(char *buf, int len)
  */
 static char *pty_init(char *host, int port, char **realhost, int nodelay)
 {
-    /* FIXME: do nothing for now */
+    int slavefd;
+    char name[FILENAME_MAX];
+    pid_t pid;
+
+    pty_master_fd = open("/dev/ptmx", O_RDWR);
+
+    if (pty_master_fd < 0) {
+       perror("/dev/ptmx: open");
+       exit(1);
+    }
+
+    if (grantpt(pty_master_fd) < 0) {
+       perror("grantpt");
+       exit(1);
+    }
+    
+    if (unlockpt(pty_master_fd) < 0) {
+       perror("unlockpt");
+       exit(1);
+    }
+
+    name[FILENAME_MAX-1] = '\0';
+    strncpy(name, ptsname(pty_master_fd), FILENAME_MAX-1);
+
+    slavefd = open(name, O_RDWR);
+    if (slavefd < 0) {
+       perror("slave pty: open");
+       return 1;
+    }
+
+    /*
+     * Fork and execute the command.
+     */
+    pid = fork();
+    if (pid < 0) {
+       perror("fork");
+       return 1;
+    }
+
+    if (pid == 0) {
+       int i;
+       /*
+        * We are the child.
+        */
+       close(pty_master_fd);
+       close(0);
+       close(1);
+       close(2);
+       fcntl(slavefd, F_SETFD, 0);    /* don't close on exec */
+       dup2(slavefd, 0);
+       dup2(slavefd, 1);
+       dup2(slavefd, 2);
+       setsid();
+       setpgrp();
+       tcsetpgrp(0, getpgrp());
+       /* Close everything _else_, for tidiness. */
+       for (i = 3; i < 1024; i++)
+           close(i);
+       execl(getenv("SHELL"), getenv("SHELL"), NULL);
+       /*
+        * If we're here, exec has gone badly foom.
+        */
+       perror("exec");
+       exit(127);
+    } else {
+       close(slavefd);
+    }
+
     return NULL;
 }
 
@@ -36,7 +111,15 @@ static char *pty_init(char *host, int port, char **realhost, int nodelay)
  */
 static int pty_send(char *buf, int len)
 {
-    c_write(buf, len);                /* FIXME: diagnostic thingy */
+    while (len > 0) {
+       int ret = write(pty_master_fd, buf, len);
+       if (ret < 0) {
+           perror("write pty master");
+           exit(1);
+       }
+       buf += ret;
+       len -= ret;
+    }
     return 0;
 }