[plt-scheme] The filename of the file in the definition window

From: Robby Findler (robby at cs.uchicago.edu)
Date: Wed Dec 19 17:09:42 EST 2007

On Dec 19, 2007 3:56 PM, Jens Axel Soegaard <jensaxel at soegaard.net> wrote:
> Robby Findler wrote:
>
> >> I get
> >>
> >>    #<struct:object:...pper/stepper-tool.ss:618:8>
> >>
> >> as the result of calling object-name with the port.
> >> Makes sense it doesn't work, since the contents
> >> of the definition window might be unsaved.
> >
> > Oh, right. This is going to change in the next release (it has already
> > changed in SVN). It is now a path or a special symbol indicating that
> > there is no path (if the file has never been saved).
>
> Fine.
>
> > Handing out that object to the user's program like that is insecure
> > (they can get it out of the syntax objects).
>
> Ah, but I am not doing that.

Right, sorry. DrScheme was (by putting the editor into the port, it
was not just handing it out to you, but also handing it out to the
user's program).

> The plan is to let
> front-end/complete-program call the external tool via
> system from "process.ss", then capture the output and
> convert the output to a syntax-object like this:
>
>     #`(display #,captured-output)
>
> The net effect is that the contents of the definition
> window is "piped" through the shell command, and the
> result is shown in the interaction window.
>
> > Here's a self-modifiying
> > program (note that you can do worse things, since you can go from the
> > editor to the drscheme frame and thus get at lots of drscheme's
> > internal state).
> >
> > (define-syntax (m stx)
> >   (with-syntax ([x (syntax-source stx)])
> >     #'x))
> >
> > (require (lib "mred.ss" "mred")
> >          (lib "class.ss"))
> >
> > (let* ([ed (m)]
> >        [line1 (send ed get-text
> >                     (send ed paragraph-start-position 0)
> >                     (send ed paragraph-end-position 0))]
> >        [mth (regexp-match #rx"^; ([0-9]+)" line1)]
> >        [lckd (send ed is-locked?)])
> >   (send ed lock #f)
> >   (if mth
> >       (begin
> >         (send ed delete
> >               (send ed paragraph-start-position 0)
> >               (send ed paragraph-end-position 0))
> >         (send ed insert (format "; ~a" (+ 1 (string->number (cadr mth)))) 0 0))
> >       (send ed insert "; 0\n" 0 0))
> >   (send ed lock lckd))
>
> That's clever! Best hack I have seen in ages.
>
> And of course given a filename, the same risk will be there...
> ... but I am willing to run the risk!
>
> >> This leads to another question: How do I check whether the
> >> contents of the definition windows has been saved?
> >>
> >> The reason I need the name is that I want to pass the content
> >> along to an external executable.
> >
> > I'm not positive, but wouldn't it be better to pass the port along?
> > The port library lets you do all kind of things, like forking the port
> > if it needs to go two places, etc. Also, you don't have to deal with
> > the "is it saved" question.
>
> Well, I as described above I actually want to call the external
> tool via "system" from (lib "process.ss"), so I can't pass
> the port along.

Sure you can! (Well, "pass" was the wrong word but the idea is right.)

Check out (lib "port.ss"). You can to create a thread that does a
copy-port from the port you've got there to the input port you get
from the process call. You'll need a pipe inbetween.

Be sure to close your ports (the pipe ones too).

Robby


Posted on the users mailing list.