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

Re: "trap ... DEBUG" to execute before (instead of after) each command



On Tue, 9 Jan 2007 14:53:47 -0600 (CST)
mkkwong2@xxxxxxxxxxxxxxxxxx wrote:
> Currently "trap ... DEBUG" is used to define some routine that is
> executed AFTER each command. This is modeled after the old ksh.
> 
> The new Korn shell 93 has changed this behavior. The "trap DEBUG"
> routine is executed BEFORE each command instead. This new behavior
> makes more sense and is useful in writing a symbolic debugger for
> shell scripts.
> 
> I suggest that future version of zsh should provide this feature (by
> using a new option that can be set or unset to get the old or new
> behavior).

This is fairly straightforward.  The patch is for the latest version of
the shell, but should apply fairly readily to anything not wildly out of
date.

Index: Doc/Zsh/options.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/options.yo,v
retrieving revision 1.50
diff -u -r1.50 options.yo
--- Doc/Zsh/options.yo	8 Nov 2006 10:38:05 -0000	1.50
+++ Doc/Zsh/options.yo	9 Jan 2007 21:50:14 -0000
@@ -1009,6 +1009,14 @@
 hexadecimal and octal.  Note that these formats will be understood on input
 irrespective of the setting of tt(C_BASES).
 )
+pindex(DEBUG_BEFORE_CMD)
+cindex(traps, DEBUG, before or after command)
+cindex(DEBUG trap, before or after command)
+item(tt(DEBUG_BEFORE_CMD))(
+Run the tt(DEBUG) trap before each command; otherwise it is run after
+each command.  Setting this option mimics the behaviour of ksh 93; with
+the option unset the behaviour is that of ksh 88.
+)
 pindex(ERR_EXIT)
 cindex(exit status, trapping)
 item(tt(ERR_EXIT) (tt(-e), ksh: tt(-e)))(
Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.107
diff -u -r1.107 exec.c
--- Src/exec.c	3 Dec 2006 21:07:17 -0000	1.107
+++ Src/exec.c	9 Jan 2007 21:50:15 -0000
@@ -865,6 +865,22 @@
      * semi-colon or ampersand (`sublists').               */
     code = *state->pc++;
     while (wc_code(code) == WC_LIST && !breaks && !retflag) {
+	int donedebug;
+	if (sigtrapped[SIGDEBUG] && isset(DEBUGBEFORECMD)) {
+	    exiting = donetrap;
+	    ret = lastval;
+	    dotrap(SIGDEBUG);
+	    lastval = ret;
+	    donetrap = exiting;
+	    noerrexit = oldnoerrexit;
+	    /*
+	     * Only execute the trap once per sublist, even
+	     * if the DEBUGBEFORECMD option changes.
+	     */
+	    donedebug = 1;
+	} else
+	    donedebug = 0;
+
 	ltype = WC_LIST_TYPE(code);
 	csp = cmdsp;
 
@@ -969,7 +985,7 @@
 
 	noerrexit = oldnoerrexit;
 
-	if (sigtrapped[SIGDEBUG]) {
+	if (sigtrapped[SIGDEBUG] && !isset(DEBUGBEFORECMD) && !donedebug) {
 	    exiting = donetrap;
 	    ret = lastval;
 	    dotrap(SIGDEBUG);
Index: Src/options.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/options.c,v
retrieving revision 1.32
diff -u -r1.32 options.c
--- Src/options.c	1 Nov 2006 12:25:22 -0000	1.32
+++ Src/options.c	9 Jan 2007 21:50:15 -0000
@@ -108,6 +108,7 @@
 {{NULL, "cshjunkiequotes",    OPT_EMULATE|OPT_CSH},	 CSHJUNKIEQUOTES},
 {{NULL, "cshnullcmd",	      OPT_EMULATE|OPT_CSH},	 CSHNULLCMD},
 {{NULL, "cshnullglob",	      OPT_EMULATE|OPT_CSH},	 CSHNULLGLOB},
+{{NULL, "debugbeforecmd",     OPT_EMULATE},		 DEBUGBEFORECMD},
 {{NULL, "emacs",	      0},			 EMACSMODE},
 {{NULL, "equals",	      OPT_EMULATE|OPT_ZSH},	 EQUALS},
 {{NULL, "errexit",	      OPT_EMULATE},		 ERREXIT},
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.106
diff -u -r1.106 zsh.h
--- Src/zsh.h	8 Jan 2007 22:05:55 -0000	1.106
+++ Src/zsh.h	9 Jan 2007 21:50:16 -0000
@@ -1602,6 +1602,7 @@
     CSHJUNKIEQUOTES,
     CSHNULLCMD,
     CSHNULLGLOB,
+    DEBUGBEFORECMD,
     EMACSMODE,
     EQUALS,
     ERREXIT,

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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