What is the optimal way to use Stockfish programmatically? I have Stockfish compiled on my machine. Instead of just running the Stockfish program in the terminal, is it possible to interact with it in a separate program?

For example, some basic things I might want to do:


Create a position object from a FEN.    
Get an evaluation from the position object (limiting with depth or time).    
Return an array of positions for all the legal moves from the position.


These are just some very basic examples. Can anyone supply some code snippets for these instructions? I'm really struggling to find any helpful documentation.

EDIT: It looks like this really has a two part solution. The first is piping the input/output to some external program. The second is understanding the UCI protocol to perform what I want to do. Does anyone know the UCI protocol for getting all of the possible moves/positions from a given position? Also, it looks like the 'eval' command only gives a static evaluation of a position (i.e. no consideration of the future). Is there anyway to retrieve a time/depth based evaluation as well?
I think you're talking about how to fork a process for the engine and how to use it for the UCI protocol.

What you need to do is:


Fork a new child process for the Stockfish engine
Send and receive UCI commands to/from the engine
Parse the UCI commands yourself


Do you want to study existing source code? Look at:

http://talkchess.com/forum/viewtopic.php?t=57770

Download Chess Game Analyzer, it's a simple Python script for automatic annotation.

When you have the source code, look at the the following:

 p = subprocess.Popen(engineName, stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)


This line creates a new process for the engine, where engineName is the path of the engine executive.

p.stdin.write("uci\n")


This line sends the uci command to the engine.

for eline in iter(p.stdout.readline, '')


This line parses a UCI response.