compat.lisp  1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
  2 
  3 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
  4 
  5 ;;; Redistribution and use in source and binary forms, with or without
  6 ;;; modification, are permitted provided that the following conditions
  7 ;;; are met:
  8 
  9 ;;;   * Redistributions of source code must retain the above copyright
 10 ;;;     notice, this list of conditions and the following disclaimer.
 11 
 12 ;;;   * Redistributions in binary form must reproduce the above
 13 ;;;     copyright notice, this list of conditions and the following
 14 ;;;     disclaimer in the documentation and/or other materials
 15 ;;;     provided with the distribution.
 16 
 17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
 18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 20 ;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 
 29 (in-package :hunchentoot)
 30 
 31 (defmacro when-let ((var form) &body body)
 32   "Evaluates FORM and binds VAR to the result, then executes BODY
 33 if VAR has a true value."
 34   `(let ((,var ,form))
 35      (when ,var ,@body)))
 36 
 37 (defmacro with-unique-names ((&rest bindings) &body body)
 38   "Syntax: WITH-UNIQUE-NAMES ( { var | (var x) }* ) declaration* form*
 39 
 40 Executes a series of forms with each VAR bound to a fresh,
 41 uninterned symbol. The uninterned symbol is as if returned by a call
 42 to GENSYM with the string denoted by X - or, if X is not supplied, the
 43 string denoted by VAR - as argument.
 44 
 45 The variable bindings created are lexical unless special declarations
 46 are specified. The scopes of the name bindings and declarations do not
 47 include the Xs.
 48 
 49 The forms are evaluated in order, and the values of all but the last
 50 are discarded \(that is, the body is an implicit PROGN)."
 51   ;; reference implementation posted to comp.lang.lisp as
 52   ;; <cy3bshuf30f.fsf@ljosa.com> by Vebjorn Ljosa - see also
 53   ;; <http://www.cliki.net/Common%20Lisp%20Utilities>
 54   `(let ,(mapcar #'(lambda (binding)
 55                      (check-type binding (or cons symbol))
 56                      (if (consp binding)
 57                        (destructuring-bind (var x) binding
 58                          (check-type var symbol)
 59                          `(,var (gensym ,(etypecase x
 60                                           (symbol (symbol-name x))
 61                                           (character (string x))
 62                                           (string x)))))
 63                        `(,binding (gensym ,(symbol-name binding)))))
 64                  bindings)
 65          ,@body))
 66 
 67 (defmacro with-rebinding (bindings &body body)
 68   "Syntax: WITH-REBINDING ( { var | (var prefix) }* ) form*
 69 
 70 Evaluates a series of forms in the lexical environment that is
 71 formed by adding the binding of each VAR to a fresh, uninterned
 72 symbol, and the binding of that fresh, uninterned symbol to VAR's
 73 original value, i.e., its value in the current lexical environment.
 74 
 75 The uninterned symbol is created as if by a call to GENSYM with the
 76 string denoted by PREFIX - or, if PREFIX is not supplied, the string
 77 denoted by VAR - as argument.
 78 
 79 The forms are evaluated in order, and the values of all but the last
 80 are discarded \(that is, the body is an implicit PROGN)."
 81   ;; reference implementation posted to comp.lang.lisp as
 82   ;; <cy3wv0fya0p.fsf@ljosa.com> by Vebjorn Ljosa - see also
 83   ;; <http://www.cliki.net/Common%20Lisp%20Utilities>
 84   (loop for binding in bindings
 85         for var = (if (consp binding) (car binding) binding)
 86         for name = (gensym)
 87         collect `(,name ,var) into renames
 88         collect ``(,,var ,,name) into temps
 89         finally (return `(let ,renames
 90                           (with-unique-names ,bindings
 91                             `(let (,,@temps)
 92                               ,,@body))))))
 93 
 94 (defun get-peer-address-and-port (socket)
 95   "Returns the peer address and port of the socket SOCKET as two
 96 values.  The address is returned as a string in dotted IP address
 97 notation."
 98   (multiple-value-bind (address port) (usocket:get-peer-name socket)
 99     (values (ecase (length address)
100               (4 (usocket:vector-quad-to-dotted-quad address))
101               #+(or) (16 (usocket:vector-to-ipv6-host address)))
102             port)))
103 
104 (defun get-local-address-and-port (socket)
105   "Returns the local address and port of the socket SOCKET as two
106 values.  The address is returned as a string in dotted IP address
107 notation."
108   (multiple-value-bind (address port) (usocket:get-local-name socket)
109     (values (ecase (length address)
110               (4 (usocket:vector-quad-to-dotted-quad address))
111               #+(or) (16 (usocket:vector-to-ipv6-host address)))
112             port)))
113 
114 (defun make-socket-stream (socket acceptor)
115   "Returns a stream for the socket SOCKET.  The ACCEPTOR argument is
116 ignored."
117   (declare (ignore acceptor))
118   (usocket:socket-stream socket))
119 
120 (defun make-lock (name)
121   "Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
122   (bt:make-lock name))
123 
124 (defmacro with-lock-held ((lock) &body body)
125   "Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
126   `(bt:with-lock-held (,lock) ,@body))
127 
128 (defun make-condition-variable (&key name)
129   (declare (ignore name))
130   (bt:make-condition-variable))
131 
132 (defun condition-variable-signal (condition-variable)
133   (bt:condition-notify condition-variable))
134 
135 (defun condition-variable-wait (condition-variable lock)
136   (bt:condition-wait condition-variable lock))