Index: cpukit/telnetd/pty.c
===================================================================
--- cpukit/telnetd/pty.c	(revision 8ad6681b6bb601c670726ca36c924f5852d01efd)
+++ cpukit/telnetd/pty.c	(revision b7fa289bf7badb15dc02ba0ca4d93050779b16ba)
@@ -1,6 +1,6 @@
 /*
- * /dev/ptyXX  (A first version for pseudo-terminals)
+ * /dev/ptyXX  (Support for pseudo-terminals)
  *
- *  Author: Fernando RUIZ CASAS (fernando.ruiz@ctv.es)
+ *  Original Author: Fernando RUIZ CASAS (fernando.ruiz@ctv.es)
  *  May 2001
  *
@@ -50,6 +50,6 @@
 #endif
 
-#define DEBUG_WH		(1<<0)
-#define DEBUG_DETAIL	(1<<1)
+#define DEBUG_WH    (1<<0)
+#define DEBUG_DETAIL  (1<<1)
 
 /* #define DEBUG DEBUG_WH */
@@ -64,11 +64,5 @@
 #include <rtems.h>
 #include <rtems/libio.h>
-#include <bsp.h>
 #include <rtems/bspIo.h>
-#if 0
-#include <rtems/pty.h>
-#else
-#define MAX_PTYS 8
-#endif
 #include <errno.h>
 #include <sys/socket.h>
@@ -100,5 +94,7 @@
 #define IAC_EOR    239
 
-#define SB_MAX		16
+#define SB_MAX    16
+
+extern int rtems_telnetd_maximum_ptys;
 
 struct pty_tt;
@@ -113,5 +109,5 @@
  int                       last_cr;
  unsigned                  iac_mode;   
- unsigned char             sb_buf[SB_MAX];	
+ unsigned char             sb_buf[SB_MAX];  
  int                       sb_ind;
  int                       width;
@@ -121,18 +117,10 @@
 
 #ifdef __cplusplus
-
 extern "C" {
-int		printk(char*,...);
-
-#endif
-
-#if     MAX_PTYS > 5
-#undef  MAX_PTYS
-#define MAX_PTYS 5
-#endif
-
-
-static int   telnet_pty_inited=FALSE;
-static pty_t telnet_ptys[MAX_PTYS];
+#endif
+
+
+static int    telnet_pty_inited=FALSE;
+static pty_t *telnet_ptys;
 
 static rtems_device_major_number pty_major;
@@ -144,8 +132,18 @@
  */
 
-char *  telnet_get_pty(int socket) {
+char *  telnet_get_pty(int socket)
+{
   int ndx;
+
   if (telnet_pty_inited) {
-    for (ndx=0;ndx<MAX_PTYS;ndx++) {
+    if ( rtems_telnetd_maximum_ptys < 5 )
+      rtems_telnetd_maximum_ptys = 5;
+
+    telnet_ptys = malloc( rtems_telnetd_maximum_ptys * sizeof (pty_t) );
+    if ( !telnet_ptys ) {
+      return NULL;
+    }
+      
+    for (ndx=0;ndx<rtems_telnetd_maximum_ptys;ndx++) {
       if (telnet_ptys[ndx].socket<0) {
         struct timeval t;
@@ -175,8 +173,9 @@
 static const char IAC_IP_RSP []="<*Interrupt*>";
 
-
 static
-int send_iac(int minor,unsigned char mode,unsigned char option) {
+int send_iac(int minor,unsigned char mode,unsigned char option)
+{
   unsigned char buf[3];
+
   buf[0]=IAC_ESC;
   buf[1]=mode;
@@ -189,5 +188,5 @@
 {
   switch (pty->sb_buf[0]) {
-    case 31:	/* NAWS */
+    case 31:  /* NAWS */
       pty->width  = (pty->sb_buf[1]<<8) + pty->sb_buf[2];
       pty->height = (pty->sb_buf[3]<<8) + pty->sb_buf[4];
@@ -205,10 +204,11 @@
 }
 
-static int read_pty(int minor) { /* Characters written to the client side*/
-   unsigned char	value;
-   unsigned int	omod;
-   int			count;
-   int			result;
-   pty_t			*pty=telnet_ptys+minor;
+static int read_pty(int minor)
+{ /* Characters written to the client side*/
+   unsigned char  value;
+   unsigned int  omod;
+   int      count;
+   int      result;
+   pty_t      *pty=telnet_ptys+minor;
 
    count=read(pty->socket,&value,sizeof(value));
@@ -293,8 +293,8 @@
            pty->iac_mode=omod;
            if (IAC_ESC==value) {
-            pty->iac_mode=(omod<<8)|value;
+             pty->iac_mode=(omod<<8)|value;
            } else {
-            if (pty->sb_ind < SB_MAX)
-              pty->sb_buf[pty->sb_ind++]=value;
+             if (pty->sb_ind < SB_MAX)
+               pty->sb_buf[pty->sb_ind++]=value;
            }
            return -1;
@@ -302,8 +302,8 @@
        case IAC_WILL:
            if (value==34){
-              send_iac(minor,IAC_DONT,   34);	/*LINEMODE*/ 
-              send_iac(minor,IAC_DO  ,    1);	/*ECHO    */
+              send_iac(minor,IAC_DONT,   34);  /*LINEMODE*/ 
+              send_iac(minor,IAC_DO  ,    1);  /*ECHO    */
            } else if (value==31) {
-              send_iac(minor,IAC_DO  ,   31);	/*NAWS	  */
+              send_iac(minor,IAC_DO  ,   31);  /*NAWS    */
 #if DEBUG & DEBUG_DETAIL
               printk("replied DO NAWS\n");
@@ -317,6 +317,6 @@
        case IAC_DO  :
            if (value==3) {
-              send_iac(minor,IAC_WILL,    3);	/* GO AHEAD*/
-           } else	if (value==1) {                         
+              send_iac(minor,IAC_WILL,    3);  /* GO AHEAD*/
+           } else  if (value==1) {                         
               /* ECHO */
            } else {
@@ -325,6 +325,9 @@
            return -1;
        case IAC_WONT:
-           if (value==1) {send_iac(minor,IAC_WILL,    1);} else /* ECHO */
-           {send_iac(minor,IAC_WONT,value);};
+           if (value==1) {
+             send_iac(minor,IAC_WILL,    1);
+           } else { /* ECHO */
+             send_iac(minor,IAC_WONT,value);
+           }
            return -1;
        default:
@@ -335,6 +338,6 @@
               result=value;  
               if ( 0
-#if 0							 /* pass CRLF through - they should use termios to handle it */
-                 ||	((value=='\n') && (pty->last_cr))
+#if 0               /* pass CRLF through - they should use termios to handle it */
+                 ||  ((value=='\n') && (pty->last_cr))
 #endif
                 /* but map telnet CRNUL to CR down here */
@@ -361,6 +364,6 @@
 static int
 ptySetAttributes(int minor,const struct termios *t) {
-  if (minor<MAX_PTYS) {	
-   telnet_ptys[minor].c_cflag=t->c_cflag;	
+  if (minor<rtems_telnetd_maximum_ptys) {
+   telnet_ptys[minor].c_cflag=t->c_cflag;
   } else {
    return -1;
@@ -373,6 +376,6 @@
   rtems_libio_open_close_args_t * args = (rtems_libio_open_close_args_t*)arg;
   struct termios t;
-        if (minor<MAX_PTYS) {	
-         if (telnet_ptys[minor].socket<0) return -1;		
+        if (minor<rtems_telnetd_maximum_ptys) {
+         if (telnet_ptys[minor].socket<0) return -1;
    telnet_ptys[minor].opened=TRUE;
    telnet_ptys[minor].ttyp= (struct rtems_termios_tty *) args->iop->data1;
@@ -385,17 +388,17 @@
   } else {
    return -1;
-  };
+  }
 }
 /*-----------------------------------------------------------*/
 static int 
 ptyShutdown(int major,int minor,void * arg) {
-        if (minor<MAX_PTYS) {	
-   telnet_ptys[minor].opened=FALSE;
-         if (telnet_ptys[minor].socket>=0) close(telnet_ptys[minor].socket);
-   telnet_ptys[minor].socket=-1;
-   chown(telnet_ptys[minor].devname,2,0);
+  if (minor<rtems_telnetd_maximum_ptys) {
+    telnet_ptys[minor].opened=FALSE;
+    if (telnet_ptys[minor].socket>=0) close(telnet_ptys[minor].socket);
+      telnet_ptys[minor].socket=-1;
+    chown(telnet_ptys[minor].devname,2,0);
   } else {
-   return -1;
-  };
+    return -1;
+  }
   return 0;
 }
@@ -406,10 +409,11 @@
 ptyPollWrite(int minor, const char * buf,int len) {
   int count;
-        if (minor<MAX_PTYS) {	
-         if (telnet_ptys[minor].socket<0) return -1;		
-   count=write(telnet_ptys[minor].socket,buf,len);
+  if (minor<rtems_telnetd_maximum_ptys) {
+    if (telnet_ptys[minor].socket<0)
+      return -1;
+    count=write(telnet_ptys[minor].socket,buf,len);
   } else {
    count=-1;
-  };
+  }
   return count;
 }
@@ -418,9 +422,11 @@
 ptyPollRead(int minor) {
   int result;
-        if (minor<MAX_PTYS) {	
-         if (telnet_ptys[minor].socket<0) return -1;		
-   result=read_pty(minor);
-   return result;
-  };
+
+  if (minor<rtems_telnetd_maximum_ptys) {
+    if (telnet_ptys[minor].socket<0)
+      return -1;
+    result=read_pty(minor);
+    return result;
+  }
   return -1;
 }
@@ -444,5 +450,5 @@
 )
 {
-int ndx;	
+int ndx;
 rtems_status_code status ;
 
@@ -451,5 +457,5 @@
    */
 
-  for (ndx=0;ndx<MAX_PTYS;ndx++) {
+  for (ndx=0;ndx<rtems_telnetd_maximum_ptys;ndx++) {
     telnet_ptys[ndx].devname=(char*)malloc(strlen("/dev/ptyXX")+1);
     sprintf(telnet_ptys[ndx].devname,"/dev/pty%X",ndx);
@@ -462,10 +468,10 @@
     telnet_ptys[ndx].height=0;
 
-  };
+  }
 
   /*
    * Register the devices
    */
-  for (ndx=0;ndx<MAX_PTYS;ndx++) {
+  for (ndx=0;ndx<rtems_telnetd_maximum_ptys;ndx++) {
     status = rtems_io_register_name(telnet_ptys[ndx].devname, major, ndx);
     if (status != RTEMS_SUCCESSFUL)
@@ -474,5 +480,6 @@
     chown(telnet_ptys[ndx].devname,2,0); /* tty,root*/
   };
-  printk("Device: /dev/pty%X../dev/pty%X (%d)pseudo-terminals registered.\n",0,MAX_PTYS-1,MAX_PTYS);
+  printk("Device: /dev/pty%X../dev/pty%X (%d)pseudo-terminals registered.\n",
+          0,rtems_telnetd_maximum_ptys-1,rtems_telnetd_maximum_ptys);
 
   return RTEMS_SUCCESSFUL;
@@ -487,7 +494,8 @@
       return 0;
 
-    for (ndx=0;ndx<MAX_PTYS;ndx++) {
+    for (ndx=0;ndx<rtems_telnetd_maximum_ptys;ndx++) {
       if (telnet_ptys[ndx].opened) {
-          fprintf(stderr,"There are still opened PTY devices, unable to proceed\n");
+          fprintf(stderr,
+            "There are still opened PTY devices, unable to proceed\n");
           return -1;
       }
@@ -497,5 +505,5 @@
         return -1;
     }
-    for (ndx=0;ndx<MAX_PTYS;ndx++) {
+    for (ndx=0;ndx<rtems_telnetd_maximum_ptys;ndx++) {
         /* rtems_io_register_name() actually creates a node in the filesystem
          * (mknod())
@@ -507,4 +515,7 @@
           free(telnet_ptys[ndx].devname);
     };
+
+    free ( telnet_ptys );
+
     fprintf(stderr,"PTY driver unloaded successfully\n");
     telnet_pty_inited=FALSE;
@@ -523,5 +534,5 @@
 )
 {
-  rtems_status_code sc ;
+  rtems_status_code sc;
   sc = rtems_termios_open(major,minor,arg,pty_get_termios_handlers(FALSE));
   return sc;
@@ -581,7 +592,7 @@
 )
 {
-rtems_libio_ioctl_args_t *args = (rtems_libio_ioctl_args_t*)arg;
-struct winsize			 *wp = (struct winsize*)args->buffer;
-pty_t					 *p=&telnet_ptys[minor];
+  rtems_libio_ioctl_args_t *args = (rtems_libio_ioctl_args_t*)arg;
+  struct winsize           *wp = (struct winsize*)args->buffer;
+  pty_t                    *p = &telnet_ptys[minor];
 
   switch (args->command) {
@@ -617,5 +628,5 @@
     default:
 
-     	break;
+     break;
   }
 
@@ -634,12 +645,12 @@
 /*-----------------------------------------------------------*/
 static const rtems_termios_callbacks pty_poll_callbacks = {
-  ptyPollInitialize,	/* FirstOpen*/
-  ptyShutdown,		/* LastClose*/
-  ptyPollRead,		/* PollRead  */
-  ptyPollWrite,		/* Write */
-  ptySetAttributes,	/* setAttributes */
-  NULL,			/* stopRemoteTX */
-  NULL,			/* StartRemoteTX */
-  0 			/* outputUsesInterrupts */
+  ptyPollInitialize,  /* FirstOpen */
+  ptyShutdown,        /* LastClose */
+  ptyPollRead,        /* PollRead  */
+  ptyPollWrite,       /* Write */
+  ptySetAttributes,   /* setAttributes */
+  NULL,               /* stopRemoteTX */
+  NULL,               /* StartRemoteTX */
+  0                   /* outputUsesInterrupts */
 };
 /*-----------------------------------------------------------*/
@@ -664,18 +675,21 @@
 class TelnetPtyIni {
 public:
-  TelnetPtyIni() { if (!nest++) {
-            pty_do_initialize();
-        }
-       };
-  ~TelnetPtyIni(){ if (!--nest) {
-            pty_do_finalize();
-        }
-       };
+  TelnetPtyIni() {
+    if (!nest++) {
+      pty_do_initialize();
+    }
+  }
+
+  ~TelnetPtyIni() {
+    if (!--nest) {
+      pty_do_finalize();
+    }
+  }
 private:
-static int nest;
+  static int nest;
 };
 
 static TelnetPtyIni onlyInst;
-int TelnetPtyIni::nest=0;
+int    TelnetPtyIni::nest=0;
 
 int telnet_pty_initialize()
