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

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

On Dec 19, 2007 4:15 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> At Wed, 19 Dec 2007 16:09:42 -0600, "Robby Findler" wrote:
> > > 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).
>
> Avoid creating pipes, because it creates too much work for you in
> managing a concurrent copy operation and closing the pipe at the right
> time. Instead, parameterize the call to `system' to set
> `current-input-port' to your port.

I would have thought that something like

  (thread (lambda () (copy-port  ...) (close-*-port ..)))

would have been all you'd need.

But when I try coding it up, I see that one can avoid a pipe, but
still use the above pattern, eg:

(require scheme/port
         scheme/system)

(define (run-wc-on-file filename)
  (call-with-input-file filename
    (λ (file-port)
      (define-values (wc-out wc-in pid wc-err proc) (apply values
(process "wc")))
      (define ts
        (list
         (thread
          (λ ()
            (copy-port file-port wc-in)
            (close-output-port wc-in)))
         (thread
          (λ ()
            (copy-port wc-out (current-output-port))
            (close-input-port wc-out)))
         (thread
          (λ ()
            (copy-port wc-err (current-error-port))
            (close-input-port wc-err)))))
      (for-each thread-wait ts))))

(run-wc-on-file "/home/robby/tmp.ss")

Did you have something else in mind, or does that code have a bug somehow?

Robby

Posted on the users mailing list.