home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / runtime / dosproc.scm < prev    next >
Text File  |  1999-02-17  |  4KB  |  121 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: dosproc.scm,v 1.3 1999/02/18 04:14:10 cph Exp $
  4.  
  5. Copyright (c) 1992-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. |#
  21.  
  22. ;;;; Subprocess Support for DOS
  23. ;;; package: (runtime)
  24.  
  25. (declare (usual-integrations))
  26.  
  27. (define run-subprocess
  28.   (let ((prim (make-primitive-procedure 'run-subprocess 4))
  29.     (channel-descriptor
  30.      (access channel-descriptor (->environment '(runtime primitive-io)))))
  31.  
  32.     (lambda (string #!optional stdin stdout stderr)
  33.       (define (run in out err)
  34.     (let ((value (prim string in out err)))
  35.       (cond ((zero? value)
  36.          unspecific)
  37.         ((< value 0)
  38.          (error "run-subprocess: Not available"))
  39.         (else
  40.          (error "run-subprocess: Command failed" value)))))
  41.  
  42.       (define (with-channel-output-port port recvr)
  43.     (call-with-temporary-filename
  44.      (lambda (fname)
  45.        (let ((value
  46.           (call-with-output-file fname
  47.             (lambda (port*)
  48.               (recvr
  49.                (channel-descriptor
  50.             (port/output-channel port*)))))))
  51.          (call-with-input-file fname
  52.            (lambda (input)
  53.          (let ((string (read-string (char-set) input)))
  54.            (if (not (eof-object? string))
  55.                (write-string string
  56.                      port)))))
  57.          value))))
  58.  
  59.       (define (with-channel-input-port port recvr)
  60.     (call-with-temporary-filename
  61.      (lambda (fname)
  62.        (call-with-output-file fname
  63.          (lambda (output)
  64.            (write-string (read-string (char-set) port)
  65.                  output)))
  66.        (call-with-input-file fname
  67.          (lambda (port*)
  68.            (recvr
  69.         (channel-descriptor
  70.          (port/input-channel port*))))))))    
  71.  
  72.       (define (with-output-channel in out)
  73.     (cond ((default-object? stderr)
  74.            (run in out out))
  75.           ((eq? stderr #t)
  76.            (run in out -1))
  77.           ((not (output-port? stderr))
  78.            (error "run: stderr not an output port" stderr))
  79.           ((port/output-channel stderr)
  80.            =>
  81.            (lambda (channel)
  82.          (output-port/flush-output stderr)
  83.          (run in out (channel-descriptor channel))))
  84.           (else
  85.            (with-channel-output-port stdout
  86.          (lambda (err)
  87.            (run in out err))))))
  88.  
  89.       (define (with-input-channel in)
  90.     (let ((stdout
  91.            (if (or (default-object? stdout)
  92.                (not stdout))
  93.            (let ((port (current-output-port)))
  94.              (fresh-line port)
  95.              port)
  96.            stdout)))
  97.       (cond ((eq? stdout #t)
  98.          (with-output-channel in -1))
  99.         ((not (output-port? stdout))
  100.          (error "run: stdout not an output port" stdout))
  101.         ((port/output-channel stdout)
  102.          =>
  103.          (lambda (channel)
  104.            (output-port/flush-output stdout)
  105.            (with-output-channel in (channel-descriptor channel))))
  106.         (else
  107.          (with-channel-output-port stdout
  108.            (lambda (out)
  109.              (with-output-channel in out)))))))
  110.  
  111.       (cond ((or (default-object? stdin)
  112.          (eq? stdin #t))
  113.          (with-input-channel -1))
  114.         ((not (input-port? stdin))
  115.          (error "run: stdin not an input port" stdin))
  116.         ((port/input-channel stdin)
  117.          => (lambda (channel)
  118.           (with-input-channel (channel-descriptor channel))))
  119.         (else
  120.          (with-channel-input-port stdin
  121.            with-input-channel))))))