]> git.sesse.net Git - stockfish/blobdiff - src/uci.cpp
Fix profile-build
[stockfish] / src / uci.cpp
index 8f8dd7be7f8f082f17a706f734e4df885f44fe3f..b7f72d9d4c8cb655df23ef9822f40724502e9d7b 100644 (file)
@@ -28,6 +28,7 @@
 #include "move.h"
 #include "position.h"
 #include "search.h"
+#include "thread.h"
 #include "ucioption.h"
 
 using namespace std;
@@ -35,7 +36,7 @@ using namespace std;
 namespace {
 
   // FEN string for the initial position
-  const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
+  const char* StarFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
 
   // Keep track of position keys along the setup moves (from start position to the
   // position just before to start searching). This is needed by draw detection
@@ -49,66 +50,71 @@ namespace {
 }
 
 
-/// execute_uci_command() takes a string as input, parses this text string as
-/// an UCI command, and calls the appropriate functions. In addition to the
-/// UCI commands, the function also supports a few debug commands.
+/// Wait for a command from the user, parse this text string as an UCI command,
+/// and calls the appropriate functions. Also intercepts EOF from stdin to
+/// ensure that we exit gracefully if the GUI dies unexpectedly. In addition to
+/// the UCI commands, the function also supports a few debug commands.
 
-bool execute_uci_command(const string& cmd) {
+void uci_loop() {
 
-  static Position pos(StartPositionFEN, false, 0); // The root position
+  Position pos(StarFEN, false, 0); // The root position
+  string cmd, token;
+  bool quit = false;
 
-  istringstream is(cmd);
-  string token;
+  while (!quit)
+  {
+      Threads.getline(cmd);
 
-  is >> skipws >> token;
+      istringstream is(cmd);
 
-  if (token == "quit")
-      return false;
+      is >> skipws >> token;
 
-  if (token == "go")
-      return go(pos, is);
+      if (token == "quit")
+          quit = true;
 
-  if (token == "ucinewgame")
-      pos.from_fen(StartPositionFEN, false);
+      else if (token == "go")
+          quit = !go(pos, is);
 
-  else if (token == "isready")
-      cout << "readyok" << endl;
+      else if (token == "ucinewgame")
+          pos.from_fen(StarFEN, false);
 
-  else if (token == "position")
-      set_position(pos, is);
+      else if (token == "isready")
+          cout << "readyok" << endl;
 
-  else if (token == "setoption")
-      set_option(is);
+      else if (token == "position")
+          set_position(pos, is);
 
-  else if (token == "perft")
-      perft(pos, is);
+      else if (token == "setoption")
+          set_option(is);
 
-  else if (token == "d")
-      pos.print();
+      else if (token == "perft")
+          perft(pos, is);
 
-  else if (token == "flip")
-      pos.flip();
+      else if (token == "d")
+          pos.print();
 
-  else if (token == "eval")
-  {
-      read_evaluation_uci_options(pos.side_to_move());
-      cout << trace_evaluate(pos) << endl;
-  }
+      else if (token == "flip")
+          pos.flip_me();
 
-  else if (token == "key")
-      cout << "key: " << hex     << pos.get_key()
-           << "\nmaterial key: " << pos.get_material_key()
-           << "\npawn key: "     << pos.get_pawn_key() << endl;
+      else if (token == "eval")
+      {
+          read_evaluation_uci_options(pos.side_to_move());
+          cout << trace_evaluate(pos) << endl;
+      }
 
-  else if (token == "uci")
-      cout << "id name "     << engine_name()
-           << "\nid author " << engine_authors()
-           << "\n"           << Options.print_all()
-           << "\nuciok"      << endl;
-  else
-      cout << "Unknown command: " << cmd << endl;
+      else if (token == "key")
+          cout << "key: " << hex     << pos.get_key()
+               << "\nmaterial key: " << pos.get_material_key()
+               << "\npawn key: "     << pos.get_pawn_key() << endl;
 
-  return true;
+      else if (token == "uci")
+          cout << "id name "     << engine_name()
+               << "\nid author " << engine_authors()
+               << "\n"           << Options.print_all()
+               << "\nuciok"      << endl;
+      else
+          cout << "Unknown command: " << cmd << endl;
+  }
 }
 
 
@@ -128,7 +134,7 @@ namespace {
 
     if (token == "startpos")
     {
-        fen = StartPositionFEN;
+        fen = StarFEN;
         is >> token; // Consume "moves" token if any
     }
     else if (token == "fen")