diff -u gexpect_tty.c.old gexpect_tty.c

Lengyel Sándor <[email protected]> Wed, 1 Dec 2004 19:44:18 +0100
Newsgroups gmane.comp.ide.gps.devel
Message-ID <[email protected]>
Here it is:
-- 
 Lengyel Sándor

Hass, alkoss, gyarapíts,
S a haza fényre derül. (Kölcsey)

_______________________________________________
gps-devel mailing list
[email protected]
http://lists.adacore.com/mailman/listinfo/gps-devel
gexpect_tty.c.diff (text/x-diff, 3.8 KB)
--- gexpect_tty.c.bak	2004-11-27 19:57:43.000000000 +0100
+++ gexpect_tty.c	2004-12-01 16:38:31.000000000 +0100
@@ -23,6 +23,9 @@
 
 */
 
+/* The code got modified again by renaming allocate_pty to allocate_pty_the_old_fashioned_way and adding almost unchanged allocate_pty from xemacs.
+Change by Sandor Lengyel who does not know what he is doing. :-) */
+
 /* Include the system-specific definitions */
 #include SYSTEM_INCLUDE
 
@@ -111,6 +114,8 @@
 #define INTEGERP(x) 1
 #define XINT(x) x
 
+static int allocate_pty_the_old_fashioned_way (void);
+
 /* Should use ptys or pipes to communicate with the processes ?
    0 = pipe
    1 = tty
@@ -788,20 +793,135 @@
 }
 #endif /* HAVE_PTYS */
 
+#ifdef HAVE_PTYS
 
 /*********************************************************
  **  allocate_pty ()
  **  Open an available pty, returning a file descriptor.
+ **  Finds in the case of unix by checking devices
+ ** /dev/ptmx /dev/ptm/clone /dev/ptc and /dev/ptmx_bsd
+ **  On failure it tries allocate_pty_the_old_fashioned_way
+ **  Return -1 for final failure.
+ **  The file name of the terminal corresponding to the pty
+ **  is left in the variable pty_name.
+ **
+ *********************************************************/
+
+
+static int
+allocate_pty (void)
+{
+
+  int master_fd = -1;
+  const char *slave_name = ((void *)0);
+  const char *clone = ((void *)0);
+  static const char * const clones[] =
+    {
+      "/dev/ptmx",
+      "/dev/ptm/clone",
+      "/dev/ptc",
+      "/dev/ptmx_bsd"
+    };
+
+
+  master_fd = getpt ();
+  if (master_fd >= 0)
+    goto have_master;
+
+
+  {
+    int slave_fd = -1;
+    int rc;
+    do { sigset_t ES_mask; sigemptyset (&ES_mask); sigaddset (&ES_mask, 17); sigprocmask (0, &ES_mask, ((void *)0)); } while (0);
+    rc = openpty (&master_fd, &slave_fd, ((void *)0), ((void *)0), ((void *)0));
+    do { sigset_t ES_mask; sigemptyset (&ES_mask); sigaddset (&ES_mask, 17); sigprocmask (1, &ES_mask, ((void *)0)); } while (0);
+    if (rc == 0)
+      {
+ slave_name = ttyname (slave_fd);
+ close (slave_fd);
+ goto have_slave_name;
+      }
+    else
+      {
+ if (master_fd >= 0)
+   close (master_fd);
+ if (slave_fd >= 0)
+   close (slave_fd);
+      }
+  }
+
+  {
+    int i;
+    for (i = 0; i < ((int) (sizeof(clones)/sizeof((clones)[0]))); i++)
+      {
+ clone = clones[i];
+ master_fd = open (clone, 02 | 04000 | (0), 0);
+ if (master_fd >= 0)
+   goto have_master;
+      }
+    clone = ((void *)0);
+  }
+
+  goto lose;
+
+ have_master:
+
+
+  slave_name = ptsname (master_fd);
+  if (slave_name)
+    goto have_slave_name;
+
+
+
+  if (clone
+      && !strcmp (clone, "/dev/ptc")
+      && (slave_name = ttyname (master_fd)) != ((void *)0))
+    goto have_slave_name;
+
+  goto lose;
+
+ have_slave_name:
+  strncpy (pty_name, slave_name, sizeof (pty_name));
+  pty_name[sizeof (pty_name) - 1] = '\0';
+  setup_pty (master_fd);
+
+  do { sigset_t ES_mask; sigemptyset (&ES_mask); sigaddset (&ES_mask, 17); sigprocmask (0, &ES_mask, ((void *)0)); } while (0);
+
+
+  grantpt (master_fd);
+
+  unlockpt (master_fd);
+
+
+  do { sigset_t ES_mask; sigemptyset (&ES_mask); sigaddset (&ES_mask, 17); sigprocmask (1, &ES_mask, ((void *)0)); } while (0);
+
+
+  return master_fd;
+
+ lose:
+  if (master_fd >= 0)
+    close (master_fd);
+
+  return allocate_pty_the_old_fashioned_way ();
+}
+
+
+
+
+/*********************************************************
+ **  allocate_pty_the_old_fashioned_way ()
+ **  Open an available pty, returning a file descriptor.
+ **  Finds in the case of unix the bsd type ptys by search.
  **  Return -1 on failure.
  **  The file name of the terminal corresponding to the pty
  **  is left in the variable pty_name.
  **
  *********************************************************/
 
-#ifdef HAVE_PTYS
+
 
 static int
-allocate_pty ()
+allocate_pty_the_old_fashioned_way ()
 {
   struct stat stb;
   register int c, i;