[plt-scheme] Can a module tell if it's being run interactively?
This may not be a great way to do it, but it looks like it'll work:
(terminal-input? (current-input-port)) returns #t on the input port
provided by the mzscheme repl and #f on the input port provided by the
DrScheme repl.
-jacob
On Tue, Apr 15, 2008 at 4:12 PM, Richard Cobbe <cobbe at ccs.neu.edu> wrote:
> Is there a way for code in a module to tell if it's being run in the
> DrScheme REPL or as part of a script?
>
> Right now, I'm trying to debug a script that reads input from stdin and
> writes output to stdout. I've got the file structured as follows:
>
> #!/path/to/mzscheme
> #lang mzscheme
>
> (define (run)
> (pretty-print (munge-input (read))))
>
> (define munge-input ...)
>
> (run)
>
> Now, If my script hits a bug, my first idea is to load the module into
> DrScheme -- which immediately stops, waiting for input. The input to the
> script is larger than I want to type at the REPL, so I comment out the call
> to `run', reload the module, then do (with-input-from-file ... run). After
> I get the bug fixed, I inevitably forget to uncomment the call to run, so
> the script is a NOP the first time I run it.
>
> It'd be really handy to say something like
>
> #!/path/to/mzscheme
> #lang scheme
>
> (define (run)
> (pretty-print (munge-input (read))))
>
> (define munge-input ...)
>
> (unless (running-at-repl?) (run))
>
> where `running-at-repl?' is provided by the "scheme" language. That way,
> `run' gets called when I run the script from the Unix shell but not when I
> load it into DrScheme and hit the "Run" button.
>
> OCaml's Sys.interactive
> (http://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html#VALinteractive)
> is basically what I'm looking for here. In OCaml, one can write something
> like
>
> let _ =
> if not !Sys.interactive
> then run ()
> else ()
>
> at module top-level to get the behavior I described above.
>
> Is this possible? If not, would it be possible to add this feature?
>
> Thanks,
>
> Richard
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>