<commit_before>/****************************************************************************
 * Sample urbi client that sends commands contained in a file.
 *
 * Copyright (C) 2004, 2006, 2007, 2008, 2009 <NAME>.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
**********************************************************************/

/* This demonstration program sends commands to an Urbi server. */

#include <list>
#include <string>

#include <libport/cstdio>
#include <libport/sysexits.hh>
#include <libport/foreach.hh>

#include <libport/cli.hh>
#include <libport/package-info.hh>
#include <libport/program-name.hh>
#include <libport/option-parser.hh>

#include <urbi/package-info.hh>
#include <urbi/uclient.hh>

using libport::program_name;

namespace
{
  static
  void
  usage(libport::OptionParser& parser)
  {
    std::cout <<
      "usage: " << program_name() << " [OPTION].. [FILE]...\n"
      "\n"
      "  FILE    to upload onto the server\n"
      "\n"
      "Options:\n";
    parser.options_doc(std::cout);
    std::cout << "\n"
	      << urbi::package_info().report_bugs()
	      << std::endl
	      << libport::exit(EX_OK);
  }

  static
  void
  version()
  {
    std::cout << "urbi-send" << std::endl
              << urbi::package_info() << std::endl
              << libport::exit(EX_OK);
  }
}

static urbi::UCallbackAction
dump(void* banner, const urbi::UMessage& msg)
{
  if ((msg.tag == "start" || msg.tag == "ident")
      && !*static_cast<bool*>(banner))
    return urbi::URBI_CONTINUE;

  switch (msg.type)
  {
    case urbi::MESSAGE_DATA:
    case urbi::MESSAGE_SYSTEM:
      std::cout << msg << std::endl;
      break;

    case urbi::MESSAGE_ERROR:
      std::cerr << msg << std::endl;
      break;
  }
  return urbi::URBI_CONTINUE;
}

static urbi::UCallbackAction
error(void* banner, const urbi::UMessage& msg)
{
  dump(banner, msg);
  exit(0);
}

/*-----------------.
| Things to send.  |
`-----------------*/

struct Data
{
  virtual ~Data() {};
  virtual void send(urbi::UClient& u) const = 0;
};

struct TextData: Data
{
  TextData(const std::string& s)
    : command_(s)
  {}

  virtual
  void
  send(urbi::UClient& u) const
  {
    u.send("%s", command_.c_str());
  }

  std::string command_;
};


struct FileData: Data
{
  FileData(const std::string& s)
    : filename_(s)
  {}

  virtual
  void
  send(urbi::UClient& u) const
  {
    u.sendFile(filename_ == "-" ? "/dev/stdin" : filename_);
  }

  std::string filename_;
};


/*-------.
| Main.  |
`-------*/

int
main(int argc, char* argv[])
{
  libport::program_initialize(argc, argv);
  /// Things to send to the server.
  typedef std::list<Data*> data_list;
  data_list data;
  /// Display the server's banner.
  bool banner = false;
  /// Server host name.
  std::string host;
  /// Server port.
  int port;

  // Parse the command line.
  libport::OptionValue arg_exp("send SCRIPT to the server", "expression", 'e');
  libport::OptionValue arg_file("send the contents of FILE to the server", "file", 'f');
  libport::OptionValue arg_pfile("file containing the port to listen to", "port-file");

  libport::OptionParser opt_parser;
  opt_parser << arg_exp << arg_file << libport::opts::help
	     << libport::opts::host_l << libport::opts::port_l
	     << libport::opts::version;
  libport::cli_args_type remainings_args = opt_parser(libport::program_arguments());

  if (libport::opts::help.get())
    usage(opt_parser);
  if (arg_exp.filled())
    data.push_back(new TextData(arg_exp.value()));
  if (arg_file.filled())
    data.push_back(new FileData(arg_file.value()));
  host = libport::opts::host_l.value("localhost");
  port = libport::opts::port_l.get<int>(urbi::UClient::URBI_PORT);
  if (arg_pfile.filled())
    port = libport::file_contents_get<int>(arg_pfile.value());
  if (libport::opts::version.get())
    version();

  foreach(std::string arg, remainings_args)
    if (arg[0] == '-' && arg[1] != 0)
      libport::invalid_option (arg);
    else
      data.push_back(new FileData(arg));

  urbi::UClient client(host, port);
  client.setKeepAliveCheck(3000, 1000);
  if (client.error())
    std::cerr << libport::program_name() << ": client failed to set up"
	      << std::endl
              << libport::exit(1);

  client.setWildcardCallback(callback(&dump, &banner));
  client.setClientErrorCallback(callback(&error, &banner));

  /*----------------.
  | Send contents.  |
  `----------------*/
  for (data_list::const_iterator i = data.begin(),
         i_end = data.end();
       i != i_end;
       ++i)
  {
    (*i)->send(client);
    delete *i;
  }
<commit_msg>urbi-send: Revert "Make use of option parser."<commit_after>/****************************************************************************
 * Sample urbi client that sends commands contained in a file.
 *
 * Copyright (C) 2004, 2006, 2007, 2008, 2009 <NAME>.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
**********************************************************************/

/* This demonstration program sends commands to an Urbi server. */

#include <list>
#include <string>

#include <libport/cstdio>
#include <libport/sysexits.hh>

#include <libport/cli.hh>
#include <libport/package-info.hh>
#include <libport/program-name.hh>

#include <urbi/package-info.hh>
#include <urbi/uclient.hh>

using libport::program_name;

namespace
{
  static
  void
  usage()
  {
    std::cout <<
      "usage: " << program_name() << " [OPTION].. [FILE]...\n"
      "\n"
      "  FILE    to upload onto the server\n"
      "\n"
      "Options:\n"
      "  -b, --banner             do not hide the server-sent banner\n"
      "  -e, --expression SCRIPT  send SCRIPT to the server\n"
      "  -f, --file FILE          send the contents of FILE to the server\n"
      "  -h, --help               display this message and exit\n"
      "  -H, --host HOST          Urbi server host [localhost]\n"
      "  -p, --port PORT          Urbi server port\n"
      "      --port-file FILE     file containing the port to listen to\n"
      "  -v, --version            display version information and exit\n"
      "\n"
                << urbi::package_info().report_bugs()
                << std::endl
                << libport::exit(EX_OK);
  }

  static
  void
  version()
  {
    std::cout << "urbi-send" << std::endl
              << urbi::package_info() << std::endl
              << libport::exit(EX_OK);
  }
}

static urbi::UCallbackAction
dump(void* banner, const urbi::UMessage& msg)
{
  if ((msg.tag == "start" || msg.tag == "ident")
      && !*static_cast<bool*>(banner))
    return urbi::URBI_CONTINUE;

  switch (msg.type)
  {
    case urbi::MESSAGE_DATA:
    case urbi::MESSAGE_SYSTEM:
      std::cout << msg << std::endl;
      break;

    case urbi::MESSAGE_ERROR:
      std::cerr << msg << std::endl;
      break;
  }
  return urbi::URBI_CONTINUE;
}

static urbi::UCallbackAction
error(void* banner, const urbi::UMessage& msg)
{
  dump(banner, msg);
  exit(0);
}

/*-----------------.
| Things to send.  |
`-----------------*/

struct Data
{
  virtual ~Data() {};
  virtual void send(urbi::UClient& u) const = 0;
};

struct TextData: Data
{
  TextData(const std::string& s)
    : command_(s)
  {}

  virtual
  void
  send(urbi::UClient& u) const
  {
    u.send("%s", command_.c_str());
  }

  std::string command_;
};


struct FileData: Data
{
  FileData(const std::string& s)
    : filename_(s)
  {}

  virtual
  void
  send(urbi::UClient& u) const
  {
    u.sendFile(filename_ == "-" ? "/dev/stdin" : filename_);
  }

  std::string filename_;
};


/*-------.
| Main.  |
`-------*/

int
main(int argc, char* argv[])
{
  libport::program_initialize(argc, argv);
  /// Things to send to the server.
  typedef std::list<Data*> data_list;
  data_list data;
  /// Display the server's banner.
  bool banner = false;
  /// Server host name.
  std::string host = "localhost";
  /// Server port.
  int port = urbi::UClient::URBI_PORT;

  // Parse the command line.
  for (int i = 1; i < argc; ++i)
  {
    std::string arg = argv[i];

    if (arg == "--banner" || arg == "-b")
      banner = true;
    else if (arg == "--expression" || arg == "-e")
      data.push_back(
        new TextData(libport::convert_argument<std::string>(arg, argv[++i])));
    else if (arg == "--file" || arg == "-f")
      data.push_back(
        new FileData(libport::convert_argument<std::string>(arg, argv[++i])));
    else if (arg == "--help" || arg == "-h")
      usage();
    else if (arg == "--host" || arg == "-H")
      host = argv[++i];
    else if (arg == "--port" || arg == "-p")
      port = libport::convert_argument<int> (arg, argv[++i]);
    else if (arg == "--port-file")
      port =
        (libport::file_contents_get<int>
         (libport::convert_argument<const char*>(arg, argv[++i])));
    else if (arg == "--version" || arg == "-v")
      version();
    else if (arg[0] == '-' && arg[1] != 0)
      libport::invalid_option (arg);
    else
      // An argument: a file.
      data.push_back(new FileData(argv[i]));
  }

  urbi::UClient client(host, port);
  client.setKeepAliveCheck(3000, 1000);
  if (client.error())
    std::cerr << libport::program_name() << ": client failed to set up"
	      << std::endl
              << libport::exit(1);

  client.setWildcardCallback(callback(&dump, &banner));
  client.setClientErrorCallback(callback(&error, &banner));

  /*----------------.
  | Send contents.  |
  `----------------*/
  for (data_list::const_iterator i = data.begin(),
         i_end = data.end();
       i != i_end;
       ++i)
  {
    (*i)->send(client);
    delete *i;
  }

{'fraction_non_alphanumeric': 0.19239557627273557, 'fraction_numerical': 0.009048533040855497, 'mean_word_length': 2.891180654338549, 'pattern_counts': {'":': 2, '<': 127, '<?xml version=': 0, '>': 38, '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': '3935976', 'n_tokens_mistral': 3671, 'n_tokens_neox': 3515, 'n_words': 958}