From dfd761058e5b187c6ec9e8695cd93d64799ccd69 Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Mon, 7 Dec 2015 09:44:06 +0000 Subject: 37310: effect of disabling typeset reserved word --- Doc/Zsh/builtins.yo | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'Doc/Zsh/builtins.yo') diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index 49806e4d8..120ec8260 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -1812,7 +1812,10 @@ this means. Note that each interface to any of the commands my be disabled separately. For example, `tt(disable -r typeset)' disables the reserved word interface to tt(typeset), exposing the builtin interface, while -`tt(disable typeset)' disables the builtin. +`tt(disable typeset)' disables the builtin. Note that disabling the +reserved word interface for tt(typeset) may cause problems with the +output of `tt(typeset -p)', which assumes the reserved word interface is +available in order to restore array and associative array values. If the shell option tt(TYPESET_SILENT) is not set, for each remaining var(name) that refers to a parameter that is already set, the name and -- cgit v1.2.3 From 15b73ea99b7e156dc9281d89a68fea8fae76706b Mon Sep 17 00:00:00 2001 From: "Barton E. Schaefer" Date: Thu, 31 Dec 2015 12:38:10 -0800 Subject: 37467: add "print -v var" / "printf -v var" --- ChangeLog | 5 +++++ Doc/Zsh/builtins.yo | 10 ++++++++-- Src/builtin.c | 34 +++++++++++++++++++++++++--------- Test/B03print.ztst | 9 +++++++++ 4 files changed, 47 insertions(+), 11 deletions(-) (limited to 'Doc/Zsh/builtins.yo') diff --git a/ChangeLog b/ChangeLog index 9fde9c7ff..eaf4f080a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2015-12-31 Barton E. Schaefer + + * 37467: Doc/Zsh/builtins.yo, Src/builtin.c, Test/B03print.ztst: + add "print -v var" / "printf -v var" + 2015-12-31 Oliver Kiddle * 37453 (with Bart, started by Baptiste Daroussin, 37315) diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index 120ec8260..dc0b947a6 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -1124,7 +1124,7 @@ tt(popd) that do not change the environment seen by an interactive user. ) findex(print) xitem(tt(print )[ tt(-abcDilmnNoOpPrsSz) ] [ tt(-u) var(n) ] [ tt(-f) var(format) ] [ tt(-C) var(cols) ]) -item(SPACES()[ tt(-xX) var(tab-stop) ] [ tt(-R) [ tt(-en) ]] [ var(arg) ... ])( +item(SPACES()[ tt(-v) var(name) ] [ tt(-xX) var(tabstop) ] [ tt(-R) [ tt(-en) ]] [ var(arg) ... ])( With the `tt(-f)' option the arguments are printed as described by tt(printf). With no flags or with the flag `tt(-)', the arguments are printed on the standard output as described by tt(echo), with the following differences: @@ -1219,6 +1219,9 @@ tt(HIST_LEX_WORDS) option active. item(tt(-u) var(n))( Print the arguments to file descriptor var(n). ) +item(tt(-v) var(name))( +Store the printed arguments as the value of the parameter var(name). +) item(tt(-x) var(tab-stop))( Expand leading tabs on each line of output in the printed string assuming a tab stop every var(tab-stop) characters. This is appropriate @@ -1250,7 +1253,7 @@ If any of `tt(-m)', `tt(-o)' or `tt(-O)' are used in combination with case of `tt(-m)') then nothing is printed. ) findex(printf) -item(tt(printf) var(format) [ var(arg) ... ])( +item(tt(printf) [ -v var(name) ] var(format) [ var(arg) ... ])( Print the arguments according to the format specification. Formatting rules are the same as used in C. The same escape sequences as for tt(echo) are recognised in the format. All C conversion specifications ending in @@ -1279,6 +1282,9 @@ until all arguments have been consumed. With the tt(print) builtin, this can be suppressed by using the tt(-r) option. If more arguments are required by the format than have been specified, the behaviour is as if zero or an empty string had been specified as the argument. + +The tt(-v) option causes the output to be stored as the value of the +parameter var(name), instead of printed. ) findex(pushd) pindex(PUSHD_TO_HOME, use of) diff --git a/Src/builtin.c b/Src/builtin.c index b06bc6de7..128bc36b5 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -99,8 +99,8 @@ static struct builtin builtins[] = #endif BUILTIN("popd", BINF_SKIPINVALID | BINF_SKIPDASH | BINF_DASHDASHVALID, bin_cd, 0, 1, BIN_POPD, "q", NULL), - BUILTIN("print", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, "abcC:Df:ilmnNoOpPrRsSu:x:X:z-", NULL), - BUILTIN("printf", 0, bin_print, 1, -1, BIN_PRINTF, NULL, NULL), + BUILTIN("print", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, "abcC:Df:ilmnNoOpPrRsSu:v:x:X:z-", NULL), + BUILTIN("printf", 0, bin_print, 1, -1, BIN_PRINTF, "v:", NULL), BUILTIN("pushd", BINF_SKIPINVALID | BINF_SKIPDASH | BINF_DASHDASHVALID, bin_cd, 0, 2, BIN_PUSHD, "qsPL", NULL), BUILTIN("pushln", 0, bin_print, 0, -1, BIN_PRINT, NULL, "-nz"), BUILTIN("pwd", 0, bin_pwd, 0, 0, 0, "rLP", NULL), @@ -4032,6 +4032,11 @@ bin_print(char *name, char **args, Options ops, int func) zulong zulongval; char *stringval; + if (OPT_ISSET(ops, 'z') + OPT_ISSET(ops, 's') + OPT_ISSET(ops, 'v') > 1) { + zwarnnam(name, "only one of -z, -s, or -v allowed"); + return 1; + } + if (func == BIN_PRINTF) { if (!strcmp(*args, "--") && !*++args) { zwarnnam(name, "not enough arguments"); @@ -4157,8 +4162,8 @@ bin_print(char *name, char **args, Options ops, int func) if ((OPT_HASARG(ops,'u') || OPT_ISSET(ops,'p')) && /* rule out conflicting options -- historical precedence */ ((!fmt && (OPT_ISSET(ops,'c') || OPT_ISSET(ops,'C'))) || - !(OPT_ISSET(ops, 'z') || - OPT_ISSET(ops, 's') || OPT_ISSET(ops, 'S')))) { + !(OPT_ISSET(ops, 'z') || OPT_ISSET(ops, 'v') || + OPT_ISSET(ops, 's') || OPT_ISSET(ops, 'S')))) { int fdarg, fd; if (OPT_ISSET(ops, 'p')) { @@ -4359,7 +4364,8 @@ bin_print(char *name, char **args, Options ops, int func) /* normal output */ if (!fmt) { - if (OPT_ISSET(ops, 'z') || OPT_ISSET(ops, 's')) { + if (OPT_ISSET(ops, 'z') || OPT_ISSET(ops, 's') || + OPT_ISSET(ops, 'v')) { /* * We don't want the arguments unmetafied after all. */ @@ -4367,6 +4373,13 @@ bin_print(char *name, char **args, Options ops, int func) metafy(args[n], len[n], META_NOALLOC); } + /* -v option -- store the arguments in the named parameter */ + if (OPT_ISSET(ops,'v')) { + queue_signals(); + assignsparam(OPT_ARG(ops, 'v'), sepjoin(args, NULL, 0), 0); + unqueue_signals(); + return 0; + } /* -z option -- push the arguments onto the editing buffer stack */ if (OPT_ISSET(ops,'z')) { queue_signals(); @@ -4474,7 +4487,7 @@ bin_print(char *name, char **args, Options ops, int func) * special cases of printing to a ZLE buffer or the history, however. */ - if (OPT_ISSET(ops,'z') || OPT_ISSET(ops,'s')) { + if (OPT_ISSET(ops,'z') || OPT_ISSET(ops,'s') || OPT_ISSET(ops, 'v')) { #ifdef HAVE_OPEN_MEMSTREAM if ((fout = open_memstream(&buf, &mcount)) == NULL) zwarnnam(name, "open_memstream failed"); @@ -4853,7 +4866,7 @@ bin_print(char *name, char **args, Options ops, int func) /* if there are remaining args, reuse format string */ } while (*argp && argp != first && !fmttrunc && !OPT_ISSET(ops,'r')); - if (OPT_ISSET(ops,'z') || OPT_ISSET(ops,'s')) { + if (OPT_ISSET(ops,'z') || OPT_ISSET(ops,'s') || OPT_ISSET(ops,'v')) { #ifdef HAVE_OPEN_MEMSTREAM putc(0, fout); fclose(fout); @@ -4865,11 +4878,14 @@ bin_print(char *name, char **args, Options ops, int func) buf[count] = '\0'; #endif queue_signals(); + stringval = metafy(buf, -1, META_REALLOC); if (OPT_ISSET(ops,'z')) { - zpushnode(bufstack, buf); + zpushnode(bufstack, stringval); + } else if (OPT_ISSET(ops,'v')) { + assignsparam(OPT_ARG(ops, 'v'), stringval, 0); } else { ent = prepnexthistent(); - ent->node.nam = buf; + ent->node.nam = stringval; ent->stim = ent->ftim = time(NULL); ent->node.flags = 0; ent->words = (short *)NULL; diff --git a/Test/B03print.ztst b/Test/B03print.ztst index eb79c4ddb..2e9bf99e5 100644 --- a/Test/B03print.ztst +++ b/Test/B03print.ztst @@ -301,3 +301,12 @@ >one two three four > one two three four > one two three four + + unset foo + print -v foo once more + print -r -- $foo + printf -v foo "%s-" into the breach + print -r -- $foo +0:print and printf into a variable +>once more +>into-the-breach- -- cgit v1.2.3 From 03adf524140dce211372124895fa4356376329a8 Mon Sep 17 00:00:00 2001 From: "Barton E. Schaefer" Date: Sat, 2 Jan 2016 12:40:31 -0800 Subject: 37493: readonly + POSIX_BUILTINS == typeset -gr --- ChangeLog | 5 +++++ Doc/Zsh/builtins.yo | 5 ++++- Src/builtin.c | 8 ++++++-- Src/hashtable.h | 2 +- Test/B02typeset.ztst | 8 ++++---- 5 files changed, 20 insertions(+), 8 deletions(-) (limited to 'Doc/Zsh/builtins.yo') diff --git a/ChangeLog b/ChangeLog index b89c9d307..afd7a514c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2016-01-02 Barton E. Schaefer + + * 37493: Doc/Zsh/builtins.yo, Src/builtin.c, Src/hashtable.h, + Test/B02typeset.ztst: readonly + POSIX_BUILTINS == typeset -gr + 2016-01-01 Barton E. Schaefer * 37483: Src/glob.c: save and possibly restore cshnullglob failure diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index dc0b947a6..fb630a713 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -1465,7 +1465,10 @@ cancels both tt(-p) and tt(-u). The tt(-c) or tt(-l) flags cancel any and all of tt(-kpquz). ) cindex(parameters, marking readonly) -alias(readonly)(typeset -r) +item(tt(readonly))( +Same as tt(typeset -r). With the tt(POSIX_BUILTINS) option set, same +as tt(typeset -gr). +) alias(rehash)(hash -r) findex(return) cindex(functions, returning from) diff --git a/Src/builtin.c b/Src/builtin.c index 05907f1d3..557487c87 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -62,7 +62,7 @@ static struct builtin builtins[] = BUILTIN("enable", 0, bin_enable, 0, -1, BIN_ENABLE, "afmprs", NULL), BUILTIN("eval", BINF_PSPECIAL, bin_eval, 0, -1, BIN_EVAL, NULL, NULL), BUILTIN("exit", BINF_PSPECIAL, bin_break, 0, 1, BIN_EXIT, NULL, NULL), - BUILTIN("export", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, BIN_EXPORT, "E:%F:%HL:%R:%TUZ:%afhi:%lprtu", "xg"), + BUILTIN("export", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "E:%F:%HL:%R:%TUZ:%afhi:%lprtu", "xg"), BUILTIN("false", 0, bin_false, 0, -1, 0, NULL, NULL), /* * We used to behave as if the argument to -e was optional. @@ -106,7 +106,7 @@ static struct builtin builtins[] = BUILTIN("pwd", 0, bin_pwd, 0, 0, 0, "rLP", NULL), BUILTIN("r", 0, bin_fc, 0, -1, BIN_R, "IlLnr", NULL), BUILTIN("read", 0, bin_read, 0, -1, 0, "cd:ek:%lnpqrst:%zu:AE", NULL), - BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%lptux", "r"), + BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL | BINF_ASSIGN, (HandlerFunc)bin_typeset, 0, -1, BIN_READONLY, "AE:%F:%HL:%R:%TUZ:%afghi:%lptux", "r"), BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "df", "r"), BUILTIN("return", BINF_PSPECIAL, bin_break, 0, 1, BIN_RETURN, NULL, NULL), BUILTIN("set", BINF_PSPECIAL | BINF_HANDLES_OPTS, bin_set, 0, -1, 0, NULL, NULL), @@ -2533,6 +2533,10 @@ bin_typeset(char *name, char **argv, LinkList assigns, Options ops, int func) if (OPT_ISSET(ops,'f')) return bin_functions(name, argv, ops, func); + /* POSIX handles "readonly" specially */ + if (func == BIN_READONLY && isset(POSIXBUILTINS) && !OPT_PLUS(ops, 'g')) + ops->ind['g'] = 1; + /* Translate the options into PM_* flags. * * Unfortunately, this depends on the order * * these flags are defined in zsh.h */ diff --git a/Src/hashtable.h b/Src/hashtable.h index b6346bb9a..3606e9785 100644 --- a/Src/hashtable.h +++ b/Src/hashtable.h @@ -53,7 +53,7 @@ #define BIN_LOGOUT 19 #define BIN_TEST 20 #define BIN_BRACKET 21 -#define BIN_EXPORT 22 +#define BIN_READONLY 22 #define BIN_ECHO 23 #define BIN_DISABLE 24 #define BIN_ENABLE 25 diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst index 7d65cc8a7..681fe73c7 100644 --- a/Test/B02typeset.ztst +++ b/Test/B02typeset.ztst @@ -479,12 +479,12 @@ setopt POSIXBUILTINS readonly pbro print ${+pbro} >&2 - (typeset pbro=3) + (typeset -g pbro=3) (pbro=4) - readonly -p | grep pbro >&2 # shows up as "readonly" although unset - typeset -r pbro # idempotent (no error)... + readonly -p pbro >&2 # shows up as "readonly" although unset + typeset -gr pbro # idempotent (no error)... print ${+pbro} >&2 # ...so still readonly... - typeset +r pbro # ...can't turn it off + typeset -g +r pbro # ...can't turn it off ) 1:readonly with POSIX_BUILTINS ?0 -- cgit v1.2.3