diff options
author | David Crocker <dcrocker@eschertech.com> | 2021-08-21 19:22:56 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2021-08-21 19:22:56 +0300 |
commit | 65510a271617119f47395e28b8ba42740776e66e (patch) | |
tree | 7c9d9c50a299de719a23f90e607489abf4ea1557 /src/GCodes/GCodeBuffer | |
parent | c651d94883ab123e569aa3e1b19464dade60b5e3 (diff) |
Implemented redirection to file in echo command
Diffstat (limited to 'src/GCodes/GCodeBuffer')
-rw-r--r-- | src/GCodes/GCodeBuffer/StringParser.cpp | 51 |
1 files changed, 45 insertions, 6 deletions
diff --git a/src/GCodes/GCodeBuffer/StringParser.cpp b/src/GCodes/GCodeBuffer/StringParser.cpp index 4876f3d6..9da35b71 100644 --- a/src/GCodes/GCodeBuffer/StringParser.cpp +++ b/src/GCodes/GCodeBuffer/StringParser.cpp @@ -306,7 +306,7 @@ bool StringParser::CheckMetaCommand(const StringRef& reply) THROWS(GCodeExceptio { if (overflowed) { - throw GCodeException(gb.GetLineNumber(), ARRAY_SIZE(gb.buffer) - 1, "GCode command too long"); + throw GCodeException(gb.GetLineNumber(), ARRAY_SIZE(gb.buffer) + commandIndent - 1, "GCode command too long"); } const bool doingFile = gb.IsDoingFile(); @@ -742,14 +742,41 @@ void StringParser::ProcessAbortCommand(const StringRef& reply) noexcept reprap.GetGCodes().AbortPrint(gb); } -void StringParser::ProcessEchoCommand(const StringRef& reply) +void StringParser::ProcessEchoCommand(const StringRef& reply) THROWS(GCodeException) { + SkipWhiteSpace(); + FileData outputFile; + + if (gb.buffer[readPointer] == '>') + { + // Redirect the line to file + ++readPointer; + OpenMode openMode; + if (gb.buffer[readPointer] == '>') + { + openMode = OpenMode::append; + ++readPointer; + } + else + { + openMode = OpenMode::write; + } + String<MaxFilenameLength> filename; + GetQuotedString(filename.GetRef(), false); + FileStore * const f = reprap.GetPlatform().OpenSysFile(filename.c_str(), openMode); + if (f == nullptr) + { + throw GCodeException(gb.GetLineNumber(), readPointer + commandIndent, "Failed to create or open file"); + } + outputFile.Set(f); + } + while (true) { SkipWhiteSpace(); if (gb.buffer[readPointer] == 0) { - return; + break; } ExpressionParser parser(gb, gb.buffer + readPointer, gb.buffer + ARRAY_SIZE(gb.buffer), commandIndent + readPointer); const ExpressionValue val = parser.Parse(); @@ -769,10 +796,22 @@ void StringParser::ProcessEchoCommand(const StringRef& reply) throw ConstructParseException("expected ','"); } } + + if (outputFile.IsLive()) + { + reply.cat('\n'); + const bool ok = outputFile.Write(reply.c_str()); + outputFile.Close(); + reply.Clear(); + if (!ok) + { + throw GCodeException(gb.GetLineNumber(), -1, "Failed to write to redirect file"); + } + } } // Evaluate the condition that should follow 'if' or 'while' -bool StringParser::EvaluateCondition() +bool StringParser::EvaluateCondition() THROWS(GCodeException) { ExpressionParser parser(gb, gb.buffer + readPointer, gb.buffer + ARRAY_SIZE(gb.buffer), commandIndent + readPointer); const bool b = parser.ParseBoolean(); @@ -1222,7 +1261,7 @@ void StringParser::CheckArrayLength(size_t actualLength, size_t maxLength) THROW } } -// Get and copy a quoted string returning true if successful +// Get and copy a quoted string returning true if successful, leaving the read pointer just after the string void StringParser::GetQuotedString(const StringRef& str, bool allowEmpty) THROWS(GCodeException) { if (readPointer <= 0) @@ -1256,7 +1295,7 @@ void StringParser::GetQuotedString(const StringRef& str, bool allowEmpty) THROWS } } -// Given that the current character is double-quote, fetch the quoted string +// Given that the current character is double-quote, fetch the quoted string, leaving the read pointer just after the string void StringParser::InternalGetQuotedString(const StringRef& str) THROWS(GCodeException) { str.Clear(); |