about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/parser.c57
-rw-r--r--tests/test_parser.c107
2 files changed, 152 insertions, 12 deletions
diff --git a/src/parser.c b/src/parser.c
index c0ee70a7..af01ddcc 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -60,6 +60,7 @@ parse_args(const char * const inp, int min, int max)
 
     int inp_size = strlen(copy);
     gboolean in_token = FALSE;
+    gboolean in_quotes = FALSE;
     char *token_start = &copy[0];
     int token_size = 0;
     GSList *tokens = NULL;
@@ -72,17 +73,33 @@ parse_args(const char * const inp, int min, int max)
                 continue;
             } else {
                 in_token = TRUE;
+                if (copy[i] == '"') {
+                    in_quotes = TRUE;
+                    i++;
+                }
                 token_start = &copy[i];
                 token_size++;
             }
         } else {
-            if (copy[i] == ' ' || copy[i] == '\0') {
-                tokens = g_slist_append(tokens, g_strndup(token_start,
-                    token_size));
-                token_size = 0;
-                in_token = FALSE;
+            if (in_quotes) {
+                if ((copy[i] == '\0') || (copy[i] == '"')) {
+                    tokens = g_slist_append(tokens, g_strndup(token_start,
+                        token_size));
+                    token_size = 0;
+                    in_token = FALSE;
+                    in_quotes = FALSE;
+                } else {
+                    token_size++;
+                }
             } else {
-                token_size++;
+                if (copy[i] == ' ' || copy[i] == '\0') {
+                    tokens = g_slist_append(tokens, g_strndup(token_start,
+                        token_size));
+                    token_size = 0;
+                    in_token = FALSE;
+                } else {
+                    token_size++;
+                }
             }
         }
     }
@@ -163,6 +180,7 @@ parse_args_with_freetext(const char * const inp, int min, int max)
     int inp_size = strlen(copy);
     gboolean in_token = FALSE;
     gboolean in_freetext = FALSE;
+    gboolean in_quotes = FALSE;
     char *token_start = &copy[0];
     int token_size = 0;
     int num_tokens = 0;
@@ -179,18 +197,33 @@ parse_args_with_freetext(const char * const inp, int min, int max)
                 num_tokens++;
                 if (num_tokens == max + 1) {
                     in_freetext = TRUE;
+                } else if (copy[i] == '"') {
+                    in_quotes = TRUE;
+                    i++;
                 }
                 token_start = &copy[i];
                 token_size++;
             }
         } else {
-            if ((!in_freetext && copy[i] == ' ') || copy[i] == '\0') {
-                tokens = g_slist_append(tokens, g_strndup(token_start,
-                    token_size));
-                token_size = 0;
-                in_token = FALSE;
+            if (in_quotes) {
+                if ((copy[i] == '\0') || (copy[i] == '"')) {
+                    tokens = g_slist_append(tokens, g_strndup(token_start,
+                        token_size));
+                    token_size = 0;
+                    in_token = FALSE;
+                    in_quotes = FALSE;
+                } else {
+                    token_size++;
+                }
             } else {
-                token_size++;
+                if ((!in_freetext && copy[i] == ' ') || copy[i] == '\0') {
+                    tokens = g_slist_append(tokens, g_strndup(token_start,
+                        token_size));
+                    token_size = 0;
+                    in_token = FALSE;
+                } else {
+                    token_size++;
+                }
             }
         }
     }
diff --git a/tests/test_parser.c b/tests/test_parser.c
index a829f37e..88e03c71 100644
--- a/tests/test_parser.c
+++ b/tests/test_parser.c
@@ -181,6 +181,105 @@ parse_cmd_min_zero_with_freetext(void)
 }
 
 void
+parse_cmd_with_quoted(void)
+{
+    char *inp = "/cmd \"arg1\" arg2";
+    gchar **result = parse_args(inp, 2, 2);
+
+    assert_int_equals(2, g_strv_length(result));
+    assert_string_equals("arg1", result[0]);
+    assert_string_equals("arg2", result[1]);
+    g_strfreev(result);
+}
+
+void
+parse_cmd_with_quoted_and_space(void)
+{
+    char *inp = "/cmd \"the arg1\" arg2";
+    gchar **result = parse_args(inp, 2, 2);
+
+    assert_int_equals(2, g_strv_length(result));
+    assert_string_equals("the arg1", result[0]);
+    assert_string_equals("arg2", result[1]);
+    g_strfreev(result);
+}
+
+void
+parse_cmd_with_quoted_and_many_spaces(void)
+{
+    char *inp = "/cmd \"the arg1 is here\" arg2";
+    gchar **result = parse_args(inp, 2, 2);
+
+    assert_int_equals(2, g_strv_length(result));
+    assert_string_equals("the arg1 is here", result[0]);
+    assert_string_equals("arg2", result[1]);
+    g_strfreev(result);
+}
+
+void
+parse_cmd_with_many_quoted_and_many_spaces(void)
+{
+    char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\"";
+    gchar **result = parse_args(inp, 2, 2);
+
+    assert_int_equals(2, g_strv_length(result));
+    assert_string_equals("the arg1 is here", result[0]);
+    assert_string_equals("and arg2 is right here", result[1]);
+    g_strfreev(result);
+}
+
+void
+parse_cmd_freetext_with_quoted(void)
+{
+    char *inp = "/cmd \"arg1\" arg2 hello there whats up";
+    gchar **result = parse_args_with_freetext(inp, 3, 3);
+
+    assert_int_equals(3, g_strv_length(result));
+    assert_string_equals("arg1", result[0]);
+    assert_string_equals("arg2", result[1]);
+    assert_string_equals("hello there whats up", result[2]);
+    g_strfreev(result);
+}
+
+void
+parse_cmd_freetext_with_quoted_and_space(void)
+{
+    char *inp = "/cmd \"the arg1\" arg2 another bit of freetext";
+    gchar **result = parse_args_with_freetext(inp, 3, 3);
+
+    assert_int_equals(3, g_strv_length(result));
+    assert_string_equals("the arg1", result[0]);
+    assert_string_equals("arg2", result[1]);
+    assert_string_equals("another bit of freetext", result[2]);
+    g_strfreev(result);
+}
+
+void
+parse_cmd_freetext_with_quoted_and_many_spaces(void)
+{
+    char *inp = "/cmd \"the arg1 is here\" arg2 some more freetext";
+    gchar **result = parse_args_with_freetext(inp, 3, 3);
+
+    assert_int_equals(3, g_strv_length(result));
+    assert_string_equals("the arg1 is here", result[0]);
+    assert_string_equals("arg2", result[1]);
+    assert_string_equals("some more freetext", result[2]);
+    g_strfreev(result);
+}
+
+void
+parse_cmd_freetext_with_many_quoted_and_many_spaces(void)
+{
+    char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\" and heres the free text";
+    gchar **result = parse_args_with_freetext(inp, 3, 3);
+
+    assert_int_equals(3, g_strv_length(result));
+    assert_string_equals("the arg1 is here", result[0]);
+    assert_string_equals("and arg2 is right here", result[1]);
+    assert_string_equals("and heres the free text", result[2]);
+    g_strfreev(result);
+}
+void
 register_parser_tests(void)
 {
     TEST_MODULE("parser tests");
@@ -200,4 +299,12 @@ register_parser_tests(void)
     TEST(parse_cmd_with_too_many_returns_null);
     TEST(parse_cmd_min_zero);
     TEST(parse_cmd_min_zero_with_freetext);
+    TEST(parse_cmd_with_quoted);
+    TEST(parse_cmd_with_quoted_and_space);
+    TEST(parse_cmd_with_quoted_and_many_spaces);
+    TEST(parse_cmd_with_many_quoted_and_many_spaces);
+    TEST(parse_cmd_freetext_with_quoted);
+    TEST(parse_cmd_freetext_with_quoted_and_space);
+    TEST(parse_cmd_freetext_with_quoted_and_many_spaces);
+    TEST(parse_cmd_freetext_with_many_quoted_and_many_spaces);
 }
'#n459'>459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598