From 3bef7a445080fcb133dfa49aefddc0f28057b226 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <thinkyhead@users.noreply.github.com>
Date: Mon, 10 Feb 2020 14:52:15 -0600
Subject: [PATCH] Add g-code quoted strings, improve stream code (#16818)

---
 Marlin/Configuration_adv.h                 |   4 +
 Marlin/src/gcode/host/M115.cpp             |  15 +-
 Marlin/src/gcode/parser.cpp                |  70 ++++++---
 Marlin/src/gcode/parser.h                  |  10 ++
 Marlin/src/gcode/queue.cpp                 | 174 ++++++++++-----------
 buildroot/share/tests/megaatmega1280-tests |   2 +-
 6 files changed, 163 insertions(+), 112 deletions(-)

diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h
index 2c3cd38def..2e0a5b5c50 100644
--- a/Marlin/Configuration_adv.h
+++ b/Marlin/Configuration_adv.h
@@ -2784,6 +2784,10 @@
  */
 #define FASTER_GCODE_PARSER
 
+#if ENABLED(FASTER_GCODE_PARSER)
+  //#define GCODE_QUOTED_STRINGS  // Support for quoted string parameters
+#endif
+
 /**
  * CNC G-code options
  * Support CNC-style G-code dialects used by laser cutters, drawing machine cams, etc.
diff --git a/Marlin/src/gcode/host/M115.cpp b/Marlin/src/gcode/host/M115.cpp
index 9123bc6e62..bce0c89184 100644
--- a/Marlin/src/gcode/host/M115.cpp
+++ b/Marlin/src/gcode/host/M115.cpp
@@ -33,7 +33,9 @@
 #endif
 
 /**
- * M115: Capabilities string
+ * M115: Capabilities string and extended capabilities report
+ *       If a capability is not reported, hosts should assume
+ *       the capability is not present.
  */
 void GcodeSuite::M115() {
 
@@ -41,6 +43,16 @@ void GcodeSuite::M115() {
 
   #if ENABLED(EXTENDED_CAPABILITIES_REPORT)
 
+    // PAREN_COMMENTS
+    #if ENABLED(PAREN_COMMENTS)
+      cap_line(PSTR("PAREN_COMMENTS"), true);
+    #endif
+
+    // QUOTED_STRINGS
+    #if ENABLED(GCODE_QUOTED_STRINGS)
+      cap_line(PSTR("QUOTED_STRINGS"), true);
+    #endif
+
     // SERIAL_XON_XOFF
     cap_line(PSTR("SERIAL_XON_XOFF")
       #if ENABLED(SERIAL_XON_XOFF)
@@ -171,6 +183,5 @@ void GcodeSuite::M115() {
       #endif
     );
 
-
   #endif // EXTENDED_CAPABILITIES_REPORT
 }
diff --git a/Marlin/src/gcode/parser.cpp b/Marlin/src/gcode/parser.cpp
index 75f88df76b..8218ee53d8 100644
--- a/Marlin/src/gcode/parser.cpp
+++ b/Marlin/src/gcode/parser.cpp
@@ -92,6 +92,26 @@ void GCodeParser::reset() {
   #endif
 }
 
+#if ENABLED(GCODE_QUOTED_STRINGS)
+
+  // Pass the address after the first quote (if any)
+  char* GCodeParser::unescape_string(char* &src) {
+    if (*src == '"') ++src;     // Skip the leading quote
+    char * const out = src;     // Start of the string
+    char *dst = src;            // Prepare to unescape and terminate
+    for (;;) {
+      char c = *src++;          // Get the next char
+      switch (c) {
+        case '\\': c = *src++; break; // Get the escaped char
+        case '"' : c = '\0'; break;   // Convert bare quote to nul
+      }
+      if (!(*dst++ = c)) break; // Copy and break on nul
+    }
+    return out;
+  }
+
+#endif
+
 // Populate all fields by parsing a single line of GCode
 // 58 bytes of SRAM are used to speed up seen/value
 void GCodeParser::parse(char *p) {
@@ -229,17 +249,12 @@ void GCodeParser::parse(char *p) {
     #if ENABLED(EXPECTED_PRINTER_CHECK)
       case 16:
     #endif
-    case 23: case 28: case 30: case 117: case 118: case 928: string_arg = p; return;
+    case 23: case 28: case 30: case 117: case 118: case 928:
+      string_arg = unescape_string(p);
+      return;
     default: break;
   }
-/*
-  #if ENABLED(CANCEL_OBJECTS)
-  if (letter == 'O') switch (codenum) {
-    case 1:  string_arg = p; return;
-    default: break;
-  }
-  #endif
-*/
+
   #if ENABLED(DEBUG_GCODE_PARSER)
     const bool debug = codenum == 800;
   #endif
@@ -252,21 +267,31 @@ void GCodeParser::parse(char *p) {
    * This allows M0/M1 with expire time to work: "M0 S5 You Win!"
    * For 'M118' you must use 'E1' and 'A1' rather than just 'E' or 'A'
    */
+  #if ENABLED(GCODE_QUOTED_STRINGS)
+    bool quoted_string_arg = false;
+  #endif
   string_arg = nullptr;
-  while (const char code = *p++) {              // Get the next parameter. A NUL ends the loop
+  while (const char param = *p++) {              // Get the next parameter. A NUL ends the loop
 
     // Special handling for M32 [P] !/path/to/file.g#
     // The path must be the last parameter
-    if (code == '!' && letter == 'M' && codenum == 32) {
+    if (param == '!' && letter == 'M' && codenum == 32) {
       string_arg = p;                           // Name starts after '!'
       char * const lb = strchr(p, '#');         // Already seen '#' as SD char (to pause buffering)
       if (lb) *lb = '\0';                       // Safe to mark the end of the filename
       return;
     }
 
+    #if ENABLED(GCODE_QUOTED_STRINGS)
+      if (!quoted_string_arg && param == '"') {
+        quoted_string_arg = true;
+        string_arg = unescape_string(p);
+      }
+    #endif
+
     // Arguments MUST be uppercase for fast GCode parsing
     #if ENABLED(FASTER_GCODE_PARSER)
-      #define PARAM_TEST WITHIN(code, 'A', 'Z')
+      #define PARAM_TEST WITHIN(param, 'A', 'Z')
     #else
       #define PARAM_TEST true
     #endif
@@ -275,16 +300,22 @@ void GCodeParser::parse(char *p) {
 
       while (*p == ' ') p++;                    // Skip spaces between parameters & values
 
-      const bool has_num = valid_float(p);
+      #if ENABLED(GCODE_QUOTED_STRINGS)
+        const bool is_str = (*p == '"'), has_val = is_str || valid_float(p);
+        char * const valptr = has_val ? is_str ? unescape_string(p) : p : nullptr;
+      #else
+        const bool has_val = valid_float(p);
+        char * const valptr = has_val ? p : nullptr;
+      #endif
 
       #if ENABLED(DEBUG_GCODE_PARSER)
         if (debug) {
-          SERIAL_ECHOPAIR("Got letter ", code, " at index ", (int)(p - command_ptr - 1));
-          if (has_num) SERIAL_ECHOPGM(" (has_num)");
+          SERIAL_ECHOPAIR("Got param ", param, " at index ", (int)(p - command_ptr - 1));
+          if (has_val) SERIAL_ECHOPGM(" (has_val)");
         }
       #endif
 
-      if (!has_num && !string_arg) {            // No value? First time, keep as string_arg
+      if (!has_val && !string_arg) {            // No value? First time, keep as string_arg
         string_arg = p - 1;
         #if ENABLED(DEBUG_GCODE_PARSER)
           if (debug) SERIAL_ECHOPAIR(" string_arg: ", hex_address((void*)string_arg)); // DEBUG
@@ -296,7 +327,7 @@ void GCodeParser::parse(char *p) {
       #endif
 
       #if ENABLED(FASTER_GCODE_PARSER)
-        set(code, has_num ? p : nullptr);       // Set parameter exists and pointer (nullptr for no number)
+        set(param, valptr);                     // Set parameter exists and pointer (nullptr for no value)
       #endif
     }
     else if (!string_arg) {                     // Not A-Z? First time, keep as the string_arg
@@ -359,7 +390,7 @@ void GCodeParser::unknown_command_warning() {
       if (seen(c)) {
         SERIAL_ECHOPAIR("Code '", c); SERIAL_ECHOPGM("':");
         if (has_value()) {
-          SERIAL_ECHOPAIR(
+          SERIAL_ECHOLNPAIR(
             "\n    float: ", value_float(),
             "\n     long: ", value_long(),
             "\n    ulong: ", value_ulong(),
@@ -374,8 +405,7 @@ void GCodeParser::unknown_command_warning() {
           );
         }
         else
-          SERIAL_ECHOPGM(" (no value)");
-        SERIAL_ECHOLNPGM("\n");
+          SERIAL_ECHOLNPGM(" (no value)");
       }
     }
   }
diff --git a/Marlin/src/gcode/parser.h b/Marlin/src/gcode/parser.h
index cce8abe5fd..08f8613c17 100644
--- a/Marlin/src/gcode/parser.h
+++ b/Marlin/src/gcode/parser.h
@@ -208,6 +208,12 @@ public:
     return SEEN_TEST('X') || SEEN_TEST('Y') || SEEN_TEST('Z') || SEEN_TEST('E');
   }
 
+  #if ENABLED(GCODE_QUOTED_STRINGS)
+    static char* unescape_string(char* &src);
+  #else
+    FORCE_INLINE static char* unescape_string(char* &src) { return src; }
+  #endif
+
   // Populate all fields by parsing a single line of GCode
   // This uses 54 bytes of SRAM to speed up seen/value
   static void parse(char * p);
@@ -223,6 +229,9 @@ public:
   // Seen a parameter with a value
   static inline bool seenval(const char c) { return seen(c) && has_value(); }
 
+  // Float removes 'E' to prevent scientific notation interpretation
+  static inline char* value_string() { return value_ptr; }
+
   // Float removes 'E' to prevent scientific notation interpretation
   static inline float value_float() {
     if (value_ptr) {
@@ -369,6 +378,7 @@ public:
   void unknown_command_warning();
 
   // Provide simple value accessors with default option
+  static inline char*    stringval(const char c, char * const dval=nullptr) { return seenval(c) ? value_string()   : dval; }
   static inline float    floatval(const char c, const float dval=0.0)   { return seenval(c) ? value_float()        : dval; }
   static inline bool     boolval(const char c, const bool dval=false)   { return seenval(c) ? value_bool()         : (seen(c) ? true : dval); }
   static inline uint8_t  byteval(const char c, const uint8_t dval=0)    { return seenval(c) ? value_byte()         : dval; }
diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp
index 5d317b6ca5..b83a211967 100644
--- a/Marlin/src/gcode/queue.cpp
+++ b/Marlin/src/gcode/queue.cpp
@@ -309,6 +309,66 @@ FORCE_INLINE bool is_M29(const char * const cmd) {  // matches "M29" & "M29 ", b
   return m29 && !NUMERIC(m29[3]);
 }
 
+#define PS_NORMAL 0
+#define PS_EOL    1
+#define PS_QUOTED 2
+#define PS_PAREN  3
+#define PS_ESC    4
+
+inline void process_stream_char(const char c, uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
+
+  if (sis == PS_EOL) return;    // EOL comment or overflow
+
+  #if ENABLED(PAREN_COMMENTS)
+    else if (sis == PS_PAREN) { // Inline comment
+      if (c == ')') sis = PS_NORMAL;
+      return;
+    }
+  #endif
+
+  else if (sis >= PS_ESC)       // End escaped char
+    sis -= PS_ESC;
+
+  else if (c == '\\') {         // Start escaped char
+    sis += PS_ESC;
+    if (sis == PS_ESC) return;  // Keep if quoting
+  }
+
+  #if ENABLED(GCODE_QUOTED_STRINGS)
+
+    else if (sis == PS_QUOTED) {
+      if (c == '"') sis = PS_NORMAL; // End quoted string
+    }
+    else if (c == '"')          // Start quoted string
+      sis = PS_QUOTED;
+
+  #endif
+
+  else if (c == ';') {          // Start end-of-line comment
+    sis = PS_EOL;
+    return;
+  }
+
+  #if ENABLED(PAREN_COMMENTS)
+    else if (c == '(') {        // Start inline comment
+      sis = PS_PAREN;
+      return;
+    }
+  #endif
+
+  buff[ind++] = c;
+  if (ind >= MAX_CMD_SIZE - 1)
+    sis = PS_EOL;               // Skip the rest on overflow
+}
+
+inline bool process_line_done(uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
+  sis = PS_NORMAL;
+  if (!ind) { thermalManager.manage_heater(); return true; }
+  buff[ind] = 0;
+  ind = 0;
+  return false;
+}
+
 /**
  * Get all commands waiting on the serial port and queue them.
  * Exit when the buffer is full or when no more characters are
@@ -316,11 +376,8 @@ FORCE_INLINE bool is_M29(const char * const cmd) {  // matches "M29" & "M29 ", b
  */
 void GCodeQueue::get_serial_commands() {
   static char serial_line_buffer[NUM_SERIAL][MAX_CMD_SIZE];
-  static bool serial_comment_mode[NUM_SERIAL] = { false }
-              #if ENABLED(PAREN_COMMENTS)
-                , serial_comment_paren_mode[NUM_SERIAL] = { false }
-              #endif
-            ;
+
+  static uint8_t serial_input_state[NUM_SERIAL] = { 0 };
 
   #if ENABLED(BINARY_FILE_TRANSFER)
     if (card.flag.binary_mode) {
@@ -350,27 +407,15 @@ void GCodeQueue::get_serial_commands() {
    */
   while (length < BUFSIZE && serial_data_available()) {
     for (uint8_t i = 0; i < NUM_SERIAL; ++i) {
-      int c;
-      if ((c = read_serial(i)) < 0) continue;
 
-      char serial_char = c;
+      const int c = read_serial(i);
+      if (c < 0) continue;
+
+      const char serial_char = c;
 
-      /**
-       * If the character ends the line
-       */
       if (serial_char == '\n' || serial_char == '\r') {
 
-        // Start with comment mode off
-        serial_comment_mode[i] = false;
-        #if ENABLED(PAREN_COMMENTS)
-          serial_comment_paren_mode[i] = false;
-        #endif
-
-        // Skip empty lines and comments
-        if (!serial_count[i]) { thermalManager.manage_heater(); continue; }
-
-        serial_line_buffer[i][serial_count[i]] = 0;       // Terminate string
-        serial_count[i] = 0;                              // Reset buffer
+        process_line_done(serial_input_state[i], serial_line_buffer[i], serial_count[i]);
 
         char* command = serial_line_buffer[i];
 
@@ -409,16 +454,17 @@ void GCodeQueue::get_serial_commands() {
             return gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM), i);
         #endif
 
-        // Movement commands alert when stopped
+        //
+        // Movement commands give an alert when the machine is stopped
+        //
+
         if (IsStopped()) {
           char* gpos = strchr(command, 'G');
           if (gpos) {
             switch (strtol(gpos + 1, nullptr, 10)) {
-              case 0:
-              case 1:
+              case 0: case 1:
               #if ENABLED(ARC_SUPPORT)
-                case 2:
-                case 3:
+                case 2: case 3:
               #endif
               #if ENABLED(BEZIER_CURVE_SUPPORT)
                 case 5:
@@ -453,31 +499,9 @@ void GCodeQueue::get_serial_commands() {
           #endif
         );
       }
-      else if (serial_count[i] >= MAX_CMD_SIZE - 1) {
-        // Keep fetching, but ignore normal characters beyond the max length
-        // The command will be injected when EOL is reached
-      }
-      else if (serial_char == '\\') {  // Handle escapes
-        // if we have one more character, copy it over
-        if ((c = read_serial(i)) >= 0 && !serial_comment_mode[i]
-          #if ENABLED(PAREN_COMMENTS)
-            && !serial_comment_paren_mode[i]
-          #endif
-        )
-          serial_line_buffer[i][serial_count[i]++] = (char)c;
-      }
-      else { // it's not a newline, carriage return or escape char
-        if (serial_char == ';') serial_comment_mode[i] = true;
-        #if ENABLED(PAREN_COMMENTS)
-          else if (serial_char == '(') serial_comment_paren_mode[i] = true;
-          else if (serial_char == ')') serial_comment_paren_mode[i] = false;
-        #endif
-        else if (!serial_comment_mode[i]
-          #if ENABLED(PAREN_COMMENTS)
-            && ! serial_comment_paren_mode[i]
-          #endif
-        ) serial_line_buffer[i][serial_count[i]++] = serial_char;
-      }
+      else
+        process_stream_char(serial_char, serial_input_state[i], serial_line_buffer[i], serial_count[i]);
+
     } // for NUM_SERIAL
   } // queue has space, serial has data
 }
@@ -490,21 +514,17 @@ void GCodeQueue::get_serial_commands() {
    * can also interrupt buffering.
    */
   inline void GCodeQueue::get_sdcard_commands() {
-    static bool sd_comment_mode = false
-                #if ENABLED(PAREN_COMMENTS)
-                  , sd_comment_paren_mode = false
-                #endif
-              ;
+    static uint8_t sd_input_state = PS_NORMAL;
 
     if (!IS_SD_PRINTING()) return;
 
-    uint16_t sd_count = 0;
+    int sd_count = 0;
     bool card_eof = card.eof();
     while (length < BUFSIZE && !card_eof) {
       const int16_t n = card.get();
-      char sd_char = (char)n;
       card_eof = card.eof();
-      if (card_eof || n == -1 || sd_char == '\n' || sd_char == '\r') {
+      const char sd_char = (char)n;
+      if (card_eof || n < 0 || sd_char == '\n' || sd_char == '\r') {
         if (card_eof) {
 
           card.printingHasFinished();
@@ -527,19 +547,10 @@ void GCodeQueue::get_serial_commands() {
             #endif // PRINTER_EVENT_LEDS
           }
         }
-        else if (n == -1)
+        else if (n < 0)
           SERIAL_ERROR_MSG(MSG_SD_ERR_READ);
 
-        sd_comment_mode = false; // for new command
-        #if ENABLED(PAREN_COMMENTS)
-          sd_comment_paren_mode = false;
-        #endif
-
-        // Skip empty lines and comments
-        if (!sd_count) { thermalManager.manage_heater(); continue; }
-
-        command_buffer[index_w][sd_count] = '\0'; // terminate string
-        sd_count = 0; // clear sd line buffer
+        process_line_done(sd_input_state, command_buffer[index_w], sd_count);
 
         _commit_command(false);
 
@@ -547,24 +558,9 @@ void GCodeQueue::get_serial_commands() {
           recovery.cmd_sdpos = card.getIndex(); // Prime for the next _commit_command
         #endif
       }
-      else if (sd_count >= MAX_CMD_SIZE - 1) {
-        /**
-         * Keep fetching, but ignore normal characters beyond the max length
-         * The command will be injected when EOL is reached
-         */
-      }
-      else {
-        if (sd_char == ';') sd_comment_mode = true;
-        #if ENABLED(PAREN_COMMENTS)
-          else if (sd_char == '(') sd_comment_paren_mode = true;
-          else if (sd_char == ')') sd_comment_paren_mode = false;
-        #endif
-        else if (!sd_comment_mode
-          #if ENABLED(PAREN_COMMENTS)
-            && ! sd_comment_paren_mode
-          #endif
-        ) command_buffer[index_w][sd_count++] = sd_char;
-      }
+      else
+        process_stream_char(sd_char, sd_input_state, command_buffer[index_w], sd_count);
+
     }
   }
 
diff --git a/buildroot/share/tests/megaatmega1280-tests b/buildroot/share/tests/megaatmega1280-tests
index 05e262f6e7..4e5ad25355 100644
--- a/buildroot/share/tests/megaatmega1280-tests
+++ b/buildroot/share/tests/megaatmega1280-tests
@@ -19,7 +19,7 @@ restore_configs
 opt_set LCD_LANGUAGE an
 opt_enable SPINDLE_FEATURE ULTIMAKERCONTROLLER LCD_BED_LEVELING \
            MESH_BED_LEVELING ENABLE_LEVELING_FADE_HEIGHT MESH_G28_REST_ORIGIN \
-           G26_MESH_VALIDATION MESH_EDIT_MENU
+           G26_MESH_VALIDATION MESH_EDIT_MENU GCODE_QUOTED_STRINGS
 exec_test $1 $2 "Spindle, MESH_BED_LEVELING, and LCD"