From 086f7ec684678cc2b27c0b56d7f1e93a290de928 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 2 Nov 2014 22:50:56 -0500 Subject: [PATCH] CLI - Half-implemented ::injectDefaults. This is a highly problematic method, and needs some thinking. --- src/CLI.cpp | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/CLI.h | 1 + 2 files changed, 98 insertions(+) diff --git a/src/CLI.cpp b/src/CLI.cpp index 381b32270..a9a2637c6 100644 --- a/src/CLI.cpp +++ b/src/CLI.cpp @@ -349,6 +349,7 @@ void CLI::analyze (bool parse /* = true */) desugarPatterns (); findIDs (); findUUIDs (); + injectDefaults (); insertIDExpr (); findOperators (); findAttributes (); @@ -1711,6 +1712,102 @@ void CLI::insertJunctions () } } +//////////////////////////////////////////////////////////////////////////////// +void CLI::injectDefaults () +{ + // Scan the top-level branches for evidence of ID, UUID, overrides and other + // arguments. + bool changes = false; + bool found_command = false; + bool found_sequence = false; + bool found_terminator = false; + + std::vector ::iterator a; + for (a = _args.begin (); a != _args.end (); ++a) + { + if (a->hasTag ("TERMINATOR")) + found_terminator = true; + + if (! found_terminator && a->hasTag ("CMD")) + found_command = true; + + else if (! found_terminator && (a->hasTag ("ID") || a->hasTag ("UUID"))) + found_sequence = true; + } + + // TODO Remove + context.debug (std::string ("CLI::injectDefaults found_command ") + (found_command ? "true" : "false")); + context.debug (std::string ("CLI::injectDefaults found_sequence ") + (found_sequence ? "true" : "false")); + + // If no command was specified, then a command will be inserted. + if (! found_command) + { + // Default command. + if (! found_sequence) + { + // Apply overrides, if any. + std::string defaultCommand = context.config.get ("default.command"); + if (defaultCommand != "") + { + if (context.config.getBoolean ("debug")) + context.debug (std::string ("No command or sequence found - assuming default.command '") + defaultCommand + "'."); + +/* + // Split the defaultCommand into args, and add them in reverse order, + // because captureFirst inserts args immediately after the command, and + // so has the effect of reversing the list. + std::vector args; + split (args, defaultCommand, ' '); + std::vector ::reverse_iterator r; + for (r = args.rbegin (); r != args.rend (); ++r) + { + if (*r != "") + { + Tree* t = captureFirst (*r); + t->tag ("DEFAULT"); + } + } + + std::string combined; + std::vector nodes; + collect (nodes, collectTerminated); + std::vector ::iterator i; + for (i = nodes.begin (); i != nodes.end (); ++i) + { + if (combined.length ()) + combined += ' '; + + combined += (*i)->attribute ("raw"); + } + + context.header ("[" + combined + "]"); +*/ + } + else + { +/* + throw std::string (STRING_TRIVIAL_INPUT); +*/ + } + } + else + { +/* + if (context.config.getBoolean ("debug")) + context.debug ("Sequence but no command found - assuming 'information' command."); + context.header (STRING_ASSUME_INFO); + + Tree* t = captureFirst ("information"); + t->tag ("ASSUMED"); +*/ + } + } + + if (changes && + context.config.getInteger ("debug.parser") >= 3) + context.debug (context.cli.dump ("CLI::analyze injectDefaults")); +} + //////////////////////////////////////////////////////////////////////////////// void CLI::decomposeModAttributes () { diff --git a/src/CLI.h b/src/CLI.h index 1e92bfa23..76849f0ea 100644 --- a/src/CLI.h +++ b/src/CLI.h @@ -101,6 +101,7 @@ private: void findOperators (); void findAttributes (); void insertJunctions (); + void injectDefaults (); void decomposeModAttributes (); void decomposeModAttributeModifiers (); void decomposeModTags ();