1.0.0.25: fix win32 build
[sbcl.git] / contrib / sb-posix / macros.lisp
1 (in-package :sb-posix)
2
3 (define-designator filename c-string
4   (pathname
5    (sb-ext:native-namestring (translate-logical-pathname filename)))
6   (string filename))
7
8 (define-designator file-descriptor (integer 32)
9   (file-stream (sb-sys:fd-stream-fd file-descriptor))
10   (fixnum file-descriptor))
11
12 (define-designator sap-or-nil sb-sys:system-area-pointer
13   (null (sb-sys:int-sap 0))
14   (sb-sys:system-area-pointer sap-or-nil))
15
16 (define-designator alien-pointer-to-anything-or-nil (* t)
17   (null (sb-alien:sap-alien (sb-sys:int-sap 0) (* t)))
18   ((alien (* t)) alien-pointer-to-anything-or-nil))
19
20 (defun lisp-for-c-symbol (name)
21   (etypecase name
22     (list
23      (lisp-for-c-symbol (car name)))
24     (string
25      (let ((root (if (eql #\_ (char name 0)) (subseq name 1) name)))
26        (intern (substitute #\- #\_ (string-upcase root)) :sb-posix)))))
27
28 (defun real-c-name (name)
29   (etypecase name
30     (list
31      (destructuring-bind (name &rest options) name
32
33        (cond #+largefile
34              ((member :largefile options)
35               (format nil "~a_largefile" name))
36              (t
37               name))))
38     (string
39      name)))
40
41 (defmacro define-call-internally (lisp-name c-name return-type error-predicate
42                                   &rest arguments)
43   (if (sb-sys:find-foreign-symbol-address c-name)
44       `(progn
45         (declaim (inline ,lisp-name))
46         (defun ,lisp-name ,(mapcar #'car arguments)
47           (let ((r (alien-funcall
48                     (extern-alien
49                      ,c-name
50                      (function ,return-type
51                                ,@(mapcar
52                                   (lambda (x)
53                                     (gethash (cadr x)
54                                              *designator-types*
55                                              (cadr x)))
56                                   arguments)))
57                     ,@(mapcar (lambda (x)
58                                 (if (nth-value 1
59                                                (gethash (cadr x)
60                                                         *designator-types*))
61                                     `(,(intern (symbol-name (cadr x))
62                                                :sb-posix)
63                                       ,(car x))
64                                     (car x)))
65                               arguments))))
66             (if (,error-predicate r) (syscall-error) r))))
67       `(sb-int:style-warn "Didn't find definition for ~S" ,c-name)))
68
69 (defmacro define-call (name return-type error-predicate &rest arguments)
70   (let ((lisp-name (lisp-for-c-symbol name))
71         (real-c-name (real-c-name name)))
72     `(progn
73        (export ',lisp-name :sb-posix)
74        (define-call-internally ,lisp-name
75            ,real-c-name
76          ,return-type
77          ,error-predicate
78          ,@arguments))))
79
80 (defmacro define-entry-point (name arglist &body body)
81   (let ((lisp-name (lisp-for-c-symbol name)))
82     `(progn
83       (export ',lisp-name :sb-posix)
84       (declaim (inline ,lisp-name))
85       (defun ,lisp-name ,arglist
86         ,@body))))