2

I'm writting a shell in Common Lisp that reads command from the user's input and returns the output after executing. When the user types a "meta" command I want to write a placeholder so that the user can either press enter and send that command to the shell, or edit this command as if it were their own input.

This what I have now:

(defun eval-print (command-executor command)
  (format t "~a~%" (funcall command-executor command)))

(defun cmd-exec-local (command)
  "Execute a command in the local shell"
  (trim (uiop:run-program command :output :string)))

(defun shell (command-executor &optional placeholder)
  "Simulate a REPL"
  (progn
    (if placeholder
        (format t ">>> ~a" placeholder) ; This is were I try to show a placeholder
        (format t ">>> "))
    (let ((command (read-line))) ; Check if the user wrote a meta command, else just execute whatever they wrote
      (cond ((string= "@exit" command) (format t "Bye!~%")) ; Exit the shell
            ((string= "@bashrev" command) (shell command-executor *bash-rev*)) ; Bash reverse shell
            ((string= "@info" command) (shell command-executor "whoami")) ; System info
            (t (eval-print command-executor command) (shell command-executor)))))) ; Execute the command

But this is the output I get:

PENTESTING> (shell #'cmd-exec-local)
>>> @info
>>> whoami ; This has been written to the standard output, so even if I press enter, nothing gets executed

>>> whoami ; This was written by the user so when pressing enter, I get the results I need
kali
>>> @exit
Bye!

My guess is I need some kind of buffer that I can write to but I've tried terminal-io and it didn't seem to work.

7
  • I reproduce… note that a real line-handling library like readline would make it possible (but then outside of the editor). Commented Dec 25, 2023 at 11:56
  • style: you don't need a progn inside the defun. Commented Dec 25, 2023 at 11:57
  • What are the definitions of your eval-print and trim functions ? Commented Dec 30, 2023 at 8:09
  • @JérômeRadix I added eval-print's definition to the OP, trim is imported from the library str. Commented Dec 30, 2023 at 12:58
  • This is indeed a great question, the use of *query-io* does not allow such kind of thing you want. And clearly your (format t ">>> ~a" placeholder) doesn't do the trick. I just don't see how it all relates to SLIME. You'd better remove all references to Slime in your question (and remove the slime tag) Commented Jan 1, 2024 at 15:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.