1999-09-14 09:32:05 -04:00
|
|
|
|
;;; A Scheme shell.
|
|
|
|
|
;;; Copyright (c) 1992 by Olin Shivers.
|
|
|
|
|
;;; Copyright (c) 1994 by Brian D. Carlstrom.
|
|
|
|
|
|
|
|
|
|
;;; Call THUNK, then die.
|
|
|
|
|
;;; A clever definition in a clever implementation allows the caller's stack
|
|
|
|
|
;;; and dynamic env to be gc'd away, since this procedure never returns.
|
|
|
|
|
|
|
|
|
|
(define (call-terminally thunk)
|
2002-04-10 11:03:02 -04:00
|
|
|
|
(with-continuation
|
|
|
|
|
null-continuation
|
|
|
|
|
(lambda ()
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(lambda () (values))
|
|
|
|
|
thunk
|
|
|
|
|
(lambda () (exit 0))))))
|
|
|
|
|
|
|
|
|
|
;; from shift-reset.scm:
|
|
|
|
|
(define null-continuation #f)
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
;;; Like FORK, but the parent and child communicate via a pipe connecting
|
|
|
|
|
;;; the parent's stdin to the child's stdout. This function side-effects
|
|
|
|
|
;;; the parent by changing his stdin.
|
|
|
|
|
|
2002-04-21 12:29:16 -04:00
|
|
|
|
(define (fork/pipe . stuff)
|
|
|
|
|
(really-fork/pipe fork stuff))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
2002-04-21 12:29:16 -04:00
|
|
|
|
(define (%fork/pipe . stuff)
|
|
|
|
|
(really-fork/pipe %fork stuff))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
;;; Common code for FORK/PIPE and %FORK/PIPE.
|
2002-04-21 12:29:16 -04:00
|
|
|
|
(define (really-fork/pipe forker rest)
|
|
|
|
|
(let-optionals rest ((maybe-thunk #f)
|
|
|
|
|
(no-new-command-level? #f))
|
|
|
|
|
(receive (r w) (pipe)
|
|
|
|
|
(let ((proc (forker #f no-new-command-level?)))
|
|
|
|
|
(cond (proc ; Parent
|
|
|
|
|
(close w)
|
|
|
|
|
(move->fdes r 0))
|
|
|
|
|
(else ; Child
|
|
|
|
|
(close r)
|
|
|
|
|
(move->fdes w 1)
|
|
|
|
|
(if maybe-thunk
|
|
|
|
|
(call-terminally maybe-thunk))))
|
|
|
|
|
proc))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; FORK/PIPE with a connection list.
|
|
|
|
|
;;; (FORK/PIPE . m-t) = (apply fork/pipe+ '((1 0)) m-t)
|
|
|
|
|
|
2002-04-21 12:29:16 -04:00
|
|
|
|
(define (%fork/pipe+ conns . stuff)
|
|
|
|
|
(really-fork/pipe+ %fork conns stuff))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
2002-04-21 12:29:16 -04:00
|
|
|
|
(define (fork/pipe+ conns . stuff)
|
|
|
|
|
(really-fork/pipe+ fork conns stuff))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
;;; Common code.
|
2002-04-21 12:29:16 -04:00
|
|
|
|
(define (really-fork/pipe+ forker conns rest)
|
|
|
|
|
(let-optionals rest ((maybe-thunk #f)
|
|
|
|
|
(no-new-command-level? #f))
|
|
|
|
|
(let* ((pipes (map (lambda (conn) (call-with-values pipe cons))
|
|
|
|
|
conns))
|
|
|
|
|
(rev-conns (map reverse conns))
|
|
|
|
|
(froms (map (lambda (conn) (reverse (cdr conn)))
|
|
|
|
|
rev-conns))
|
|
|
|
|
(tos (map car rev-conns)))
|
|
|
|
|
|
|
|
|
|
(let ((proc (forker #f no-new-command-level?)))
|
|
|
|
|
(cond (proc ; Parent
|
|
|
|
|
(for-each (lambda (to r/w)
|
|
|
|
|
(let ((w (cdr r/w))
|
|
|
|
|
(r (car r/w)))
|
|
|
|
|
(close w)
|
|
|
|
|
(move->fdes r to)))
|
|
|
|
|
tos pipes))
|
|
|
|
|
|
|
|
|
|
(else ; Child
|
|
|
|
|
(for-each (lambda (from r/w)
|
|
|
|
|
(let ((r (car r/w))
|
|
|
|
|
(w (cdr r/w)))
|
|
|
|
|
(close r)
|
|
|
|
|
(for-each (lambda (fd) (dup w fd)) from)
|
|
|
|
|
(close w))) ; Unrevealed ports win.
|
|
|
|
|
froms pipes)
|
|
|
|
|
(if (pair? maybe-thunk)
|
|
|
|
|
(call-terminally (car maybe-thunk)))))
|
|
|
|
|
proc))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
(define (tail-pipe a b)
|
|
|
|
|
(fork/pipe a)
|
|
|
|
|
(call-terminally b))
|
|
|
|
|
|
|
|
|
|
(define (tail-pipe+ conns a b)
|
|
|
|
|
(fork/pipe+ conns a)
|
|
|
|
|
(call-terminally b))
|
|
|
|
|
|
|
|
|
|
;;; Lay a pipeline, one process for each thunk. Last thunk is called
|
|
|
|
|
;;; in this process. PIPE* never returns.
|
|
|
|
|
|
|
|
|
|
(define (pipe* . thunks)
|
|
|
|
|
(letrec ((lay-pipe (lambda (thunks)
|
|
|
|
|
(let ((thunk (car thunks))
|
|
|
|
|
(thunks (cdr thunks)))
|
|
|
|
|
(if (pair? thunks)
|
|
|
|
|
(begin (fork/pipe thunk)
|
|
|
|
|
(lay-pipe thunks))
|
|
|
|
|
(call-terminally thunk)))))) ; Last one.
|
|
|
|
|
(if (pair? thunks)
|
|
|
|
|
(lay-pipe thunks)
|
|
|
|
|
(error "No thunks passed to PIPE*"))))
|
|
|
|
|
|
|
|
|
|
;;; Splice the processes into the i/o flow upstream from us.
|
|
|
|
|
;;; First thunk's process reads from our stdin; last thunk's process'
|
|
|
|
|
;;; output becomes our new stdin. Essentially, n-ary fork/pipe.
|
|
|
|
|
;;;
|
|
|
|
|
;;; This procedure is so trivial it isn't included.
|
|
|
|
|
;;; (define (pipe-splice . thunks) (for-each fork/pipe thunks))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-09 17:23:04 -04:00
|
|
|
|
;;; Should be moved to somewhere else
|
|
|
|
|
(define (with-lock lock thunk)
|
2001-12-07 06:28:37 -05:00
|
|
|
|
(dynamic-wind
|
|
|
|
|
(lambda ()
|
|
|
|
|
(release-lock lock))
|
|
|
|
|
thunk
|
|
|
|
|
(lambda ()
|
|
|
|
|
(release-lock lock))))
|
|
|
|
|
|
2001-08-06 04:33:24 -04:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2002-08-13 02:49:22 -04:00
|
|
|
|
;;; working directory per thread
|
2001-12-05 03:34:37 -05:00
|
|
|
|
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(define *cwd-cache* 'uninitialized)
|
|
|
|
|
(define cwd-lock (make-lock))
|
|
|
|
|
|
|
|
|
|
(define (initialize-cwd)
|
|
|
|
|
(set! *cwd-cache* (process-cwd))
|
|
|
|
|
(set! $cwd ;;; TODO The old thread-fluid will remain
|
|
|
|
|
(make-preserved-thread-fluid
|
|
|
|
|
(cwd-cache))))
|
|
|
|
|
; (set! cwd-lock (make-lock)))
|
|
|
|
|
|
|
|
|
|
(define (cwd-cache)
|
|
|
|
|
*cwd-cache*)
|
|
|
|
|
|
|
|
|
|
;; Actually do the syscall and update the cache
|
|
|
|
|
;; assumes the cwd lock obtained
|
|
|
|
|
(define (change-and-cache-cwd new-cwd)
|
|
|
|
|
(if (not (file-name-absolute? new-cwd))
|
|
|
|
|
(process-chdir (string-append (cwd) "/" new-cwd))
|
|
|
|
|
(process-chdir new-cwd))
|
|
|
|
|
(set! *cwd-cache* (process-cwd)))
|
|
|
|
|
|
|
|
|
|
;; The thread-specific cwd: A thread fluid
|
|
|
|
|
|
|
|
|
|
(define $cwd 'empty-cwd-value)
|
|
|
|
|
|
|
|
|
|
(define (cwd) (thread-fluid $cwd))
|
|
|
|
|
(define (thread-set-cwd! cwd) (set-thread-fluid! $cwd cwd))
|
|
|
|
|
(define (let-cwd cwd thunk)
|
|
|
|
|
(let-thread-fluid $cwd cwd thunk))
|
|
|
|
|
|
|
|
|
|
(define (with-cwd* new-cwd thunk)
|
|
|
|
|
(let ((changed-cwd
|
|
|
|
|
(with-lock cwd-lock
|
|
|
|
|
(lambda ()
|
|
|
|
|
(change-and-cache-cwd new-cwd)
|
|
|
|
|
(cwd-cache)))))
|
|
|
|
|
(let-cwd changed-cwd thunk)))
|
|
|
|
|
|
|
|
|
|
;; Align the value of the Unix cwd with scsh's value.
|
|
|
|
|
;; Since another thread could disalign, this call and
|
|
|
|
|
;; any ensuring syscall that relies upon it should
|
|
|
|
|
;; be "glued together" with the cwd lock.
|
|
|
|
|
|
|
|
|
|
(define (align-cwd!)
|
|
|
|
|
(let ((thread-cwd (cwd)))
|
|
|
|
|
(if (not (string=? thread-cwd (cwd-cache)))
|
|
|
|
|
(change-and-cache-cwd thread-cwd))))
|
|
|
|
|
|
|
|
|
|
(define (chdir cwd)
|
|
|
|
|
(with-lock cwd-lock
|
|
|
|
|
(lambda ()
|
|
|
|
|
(change-and-cache-cwd cwd)
|
|
|
|
|
(thread-set-cwd! (cwd-cache)))))
|
|
|
|
|
|
|
|
|
|
(define-record-type resource :resource
|
|
|
|
|
(make-resource align! lock)
|
|
|
|
|
resource?
|
|
|
|
|
(align! resource-align!)
|
|
|
|
|
(lock resource-lock))
|
|
|
|
|
|
|
|
|
|
(define (with-resources-aligned resources thunk)
|
|
|
|
|
(let ((locks (map resource-lock resources)))
|
|
|
|
|
(apply obtain-all-or-none locks)
|
|
|
|
|
(for-each
|
|
|
|
|
(lambda (align!) (align!))
|
|
|
|
|
(map resource-align! resources))
|
|
|
|
|
(let ((val (with-handler
|
|
|
|
|
(lambda (cond more)
|
|
|
|
|
(for-each release-lock locks)
|
|
|
|
|
(more))
|
|
|
|
|
thunk)))
|
|
|
|
|
(for-each release-lock locks)
|
|
|
|
|
val)))
|
|
|
|
|
|
|
|
|
|
(define cwd-resource (make-resource align-cwd! cwd-lock))
|
|
|
|
|
|
|
|
|
|
;; example syscall
|
|
|
|
|
;; (define (exported-delete-file fname)
|
|
|
|
|
;; (with-cwd-aligned (really-delete-file fname)))
|
|
|
|
|
|
|
|
|
|
(define cwd-reinitializer
|
|
|
|
|
(make-reinitializer (lambda () (warn "calling resumer") (initialize-cwd))))
|
2001-07-09 14:29:26 -04:00
|
|
|
|
|
|
|
|
|
|
2001-07-09 17:23:04 -04:00
|
|
|
|
(initialize-cwd)
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
;;; umask per thread
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(define *umask-cache* 'uninitialized)
|
|
|
|
|
(define umask-lock (make-lock))
|
|
|
|
|
|
|
|
|
|
(define (initialize-umask)
|
|
|
|
|
(set! *umask-cache* (process-umask))
|
|
|
|
|
(set! $umask ;;; TODO The old thread-fluid will remain
|
|
|
|
|
(make-preserved-thread-fluid
|
|
|
|
|
(umask-cache))))
|
|
|
|
|
; (set! umask-lock (make-lock)))
|
|
|
|
|
|
|
|
|
|
(define (umask-cache)
|
|
|
|
|
*umask-cache*)
|
|
|
|
|
|
|
|
|
|
;; Actually do the syscall and update the cache
|
|
|
|
|
;; assumes the resource lock obtained
|
|
|
|
|
(define (change-and-cache-umask new-umask)
|
|
|
|
|
(set-process-umask new-umask)
|
|
|
|
|
(set! *umask-cache* (process-umask)))
|
|
|
|
|
|
|
|
|
|
;; The thread-specific umask: A thread fluid
|
|
|
|
|
|
|
|
|
|
(define $umask 'empty-umask-value)
|
|
|
|
|
|
|
|
|
|
(define (umask) (thread-fluid $umask))
|
|
|
|
|
(define (thread-set-umask! new-umask) (set-thread-fluid! $umask new-umask))
|
|
|
|
|
(define (let-umask new-umask thunk)
|
|
|
|
|
(let-thread-fluid $umask new-umask thunk))
|
|
|
|
|
|
|
|
|
|
(define (with-umask* new-umask thunk)
|
|
|
|
|
(let ((changed-umask
|
|
|
|
|
(with-lock umask-lock
|
|
|
|
|
(lambda ()
|
|
|
|
|
(change-and-cache-umask new-umask)
|
|
|
|
|
(umask-cache)))))
|
|
|
|
|
(let-umask changed-umask thunk)))
|
|
|
|
|
|
|
|
|
|
;; Align the value of the Unix umask with scsh's value.
|
|
|
|
|
;; Since another thread could disalign, this call and
|
|
|
|
|
;; any ensuring syscall that relies upon it should
|
|
|
|
|
;; be "glued together" with the umask lock.
|
|
|
|
|
|
|
|
|
|
(define (align-umask!)
|
|
|
|
|
(let ((thread-umask (umask)))
|
|
|
|
|
(if (not (= thread-umask (umask-cache)))
|
|
|
|
|
(change-and-cache-umask thread-umask))))
|
|
|
|
|
|
|
|
|
|
(define (set-umask new-umask)
|
|
|
|
|
(with-lock umask-lock
|
|
|
|
|
(lambda ()
|
|
|
|
|
(change-and-cache-umask new-umask)
|
|
|
|
|
(thread-set-umask! (umask-cache)))))
|
|
|
|
|
|
|
|
|
|
(define umask-resource (make-resource align-umask! umask-lock))
|
|
|
|
|
|
|
|
|
|
;; example syscall
|
|
|
|
|
;; (define (exported-delete-file fname)
|
|
|
|
|
;; (with-cwd-aligned (really-delete-file fname)))
|
|
|
|
|
|
|
|
|
|
(define umask-reinitializer
|
|
|
|
|
(make-reinitializer (lambda () (warn "calling resumer") (initialize-umask))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
2001-07-09 17:23:04 -04:00
|
|
|
|
|
|
|
|
|
(initialize-umask)
|
2001-08-06 04:33:24 -04:00
|
|
|
|
|
2002-08-13 02:49:22 -04:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
;;; ensure S48 is aligned too
|
|
|
|
|
|
|
|
|
|
(set-with-fs-context-aligned*!
|
2001-12-19 04:14:15 -05:00
|
|
|
|
(lambda (thunk)
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(with-resources-aligned (list cwd-resource umask-resource) thunk)))
|
2001-08-06 04:33:24 -04:00
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
;;; Environment per thread
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(define *env-cache* 'uninitialized)
|
|
|
|
|
(define env-lock (make-lock))
|
|
|
|
|
|
|
|
|
|
(define (install-env)
|
|
|
|
|
(set! *env-cache* (environ**-read))
|
|
|
|
|
(set! $env ;;; TODO The old thread-fluid will remain
|
|
|
|
|
(make-preserved-thread-fluid
|
|
|
|
|
(env-cache))))
|
|
|
|
|
; (set! env-lock (make-lock)))
|
|
|
|
|
|
|
|
|
|
(define (env-cache)
|
|
|
|
|
*env-cache*)
|
|
|
|
|
|
|
|
|
|
;; Actually do the syscall and update the cache
|
|
|
|
|
;; assumes the env lock obtained
|
|
|
|
|
(define (change-and-cache-env env)
|
|
|
|
|
(environ**-set env)
|
|
|
|
|
(set! *env-cache* env))
|
|
|
|
|
|
|
|
|
|
;; The thread-specific env: A thread fluid
|
|
|
|
|
|
|
|
|
|
(define $env 'empty-env-value)
|
|
|
|
|
|
|
|
|
|
(define (thread-read-env) (thread-fluid $env))
|
|
|
|
|
(define (thread-set-env! res) (set-thread-fluid! $env res))
|
|
|
|
|
(define (let-env res thunk)
|
|
|
|
|
(let-thread-fluid $env res thunk))
|
|
|
|
|
|
|
|
|
|
(define (really-with-env* env thunk)
|
|
|
|
|
(with-lock env-lock
|
|
|
|
|
(lambda ()
|
|
|
|
|
(change-and-cache-env env)))
|
|
|
|
|
(let-env env thunk))
|
|
|
|
|
|
|
|
|
|
;; Align the value of the Unix env with scsh's value.
|
|
|
|
|
;; Since another thread could disalign, this call and
|
|
|
|
|
;; any ensuring syscall that relies upon it should
|
|
|
|
|
;; be "glued together" with the env lock.
|
|
|
|
|
|
|
|
|
|
(define (align-env!)
|
|
|
|
|
(let ((res (thread-read-env)))
|
|
|
|
|
(if (not (env=? res (env-cache)))
|
|
|
|
|
(change-and-cache-env res))))
|
|
|
|
|
|
|
|
|
|
(define (thread-change-env res)
|
|
|
|
|
(with-lock env-lock
|
|
|
|
|
(lambda ()
|
|
|
|
|
(change-and-cache-env res)
|
|
|
|
|
(thread-set-env! (env-cache)))))
|
|
|
|
|
|
|
|
|
|
(define environ-resource (make-resource align-env! env-lock))
|
|
|
|
|
|
|
|
|
|
;; example syscall
|
|
|
|
|
;; (define (exported-delete-file fname)
|
|
|
|
|
;; (with-cwd-aligned (really-delete-file fname)))
|
|
|
|
|
|
|
|
|
|
(define env-reinitializer
|
2002-08-16 08:33:00 -04:00
|
|
|
|
(make-reinitializer install-env))
|
2002-08-13 02:49:22 -04:00
|
|
|
|
|
2001-08-06 04:33:24 -04:00
|
|
|
|
|
|
|
|
|
(define-record env
|
|
|
|
|
envvec
|
|
|
|
|
alist) ; Corresponding alist
|
|
|
|
|
|
2001-08-06 05:24:24 -04:00
|
|
|
|
(define-record-resumer type/env
|
|
|
|
|
(lambda (env)
|
|
|
|
|
(set-env:envvec env #f)))
|
2001-08-06 04:33:24 -04:00
|
|
|
|
|
|
|
|
|
(define (env=? e1 e2)
|
|
|
|
|
(and (env:envvec e1)
|
|
|
|
|
(eq? (env:envvec e1)
|
|
|
|
|
(env:envvec e2))))
|
|
|
|
|
|
|
|
|
|
(define-record envvec
|
|
|
|
|
environ ;; char**
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
(define (add-envvec-finalizer! envvec)
|
|
|
|
|
(add-finalizer! envvec envvec-finalizer))
|
|
|
|
|
|
|
|
|
|
(define-exported-binding "envvec-record-type" type/envvec)
|
|
|
|
|
(define-exported-binding "add-envvec-finalizer!" add-envvec-finalizer!)
|
|
|
|
|
|
|
|
|
|
(define (envvec-finalizer envvec)
|
|
|
|
|
(%free-env envvec))
|
|
|
|
|
|
|
|
|
|
(define (environ**-read)
|
|
|
|
|
(let ((alist.envvec (environ-env->alist)))
|
|
|
|
|
(make-env (cdr alist.envvec) (car alist.envvec))))
|
|
|
|
|
|
|
|
|
|
(define (environ**-set env)
|
|
|
|
|
(if (env:envvec env)
|
|
|
|
|
(%align-env (env:envvec env))
|
|
|
|
|
(set-env:envvec env (envvec-alist->env (env:alist env)))))
|
|
|
|
|
|
|
|
|
|
(define (getenv var)
|
|
|
|
|
(let* ((env (thread-read-env))
|
|
|
|
|
(res (assoc var (env:alist env))))
|
|
|
|
|
(if res (cdr res) res)))
|
|
|
|
|
|
|
|
|
|
(define (env->alist)
|
|
|
|
|
(env:alist (thread-read-env)))
|
|
|
|
|
|
|
|
|
|
(define (setenv var val)
|
|
|
|
|
(let* ((env (thread-read-env))
|
2002-04-28 14:47:00 -04:00
|
|
|
|
(alist (if val
|
|
|
|
|
(alist-update
|
|
|
|
|
var
|
|
|
|
|
val
|
|
|
|
|
(env:alist env))
|
|
|
|
|
(alist-delete
|
|
|
|
|
var
|
|
|
|
|
(env:alist env)))))
|
2001-08-06 04:33:24 -04:00
|
|
|
|
(thread-set-env!
|
|
|
|
|
(make-env
|
|
|
|
|
#f
|
2002-04-28 14:47:00 -04:00
|
|
|
|
alist))))
|
2001-08-06 04:33:24 -04:00
|
|
|
|
|
|
|
|
|
(define (alist->env alist)
|
|
|
|
|
(thread-set-env!
|
|
|
|
|
(make-env
|
|
|
|
|
#f
|
|
|
|
|
alist)))
|
|
|
|
|
|
|
|
|
|
(define (with-env* alist-delta thunk)
|
|
|
|
|
(let ((new-env (fold (lambda (key/val alist)
|
|
|
|
|
(alist-update (car key/val) (cdr key/val) alist))
|
|
|
|
|
(env->alist)
|
|
|
|
|
alist-delta)))
|
|
|
|
|
(with-total-env* new-env thunk)))
|
|
|
|
|
|
|
|
|
|
(define (with-total-env* alist thunk)
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(really-with-env* (make-env #f alist) thunk))
|
2001-08-06 04:33:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; These two functions are obsoleted by the more general INFIX-SPLITTER and
|
|
|
|
|
;;; JOIN-STRINGS functions. However, we keep SPLIT-COLON-LIST defined
|
|
|
|
|
;;; internally so the top-level startup code (INIT-SCSH) can use it
|
|
|
|
|
;;; to split up $PATH without requiring the field-splitter or regexp code.
|
|
|
|
|
|
|
|
|
|
(define (split-colon-list clist)
|
|
|
|
|
(let ((len (string-length clist)))
|
|
|
|
|
(if (= 0 len) '() ; Special case "" -> ().
|
|
|
|
|
|
|
|
|
|
;; Main loop.
|
|
|
|
|
(let split ((i 0))
|
|
|
|
|
(cond ((string-index clist #\: i) =>
|
|
|
|
|
(lambda (colon)
|
|
|
|
|
(cons (substring clist i colon)
|
|
|
|
|
(split (+ colon 1)))))
|
|
|
|
|
(else (list (substring clist i len))))))))
|
|
|
|
|
|
|
|
|
|
;;; Unix colon lists typically use colons as separators, which
|
|
|
|
|
;;; is not as clean to deal with as terminators, but that's Unix.
|
|
|
|
|
;;; Note ambiguity: (s-l->c-l '()) = (s-l->c-l '("")) = "".
|
|
|
|
|
|
|
|
|
|
; (define (string-list->colon-list slist)
|
|
|
|
|
; (if (pair? slist)
|
|
|
|
|
; (apply string-append
|
|
|
|
|
; (let colonise ((lis slist)) ; LIS is always
|
|
|
|
|
; (let ((tail (cdr lis))) ; a pair.
|
|
|
|
|
; (cons (car lis)
|
|
|
|
|
; (if (pair? tail)
|
|
|
|
|
; (cons ":" (colonise tail))
|
|
|
|
|
; '())))))
|
|
|
|
|
; "")) ; () case.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (alist-delete key alist)
|
|
|
|
|
(filter (lambda (key/val) (not (equal? key (car key/val)))) alist))
|
|
|
|
|
|
|
|
|
|
(define (alist-update key val alist)
|
|
|
|
|
(cons (cons key val)
|
|
|
|
|
(alist-delete key alist)))
|
|
|
|
|
|
|
|
|
|
;;; Remove shadowed entries from ALIST. Preserves element order.
|
|
|
|
|
;;; (This version shares no structure.)
|
|
|
|
|
|
|
|
|
|
(define (alist-compress alist)
|
|
|
|
|
(reverse (let compress ((alist alist) (ans '()))
|
|
|
|
|
(if (pair? alist)
|
|
|
|
|
(let ((key/val (car alist))
|
|
|
|
|
(alist (cdr alist)))
|
|
|
|
|
(compress alist (if (assoc (car key/val) ans) ans
|
|
|
|
|
(cons key/val ans))))
|
|
|
|
|
ans))))
|
|
|
|
|
|
|
|
|
|
(define (add-before elt before list)
|
|
|
|
|
(let rec ((list list))
|
|
|
|
|
(if (pair? list)
|
|
|
|
|
(let ((x (car list)))
|
|
|
|
|
(if (equal? x before)
|
|
|
|
|
(cons elt list)
|
|
|
|
|
(cons x (rec (cdr list)))))
|
|
|
|
|
(cons elt list))))
|
|
|
|
|
|
|
|
|
|
;;; In ADD-AFTER, the labelled LET adds ELT after the last occurrence of AFTER
|
|
|
|
|
;;; in LIST, and returns the list. However, if the LET finds no occurrence
|
|
|
|
|
;;; of AFTER in LIST, it returns #F instead.
|
|
|
|
|
|
|
|
|
|
(define (add-after elt after list)
|
|
|
|
|
(or (let rec ((list list))
|
|
|
|
|
(if (pair? list)
|
|
|
|
|
(let* ((x (car list))
|
|
|
|
|
(tail (cdr list))
|
|
|
|
|
(ans (rec tail))) ; #f if AFTER wasn't encountered.
|
|
|
|
|
(cond (ans (cons x ans))
|
|
|
|
|
((equal? x after)
|
|
|
|
|
(cons x (cons elt tail)))
|
|
|
|
|
(else #f))) ; AFTER doesn't appear in LIST.
|
|
|
|
|
#f)) ; AFTER doesn't appear in LIST.
|
|
|
|
|
(cons elt list)))
|
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
;;; Sugar:
|
|
|
|
|
|
|
|
|
|
(define-simple-syntax (with-cwd dir . body)
|
|
|
|
|
(with-cwd* dir (lambda () . body)))
|
|
|
|
|
|
|
|
|
|
(define-simple-syntax (with-umask mask . body)
|
|
|
|
|
(with-umask* mask (lambda () . body)))
|
|
|
|
|
|
|
|
|
|
(define-simple-syntax (with-env delta . body)
|
|
|
|
|
(with-env* `delta (lambda () . body)))
|
|
|
|
|
|
|
|
|
|
(define-simple-syntax (with-total-env env . body)
|
|
|
|
|
(with-total-env* `env (lambda () . body)))
|
|
|
|
|
|
1999-11-04 16:40:50 -05:00
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
(define (call/temp-file writer user)
|
|
|
|
|
(let ((fname #f))
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(lambda () (if fname (error "Can't wind back into a CALL/TEMP-FILE")
|
|
|
|
|
(set! fname (create-temp-file))))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(with-output-to-file fname writer)
|
|
|
|
|
(user fname))
|
|
|
|
|
(lambda () (if fname (delete-file fname))))))
|
|
|
|
|
|
|
|
|
|
;;; Create a new temporary file and return its name.
|
|
|
|
|
;;; The optional argument specifies the filename prefix to use, and defaults
|
2000-12-22 09:13:17 -05:00
|
|
|
|
;;; to "/tmp/<pid>.", where <pid> is the current process' id. The procedure
|
1999-09-14 09:32:05 -04:00
|
|
|
|
;;; scans through the files named <prefix>0, <prefix>1, ... until it finds a
|
|
|
|
|
;;; filename that doesn't exist in the filesystem. It creates the file with
|
|
|
|
|
;;; permission #o600, and returns the filename.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define (create-temp-file . maybe-prefix)
|
|
|
|
|
(let ((oflags (bitwise-ior open/write
|
|
|
|
|
(bitwise-ior open/create open/exclusive))))
|
|
|
|
|
(apply temp-file-iterate
|
|
|
|
|
(lambda (fname)
|
|
|
|
|
(close-fdes (open-fdes fname oflags #o600))
|
|
|
|
|
fname)
|
|
|
|
|
(if (null? maybe-prefix) '()
|
2001-06-02 13:45:25 -04:00
|
|
|
|
(list (string-append (constant-format-string (car maybe-prefix))
|
|
|
|
|
".~a"))))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
2001-10-23 04:07:53 -04:00
|
|
|
|
(define (initial-temp-file)
|
|
|
|
|
(let ((tmpdir (getenv "TMPDIR")))
|
|
|
|
|
(string-append
|
|
|
|
|
(if tmpdir
|
|
|
|
|
tmpdir
|
|
|
|
|
"/var/tmp")
|
|
|
|
|
"/"
|
|
|
|
|
(number->string (pid))
|
|
|
|
|
"~a")))
|
|
|
|
|
|
|
|
|
|
(define *temp-file-template* (make-fluid 'not-initialized-temp-file-template))
|
|
|
|
|
|
|
|
|
|
(define temp-file-reinitializer
|
|
|
|
|
(make-reinitializer
|
|
|
|
|
(lambda ()
|
|
|
|
|
(set-fluid! *temp-file-template* (initial-temp-file)))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
(define (temp-file-iterate maker . maybe-template)
|
|
|
|
|
(let ((template (:optional maybe-template (fluid *temp-file-template*))))
|
|
|
|
|
(let loop ((i 0))
|
|
|
|
|
(if (> i 1000) (error "Can't create temp-file")
|
|
|
|
|
(let ((fname (format #f template (number->string i))))
|
|
|
|
|
(receive retvals (with-errno-handler
|
|
|
|
|
((errno data)
|
2002-08-13 02:49:22 -04:00
|
|
|
|
((errno/exist errno/acces) #f))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
(maker fname))
|
|
|
|
|
(if (car retvals) (apply values retvals)
|
|
|
|
|
(loop (+ i 1)))))))))
|
|
|
|
|
|
|
|
|
|
|
2001-06-02 13:45:25 -04:00
|
|
|
|
;; Double tildes in S.
|
|
|
|
|
;; Using the return value as a format string will output exactly S.
|
|
|
|
|
(define (constant-format-string s) ; Ugly code. Would be much clearer
|
|
|
|
|
(let* ((len (string-length s)) ; if written with string SRFI.
|
|
|
|
|
(tilde? (lambda (s i) (char=? #\~ (string-ref s i))))
|
|
|
|
|
(newlen (do ((i (- len 1) (- i 1))
|
|
|
|
|
(ans 0 (+ ans (if (tilde? s i) 2 1))))
|
|
|
|
|
((< i 0) ans)))
|
|
|
|
|
(fs (make-string newlen)))
|
|
|
|
|
(let lp ((i 0) (j 0))
|
|
|
|
|
(cond ((< i len)
|
|
|
|
|
(let ((j (cond ((tilde? s i) (string-set! fs j #\~) (+ j 1))
|
|
|
|
|
(else j))))
|
|
|
|
|
(string-set! fs j (string-ref s i))
|
|
|
|
|
(lp (+ i 1) (+ j 1))))))
|
|
|
|
|
fs))
|
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
;;; Roughly equivalent to (pipe).
|
|
|
|
|
;;; Returns two file ports [iport oport] open on a temp file.
|
|
|
|
|
;;; Use this when you may have to buffer large quantities between
|
|
|
|
|
;;; writing and reading. Note that if the consumer gets ahead of the
|
|
|
|
|
;;; producer, it won't hang waiting for input, it will just return
|
|
|
|
|
;;; EOF. To play it safe, make sure that the producer runs to completion
|
|
|
|
|
;;; before starting the consumer.
|
|
|
|
|
;;;
|
|
|
|
|
;;; The temp file is deleted before TEMP-FILE-CHANNEL returns, so as soon
|
|
|
|
|
;;; as the ports are closed, the file's disk storage is reclaimed.
|
|
|
|
|
|
|
|
|
|
(define (temp-file-channel)
|
|
|
|
|
(let* ((fname (create-temp-file))
|
|
|
|
|
(iport (open-input-file fname))
|
|
|
|
|
(oport (open-output-file fname)))
|
|
|
|
|
(delete-file fname)
|
|
|
|
|
(values iport oport)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Return a Unix port such that reads on it get the chars produced by
|
|
|
|
|
;; DISPLAYing OBJ. For example, if OBJ is a string, then reading from
|
|
|
|
|
;; the port produces the characters of OBJ.
|
|
|
|
|
;;
|
|
|
|
|
;; This implementation works by writing the string out to a temp file,
|
|
|
|
|
;; but that isn't necessary. It could work, for example, by forking off a
|
|
|
|
|
;; writer process that outputs to a pipe, i.e.,
|
|
|
|
|
;; (run/port (begin (display obj (fdes->outport 1))))
|
|
|
|
|
|
|
|
|
|
(define (open-string-source obj)
|
|
|
|
|
(receive (inp outp) (temp-file-channel)
|
|
|
|
|
(display obj outp)
|
|
|
|
|
(close-output-port outp)
|
|
|
|
|
inp))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;; Process->Scheme interface forms: run/collecting, run/port, run/string, ...
|
|
|
|
|
|
|
|
|
|
;;; (run/collecting FDS . EPF)
|
|
|
|
|
;;; --------------------------
|
|
|
|
|
;;; RUN/COLLECTING and RUN/COLLECTING* run processes that produce multiple
|
|
|
|
|
;;; output streams and return ports open on these streams.
|
|
|
|
|
;;;
|
|
|
|
|
;;; To avoid issues of deadlock, RUN/COLLECTING first runs the process
|
|
|
|
|
;;; with output to temp files, then returns the ports open on the temp files.
|
|
|
|
|
;;;
|
|
|
|
|
;;; (run/collecting (1 2) (ls))
|
|
|
|
|
;;; runs ls with stdout (fd 1) and stderr (fd 2) redirected to temporary files.
|
|
|
|
|
;;; When ls is done, RUN/COLLECTING returns two ports open on the temporary
|
|
|
|
|
;;; files. The files are deleted before RUN/COLLECTING returns, so when
|
|
|
|
|
;;; the ports are closed, they vanish.
|
|
|
|
|
;;;
|
|
|
|
|
;;; The FDS list of file descriptors is implicitly backquoted.
|
|
|
|
|
;;;
|
|
|
|
|
;;; RUN/COLLECTING* is the procedural abstraction of RUN/COLLECTING.
|
|
|
|
|
|
|
|
|
|
(define (run/collecting* fds thunk)
|
|
|
|
|
;; First, generate a pair of ports for each communications channel.
|
|
|
|
|
;; Each channel buffers through a temp file.
|
|
|
|
|
(let* ((channels (map (lambda (ignore)
|
|
|
|
|
(call-with-values temp-file-channel cons))
|
|
|
|
|
fds))
|
|
|
|
|
(read-ports (map car channels))
|
|
|
|
|
(write-ports (map cdr channels))
|
|
|
|
|
|
|
|
|
|
;; In a subprocess, close the read ports, redirect input from
|
|
|
|
|
;; the write ports, and run THUNK.
|
2000-06-28 06:30:59 -04:00
|
|
|
|
(status (run (begin (for-each close-input-port read-ports)
|
1999-09-14 09:32:05 -04:00
|
|
|
|
(for-each move->fdes write-ports fds)
|
|
|
|
|
(thunk)))))
|
|
|
|
|
|
|
|
|
|
;; In this process, close the write ports and return the exit status
|
|
|
|
|
;; and all the the read ports.
|
|
|
|
|
(for-each close-output-port write-ports)
|
|
|
|
|
(apply values status read-ports)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Single-stream collectors:
|
|
|
|
|
;;; Syntax: run/port, run/file, run/string, run/strings, run/sexp, run/sexps
|
|
|
|
|
;;; Procedures: run/port*, run/file*, run/string*, run/strings*, run/sexp*,
|
|
|
|
|
;;; run/sexps*
|
|
|
|
|
;;; port->string, port->string-list, port->sexp-list,
|
|
|
|
|
;;; port->list
|
|
|
|
|
;;;
|
|
|
|
|
;;; Syntax:
|
|
|
|
|
;;; (run/port . epf)
|
|
|
|
|
;;; Fork off the process EPF and return a port on its stdout.
|
|
|
|
|
;;; (run/file . epf)
|
|
|
|
|
;;; Run process EPF with stdout redirected into a temp file.
|
|
|
|
|
;;; When the process exits, return the name of the file.
|
|
|
|
|
;;; (run/string . epf)
|
|
|
|
|
;;; Read the process' stdout into a string and return it.
|
|
|
|
|
;;; (run/strings . epf)
|
|
|
|
|
;;; Run process EPF, reading newline-terminated strings from its stdout
|
|
|
|
|
;;; until EOF. After process exits, return list of strings read. Delimiting
|
|
|
|
|
;;; newlines are trimmed from the strings.
|
|
|
|
|
;;; (run/sexp . epf)
|
|
|
|
|
;;; Run process EPF, read and return one sexp from its stdout with READ.
|
|
|
|
|
;;; (run/sexps . epf)
|
|
|
|
|
;;; Run process EPF, read sexps from its stdout with READ until EOF.
|
|
|
|
|
;;; After process exits, return list of items read.
|
|
|
|
|
;;;
|
|
|
|
|
;;; Procedural abstractions:
|
|
|
|
|
;;; run/port*, run/file*, run/string*, run/strings*, run/sexp*, run/sexps*
|
|
|
|
|
;;;
|
|
|
|
|
;;; These are all procedural equivalents for the macros. They all take
|
|
|
|
|
;;; one argument: the process to be executed passed as a thunk. For example,
|
|
|
|
|
;;; (RUN/PORT . epf) expands into (RUN/PORT* (LAMBDA () (EXEC-EPF . epf)))
|
|
|
|
|
;;;
|
|
|
|
|
;;; Other useful procedures:
|
|
|
|
|
;;;
|
|
|
|
|
;;; (port->string port)
|
|
|
|
|
;;; Read characters from port until EOF; return string collected.
|
|
|
|
|
;;; (port->string-list port)
|
|
|
|
|
;;; Read newline-terminated strings from port until EOF. Return
|
|
|
|
|
;;; the list of strings collected.
|
|
|
|
|
;;; (port->sexp-list port)
|
|
|
|
|
;;; Read sexps from port with READ until EOF. Return list of items read.
|
|
|
|
|
;;; (port->list reader port)
|
|
|
|
|
;;; Repeatedly applies READER to PORT, accumulating results into a list.
|
|
|
|
|
;;; On EOF, returns the list of items thus collected.
|
1999-09-24 19:52:32 -04:00
|
|
|
|
;;; (port-fold port reader op . seeds)
|
1999-09-14 09:32:05 -04:00
|
|
|
|
;;; Repeatedly read things from PORT with READER. Each time you read
|
|
|
|
|
;;; some value V, compute a new set of seeds with (apply OP V SEEDS).
|
|
|
|
|
;;; (More than 1 seed means OP must return multiple values).
|
1999-09-24 19:52:32 -04:00
|
|
|
|
;;; On eof, return the seeds: (apply value SEEDS).
|
|
|
|
|
;;; PORT->LIST is just (PORT-FOLD PORT READ CONS '())
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
(define (run/port+proc* thunk)
|
|
|
|
|
(receive (r w) (pipe)
|
|
|
|
|
(let ((proc (fork (lambda ()
|
|
|
|
|
(close r)
|
|
|
|
|
(move->fdes w 1)
|
|
|
|
|
(with-current-output-port* w thunk)))))
|
|
|
|
|
(close w)
|
|
|
|
|
(values r proc))))
|
|
|
|
|
|
|
|
|
|
(define (run/port* thunk)
|
|
|
|
|
(receive (port proc) (run/port+proc* thunk)
|
|
|
|
|
port))
|
|
|
|
|
|
|
|
|
|
(define (run/file* thunk)
|
|
|
|
|
(let ((fname (create-temp-file)))
|
2000-06-28 06:30:59 -04:00
|
|
|
|
(run (begin (thunk)) (> ,fname))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
fname))
|
|
|
|
|
|
|
|
|
|
(define (run/string* thunk)
|
|
|
|
|
(close-after (run/port* thunk) port->string))
|
|
|
|
|
|
|
|
|
|
(define (run/sexp* thunk)
|
|
|
|
|
(close-after (run/port* thunk) read))
|
|
|
|
|
|
|
|
|
|
(define (run/sexps* thunk)
|
|
|
|
|
(close-after (run/port* thunk) port->sexp-list))
|
|
|
|
|
|
|
|
|
|
(define (run/strings* thunk)
|
|
|
|
|
(close-after (run/port* thunk) port->string-list))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Read characters from PORT until EOF, collect into a string.
|
|
|
|
|
|
|
|
|
|
(define (port->string port)
|
|
|
|
|
(let ((sc (make-string-collector)))
|
|
|
|
|
(letrec ((lp (lambda ()
|
|
|
|
|
(cond ((read-string 1024 port) =>
|
|
|
|
|
(lambda (s)
|
|
|
|
|
(collect-string! sc s)
|
|
|
|
|
(lp)))
|
|
|
|
|
(else (string-collector->string sc))))))
|
|
|
|
|
(lp))))
|
|
|
|
|
|
|
|
|
|
;;; (loop (initial (sc (make-string-collector)))
|
|
|
|
|
;;; (bind (s (read-string 1024 port)))
|
|
|
|
|
;;; (while s)
|
|
|
|
|
;;; (do (collect-string! sc s))
|
|
|
|
|
;;; (result (string-collector->string sc)))
|
|
|
|
|
|
|
|
|
|
;;; Read items from PORT with READER until EOF. Collect items into a list.
|
|
|
|
|
|
|
|
|
|
(define (port->list reader port)
|
|
|
|
|
(let lp ((ans '()))
|
|
|
|
|
(let ((x (reader port)))
|
|
|
|
|
(if (eof-object? x) (reverse! ans)
|
|
|
|
|
(lp (cons x ans))))))
|
|
|
|
|
|
|
|
|
|
(define (port->sexp-list port)
|
|
|
|
|
(port->list read port))
|
|
|
|
|
|
|
|
|
|
(define (port->string-list port)
|
|
|
|
|
(port->list read-line port))
|
|
|
|
|
|
1999-09-24 19:52:32 -04:00
|
|
|
|
(define (port-fold port reader op . seeds)
|
|
|
|
|
(letrec ((fold (lambda seeds
|
1999-09-14 09:32:05 -04:00
|
|
|
|
(let ((x (reader port)))
|
|
|
|
|
(if (eof-object? x) (apply values seeds)
|
|
|
|
|
(call-with-values (lambda () (apply op x seeds))
|
1999-09-24 19:52:32 -04:00
|
|
|
|
fold))))))
|
|
|
|
|
(apply fold seeds)))
|
|
|
|
|
|
|
|
|
|
(define reduce-port
|
|
|
|
|
(deprecated-proc port-fold 'reduce-port "Use port-fold instead."))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
;;; Not defined:
|
|
|
|
|
;;; (field-reader field-delims record-delims)
|
|
|
|
|
;;; Returns a reader that reads strings delimited by 1 or more chars from
|
|
|
|
|
;;; the string FIELD-DELIMS. These strings are collected in a list until
|
|
|
|
|
;;; eof or until 1 or more chars from RECORD-DELIMS are read. Then the
|
|
|
|
|
;;; accumulated list of strings is returned. For example, if we want
|
|
|
|
|
;;; a procedure that reads one line of input, splitting it into
|
|
|
|
|
;;; whitespace-delimited strings, we can use
|
|
|
|
|
;;; (field-reader " \t" "\n")
|
|
|
|
|
;;; for a reader.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Loop until EOF reading characters or strings and writing (FILTER char)
|
|
|
|
|
;; or (FILTER string). Useful as an arg to FORK or FORK/PIPE.
|
|
|
|
|
|
|
|
|
|
(define (char-filter filter)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let lp ()
|
|
|
|
|
(let ((c (read-char)))
|
|
|
|
|
(if (not (eof-object? c))
|
|
|
|
|
(begin (write-char (filter c))
|
|
|
|
|
(lp)))))))
|
|
|
|
|
|
|
|
|
|
(define (string-filter filter . maybe-buflen)
|
|
|
|
|
(let* ((buflen (:optional maybe-buflen 1024))
|
|
|
|
|
(buf (make-string buflen)))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let lp ()
|
|
|
|
|
(cond ((read-string! buf 0 buflen) =>
|
|
|
|
|
(lambda (nread)
|
|
|
|
|
(display (filter (if (= nread buflen) buf
|
|
|
|
|
(substring buf 0 nread)))) ; last one.
|
|
|
|
|
(lp))))))))
|
|
|
|
|
|
|
|
|
|
(define (y-or-n? question . maybe-eof-value)
|
|
|
|
|
(let loop ((count *y-or-n-eof-count*))
|
|
|
|
|
(display question)
|
|
|
|
|
(display " (y/n)? ")
|
|
|
|
|
(let ((line (read-line)))
|
|
|
|
|
(cond ((eof-object? line)
|
|
|
|
|
(newline)
|
|
|
|
|
(if (= count 0)
|
|
|
|
|
(:optional maybe-eof-value (error "EOF in y-or-n?"))
|
|
|
|
|
(begin (display "I'll only ask another ")
|
|
|
|
|
(write count)
|
|
|
|
|
(display " times.")
|
|
|
|
|
(newline)
|
|
|
|
|
(loop (- count 1)))))
|
|
|
|
|
((< (string-length line) 1) (loop count))
|
|
|
|
|
((char=? (string-ref line 0) #\y) #t)
|
|
|
|
|
((char=? (string-ref line 0) #\n) #f)
|
|
|
|
|
(else (loop count))))))
|
|
|
|
|
|
|
|
|
|
(define *y-or-n-eof-count* 100)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Stdio/stdport sync procedures
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
|
|
(define (stdio->stdports)
|
|
|
|
|
(set-current-input-port! (fdes->inport 0))
|
|
|
|
|
(set-current-output-port! (fdes->outport 1))
|
|
|
|
|
(set-current-error-port! (fdes->outport 2)))
|
|
|
|
|
|
|
|
|
|
(define (with-stdio-ports* thunk)
|
|
|
|
|
(with-current-input-port (fdes->inport 0)
|
|
|
|
|
(with-current-output-port (fdes->outport 1)
|
|
|
|
|
(with-current-error-port (fdes->outport 2)
|
|
|
|
|
(thunk)))))
|
|
|
|
|
|
|
|
|
|
(define-simple-syntax (with-stdio-ports body ...)
|
|
|
|
|
(with-stdio-ports* (lambda () body ...)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (stdports->stdio)
|
|
|
|
|
(dup (current-input-port) 0)
|
|
|
|
|
(dup (current-output-port) 1)
|
|
|
|
|
(dup (current-error-port) 2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Command-line argument access
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
|
|
;;; Some globals.
|
|
|
|
|
(define %command-line '()) ; Includes program.
|
|
|
|
|
(define command-line-arguments #f) ; Doesn't include program.
|
|
|
|
|
|
|
|
|
|
(define (set-command-line-args! args)
|
|
|
|
|
(set! %command-line args)
|
|
|
|
|
(set! command-line-arguments (append (cdr args) '())))
|
|
|
|
|
|
|
|
|
|
(define (arg* arglist n . maybe-default-thunk)
|
|
|
|
|
(let ((oops (lambda () (error "argument out of bounds" arglist n))))
|
|
|
|
|
(if (< n 1) (oops)
|
|
|
|
|
(let lp ((al arglist) (n n))
|
|
|
|
|
(if (pair? al)
|
|
|
|
|
(if (= n 1) (car al)
|
|
|
|
|
(lp (cdr al) (- n 1)))
|
|
|
|
|
(if (and (pair? maybe-default-thunk)
|
|
|
|
|
(null? (cdr maybe-default-thunk)))
|
|
|
|
|
((car maybe-default-thunk))
|
|
|
|
|
(oops)))))))
|
|
|
|
|
|
|
|
|
|
(define (arg arglist n . maybe-default)
|
|
|
|
|
(if maybe-default (arg* arglist n (lambda () (car maybe-default)))
|
|
|
|
|
(arg* arglist n)))
|
|
|
|
|
|
|
|
|
|
(define (argv n . maybe-default)
|
1999-09-24 19:52:32 -04:00
|
|
|
|
(apply arg %command-line (+ n 1) maybe-default))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
(define (command-line) (append %command-line '()))
|
|
|
|
|
|
|
|
|
|
;;; EXEC support
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
;;; Assumes a low-level %exec procedure:
|
|
|
|
|
;;; (%exec prog arglist env)
|
|
|
|
|
;;; ENV is either #t, meaning the current environment, or a string->string
|
|
|
|
|
;;; alist.
|
|
|
|
|
;;; %EXEC stringifies PROG and the elements of ARGLIST.
|
|
|
|
|
|
|
|
|
|
(define (stringify thing)
|
|
|
|
|
(cond ((string? thing) thing)
|
|
|
|
|
((symbol? thing)
|
|
|
|
|
(symbol->string thing))
|
|
|
|
|
; ((symbol? thing)
|
|
|
|
|
; (list->string (map char-downcase
|
|
|
|
|
; (string->list (symbol->string thing)))))
|
|
|
|
|
((integer? thing)
|
|
|
|
|
(number->string thing))
|
|
|
|
|
(else (error "Can only stringify strings, symbols, and integers."
|
|
|
|
|
thing))))
|
|
|
|
|
|
|
|
|
|
(define (exec-path-search prog path-list)
|
2002-08-14 10:45:33 -04:00
|
|
|
|
(cond
|
|
|
|
|
((not (file-name-absolute? prog))
|
|
|
|
|
(let loop ((path-list path-list))
|
|
|
|
|
(if (not (null? path-list))
|
|
|
|
|
(let* ((dir (car path-list))
|
|
|
|
|
(fname (string-append dir "/" prog)))
|
|
|
|
|
(if (file-executable? fname)
|
|
|
|
|
fname
|
|
|
|
|
(loop (cdr path-list)))))))
|
|
|
|
|
((file-executable? prog)
|
|
|
|
|
prog)
|
|
|
|
|
(else #f)))
|
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
(define (exec/env prog env . arglist)
|
|
|
|
|
(flush-all-ports)
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(with-resources-aligned (list environ-resource cwd-resource umask-resource)
|
1999-11-04 16:40:50 -05:00
|
|
|
|
(lambda ()
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(%exec prog (cons prog arglist) env))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
;(define (exec-path/env prog env . arglist)
|
2001-03-11 13:58:54 -05:00
|
|
|
|
; (cond ((exec-path-search (stringify prog) (fluid exec-path-list)) =>
|
1999-09-14 09:32:05 -04:00
|
|
|
|
; (lambda (binary)
|
|
|
|
|
; (apply exec/env binary env arglist)))
|
|
|
|
|
; (else (error "No executable found." prog arglist))))
|
|
|
|
|
|
|
|
|
|
;;; This procedure is bummed by tying in directly to %%exec/errno
|
|
|
|
|
;;; and pulling some of %exec's code out of the inner loop so that
|
|
|
|
|
;;; the inner loop will be fast. Folks don't like waiting...
|
|
|
|
|
|
|
|
|
|
(define (exec-path/env prog env . arglist)
|
|
|
|
|
(flush-all-ports)
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(with-resources-aligned
|
|
|
|
|
(list environ-resource cwd-resource umask-resource)
|
1999-11-04 16:40:50 -05:00
|
|
|
|
(lambda ()
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(let ((prog (stringify prog)))
|
|
|
|
|
(if (string-index prog #\/)
|
2001-07-09 17:23:04 -04:00
|
|
|
|
|
2002-08-13 02:49:22 -04:00
|
|
|
|
;; Contains a slash -- no path search.
|
|
|
|
|
(%exec prog (cons prog arglist) env)
|
2001-07-09 17:23:04 -04:00
|
|
|
|
|
2002-08-13 02:49:22 -04:00
|
|
|
|
;; Try each directory in PATH-LIST.
|
|
|
|
|
(let ((argv (list->vector (cons prog (map stringify arglist)))))
|
|
|
|
|
(for-each (lambda (dir)
|
|
|
|
|
(let ((binary (string-append dir "/" prog)))
|
|
|
|
|
(%%exec binary argv env)))
|
|
|
|
|
(thread-fluid exec-path-list)))))
|
2001-07-09 17:23:04 -04:00
|
|
|
|
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(error "No executable found." prog arglist))))
|
1999-11-04 16:40:50 -05:00
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
(define (exec-path prog . arglist)
|
|
|
|
|
(apply exec-path/env prog #t arglist))
|
|
|
|
|
|
|
|
|
|
(define (exec prog . arglist)
|
|
|
|
|
(apply exec/env prog #t arglist))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Assumes niladic primitive %%FORK.
|
|
|
|
|
|
2002-04-10 11:03:02 -04:00
|
|
|
|
(define (fork . stuff)
|
1999-09-14 09:32:05 -04:00
|
|
|
|
(flush-all-ports)
|
2002-04-10 11:03:02 -04:00
|
|
|
|
(apply fork-1 #t stuff))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
2002-04-10 11:03:02 -04:00
|
|
|
|
(define (%fork . stuff)
|
|
|
|
|
(apply fork-1 #f stuff))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
2002-04-10 11:03:02 -04:00
|
|
|
|
(define (fork-1 clear-interactive? . rest)
|
|
|
|
|
(let-optionals rest ((maybe-thunk #f)
|
2002-05-03 09:42:36 -04:00
|
|
|
|
(dont-narrow? #f))
|
2002-04-10 11:03:02 -04:00
|
|
|
|
(really-fork clear-interactive?
|
2002-05-03 09:42:36 -04:00
|
|
|
|
(not dont-narrow?)
|
2002-04-10 11:03:02 -04:00
|
|
|
|
maybe-thunk)))
|
1999-11-04 16:40:50 -05:00
|
|
|
|
|
2002-05-06 11:54:38 -04:00
|
|
|
|
(define (preserve-ports thunk)
|
|
|
|
|
(let ((current-input (current-input-port))
|
|
|
|
|
(current-output (current-output-port))
|
|
|
|
|
(current-error (current-error-port)))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(with-current-input-port*
|
|
|
|
|
current-input
|
|
|
|
|
(lambda ()
|
|
|
|
|
(with-current-output-port*
|
|
|
|
|
current-output
|
|
|
|
|
(lambda ()
|
|
|
|
|
(with-current-error-port*
|
|
|
|
|
current-error
|
|
|
|
|
thunk))))))))
|
|
|
|
|
|
2002-05-03 09:42:36 -04:00
|
|
|
|
(define (really-fork clear-interactive? narrow? maybe-thunk)
|
2002-05-15 13:05:02 -04:00
|
|
|
|
(let ((proc #f)
|
|
|
|
|
(maybe-narrow
|
|
|
|
|
(if narrow?
|
|
|
|
|
(lambda (thunk)
|
|
|
|
|
;; narrow loses the thread fluids and the dynamic environment
|
|
|
|
|
(narrow (preserve-ports (preserve-thread-fluids thunk))
|
|
|
|
|
'forking))
|
|
|
|
|
(lambda (thunk) (thunk)))))
|
|
|
|
|
(maybe-narrow
|
|
|
|
|
(lambda ()
|
|
|
|
|
;; There was an atomicity problem/race condition -- if a child
|
|
|
|
|
;; process died after it was forked, but before the scsh fork
|
|
|
|
|
;; procedure could register the child's procobj in the
|
|
|
|
|
;; pid/procobj table, then when the SIGCHLD signal-handler reaped
|
|
|
|
|
;; the process, there would be no procobj for it. We now lock
|
|
|
|
|
;; out interrupts across the %%FORK and NEW-CHILD-PROC
|
|
|
|
|
;; operations.
|
|
|
|
|
|
|
|
|
|
(((structure-ref interrupts with-interrupts-inhibited)
|
|
|
|
|
(lambda ()
|
|
|
|
|
;; with-env-aligned is not neccessary here but it will
|
|
|
|
|
;; create the environ object in the parent process which
|
|
|
|
|
;; could reuse it on further forks
|
2002-08-13 02:49:22 -04:00
|
|
|
|
(let ((pid (with-resources-aligned
|
|
|
|
|
(list environ-resource)
|
2002-05-15 13:05:02 -04:00
|
|
|
|
%%fork)))
|
|
|
|
|
(if (zero? pid)
|
|
|
|
|
;; Child
|
|
|
|
|
(lambda () ; Do all this outside the WITH-INTERRUPTS.
|
|
|
|
|
;; There is no session if parent was started in batch-mode
|
|
|
|
|
(if (and (session-started?) clear-interactive?)
|
|
|
|
|
(set-batch-mode?! #t)) ; Children are non-interactive.
|
|
|
|
|
(if maybe-thunk
|
|
|
|
|
(call-terminally maybe-thunk)))
|
|
|
|
|
;; Parent
|
|
|
|
|
(begin
|
|
|
|
|
(set! proc (new-child-proc pid))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(values))))))))))
|
|
|
|
|
proc))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
(define (exit . maybe-status)
|
|
|
|
|
(flush-all-ports)
|
|
|
|
|
(exit/errno (:optional maybe-status 0))
|
|
|
|
|
(display "The evil undead walk the earth." 2)
|
2001-04-09 03:59:17 -04:00
|
|
|
|
(if #t (error "(exit) returned.")))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; The classic T 2.0 primitive.
|
|
|
|
|
;;; This definition works for procedures running on top of Unix systems.
|
|
|
|
|
(define (halts? proc) #t)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Low-level init absolutely required for any scsh program.
|
|
|
|
|
|
|
|
|
|
(define (init-scsh-hindbrain relink-ff?)
|
2001-04-09 03:59:17 -04:00
|
|
|
|
(if #t (error "call to init-scsh-hindbrain which is dead"))
|
1999-09-24 19:52:32 -04:00
|
|
|
|
; (if relink-ff? (lookup-all-externals)) ; Re-link C calls.
|
1999-11-04 16:40:50 -05:00
|
|
|
|
; (init-fdports!)
|
|
|
|
|
; (%install-unix-scsh-handlers)
|
|
|
|
|
)
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Some globals:
|
|
|
|
|
(define home-directory "")
|
2002-05-07 04:58:53 -04:00
|
|
|
|
(define exec-path-list)
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
(define (init-scsh-vars quietly?)
|
|
|
|
|
(set! home-directory
|
2002-02-06 10:17:19 -05:00
|
|
|
|
(cond ((getenv "HOME") => ensure-file-name-is-nondirectory)
|
|
|
|
|
;; loosing at this point would be really bad, so some
|
|
|
|
|
;; paranoia comes in order
|
|
|
|
|
(else (call-with-current-continuation
|
|
|
|
|
(lambda (k)
|
|
|
|
|
(with-handler
|
|
|
|
|
(lambda (condition more)
|
|
|
|
|
(cond ((not quietly?)
|
|
|
|
|
(display "Starting up with no home directory ($HOME).")
|
|
|
|
|
(newline)))
|
|
|
|
|
(k "/"))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(user-info:home-dir (user-info (user-uid))))))))))
|
|
|
|
|
|
2002-05-07 04:58:53 -04:00
|
|
|
|
(set! exec-path-list
|
|
|
|
|
(make-preserved-thread-fluid
|
|
|
|
|
(cond ((getenv "PATH") => split-colon-list)
|
|
|
|
|
(else (if (not quietly?)
|
|
|
|
|
(warn "Starting up with no path ($PATH)."))
|
|
|
|
|
'())))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; SIGTSTP blows s48 away. ???
|
|
|
|
|
(define (suspend) (signal-process 0 signal/stop))
|
1999-11-04 16:40:50 -05:00
|
|
|
|
|