PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/libgpg-error/libgpg-error-1.8/lang/cl/gpg-error.lisp

https://bitbucket.org/thelearninglabs/uclinux-distro-tll-public
Lisp | 233 lines | 117 code | 60 blank | 56 comment | 4 complexity | 885cd7c8ab23ebd13d71609e680f5cd1 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, Unlicense, GPL-2.0, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0, ISC, MIT, 0BSD, LGPL-2.0
  1. ;;;; libgpg-error.lisp
  2. ;;; Copyright (C) 2006 g10 Code GmbH
  3. ;;;
  4. ;;; This file is part of libgpg-error.
  5. ;;;
  6. ;;; libgpg-error is free software; you can redistribute it and/or
  7. ;;; modify it under the terms of the GNU Lesser General Public License
  8. ;;; as published by the Free Software Foundation; either version 2.1 of
  9. ;;; the License, or (at your option) any later version.
  10. ;;;
  11. ;;; libgpg-error is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;; Lesser General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU Lesser General Public
  17. ;;; License along with libgpg-error; if not, write to the Free
  18. ;;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. ;;; 02111-1307, USA.
  20. ;;; Set up the library.
  21. (in-package :gpg-error)
  22. (define-foreign-library libgpg-error
  23. (:unix "libgpg-error.so")
  24. (t (:default "libgpg-error")))
  25. (use-foreign-library libgpg-error)
  26. ;;; System dependencies.
  27. (defctype size-t :unsigned-int "The system size_t type.")
  28. ;;; Error sources.
  29. (defcenum gpg-err-source-t
  30. "The GPG error source type."
  31. (:gpg-err-source-unknown 0)
  32. (:gpg-err-source-gcrypt 1)
  33. (:gpg-err-source-gpg 2)
  34. (:gpg-err-source-gpgsm 3)
  35. (:gpg-err-source-gpgagent 4)
  36. (:gpg-err-source-pinentry 5)
  37. (:gpg-err-source-scd 6)
  38. (:gpg-err-source-gpgme 7)
  39. (:gpg-err-source-keybox 8)
  40. (:gpg-err-source-ksba 9)
  41. (:gpg-err-source-dirmngr 10)
  42. (:gpg-err-source-gsti 11)
  43. (:gpg-err-source-any 31)
  44. (:gpg-err-source-user-1 32)
  45. (:gpg-err-source-user-2 33)
  46. (:gpg-err-source-user-3 34)
  47. (:gpg-err-source-user-4 35))
  48. (defconstant +gpg-err-source-dim+ 256)
  49. ;;; The error code type gpg-err-code-t.
  50. ;;; libgpg-error-codes.lisp is loaded by ASDF.
  51. (defctype gpg-error-t :unsigned-int "The GPG error code type.")
  52. ;;; Bit mask manipulation constants.
  53. (defconstant +gpg-err-code-mask+ (- +gpg-err-code-dim+ 1))
  54. (defconstant +gpg-err-source-mask+ (- +gpg-err-source-dim+ 1))
  55. (defconstant +gpg-err-source-shift+ 24)
  56. ;;; Constructor and accessor functions.
  57. ;;; If we had in-library versions of our static inlines, we wouldn't
  58. ;;; need to replicate them here. Oh well.
  59. (defun c-gpg-err-make (source code)
  60. "Construct an error value from an error code and source.
  61. Within a subsystem, use gpg-error instead."
  62. (logior
  63. (ash (logand source +gpg-err-source-mask+)
  64. +gpg-err-source-shift+)
  65. (logand code +gpg-err-code-mask+)))
  66. (defun c-gpg-err-code (err)
  67. "retrieve the error code from an error value."
  68. (logand err +gpg-err-code-mask+))
  69. (defun c-gpg-err-source (err)
  70. "retrieve the error source from an error value."
  71. (logand (ash err (- +gpg-err-source-shift+))
  72. +gpg-err-source-mask+))
  73. ;;; String functions.
  74. (defcfun ("gpg_strerror" c-gpg-strerror) :string
  75. (err gpg-error-t))
  76. (defcfun ("gpg_strsource" c-gpg-strsource) :string
  77. (err gpg-error-t))
  78. ;;; Mapping of system errors (errno).
  79. (defcfun ("gpg_err_code_from_errno" c-gpg-err-code-from-errno) gpg-err-code-t
  80. (err :int))
  81. (defcfun ("gpg_err_code_to_errno" c-gpg-err-code-to-errno) :int
  82. (code gpg-err-code-t))
  83. (defcfun ("gpg_err_code_from_syserror"
  84. c-gpg-err-code-from-syserror) gpg-err-code-t)
  85. ;;; Self-documenting convenience functions.
  86. ;;; See below.
  87. ;;;
  88. ;;;
  89. ;;; Lispy interface.
  90. ;;;
  91. ;;;
  92. ;;; Low-level support functions.
  93. (defun gpg-err-code-as-value (code-key)
  94. (foreign-enum-value 'gpg-err-code-t code-key))
  95. (defun gpg-err-code-as-key (code)
  96. (foreign-enum-keyword 'gpg-err-code-t code))
  97. (defun gpg-err-source-as-value (source-key)
  98. (foreign-enum-value 'gpg-err-source-t source-key))
  99. (defun gpg-err-source-as-key (source)
  100. (foreign-enum-keyword 'gpg-err-source-t source))
  101. (defun gpg-err-canonicalize (err)
  102. "Canonicalize the error value err."
  103. (gpg-err-make (gpg-err-source err) (gpg-err-code err)))
  104. (defun gpg-err-as-value (err)
  105. "Get the integer representation of the error value ERR."
  106. (let ((error (gpg-err-canonicalize err)))
  107. (c-gpg-err-make (gpg-err-source-as-value (gpg-err-source error))
  108. (gpg-err-code-as-value (gpg-err-code error)))))
  109. ;;; Constructor and accessor functions.
  110. (defun gpg-err-make (source code)
  111. "Construct an error value from an error code and source.
  112. Within a subsystem, use gpg-error instead."
  113. ;; As an exception to the rule, the function gpg-err-make will use
  114. ;; the error source value as is when provided as integer, instead of
  115. ;; parsing it as an error value.
  116. (list (if (integerp source)
  117. (gpg-err-source-as-key source)
  118. (gpg-err-source source))
  119. (gpg-err-code code)))
  120. (defvar *gpg-err-source-default* :gpg-err-source-unknown
  121. "define this to specify a default source for gpg-error.")
  122. (defun gpg-error (code)
  123. "Construct an error value from an error code, using the default source."
  124. (gpg-err-make *gpg-err-source-default* code))
  125. (defun gpg-err-code (err)
  126. "Retrieve an error code from the error value ERR."
  127. (cond ((listp err) (second err))
  128. ((keywordp err) err) ; FIXME
  129. (t (gpg-err-code-as-key (c-gpg-err-code err)))))
  130. (defun gpg-err-source (err)
  131. "Retrieve an error source from the error value ERR."
  132. (cond ((listp err) (first err))
  133. ((keywordp err) err) ; FIXME
  134. (t (gpg-err-source-as-key (c-gpg-err-source err)))))
  135. ;;; String functions.
  136. (defun gpg-strerror (err)
  137. "Return a string containig a description of the error code."
  138. (c-gpg-strerror (gpg-err-as-value err)))
  139. ;;; FIXME: maybe we should use this as the actual implementation for
  140. ;;; gpg-strerror.
  141. ;; (defcfun ("gpg_strerror_r" c-gpg-strerror-r) :int
  142. ;; (err gpg-error-t)
  143. ;; (buf :string)
  144. ;; (buflen size-t))
  145. ;; (defun gpg-strerror-r (err)
  146. ;; "Return a string containig a description of the error code."
  147. ;; (with-foreign-pointer-as-string (errmsg 256 errmsg-size)
  148. ;; (c-gpg-strerror-r (gpg-err-code-as-value (gpg-err-code err))
  149. ;; errmsg errmsg-size)))
  150. (defun gpg-strsource (err)
  151. "Return a string containig a description of the error source."
  152. (c-gpg-strsource (gpg-err-as-value err)))
  153. ;;; Mapping of system errors (errno).
  154. (defun gpg-err-code-from-errno (err)
  155. "Retrieve the error code for the system error. If the system error
  156. is not mapped, :gpg-err-unknown-errno is returned."
  157. (gpg-err-code-as-key (c-gpg-err-code-from-errno err)))
  158. (defun gpg-err-code-to-errno (code)
  159. "Retrieve the system error for the error code. If this is not a
  160. system error, 0 is returned."
  161. (c-gpg-err-code-to-errno (gpg-err-code code)))
  162. (defun gpg-err-code-from-syserror ()
  163. "Retrieve the error code directly from the system ERRNO. If the system error
  164. is not mapped, :gpg-err-unknown-errno is returned and
  165. :gpg-err-missing-errno if ERRNO has the value 0."
  166. (gpg-err-code-as-key (c-gpg-err-code-from-syserror)))
  167. ;;; Self-documenting convenience functions.
  168. (defun gpg-err-make-from-errno (source err)
  169. (gpg-err-make source (gpg-err-code-from-errno err)))
  170. (defun gpg-error-from-errno (err)
  171. (gpg-error (gpg-err-code-from-errno err)))
  172. (defun gpg-error-from-syserror ()
  173. (gpg-error (gpg-err-code-from-syserror)))