Browse Source

Shell: Add POSIX-compliant character escaping

POSIX.1-2017, Shells & Utilities, section 2.2
Aaron Malpas 5 năm trước cách đây
mục cha
commit
f44e7dc5d0
1 tập tin đã thay đổi với 25 bổ sung0 xóa
  1. 25 0
      Shell/Parser.cpp

+ 25 - 0
Shell/Parser.cpp

@@ -84,6 +84,16 @@ Vector<Command> Parser::parse()
                 m_state = State::InRedirectionPath;
                 m_state = State::InRedirectionPath;
                 break;
                 break;
             }
             }
+            if (ch == '\\') {
+                if (i == m_input.length() - 1) {
+                    fprintf(stderr, "Syntax error: Nothing to escape (\\)\n");
+                    return {};
+                }
+                char next_ch = m_input.characters()[i + 1];
+                m_token.append(next_ch);
+                ++i;
+                break;
+            }
             if (ch == '\'') {
             if (ch == '\'') {
                 m_state = State::InSingleQuotes;
                 m_state = State::InSingleQuotes;
                 break;
                 break;
@@ -147,6 +157,21 @@ Vector<Command> Parser::parse()
                 m_state = State::Free;
                 m_state = State::Free;
                 break;
                 break;
             }
             }
+            if (ch == '\\') {
+                if (i == m_input.length() - 1) {
+                    fprintf(stderr, "Syntax error: Nothing to escape (\\)\n");
+                    return {};
+                }
+                char next_ch = m_input.characters()[i + 1];
+                if (next_ch == '$' || next_ch == '`'
+                    || next_ch == '"' || next_ch == '\\') {
+                    m_token.append(next_ch);
+                    ++i;
+                    continue;
+                }
+                m_token.append('\\');
+                break;
+            }
             m_token.append(ch);
             m_token.append(ch);
             break;
             break;
         };
         };