Tcl Source Code

View Ticket
Login
Ticket UUID: 3432962
Title: Some noninteractive invocations of tclsh still run .tclshrc
Type: RFE Version: None
Submitter: karll Created on: 2011-11-03 20:16:54
Subsystem: 50. Embedding Support Assigned To: dgp
Priority: 8 Severity:
Status: Open Last Modified: 2012-11-27 00:58:50
Resolution: None Closed By: dgp
    Closed on: 2011-11-03 20:28:09
Description:
Running Tcl without command line arguments but with input not coming from a tty causes .tclshrc (or equivalent) to be loaded.  It shouldn't.  (We don't want peoples' tclshrc files to cause noninteractive Tcl programs to change their behavior.)

How to reproduce:
Put something in your .tclshrc like...  puts ".tclshrc loaded"

echo exit | tclsh8.5

It will emit ".tclshrc loaded".  It shouldn't.

Note that in the above example, or any other where stdin isn't a tty, tcl_interactive is correctly set to 0.

Here is a context diff for a patch that fixes the bug:

*** tclMain.c.origThu Nov  3 15:02:54 2011
--- tclMain.cThu Nov  3 15:04:12 2011
***************
*** 460,472 ****
      }
  
      /*
!      * We're running interactively. Source a user-specific startup file if the
!      * application specified one and if the file exists.
       */
  
!     Tcl_SourceRCFile(interp);
!     if (Tcl_LimitExceeded(interp)) {
! goto done;
      }
  
      /*
--- 460,475 ----
      }
  
      /*
!      * No script file was specified on the command line.
!      * If we're running interactively, source a user-specific 
!      * startup file if the application specified one and if the file exists.
       */
  
!     if (tty) {
! Tcl_SourceRCFile(interp);
! if (Tcl_LimitExceeded(interp)) {
!     goto done;
! }
      }
  
      /*
User Comments: dgp added on 2012-11-27 00:58:50:

data_type - 360894

dgp added on 2011-11-04 21:42:51:
I was under the impression that
this was a change introduced when
I revised Tcl_Main() for Tcl 8.4 in 2002.  A
review of the history indicates otherwise.

The reading of an "rc file" by Tcl appears
to be introduced in the Tcl 7.0 release, and
starting then and unchanged since, Tcl has
been sourcing the rc file whenever it is in
interactive mode -- reading cmds from stdin --
without regard to whether stdin is a tty.

dgp added on 2011-11-04 21:25:11:
Thank you for the followup.  I need
to review the impact of the change
on docs and tests before accepting it.

karll added on 2011-11-04 03:56:08:
I am the inventor of .tclshrc, which first appeared in TclX in, like, 1991.  My intention in making it that tclshrc only be loaded when Tcl is used interactively was to prevent peoples' tclshrc files from changing the behavior of noninteractive Tcl programs, preventing "spooky actions at a distance" and also trying to keep people from accidentally distributing programs with subtle dependencies on something that's done in their tclshrc file, that they might not even realize, like loading some package.

I'm not proposing changing tcl_interactive or how it gets set.  I'm not proposing changing how Tcl behaves if it has any arguments on the command line, which is by far the most common noninteractive use. I'm merely asking for the rc file to never be loaded if Tcl isn't interactive, whereas right now it *almost* never is loaded if Tcl isn't interactive.  What's the test for setting tcl_interactive?  It's set to the result of isatty(0).

For the example of a Makefile action, like

    echo "pkg_mkIndex ." | tclsh

tcl_interactive will properly be 0.  I assert this is truly noninteractive invocation of tclsh (you'll never get a command prompt) and Tcl agrees with me in that it set tcl_interactive to 0.  All I'm asking is that tclshrc not be loaded when Tcl isn't interactive, which is aligned with its original intention.

The problem was discovered by exactly the problem I was trying to prevent.  One of our developers had a custom tclshrc file that caused 'echo "pkg_mkIndex ." | tclsh' to malfunction, for them and them only, in a subtle way that took hours to track down.

dgp added on 2011-11-04 03:37:24:
The test suite makes use of the existing
logic.  The proposed patch makes 5 tests fail.

dgp added on 2011-11-04 03:28:09:
The logic currently in Tcl_Main, and the meaning
of "interactive" on which it is based, have been in
place nearly 10 years.  I'm not inclined to change
it without a clearer reason why.

karll added on 2011-11-04 03:19:07:

File Added - 427654: patch

Attachments: