How to intercept command inputs and command outputs #5410
-
I have an idea for a web app I want to build. It should basically be an xterm terminal acting as input (in my case backed by ZSH in node-pty) and a display area as output above that. I want to intercept what commands the user is issuing and what the return values for that are and, depending on the data, display it in the output. I know I can use events on the websocket, on the PTY instance, and on |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Correct, that is inherently not possible with what OSes have with PTY and command shells these days, at least not in a reliable fashion. What would get you close is writing your own command shell, where you control every aspect of the involved file descriptors and limit the commands down to non-interactive commands only, e.g. no interactive python call either. As soon as you allow ppl to run their own interactive programs like the python interpreter, that has its own sub commands and can spawn own DSL-based shells you lose the control again. There are some tricks possible with interactive programs (like putting them into their own PTY enclave) - but with those tricks you again would only know, that a certain output came from those, but still not know semantically, what to make of the outputs. So yeah, with a custom shell some control is possible but it is still very error-prone and only possible with a heuristic approach. In every case the terminal is the wrong point to put those logic, as it is just an IO device not aware of the program on the "other side". (In other words - you also would not ask the graphics card, which program rendered pixel xy, but try to get that info from the "desktop shell", which is typically baked into your window manager...) |
Beta Was this translation helpful? Give feedback.
-
I was afraid I'd get that answer :-( For future reference, I'll post part of the answer I got from ChatGPT:
I think insofar the hint about VS Code's implementation is correct, it's a strong indicator that that's the way to go. |
Beta Was this translation helpful? Give feedback.
Correct, that is inherently not possible with what OSes have with PTY and command shells these days, at least not in a reliable fashion.
What would get you close is writing your own command shell, where you control every aspect of the involved file descriptors and limit the commands down to non-interactive commands only, e.g. no interactive python call either. As soon as you allow ppl to run their own interactive programs like the python interpreter, that has its own sub commands and can spawn own DSL-based shells you lose the control …