Created
September 29, 2019 15:52
-
-
Save wasamasa/b59aafb92b407df8dad919a5641cbb8e to your computer and use it in GitHub Desktop.
r2pipe.el
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'cl-lib) | |
(require 'json) | |
(defvar r2pipe-bin (executable-find "radare2")) | |
(defvar r2pipe-buffer "*r2pipe*") | |
(defun r2pipe-process-filter (proc string) | |
(when (buffer-live-p (process-buffer proc)) | |
(with-current-buffer (process-buffer proc) | |
(let ((moving (= (point) (process-mark proc)))) | |
(save-excursion | |
(goto-char (process-mark proc)) | |
(insert (ansi-color-apply string)) | |
(set-marker (process-mark proc) (point)) | |
(when (zerop (char-before (point))) | |
(set-marker r2pipe-output-start r2pipe-output-end) | |
(set-marker r2pipe-output-end (point)))) | |
(if moving (goto-char (process-mark proc))))))) | |
(defun r2pipe-open (path) | |
(let* ((process-connection-type nil) | |
(proc (start-process "r2pipe" r2pipe-buffer r2pipe-bin "-Nq0" path))) | |
(set-process-filter proc 'r2pipe-process-filter) | |
proc)) | |
(defun r2pipe-last-cmd-output (proc) | |
(when (buffer-live-p (process-buffer proc)) | |
(with-current-buffer (process-buffer proc) | |
(sit-for 0.05) | |
(while (not (zerop (char-before (point-max)))) | |
(sit-for 0.05)) | |
(let ((to (save-excursion | |
(goto-char (point-max)) | |
(search-backward "\x00"))) | |
(from (save-excursion | |
(goto-char (point-max)) | |
(search-backward "\x00") | |
(search-backward "\x00") | |
(1+ (point))))) | |
(buffer-substring from to))))) | |
(defun r2pipe-cmd (proc cmd) | |
(process-send-string proc (concat cmd "\n")) | |
(r2pipe-last-cmd-output proc)) | |
(defun r2pipe-cmdj (proc cmd) | |
(json-read-from-string (r2pipe-cmd proc cmd))) | |
(defmacro r2pipe (path &rest body) | |
(declare (indent 1)) | |
(let ((proc-sym (make-symbol "proc"))) | |
`(let ((,proc-sym (r2pipe-open ,path))) | |
(unwind-protect | |
(cl-macrolet ((cmd (c) `(r2pipe-cmd ,',proc-sym ,c)) | |
(cmdj (c) `(r2pipe-cmdj ,',proc-sym ,c))) | |
,@body) | |
(delete-process ,proc-sym))))) | |
;; (r2pipe "/home/wasa/downloads/mystery" | |
;; (message "XXX: %s" (cmd "pi 5")) | |
;; (message "YYY: %s" (cmd "aaa")) | |
;; (message "ZZZ: %s" (cmd "? 1")) | |
;; (message "JJJ: %S" (cmdj "aflj"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment