Debian Codegraph @

scheme/ikarus.posix.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus.posix)
18
19 (export
20 posix-fork fork waitpid getpid getppid system file-exists? delete-file
21 nanosleep getenv setenv unsetenv env environ split-file-name
22 file-ctime file-mtime file-real-path current-directory
23 file-regular? file-directory? file-readable? file-writable?
24 file-executable? file-size rename-file file-symbolic-link?
25 make-symbolic-link make-hard-link directory-list make-directory
26 make-directory* delete-directory change-mode kill strerror
27 wstatus-pid wstatus-exit-status wstatus-received-signal
28 sigprocmask sigpending
29 signal-code->signal-name signal-name->signal-code)
30
31 (import
32 (rnrs bytevectors)
33 (except (ikarus)
34 nanosleep posix-fork fork waitpid getpid getppid system file-exists?
35 delete-file getenv setenv unsetenv env environ split-file-name
36 file-ctime file-mtime file-real-path current-directory
37 file-regular? file-directory? file-readable? file-writable?
38 file-executable? file-size rename-file file-symbolic-link?
39 make-symbolic-link make-hard-link directory-list
40 make-directory make-directory* delete-directory change-mode
41 sigprocmask sigpending
42 kill strerror wstatus-pid wstatus-exit-status
43 wstatus-received-signal))
44
45 (define posix-fork
46 (lambda ()
47 (foreign-call "ikrt_fork")))
48
49 (define fork
50 (lambda (parent-proc child-proc)
51 (let ([pid (posix-fork)])
52 (cond
53 [(fx= pid 0) (child-proc)]
54 [(fx< pid 0) (raise/strerror 'fork pid)]
55 [else (parent-proc pid)]))))
56
57 (module (signal-code->signal-name signal-name->signal-code)
58
59 (define signal-names-al
60 ;; From ikarus-process.c
61 '((1 . SIGABRT)
62 (2 . SIGALRM)
63 (3 . SIGBUS)
64 (4 . SIGCHLD)
65 (5 . SIGCONT)
66 (6 . SIGFPE)
67 (7 . SIGHUP)
68 (8 . SIGILL)
69 (9 . SIGINT)
70 (10 . SIGKILL)
71 (11 . SIGPIPE)
72 (12 . SIGQUIT)
73 (13 . SIGSEGV)
74 (14 . SIGSTOP)
75 (15 . SIGTERM)
76 (16 . SIGTSTP)
77 (17 . SIGTTIN)
78 (18 . SIGTTOU)
79 (19 . SIGUSR1)
80 (20 . SIGUSR2)
81 (21 . SIGPOLL)
82 (22 . SIGPROF)
83 (23 . SIGSYS)
84 (24 . SIGTRAP)
85 (25 . SIGURG)
86 (26 . SIGVTALRM)
87 (27 . SIGXCPU)
88 (28 . SIGXFSZ)))
89
90 (define signal-code->signal-name
91 (lambda (sigcode)
92 (cond
93 [(assv sigcode signal-names-al) => cdr]
94 [else sigcode])))
95
96 (define signal-name->signal-code
97 (lambda (signame)
98 (cond
99 [(find (lambda (p) (eqv? (cdr p) signame)) signal-names-al) => car]
100 [else #f])))
101 )
102
103 (define sigprocmask
104 (case-lambda
105 ((how signals get-old?)
106 (define (oops . a) (apply die 'sigprocmask a))
107 (cond ((and how signals)
108 (unless (list? signals)
109 (oops "not a list" signals)))
110 ((or how signals)
111 (oops "both must be false" how signals)))
112 (let ((r (foreign-call "ik_sigprocmask"
113 (case how ((add) 1) ((set) 2) ((remove) 3) ((#F) #F)
114 (else (oops "invalid how" how)))
115 (and signals (map (lambda (s)
116 (or (signal-name->signal-code s)
117 (oops "invalid signal name" s)))
118 signals))
119 get-old?)))
120 (cond ((fixnum? r)
121 (raise/strerror 'sigprocmask r))
122 (get-old?
123 (map signal-code->signal-name r)))))
124 ((how signals)
125 (sigprocmask how signals #F))
126 (()
127 (sigprocmask #F #F #T))))
128
129 (define sigpending
130 (lambda ()
131 (map signal-code->signal-name (foreign-call "ik_sigpending"))))
132
133 (define kill
134 (lambda (pid signame)
135 (define who 'kill)
136 (unless (fixnum? pid) (die who "not a fixnum" pid))
137 (unless (symbol? signame) (die who "not a symbol" signame))
138 (let ([r (foreign-call "ikrt_kill" pid
139 (or (signal-name->signal-code signame)
140 (die who "invalid signal name" signame)))])
141 (when (fx< r 0)
142 (error who (strerror r) pid signame)))))
143
144 (define-struct wstatus (pid exit-status received-signal))
145
146 (define waitpid
147 ;; If block? is #f and waitpid() would have blocked,
148 ;; or if want-error? is #f and there was an error,
149 ;; the value returned is #f
150 (case-lambda
151 [() (waitpid -1 #t #t)]
152 [(pid) (waitpid pid #t #t)]
153 [(pid block?) (waitpid pid block? #t)]
154 [(pid block? want-error?)
155 (define who 'waitpid)
156 (unless (fixnum? pid) (die who "not a fixnum" pid))
157 (unless (boolean? block?) (die who "not a boolean" block?))
158 (let ([r (foreign-call "ikrt_waitpid" (make-wstatus #f #f #f) pid block?)])
159 (cond
160 [(wstatus? r)
161 (set-wstatus-received-signal! r
162 (signal-code->signal-name
163 (wstatus-received-signal r)))
164 r]
165 [(and want-error? (not (eqv? r 0)))
166 (error who (strerror r) pid)]
167 [else #f]))]))
168 (define getpid
169 (lambda ()
170 (foreign-call "ikrt_getpid")))
171
172 (define getppid
173 (lambda ()
174 (foreign-call "ikrt_getppid")))
175
176 (define system
177 (lambda (x)
178 (unless (string? x)
179 (die 'system "not a string" x))
180 (let ([rv (foreign-call "ik_system"
181 (string->utf8 x))])
182 (if (fx< rv 0)
183 (raise/strerror 'system rv)
184 rv))))
185
186 (define stat
187 (lambda (path follow who)
188 (unless (string? path)
189 (die who "not a string" path))
190 (let ([r (foreign-call "ikrt_stat" (string->utf8 path) follow)])
191 (case r
192 [(0) 'unknown]
193 [(1) 'regular]
194 [(2) 'directory]
195 [(3) 'symlink]
196 [(-45) #f] ;; from ikarus-errno.c: ENOENT -- path does not exist
197 [(-57) #f] ;; from ikarus-errno.c: ENOTDIR -- path does not exist
198 [else (raise/strerror who r path)]))))
199
200 (define (split-file-name str)
201 (define who 'split-file-name)
202 (define path-sep #\/)
203 (define (find-last c str)
204 (let f ([i (string-length str)])
205 (if (fx=? i 0)
206 #f
207 (let ([i (fx- i 1)])
208 (if (char=? (string-ref str i) c)
209 i
210 (f i))))))
211 (unless (string? str) (die who "not a string" str))
212 (cond
213 [(find-last path-sep str) =>
214 (lambda (i)
215 (values
216 (substring str 0 i)
217 (let ([i (fx+ i 1)])
218 (substring str i (string-length str) ))))]
219 [else (values "" str)]))
220
221 (define access
222 (lambda (path how who)
223 (unless (string? path)
224 (die who "not a string" path))
225 (let ([r (foreign-call "ikrt_access" (string->utf8 path) how)])
226 (unless (boolean? r) (raise/strerror who r path))
227 r)))
228
229 (define file-exists?
230 (case-lambda
231 [(path) (file-exists? path #t)]
232 [(path follow)
233 (and (stat path follow 'file-exists?) #t)]))
234
235 (define file-regular?
236 (case-lambda
237 [(path) (file-regular? path #t)]
238 [(path follow)
239 (eq? 'regular (stat path follow 'file-regular?))]))
240
241 (define file-directory?
242 (case-lambda
243 [(path) (file-directory? path #t)]
244 [(path follow)
245 (eq? 'directory (stat path follow 'file-directory?))]))
246
247 (define file-symbolic-link?
248 (lambda (path)
249 (eq? 'symlink (stat path #f 'file-symbolic-link?))))
250
251 (define file-readable?
252 (lambda (path)
253 (access path 1 'file-readable?)))
254
255 (define file-writable?
256 (lambda (path)
257 (access path 2 'file-writable?)))
258
259 (define file-executable?
260 (lambda (path)
261 (access path 4 'file-executable?)))
262
263 (define file-size
264 (lambda (path)
265 (define who 'file-size)
266 (unless (string? path)
267 (die who "filename is not a string" path))
268 (let* ([v (foreign-call "ikrt_file_size" (string->utf8 path))])
269 (if (>= v 0)
270 v
271 (raise/strerror who v path)))))
272
273 (define delete-file
274 (lambda (x)
275 (define who 'delete-file)
276 (unless (string? x)
277 (die who "filename is not a string" x))
278 (let ([v (foreign-call "ikrt_delete_file"
279 (string->utf8 x))])
280 (unless (eq? v #t)
281 (raise/strerror who v x)))))
282
283 (define rename-file
284 (lambda (src dst)
285 (define who 'rename-file)
286 (unless (string? src)
287 (die who "source file name is not a string" src))
288 (unless (string? dst)
289 (die who "destination file name is not a string" dst))
290 (let ([v (foreign-call "ikrt_rename_file"
291 (string->utf8 src)
292 (string->utf8 dst))])
293 (unless (eq? v #t)
294 (raise/strerror who v src)))))
295
296 (define directory-list
297 (lambda (path)
298 (define who 'directory-list)
299 (unless (string? path)
300 (die who "not a string" path))
301 (let ([r (foreign-call "ikrt_directory_list" (string->utf8 path))])
302 (if (fixnum? r)
303 (raise/strerror who r path)
304 (map utf8->string (reverse r))))))
305
306 (define ($make-directory path mode who)
307 (unless (string? path)
308 (die who "not a string" path))
309 (unless (fixnum? mode)
310 (die who "not a fixnum" mode))
311 (let ([r (foreign-call "ikrt_mkdir" (string->utf8 path) mode)])
312 (unless (eq? r #t)
313 (raise/strerror who r path))))
314
315 (define default-dir-mode #o755)
316
317 (define make-directory
318 (case-lambda
319 [(path) (make-directory path default-dir-mode)]
320 [(path mode) ($make-directory path mode 'make-directory)]))
321
322 (module (make-directory*)
323 (define who 'make-directory*)
324 (define (mkdir* dirname0 mode)
325 (unless (string? dirname0)
326 (die who "not a string" dirname0))
327 (let f ([dirname dirname0])
328 (cond
329 [(file-exists? dirname)
330 (unless (file-directory? dirname)
331 (die who
332 (format "path component ~a is not a directory" dirname)
333 dirname0))]
334 [else
335 (let-values ([(base suffix) (split-file-name dirname)])
336 (unless (string=? base "") (f base))
337 (unless (string=? suffix "")
338 ($make-directory dirname mode who)))])))
339 (define make-directory*
340 (case-lambda
341 [(name) (mkdir* name default-dir-mode)]
342 [(name mode) (mkdir* name mode)])))
343
344 (define delete-directory
345 (case-lambda
346 [(path) (delete-directory path #f)]
347 [(path want-error?)
348 (define who 'delete-directory)
349 (unless (string? path)
350 (die who "not a string" path))
351 (let ([r (foreign-call "ikrt_rmdir" (string->utf8 path))])
352 (if want-error?
353 (unless (eq? r #t) (raise/strerror who r path))
354 (eq? r #t)))]))
355
356 (define change-mode
357 (lambda (path mode)
358 (define who 'change-mode)
359 (unless (string? path)
360 (die who "not a string" path))
361 (unless (fixnum? mode)
362 (die who "not a fixnum" mode))
363 (let ([r (foreign-call "ikrt_chmod" (string->utf8 path) mode)])
364 (unless (eq? r #t)
365 (raise/strerror who r path)))))
366
367 (define ($make-link to path who proc)
368 (unless (and (string? to) (string? path))
369 (die who "not a string" (if (string? to) path to)))
370 (let ([r (proc (string->utf8 to) (string->utf8 path))])
371 (unless (eq? r #t)
372 (raise/strerror who r path))))
373
374 (define (make-symbolic-link to path)
375 ($make-link to path 'make-symbolic-link
376 (lambda (u-to u-path)
377 (foreign-call "ikrt_symlink" u-to u-path))))
378
379 (define (make-hard-link to path)
380 ($make-link to path 'make-hard-link
381 (lambda (u-to u-path)
382 (foreign-call "ikrt_link" u-to u-path))))
383
384 (define ($file-time x who proc)
385 (unless (string? x)
386 (die who "not a string" x))
387 (let ([v (proc (string->utf8 x))])
388 (cond
389 [(bytevector? v)
390 (let ([n0 (bytevector-u8-ref v 0)]
391 [n1 (bytevector-u8-ref v 1)])
392 (+ (* (bytevector-uint-ref v 2 (native-endianness) n0)
393 #e1e9)
394 (bytevector-uint-ref v (+ 2 n0) (native-endianness) n1)))]
395 [else (raise/strerror who v x)])))
396
397 (define (file-ctime x)
398 ($file-time x 'file-ctime
399 (lambda (u) (foreign-call "ikrt_file_ctime2" u))))
400
401 (define (file-mtime x)
402 ($file-time x 'file-mtime
403 (lambda (u) (foreign-call "ikrt_file_mtime2" u))))
404
405 (define (file-real-path x)
406 (define who 'file-real-path)
407 (unless (string? x)
408 (die who "not a string" x))
409 (let ([v (foreign-call "ikrt_realpath" (string->utf8 x))])
410 (cond
411 [(bytevector? v)
412 (let ([s (utf8->string v)])
413 (when (or (string=? s "")
414 (not (char=? (string-ref s 0) #\/)))
415 (error who "unexpected value returned from OS" s x))
416 s)]
417 [else (raise/strerror who v x)])))
418
419 (define (getenv key)
420 (define who 'getenv)
421 (define ($getenv-str key)
422 (define ($getenv-bv key)
423 (foreign-call "ikrt_getenv" key))
424 (let ([rv ($getenv-bv (string->utf8 key))])
425 (and rv (utf8->string rv))))
426 (if (string? key)
427 ($getenv-str key)
428 (die who "key is not a string" key)))
429
430 (define ($setenv key val overwrite)
431 (foreign-call "ikrt_setenv"
432 (string->utf8 key) (string->utf8 val) overwrite))
433
434 (define setenv
435 (case-lambda
436 [(key val overwrite)
437 (define who 'setenv)
438 (if (string? key)
439 (if (string? val)
440 (unless ($setenv key val overwrite)
441 (error who "cannot setenv"))
442 (die who "invalid value" val))
443 (die who "invalid key" key))]
444 [(key val) (setenv key val #t)]))
445
446 (define (unsetenv key)
447 (define who 'unsetenv)
448 (if (string? key)
449 (foreign-call "ikrt_unsetenv" (string->utf8 key))
450 (die who "invalid key" key)))
451
452 (define env
453 (let ()
454 (define env
455 (case-lambda
456 [(key)
457 (if (string? key)
458 (foreign-call "ikrt_getenv" key)
459 (die 'env "the key is not a string" key))]
460 [(key val) (env key val #t)]
461 [(key val overwrite?)
462 (if (string? key)
463 (if (string? val)
464 (unless (foreign-call "ikrt_setenv" key val overwrite?)
465 (die 'env "failed" key val))
466 (die 'env "the value is not a string" val))
467 (die 'env "the key is not a string" key))]))
468 (define busted (lambda args (die 'env "BUG: busted!")))
469 busted))
470
471
472 (define environ
473 (lambda ()
474 (map
475 (lambda (bv)
476 (let ([s (utf8->string bv)])
477 (define (loc= s i n)
478 (cond
479 [(fx= i n) i]
480 [(char=? (string-ref s i) #\=) i]
481 [else (loc= s (fx+ i 1) n)]))
482 (let ([n (string-length s)])
483 (let ([i (loc= s 0 n)])
484 (cons (substring s 0 i)
485 (if (fx< (fxadd1 i) n)
486 (substring s (fxadd1 i) n)
487 ""))))))
488 (foreign-call "ikrt_environ"))))
489
490 (define (nanosleep secs nsecs)
491 (import (ikarus system $fx))
492 (unless (cond
493 [(fixnum? secs) ($fx>= secs 0)]
494 [(bignum? secs) (<= 0 secs (- (expt 2 32) 1))]
495 [else (die 'nanosleep "not an exact integer" secs)])
496 (die 'nanosleep "seconds must be a nonnegative integer <=" secs))
497 (unless (cond
498 [(fixnum? nsecs) ($fx>= nsecs 0)]
499 [(bignum? nsecs) (<= 0 nsecs 999999999)]
500 [else (die 'nanosleep "not an exact integer" nsecs)])
501 (die 'nanosleep "nanoseconds must be an integer \
502 in the range 0..999999999" nsecs))
503 (let ([rv (foreign-call "ikrt_nanosleep" secs nsecs)])
504 (unless (eq? rv 0)
505 (error 'nanosleep "failed"))))
506
507
508 (define current-directory
509 (case-lambda
510 [()
511 (let ([v (foreign-call "ikrt_getcwd")])
512 (if (bytevector? v)
513 (utf8->string v)
514 (raise/strerror 'current-directory v)))]
515 [(x)
516 (if (string? x)
517 (let ([rv (foreign-call "ikrt_chdir" (string->utf8 x))])
518 (unless (eq? rv #t)
519 (raise/strerror 'current-directory rv x)))
520 (die 'current-directory "not a string" x))]))
521
522 (define raise/strerror
523 (case-lambda
524 [(who errno-code)
525 (raise/strerror who errno-code #f)]
526 [(who errno-code filename)
527 (raise
528 (condition
529 (make-error)
530 (make-who-condition who)
531 (make-message-condition (strerror errno-code))
532 (if filename
533 (make-i/o-filename-error filename)
534 (condition))))]))
535
536 (define strerror
537 (lambda (errno-code)
538 (define who 'strerror)
539 (unless (fixnum? errno-code)
540 (die who "not a fixnum" errno-code))
541 (let ([emsg (foreign-call "ikrt_strerror" errno-code)])
542 (if emsg
543 (let ([errno-name
544 (foreign-call "ikrt_errno_code_to_name" errno-code)])
545 (assert errno-name)
546 (format "~a: ~a"
547 (utf8->string errno-name)
548 (utf8->string emsg)))
549 (format "Ikarus's ~a: don't know Ikarus errno code ~s"
550 who errno-code)))))
551
552 )