Reliable Stdio and Input Redirection: Plan of a Tac

Rebol has historically in my experience been very bad at dealing with redirection. I've summarized some of that before.

I've also summarized the varous reasons that the Device Model in R3-Alpha really amounted to obfuscating make-work. Really it just asked you to unpack assembled Rebol cells into plain-C-only data buffers and types. It did so all for the sake of throwing over the wall to an abstract OS that didn't match or model any existing OS...and lacked invariants or asserts.

But as far as stdio goes, I've managed to excise the "REBDEV" Device for it. And yet the stdio is all in an extension. But it's an extension that knows what Rebol values are, and can speak in terms of them and talk to the OS without needing a middleman.

And Now... We Have Stdio Input and Output Redirection Tests!

Not too many tests, but it was more than enough to keep me busy making them all work cross platform...while not screwing up the console experience. The model for managing Ctrl-C cancellations is much better now.

At the moment the tests are all passing. I'm hoping that we can get tests that check how Ctrl-C reactions are handled and do some dynamic typing in "pseudoterminals" so that the console gets tested.

Things will be bumpy for a bit, but to make a long story short...they're definitely on a much better track now. It's getting under control which means that features will be easier tow rite.

The Reverse Cat Test

For a first test program I picked something simple... to write a Ren-C version of the UNIX "tac" program. This just reverses the lines in a file. But I wanted it to do that to a stream of piped text...receive lines in, output reversed lines.

Here was how I thought to write it:

write-stdout try delimit/tail newline reverse collect [
    until [not keep line: try read-line]
]

It makes use of a new feature of delimit: you can ask the delimiter be added at the /HEAD or /TAIL also:

>> delimit/tail "," ["a" "b" "c"]
== "a,b,c,"

>> delimit/head "," ["a" "b" "c"]
== ",a,b,c"

>> delimit/head/tail "," ["a" "b" "c"]
== ",a,b,c,"

So when you're taking lines that have been read and given back to you without the newline on them, putting them back to having the newlines should involve adding newlines after each item. Said another way: "between all the items plus one after the last"

We Need More of These Programs! More of These Tests!

There's a lot more I could say about this, but I'm worn out at present. So I'll just end the post with a plea that more of these small programs, data, and redirection be cooked up.

:sleeping_bed:

3 Likes