From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22418 invoked by alias); 20 Feb 2012 15:43:57 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 30238 Received: (qmail 5542 invoked from network); 20 Feb 2012 15:43:51 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at csr.com does not designate permitted sender hosts) Date: Mon, 20 Feb 2012 15:13:09 +0000 From: Peter Stephenson To: Subject: Re: suspend (^Z) behavior while a function is running is unclear Message-ID: <20120220151309.4cc1a83d@pwslap01u.europe.root.pri> In-Reply-To: <20120220130259.GB7797@xvii.vinc17.org> References: <20120220130259.GB7797@xvii.vinc17.org> Organization: Cambridge Silicon Radio X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.101.10.170] X-Scanned-By: MailControl 7.6.6 (www.mailcontrol.com) on 10.68.0.158 On Mon, 20 Feb 2012 14:02:59 +0100 Vincent Lefevre wrote: > foo() { emacs "$@"; d=`date`; echo "$d"; } > > and the following test under X (so that Emacs uses its own window, > or you can try with another X application): > > 1. Run foo. > 2. Type ^Z in the terminal. > 3. Type: fg [Return]. > 4. Quit Emacs. > 5. Type: echo "$d" [Return]. > > With zsh, the whole function is suspended. The effect is that zsh is > forked (this is not documented, though can be guessed), thus $d will > not be set in the main shell (Step 5: nothing is displayed). Yes, this is long-standing behaviour that's never been described. I've tried to document it. > BTW, concerning zsh, if the behavior is regarded as correct, a fork > is still done even when the suspended command is the last one in the > function, e.g. > > foo() { emacs "$@"; } There's certainly no good reason for trying to make a special case here; it's complicated for very little gain. > Is the fork really necessary? If you're actually trying to do real, live job control (rather than some internal approximation to it), yes, as that operates on processes, and (in your example) the shell function is the top-level code you need to suspend, regardless of any processes that may also have been started from it. I can't tell you if it's necessary for what you thought you were trying to do, if it wasn't job control. Index: Doc/Zsh/jobs.yo =================================================================== RCS file: /cvsroot/zsh/zsh/Doc/Zsh/jobs.yo,v retrieving revision 1.5 diff -p -u -r1.5 jobs.yo --- Doc/Zsh/jobs.yo 29 Mar 2006 19:25:15 -0000 1.5 +++ Doc/Zsh/jobs.yo 20 Feb 2012 15:00:32 -0000 @@ -37,6 +37,21 @@ when it is typed. A job being run in the background will suspend if it tries to read from the terminal. + +Note that if the job running in the foreground is a shell function, +then suspending it will have the effect of causing the shell to fork. +This is necessary to separate the function's state from that of the +parent shell performing the job control, so that the latter can return +to the command line prompt. As a result, even if tt(fg) is +used to continue the job the function will no longer be part of the +parent shell, and any variables set by the function will not be visible +in the parent shell. Thus the behaviour is different from the case +where the function was never suspended. Zsh is is different from many +other shells in this regard. + +The same behaviour is found when the shell is executing code as the +right hand side of a pipeline, in order that the entire pipeline +can be managed as a single job. cindex(background jobs, I/O) cindex(jobs, background, I/O) Background jobs are normally allowed to produce output, -- Peter Stephenson Software Engineer Tel: +44 (0)1223 692070 Cambridge Silicon Radio Limited Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog