2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-05-05 22:14:06 -04:00
|
|
|
|
2008-05-31 13:43:55 -04:00
|
|
|
(library (ikarus.posix)
|
2008-11-10 23:36:11 -05:00
|
|
|
|
|
|
|
(export
|
|
|
|
posix-fork fork waitpid system file-exists? delete-file
|
|
|
|
nanosleep getenv env environ file-ctime file-mtime
|
|
|
|
current-directory file-regular? file-directory? file-readable?
|
|
|
|
file-writable? file-executable? file-size rename-file
|
|
|
|
file-symbolic-link? make-symbolic-link directory-list
|
|
|
|
make-directory delete-directory change-mode kill strerror
|
|
|
|
wstatus-pid wstatus-exit-status wstatus-received-signal)
|
|
|
|
|
2007-05-05 22:14:06 -04:00
|
|
|
(import
|
2007-10-12 00:33:19 -04:00
|
|
|
(rnrs bytevectors)
|
2007-05-05 22:14:06 -04:00
|
|
|
(except (ikarus)
|
2008-11-10 23:36:11 -05:00
|
|
|
nanosleep posix-fork fork waitpid system file-exists?
|
|
|
|
delete-file getenv env environ file-ctime file-mtime
|
|
|
|
current-directory file-regular? file-directory?
|
|
|
|
file-readable? file-writable? file-executable? file-size
|
|
|
|
rename-file file-symbolic-link? make-symbolic-link
|
|
|
|
directory-list make-directory delete-directory change-mode
|
|
|
|
kill strerror wstatus-pid wstatus-exit-status
|
|
|
|
wstatus-received-signal))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
|
|
|
(define posix-fork
|
|
|
|
(lambda ()
|
|
|
|
(foreign-call "ikrt_fork")))
|
|
|
|
|
|
|
|
(define fork
|
|
|
|
(lambda (parent-proc child-proc)
|
|
|
|
(let ([pid (posix-fork)])
|
|
|
|
(cond
|
|
|
|
[(fx= pid 0) (child-proc)]
|
2008-05-31 13:43:55 -04:00
|
|
|
[(fx< pid 0) (raise/strerror 'fork pid)]
|
2007-05-05 22:14:06 -04:00
|
|
|
[else (parent-proc pid)]))))
|
2008-06-13 08:43:17 -04:00
|
|
|
|
|
|
|
(module (signal-code->signal-name signal-name->signal-code)
|
|
|
|
|
|
|
|
(define signal-names-al
|
|
|
|
;; From ikarus-process.c
|
|
|
|
'((1 . SIGABRT)
|
|
|
|
(2 . SIGALRM)
|
|
|
|
(3 . SIGBUS)
|
|
|
|
(4 . SIGCHLD)
|
|
|
|
(5 . SIGCONT)
|
|
|
|
(6 . SIGFPE)
|
|
|
|
(7 . SIGHUP)
|
|
|
|
(8 . SIGILL)
|
|
|
|
(9 . SIGINT)
|
|
|
|
(10 . SIGKILL)
|
|
|
|
(11 . SIGPIPE)
|
|
|
|
(12 . SIGQUIT)
|
|
|
|
(13 . SIGSEGV)
|
|
|
|
(14 . SIGSTOP)
|
|
|
|
(15 . SIGTERM)
|
|
|
|
(16 . SIGTSTP)
|
|
|
|
(17 . SIGTTIN)
|
|
|
|
(18 . SIGTTOU)
|
|
|
|
(19 . SIGUSR1)
|
|
|
|
(20 . SIGUSR2)
|
|
|
|
(21 . SIGPOLL)
|
|
|
|
(22 . SIGPROF)
|
|
|
|
(23 . SIGSYS)
|
|
|
|
(24 . SIGTRAP)
|
|
|
|
(25 . SIGURG)
|
|
|
|
(26 . SIGVTALRM)
|
|
|
|
(27 . SIGXCPU)
|
|
|
|
(28 . SIGXFSZ)))
|
|
|
|
|
|
|
|
(define signal-code->signal-name
|
|
|
|
(lambda (sigcode)
|
|
|
|
(cond
|
|
|
|
[(assv sigcode signal-names-al) => cdr]
|
|
|
|
[else sigcode])))
|
|
|
|
|
|
|
|
(define signal-name->signal-code
|
|
|
|
(lambda (signame)
|
|
|
|
(cond
|
|
|
|
[(find (lambda (p) (eqv? (cdr p) signame)) signal-names-al) => car]
|
|
|
|
[else #f])))
|
|
|
|
)
|
|
|
|
|
|
|
|
(define kill
|
|
|
|
(lambda (pid signame)
|
|
|
|
(define who 'kill)
|
|
|
|
(unless (fixnum? pid) (die who "not a fixnum" pid))
|
|
|
|
(unless (symbol? signame) (die who "not a symbol" signame))
|
|
|
|
(let ([r (foreign-call "ikrt_kill" pid
|
|
|
|
(or (signal-name->signal-code signame)
|
|
|
|
(die who "invalid signal name" signame)))])
|
|
|
|
(when (fx< r 0)
|
|
|
|
(error who (strerror r) pid signame)))))
|
|
|
|
|
|
|
|
(define-struct wstatus (pid exit-status received-signal))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
|
|
|
(define waitpid
|
2008-06-13 08:43:17 -04:00
|
|
|
;; If block? is #f and waitpid() would have blocked,
|
|
|
|
;; or if want-error? is #f and there was an error,
|
|
|
|
;; the value returned is #f
|
|
|
|
(case-lambda
|
|
|
|
[() (waitpid -1 #t #t)]
|
|
|
|
[(pid) (waitpid pid #t #t)]
|
|
|
|
[(pid block?) (waitpid pid block? #t)]
|
|
|
|
[(pid block? want-error?)
|
|
|
|
(define who 'waitpid)
|
|
|
|
(unless (fixnum? pid) (die who "not a fixnum" pid))
|
|
|
|
(unless (boolean? block?) (die who "not a boolean" block?))
|
|
|
|
(let ([r (foreign-call "ikrt_waitpid" (make-wstatus #f #f #f) pid block?)])
|
|
|
|
(cond
|
|
|
|
[(wstatus? r)
|
|
|
|
(set-wstatus-received-signal! r
|
|
|
|
(signal-code->signal-name
|
|
|
|
(wstatus-received-signal r)))
|
|
|
|
r]
|
2008-06-14 01:11:51 -04:00
|
|
|
[(and want-error? (not (eqv? r 0)))
|
2008-06-13 08:43:17 -04:00
|
|
|
(error who (strerror r) pid)]
|
|
|
|
[else #f]))]))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
|
|
|
(define system
|
|
|
|
(lambda (x)
|
|
|
|
(unless (string? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'system "not a string" x))
|
2007-05-18 22:15:52 -04:00
|
|
|
(let ([rv (foreign-call "ik_system"
|
2007-10-12 00:33:19 -04:00
|
|
|
(string->utf8 x))])
|
2008-05-31 13:43:55 -04:00
|
|
|
(if (fx< rv 0)
|
|
|
|
(raise/strerror 'system rv)
|
2007-05-05 22:14:06 -04:00
|
|
|
rv))))
|
2008-05-31 13:43:55 -04:00
|
|
|
|
|
|
|
(define stat
|
|
|
|
(lambda (path follow who)
|
|
|
|
(unless (string? path)
|
|
|
|
(die who "not a string" path))
|
|
|
|
(let ([r (foreign-call "ikrt_stat" (string->utf8 path) follow)])
|
|
|
|
(case r
|
|
|
|
[(0) 'unknown]
|
|
|
|
[(1) 'regular]
|
|
|
|
[(2) 'directory]
|
|
|
|
[(3) 'symlink]
|
|
|
|
[(-45) #f] ;; from ikarus-errno.c: ENOENT -- path does not exist
|
|
|
|
[else (raise/strerror who r path)]))))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
2008-11-10 23:36:11 -05:00
|
|
|
|
|
|
|
(define access
|
|
|
|
(lambda (path how who)
|
|
|
|
(unless (string? path)
|
|
|
|
(die who "not a string" path))
|
|
|
|
(let ([r (foreign-call "ikrt_access" (string->utf8 path) how)])
|
|
|
|
(unless (boolean? r) (raise/strerror who r path))
|
|
|
|
r)))
|
|
|
|
|
2007-05-05 22:14:06 -04:00
|
|
|
(define file-exists?
|
2008-05-31 13:43:55 -04:00
|
|
|
(case-lambda
|
|
|
|
[(path) (file-exists? path #t)]
|
|
|
|
[(path follow)
|
|
|
|
(and (stat path follow 'file-exists?) #t)]))
|
|
|
|
|
|
|
|
(define file-regular?
|
|
|
|
(case-lambda
|
|
|
|
[(path) (file-regular? path #t)]
|
|
|
|
[(path follow)
|
|
|
|
(eq? 'regular (stat path follow 'file-regular?))]))
|
|
|
|
|
|
|
|
(define file-directory?
|
|
|
|
(case-lambda
|
|
|
|
[(path) (file-directory? path #t)]
|
|
|
|
[(path follow)
|
|
|
|
(eq? 'directory (stat path follow 'file-directory?))]))
|
|
|
|
|
|
|
|
(define file-symbolic-link?
|
|
|
|
(lambda (path)
|
|
|
|
(eq? 'symlink (stat path #f 'file-symbolic-link?))))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
2008-11-10 23:36:11 -05:00
|
|
|
|
|
|
|
(define file-readable?
|
|
|
|
(lambda (path)
|
|
|
|
(access path 1 'file-readable?)))
|
|
|
|
|
|
|
|
(define file-writable?
|
|
|
|
(lambda (path)
|
|
|
|
(access path 2 'file-writable?)))
|
|
|
|
|
|
|
|
(define file-executable?
|
|
|
|
(lambda (path)
|
|
|
|
(access path 4 'file-executable?)))
|
|
|
|
|
|
|
|
(define file-size
|
|
|
|
(lambda (path)
|
|
|
|
(define who 'file-size)
|
|
|
|
(unless (string? path)
|
|
|
|
(die who "filename is not a string" path))
|
|
|
|
(let* ([v (foreign-call "ikrt_file_size" (string->utf8 path))])
|
|
|
|
(if (>= v 0)
|
|
|
|
v
|
|
|
|
(raise/strerror who v path)))))
|
|
|
|
|
2007-05-05 22:14:06 -04:00
|
|
|
(define delete-file
|
|
|
|
(lambda (x)
|
2008-05-31 13:43:55 -04:00
|
|
|
(define who 'delete-file)
|
2007-05-05 22:14:06 -04:00
|
|
|
(unless (string? x)
|
2008-05-31 13:43:55 -04:00
|
|
|
(die who "filename is not a string" x))
|
2007-05-18 22:21:36 -04:00
|
|
|
(let ([v (foreign-call "ikrt_delete_file"
|
2007-10-12 00:33:19 -04:00
|
|
|
(string->utf8 x))])
|
2008-05-31 13:43:55 -04:00
|
|
|
(unless (eq? v #t)
|
|
|
|
(raise/strerror who v x)))))
|
|
|
|
|
2008-11-10 23:36:11 -05:00
|
|
|
|
|
|
|
(define rename-file
|
|
|
|
(lambda (src dst)
|
|
|
|
(define who 'rename-file)
|
|
|
|
(unless (string? src)
|
|
|
|
(die who "source file name is not a string" src))
|
|
|
|
(unless (string? dst)
|
|
|
|
(die who "destination file name is not a string" dst))
|
|
|
|
(let ([v (foreign-call "ikrt_rename_file"
|
|
|
|
(string->utf8 src)
|
|
|
|
(string->utf8 dst))])
|
|
|
|
(unless (eq? v #t)
|
|
|
|
(raise/strerror who v src)))))
|
|
|
|
|
2008-05-31 13:43:55 -04:00
|
|
|
(define directory-list
|
|
|
|
(lambda (path)
|
|
|
|
(define who 'directory-list)
|
|
|
|
(unless (string? path)
|
|
|
|
(die who "not a string" path))
|
|
|
|
(let ([r (foreign-call "ikrt_directory_list" (string->utf8 path))])
|
|
|
|
(if (fixnum? r)
|
|
|
|
(raise/strerror who r path)
|
|
|
|
(map utf8->string (reverse r))))))
|
|
|
|
|
|
|
|
(define make-directory
|
|
|
|
(case-lambda
|
|
|
|
[(path) (make-directory path #o755)]
|
|
|
|
[(path mode)
|
|
|
|
(define who 'make-directory)
|
|
|
|
(unless (string? path)
|
|
|
|
(die who "not a string" path))
|
|
|
|
(unless (fixnum? mode)
|
|
|
|
(die who "not a fixnum" mode))
|
|
|
|
(let ([r (foreign-call "ikrt_mkdir" (string->utf8 path) mode)])
|
|
|
|
(unless (eq? r #t)
|
|
|
|
(raise/strerror who r path)))]))
|
|
|
|
|
|
|
|
(define delete-directory
|
|
|
|
(case-lambda
|
|
|
|
[(path) (delete-directory path #f)]
|
|
|
|
[(path want-error?)
|
|
|
|
(define who 'delete-directory)
|
|
|
|
(unless (string? path)
|
|
|
|
(die who "not a string" path))
|
|
|
|
(let ([r (foreign-call "ikrt_rmdir" (string->utf8 path))])
|
|
|
|
(if want-error?
|
|
|
|
(unless (eq? r #t) (raise/strerror who r path))
|
|
|
|
(eq? r #t)))]))
|
|
|
|
|
|
|
|
(define change-mode
|
|
|
|
(lambda (path mode)
|
|
|
|
(define who 'change-mode)
|
|
|
|
(unless (string? path)
|
|
|
|
(die who "not a string" path))
|
|
|
|
(unless (fixnum? mode)
|
|
|
|
(die who "not a fixnum" mode))
|
|
|
|
(let ([r (foreign-call "ikrt_chmod" (string->utf8 path) mode)])
|
|
|
|
(unless (eq? r #t)
|
|
|
|
(raise/strerror who r path)))))
|
|
|
|
|
|
|
|
(define make-symbolic-link
|
|
|
|
(lambda (to path)
|
|
|
|
(define who 'make-symbolic-link)
|
|
|
|
(unless (and (string? to) (string? path))
|
|
|
|
(die who "not a string" (if (string? to) path to)))
|
|
|
|
(let ([r (foreign-call "ikrt_symlink"
|
|
|
|
(string->utf8 to) (string->utf8 path))])
|
|
|
|
(unless (eq? r #t)
|
|
|
|
(raise/strerror who r path)))))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
2008-11-01 07:28:08 -04:00
|
|
|
(define ($file-time x who proc)
|
2008-02-18 21:58:11 -05:00
|
|
|
(unless (string? x)
|
|
|
|
(die who "not a string" x))
|
2008-11-16 03:59:30 -05:00
|
|
|
(let ([v (proc (string->utf8 x))])
|
|
|
|
(cond
|
|
|
|
[(bytevector? v)
|
|
|
|
(let ([n0 (bytevector-u8-ref v 0)]
|
|
|
|
[n1 (bytevector-u8-ref v 1)])
|
|
|
|
(+ (* (bytevector-uint-ref v 2 (native-endianness) n0)
|
|
|
|
#e1e9)
|
|
|
|
(bytevector-uint-ref v (+ 2 n0) (native-endianness) n1)))]
|
|
|
|
[else (raise/strerror who v x)])))
|
2008-02-18 21:58:11 -05:00
|
|
|
|
2008-11-01 07:28:08 -04:00
|
|
|
(define (file-ctime x)
|
|
|
|
($file-time x 'file-ctime
|
2008-11-16 03:59:30 -05:00
|
|
|
(lambda (u) (foreign-call "ikrt_file_ctime2" u))))
|
2008-11-01 07:28:08 -04:00
|
|
|
|
|
|
|
(define (file-mtime x)
|
|
|
|
($file-time x 'file-mtime
|
2008-11-16 03:59:30 -05:00
|
|
|
(lambda (u) (foreign-call "ikrt_file_mtime2" u))))
|
2008-02-18 21:58:11 -05:00
|
|
|
|
2007-11-19 12:57:50 -05:00
|
|
|
(define ($getenv-bv key)
|
|
|
|
(foreign-call "ikrt_getenv" key))
|
|
|
|
(define ($getenv-str key)
|
|
|
|
(utf8->string ($getenv-bv (string->utf8 key))))
|
|
|
|
|
|
|
|
(define (getenv key)
|
|
|
|
(if (string? key)
|
|
|
|
($getenv-str key)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'getenv "the key is not a string" key)))
|
2007-11-19 12:57:50 -05:00
|
|
|
|
2007-05-05 22:14:06 -04:00
|
|
|
(define env
|
|
|
|
(let ()
|
|
|
|
(define env
|
|
|
|
(case-lambda
|
|
|
|
[(key)
|
|
|
|
(if (string? key)
|
|
|
|
(foreign-call "ikrt_getenv" key)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'env "the key is not a string" key))]
|
2007-05-05 22:14:06 -04:00
|
|
|
[(key val) (env key val #t)]
|
|
|
|
[(key val overwrite?)
|
|
|
|
(if (string? key)
|
|
|
|
(if (string? val)
|
|
|
|
(unless (foreign-call "ikrt_setenv" key val overwrite?)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'env "failed" key val))
|
|
|
|
(die 'env "the value is not a string" val))
|
|
|
|
(die 'env "the key is not a string" key))]))
|
|
|
|
(define busted (lambda args (die 'env "BUG: busted!")))
|
2007-05-18 22:26:22 -04:00
|
|
|
busted))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
2007-05-18 22:26:22 -04:00
|
|
|
|
2007-12-15 08:22:49 -05:00
|
|
|
(define environ (lambda args (die 'environ "busted!")))
|
2007-05-18 22:26:22 -04:00
|
|
|
(define environ^
|
2007-05-05 22:14:06 -04:00
|
|
|
(lambda ()
|
|
|
|
(map
|
|
|
|
(lambda (s)
|
|
|
|
(define (loc= s i n)
|
|
|
|
(cond
|
|
|
|
[(fx= i n) i]
|
|
|
|
[(char=? (string-ref s i) #\=) i]
|
|
|
|
[else (loc= s (fx+ i 1) n)]))
|
|
|
|
(let ([n (string-length s)])
|
|
|
|
(let ([i (loc= s 0 n)])
|
|
|
|
(cons (substring s 0 i)
|
|
|
|
(if (fx< (fxadd1 i) n)
|
|
|
|
(substring s (fxadd1 i) n)
|
|
|
|
"")))))
|
|
|
|
(foreign-call "ikrt_environ"))))
|
2008-04-11 05:36:54 -04:00
|
|
|
|
|
|
|
(define (nanosleep secs nsecs)
|
|
|
|
(import (ikarus system $fx))
|
|
|
|
(unless (cond
|
|
|
|
[(fixnum? secs) ($fx>= secs 0)]
|
|
|
|
[(bignum? secs) (<= 0 secs (- (expt 2 32) 1))]
|
|
|
|
[else (die 'nanosleep "not an exact integer" secs)])
|
|
|
|
(die 'nanosleep "seconds must be a nonnegative integer <=" secs))
|
|
|
|
(unless (cond
|
|
|
|
[(fixnum? nsecs) ($fx>= nsecs 0)]
|
|
|
|
[(bignum? nsecs) (<= 0 nsecs 999999999)]
|
|
|
|
[else (die 'nanosleep "not an exact integer" nsecs)])
|
|
|
|
(die 'nanosleep "nanoseconds must be an integer \
|
|
|
|
in the range 0..999999999" nsecs))
|
|
|
|
(let ([rv (foreign-call "ikrt_nanosleep" secs nsecs)])
|
|
|
|
(unless (eq? rv 0)
|
|
|
|
(error 'nanosleep "failed"))))
|
|
|
|
|
2008-05-03 02:39:49 -04:00
|
|
|
|
|
|
|
(define current-directory
|
|
|
|
(case-lambda
|
|
|
|
[()
|
|
|
|
(let ([v (foreign-call "ikrt_getcwd")])
|
2008-05-31 23:10:17 -04:00
|
|
|
(if (bytevector? v)
|
|
|
|
(utf8->string v)
|
|
|
|
(raise/strerror 'current-directory v)))]
|
2008-05-03 02:39:49 -04:00
|
|
|
[(x)
|
|
|
|
(if (string? x)
|
|
|
|
(let ([rv (foreign-call "ikrt_chdir" (string->utf8 x))])
|
2008-05-31 13:43:55 -04:00
|
|
|
(unless (eq? rv #t)
|
|
|
|
(raise/strerror 'current-directory rv x)))
|
2008-05-03 02:39:49 -04:00
|
|
|
(die 'current-directory "not a string" x))]))
|
2008-05-31 13:43:55 -04:00
|
|
|
|
|
|
|
(define raise/strerror
|
|
|
|
(case-lambda
|
|
|
|
[(who errno-code)
|
|
|
|
(raise/strerror who errno-code #f)]
|
|
|
|
[(who errno-code filename)
|
|
|
|
(raise
|
|
|
|
(condition
|
2008-06-06 11:10:17 -04:00
|
|
|
(make-error)
|
2008-05-31 13:43:55 -04:00
|
|
|
(make-who-condition who)
|
|
|
|
(make-message-condition (strerror errno-code))
|
|
|
|
(if filename
|
|
|
|
(make-i/o-filename-error filename)
|
|
|
|
(condition))))]))
|
|
|
|
|
|
|
|
(define strerror
|
|
|
|
(lambda (errno-code)
|
|
|
|
(define who 'strerror)
|
|
|
|
(unless (fixnum? errno-code)
|
|
|
|
(die who "not a fixnum" errno-code))
|
|
|
|
(let ([emsg (foreign-call "ikrt_strerror" errno-code)])
|
|
|
|
(if emsg
|
|
|
|
(let ([errno-name
|
|
|
|
(foreign-call "ikrt_errno_code_to_name" errno-code)])
|
|
|
|
(assert errno-name)
|
|
|
|
(format "~a: ~a"
|
|
|
|
(utf8->string errno-name)
|
|
|
|
(utf8->string emsg)))
|
|
|
|
(format "Ikarus's ~a: don't know Ikarus errno code ~s"
|
|
|
|
who errno-code)))))
|
2008-05-03 02:39:49 -04:00
|
|
|
|
2007-05-05 22:14:06 -04:00
|
|
|
)
|