$NetBSD: patch-ae,v 1.2 2005/12/03 17:16:52 joerg Exp $

--- src/serial.c.orig	1999-06-28 22:12:29.000000000 +0000
+++ src/serial.c
@@ -69,7 +69,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <sys/time.h>
-#if defined(LINUX) || defined(HPUX)
+#if defined(LINUX) || defined(HPUX) || defined(CSRG_BASED)
 #include <sys/ioctl.h>
 #endif
 #include <unistd.h>
@@ -77,6 +77,8 @@
 #ifdef SOLARIS
 #include <sys/stream.h>
 #include <sys/stropts.h>
+#endif
+#if defined(SOLARIS) || defined(CSRG_BASED)
 #include <sys/termios.h>
 #endif
 
@@ -129,6 +131,9 @@ serial_init()
 #else
   int   c;
 #endif
+#if defined(CSRG_BASED)
+  char *ptr1, *ptr2;
+#endif
   int   n;
   char  tty_dev_name[128];
   struct termios ttybuf;
@@ -200,6 +205,51 @@ serial_init()
         }
       while ((wire_fd < 0) && (errno != ENOENT));
 #else  /* LINUX */
+#if defined(CSRG_BASED)
+      c = c; /* XXX gcc */
+/* This is how Stevens does it in:
+ * Advanced programming in the UNIX environment
+ */
+      strcpy(tty_dev_name, "/dev/ptyXY");
+        /* array index: 0123456789 (for references in following code) */
+      for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++)
+        {
+          tty_dev_name[8] = *ptr1;
+          for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++)
+            {
+              tty_dev_name[9] = *ptr2;
+
+              /* try open the master */
+              if ((wire_fd = open(tty_dev_name, O_RDWR, 0666)) < 0)
+                {
+                  if (errno == ENOENT)
+                    {
+                      wire_fd = -1;              /* out of pty devices */
+                      ttyp = -1;
+                      goto break_for_pty;
+                    }
+                  else
+                    continue;                    /* try next pty device */
+                }
+
+              tty_dev_name[5] = 't';   /* change pty to tty */
+              if ((ttyp = open(tty_dev_name, O_RDWR, 0666)) < 0)
+                {
+                  printf("open tty: %s failed: %s, trying with another\n",
+                          tty_dev_name, strerror(errno));
+                  close(wire_fd);
+                  wire_fd = -1;
+                  ttyp = -1;
+                  continue;
+                }
+              if (verbose)
+                printf("%s: wire connection on %s\n", progname, tty_dev_name);
+              wire_name = strdup(tty_dev_name);
+              goto break_for_pty;
+            }
+        }
+break_for_pty:
+#else
       /*
        * Here we go for SUNOS, HPUX
        */
@@ -231,14 +281,16 @@ serial_init()
           c++;
         }
       while ((wire_fd < 0) && (errno != ENOENT));
+#endif /* CSRG_BASED */
 #endif /* LINUX */
 #endif /* SOLARIS */
 #endif /* IRIX */
+	;
     }
 
   if (ttyp >= 0)
     {
-#if defined(SUNOS) || defined(HPUX)
+#if defined(SUNOS) || defined(HPUX) || defined(CSRG_BASED)
       if (tcgetattr(ttyp, &ttybuf) < 0)
 #else
       if (ioctl(ttyp, TCGETS, (char *)&ttybuf) < 0)
@@ -263,7 +315,7 @@ serial_init()
 
   if (ttyp >= 0)
     {
-#if defined(SUNOS) || defined (HPUX)
+#if defined(SUNOS) || defined (HPUX) || defined(CSRG_BASED)
       if (tcsetattr(ttyp, TCSANOW, &ttybuf) < 0)
 #else
       if (ioctl(ttyp, TCSETS, (char *)&ttybuf) < 0)
@@ -291,7 +343,7 @@ serial_init()
 
   if (ir_fd >= 0)
     {
-#if defined(SUNOS) || defined (HPUX)
+#if defined(SUNOS) || defined (HPUX) || defined(CSRG_BASED)
       if (tcgetattr(ir_fd, &ttybuf) < 0)
 #else
       if (ioctl(ir_fd, TCGETS, (char *)&ttybuf) < 0)
@@ -315,7 +367,7 @@ serial_init()
 
   if (ir_fd >= 0)
     {
-#if defined(SUNOS) || defined(HPUX)
+#if defined(SUNOS) || defined(HPUX) || defined(CSRG_BASED)
       if (tcsetattr(ir_fd, TCSANOW, &ttybuf) < 0)
 #else
       if (ioctl(ir_fd, TCSETS, (char *)&ttybuf) < 0)
@@ -344,7 +396,7 @@ int baud;
 
   if (ir_fd >= 0)
     {
-#if defined(SUNOS) || defined (HPUX)
+#if defined(SUNOS) || defined (HPUX) || defined(CSRG_BASED)
       if (tcgetattr(ir_fd, &ttybuf) < 0)
 #else
       if (ioctl(ir_fd, TCGETS, (char *)&ttybuf) < 0)
@@ -358,6 +410,10 @@ int baud;
         }
     }
 
+#if defined(CSRG_BASED)
+  cfsetspeed(&ttybuf, baud);
+#else
+
   ttybuf.c_cflag &= ~CBAUD;
 
   baud &= 0x7;
@@ -404,9 +460,11 @@ int baud;
       ttybuf.c_cflag |= B9600;
     }
 
+#endif /* CSRG_BASED */
+
   if (ir_fd >= 0)
     {
-#if defined(SUNOS) || defined(HPUX)
+#if defined(SUNOS) || defined(HPUX) || defined(CSRG_BASED)
       if (tcsetattr(ir_fd, TCSANOW, &ttybuf) < 0)
 #else
       if (ioctl(ir_fd, TCSETS, (char *)&ttybuf) < 0)
@@ -422,7 +480,7 @@ int baud;
 
   if (ttyp >= 0)
     {
-#if defined(SUNOS) || defined(HPUX)
+#if defined(SUNOS) || defined(HPUX) || defined(CSRG_BASED)
       if (tcgetattr(ttyp, &ttybuf) < 0)
 #else
       if (ioctl(ttyp, TCGETS, (char *)&ttybuf) < 0)
@@ -437,6 +495,9 @@ int baud;
         }
     }
 
+#if defined(CSRG_BASED)
+  cfsetspeed(&ttybuf, baud);
+#else
   ttybuf.c_cflag &= ~CBAUD;
 
   baud &= 0x7;
@@ -482,10 +543,11 @@ int baud;
         fprintf(stderr, "%s: can\'t set baud rate, using 9600\n", progname);
       ttybuf.c_cflag |= B9600;
     }
+#endif
 
   if (ttyp >= 0)
     {
-#if defined(SUNOS) || defined(HPUX)
+#if defined(SUNOS) || defined(HPUX) || defined(CSRG_BASED)
       if (tcsetattr(ttyp, TCSANOW, &ttybuf) < 0)
 #else
       if (ioctl(ttyp, TCSETS, (char *)&ttybuf) < 0)
