zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Richard Hansen <rhansen@bbn.com>
Cc: zsh-workers@zsh.org
Subject: Re: 'emulate sh -c' and $0
Date: Fri, 30 May 2014 10:00:50 -0700	[thread overview]
Message-ID: <140530100050.ZM18382@torch.brasslantern.com> (raw)
In-Reply-To: <5388461D.8060203@bbn.com>

On May 30,  4:49am, Richard Hansen wrote:
}
} On 2014-05-29 23:45, Bart Schaefer wrote:
} > On May 29,  7:04pm, Richard Hansen wrote:
} >>
} >> I just encountered what I think is a bug in Zsh 5.0.5.
} > 
} > To the extent that it's working exactly as documented, it's not a bug ...
} 
} Would you mind pointing me to where this specific behavior is
} documented?  It was non-obvious to me when I was digging around.

I don't pretend there couldn't be more clarity in the zsh documentation,
but under "Parameters" there's first this:

 In the parameter lists that follow, the mark `<S>' indicates that the
 parameter is special.  Special parameters cannot have their type
 changed or their readonly attribute turned off, and if a special
 parameter is unset, then later recreated, the special properties will
 be retained.  `<Z>' indicates that the parameter does not exist when
 the shell initializes in sh or ksh emulation mode.

And then in the "Parameters Set by the Shell" subsection there is:

 0 <S>
     The name used to invoke the current shell.  If the
     FUNCTION_ARGZERO option is set, this is set temporarily within a
     shell function to the name of the function, and within a sourced
     script to the name of the script.

There's no <Z> there, and there's no mention in the "Compatibility"
section about the behavior of $0, only that NO_FUNCTION_ARGZERO is set.
Finally the entry for the emulate builtin says:

     With single argument set up zsh options to emulate the specified
     shell as much as possible.  `csh' will never be fully emulated.
     If the argument is not one of the shells listed above, zsh will be
     used as a default; more precisely, the tests performed on the
     argument are the same as those used to determine the emulation at
     startup based on the shell name, see Compatibility.

} (I am aware of the documentation for the FUNCTION_ARGZERO option.  I'm
} more interested in what it really means to be running in sh emulation
} mode, as that's where I think the bug is.)

In general, emulation is at its most complete if and only if the shell
is actually started as an emulator (e.g., the path name to the shell
binary itself is not zsh, or ARGV0 is set in the environment).  The
"emulate" builtin only changes setopts to the closest possible.

} > I don't find those examples particularly compelling,
} 
} Here's the real-world problem that motivated my bug report; perhaps it
} is a more compelling example (or perhaps you'll think of a better way to
} solve the problem I was addressing):
} 
} http://article.gmane.org/gmane.comp.version-control.git/250409

Instead of "compelling" I perhaps should have said "likely to come up
in common usage."  You have a fairly rare special case there.  In that
example,

    ARGV0=sh exec zsh "$0" "$ <at> "

might do what you want, but I'm not entirely following from the diff
context what's intended.
 
} > but the original
} > value of $0 is already stashed; what would need to change is that the
} > *local* value of $0 gets temporarily replaced by the global one.
} 
} That's good news; that should make it easier to write a patch that
} temporarily replaces the local value with the global value.

Unfortunately the way the local value is implemented is usually to use
C local variable scoping to stash and restore the contents of a C global
pointer, so this would mean at least one additional C global.

} Would you (or anyone else in the community) be opposed to such a patch?

The use cases in both directions seem pretty unusual to me.  Losing the
ability to "localize" $0 for scripts feels almost as likely to create
questions as does your situation.  I suppose if both values were in the
C global state, it would be possible to have the "correct" one appear
at the instant functionargzero changes, instead of being determined by
the setting at the time the function is entered.  OTOH that would be a
larger behavior difference / lack of backward compatibilty.

} If not, can you point me to the relevant bits of code to help me get
} started?

Search Src/*.c for references to "argzero", with particular attention to
builtin.c:bin_emulate.


  reply	other threads:[~2014-05-30 17:01 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-29 23:04 Richard Hansen
2014-05-30  3:45 ` Bart Schaefer
2014-05-30  8:49   ` Richard Hansen
2014-05-30 17:00     ` Bart Schaefer [this message]
2014-05-30 21:14       ` Richard Hansen
2014-05-31  5:13         ` Bart Schaefer
2014-05-31 23:47           ` Bart Schaefer
2014-06-03 20:15           ` Richard Hansen
2014-06-03 20:26             ` Peter Stephenson
2014-06-03 21:10               ` Bart Schaefer
2014-06-03 23:35                 ` Richard Hansen
2014-06-04  0:09                   ` Bart Schaefer
2014-06-03 21:21             ` Bart Schaefer
2014-06-03 22:54               ` Richard Hansen
2014-06-04  0:03                 ` Bart Schaefer
2014-06-04  1:10                   ` Bart Schaefer
2014-06-04  1:23                 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=140530100050.ZM18382@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=rhansen@bbn.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).