From a58aa948b89fd196191852f5bf4326e5046fe39b Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 11 Jun 2009 01:10:53 -0400 Subject: [PATCH] Enhancement - default.command - Implemented default.command handling. --- src/Context.cpp | 58 +++++++++++++++++++++++++++++-------------------- src/Context.h | 1 + src/TDB2.cpp | 16 ++++++++++++-- src/TDB2.h | 1 + 4 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/Context.cpp b/src/Context.cpp index 55d0f3d3f..181b64dd9 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -72,6 +72,12 @@ void Context::initialize (int argc, char** argv) else args.push_back (argv[i]); + initialize (); +} + +//////////////////////////////////////////////////////////////////////////////// +void Context::initialize () +{ // Load the configuration file from the home directory. If the file cannot // be found, offer to create a sample one. loadCorrectConfigFile (); @@ -95,6 +101,7 @@ void Context::initialize (int argc, char** argv) // TODO Handle "--version, -v" right here? // init TDB. + tdb.clear (); std::vector all; split (all, location, ','); foreach (path, all) @@ -104,7 +111,6 @@ void Context::initialize (int argc, char** argv) //////////////////////////////////////////////////////////////////////////////// int Context::run () { - std::cout << "--- start 1.8.0 ---" << std::endl; try { parse (); // Parse command line. @@ -133,26 +139,12 @@ int Context::run () std::cout << *f << std::endl; } - std::cout << "--- end 1.8.0 ---" << std::endl; return 0; } //////////////////////////////////////////////////////////////////////////////// void Context::dispatch () { -/* - // If argc == 1 and there is a default.command, use it. Otherwise use - // argc/argv. - std::string defaultCommand = context.config.get ("default.command"); - if (args.size () == 0 || defaultCommand != "") - { - // Stuff the command line. - args.clear (); - split (args, defaultCommand, ' '); - std::cout << "[task " << defaultCommand << "]" << std::endl; - } -*/ - bool gcMod = false; // Change occurred by way of gc. bool cmdMod = false; // Change occurred by way of command type. std::string out; @@ -337,7 +329,7 @@ void Context::parse () // The '--' argument shuts off all parsing - everything is an argument. if (*arg == "--") { -std::cout << "# parse terminator '" << *arg << "'" << std::endl; +std::cout << "# parse terminator '" << *arg << "'" << std::endl; terminated = true; } @@ -347,7 +339,7 @@ std::cout << "# parse terminator '" << *arg << "'" << std::endl; ! foundSomethingAfterSequence && sequence.valid (*arg)) { -std::cout << "# parse sequence '" << *arg << "'" << std::endl; +std::cout << "# parse sequence '" << *arg << "'" << std::endl; sequence.parse (*arg); foundSequence = true; } @@ -356,7 +348,7 @@ std::cout << "# parse sequence '" << *arg << "'" << std::endl; else if (arg->length () > 1 && (*arg)[0] == '+') { -std::cout << "# parse tag addition '" << *arg << "'" << std::endl; +std::cout << "# parse tag addition '" << *arg << "'" << std::endl; if (foundSequence) foundSomethingAfterSequence = true; @@ -367,7 +359,7 @@ std::cout << "# parse tag addition '" << *arg << "'" << std::endl; else if (arg->length () > 1 && (*arg)[0] == '-') { -std::cout << "# parse tag removal '" << *arg << "'" << std::endl; +std::cout << "# parse tag removal '" << *arg << "'" << std::endl; if (foundSequence) foundSomethingAfterSequence = true; @@ -380,7 +372,7 @@ std::cout << "# parse tag removal '" << *arg << "'" << std::endl; else if (attribute.valid (*arg)) { -std::cout << "# parse attribute '" << *arg << "'" << std::endl; +std::cout << "# parse attribute '" << *arg << "'" << std::endl; if (foundSequence) foundSomethingAfterSequence = true; @@ -394,7 +386,7 @@ std::cout << "# parse attribute '" << *arg << "'" << std::endl; if (foundSequence) foundSomethingAfterSequence = true; -std::cout << "# parse subst '" << *arg << "'" << std::endl; +std::cout << "# parse subst '" << *arg << "'" << std::endl; subst.parse (*arg); } @@ -402,7 +394,7 @@ std::cout << "# parse subst '" << *arg << "'" << std::endl; else if (cmd.command == "" && cmd.valid (*arg)) { -std::cout << "# parse cmd '" << *arg << "'" << std::endl; +std::cout << "# parse cmd '" << *arg << "'" << std::endl; cmd.parse (*arg); if (foundSequence) @@ -412,7 +404,7 @@ std::cout << "# parse cmd '" << *arg << "'" << std::endl; // Anything else is just considered description. else { -std::cout << "# parse description '" << *arg << "'" << std::endl; +std::cout << "# parse description '" << *arg << "'" << std::endl; if (foundSequence) foundSomethingAfterSequence = true; @@ -425,7 +417,7 @@ std::cout << "# parse description '" << *arg << "'" << std::endl; // terminated, therefore everything subsequently is a description. else { -std::cout << "# parse post-termination description '" << *arg << "'" << std::endl; +std::cout << "# parse post-termination description '" << *arg << "'" << std::endl; if (foundSequence) foundSomethingAfterSequence = true; @@ -439,6 +431,24 @@ std::cout << "# parse post-termination description '" << *arg << "'" << std::end task.set ("description", descCandidate); constructFilter (); + + // If no command was specified, and there were no command line arguments + // then invoke the default command. + if (cmd.command == "" && args.size () == 0) + { + std::string defaultCommand = config.get ("default.command"); + if (defaultCommand != "") + { + // Stuff the command line. + args.clear (); + split (args, defaultCommand, ' '); + std::cout << "[task " << defaultCommand << "]" << std::endl; + + // Reinitialize the context and recurse. + initialize (); + parse (); + } + } } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/Context.h b/src/Context.h index fd34f5ab2..5985dc14b 100644 --- a/src/Context.h +++ b/src/Context.h @@ -44,6 +44,7 @@ public: ~Context (); // Destructor void initialize (int, char**); // all startup + void initialize (); // for reinitializing int run (); // task classic int interactive (); // task interactive (not implemented) void dispatch (); // command handler dispatch diff --git a/src/TDB2.cpp b/src/TDB2.cpp index 4eff05891..76a478e28 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -111,6 +111,18 @@ TDB2::~TDB2 () unlock (); } +//////////////////////////////////////////////////////////////////////////////// +void TDB2::clear () +{ + mLocations.clear (); + mLock = true; + + if (mAllOpenAndLocked) + unlock (); + + mAllOpenAndLocked = false; +} + //////////////////////////////////////////////////////////////////////////////// void TDB2::location (const std::string& path) { @@ -179,7 +191,7 @@ int TDB2::loadPending (std::vector & tasks, Filter& filter) char line[T_LINE_MAX]; foreach (location, mLocations) { - std::cout << "# location.path: " << location->path << std::endl; + std::cout << "# location.path: " << location->path << "" << std::endl; line_number = 1; file = location->path + "/pending.data"; @@ -226,7 +238,7 @@ int TDB2::loadCompleted (std::vector & tasks, Filter& filter) char line[T_LINE_MAX]; foreach (location, mLocations) { - std::cout << "# location.path: " << location->path << std::endl; + std::cout << "# location.path: " << location->path << "" << std::endl; // TODO If the filter contains Status:x where x is not deleted or // completed, then this can be skipped. diff --git a/src/TDB2.h b/src/TDB2.h index 984ad6cc0..5e32844c2 100644 --- a/src/TDB2.h +++ b/src/TDB2.h @@ -45,6 +45,7 @@ public: TDB2& operator= (const TDB2&); // Assignment operator ~TDB2 (); // Destructor + void clear (); void location (const std::string&); void lock (bool lockFile = true);