I want to call a process and provide stdin and get stdout how to do that?
The pipe gives and takes a file and I do not know how to read it or create one from just hard coded string, this is somewhat pseudo code, any ideas how to make it work:
A dangerous working snippet for illustration purposes. You still need check optional entries here.
I did need to read the standard library example to understand this. To be able to pass the stdin you need to write to child.stdin using the writer interface, and after that you need to close it to “send EOF” otherwise the cat process get stuck waiting for it.
Some of things I didn’t understand: I tried to get the stdout.bufferedLen but it’s always returning 0 (My guess is this zeroed after child wait but I need to dig more into this), so I used stdout_reader.pos instead to delimiter until where I should read the output, but I believe this is not the ideal nor safe.
I feel like the usecase of All() might be something like if you’re reading in a binary format and you know the exact size of the input such that any deviation is an encoding error.
I find the “All” confusing in the function name because it’s unclear “all what?” Only by reading the implementation do I understand it means to copy into the whole buffer or fail.
In my own code, I call fill(1) and read from buffered(). Although, looking at the library code right now, maybe fillMore() is more explicit.
It’s primary purpose is to copy from the reader buffer into the argument buffer. But using it like this,
It’s just copying to and from the same buffer. When I was researching the IO system, I came across one or two code snippets like this, but it was really confusing why they were initializing the reader with the same buffer they were passing to readSliceAll().
Yes, to be honest I didn’t understand it very well too, and I copied exactly like it’s done in the Zig codebase (I assume the right way it’s how it’s done there), I’m still in the process of understanding how reader/writer interface work in this kind of use case
It might not be needed in your case, but in general, this can deadlock both processes, if for example, the subprocess needs to write to stderr more data than the system buffer allows and only then close stdout. You would be waiting on stdout to close, the subprocess to witing on stderr to get drained.
For a proper solution, you use use something like this:
Or spawn two extra tasks for reading stdout/stderr.
Actually, I just reread the example, yes, you will deadlock it if stdin is larger than the system buffer without draining stdout in between stdin writes.