Subject: Re: [ANNOUNCEMENT] Updated: cygwin-1.5.12-1
From: "Ehud Karni" <[email-masked]>
To: [email-masked]
Date: Mon, 15 Nov 2004 15:11:37 +0200

On Thu, 11 Nov 2004, Christopher Faylor wrote:

> I've made a new version of the Cygwin DLL and associated utilities
> available for download.  As usual, a list of what has changed is below.
[snip]
> - Use proper wait value for tty reads.  (Mark Paulus)

Thanks VERY, VERY MUCH.

I've waited for this fix for 2 years now (see my post "Possible bug
in non blocking read from /dev/tty" from 2002-11-20:
    http://sources.redhat.com/ml/cygwin/2002-11/msg01106.html ).

Today, after upgrading to cygwin-1.5.12-1 I checked this again and
it WORKED ! Thank you Mark Paulus !

Bellow is a program that use this feature to execute a command every
<N> seconds and exist after ANY keyboard input.

Ehud.

/* cmd_loop:   execute command in loop, stop on any input

       RCS: $Id: cmd_loop.c,v 1.101 1998/03/17 17:06:09 ehud Exp $

       $Log: cmd_loop.c,v $
       Revision 1.101  1998/03/17  17:06:09  ehud
       Allow \xFD (replace by space) in command name

       Revision 1.100  1997/12/16 18:20:36  ehud
       Initial RCS version - Tested

command call:  cmd_loop <command> [<sleep time>]
       default sleep time is 15 seconds.
       the program stops on any input.
                                                       Ehud  Karni 16/12/97

*/

#include <stdio.h>                         /* standard I/O library */
#include <stdlib.h>                        /* standard lib - ttyname */
#include <unistd.h>                        /* standard UNIX operations */
#include <termios.h>                       /* for reading long lines */

static const char *RCS_WHAT = "<@(#) $Id: cmd_loop.c,v 1.101 1998/03/17 17:06:09 ehud Exp $ >" ;
                                           /* RCS identification for what */

static FILE *devtty = NULL ;               /* stream for terminal I/O - read only */
char *TTY = "/dev/tty" ;                   /* terminal device path */
int slp = 15 ;                             /* sleep seconds (default set) */
int vtime_val = 10 ;                       /* time to wait when there is no input (in 1/10 sec) */
char rdbuf [ 10 ] = "\xFE\xFE\xFE\xFE" ;   /* tty check buffer */

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
void ttyread ( char *lni, int length ) ;   /* This sub reads 1 line */
/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */

int main ( int argc, char *argv[] )
{
int i , tslp , dif = 3 ;

   if ( argc > 2 )                         /* is there 2nd arg ? */
   {
       i = atoi ( argv [ 2 ] ) % 2000 ;    /* sleep time (mod 2000) */
       if ( i > 0 )
           slp = i ;                       /* set sleep seconds */
       if ( i > 499 )
           dif = 5 ;                       /* allow 1/2 second diff for long waits */
   }

   tslp = slp * 10 ;                       /* use the read for wait (in 1/10 seconds) */
   slp = tslp / 251 ;                      /* sleep counter ( - 1 ) */

   do
   {
       slp ++ ;                            /* sleep counter */
       i = tslp / slp ;                    /* vtime value (1/10 seconds) */
   }   while ( ( tslp - i * slp ) > dif ) ;/* diff must be less then 1/2 second */

   vtime_val = i ;                         /* set vtime in tty.io */

   if ( ( devtty = fopen ( TTY , "r+" ) ) == NULL )    /* open (terminal ?) for read */
       devtty = stdin ;                                /* failed ? ==> use stdin instead */

   i = 0 ;                                 /* i is loop checker */
   while ( rdbuf [0] == '\xFE' )           /* exit on char read */
   {
       if ( i == 0 )
       {
           system ( argv [ 1 ] ) ;         /* execute command - 1st arg */
           i = slp ;                       /* each read takes 1/2 sec */
       }
       i-- ;                               /* execute counter */
       ttyread ( rdbuf , 2 ) ;             /* read max of 2 chars / wait up to vtime_val / 10 seconds */
   }
   return ( 0 ) ;                          /* return with exit code 0 (OK) */
}
/*=========================================================================*/

void ttyread ( char *lni, int length )     /*  This sub reads 1 line until
                                               new-line or EOF */
{
struct termios ttyarg, intrm ;             /* arguments for ioctl / termios */
int ftty, n ;                              /* devtty file number, temp n */

   fflush ( devtty ) ;                     /* must flush before read */

   ftty = fileno ( devtty ) ;              /* file number of devtty */
   tcgetattr( ftty , &intrm ) ;            /* get stdin arguments (SAVE) */
   ttyarg = intrm ;                        /* saved original configuration */

   ttyarg.c_cc [VMIN] = 0 ;                /* min of 0 chars */
   ttyarg.c_cc [VTIME] = vtime_val ;       /* wait max vtime_val/10 sec (def 15/10) */
   ttyarg.c_iflag = IGNBRK | IGNPAR | ICRNL | IMAXBEL ;    /* set these modes only on input */
   ttyarg.c_lflag = 0 ;                    /* clear lflag of all processing */

   n = tcsetattr( ftty , TCSANOW , &ttyarg ) ; /* set changed tty arguments */

   lni [ length ] = 0 ;                    /* last pos must be Z byte */
   n = read ( ftty , lni , length ) ;      /* read devtty, max n chars, up to vtime_val / 10 sec wait */
   tcsetattr( ftty , TCSANOW , & intrm ) ; /* reset devtty to saved value */

   if ( n < 0 )                            /* error ? */
       perror ( "error in read tty " ) ;   /* message to stderr */

}
/*=========================================================================*/

--
 Ehud Karni           Tel: +972-3-7966-561  /"\
 Mivtach - Simon      Fax: +972-3-7966-667  \ /  ASCII Ribbon Campaign
 Insurance agencies   (USA) voice mail and   X   Against   HTML   Mail
 http://www.mvs.co.il  FAX:  1-815-5509341  / \
 GnuPG: 98EA398D <http://www.keyserver.net/>    Better Safe Than Sorry

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/