From nobody@FreeBSD.org  Mon Aug 21 19:09:46 2006
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id D2EBC16A4DF
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 21 Aug 2006 19:09:46 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 5E6A743D55
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 21 Aug 2006 19:09:46 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id k7LJ9kuY099517
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 21 Aug 2006 19:09:46 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id k7LJ9kBA099514;
	Mon, 21 Aug 2006 19:09:46 GMT
	(envelope-from nobody)
Message-Id: <200608211909.k7LJ9kBA099514@www.freebsd.org>
Date: Mon, 21 Aug 2006 19:09:46 GMT
From: Todor Dragnev <todor.dragnev@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: csh/tcsh jobs control
X-Send-Pr-Version: www-2.3

>Number:         102357
>Category:       bin
>Synopsis:       [patch] tcsh(1)/csh(1) jobs control: sometimes 'fg' command doesn't capture back the background process
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Aug 21 19:10:13 GMT 2006
>Closed-Date:    
>Last-Modified:  Fri Dec 05 03:45:50 UTC 2008
>Originator:     Todor Dragnev
>Release:        freebsd 6.1
>Organization:
>Environment:
FreeBSD iLux.g00net.org 6.1-STABLE FreeBSD 6.1-STABLE #0: Wed Jun 14 00:56:11 EEST 2006     root@iLux.g00net.org:/usr/src/sys/i386/compile/ZEON  i386

>Description:
I have problem with jobs control in 'tcsh' or 'csh' shell.
Sometimes 'fg' command doesn't captures back the background process.
When executing something else, 'fg' starts working.
>How-To-Repeat:
~> tail -f /var/log/httpd-error.log
..
--- output is removed ---
..
^Z
Suspended
~> bg
[1]    tail -f /var/log/httpd-error.log &
~> jobs 
[1]    Running                       tail -f /var/log/httpd-error.log
~> jobs -l
[1]     4372 Running                       tail -f /var/log/httpd-error.log
~> fg
fg: No current job.
~> man
What manual page do you want?
~> fg
tail -f /var/log/httpd-error.log
^C
>Fix:

>Release-Note:
>Audit-Trail:

From: Nate Eldredge <neldredge@math.ucsd.edu>
To: bug-followup@FreeBSD.org, todor.dragnev@gmail.com
Cc:  
Subject: Re: bin/102357: tcsh(1)/csh(1) jobs control: sometimes 'fg' command
 doesn't capture back the background process
Date: Wed, 3 Dec 2008 18:19:01 -0800 (PST)

 It looks like this is a "feature".
 
 csh has a notion of the "current" job.  If you run 'jobs', the current job 
 will be marked with a +.  Running fg, bg, etc without arguments acts on 
 the current job.
 
 There is code in tcsh's bg command that explicitly sets the job just 
 backgrounded not to be the current job (calling the pclrcurr() function). 
 If there are other jobs running, one of them becomes the current job.  If 
 no other jobs are running, as in your test case, then there will be no 
 current job.  (pcurrent is set to NULL.)  Note at this point that the 
 'jobs' command doesn't display a + by your job.  'fg' without arguments 
 correctly says there is no current job.  'fg %1' will do what you 
 want, of course.
 
 I am not sure what is the point of this behavior, but it definitely seems 
 intentional.  It goes back at least as far as tcsh 6.00 circa 1995.  I 
 could imagine that some scripts may depend on it now so I think maybe it 
 should not be changed.  However, I've attached a patch that will eliminate 
 this behavior, if you want to try it.  With the patch a backgrounded 
 current job remains current.
 
 When you run another job ('man' in your example), it becomes the current 
 job when it is started.  When it completes, it can't be the current job 
 anymore, so, as before, tcsh looks for another job to set as the current 
 one.  The only candidate is 'tail -f', so it becomes the current job 
 again.  At this point if you run 'jobs' again you'll see the + indicator, 
 and fg/bg will work as you expect.
 
 --- tcsh.orig/sh.proc.c	2007-10-16 09:18:39.000000000 -0700
 +++ src/contrib/tcsh/sh.proc.c	2008-12-03 18:01:35.000000000 -0800
 @@ -1546,8 +1546,6 @@
   		np->p_flags &= ~PFOREGND;
   	}
       } while ((np = np->p_friends) != pp);
 -    if (!foregnd)
 -	pclrcurr(pp);
       (void) pprint(pp, foregnd ? NAME | JOBDIR : NUMBER | NAME | AMPERSAND);
 
       /* GrP run jobcmd hook if foregrounding */
 
 
 
 -- 
 
 Nate Eldredge
 neldredge@math.ucsd.edu
>Unformatted:
