bc: fix interactive handling of comments in strings and quotes in comments
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 25 Dec 2018 22:45:57 +0000 (23:45 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 25 Dec 2018 22:45:57 +0000 (23:45 +0100)
function                                             old     new   delta
zbc_lex_next                                        1965    1979     +14

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/bc.c
testsuite/bc.tests

index 809b4bfc480fc03c483ead1451956482b4427e64..9d04ddea3d6b0402e36822987f9c1a7e3c26a2a5 100644 (file)
@@ -2910,25 +2910,29 @@ static bool bc_lex_more_input(void)
                string = G.input_buffer.v + prevlen;
                while (*string) {
                        char c = *string;
-                       if (string == G.input_buffer.v || string[-1] != '\\') {
-                               if (IS_BC)
-                                       str ^= (c == '"');
-                               else {
-                                       if (c == ']')
-                                               str -= 1;
-                                       else if (c == '[')
-                                               str += 1;
+                       if (!comment) {
+                               if (string == G.input_buffer.v || string[-1] != '\\') {
+                                       if (IS_BC)
+                                               str ^= (c == '"');
+                                       else {
+                                               if (c == ']')
+                                                       str -= 1;
+                                               else if (c == '[')
+                                                       str += 1;
+                                       }
                                }
                        }
                        string++;
-                       if (c == '/' && *string == '*') {
-                               comment = true;
-                               string++;
-                               continue;
-                       }
-                       if (c == '*' && *string == '/') {
-                               comment = false;
-                               string++;
+                       if (!str) {
+                               if (c == '/' && *string == '*') {
+                                       comment = true;
+                                       string++;
+                                       continue;
+                               }
+                               if (c == '*' && *string == '/') {
+                                       comment = false;
+                                       string++;
+                               }
                        }
                }
                if (str != 0 || comment) {
index 42fe83013a471ad848244e7495dd3565f9cb919b..3fbb499963d00736081f3e808526fd303b609d06 100755 (executable)
@@ -6,16 +6,28 @@
 
 # testing "test name" "command" "expected result" "file input" "stdin"
 
-testing "bc comment 1" \
+testing "bc comment" \
        "bc" \
        "3\n" \
        "" "1 /* comment */ + 2"
 
-testing "bc comment 2: /*/ is not a closed comment" \
+testing "bc /*/ is not a closed comment" \
        "bc" \
        "4\n" \
        "" "1 /*/ + 2 */ + 3"
 
+# this needs interactive testing
+testing "bc comment with \"" \
+       "bc" \
+       "3\n" \
+       "" "1 /* \" */ + 2"
+
+# this needs interactive testing
+testing "bc \"string/*\" is not a comment" \
+       "bc" \
+       "string/*9\n" \
+       "" "\"string/*\";9"
+
 testing "bc comment 3: unterminated #comment" \
        "bc" \
        "" \