From 0628802baf5c9245138db82dd058cad023a7d0ae Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Thu, 29 Oct 2015 15:01:07 +0000 Subject: 37014: Improved internal parameter setting. Enhance WARNCREATEGLOBAL to work in many more cases. Don't create REPLY as an integer if it didn't previously exist as one, even if the value to be set is integral, as this is likely to mess up later uses of REPLY. --- Src/builtin.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'Src/builtin.c') diff --git a/Src/builtin.c b/Src/builtin.c index 97022addf..8045bc8f7 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -2090,7 +2090,9 @@ typeset_single(char *cname, char *pname, Param pm, UNUSED(int func), tc = 0; /* but don't do a normal conversion */ } } else if (!setsecondstype(pm, on, off)) { - if (asg->value.scalar && !(pm = setsparam(pname, ztrdup(asg->value.scalar)))) + if (asg->value.scalar && + !(pm = assignsparam( + pname, ztrdup(asg->value.scalar), 0))) return NULL; usepm = 1; err = 0; @@ -2202,12 +2204,13 @@ typeset_single(char *cname, char *pname, Param pm, UNUSED(int func), } else if (pm->env && !(pm->node.flags & PM_HASHELEM)) delenv(pm); DPUTS(ASG_ARRAYP(asg), "BUG: typeset got array value where scalar expected"); - if (asg->value.scalar && !(pm = setsparam(pname, ztrdup(asg->value.scalar)))) + if (asg->value.scalar && + !(pm = assignsparam(pname, ztrdup(asg->value.scalar), 0))) return NULL; } else if (asg->is_array) { - if (!(pm = setaparam(pname, asg->value.array ? + if (!(pm = assignaparam(pname, asg->value.array ? zlinklist2array(asg->value.array) : - mkarray(NULL)))) + mkarray(NULL), 0))) return NULL; } pm->node.flags |= (on & PM_READONLY); @@ -2347,16 +2350,18 @@ typeset_single(char *cname, char *pname, Param pm, UNUSED(int func), * creating a stray parameter along the way via createparam(), * now called below in the isident() branch. */ - if (!(pm = setsparam(pname, ztrdup(asg->value.scalar ? asg->value.scalar : "")))) + if (!(pm = assignsparam( + pname, + ztrdup(asg->value.scalar ? asg->value.scalar : ""), 0))) return NULL; dont_set = 1; asg->is_array = 0; keeplocal = 0; on = pm->node.flags; } else if (PM_TYPE(on) == PM_ARRAY && ASG_ARRAYP(asg)) { - if (!(pm = setaparam(pname, asg->value.array ? - zlinklist2array(asg->value.array) : - mkarray(NULL)))) + if (!(pm = assignaparam(pname, asg->value.array ? + zlinklist2array(asg->value.array) : + mkarray(NULL), 0))) return NULL; dont_set = 1; keeplocal = 0; @@ -2433,13 +2438,13 @@ typeset_single(char *cname, char *pname, Param pm, UNUSED(int func), Param ipm = pm; if (pm->node.flags & (PM_ARRAY|PM_HASHED)) { DPUTS(!ASG_ARRAYP(asg), "BUG: inconsistent scalar value for array"); - if (!(pm=setaparam(pname, asg->value.array ? - zlinklist2array(asg->value.array) : - mkarray(NULL)))) + if (!(pm=assignaparam(pname, asg->value.array ? + zlinklist2array(asg->value.array) : + mkarray(NULL), 0))) return NULL; } else { DPUTS(ASG_ARRAYP(asg), "BUG: inconsistent array value for scalar"); - if (!(pm = setsparam(pname, ztrdup(asg->value.scalar)))) + if (!(pm = assignsparam(pname, ztrdup(asg->value.scalar), 0))) return NULL; } if (pm != ipm) { @@ -2687,9 +2692,10 @@ bin_typeset(char *name, char **argv, LinkList assigns, Options ops, int func) /* Update join character */ tdp->joinchar = joinchar; if (asg0.value.scalar) - setsparam(asg0.name, ztrdup(asg0.value.scalar)); + assignsparam(asg0.name, ztrdup(asg0.value.scalar), 0); else if (asg->value.array) - setaparam(asg->name, zlinklist2array(asg->value.array)); + assignaparam( + asg->name, zlinklist2array(asg->value.array), 0); return 0; } else { zwarnnam(name, "can't tie already tied scalar: %s", @@ -2750,9 +2756,9 @@ bin_typeset(char *name, char **argv, LinkList assigns, Options ops, int func) zsfree(apm->ename); apm->ename = ztrdup(asg0.name); if (asg->value.array) - setaparam(asg->name, zlinklist2array(asg->value.array)); + assignaparam(asg->name, zlinklist2array(asg->value.array), 0); else if (oldval) - setsparam(asg0.name, oldval); + assignsparam(asg0.name, oldval, 0); unqueue_signals(); return 0; -- cgit v1.2.3 From ba36967b575ca57ebde3349e62c190ca69b73832 Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Sun, 1 Nov 2015 18:12:40 +0000 Subject: 37038: add -l option to emulate to list options in emulations --- ChangeLog | 5 +++++ Doc/Zsh/builtins.yo | 10 +++++++++- Src/builtin.c | 33 +++++++++++++++++++++++++-------- Src/options.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 9 deletions(-) (limited to 'Src/builtin.c') diff --git a/ChangeLog b/ChangeLog index 20d848320..13f262bea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2015-11-01 Peter Stephenson + + * 37038: Doc/Zsh/builtins.yo, Src/builtin.c, Src/options.c: add + -l option to emulate. + 2015-10-31 Oliver Kiddle * 37029: Completion/Unix/Command/_date, diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index 85d1742f0..985e7267f 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -477,7 +477,7 @@ cindex(compatibility, csh) cindex(sh, compatibility) cindex(ksh, compatibility) cindex(csh, compatibility) -item(tt(emulate) [ tt(-LR) ] [ {tt(zsh)|tt(sh)|tt(ksh)|tt(csh)} [ var(flags) ... ] ])( +item(tt(emulate) [ tt(-lLR) ] [ {tt(zsh)|tt(sh)|tt(ksh)|tt(csh)} [ var(flags) ... ] ])( Without any argument print current emulation mode. With single argument set up zsh options to emulate the specified shell @@ -518,6 +518,14 @@ function, if any; normally these options are turned off in all emulation modes except tt(ksh). The tt(-L) switch is mutually exclusive with the use of tt(-c) in var(flags). +If there is a single argument and the tt(-l) switch is given, the +options that would be set or unset (the latter indicated with the prefix +`tt(no)') are listed. tt(-l) can be combined with tt(-L) or tt(-R) and +the list will be modified in the appropriate way. Note the list does +not depend on the current setting of options, i.e. it includes all +options that may in principle change, not just those that would actually +change. + The var(flags) may be any of the invocation-time flags described in ifnzman(noderef(Invocation))\ ifzman(the section INVOCATION in zmanref(zsh)), diff --git a/Src/builtin.c b/Src/builtin.c index 8045bc8f7..0be20a5d2 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -58,7 +58,7 @@ static struct builtin builtins[] = BUILTIN("disable", 0, bin_enable, 0, -1, BIN_DISABLE, "afmprs", NULL), BUILTIN("disown", 0, bin_fg, 0, -1, BIN_DISOWN, NULL, NULL), BUILTIN("echo", BINF_SKIPINVALID, bin_print, 0, -1, BIN_ECHO, "neE", "-"), - BUILTIN("emulate", 0, bin_emulate, 0, -1, 0, "LR", NULL), + BUILTIN("emulate", 0, bin_emulate, 0, -1, 0, "lLR", NULL), 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), @@ -5425,10 +5425,11 @@ eval(char **argv) /**/ int -bin_emulate(UNUSED(char *nam), char **argv, Options ops, UNUSED(int func)) +bin_emulate(char *nam, char **argv, Options ops, UNUSED(int func)) { int opt_L = OPT_ISSET(ops, 'L'); int opt_R = OPT_ISSET(ops, 'R'); + int opt_l = OPT_ISSET(ops, 'l'); int saveemulation, savehackchar; int ret = 1, new_emulation; unsigned int savepatterns; @@ -5443,7 +5444,7 @@ bin_emulate(UNUSED(char *nam), char **argv, Options ops, UNUSED(int func)) /* without arguments just print current emulation */ if (!shname) { if (opt_L || opt_R) { - zwarnnam("emulate", "not enough arguments"); + zwarnnam(nam, "not enough arguments"); return 1; } @@ -5471,27 +5472,43 @@ bin_emulate(UNUSED(char *nam), char **argv, Options ops, UNUSED(int func)) /* with single argument set current emulation */ if (!argv[1]) { - emulate(shname, opt_R, &emulation, opts); + char *cmdopts; + if (opt_l) { + cmdopts = (char *)zhalloc(OPT_SIZE); + memcpy(cmdopts, opts, OPT_SIZE); + } else + cmdopts = opts; + emulate(shname, opt_R, &emulation, cmdopts); if (opt_L) - opts[LOCALOPTIONS] = opts[LOCALTRAPS] = opts[LOCALPATTERNS] = 1; + cmdopts[LOCALOPTIONS] = cmdopts[LOCALTRAPS] = + cmdopts[LOCALPATTERNS] = 1; + if (opt_l) { + list_emulate_options(cmdopts, opt_R); + return 0; + } clearpatterndisables(); return 0; } + if (opt_l) { + zwarnnam(nam, "too many arguments for -l"); + return 1; + } + argv++; memcpy(saveopts, opts, sizeof(opts)); memcpy(new_opts, opts, sizeof(opts)); savehackchar = keyboardhackchar; emulate(shname, opt_R, &new_emulation, new_opts); optlist = newlinklist(); - if (parseopts("emulate", &argv, new_opts, &cmd, optlist)) { + if (parseopts(nam, &argv, new_opts, &cmd, optlist)) { ret = 1; goto restore; } /* parseopts() has consumed anything that looks like an option */ if (*argv) { - zwarnnam("emulate", "unknown argument %s", *argv); + zwarnnam(nam, "unknown argument %s", *argv); goto restore; } @@ -5510,7 +5527,7 @@ bin_emulate(UNUSED(char *nam), char **argv, Options ops, UNUSED(int func)) */ if (cmd) { if (opt_L) { - zwarnnam("emulate", "option -L incompatible with -c"); + zwarnnam(nam, "option -L incompatible with -c"); goto restore2; } *--argv = cmd; /* on stack, never free()d, see execbuiltin() */ diff --git a/Src/options.c b/Src/options.c index 3bf9f39a4..2678626c7 100644 --- a/Src/options.c +++ b/Src/options.c @@ -902,3 +902,33 @@ printoptionlist_printequiv(int optno) optno *= (isneg ? -1 : 1); printf(" equivalent to --%s%s\n", isneg ? "no-" : "", optns[optno-1].node.nam); } + +/**/ +static char *print_emulate_opts; + +/**/ +static void +print_emulate_option(HashNode hn, int fully) +{ + Optname on = (Optname) hn; + + if (!(on->node.flags & OPT_ALIAS) && + ((fully && !(on->node.flags & OPT_SPECIAL)) || + (on->node.flags & OPT_EMULATE))) + { + if (!print_emulate_opts[on->optno]) + fputs("no", stdout); + puts(on->node.nam); + } +} + +/* + * List the settings of options associated with an emulation + */ + +/**/ +void list_emulate_options(char *cmdopts, int fully) +{ + print_emulate_opts = cmdopts; + scanhashtable(optiontab, 1, 0, 0, print_emulate_option, fully); +} -- cgit v1.2.3 From 30b90f166ef0ca0883985461d498c066e4f723da Mon Sep 17 00:00:00 2001 From: "Barton E. Schaefer" Date: Sun, 8 Nov 2015 12:44:31 -0800 Subject: 37080: use paramtab abstraction more consistently, add explanatory comments --- ChangeLog | 3 +++ Src/builtin.c | 11 +++++++---- Src/params.c | 6 ++++-- 3 files changed, 14 insertions(+), 6 deletions(-) (limited to 'Src/builtin.c') diff --git a/ChangeLog b/ChangeLog index cee715d33..110f10743 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2015-11-08 Barton E. Schaefer + * 37080: Src/builtin.c, Src/params.c: use paramtab abstraction more + consistently, add explanatory comments + * 37079: Etc/zsh-development-guide: better description of "optstr" field in struct builtin as used in BUILTIN() macro diff --git a/Src/builtin.c b/Src/builtin.c index 0be20a5d2..18dfdce05 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -2334,7 +2334,8 @@ typeset_single(char *cname, char *pname, Param pm, UNUSED(int func), } else if ((on & PM_LOCAL) && locallevel) { *subscript = 0; pm = (Param) (paramtab == realparamtab ? - gethashnode2(paramtab, pname) : + /* getnode2() to avoid autoloading */ + paramtab->getnode2(paramtab, pname) : paramtab->getnode(paramtab, pname)); *subscript = '['; if (!pm || pm->level != locallevel) { @@ -2825,11 +2826,12 @@ bin_typeset(char *name, char **argv, LinkList assigns, Options ops, int func) /* Take arguments literally. Don't glob */ while ((asg = getasg(&argv, assigns))) { HashNode hn = (paramtab == realparamtab ? - gethashnode2(paramtab, asg->name) : + /* getnode2() to avoid autoloading */ + paramtab->getnode2(paramtab, asg->name) : paramtab->getnode(paramtab, asg->name)); if (OPT_ISSET(ops,'p')) { if (hn) - printparamnode(hn, printflags); + paramtab->printnode(hn, printflags); else { zwarnnam(name, "no such variable: %s", asg->name); returnval = 1; @@ -3319,7 +3321,8 @@ bin_unset(char *name, char **argv, Options ops, int func) *ss = 0; } pm = (Param) (paramtab == realparamtab ? - gethashnode2(paramtab, s) : + /* getnode2() to avoid autoloading */ + paramtab->getnode2(paramtab, s) : paramtab->getnode(paramtab, s)); /* * Unsetting an unset variable is not an error. diff --git a/Src/params.c b/Src/params.c index de456c144..3ed771e3b 100644 --- a/Src/params.c +++ b/Src/params.c @@ -447,7 +447,7 @@ newparamtable(int size, char const *name) ht->cmpnodes = strcmp; ht->addnode = addhashnode; ht->getnode = getparamnode; - ht->getnode2 = getparamnode; + ht->getnode2 = gethashnode2; ht->removenode = removehashnode; ht->disablenode = NULL; ht->enablenode = NULL; @@ -869,6 +869,7 @@ createparam(char *name, int flags) if (name != nulstring) { oldpm = (Param) (paramtab == realparamtab ? + /* gethashnode2() for direct table read */ gethashnode2(paramtab, name) : paramtab->getnode(paramtab, name)); @@ -3111,7 +3112,8 @@ unsetparam(char *s) queue_signals(); if ((pm = (Param) (paramtab == realparamtab ? - gethashnode2(paramtab, s) : + /* getnode2() to avoid autoloading */ + paramtab->getnode2(paramtab, s) : paramtab->getnode(paramtab, s)))) unsetparam_pm(pm, 0, 1); unqueue_signals(); -- cgit v1.2.3 From 72a67716aca9896cb75a2b92457fdaed060d8e9e Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Fri, 20 Nov 2015 11:18:16 +0000 Subject: 20974: Bug with scalar assignment to special array in typeset --- ChangeLog | 5 +++++ Src/builtin.c | 24 ++++++++++++++++++++---- Test/D04parameter.ztst | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) (limited to 'Src/builtin.c') diff --git a/ChangeLog b/ChangeLog index 252f2fc5f..7a507fe00 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2015-11-20 Peter Stephenson + + * 20974: Src/builtin.c, Test/D04parameter.ztst: fix bug + when scalar value assigned to special array in typeset. + 2015-11-20 Daniel Shahaf * 37164: Completion/Unix/Command/_tmux: _tmux: Complete external diff --git a/Src/builtin.c b/Src/builtin.c index 18dfdce05..01eb5b84c 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -2438,10 +2438,26 @@ typeset_single(char *cname, char *pname, Param pm, UNUSED(int func), if (ASG_VALUEP(asg) && !dont_set) { Param ipm = pm; if (pm->node.flags & (PM_ARRAY|PM_HASHED)) { - DPUTS(!ASG_ARRAYP(asg), "BUG: inconsistent scalar value for array"); - if (!(pm=assignaparam(pname, asg->value.array ? - zlinklist2array(asg->value.array) : - mkarray(NULL), 0))) + char **arrayval; + if (!ASG_ARRAYP(asg)) { + /* + * Attempt to assign a scalar value to an array. + * This can happen if the array is special. + * We'll be lenient and guess what the user meant. + * This is how normal assigment works. + */ + if (*asg->value.scalar) { + /* Array with one value */ + arrayval = mkarray(ztrdup(asg->value.scalar)); + } else { + /* Empty array */ + arrayval = mkarray(NULL); + } + } else if (asg->value.array) + arrayval = zlinklist2array(asg->value.array); + else + arrayval = mkarray(NULL); + if (!(pm=assignaparam(pname, arrayval, 0))) return NULL; } else { DPUTS(ASG_ARRAYP(asg), "BUG: inconsistent array value for scalar"); diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst index 210c0d854..a3c5d71df 100644 --- a/Test/D04parameter.ztst +++ b/Test/D04parameter.ztst @@ -1847,3 +1847,22 @@ 0:nested parameter name references >all these worlds belong to foo >worlds + + ( + path=(/random /value) + testfn1() { + local path= + print $#path + } + testfn1 + testfn2() { + local path=/somewhere + print $#path $path + } + testfn2 + print $#path $path + ) +0:Local special variables with loose typing +>0 +>1 /somewhere +>2 /random /value -- cgit v1.2.3