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

Prompt escape sequences.



I wanted to add a new escape sequence %${varname} to print the
content of a variable. Looking at putpromptchar, I found some
bugs...

print -P %2147483648v
	does a segmentation fault because 2147483648 is translated
	to a negative number by zstrtol.
	The patch below corrects this bug.
print -P %-%7-
	outputs '%-%-' instead of '%-%7-'.
	I did not correct this behaviour.
	It may be easy to do.
print -P 'a\0b'
	outputs 'a', since \0 ends the string.
	"print 'a\0b'" does output a null character.
	I did not correct this behaviour.
	We may need to rewrite the whole function.


*** Src/zle_misc.c.orig	Tue Aug 13 22:24:14 1996
--- Src/zle_misc.c	Tue Oct  1 19:05:11 1996
***************
*** 859,865 ****
  putpromptchar(int doprint, int endchar)
  {
      char buf3[PATH_MAX], *ss;
!     int t0, arg, test, sep;
      struct tm *tm;
      time_t timet;
      Nameddir nd;
--- 859,866 ----
  putpromptchar(int doprint, int endchar)
  {
      char buf3[PATH_MAX], *ss;
!     int t0, test, sep;
!     unsigned int arg;
      struct tm *tm;
      time_t timet;
      Nameddir nd;
***************
*** 894,909 ****
  		case '.':
  		case '~':
  		    if ((nd = finddir(ss))) {
! 			arg--;
  			ss += strlen(nd->dir);
  		    }
  		case '/':
  		case 'C':
  		    for (; *ss; ss++)
  			if (*ss == '/')
! 			    arg--;
! 		    if (arg <= 0)
! 			test = 1;
  		    break;
  		case 't':
  		case 'T':
--- 895,909 ----
  		case '.':
  		case '~':
  		    if ((nd = finddir(ss))) {
! 			test++;
  			ss += strlen(nd->dir);
  		    }
  		case '/':
  		case 'C':
  		    for (; *ss; ss++)
  			if (*ss == '/')
! 			    test++;
! 		    test = (arg <= test);
  		    break;
  		case 't':
  		case 'T':
***************
*** 1230,1235 ****
--- 1230,1262 ----
  		addbufspc(1);
  		*bp++ = (geteuid())? '%' : '#';
  		break;
+ 	    case '$':
+ 		if (fm[1] != '{') {
+ 		    *bp++ = '%';
+ 		    *bp++ = '$';
+ 		    break;
+ 		}
+ 		else {
+ 		    char **array, *variable, endname;
+ 		    fm++; variable = fm+1;
+                     while (*fm && *fm != '}') fm++;
+                     /* We don't need to allocate something for the var name */
+                     endname = *fm;
+                     *fm = NULL;
+                     if (arg) {
+ 		        array = getaparam(variable);
+ 		        variable = NULL;
+                         if ( array && (arrlen(array) >= arg) )
+ 		            variable = array[arg-1];
+                     }
+                     else
+ 		      variable = getsparam(variable);
+ 		    stradd(variable ? variable : "");
+                     *fm = endname;
+ 		}
+ 		if(!*fm)
+ 		    return 0;
+ 		break;
  	    case 'v':
  		if (!arg)
  		    arg = 1;
***************
*** 1241,1247 ****
  		break;
  	    case '_':
  		if (cmdsp) {
! 		    if (arg > cmdsp || arg <= 0)
  			arg = cmdsp;
  		    for (t0 = cmdsp - arg; arg--; t0++) {
  			stradd(cmdnames[cmdstack[t0]]);
--- 1268,1274 ----
  		break;
  	    case '_':
  		if (cmdsp) {
! 		    if (arg > cmdsp || !arg)
  			arg = cmdsp;
  		    for (t0 = cmdsp - arg; arg--; t0++) {
  			stradd(cmdnames[cmdstack[t0]]);
*** Doc/zshparam.man.orig	Tue Aug 13 22:24:13 1996
--- Doc/zshparam.man	Tue Oct  1 19:05:08 1996
***************
*** 579,584 ****
--- 579,589 ----
  The value of the first element of the $psvar array parameter.  Following
  the '%' with an integer gives that element of the array.
  .TP
+ .B %${\fIvarname\fB}
+ The value of the variable $varname.
+ Following the '%' with an integer gives that element of the array.
+ Using the $psvar array is faster.
+ .TP
  \fB%{\fP...\fB%}\fP
  Include a string as a literal escape sequence.
  The string within the braces should not change the cursor
*** Doc/zsh.texi.orig	Thu Aug 15 18:47:39 1996
--- Doc/zsh.texi	Tue Oct  1 19:09:00 1996
***************
*** 4144,4149 ****
--- 4144,4154 ----
  Following the @code{%} with an integer gives that element of the
  array.
  
+ @item %$@{@var{varname}@}
+ The value of the variable @code{varname}.
+ Following the @code{%} with an integer gives that element of the array.
+ Using the $psvar array is faster.
+ 
  @item %@{@dots{}%@}
  Include a string as a literal escape sequence.  The string within the
  braces should not change the cursor position.



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