pythonlinuxbashfile-descriptorprocfs

is there a way to 'own' or 'get priority over' the stdin file descriptor of an external process in linux?


tl;dr although a tldr wont explain everything fully, i have an external program ( lets say pid 1234 ) is trying to read from another external process ( lets say pid 1111 ), 1111 always reads from its own stdin, but 1234 wants to handle the stdin instead of the program, but 1111 sometimes blocks 1234 from reading a byte from /proc/1111/fd/0, which is not desired, i want to know how to make 1234 always read a byte from it and 1111 never or rarely be able to read from it

i have been trying to develop a concept for GNU BASH syntax highlighting as the only other syntax highlighter for bash i found is VERY slow because it implements the whole readline lib in BASH

i ran into some issues but came up with something working, somewhat -- https://gist.github.com/TruncatedDinosour/e2034cf470f268596235a5c88ffcd048

you can find a more in-depth explenation on it at https://blog.ari-web.xyz/b/bash-syntax-highlighting-part-one-concept/ currently i asked for general developer public for the answer, 'maybe someone knows' i thought to myself, but i think i might get an answer faster here

basically, this concept has a problem, sometimes it misses a byte because bash steals the read() i think, so far i have tried

currently, i never was able to fully get rid of the issue, so i thought to ask here and the general public, maybe you, fellow people, know how to achieve what im trying to achieve

thanks for any help, ideas or clues in advance :)


Solution

  • To better understand your options we need to take a step back and consider how a process gets its I/O.

    if you want to intercept, you thus have several options:

    Per your specific issue - when the stdin is a terminal - there are other considerations. Namely, that bash or whichever shells also manipulates the terminal directly - using ioctl(2) codes (look at stty(1) for examples, too). One other consideration is that the syntax highlighting is done via ANSI escape sequences, which involves writing to the terminal - (\e + other curses). This might potentially account for the "stolen byte" you mention you're encountering.

    TL;DR, like you say - that answers the injection question in the most comprehensive way we can try, but your particulars of syntax highlighting might require to be addressed differently. Be more specific, and we can try to be, as well :-)