Browse Source

Add g-code quoted strings, improve stream code (#16818)

Scott Lahteine 4 years ago
parent
commit
3bef7a4450
No account linked to committer's email address

+ 4
- 0
Marlin/Configuration_adv.h View File

@@ -2784,6 +2784,10 @@
2784 2784
  */
2785 2785
 #define FASTER_GCODE_PARSER
2786 2786
 
2787
+#if ENABLED(FASTER_GCODE_PARSER)
2788
+  //#define GCODE_QUOTED_STRINGS  // Support for quoted string parameters
2789
+#endif
2790
+
2787 2791
 /**
2788 2792
  * CNC G-code options
2789 2793
  * Support CNC-style G-code dialects used by laser cutters, drawing machine cams, etc.

+ 13
- 2
Marlin/src/gcode/host/M115.cpp View File

@@ -33,7 +33,9 @@
33 33
 #endif
34 34
 
35 35
 /**
36
- * M115: Capabilities string
36
+ * M115: Capabilities string and extended capabilities report
37
+ *       If a capability is not reported, hosts should assume
38
+ *       the capability is not present.
37 39
  */
38 40
 void GcodeSuite::M115() {
39 41
 
@@ -41,6 +43,16 @@ void GcodeSuite::M115() {
41 43
 
42 44
   #if ENABLED(EXTENDED_CAPABILITIES_REPORT)
43 45
 
46
+    // PAREN_COMMENTS
47
+    #if ENABLED(PAREN_COMMENTS)
48
+      cap_line(PSTR("PAREN_COMMENTS"), true);
49
+    #endif
50
+
51
+    // QUOTED_STRINGS
52
+    #if ENABLED(GCODE_QUOTED_STRINGS)
53
+      cap_line(PSTR("QUOTED_STRINGS"), true);
54
+    #endif
55
+
44 56
     // SERIAL_XON_XOFF
45 57
     cap_line(PSTR("SERIAL_XON_XOFF")
46 58
       #if ENABLED(SERIAL_XON_XOFF)
@@ -171,6 +183,5 @@ void GcodeSuite::M115() {
171 183
       #endif
172 184
     );
173 185
 
174
-
175 186
   #endif // EXTENDED_CAPABILITIES_REPORT
176 187
 }

+ 50
- 20
Marlin/src/gcode/parser.cpp View File

@@ -92,6 +92,26 @@ void GCodeParser::reset() {
92 92
   #endif
93 93
 }
94 94
 
95
+#if ENABLED(GCODE_QUOTED_STRINGS)
96
+
97
+  // Pass the address after the first quote (if any)
98
+  char* GCodeParser::unescape_string(char* &src) {
99
+    if (*src == '"') ++src;     // Skip the leading quote
100
+    char * const out = src;     // Start of the string
101
+    char *dst = src;            // Prepare to unescape and terminate
102
+    for (;;) {
103
+      char c = *src++;          // Get the next char
104
+      switch (c) {
105
+        case '\\': c = *src++; break; // Get the escaped char
106
+        case '"' : c = '\0'; break;   // Convert bare quote to nul
107
+      }
108
+      if (!(*dst++ = c)) break; // Copy and break on nul
109
+    }
110
+    return out;
111
+  }
112
+
113
+#endif
114
+
95 115
 // Populate all fields by parsing a single line of GCode
96 116
 // 58 bytes of SRAM are used to speed up seen/value
97 117
 void GCodeParser::parse(char *p) {
@@ -229,17 +249,12 @@ void GCodeParser::parse(char *p) {
229 249
     #if ENABLED(EXPECTED_PRINTER_CHECK)
230 250
       case 16:
231 251
     #endif
232
-    case 23: case 28: case 30: case 117: case 118: case 928: string_arg = p; return;
233
-    default: break;
234
-  }
235
-/*
236
-  #if ENABLED(CANCEL_OBJECTS)
237
-  if (letter == 'O') switch (codenum) {
238
-    case 1:  string_arg = p; return;
252
+    case 23: case 28: case 30: case 117: case 118: case 928:
253
+      string_arg = unescape_string(p);
254
+      return;
239 255
     default: break;
240 256
   }
241
-  #endif
242
-*/
257
+
243 258
   #if ENABLED(DEBUG_GCODE_PARSER)
244 259
     const bool debug = codenum == 800;
245 260
   #endif
@@ -252,21 +267,31 @@ void GCodeParser::parse(char *p) {
252 267
    * This allows M0/M1 with expire time to work: "M0 S5 You Win!"
253 268
    * For 'M118' you must use 'E1' and 'A1' rather than just 'E' or 'A'
254 269
    */
270
+  #if ENABLED(GCODE_QUOTED_STRINGS)
271
+    bool quoted_string_arg = false;
272
+  #endif
255 273
   string_arg = nullptr;
256
-  while (const char code = *p++) {              // Get the next parameter. A NUL ends the loop
274
+  while (const char param = *p++) {              // Get the next parameter. A NUL ends the loop
257 275
 
258 276
     // Special handling for M32 [P] !/path/to/file.g#
259 277
     // The path must be the last parameter
260
-    if (code == '!' && letter == 'M' && codenum == 32) {
278
+    if (param == '!' && letter == 'M' && codenum == 32) {
261 279
       string_arg = p;                           // Name starts after '!'
262 280
       char * const lb = strchr(p, '#');         // Already seen '#' as SD char (to pause buffering)
263 281
       if (lb) *lb = '\0';                       // Safe to mark the end of the filename
264 282
       return;
265 283
     }
266 284
 
285
+    #if ENABLED(GCODE_QUOTED_STRINGS)
286
+      if (!quoted_string_arg && param == '"') {
287
+        quoted_string_arg = true;
288
+        string_arg = unescape_string(p);
289
+      }
290
+    #endif
291
+
267 292
     // Arguments MUST be uppercase for fast GCode parsing
268 293
     #if ENABLED(FASTER_GCODE_PARSER)
269
-      #define PARAM_TEST WITHIN(code, 'A', 'Z')
294
+      #define PARAM_TEST WITHIN(param, 'A', 'Z')
270 295
     #else
271 296
       #define PARAM_TEST true
272 297
     #endif
@@ -275,16 +300,22 @@ void GCodeParser::parse(char *p) {
275 300
 
276 301
       while (*p == ' ') p++;                    // Skip spaces between parameters & values
277 302
 
278
-      const bool has_num = valid_float(p);
303
+      #if ENABLED(GCODE_QUOTED_STRINGS)
304
+        const bool is_str = (*p == '"'), has_val = is_str || valid_float(p);
305
+        char * const valptr = has_val ? is_str ? unescape_string(p) : p : nullptr;
306
+      #else
307
+        const bool has_val = valid_float(p);
308
+        char * const valptr = has_val ? p : nullptr;
309
+      #endif
279 310
 
280 311
       #if ENABLED(DEBUG_GCODE_PARSER)
281 312
         if (debug) {
282
-          SERIAL_ECHOPAIR("Got letter ", code, " at index ", (int)(p - command_ptr - 1));
283
-          if (has_num) SERIAL_ECHOPGM(" (has_num)");
313
+          SERIAL_ECHOPAIR("Got param ", param, " at index ", (int)(p - command_ptr - 1));
314
+          if (has_val) SERIAL_ECHOPGM(" (has_val)");
284 315
         }
285 316
       #endif
286 317
 
287
-      if (!has_num && !string_arg) {            // No value? First time, keep as string_arg
318
+      if (!has_val && !string_arg) {            // No value? First time, keep as string_arg
288 319
         string_arg = p - 1;
289 320
         #if ENABLED(DEBUG_GCODE_PARSER)
290 321
           if (debug) SERIAL_ECHOPAIR(" string_arg: ", hex_address((void*)string_arg)); // DEBUG
@@ -296,7 +327,7 @@ void GCodeParser::parse(char *p) {
296 327
       #endif
297 328
 
298 329
       #if ENABLED(FASTER_GCODE_PARSER)
299
-        set(code, has_num ? p : nullptr);       // Set parameter exists and pointer (nullptr for no number)
330
+        set(param, valptr);                     // Set parameter exists and pointer (nullptr for no value)
300 331
       #endif
301 332
     }
302 333
     else if (!string_arg) {                     // Not A-Z? First time, keep as the string_arg
@@ -359,7 +390,7 @@ void GCodeParser::unknown_command_warning() {
359 390
       if (seen(c)) {
360 391
         SERIAL_ECHOPAIR("Code '", c); SERIAL_ECHOPGM("':");
361 392
         if (has_value()) {
362
-          SERIAL_ECHOPAIR(
393
+          SERIAL_ECHOLNPAIR(
363 394
             "\n    float: ", value_float(),
364 395
             "\n     long: ", value_long(),
365 396
             "\n    ulong: ", value_ulong(),
@@ -374,8 +405,7 @@ void GCodeParser::unknown_command_warning() {
374 405
           );
375 406
         }
376 407
         else
377
-          SERIAL_ECHOPGM(" (no value)");
378
-        SERIAL_ECHOLNPGM("\n");
408
+          SERIAL_ECHOLNPGM(" (no value)");
379 409
       }
380 410
     }
381 411
   }

+ 10
- 0
Marlin/src/gcode/parser.h View File

@@ -208,6 +208,12 @@ public:
208 208
     return SEEN_TEST('X') || SEEN_TEST('Y') || SEEN_TEST('Z') || SEEN_TEST('E');
209 209
   }
210 210
 
211
+  #if ENABLED(GCODE_QUOTED_STRINGS)
212
+    static char* unescape_string(char* &src);
213
+  #else
214
+    FORCE_INLINE static char* unescape_string(char* &src) { return src; }
215
+  #endif
216
+
211 217
   // Populate all fields by parsing a single line of GCode
212 218
   // This uses 54 bytes of SRAM to speed up seen/value
213 219
   static void parse(char * p);
@@ -224,6 +230,9 @@ public:
224 230
   static inline bool seenval(const char c) { return seen(c) && has_value(); }
225 231
 
226 232
   // Float removes 'E' to prevent scientific notation interpretation
233
+  static inline char* value_string() { return value_ptr; }
234
+
235
+  // Float removes 'E' to prevent scientific notation interpretation
227 236
   static inline float value_float() {
228 237
     if (value_ptr) {
229 238
       char *e = value_ptr;
@@ -369,6 +378,7 @@ public:
369 378
   void unknown_command_warning();
370 379
 
371 380
   // Provide simple value accessors with default option
381
+  static inline char*    stringval(const char c, char * const dval=nullptr) { return seenval(c) ? value_string()   : dval; }
372 382
   static inline float    floatval(const char c, const float dval=0.0)   { return seenval(c) ? value_float()        : dval; }
373 383
   static inline bool     boolval(const char c, const bool dval=false)   { return seenval(c) ? value_bool()         : (seen(c) ? true : dval); }
374 384
   static inline uint8_t  byteval(const char c, const uint8_t dval=0)    { return seenval(c) ? value_byte()         : dval; }

+ 85
- 89
Marlin/src/gcode/queue.cpp View File

@@ -309,6 +309,66 @@ FORCE_INLINE bool is_M29(const char * const cmd) {  // matches "M29" & "M29 ", b
309 309
   return m29 && !NUMERIC(m29[3]);
310 310
 }
311 311
 
312
+#define PS_NORMAL 0
313
+#define PS_EOL    1
314
+#define PS_QUOTED 2
315
+#define PS_PAREN  3
316
+#define PS_ESC    4
317
+
318
+inline void process_stream_char(const char c, uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
319
+
320
+  if (sis == PS_EOL) return;    // EOL comment or overflow
321
+
322
+  #if ENABLED(PAREN_COMMENTS)
323
+    else if (sis == PS_PAREN) { // Inline comment
324
+      if (c == ')') sis = PS_NORMAL;
325
+      return;
326
+    }
327
+  #endif
328
+
329
+  else if (sis >= PS_ESC)       // End escaped char
330
+    sis -= PS_ESC;
331
+
332
+  else if (c == '\\') {         // Start escaped char
333
+    sis += PS_ESC;
334
+    if (sis == PS_ESC) return;  // Keep if quoting
335
+  }
336
+
337
+  #if ENABLED(GCODE_QUOTED_STRINGS)
338
+
339
+    else if (sis == PS_QUOTED) {
340
+      if (c == '"') sis = PS_NORMAL; // End quoted string
341
+    }
342
+    else if (c == '"')          // Start quoted string
343
+      sis = PS_QUOTED;
344
+
345
+  #endif
346
+
347
+  else if (c == ';') {          // Start end-of-line comment
348
+    sis = PS_EOL;
349
+    return;
350
+  }
351
+
352
+  #if ENABLED(PAREN_COMMENTS)
353
+    else if (c == '(') {        // Start inline comment
354
+      sis = PS_PAREN;
355
+      return;
356
+    }
357
+  #endif
358
+
359
+  buff[ind++] = c;
360
+  if (ind >= MAX_CMD_SIZE - 1)
361
+    sis = PS_EOL;               // Skip the rest on overflow
362
+}
363
+
364
+inline bool process_line_done(uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
365
+  sis = PS_NORMAL;
366
+  if (!ind) { thermalManager.manage_heater(); return true; }
367
+  buff[ind] = 0;
368
+  ind = 0;
369
+  return false;
370
+}
371
+
312 372
 /**
313 373
  * Get all commands waiting on the serial port and queue them.
314 374
  * 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
316 376
  */
317 377
 void GCodeQueue::get_serial_commands() {
318 378
   static char serial_line_buffer[NUM_SERIAL][MAX_CMD_SIZE];
319
-  static bool serial_comment_mode[NUM_SERIAL] = { false }
320
-              #if ENABLED(PAREN_COMMENTS)
321
-                , serial_comment_paren_mode[NUM_SERIAL] = { false }
322
-              #endif
323
-            ;
379
+
380
+  static uint8_t serial_input_state[NUM_SERIAL] = { 0 };
324 381
 
325 382
   #if ENABLED(BINARY_FILE_TRANSFER)
326 383
     if (card.flag.binary_mode) {
@@ -350,27 +407,15 @@ void GCodeQueue::get_serial_commands() {
350 407
    */
351 408
   while (length < BUFSIZE && serial_data_available()) {
352 409
     for (uint8_t i = 0; i < NUM_SERIAL; ++i) {
353
-      int c;
354
-      if ((c = read_serial(i)) < 0) continue;
355 410
 
356
-      char serial_char = c;
411
+      const int c = read_serial(i);
412
+      if (c < 0) continue;
357 413
 
358
-      /**
359
-       * If the character ends the line
360
-       */
361
-      if (serial_char == '\n' || serial_char == '\r') {
362
-
363
-        // Start with comment mode off
364
-        serial_comment_mode[i] = false;
365
-        #if ENABLED(PAREN_COMMENTS)
366
-          serial_comment_paren_mode[i] = false;
367
-        #endif
414
+      const char serial_char = c;
368 415
 
369
-        // Skip empty lines and comments
370
-        if (!serial_count[i]) { thermalManager.manage_heater(); continue; }
416
+      if (serial_char == '\n' || serial_char == '\r') {
371 417
 
372
-        serial_line_buffer[i][serial_count[i]] = 0;       // Terminate string
373
-        serial_count[i] = 0;                              // Reset buffer
418
+        process_line_done(serial_input_state[i], serial_line_buffer[i], serial_count[i]);
374 419
 
375 420
         char* command = serial_line_buffer[i];
376 421
 
@@ -409,16 +454,17 @@ void GCodeQueue::get_serial_commands() {
409 454
             return gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM), i);
410 455
         #endif
411 456
 
412
-        // Movement commands alert when stopped
457
+        //
458
+        // Movement commands give an alert when the machine is stopped
459
+        //
460
+
413 461
         if (IsStopped()) {
414 462
           char* gpos = strchr(command, 'G');
415 463
           if (gpos) {
416 464
             switch (strtol(gpos + 1, nullptr, 10)) {
417
-              case 0:
418
-              case 1:
465
+              case 0: case 1:
419 466
               #if ENABLED(ARC_SUPPORT)
420
-                case 2:
421
-                case 3:
467
+                case 2: case 3:
422 468
               #endif
423 469
               #if ENABLED(BEZIER_CURVE_SUPPORT)
424 470
                 case 5:
@@ -453,31 +499,9 @@ void GCodeQueue::get_serial_commands() {
453 499
           #endif
454 500
         );
455 501
       }
456
-      else if (serial_count[i] >= MAX_CMD_SIZE - 1) {
457
-        // Keep fetching, but ignore normal characters beyond the max length
458
-        // The command will be injected when EOL is reached
459
-      }
460
-      else if (serial_char == '\\') {  // Handle escapes
461
-        // if we have one more character, copy it over
462
-        if ((c = read_serial(i)) >= 0 && !serial_comment_mode[i]
463
-          #if ENABLED(PAREN_COMMENTS)
464
-            && !serial_comment_paren_mode[i]
465
-          #endif
466
-        )
467
-          serial_line_buffer[i][serial_count[i]++] = (char)c;
468
-      }
469
-      else { // it's not a newline, carriage return or escape char
470
-        if (serial_char == ';') serial_comment_mode[i] = true;
471
-        #if ENABLED(PAREN_COMMENTS)
472
-          else if (serial_char == '(') serial_comment_paren_mode[i] = true;
473
-          else if (serial_char == ')') serial_comment_paren_mode[i] = false;
474
-        #endif
475
-        else if (!serial_comment_mode[i]
476
-          #if ENABLED(PAREN_COMMENTS)
477
-            && ! serial_comment_paren_mode[i]
478
-          #endif
479
-        ) serial_line_buffer[i][serial_count[i]++] = serial_char;
480
-      }
502
+      else
503
+        process_stream_char(serial_char, serial_input_state[i], serial_line_buffer[i], serial_count[i]);
504
+
481 505
     } // for NUM_SERIAL
482 506
   } // queue has space, serial has data
483 507
 }
@@ -490,21 +514,17 @@ void GCodeQueue::get_serial_commands() {
490 514
    * can also interrupt buffering.
491 515
    */
492 516
   inline void GCodeQueue::get_sdcard_commands() {
493
-    static bool sd_comment_mode = false
494
-                #if ENABLED(PAREN_COMMENTS)
495
-                  , sd_comment_paren_mode = false
496
-                #endif
497
-              ;
517
+    static uint8_t sd_input_state = PS_NORMAL;
498 518
 
499 519
     if (!IS_SD_PRINTING()) return;
500 520
 
501
-    uint16_t sd_count = 0;
521
+    int sd_count = 0;
502 522
     bool card_eof = card.eof();
503 523
     while (length < BUFSIZE && !card_eof) {
504 524
       const int16_t n = card.get();
505
-      char sd_char = (char)n;
506 525
       card_eof = card.eof();
507
-      if (card_eof || n == -1 || sd_char == '\n' || sd_char == '\r') {
526
+      const char sd_char = (char)n;
527
+      if (card_eof || n < 0 || sd_char == '\n' || sd_char == '\r') {
508 528
         if (card_eof) {
509 529
 
510 530
           card.printingHasFinished();
@@ -527,19 +547,10 @@ void GCodeQueue::get_serial_commands() {
527 547
             #endif // PRINTER_EVENT_LEDS
528 548
           }
529 549
         }
530
-        else if (n == -1)
550
+        else if (n < 0)
531 551
           SERIAL_ERROR_MSG(MSG_SD_ERR_READ);
532 552
 
533
-        sd_comment_mode = false; // for new command
534
-        #if ENABLED(PAREN_COMMENTS)
535
-          sd_comment_paren_mode = false;
536
-        #endif
537
-
538
-        // Skip empty lines and comments
539
-        if (!sd_count) { thermalManager.manage_heater(); continue; }
540
-
541
-        command_buffer[index_w][sd_count] = '\0'; // terminate string
542
-        sd_count = 0; // clear sd line buffer
553
+        process_line_done(sd_input_state, command_buffer[index_w], sd_count);
543 554
 
544 555
         _commit_command(false);
545 556
 
@@ -547,24 +558,9 @@ void GCodeQueue::get_serial_commands() {
547 558
           recovery.cmd_sdpos = card.getIndex(); // Prime for the next _commit_command
548 559
         #endif
549 560
       }
550
-      else if (sd_count >= MAX_CMD_SIZE - 1) {
551
-        /**
552
-         * Keep fetching, but ignore normal characters beyond the max length
553
-         * The command will be injected when EOL is reached
554
-         */
555
-      }
556
-      else {
557
-        if (sd_char == ';') sd_comment_mode = true;
558
-        #if ENABLED(PAREN_COMMENTS)
559
-          else if (sd_char == '(') sd_comment_paren_mode = true;
560
-          else if (sd_char == ')') sd_comment_paren_mode = false;
561
-        #endif
562
-        else if (!sd_comment_mode
563
-          #if ENABLED(PAREN_COMMENTS)
564
-            && ! sd_comment_paren_mode
565
-          #endif
566
-        ) command_buffer[index_w][sd_count++] = sd_char;
567
-      }
561
+      else
562
+        process_stream_char(sd_char, sd_input_state, command_buffer[index_w], sd_count);
563
+
568 564
     }
569 565
   }
570 566
 

+ 1
- 1
buildroot/share/tests/megaatmega1280-tests View File

@@ -19,7 +19,7 @@ restore_configs
19 19
 opt_set LCD_LANGUAGE an
20 20
 opt_enable SPINDLE_FEATURE ULTIMAKERCONTROLLER LCD_BED_LEVELING \
21 21
            MESH_BED_LEVELING ENABLE_LEVELING_FADE_HEIGHT MESH_G28_REST_ORIGIN \
22
-           G26_MESH_VALIDATION MESH_EDIT_MENU
22
+           G26_MESH_VALIDATION MESH_EDIT_MENU GCODE_QUOTED_STRINGS
23 23
 exec_test $1 $2 "Spindle, MESH_BED_LEVELING, and LCD"
24 24
 
25 25
 

Loading…
Cancel
Save