0.8.7.22:
[sbcl.git] / contrib / sb-posix / macros.lisp
1 (in-package :sb-posix-internal)
2
3 ;;; some explanation may be necessary.  The namestring "[foo]" 
4 ;;; denotes a wild pathname.  When there's a file on the disk whose
5 ;;; Unix name is "[foo]", the appropriate CL namestring for it is
6 ;;; "\\[foo]".  So, don't call NAMESTRING, instead call a function
7 ;;; that gets us the Unix name
8 (defun native-filename (pathname)
9   (let ((directory (pathname-directory pathname))
10         (name (pathname-name pathname))
11         (type (pathname-type pathname)))
12     (with-output-to-string (s nil :element-type 'base-char)
13       (etypecase directory
14         (string (write-string directory s))
15         (list
16          (when (eq (car directory) :absolute)
17            (write-char #\/ s))
18          (dolist (piece (cdr directory))
19            (etypecase piece
20              (string (write-string piece s) (write-char #\/ s))
21              ((member :up) (write-string "../" s))))))
22       (etypecase name
23         (null)
24         (string (write-string name s)))
25       (etypecase type
26         (null)
27         (string (write-char #\. s) (write-string type s))))))
28
29 (define-designator filename c-string
30   (pathname 
31    (native-filename (translate-logical-pathname filename)))
32   (string filename))
33
34 (define-designator file-descriptor (integer 32)
35   (file-stream (sb-sys:fd-stream-fd file-descriptor))
36   (fixnum file-descriptor))
37
38 (define-designator sap-or-nil sb-sys:system-area-pointer
39   (null (sb-sys:int-sap 0))
40   (sb-sys:system-area-pointer sap-or-nil))
41
42 (defun lisp-for-c-symbol (s)
43   (intern (substitute #\- #\_ (string-upcase s)) :sb-posix))
44
45 (defmacro define-call (name return-type error-predicate &rest arguments)
46   (let ((lisp-name (lisp-for-c-symbol name)))
47     (if (sb-fasl::foreign-symbol-address-as-integer-or-nil
48          (sb-vm:extern-alien-name name))
49         `(progn
50           (export ',lisp-name :sb-posix)
51           (declaim (inline ,lisp-name))
52           (defun ,lisp-name ,(mapcar #'car arguments)
53             (let ((r (alien-funcall
54                       (extern-alien
55                        ,name
56                        (function ,return-type
57                                  ,@(mapcar
58                                     (lambda (x)
59                                       (gethash (cadr x) *designator-types* (cadr x)))
60                                     arguments)))
61                       ,@(mapcar (lambda (x)
62                                   (if (nth-value 1 (gethash (cadr x) *designator-types*))
63                                       `(,(intern (symbol-name (cadr x)) :sb-posix)
64                                         ,(car x))
65                                       (car x)))
66                                 arguments))))
67               (if (,error-predicate r) (syscall-error) r))))
68         `(progn
69           (export ',lisp-name :sb-posix)
70           (sb-int:style-warn "Didn't find definition for ~S" ,name)))))