Skip to content
Tcl Source Code History Of Ticket 0e7c09eb49912d9c
Login

History Of Ticket 0e7c09eb49912d9c

Artifacts Associated With Ticket 0e7c09eb49912d9c

  1. Ticket change [3981a1206b] (rid 141540) by pooryorick on 2015-04-14 14:55:42:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized to:
      tclsh is schizophrenic about interactive mode.  In the case where a file is
      given as an argument to tclsh, the code path is clear, and tclsh runs in true
      non-interactive mode, passing the script off to Tcl_FSEvalFileEx.  If stdin is
      a terminal and a script is not provided, ::tcl_interactive is true, and tclsh runs
      in interactive mode. Prompts and command results are printed to stdout, error
      messages printed on stderr, errors do not end the REPL, and when the end of the
      file is reached, the exit status is 0 even if the last command resulted in an
      error.
      
      Here's the multiple-personality case:  If stdin is not a terminal and a script is
      not provided, ::tcl_interactive is false but tclsh still operates mostly in
      interactive mode, except that it doesn't print prompts and command results to
      stdout.  What it does do is not halt on errors, and exit with a status of 0
      even if the last command in the script returns an error.  This means that the
      exit status of a pipeline like the following is 0: 
      
      <code><verbatim>echo some_command_that_doesnt_exist | tclsh </verbatim></code>
      
      It also means that how a script is passed to the interpreter can determine its
      exit status.  Consider a script named <code>myscript</code> with the following
      contents:
      
      <code><verbatim>puts too many arguments</verbatim></code>
      
      If invoked like this, the exit status is 1:
      
      <code><verbatim>tclsh myscript</verbatim></code>
      
      But if invoked in these ways, the short error result is displayed, the exit status is 0:
      
      <code><verbatim>tclsh < myscript</verbatim></code>
      
      <code><verbatim>cat myscript | tclsh</verbatim></code>
      
      Tclsh in a pipeline is an important use case on Unix-like systems.  It could
      behave in a more expected manner by halting on errors and passing a non-zero
      exit status when script input is via stdin and that channel is not a terminal.
      The change to accomplish that is trivial:
      
      <code><verbatim>
      --- generic/tclMain.c
      +++ generic/tclMain.c
      @@ -540,10 +540,14 @@
       	    if (code != TCL_OK) {
       		chan = Tcl_GetStdChannel(TCL_STDERR);
       		if (chan) {
       		    Tcl_WriteObj(chan, Tcl_GetObjResult(interp));
       		    Tcl_WriteChars(chan, "\n", 1);
      +		}
      +		if (!is.tty) {
      +		    exitCode = 1;
      +		    goto done;
       		}
       	    } else if (is.tty) {
       		resultPtr = Tcl_GetObjResult(interp);
       		Tcl_IncrRefCount(resultPtr);
       		Tcl_GetStringFromObj(resultPtr, &length);
      </verbatim></code>
      
      Apart from the modified lines in this page, it can be seen that this code path
      was already making decisions based on is.tty, even though the whole path is
      ostensibly already the interactive path.  What it really is is the stream path.
      One additional decision based on is.tty seems reasonable enough.  This places
      the burden of deciding whether stdin is interactive more squarely on the
      platform-specific parts of Tcl that live off in their own directories.
      
      The potential downside to this change is any disruption it may cause to
      existing processes feed scripts to tclsh via an stdin that is not a terminal.
      This is probably uncommon because this strategy is playing with fire.  Such
      processes would no longer be able to rely on the interpreter to ignore errors.
      The solution would be to explicitly catch errors in the scripts or to use
      something like Expect, which could provide a pseudo terminal.
      
      One hint that this fix is good comes from the test suite.  The changes required
      to support this patch are for the most part to use ::tcl_interactive explicitly
      in order to get the desired outcome, which was the reason for introducing
      ::tcl_interactive in the first place.
      
    5. foundin initialized to: "8.6.4"
    6. is_private initialized to: "0"
    7. login: "pooryorick"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "e476148cded301ffff84bcdc432536dc0c949e88"
    10. resolution initialized to: "None"
    11. severity initialized to: "Important"
    12. status initialized to: "Open"
    13. submitter initialized to: "pooryorick"
    14. subsystem initialized to: "69. Other"
    15. title initialized to:
      tclsh in a command pipeline: ::tcl_interactive is 0, but interpreter is in interactive mode (halfway).
      
    16. type initialized to: "Bug"
  2. Add attachment "tclsh_noninteractive_stream.diff" [737764d7c1] (rid 141541) by pooryorick on 2015-04-14 15:01:01.

  3. Ticket change [4baa4902fa] (rid 141542) by pooryorick on 2015-04-14 17:48:43:

    1. comment changed to:
      tclsh is schizophrenic about interactive mode.  In the case where a file is
      given as an argument to tclsh, the code path is clear, and tclsh runs in true
      non-interactive mode, passing the script off to Tcl_FSEvalFileEx.  If stdin is
      a terminal and a script is not provided, ::tcl_interactive is true, and tclsh runs
      in interactive mode. Prompts and command results are printed to stdout, error
      messages printed on stderr, errors do not end the REPL, and when the end of the
      file is reached, the exit status is 0 even if the last command resulted in an
      error.
      
      Here's the multiple-personality case:  If stdin is not a terminal and a script is
      not provided, ::tcl_interactive is false but tclsh still operates mostly in
      interactive mode, except that it doesn't print prompts and command results to
      stdout.  What it does do is not halt on errors, and exit with a status of 0
      even if the last command in the script returns an error.  This means that the
      exit status of a pipeline like the following is 0: 
      
      <code><verbatim>echo some_command_that_doesnt_exist | tclsh </verbatim></code>
      
      It also means that how a script is passed to the interpreter can determine its
      exit status.  Consider a script named <code>myscript</code> with the following
      contents:
      
      <code><verbatim>puts too many arguments</verbatim></code>
      
      If invoked like this, the exit status is 1:
      
      <code><verbatim>tclsh myscript</verbatim></code>
      
      But if invoked in these ways, the short error result is displayed, the exit status is 0:
      
      <code><verbatim>tclsh < myscript</verbatim></code>
      
      <code><verbatim>cat myscript | tclsh</verbatim></code>
      
      Tclsh in a pipeline is an important use case on Unix-like systems.  It could
      behave in a more expected manner by halting on errors and passing a non-zero
      exit status when script input is via stdin and that channel is not a terminal.
      The change to accomplish that is trivial:
      
      <code><verbatim>
      --- generic/tclMain.c
      +++ generic/tclMain.c
      @@ -540,10 +540,14 @@
       	    if (code != TCL_OK) {
       		chan = Tcl_GetStdChannel(TCL_STDERR);
       		if (chan) {
       		    Tcl_WriteObj(chan, Tcl_GetObjResult(interp));
       		    Tcl_WriteChars(chan, "\n", 1);
      +		}
      +		if (!is.tty) {
      +		    exitCode = 1;
      +		    goto done;
       		}
       	    } else if (is.tty) {
       		resultPtr = Tcl_GetObjResult(interp);
       		Tcl_IncrRefCount(resultPtr);
       		Tcl_GetStringFromObj(resultPtr, &length);
      </verbatim></code>
      
      Apart from the modified lines in this page, it can be seen that this code path
      was already making decisions based on is.tty, even though the whole path is
      ostensibly already the interactive path.  What it really is is the stream path.
      One additional decision based on is.tty seems reasonable enough.  This places
      the burden of deciding whether stdin is interactive more squarely on the
      platform-specific parts of Tcl that live off in their own directories.
      
      The potential downside to this change is any disruption it may cause to
      existing processes that feed scripts to tclsh via an stdin that is not a terminal.
      This is probably uncommon because this strategy is playing with fire.  Such
      processes would no longer be able to rely on the interpreter to ignore errors.
      The solution would be to explicitly catch errors in the scripts or to use
      something like Expect, which could provide a pseudo terminal.
      
      One hint that this fix is good comes from the test suite.  The changes required
      to support this patch are for the most part to use ::tcl_interactive explicitly
      in order to get the desired outcome, which was the reason for introducing
      ::tcl_interactive in the first place.
      
    2. login: "pooryorick"
    3. mimetype: "text/plain"