Skip to content

Commit

Permalink
Macro hygiene.
Browse files Browse the repository at this point in the history
Most of these changes are just paranoia (or "belt and braces" if you
want to be more charitable) but it makes sense to be a bit careful.
  • Loading branch information
conormcd committed Feb 7, 2016
1 parent 15341b1 commit b9d2a03
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/clj_libssh2/error.clj
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@
(but not equal to LIBSSH2_ERROR_EAGAIN). In those cases an exception will be
thrown using maybe-throw-error."
[session & body]
`(let [res# (do ~@body)]
(maybe-throw-error (:session ~session) res#)
`(let [session# ~session
res# (do ~@body)]
(maybe-throw-error (:session session#) res#)
res#))

(defn get-timeout
Expand Down
9 changes: 5 additions & 4 deletions src/clj_libssh2/session.clj
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,11 @@
default-opts) plus :hostname, :port and :credentials which
will be passed as the first three arguments to open."
[session session-params & body]
`(let [~session (open (:hostname ~session-params)
(:port ~session-params)
(:credentials ~session-params)
(dissoc ~session-params :hostname :port :credentials))]
`(let [session-params# ~session-params
~session (open (:hostname session-params#)
(:port session-params#)
(:credentials session-params#)
(dissoc session-params# :hostname :port :credentials))]
(try
(do ~@body)
(finally
Expand Down
16 changes: 9 additions & 7 deletions src/clj_libssh2/socket.clj
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,22 @@
(defmacro block
"Turn a non-blocking call that returns EAGAIN into a blocking one."
[session & body]
`(let [start-time# (System/currentTimeMillis)]
`(let [session# ~session
start-time# (System/currentTimeMillis)]
(while (= libssh2/ERROR_EAGAIN (do ~@body))
(handle-errors ~session
(wait ~session start-time#)))))
(handle-errors session#
(wait session# start-time#)))))

(defmacro block-return
"Similar to block, but for functions that return a pointer"
[session & body]
`(let [start-time# (System/currentTimeMillis)]
`(let [session# ~session
start-time# (System/currentTimeMillis)]
(loop [result# (do ~@body)]
(if (nil? result#)
(let [errno# (libssh2-session/last-errno (:session ~session))]
(handle-errors ~session errno#)
(let [errno# (libssh2-session/last-errno (:session session#))]
(handle-errors session# errno#)
(when (= libssh2/ERROR_EAGAIN errno#)
(wait ~session start-time#))
(wait session# start-time#))
(recur (do ~@body)))
result#))))
17 changes: 9 additions & 8 deletions src/clj_libssh2/ssh.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
describing a potential session (in which case this calls
clj-libssh2.session/with-session to create the session)."
[session session-or-host & body]
`(if (instance? Session ~session-or-host)
(let [~session ~session-or-host]
(do ~@body))
(let [defaults# {:hostname "127.0.0.1"
:port 22
:credentials {:username (System/getProperty "user.name")}}]
(session/with-session ~session (merge defaults# ~session-or-host)
(do ~@body)))))
`(let [session-or-host# ~session-or-host]
(if (instance? Session session-or-host#)
(let [~session session-or-host#]
(do ~@body))
(let [defaults# {:hostname "127.0.0.1"
:port 22
:credentials {:username (System/getProperty "user.name")}}]
(session/with-session ~session (merge defaults# session-or-host#)
(do ~@body))))))

(defn exec
"Execute a command and get the results.
Expand Down

0 comments on commit b9d2a03

Please sign in to comment.