sysdep.lisp 13.1 KB
Newer Older
Eric Marsden's avatar
Eric Marsden committed
1 2
;;; sysdep.lisp -- system-dependent parts of pg-dot-lisp
;;;
3
;;; Author: Eric Marsden <eric.marsden@free.fr>
4
;;; Time-stamp: <2006-11-19 emarsden>
Eric Marsden's avatar
Eric Marsden committed
5 6
;;
;;
Eric Marsden's avatar
Eric Marsden committed
7

Eric Marsden's avatar
Eric Marsden committed
8
(in-package :postgresql)
Eric Marsden's avatar
Eric Marsden committed
9

10 11 12 13
(eval-when (:compile-toplevel :load-toplevel :execute)
  #+lispworks (require "comm")
  #+cormanlisp (require :sockets)
  #+armedbear (require :socket))
14

Eric Marsden's avatar
Eric Marsden committed
15

16 17 18 19 20 21
(defmacro %sysdep (desc &rest forms)
  (when (null forms)
    (error "No system dependent code to ~A" desc))
  (car forms))


Eric Marsden's avatar
Eric Marsden committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#+(and cmu glibc2)
(eval-when (:compile-toplevel :load-toplevel)
  (format t ";; Loading libcrypt~%")
  ;; (ext:load-foreign "/lib/libcrypt.so.1")
  (sys::load-object-file "/usr/lib/libcrypt.so"))

#+(and cmu glibc2)
(defun crypt (key salt)
  (declare (type string key salt))
  (alien:alien-funcall
   (alien:extern-alien "crypt"
          (function c-call:c-string c-call:c-string c-call:c-string))
   key salt))

#-(and cmu glibc2)
(defun crypt (key salt)
  (declare (ignore salt))
  key)


Eric Marsden's avatar
Eric Marsden committed
42 43
(defun md5-digest (string &rest strings)
  (declare (type simple-string string))
44 45 46
  (let ((vec (md5sum-sequence
              (map '(vector (unsigned-byte 8)) #'char-code 
                   (apply #'concatenate 'string string strings)))))
Eric Marsden's avatar
Eric Marsden committed
47 48 49 50 51 52 53 54
    (format nil "~(~{~2,'0X~}~)" (coerce vec 'list))))

(defun md5-encode-password (user password salt)
  (concatenate 'string "md5"
               (md5-digest (md5-digest password user) salt)))



Eric Marsden's avatar
Eric Marsden committed
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
;; this is a little fiddly, because CLISP can be built without support
;; for the Linux package
;; #+CLISP
;; (defun crypt (key salt)
;;   (linux::crypt key salt))


;; bug in WRITE-SEQUENCE in CMUCL
#+(or cmu18c cmu18d)
(defun write-sequence (seq stream &key start end)
  (declare (ignore start end))
  (loop :for element :across seq
        :do (write-byte element stream)))



;; work around bug in FASL fop dumping 
#+cmu (setf c::top-level-lambda-max 0)


#+(and cmu ssl)
(defun socket-connect (port host)
  (declare (type integer port))
  (handler-case
      (let ((fd (ext:connect-to-inet-socket host port)))
        (ssl:make-ssl-client-stream fd))
    (error (e)
      (error 'connection-failure
             :host host
             :port port
             :transport-error e))))


#+cmu
(defun socket-connect (port host)
  (declare (type integer port))
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  (let ((host (if (typep host 'pathname)
                  (namestring host)
                  host)))
    (handler-case
        (let ((fd (if (eql #\/ (char host 0))
                      (ext:connect-to-unix-socket
                       (format nil "~A.s.PGSQL.~D" (string host) port))
                      (ext:connect-to-inet-socket host port))))
          (sys:make-fd-stream fd :input t :output t
                              :element-type '(unsigned-byte 8)))
      (error (e)
        (error 'connection-failure
               :host host
               :port port
               :transport-error e)))))
Eric Marsden's avatar
Eric Marsden committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

;; this doesn't currently work, because WRITE-SEQUENCE is not
;; implemented
#+(and cmu simple-streams broken)
(defun socket-connect (port host)
  (declare (type integer port))
  (handler-case
      (make-instance 'stream:socket-simple-stream
                     :remote-host host
                     :remote-port port
                     :direction :io)
    (error (e)
      (error 'connection-failure
             :host host
             :port port
             :transport-error e))))

#+clisp
(defun socket-connect (port host)
  (declare (type integer port))
  (handler-case
   (#+lisp=cl socket:socket-connect
    #-lisp=cl lisp:socket-connect
129 130 131
    port host
    :element-type '(unsigned-byte 8)
    :buffered t)
Eric Marsden's avatar
Eric Marsden committed
132 133 134 135 136 137
   (error (e)
      (declare (ignore e))
      (error 'connection-failure :host host :port port))))


#+sbcl
138
(defun socket-connect (port host-name)
Eric Marsden's avatar
Eric Marsden committed
139
  (declare (type integer port))
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  (let ((host (if (typep host-name 'pathname)
                  (namestring host-name)
                  host-name)))
    (handler-case
        (sb-bsd-sockets:socket-make-stream
         (if (eql #\/ (char host 0))
             (let ((s (make-instance 'sb-bsd-sockets:local-socket :type :stream)))
               (sb-bsd-sockets:socket-connect
                s (format nil  "~A.s.PGSQL.~D" (string host) port))
               s)
             (let ((s (make-instance 'sb-bsd-sockets:inet-socket
                         :type :stream :protocol :tcp))
                   (num (car (sb-bsd-sockets:host-ent-addresses
                              (sb-bsd-sockets:get-host-by-name host)))))
               (sb-bsd-sockets:socket-connect s num port)
               s))
         :element-type '(unsigned-byte 8)
         :input t
         :output t
         :buffering :none)
      (error (e)
        (error 'connection-failure :host host :port port :transport-error e)))))
Eric Marsden's avatar
Eric Marsden committed
162 163 164 165 166

#+allegro
(defun socket-connect (port host)
  (declare (type integer port))
  (handler-case
167 168 169 170 171 172 173 174 175 176 177
      (if (eql #\/ (char host 0))
          (socket:make-socket :type :stream
                              :address-family :file
                              :connect :active
                              :remote-filename (format nil "~A.s.PGSQL.~D" (string host) port)
                              :format :binary)
          (socket:make-socket :remote-host host
                              :remote-port port
                              :connect :active
                              :format :binary))
    (error (e)
178
      (error 'connection-failure
179 180 181
             :host host
             :port port
             :transport-error e))))
Eric Marsden's avatar
Eric Marsden committed
182 183 184 185 186 187

;; Lispworks 4.2 doesn't seem to implement WRITE-SEQUENCE on binary
;; streams. Fixed in version 4.3. 
#+lispworks
(defun socket-connect (port host)
  (declare (type integer port))
188 189 190 191
  (handler-case
      (comm:open-tcp-stream host port
                            :element-type '(unsigned-byte 8)
                            :direction :io)
192 193
    ;; note that Lispworks (at least 4.3) does not signal an error if
    ;; the hostname cannot be resolved; it simply returns NIL
194 195 196 197 198
    (error (e)
      (error 'connection-failure
             :host host
             :port port
             :transport-error e))))
Eric Marsden's avatar
Eric Marsden committed
199 200 201 202 203 204 205 206 207 208 209 210

;; this doesn't work, since the Corman sockets module doesn't support
;; binary I/O on socket streams.
#+cormanlisp
(defun socket-connect (port host)
  (declare (type integer port))
  (handler-case
      (progn
        (sockets:start-sockets)
        (let ((sock (sockets:make-client-socket :host host :port port)))
          (sockets:make-socket-stream sock)))
    (error (e)
211 212 213 214
      (error 'connection-failure
             :host host
             :port port
             :transport-error e))))
Eric Marsden's avatar
Eric Marsden committed
215 216 217 218

#+openmcl
(defun socket-connect (port host)
  (declare (type integer port))
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  (let ((host (if (typep host 'pathname)
                  (namestring host)
                  host)))
    (handler-case
        (if (eql #\/ (char host 0))
            (make-socket :address-family :file
                         :type :stream
                         :connect :active
                         :format :binary
                         :remote-filename (format nil "~A.s.PGSQL.~D" (string host) port))
            (make-socket :address-family :internet
                         :type :stream
                         :connect :active
                         :format :binary
                         :remote-host host
                         :remote-port port))
      (error (e)
        (error 'connection-failure
               :host host
               :port port
               :transport-error e)))))
Eric Marsden's avatar
Eric Marsden committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

;; from John DeSoi
#+(and mcl (not openmcl))
(defun socket-connect (port host)
  (declare (type integer port))
  (ccl::open-tcp-stream host port :element-type '(unsigned-byte 8)))

;; There is a bug in MCL (4.3.1 tested) where read-sequence and
;; write-sequence fail with binary tcp streams. These two methods
;; provide a work-around.
#+(and mcl (not openmcl))
(defmethod ccl:stream-write-sequence ((s ccl::opentransport-binary-tcp-stream)
                                      (sequence ccl::simple-unsigned-byte-vector)
                                      &key (start 0) end)
  (ccl::stream-write-vector s sequence start (or end (length sequence)))
  s)

#+(and mcl (not openmcl))
(defmethod ccl:stream-read-sequence ((s ccl::opentransport-binary-tcp-stream)
                                     (sequence ccl::simple-unsigned-byte-vector)
                                     &key (start 0) (end (length sequence)))
  (ccl::io-buffer-read-bytes-to-vector (ccl::stream-io-buffer s)
                                       sequence (- end start) start)
  end)


#+ecl
(defun socket-connect (port host)
  (declare (type integer port))
269 270 271 272 273 274 275
  (handler-case 
      (si:open-client-stream host port)
    (error (e)
      (error 'connection-failure
             :host host
             :port port
             :transport-error e))))
Eric Marsden's avatar
Eric Marsden committed
276 277 278 279 280 281 282 283 284 285 286



;; as of version 2.6 GCL is way too broken to run this: DEFPACKAGE doesn't
;; work, DEFINE-CONDITION not implemented, ...
#+gcl
(defun socket-connect (port host)
  (declare (type integer port))
  (si::socket port :host host))


287 288 289

#+armedbear
(eval-when (:load-toplevel :execute :compile-toplevel)
290
  (require :socket))
291

292 293 294
;; could provide support for connections via a unix-domain socket by
;; using http://freshmeat.net/projects/j-buds/ (requires linking to a
;; shared libary)
Eric Marsden's avatar
Eric Marsden committed
295 296 297
#+armedbear
(defun socket-connect (port host)
  (declare (type integer port))
298 299 300
  (handler-case
      (ext:get-socket-stream (ext:make-socket host port)
                             :element-type '(unsigned-byte 8))
301 302 303 304 305
    (error (e)
      (error 'connection-failure
             :host host
             :port port
             :transport-error e))))
Eric Marsden's avatar
Eric Marsden committed
306 307


308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
;; for Lispworks
;;     (defun encode-lisp-string (string)
;;       (translate-string-via-fli string :utf-8 :latin-1))
;; 
;;     (defun decode-external-string (string)
;;       (translate-string-via-fli string :latin-1 :utf-8))
;; 
;;     ;; Note that a :utf-8 encoding of a null in a latin-1 string is
;;     ;; also null, and vice versa.  So don't have to worry about
;;     ;; null-termination or length. (If we were translating to/from
;;     ;; :unicode, this would become an issue.)
;; 
;;     (defun translate-string-via-fli (string from to)
;;       (fli:with-foreign-string (ptr elements bytes :external-format from)
;; 	  string
;; 	(declare (ignore elements bytes))
;; 	(fli:convert-from-foreign-string ptr :external-format to)))

326 327 328 329 330

;;; character encoding support

(defvar *pg-client-encoding*)

331
(defun implementation-name-for-encoding (encoding)
332
  (%sysdep "convert from client encoding to external format name"
333
     #+(and clisp unicode)
334 335
     (cond ((string-equal encoding "SQL_ASCII") charset:ascii)
           ((string-equal encoding "LATIN1") charset:iso-8859-1)
336
           ((string-equal encoding "LATIN2") charset:iso-8859-2)
337 338
           ((string-equal encoding "LATIN9") charset:iso-8859-9)
           ((string-equal encoding "UTF8") charset:utf-8)
339 340
           (t (error "unknown encoding ~A" encoding)))
     #+(and allegro ics)
341 342 343 344
     (cond ((string-equal encoding "SQL_ASCII") :ascii)
           ((string-equal encoding "LATIN1") :latin1)
           ((string-equal encoding "LATIN9") :latin9)
           ((string-equal encoding "UTF8") :utf8)
345 346
           (t (error "unknown encoding ~A" encoding)))
     #+(and sbcl sb-unicode)
347
     (cond ((string-equal encoding "SQL_ASCII") :ascii)
348 349 350
           ((string-equal encoding  "LATIN1") :iso-8859-1)
           ((string-equal encoding  "LATIN2") :iso-8859-2)
           ((string-equal encoding "LATIN9") :iso-8859-9)
351
           ((string-equal encoding "UTF8") :utf8)
352
           (t (error "unknown encoding ~A" encoding)))
353
     #+(or cmu gcl ecl abcl openmcl lispworks)
354
     nil))
355

356
(defun convert-string-to-bytes (string encoding)
357
  (declare (type string string))
358
  (%sysdep "convert string to octet-array"
359
     #+(and clisp unicode)
360
     (ext:convert-string-to-bytes string (implementation-name-for-encoding encoding))
361 362
     #+(and allegro ics)
     (excl:string-to-octets string :null-terminate nil
363
			    :external-format (implementation-name-for-encoding encoding))
364
     #+(and :sbcl :sb-unicode)
365 366
     (sb-ext:string-to-octets string
                              :external-format (implementation-name-for-encoding encoding))
367
     #+(or cmu gcl ecl abcl openmcl lispworks)
368 369 370 371
     (if (member encoding '("SQL_ASCII" "LATIN1" "LATIN9") :test #'string-equal)
         (let ((octets (make-array (length string) :element-type '(unsigned-byte 8))))
           (map-into octets #'char-code string))
         (error "Can't convert ~A string to octets" encoding))))
372

373
(defun convert-string-from-bytes (bytes encoding)
374 375 376
  (declare (type (vector (unsigned-byte 8)) bytes))
  (%sysdep "convert octet-array to string"
    #+(and clisp unicode)
377
    (ext:convert-string-from-bytes bytes (implementation-name-for-encoding encoding))
378
    #+(and allegro ics)
379
    (excl:octets-to-string bytes :external-format (implementation-name-for-encoding encoding))
380
    #+(and :sbcl :sb-unicode)
381
    (sb-ext:octets-to-string bytes :external-format (implementation-name-for-encoding encoding))
382 383 384
    ;; for implementations that have no support for character
    ;; encoding, we assume that the encoding is an octet-for-octet
    ;; encoding, and convert directly
385
    #+(or cmu (and sbcl (not :sb-unicode)) gcl ecl abcl openmcl lispworks)
386 387 388 389
    (let ((string (make-string (length bytes))))
      (map-into string #'code-char bytes))))


Eric Marsden's avatar
Eric Marsden committed
390
;; EOF