File: early.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (149 lines) | stat: -rw-r--r-- 6,423 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
;;;
;;; early.lisp --- Early definitions used throughout OSICAT-WINDOWS.
;;;
;;; Copyright (C) 2005-2006, Matthew Backes  <lucca@accela.net>
;;; Copyright (C) 2005-2006, Dan Knapp  <dankna@accela.net>
;;; Copyright (C) 2006-2007, Stelian Ionescu  <stelian.ionescu-zeus@poste.it>
;;; Copyright (C) 2007, Luis Oliveira  <loliveira@common-lisp.net>
;;; Copyright (c) 2021, Eric Timmons   <eric@timmons.dev>
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation
;;; files (the "Software"), to deal in the Software without
;;; restriction, including without limitation the rights to use, copy,
;;; modify, merge, publish, distribute, sublicense, and/or sell copies
;;; of the Software, and to permit persons to whom the Software is
;;; furnished to do so, subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;; NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;;; DEALINGS IN THE SOFTWARE.

(in-package #:osicat-windows)


;;; Early types

(defctype bool (:boolean :uchar))
(defctype wchar :uint16)
(defctype large-integer :int64)
(defctype search-handle :pointer)
(defctype handle :pointer)
(defctype dword :uint32)

;;; We'll define the translators for this later, once we've set up the needed
;;; foreign functions.
(define-foreign-type wide-string ()
  ()
  (:actual-type :pointer))

(define-parse-method wide-string ()
  (make-instance 'wide-string))


(define-foreign-type wide-filename ()
  ()
  (:actual-type :pointer))

(define-parse-method wide-filename ()
  (make-instance 'wide-filename))

;;; Subtypes of WIN32-ERROR correspond to errors detected through the
;;; GetLastError mechanism.  These are defined below.
(define-condition win32-error (system-error)
  ((syscall :initform nil :initarg :syscall :reader win32-error-syscall))
  (:documentation
   "WIN32-ERRORs are signalled whenever GetLastError is set by a call or where
the call signals an error via the return value.")
  (:report (lambda (condition stream)
             (format stream "Error calling ~S~%~%~A"
                     (win32-error-syscall condition)
                     (get-error-message (system-error-code condition))))))

(defun win32-error (&optional (error-code (get-last-error)) syscall)
  (error (make-instance 'win32-error :code error-code :syscall syscall)))

;;; Default ERROR-GENERATOR for ERRNO-WRAPPER.
(defun syscall-signal-win32-error (return-value syscall)
  (declare (ignore return-value))
  (win32-error (get-last-error) syscall))

;;; Error predicate that always returns NIL.  Not actually used
;;; because the WIN32-ERROR-WRAPPER optimizes this call away.
(defun never-fails (&rest args)
  (declare (ignore args))
  nil)

;;; This type is used by DEFWINAPI to automatically check for errors
;;; using the ERROR-PREDICATE function which is passed the foreign
;;; function's return value (after going through RETURN-FILTER).  If
;;; ERROR-PREDICATE returns true, ERROR-GENERATOR is invoked.  See the
;;; ERRNO-WRAPPER parse method and type translation.
(define-foreign-type win32-error-wrapper ()
  ((error-predicate :initarg :error-predicate :reader error-predicate)
   (return-filter :initarg :return-filter :reader return-filter)
   (error-generator :initarg :error-generator :reader error-generator)
   (base-type :initarg :base-type :reader base-type)
   (function-name :initarg :function-name :reader function-name)))

(define-parse-method win32-error-wrapper
    (base-type &key error-predicate (return-filter 'identity)
               (error-generator 'syscall-signal-win32-error)
               function-name)
  ;; pick a default error-predicate
  (unless error-predicate
    (ecase base-type
      ((handle search-handle)
       (setf error-predicate '(lambda (p) (= (pointer-address p) +invalid-handle-value+))))
      (:pointer
       (setf error-predicate 'null-pointer-p))
      ((dword large-integer :int)
       (setf error-predicate 'zerop))
      ((bool :bool)
       (setf error-predicate 'not))))
  (make-instance 'win32-error-wrapper
                 :actual-type base-type
                 :base-type base-type
                 :error-predicate error-predicate
                 :return-filter return-filter
                 :error-generator error-generator
                 :function-name function-name))

;;; This type translator sets up the appropriate calls to
;;; RETURN-FILTER, ERROR-PREDICATE and ERROR-GENERATOR around the
;;; foreign function call.
(defmethod expand-from-foreign (value (type win32-error-wrapper))
  (if (and (eq (return-filter type) 'identity)
           (eq (error-predicate type) 'never-fails))
      value
      `(let ((r (convert-from-foreign ,value ',(base-type type))))
         ,(let ((return-exp (if (eq (return-filter type) 'identity)
                               'r
                               `(,(return-filter type) r))))
            (if (eq (error-predicate type) 'never-fails)
                return-exp
                `(if (,(error-predicate type) r)
                     (,(error-generator type) r ',(function-name type))
                     ,return-exp))))))

(defmacro defrawwinapi (name-and-opts return-type &body params)
  (multiple-value-bind (lisp-name c-name options)
      (cffi::parse-name-and-options name-and-opts)
    `(defcfun (,c-name ,lisp-name :convention :stdcall ,@options) ,return-type
       ,@params)))

(defmacro defwinapi (name-and-opts return-type &body args)
  (multiple-value-bind (lisp-name c-name options)
      (cffi::parse-name-and-options name-and-opts)
    (declare (ignore c-name options))
    `(defrawwinapi ,name-and-opts (win32-error-wrapper ,return-type :function-name ,lisp-name)
       ,@args)))