Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: set blocking read on stdin



This patch sets blocking read on stdin when the shell reads commands from
there or when it is used by the read builtin.  This is required by POSIX.

Zoli


*** Src/builtin.c.orig	Fri May  8 01:04:12 1998
--- Src/builtin.c	Fri May  8 01:15:15 1998
*************** bin_read(char *name, char **args, char *
*** 3175,3181 ****
  static int
  zread(void)
  {
!     char cc;
  
      /* use zbuf if possible */
      if (zbuf)
--- 3175,3181 ----
  static int
  zread(void)
  {
!     char cc, retry = 0;
  
      /* use zbuf if possible */
      if (zbuf)
*************** zread(void)
*** 3186,3197 ****
  	    return zbuf++, STOUC(*zbuf++ ^ 32);
  	else
  	    return (*zbuf) ? STOUC(*zbuf++) : EOF;
!     /* read a character from readfd */
!     if (read(readfd, &cc, 1) != 1)
! 	/* on EOF, return EOF */
  	return EOF;
!     /* return the character read */
!     return STOUC(cc);
  }
  
  /* holds arguments for testlex() */
--- 3186,3207 ----
  	    return zbuf++, STOUC(*zbuf++ ^ 32);
  	else
  	    return (*zbuf) ? STOUC(*zbuf++) : EOF;
!     for (;;) {
! 	/* read a character from readfd */
! 	switch (read(readfd, &cc, 1)) {
! 	case 1:
! 	    /* return the character read */
! 	    return STOUC(cc);
! 	case -1:
! 	    if (!retry && errno == EWOULDBLOCK &&
! 		readfd == 0 && setblock_stdin()) {
! 		retry = 1;
! 		continue;
! 	    }
! 	    break;
! 	}
  	return EOF;
!     }
  }
  
  /* holds arguments for testlex() */
*** Src/init.c.orig	Sat May  2 03:45:37 1998
--- Src/init.c	Fri May  8 01:15:15 1998
*************** loop(int toplevel, int justonce)
*** 88,95 ****
      for (;;) {
  	freeheap();
  	errflag = 0;
! 	if (interact && isset(SHINSTDIN))
! 	    preprompt();
  	hbegin();		/* init history mech        */
  	intr();			/* interrupts on            */
  	lexinit();              /* initialize lexical state */
--- 88,98 ----
      for (;;) {
  	freeheap();
  	errflag = 0;
! 	if (isset(SHINSTDIN)) {
! 	    setblock_stdin();
! 	    if (interact)
! 		preprompt();
! 	}
  	hbegin();		/* init history mech        */
  	intr();			/* interrupts on            */
  	lexinit();              /* initialize lexical state */
*** Src/utils.c.orig	Sat May  2 03:47:05 1998
--- Src/utils.c	Fri May  8 01:18:58 1998
*************** zstrtol(const char *s, char **t, int bas
*** 1076,1081 ****
--- 1076,1099 ----
  
  /**/
  int
+ setblock_stdin(void)
+ {
+ #ifdef O_NONBLOCK
+     struct stat st;
+     long mode;
+ 
+     if (!fstat(0, &st) && !S_ISREG(st.st_mode)) {
+ 	mode = fcntl(0, F_GETFL);
+ 	if (mode != -1 && (mode & O_NONBLOCK) &&
+ 	    !fcntl(0, F_SETFL, mode & ~O_NONBLOCK))
+ 	    return 1;
+     }
+ #endif
+     return 0;
+ }
+ 
+ /**/
+ int
  checkrmall(char *s)
  {
      fprintf(shout, "zsh: sure you want to delete all the files in ");



Messages sorted by: Reverse Date, Date, Thread, Author