<commit_before><?php

/**
 * The Readline client is what the user spends their time entering text into.
 *
 * Input is collected and sent to {@link Boris_EvalWorker} for processing.
 */
class Boris_ReadlineClient {
  private $_socket;

  /**
   * Create a new ReadlineClient using $socket for communication.
   *
   * @param resource $socket
   */
  public function __construct($socket) {
    $this->_socket = $socket;
  }

  /**
   * Start the client with an prompt and readline history path.
   *
   * This method never returns.
   *
   * @param string $prompt
   * @param string $historyFile
   */
  public function start($prompt, $historyFile) {
    readline_read_history($historyFile);

    declare(ticks = 1);
    pcntl_signal(SIGCHLD, SIG_IGN);

    $parser = new Boris_ShallowParser();
    $buf = '';

    for (;;) {
      $line = readline($buf == '' ? $prompt : str_pad('*> ', strlen($prompt), ' ', STR_PAD_LEFT));

      if (false === $line) {
        $buf = 'exit(0);'; // ctrl-d acts like exit
      }

      if (strlen($line) > 0) {
        readline_add_history($line);
      }

      $buf .= sprintf("%s\n", $line);

      if ($statements = $parser->statements($buf)) {
        $buf = '';
        foreach ($statements as $stmt) {
          if (false === $written = socket_write($this->_socket, $stmt)) {
            throw new RuntimeException('Socket error: failed to write data');
          }
          if ($written > 0) {
            $status = socket_read($this->_socket, 1);
            if ($status == Boris_EvalWorker::EXITED) {
              readline_write_history($historyFile);
              echo "\n";
              exit(0);
            }
          }
        }
      }
    }
  }
}<commit_msg>Clear buffer on ctrl-c... this seems to have a nasty side-effect on the ctrl-d handling though<commit_after><?php

/**
 * The Readline client is what the user spends their time entering text into.
 *
 * Input is collected and sent to {@link Boris_EvalWorker} for processing.
 */
class Boris_ReadlineClient {
  private $_socket;
  private $_prompt;
  private $_historyFile;
  private $_clear = false;

  /**
   * Create a new ReadlineClient using $socket for communication.
   *
   * @param resource $socket
   */
  public function __construct($socket) {
    $this->_socket = $socket;
  }

  /**
   * Start the client with an prompt and readline history path.
   *
   * This method never returns.
   *
   * @param string $prompt
   * @param string $historyFile
   */
  public function start($prompt, $historyFile) {
    readline_read_history($historyFile);

    declare(ticks = 1);
    pcntl_signal(SIGCHLD, SIG_IGN);
    pcntl_signal(SIGINT, array($this, 'clear'));

    $parser = new Boris_ShallowParser();
    $buf = '';

    for (;;) {
      $this->_clear = false;
      $line = readline($buf == '' ? $prompt : str_pad('*> ', strlen($prompt), ' ', STR_PAD_LEFT));

      if ($this->_clear) {
        $buf = '';
        continue;
      }

      if (false === $line) {
        $buf = 'exit(0);'; // ctrl-d acts like exit
      }

      if (strlen($line) > 0) {
        readline_add_history($line);
      }

      $buf .= sprintf("%s\n", $line);

      if ($statements = $parser->statements($buf)) {
        $buf = '';
        foreach ($statements as $stmt) {
          if (false === $written = socket_write($this->_socket, $stmt)) {
            throw new RuntimeException('Socket error: failed to write data');
          }
          if ($written > 0) {
            $status = socket_read($this->_socket, 1);
            if ($status == Boris_EvalWorker::EXITED) {
              readline_write_history($historyFile);
              echo "\n";
              exit(0);
            }
          }
        }
      }
    }
  }

  public function clear() {
    $this->_clear = true;
  }
}
{'fraction_non_alphanumeric': 0.14349775784753363, 'fraction_numerical': 0.0031653917172250064, 'mean_word_length': 2.2327365728900257, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1526527', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1122, 'n_words': 304}