From 190c6b53fc0fa9abba5757d6bb4a93a49a23db3f Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 7 Jun 2009 14:58:32 -0400 Subject: [PATCH] Enhancements - Config - Added processing for context.config overrides, and associated argc,argv handling. - Bug fix in filt.t.cpp, but three tests still fail. Too big a distraction to fix right now. - Warning: build is not broken, but task is broken. --- src/Att.cpp | 3 +- src/Config.cpp | 2 + src/Context.cpp | 212 +++++++++++++++++++++---------------------- src/parse.cpp | 171 +++++++++++++++++----------------- src/report.cpp | 2 +- src/task.cpp | 27 +++--- src/tests/filt.t.cpp | 4 +- 7 files changed, 211 insertions(+), 210 deletions(-) diff --git a/src/Att.cpp b/src/Att.cpp index 84cc87bba..11b72209b 100644 --- a/src/Att.cpp +++ b/src/Att.cpp @@ -103,7 +103,8 @@ bool Att::valid (const std::string& input) const if (n.skip (':') && (n.getQuoted ('"', ignored) || n.getUntil (' ', ignored) || - n.getUntilEOS (ignored))) + n.getUntilEOS (ignored) || + n.depleted ())) return true; return false; diff --git a/src/Config.cpp b/src/Config.cpp index 4ef586fa0..36531f40c 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -85,6 +85,8 @@ Config::Config (const std::string& file) // not tolerated, but blank lines and comments starting with # are allowed. bool Config::load (const std::string& file) { + this->clear (); + std::ifstream in; in.open (file.c_str (), std::ifstream::in); if (in.good ()) diff --git a/src/Context.cpp b/src/Context.cpp index ca330abe3..dd91e13f7 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -145,7 +145,7 @@ void Context::initialize (int argc, char** argv) //////////////////////////////////////////////////////////////////////////////// int Context::run () { - std::cout << "--- start 1.8.0 ---" << std::endl; + std::cout << "--- start 1.8.0 ---" << std::endl; try { parse (); @@ -177,7 +177,7 @@ int Context::run () std::cout << *f << std::endl; } - std::cout << "--- end 1.8.0 ---" << std::endl; + std::cout << "--- end 1.8.0 ---" << std::endl; return 0; } @@ -194,6 +194,9 @@ void Context::loadCorrectConfigFile () messages.push_back (std::string ("Using alternate .taskrc file ") + file); // TODO i18n config.load (file); + + // No need to handle it again. + args.erase (arg); } } @@ -224,6 +227,9 @@ void Context::loadCorrectConfigFile () messages.push_back (std::string ("Configuration override ") + // TODO i18n arg->substr (3, std::string::npos)); } + + // No need to handle it again. + args.erase (arg); } } } @@ -231,119 +237,100 @@ void Context::loadCorrectConfigFile () //////////////////////////////////////////////////////////////////////////////// void Context::parse () { - command = ""; - - bool terminated = false; - bool foundSequence = false; + command = ""; + std::string descCandidate = ""; + bool terminated = false; + bool foundSequence = false; bool foundSomethingAfterSequence = false; - std::string descCandidate = ""; - foreach (arg, args) { - // Skip any argument that starts with "rc:" or "rc." because it has already - // been processed. - if (arg->substr (0, 3) != "rc:" || - arg->substr (0, 3) != "rc.") + if (!terminated) { - if (!terminated) + // The '--' argument shuts off all parsing - everything is an argument. + if (*arg == "--") + terminated = true; + + // Sequence + // Note: "add" doesn't require an ID + else if (command != "add" && + sequence.valid (*arg) && + ! foundSomethingAfterSequence) { - // The '--' argument shuts off all parsing - everything is an argument. - if (*arg == "--") - terminated = true; - - // Sequence - // Note: "add" doesn't require an ID - else if (command != "add" && - sequence.valid (*arg) && - ! foundSomethingAfterSequence) - { - std::cout << "# found sequence" << std::endl; - sequence.parse (*arg); - foundSequence = true; - } - -/* - // Tags begin with + or - and contain arbitrary text. - else if (validTag (arg)) - { - if (foundSequence) - foundSomethingAfterSequence = true; - - if (arg[0] == '+') - task.addTag (arg->substr (1, std::string::npos)); - else if (arg[0] == '-') - task.addRemoveTag (arg->substr (1, std::string::npos)); - } -*/ -/* - // Attributes contain a constant string followed by a colon, followed by a - // value. - else if ((colon = arg->find (":")) != std::string::npos) - { - if (foundSequence) - foundSomethingAfterSequence = true; - - std::string name = lowerCase (arg->substr (0, colon)); - std::string value = arg->substr (colon + 1, std::string::npos); - - if (validAttribute (name, value)) - { - if (name != "recur" || validDuration (value)) - task.setAttribute (name, value); - } - - // If it is not a valid attribute, then allow the argument as part of - // the description. - else - { - if (descCandidate.length ()) - descCandidate += " "; - descCandidate += arg; - } - } -*/ - - // Substitution of description and/or annotation text. - else if (subst.valid (*arg)) - { - if (foundSequence) - foundSomethingAfterSequence = true; - - std::cout << "# found subst" << std::endl; - subst.parse (*arg); - } -/* - // Command. - else if (command == "") - { - if (foundSequence) - foundSomethingAfterSequence = true; - - std::string l = lowerCase (arg); - if (isCommand (l) && validCommand (l)) - command = l; - else - { - if (descCandidate.length ()) - descCandidate += " "; - descCandidate += arg; - } - } -*/ - // Anything else is just considered description. - else - { - if (foundSequence) - foundSomethingAfterSequence = true; - - if (descCandidate.length ()) - descCandidate += " "; - descCandidate += *arg; - } + std::cout << "# found sequence" << std::endl; + sequence.parse (*arg); + foundSequence = true; } - // terminated, therefore everything subsequently is a description. +/* + // Tags begin with + or - and contain arbitrary text. + else if (validTag (arg)) + { + if (foundSequence) + foundSomethingAfterSequence = true; + + if (arg[0] == '+') + task.addTag (arg->substr (1, std::string::npos)); + else if (arg[0] == '-') + task.addRemoveTag (arg->substr (1, std::string::npos)); + } +*/ +/* + // Attributes contain a constant string followed by a colon, followed by a + // value. + else if ((colon = arg->find (":")) != std::string::npos) + { + if (foundSequence) + foundSomethingAfterSequence = true; + + std::string name = lowerCase (arg->substr (0, colon)); + std::string value = arg->substr (colon + 1, std::string::npos); + + if (validAttribute (name, value)) + { + if (name != "recur" || validDuration (value)) + task.setAttribute (name, value); + } + + // If it is not a valid attribute, then allow the argument as part of + // the description. + else + { + if (descCandidate.length ()) + descCandidate += " "; + descCandidate += arg; + } + } +*/ + + // Substitution of description and/or annotation text. + else if (subst.valid (*arg)) + { + if (foundSequence) + foundSomethingAfterSequence = true; + + std::cout << "# found subst" << std::endl; + subst.parse (*arg); + } +/* + // Command. + else if (command == "") + { + if (foundSequence) + foundSomethingAfterSequence = true; + + std::string l = lowerCase (arg); + if (isCommand (l) && validCommand (l)) + command = l; + else + { + if (descCandidate.length ()) + descCandidate += " "; + descCandidate += arg; + } + } +*/ + // Anything else is just considered description. else { if (foundSequence) @@ -354,6 +341,17 @@ void Context::parse () descCandidate += *arg; } } + + // terminated, therefore everything subsequently is a description. + else + { + if (foundSequence) + foundSomethingAfterSequence = true; + + if (descCandidate.length ()) + descCandidate += " "; + descCandidate += *arg; + } } if (validDescription (descCandidate)) diff --git a/src/parse.cpp b/src/parse.cpp index b96c89d18..417c75fe4 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -440,108 +440,93 @@ void parse ( { std::string arg (args[i]); - // Ignore any argument that is "rc:...", because that is the command line - // specified rc file. - if (arg.substr (0, 3) != "rc:") + if (!terminated) { - if (!terminated) + size_t colon; // Pointer to colon in argument. + std::string from; + std::string to; + bool global; + std::vector sequence; + + // The '--' argument shuts off all parsing - everything is an argument. + if (arg == "--") + terminated = true; + + // An id is the first argument found that contains all digits. + else if (lowerCase (command) != "add" && // "add" doesn't require an ID + validSequence (arg, sequence) && + ! foundSomethingAfterSequence) { - size_t colon; // Pointer to colon in argument. - std::string from; - std::string to; - bool global; - std::vector sequence; + foundSequence = true; + foreach (id, sequence) + task.addId (*id); + } - // The '--' argument shuts off all parsing - everything is an argument. - if (arg == "--") - terminated = true; + // Tags begin with + or - and contain arbitrary text. + else if (validTag (arg)) + { + if (foundSequence) + foundSomethingAfterSequence = true; - // An id is the first argument found that contains all digits. - else if (lowerCase (command) != "add" && // "add" doesn't require an ID - validSequence (arg, sequence) && - ! foundSomethingAfterSequence) + if (arg[0] == '+') + task.addTag (arg.substr (1, std::string::npos)); + else if (arg[0] == '-') + task.addRemoveTag (arg.substr (1, std::string::npos)); + } + + // Attributes contain a constant string followed by a colon, followed by a + // value. + else if ((colon = arg.find (":")) != std::string::npos) + { + if (foundSequence) + foundSomethingAfterSequence = true; + + std::string name = lowerCase (arg.substr (0, colon)); + std::string value = arg.substr (colon + 1, std::string::npos); + + if (validAttribute (name, value)) { - foundSequence = true; - foreach (id, sequence) - task.addId (*id); + if (name != "recur" || validDuration (value)) + task.setAttribute (name, value); } - // Tags begin with + or - and contain arbitrary text. - else if (validTag (arg)) - { - if (foundSequence) - foundSomethingAfterSequence = true; - - if (arg[0] == '+') - task.addTag (arg.substr (1, std::string::npos)); - else if (arg[0] == '-') - task.addRemoveTag (arg.substr (1, std::string::npos)); - } - - // Attributes contain a constant string followed by a colon, followed by a - // value. - else if ((colon = arg.find (":")) != std::string::npos) - { - if (foundSequence) - foundSomethingAfterSequence = true; - - std::string name = lowerCase (arg.substr (0, colon)); - std::string value = arg.substr (colon + 1, std::string::npos); - - if (validAttribute (name, value)) - { - if (name != "recur" || validDuration (value)) - task.setAttribute (name, value); - } - - // If it is not a valid attribute, then allow the argument as part of - // the description. - else - { - if (descCandidate.length ()) - descCandidate += " "; - descCandidate += arg; - } - } - - // Substitution of description text. - else if (validSubstitution (arg, from, to, global)) - { - if (foundSequence) - foundSomethingAfterSequence = true; - - task.setSubstitution (from, to, global); - } - - // Command. - else if (command == "") - { - if (foundSequence) - foundSomethingAfterSequence = true; - - std::string l = lowerCase (arg); - if (isCommand (l) && validCommand (l)) - command = l; - else - { - if (descCandidate.length ()) - descCandidate += " "; - descCandidate += arg; - } - } - - // Anything else is just considered description. + // If it is not a valid attribute, then allow the argument as part of + // the description. else { - if (foundSequence) - foundSomethingAfterSequence = true; - if (descCandidate.length ()) descCandidate += " "; descCandidate += arg; } } - // terminated, therefore everything subsequently is a description. + + // Substitution of description text. + else if (validSubstitution (arg, from, to, global)) + { + if (foundSequence) + foundSomethingAfterSequence = true; + + task.setSubstitution (from, to, global); + } + + // Command. + else if (command == "") + { + if (foundSequence) + foundSomethingAfterSequence = true; + + std::string l = lowerCase (arg); + if (isCommand (l) && validCommand (l)) + command = l; + else + { + if (descCandidate.length ()) + descCandidate += " "; + descCandidate += arg; + } + } + + // Anything else is just considered description. else { if (foundSequence) @@ -552,6 +537,16 @@ void parse ( descCandidate += arg; } } + // terminated, therefore everything subsequently is a description. + else + { + if (foundSequence) + foundSomethingAfterSequence = true; + + if (descCandidate.length ()) + descCandidate += " "; + descCandidate += arg; + } } if (validDescription (descCandidate)) diff --git a/src/report.cpp b/src/report.cpp index 879d0b003..2e67765a1 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -35,7 +35,7 @@ #include #include -#include "Config.h" +#include "Context.h" #include "Date.h" #include "Table.h" #include "TDB.h" diff --git a/src/task.cpp b/src/task.cpp index bdf6d18eb..8433b2a9b 100644 --- a/src/task.cpp +++ b/src/task.cpp @@ -36,7 +36,7 @@ #include #include -#include "Config.h" +#include "Context.h" #include "Date.h" #include "Duration.h" #include "Table.h" @@ -299,13 +299,15 @@ int main (int argc, char** argv) srand (time (NULL)); #endif + int status = 0; + try { context.initialize (argc, argv); if (context.args[0].find ("itask") != std::string::npos) - /* return */ context.interactive (); + status = context.interactive (); else - /* return */ context.run (); + status = context.run (); // start OBSOLETE TDB tdb; @@ -329,7 +331,7 @@ int main (int argc, char** argv) "overwrite your completed tasks. Please change it."); } - std::cout << runTaskCommand (argc, argv, tdb); + std::cout << runTaskCommand (context.args, tdb); } catch (std::string& error) @@ -345,7 +347,7 @@ int main (int argc, char** argv) } // end OBSOLETE - return 0; + return status; } //////////////////////////////////////////////////////////////////////////////// @@ -812,7 +814,12 @@ std::string runTaskCommand ( { std::vector args; for (int i = 1; i < argc; ++i) - args.push_back (argv[i]); + if (strncmp (argv[i], "rc:", 3) && + strncmp (argv[i], "rc.", 3)) +{ +std::cout << "arg=" << argv[i] << std::endl; + args.push_back (argv[i]); +} return runTaskCommand (args, tdb, gc, shadow); } @@ -824,12 +831,10 @@ std::string runTaskCommand ( bool gc /* = false */, bool shadow /* = false */) { - // If argc == 1 and the default.command configuration variable is set, - // then use that, otherwise stick with argc/argv. + // 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 || - (args.size () == 1 && args[0].substr (0, 3) == "rc:")) && - defaultCommand != "") + if (args.size () == 0 || defaultCommand != "") { // Stuff the command line. args.clear (); diff --git a/src/tests/filt.t.cpp b/src/tests/filt.t.cpp index d7a035cbf..0d4492a88 100644 --- a/src/tests/filt.t.cpp +++ b/src/tests/filt.t.cpp @@ -57,11 +57,11 @@ int main (int argc, char** argv) T2 no1; no1.set ("name3", "value3"); - test.notok (f.pass (no0), "no match against mismatch T2"); + test.notok (f.pass (no1), "no match against mismatch T2"); T2 partial; partial.set ("name1", "value1"); - test.notok (f.pass (no0), "no match against partial T2"); + test.notok (f.pass (partial), "no match against partial T2"); // Modifiers. T2 mods;