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

PATCH: read builtin cleanup



This patch does some cleanup in the read builtin and adds proper handling
of backslashes which should escape whithespaces and backslashes.

Zoli


*** Src/builtin.c	1997/06/16 05:11:38	3.1.3.1
--- Src/builtin.c	1997/07/13 05:21:43	3.1.3.2
*************** int
*** 2718,2724 ****
  bin_read(char *name, char **args, char *ops, int func)
  {
      char *reply, *readpmpt;
!     int bsiz, c = 0, gotnl = 0, al = 0, first, nchars = 1;
      int haso = 0;	/* true if /dev/tty has been opened specially */
      int isem = !strcmp(term, "emacs");
      char *buf, *bptr, *firstarg, *zbuforig;
--- 2718,2724 ----
  bin_read(char *name, char **args, char *ops, int func)
  {
      char *reply, *readpmpt;
!     int bsiz, c = 0, gotnl = 0, al = 0, first, nchars = 1, bslash;
      int haso = 0;	/* true if /dev/tty has been opened specially */
      int isem = !strcmp(term, "emacs");
      char *buf, *bptr, *firstarg, *zbuforig;
*************** bin_read(char *name, char **args, char *
*** 2767,2793 ****
      } else if (ops['u'] && !ops['p']) {
  	/* -u means take input from the specified file descriptor. *
  	 * -up means take input from the coprocess.                */
! 	for (readfd = 0; readfd < 10; ++readfd)
! 	    if (ops[readfd + '0'])
! 		break;
! 	if (readfd == 10)
! 	    readfd = 0;
      } else if (ops['p'])
  	readfd = coprocin;
!     else {
! 	/* last resort: take input from plain old stdin */
! 	attachtty((jobtab[thisjob].gleader) ? jobtab[thisjob].gleader : mypgrp);
  	readfd = 0;
! #if 0
! 	  else if (isset(SHINSTDIN) && unset(INTERACTIVE)) {
! 	    /* use stdout or stderr, if either is a tty */
! 	    if (isatty(1))
! 		readfd = 1;
! 	    else if (isatty(2))
! 		readfd = 2;
! 	}
! #endif
!     }
      /* handle prompt */
      if (firstarg) {
  	for (readpmpt = firstarg;
--- 2767,2778 ----
      } else if (ops['u'] && !ops['p']) {
  	/* -u means take input from the specified file descriptor. *
  	 * -up means take input from the coprocess.                */
! 	for (readfd = 9; readfd && !ops[readfd + '0']; --readfd);
      } else if (ops['p'])
  	readfd = coprocin;
!     else
  	readfd = 0;
! 
      /* handle prompt */
      if (firstarg) {
  	for (readpmpt = firstarg;
*************** bin_read(char *name, char **args, char *
*** 2874,2895 ****
      zbuforig = zbuf = (!ops['z']) ? NULL :
  	(nonempty(bufstack)) ? (char *) getlinknode(bufstack) : ztrdup("");
      first = 1;
      while (*args || (ops['A'] && !gotnl)) {
  	buf = bptr = (char *)zalloc(bsiz = 64);
  	/* get input, a character at a time */
! 	for (;;) {
! 	    if (gotnl)
! 		break;
  	    c = zread();
  	    /* \ at the end of a line indicates a continuation *
  	     * line, except in raw mode (-r option)            */
! 	    if (!ops['r'] && c == '\n' && bptr != buf && bptr[-1] == '\\') {
! 		bptr--;
  		continue;
  	    }
  	    if (c == EOF || c == '\n')
  		break;
! 	    if (isep(c)) {
  		if (bptr != buf || (!iwsep(c) && first)) {
  		    first |= !iwsep(c);
  		    break;
--- 2859,2879 ----
      zbuforig = zbuf = (!ops['z']) ? NULL :
  	(nonempty(bufstack)) ? (char *) getlinknode(bufstack) : ztrdup("");
      first = 1;
+     bslash = 0;
      while (*args || (ops['A'] && !gotnl)) {
  	buf = bptr = (char *)zalloc(bsiz = 64);
  	/* get input, a character at a time */
! 	while (!gotnl) {
  	    c = zread();
  	    /* \ at the end of a line indicates a continuation *
  	     * line, except in raw mode (-r option)            */
! 	    if (bslash && c == '\n') {
! 		bslash = 0;
  		continue;
  	    }
  	    if (c == EOF || c == '\n')
  		break;
! 	    if (!bslash && isep(c)) {
  		if (bptr != buf || (!iwsep(c) && first)) {
  		    first |= !iwsep(c);
  		    break;
*************** bin_read(char *name, char **args, char *
*** 2897,2902 ****
--- 2881,2889 ----
  		first |= !iwsep(c);
  		continue;
  	    }
+ 	    bslash = c == '\\' && !bslash && !ops['r'];
+ 	    if (bslash)
+ 		continue;
  	    first = 0;
  	    if (imeta(c)) {
  		*bptr++ = Meta;
*************** bin_read(char *name, char **args, char *
*** 2964,2987 ****
      }
      buf = bptr = (char *)zalloc(bsiz = 64);
      /* any remaining part of the line goes into one parameter */
      if (!gotnl)
  	for (;;) {
  	    c = zread();
  	    /* \ at the end of a line introduces a continuation line, except in
  	    raw mode (-r option) */
! 	    if (!ops['r'] && c == '\n' && bptr != buf && bptr[-1] == '\\') {
! 		bptr--;
  		continue;
  	    }
  	    if (c == EOF || (c == '\n' && !zbuf))
  		break;
! 	    if (isep(c) && bptr == buf)
  		if (iwsep(c))
  		    continue;
  		else if (!first) {
  		    first = 1;
  		    continue;
  		}
  	    if (imeta(c)) {
  		*bptr++ = Meta;
  		*bptr++ = c ^ 32;
--- 2951,2978 ----
      }
      buf = bptr = (char *)zalloc(bsiz = 64);
      /* any remaining part of the line goes into one parameter */
+     bslash = 0;
      if (!gotnl)
  	for (;;) {
  	    c = zread();
  	    /* \ at the end of a line introduces a continuation line, except in
  	    raw mode (-r option) */
! 	    if (bslash && c == '\n') {
! 		bslash = 0;
  		continue;
  	    }
  	    if (c == EOF || (c == '\n' && !zbuf))
  		break;
! 	    if (!bslash && isep(c) && bptr == buf)
  		if (iwsep(c))
  		    continue;
  		else if (!first) {
  		    first = 1;
  		    continue;
  		}
+ 	    bslash = c == '\\' && !bslash && !ops['r'];
+ 	    if (bslash)
+ 		continue;
  	    if (imeta(c)) {
  		*bptr++ = Meta;
  		*bptr++ = c ^ 32;



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