Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2012-11-27
00:58 • Ticket [3432962fff] Some noninteractive invocations of tclsh still run .tclshrc<... artifact: 423c28a55c user: dgp
2011-11-04
21:42 • Ticket [3432962fff]: 4 changes artifact: f58d433301 user: dgp
21:25 • Open ticket [3432962fff]. artifact: 947531719d user: dgp
03:56 • Ticket [3432962fff]: 4 changes artifact: c6386d3ae0 user: karll
03:37 • Ticket [3432962fff]: 4 changes artifact: 2b548399b6 user: dgp
03:28 • Pending ticket [3432962fff]. artifact: 357b6cada4 user: dgp
03:19 • Add attachment patch to ticket [3432962fff] artifact: 56a7a77cd0 user: karll
03:19 • Ticket [3432962fff] Some noninteractive invocations of tclsh still run .tclshrc<... artifact: c693cd6fb6 user: karll
2011-11-03
20:16 • New ticket [3432962fff]. artifact: b66194d492 user: karll

Some noninteractive invocations of tclsh still run .tclshrc

Open3432962fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 14 years ago
Descriptionplain2011-11-03 20:16:54
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;
! }
      }
  
      /*
karllplain2011-11-04 03:19:07
File Added - 427654: patch
dgpplain2011-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.
dgpplain2011-11-04 03:37:24
The test suite makes use of the existing
logic.  The proposed patch makes 5 tests fail.
karllplain2011-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.
dgpplain2011-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.
dgpplain2011-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.
dgpplain2012-11-27 00:58:50
data_type - 360894
Log in (anonymous is fine) to add a comment.

Attachments: