Arc Forumnew | comments | leaders | submitlogin
Popen in arc
9 points by lg 5504 days ago | discuss
I find the IO.popen call for running a subprocess indispensable in ruby, so here it is in arc (ok, this is more like Open3.popen3, but it's actually even better).

First, add this to ac.scm:

  (xdef process process)
This gives you the process procedure from mzlib/process.ss.

  (process "echo hello world")  returns a list of 5 args:
input-port, output-port, PID, error-port, control-proc.

control-proc is a procedure of 1 argument that lets you interact with the process a bit. You can pass it 'kill, 'status, 'wait, 'interrupt, whatever. documentation: http://docs.plt-scheme.org/reference/subprocess.html#(def._((lib._scheme/system..ss)._process))

process gives you everything you need to write a simple popen:

  (mac popen (cmd args . body)
       (w/uniq (i o e ctrl pid)
               `(let (,i ,o ,pid ,e ,ctrl) (process ,cmd)
                     (let ,args (list ,i ,o ,e ,ctrl ,pid)
                          (do1 (do ,@body)
                               (close ,i ,o ,e)
                               (,ctrl 'kill))))))
I changed the order of the args because putting error port after pid was stupid IMO, sometimes i need error port but i never need pid.

usage example:

  (def cleaner-shash (str)
       (popen "openssl dgst -sha1" (ip op)
              (disp str op)
              (close op)
              (readline ip)))

  arc> (cleaner-shash "\"my\"quoty\"password")
  ;=> "1cc3234acced74208915e9da70d79022a661c8b6"
This answers pg's comment in app.arc :)