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

Re: FreeBSD compatability feature request



Vincent Stemen wrote:
> Here is the FreeBSD manual entry:
> 
>   -T trapsasync
>         When waiting for a child, execute traps immediately.  If this
>         option is not set, traps are executed after the child exits,
>         as specified in IEEE Std 1003.2 (``POSIX.2'') This nonstandard
>         option is useful for putting guarding shells around children
>         that block signals.  The surrounding shell may kill the child
>         or it may just return control to the tty and leave the child
>         alone, like this:
> 
>            sh -T -c "trap 'exit 1' 2 ; some-blocking-program"

Good(ish) news...

Just tried this out, and it looks like the `trapsasync' behaviour
is the standard (and only) one in zsh.  Hence it doesn't conform to
POSIX (which isn't likely to be an earth-shattering revelation to zsh
regulars).

This means that almost everything we need for both behaviours is already
there.  It remains to add the option TRAPS_ASYNC, turned on by default
in non-Bourne mode for compatibility, and make sure we can still handle
SIGCHLD when the option is unset.

Someone already had the foresight to handle sh options separately, so -T
does the right thing in sh or ksh emulation and the example you gave
should now work as expected.

Further follow-ups should probably go to zsh-workers.

Index: Doc/Zsh/options.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/options.yo,v
retrieving revision 1.30
diff -u -r1.30 options.yo
--- Doc/Zsh/options.yo	6 Mar 2004 00:23:03 -0000	1.30
+++ Doc/Zsh/options.yo	19 Apr 2004 11:13:25 -0000
@@ -1189,6 +1189,14 @@
 Remove any right prompt from display when accepting a command
 line.  This may be useful with terminals with other cut/paste methods.
 )
+pindex(TRAPS_ASYNC)
+cindex(traps, asynchronous)
+item(tt(TRAPS_ASYNC) <C> <Z>)(
+While waiting for a program to exit, run traps immediately.  Otherwise
+the trap is run after the program has exited.  Note this does not affect
+the point at which traps are run for any case other than when the shell is
+waiting for a child process.
+)
 pindex(TYPESET_SILENT)
 item(tt(TYPESET_SILENT))(
 If this is unset, executing any of the `tt(typeset)' family of
@@ -1366,6 +1374,7 @@
 subsect(sh/ksh emulation set)
 startsitem()
 sitem(tt(-C))(em(NO_)CLOBBER)
+sitem(tt(-T))(TRAPS_ASYNC)
 sitem(tt(-X))(MARK_DIRS)
 sitem(tt(-a))(ALL_EXPORT)
 sitem(tt(-b))(NOTIFY)
Index: Src/jobs.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/jobs.c,v
retrieving revision 1.25
diff -u -r1.25 jobs.c
--- Src/jobs.c	17 Feb 2004 10:41:37 -0000	1.25
+++ Src/jobs.c	19 Apr 2004 11:13:25 -0000
@@ -994,7 +994,11 @@
     int q = queue_signal_level();
     Job jn = jobtab + job;
 
-    dont_queue_signals();
+    queue_not_sigchld++;
+    if (isset(TRAPSASYNC))
+	dont_queue_signals();
+    else
+	queue_signals();
     child_block();		 /* unblocked during child_suspend() */
     if (jn->procs || jn->auxprocs) { /* if any forks were done         */
 	jn->stat |= STAT_LOCKED;
@@ -1026,6 +1030,9 @@
     }
     child_unblock();
     restore_queue_signals(q);
+    if (!queueing_enabled)
+	run_queued_signals();
+    queue_not_sigchld--;
 }
 
 /* wait for running job to finish */
Index: Src/options.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/options.c,v
retrieving revision 1.17
diff -u -r1.17 options.c
--- Src/options.c	6 Mar 2004 00:23:03 -0000	1.17
+++ Src/options.c	19 Apr 2004 11:13:25 -0000
@@ -203,6 +203,7 @@
 {NULL, "singlelinezle",	      OPT_KSH,			 SINGLELINEZLE},
 {NULL, "sunkeyboardhack",     0,			 SUNKEYBOARDHACK},
 {NULL, "transientrprompt",    0,			 TRANSIENTRPROMPT},
+{NULL, "trapsasync",	      OPT_EMULATE|OPT_NONBOURNE, TRAPSASYNC},
 {NULL, "typesetsilent",	      OPT_EMULATE|OPT_BOURNE,	 TYPESETSILENT},
 {NULL, "unset",		      OPT_EMULATE|OPT_BSHELL,	 UNSET},
 {NULL, "verbose",	      0,			 VERBOSE},
@@ -346,7 +347,7 @@
     /* Q */  0,
     /* R */  0,
     /* S */  0,
-    /* T */  0,
+    /* T */  TRAPSASYNC,
     /* U */  0,
     /* V */  0,
     /* W */  0,
Index: Src/signals.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/signals.c,v
retrieving revision 1.25
diff -u -r1.25 signals.c
--- Src/signals.c	25 Mar 2004 10:07:15 -0000	1.25
+++ Src/signals.c	19 Apr 2004 11:13:25 -0000
@@ -49,7 +49,7 @@
 /* Variables used by signal queueing */
 
 /**/
-mod_export int queueing_enabled, queue_front, queue_rear;
+mod_export int queueing_enabled, queue_front, queue_rear, queue_not_sigchld;
 /**/
 mod_export int signal_queue[MAX_QUEUE_SIZE];
 /**/
@@ -425,7 +425,8 @@
     }
 #endif
 
-    if (queueing_enabled) {           /* Are we queueing signals now?      */
+    /* Are we queueing signals now?      */
+    if (queueing_enabled && (sig != SIGCHLD || !queue_not_sigchld)) {
         int temp_rear = ++queue_rear % MAX_QUEUE_SIZE;
 
 	DPUTS(temp_rear == queue_front, "BUG: signal queue full");
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.54
diff -u -r1.54 zsh.h
--- Src/zsh.h	11 Mar 2004 14:25:12 -0000	1.54
+++ Src/zsh.h	19 Apr 2004 11:13:25 -0000
@@ -1523,6 +1523,7 @@
     SINGLELINEZLE,
     SUNKEYBOARDHACK,
     TRANSIENTRPROMPT,
+    TRAPSASYNC,
     TYPESETSILENT,
     UNSET,
     VERBOSE,

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



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