zsh-workers
 help / color / mirror / code / Atom feed
* Prompt escape sequences.
@ 1996-10-01 17:11 Louis Granboulan
  1996-10-02 23:18 ` Zoltan Hidvegi
  0 siblings, 1 reply; 5+ messages in thread
From: Louis Granboulan @ 1996-10-01 17:11 UTC (permalink / raw)
  To: zsh-workers

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.


^ permalink raw reply	[flat|nested] 5+ messages in thread
[parent not found: <20071008034223.GA789@primenet.com.au>]

end of thread, other threads:[~2007-10-09  3:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-10-01 17:11 Prompt escape sequences Louis Granboulan
1996-10-02 23:18 ` Zoltan Hidvegi
1996-10-03  6:09   ` Richard Coleman
     [not found] <20071008034223.GA789@primenet.com.au>
     [not found] ` <071007210217.ZM24374@torch.brasslantern.com>
     [not found]   ` <20071008042759.GA2413@primenet.com.au>
2007-10-08 15:44     ` PROMPT " Bart Schaefer
2007-10-09  3:23       ` Geoff Wing

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).