예제 #1
0
파일: Context.cpp 프로젝트: SEJeff/task
int Context::initialize (int argc, const char** argv)
{
  timer_init.start ();
  int rc = 0;

  try
  {
    // char** argv --> std::vector <std::string> Context::a3.
    a3.capture (argc, argv);

    // echo one two -- three | task zero --> task zero one two
    // 'three' is left in the input buffer.
    a3.append_stdin ();

    // Assume default .taskrc and .task locations.
    assumeLocations ();

    // Process 'rc:<file>' command line override, and remove the argument from the
    // Context::a3.
    a3.categorize ();
    a3.rc_override (home_dir, rc_file);

    // TASKRC environment variable overrides the command line.
    char* override = getenv ("TASKRC");
    if (override)
    {
      rc_file = File (override);
      debug ("TASKRC override: ");
      header (format (STRING_CONTEXT_RC_OVERRIDE, rc_file._data));
    }

    // Dump any existing values and load rc file.
    config.clear ();
    config.load  (rc_file);

    // The data location, Context::data_dir, is determined from the assumed
    // location (~/.task), or set by data.location in the config file, or
    // overridden by rc.data.location on the command line.
    std::string location;
    a3.get_data_location (location);
    data_dir = Directory (location);

    override = getenv ("TASKDATA");
    if (override)
    {
      data_dir = Directory (override);
      config.set ("data.location", data_dir._data);
      header (format (STRING_CONTEXT_DATA_OVERRIDE, data_dir._data));
    }

/* TODO Enable this when the time is right, say for 2.1
    extension_dir = data_dir._data + "/extensions";
*/

    // Create missing config file and data directory, if necessary.
    createDefaultConfig ();

    // Handle Aliases.
    loadAliases ();
    a3.resolve_aliases ();

    // Apply rc overrides to Context::config, capturing raw args for later use.
    a3.apply_overrides ();

    // Initialize the color rules, if necessary.
    if (color ())
      initializeColorRules ();

    // Instantiate built-in command objects.
    Command::factory (commands);

    // Instantiate built-in column objects.
    Column::factory (columns);

    // Categorize all arguments one more time.  THIS IS NECESSARY - it helps the
    // following inject_defaults method determine whether there needs to be a
    // default command assumed.
    a3.categorize ();

    // Handle default command and assumed 'info' command.
    a3.inject_defaults ();

    // The re-categorization allows all injected arguments to be properly given
    // a category.
    a3.categorize ();
    a3.dump ("Context::initialize");

    // TODO Instantiate extension command objects.
    // TODO Instantiate default command object.

    // TODO Instantiate extension column objects.

    // TODO Instantiate extension UDA objects.
    // TODO Instantiate extension format objects.

    // If there is a locale variant (en-US.<variant>), then strip it.
    std::string locale = config.get ("locale");
    std::string::size_type period = locale.find ('.');
    if (period != std::string::npos)
      locale = locale.substr (0, period);

    // Initialize the database.
    tdb2.set_location (data_dir);

    // Hook system init, plus post-start event occurring at the first possible
    // moment after hook initialization.
    hooks.initialize ();
    hooks.trigger ("on-launch");
  }
예제 #2
0
파일: Context.cpp 프로젝트: gerarldlee/task
int Context::initialize (int argc, const char** argv)
{
  timer_init.start ();
  int rc = 0;

  try
  {
    ////////////////////////////////////////////////////////////////////////////
    //
    // [1] Load the correct config file.
    //     - Default to ~/.taskrc (ctor).
    //     - Allow command line override rc:<file>
    //     - Allow $TASKRC override.
    //     - Load resultant file.
    //     - Apply command line overrides to the config.
    //
    ////////////////////////////////////////////////////////////////////////////

    CLI::getOverride (argc, argv, home_dir, rc_file);

    char* override = getenv ("TASKRC");
    if (override)
    {
      rc_file = File (override);
      header (format (STRING_CONTEXT_RC_OVERRIDE, rc_file._data));
    }

    config.clear ();
    config.load (rc_file);
    CLI::applyOverrides (argc, argv);

    ////////////////////////////////////////////////////////////////////////////
    //
    // [2] Locate the data directory.
    //     - Default to ~/.task (ctor).
    //     - Allow command line override rc.data.location:<dir>
    //     - Allow $TASKDATA override.
    //     - Inform TDB2 where to find data.
    //     - Create the rc_file and data_dir, if necessary.
    //
    ////////////////////////////////////////////////////////////////////////////

    CLI::getDataLocation (argc, argv, data_dir);

    override = getenv ("TASKDATA");
    if (override)
    {
      data_dir = Directory (override);
      config.set ("data.location", data_dir._data);
      header (format (STRING_CONTEXT_DATA_OVERRIDE, data_dir._data));
    }

    tdb2.set_location (data_dir);
    createDefaultConfig ();

    ////////////////////////////////////////////////////////////////////////////
    //
    // [3] Instantiate Command objects and capture entities.
    //
    ////////////////////////////////////////////////////////////////////////////

    Command::factory (commands);
    std::map <std::string, Command*>::iterator cmd;
    for (cmd = commands.begin (); cmd != commands.end (); ++cmd)
    {
      cli.entity ("cmd", cmd->first);
      cli.entity ((cmd->second->read_only () ? "readcmd" : "writecmd"), cmd->first);

      if (cmd->first[0] == '_')
        cli.entity ("helper", cmd->first);
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // [4] Instantiate Column objects and capture entities.
    //
    ////////////////////////////////////////////////////////////////////////////

    Column::factory (columns);
    std::map <std::string, Column*>::iterator col;
    for (col = columns.begin (); col != columns.end (); ++col)
      cli.entity ("attribute", col->first);

    cli.entity ("pseudo", "limit");

    ////////////////////////////////////////////////////////////////////////////
    //
    // [5] Capture modifier and operator entities.
    //
    ////////////////////////////////////////////////////////////////////////////

    for (unsigned int i = 0; i < NUM_MODIFIER_NAMES; ++i)
      cli.entity ("modifier", modifierNames[i]);

    std::vector <std::string> operators;
    Eval::getOperators (operators);
    std::vector <std::string>::iterator op;
    for (op = operators.begin (); op != operators.end (); ++op)
      cli.entity ("operator", *op);

    Eval::getBinaryOperators (operators);
    for (op = operators.begin (); op != operators.end (); ++op)
      cli.entity ("binary_operator", *op);

    ////////////////////////////////////////////////////////////////////////////
    //
    // [6] Complete the Context initialization.
    //
    ////////////////////////////////////////////////////////////////////////////

    initializeColorRules ();
    staticInitialization ();
    propagateDebug ();
    loadAliases ();

    ////////////////////////////////////////////////////////////////////////////
    //
    // [7] Parse the command line.
    //
    ////////////////////////////////////////////////////////////////////////////

    // Scan command line for 'rc:<file>' only.
    cli.initialize (argc, argv);
    cli.analyze (true, true);

    // Extract a recomposed command line.
    bool foundDefault = false;
    bool foundAssumed = false;
    std::string combined;
    std::vector <A>::const_iterator a;
    for (a = cli._args.begin (); a != cli._args.end (); ++a)
    {
      if (combined.length ())
        combined += ' ';

      if (a->attribute ("canonical") != "")
        combined += a->attribute ("canonical");
      else
        combined += a->attribute ("raw");

      if (a->hasTag ("DEFAULT"))
        foundDefault = true;

      if (a->hasTag ("ASSUMED"))
        foundAssumed = true;
    }

    if (foundDefault)
      header ("[" + combined + "]");

    if (foundAssumed)
      header (STRING_ASSUME_INFO);

    ////////////////////////////////////////////////////////////////////////////
    //
    // [8] Initialize hooks.
    //
    ////////////////////////////////////////////////////////////////////////////

    hooks.initialize ();
  }