How to redirect stdout back into stdin - unix

Quite straight forward question. I would like a program to read its output.
This is not a common thing to do, but it makes a whole lot of tests I have a lot easier as the program can test its output and return success or failure.
Note I can't just read the output as i'm testing the compiler and runtime.
Obviously I can use 2 programs but I would prefer each test to be independent.

Related

How do i programmatically write to *standard-input* for evaluation at the repl?

I type the following in a repl (clozure common lisp)
(defparameter test 1)
The repl responds with test
Now I enter:
(format *standard-input* "(defparameter test 2)")
Repl outputs (defparameter test 2) followed by nil.
But the value of test remains unchanged at 1.
Why is this? Isn't writing to the variable *standard-input* the same as entering the text at the repl?
How do I achieve the desired evaluation?
Some context:
I'm making a custom frontend for common lisp development using sockets. I need to write to standard input because even though I can evaluate code using eval and read, I cannot debug the code on errors.
For instance entering 1 to unwind the stack and return to top level is impossible without writing to standard input (as far as I can tell). I have the output parts figured out.
*standard-input* is an input stream, as its name implies. It's a stream you read from, not one you write to. It may be an output stream as well, but if it is then writing to it is not going to inject strings into the REPL.
I'd suggest looking at SLIME or SLY if you want to understand how to have REPLs & debuggers which interact with things down streams. In particular SWANK is probably the interesting bit to understand, or the equivalent for SLY, which is SLYNK (or slynk, not sure of the capitalisation). The implementations of these protocols in various Lisps are not entirely trivial, but the implementations exist already: you don't have to write them. Screen-scraping an interface made for humans to interact with is almost always a terrible approach: it's only reasonable when there is no better way, and in this case there is a better way, in fact there are at least two.

Abort ExUnit on the first test that does not pass

In Ruby, specifically RSpec, you can tell the test runner to abort on the first test that does not pass by the command-line flag --fail-fast. This helps a lot to not waste time or lose focus when fixing a lot of test in a row, for example when doing test-driven or behavior-driven development.
Now on Elixir with ExUnit I am looking for a way to do exactly that. Is there a way to do this?
There is such an option since Elixir 1.8.
Use the --max-failures switch to limit the number of tests evaluated with failure. To halt the test suite after the first failure, run this:
mix test --max-failures 1
Unfortunately there is (to my knowledge) no such flag implemented.
However, you can run a single test by
mix test path/to/testfile.exs:12
where 12 is the line number of the test.
Hope that helps!
That makes not much sense since tests in Elixir are a) to be run blazingly fast and b) in most cases are to be run asynchronously. Immediate termination of the test suite on the failed test is an anti-pattern and that’s why it’s not allowed by ExUnit authors.
One still has an option to shoot their own leg: just implement a custom handler for the EventManager and kill the whole application on “test failed” event.
For BDD, one preferably uses tags, running the test suite with only this feature included. That way you’ll get an ability to run tests per feature at any time in the future.
Also, as a last resort one might run a specific case only by passing the file name to mix test and/or a specific test only by passing the file name followed by a colon and a line number.

IO buffering turn off

Commands which are invoked from LISP are having trouble with buffered IO streams, how to turn it off? I found only flushing functions, which are good for nothing in this situation.
You cannot do that
There is no portable way to turn buffering on and off, and for a very good reason: buffered i/o is orders of magnitude faster.
You should not try to do that
You might be able to find out how to do it in the implementation you use, but it will be a waste of time - you will soon realize that it was a mistake. Please do yourself a favor and do not retrace my steps - I tried that almost 20 years ago.
There is a better way
When you are done writing, just flush the output.
You can encapsulate this using classes or macros, so that no additional typing would be involved.
It is better for many reasons
Please note that the approach I suggest will make your code more readable - it will require you to specify your message boundaries explicitly.
Remember, you write code for others (and yourself 6 months from now!) to read, modify, and debug.
So, I finally found the problem (maybe bug??)
Form looks like this (SBCL API)
(run-program "sudo" '("mv" "foo.txt" "/usr/bin")
:search t
:output t
:input t
:wait t)
This works exactly how I wanted, problem was that instead of t in input and output args, I specified *standard-input* and *standard-output* which caused not proper working of command (if invoked program wanted some input from user like sudo for example).
No idea why, but at least, it works now.

Difference between write() and printf()

Recently I am studying operating system..I just wanna know:
What’s the difference between a system call (like write()) and a standard library function (like printf())?
A system call is a call to a function that is not part of the application but is inside the kernel. The kernel is a software layer that provides you some basic functionalities to abstract the hardware to you. Roughly, the kernel is something that turns your hardware into software.
You always ultimately use write() to write anything on a peripheral whatever is the kind of device you write on. write() is designed to only write a sequence of bytes, that's all and nothing more. But as write() is considered too basic (you may want to write an integer in ten basis, or a float number in scientific notation, etc), different libraries are provided to you by different kind of programming environments to ease you.
For example, the C programming langage gives you printf() that lets you write data in many different formats. So, you can understand printf() as a function that convert your data into a formatted sequence of bytes and that calls write() to write those bytes onto the output. But C++ gives you cout; Java System.out.println, etc. Each of these functions ends to a call to write() (at least on POSIX systems).
One thing to know (important) is that such a system call is costly! It is not a simple function call because you need to call something that is outside of your own code and the system must ensure that you are not trying to do nasty things, etc. So it is very common in higher print-like function that some buffering is built-in; such that write is not always called, but your data are kept into some hidden structure and written only when it is really needed or necessary (buffer is full or you really want to see the result of your print).
This is exactly what happens when you manage your money. If many people gives you 5 bucks each, you won't go deposit each to the bank! You keep them on your wallet (this is the print) up to the point it is full or you don't want to keep them anymore. Then you go to the bank and make a big deposit (this is the write). And you know that putting 5 bucks to your wallet is much much faster than going to the bank and make the deposit. The bank is the kernel/OS.
System calls are implemented by the operating system, and run in kernel mode. Library functions are implemented in user mode, just like application code. Library functions might invoke system calls (e.g. printf eventually calls write), but that depends on what the library function is for (math functions usually don't need to use the kernel).
System Call's in OS are used in interacting with the OS. E.g. Write() could be used something into the system or into a program.
While Standard Library functions are program specific, E.g. printf() will print something out but it will only be in GUI/command line and wont effect system.
Sorry couldnt comment, because i need 50 reputation to comment.
EDIT: Barmar has good answer
I am writing a small program. At the moment it just reads each line from stdin and prints it to stdout. I can add a call to write in the loop, and it would add a few characters at the end of each line. But when I use printf instead, then all the extra characters are clustered and appear all at once, instead of appearing on each line.
It seems that using printf causes stderr to be buffered. Adding fflush(stdout); after calling printf fixes the discrepancy in output.
I'd like to mention another point that the stdio buffers are maintained in a process’s user-space memory, while system call write transfers data directly to a kernel buffer. It means that if you fork a process after write and printf calls, flushing may bring about to give output three times subject to line-buffering and block-buffering, two of them belong to printf call since stdio buffers are duplicated in the child by fork.
printf() is one of the APIs or interfaces exposed to user space to call functions from C library.
printf() actually uses write() system call. The write() system call is actually responsible for sending data to the output.

Accessing Console Application IO

I have a linux console application - a scientific simulation program that I use. It opens up a TCL shell that you then issue commands to. Normally what I do is pre-write all my test vectors and look at the output by manually inputting the data, but now I'd like to move on to something more complicated: incorporating external feedback.
My idea is, I'll have a external simulation running that takes the output of the simulator and then generates new test vectors on the fly to feed back into the simulation. I'm kind of hazy on the on the details of how to implement this. I am semi-familiar with C and with Python.
I guess, getting into specifics - how do I hook into the program's terminal I/O? I'd prefer to use Python if possible. Are there any references I can read to get up to speed on this?
Your idea is quite reasonable. Python supports this very well: Sub-process launching, and inter-process communication. Documentation like the following might be helpful:
http://docs.python.org/library/subprocess.html
In short, you're going to "read from" child-process stdout (and maybe stderr), and "write to" child-process stdin. You can have your interactive console like you describe, or read from/write to text files, and even "hook-together" processes to talk (like piping "mycommand | mycommand2").
For Python, there are many strong examples (like the "scons" build system written in Python that does this a lot). Further, the Qt's QProcess class makes this pretty easy, and there are a few really good Python wrappers available like "PySide", "PyQt", and "PythonQt" (probably others).

Resources