Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: $! on bg



A thread on the Austin Group mailing list suggests we should be setting
$! when a process is put into the background, not just when a process
is started in the background.  Bash already does this.  (There's some
other grungy stuff about $! on the list I'm less concerned with.)

It's hard to see any compatilibity problems arising with this since bg
is mostly used at the command line, but maybe Bart or someone else can
think of problems.

This updates the documentation, too.  It was probably intended to mean
what the shell actually did, i.e. set $! on "... &" only, although one
could interpret running "bg" as "invoking a background command".

I haven't attempted to lever this into Sven's hack whereby the
shell can fork and allow you to put a job that was running
into the current shell into the background; however, it "might just
work", since the fudged job should already be in the job table by the
time you do "bg".

Index: Doc/Zsh/params.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/params.yo,v
retrieving revision 1.34
diff -u -r1.34 params.yo
--- Doc/Zsh/params.yo	5 Oct 2006 13:28:24 -0000	1.34
+++ Doc/Zsh/params.yo	26 Oct 2006 17:24:12 -0000
@@ -477,7 +477,8 @@
 startitem()
 vindex(!)
 item(tt(!) <S>)(
-The process ID of the last background command invoked.
+The process ID of the last command started in the background with tt(&),
+or put into the background with the tt(bg) builtin.
 )
 vindex(#)
 item(tt(#) <S>)(
Index: Src/jobs.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/jobs.c,v
retrieving revision 1.47
diff -u -r1.47 jobs.c
--- Src/jobs.c	28 Jun 2006 13:12:55 -0000	1.47
+++ Src/jobs.c	26 Oct 2006 17:24:12 -0000
@@ -1789,9 +1789,21 @@
 	case BIN_WAIT:
 	    if (func == BIN_BG)
 		jobtab[job].stat |= STAT_NOSTTY;
-	    if ((stopped = (jobtab[job].stat & STAT_STOPPED)))
+	    if ((stopped = (jobtab[job].stat & STAT_STOPPED))) {
 		makerunning(jobtab + job);
-	    else if (func == BIN_BG) {
+		if (func == BIN_BG) {
+		    /* Set $! to indicate this was backgrounded */
+		    Process pn = jobtab[job].procs;
+		    for (;;) {
+			Process next = pn->next;
+			if (!next) {
+			    lastpid = (zlong) pn->pid;
+			    break;
+			}
+			pn = next;
+		    }
+		}
+	    } else if (func == BIN_BG) {
 		/* Silly to bg a job already running. */
 		zwarnnam(name, "job already in background");
 		thisjob = ocj;

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



Messages sorted by: Reverse Date, Date, Thread, Author