Parser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Parser.h"
  27. #include <ctype.h>
  28. #include <stdio.h>
  29. #include <unistd.h>
  30. void Parser::commit_token(Token::Type type, AllowEmptyToken allow_empty)
  31. {
  32. if (allow_empty == AllowEmptyToken::No && m_token.is_empty())
  33. return;
  34. if (state() == InRedirectionPath) {
  35. m_redirections.last().path = String::copy(m_token);
  36. m_token.clear_with_capacity();
  37. return;
  38. }
  39. m_tokens.append({ String::copy(m_token), m_position, m_token.size(), type });
  40. m_token.clear_with_capacity();
  41. };
  42. void Parser::commit_subcommand()
  43. {
  44. if (m_tokens.is_empty())
  45. return;
  46. m_subcommands.append({ move(m_tokens), move(m_redirections), {} });
  47. }
  48. void Parser::commit_command()
  49. {
  50. if (m_subcommands.is_empty())
  51. return;
  52. m_commands.append({ move(m_subcommands) });
  53. }
  54. void Parser::do_pipe()
  55. {
  56. m_redirections.append({ Redirection::Pipe, STDOUT_FILENO });
  57. commit_subcommand();
  58. }
  59. void Parser::begin_redirect_read(int fd)
  60. {
  61. m_redirections.append({ Redirection::FileRead, fd });
  62. }
  63. void Parser::begin_redirect_write(int fd)
  64. {
  65. m_redirections.append({ Redirection::FileWrite, fd });
  66. }
  67. bool Parser::in_state(State state) const
  68. {
  69. for (auto s : m_state_stack)
  70. if (s == state)
  71. return true;
  72. return false;
  73. }
  74. Vector<Command> Parser::parse()
  75. {
  76. for (size_t i = 0; i < m_input.length(); ++i, m_position = i) {
  77. char ch = m_input.characters()[i];
  78. switch (state()) {
  79. case State::Free:
  80. if (ch == '#') {
  81. commit_token(Token::Bare);
  82. while (i < m_input.length()) {
  83. ch = m_input.characters()[++i];
  84. ++m_position;
  85. if (ch == '\n')
  86. break;
  87. m_token.append(ch);
  88. }
  89. commit_token(Token::Comment);
  90. break;
  91. }
  92. if (ch == ' ') {
  93. commit_token(Token::Bare);
  94. break;
  95. }
  96. if (ch == ';') {
  97. commit_token(Token::Special);
  98. commit_subcommand();
  99. commit_command();
  100. break;
  101. }
  102. if (ch == '|') {
  103. commit_token(Token::Special);
  104. if (m_tokens.is_empty()) {
  105. fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
  106. return {};
  107. }
  108. do_pipe();
  109. break;
  110. }
  111. if (ch == '>') {
  112. commit_token(Token::Special);
  113. begin_redirect_write(STDOUT_FILENO);
  114. // Search for another > for append.
  115. push_state(State::InWriteAppendOrRedirectionPath);
  116. break;
  117. }
  118. if (ch == '<') {
  119. commit_token(Token::Special);
  120. begin_redirect_read(STDIN_FILENO);
  121. push_state(State::InRedirectionPath);
  122. break;
  123. }
  124. if (ch == '\\') {
  125. if (i == m_input.length() - 1) {
  126. fprintf(stderr, "Syntax error: Nothing to escape (\\)\n");
  127. return {};
  128. }
  129. char next_ch = m_input.characters()[i + 1];
  130. m_token.append(next_ch);
  131. ++i;
  132. break;
  133. }
  134. if (ch == '\'') {
  135. push_state(State::InSingleQuotes);
  136. break;
  137. }
  138. if (ch == '\"') {
  139. push_state(State::InDoubleQuotes);
  140. break;
  141. }
  142. // redirection from zsh-style multi-digit fd, such as {10}>file
  143. if (ch == '{') {
  144. bool is_multi_fd_redirection = false;
  145. size_t redir_end = i + 1;
  146. while (redir_end < m_input.length()) {
  147. char lookahead_ch = m_input.characters()[redir_end];
  148. if (isdigit(lookahead_ch)) {
  149. ++redir_end;
  150. continue;
  151. }
  152. if (lookahead_ch == '}' && redir_end + 1 != m_input.length()) {
  153. // Disallow {}> and {}<
  154. if (redir_end == i + 1)
  155. break;
  156. ++redir_end;
  157. if (m_input.characters()[redir_end] == '>' || m_input.characters()[redir_end] == '<')
  158. is_multi_fd_redirection = true;
  159. break;
  160. }
  161. break;
  162. }
  163. if (is_multi_fd_redirection) {
  164. commit_token(Token::Special);
  165. int fd = atoi(&m_input.characters()[i + 1]);
  166. if (m_input.characters()[redir_end] == '>') {
  167. begin_redirect_write(fd);
  168. // Search for another > for append.
  169. push_state(State::InWriteAppendOrRedirectionPath);
  170. }
  171. if (m_input.characters()[redir_end] == '<') {
  172. begin_redirect_read(fd);
  173. push_state(State::InRedirectionPath);
  174. }
  175. i = redir_end;
  176. break;
  177. }
  178. }
  179. if (isdigit(ch)) {
  180. if (i != m_input.length() - 1) {
  181. char next_ch = m_input.characters()[i + 1];
  182. if (next_ch == '>') {
  183. commit_token(Token::Special);
  184. begin_redirect_write(ch - '0');
  185. ++i;
  186. // Search for another > for append.
  187. push_state(State::InWriteAppendOrRedirectionPath);
  188. break;
  189. }
  190. if (next_ch == '<') {
  191. commit_token(Token::Special);
  192. begin_redirect_read(ch - '0');
  193. ++i;
  194. push_state(State::InRedirectionPath);
  195. break;
  196. }
  197. }
  198. }
  199. m_token.append(ch);
  200. break;
  201. case State::InWriteAppendOrRedirectionPath:
  202. if (ch == '>') {
  203. commit_token(Token::Special);
  204. pop_state();
  205. push_state(State::InRedirectionPath);
  206. ASSERT(m_redirections.size());
  207. m_redirections[m_redirections.size() - 1].type = Redirection::FileWriteAppend;
  208. break;
  209. }
  210. // Not another > means that it's probably a path.
  211. pop_state();
  212. push_state(InRedirectionPath);
  213. [[fallthrough]];
  214. case State::InRedirectionPath:
  215. if (ch == '<') {
  216. commit_token(Token::Special);
  217. begin_redirect_read(STDIN_FILENO);
  218. pop_state();
  219. push_state(State::InRedirectionPath);
  220. break;
  221. }
  222. if (ch == '>') {
  223. commit_token(Token::Special);
  224. begin_redirect_read(STDOUT_FILENO);
  225. pop_state();
  226. push_state(State::InRedirectionPath);
  227. break;
  228. }
  229. if (ch == '|') {
  230. commit_token(Token::Special);
  231. if (m_tokens.is_empty()) {
  232. fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
  233. return {};
  234. }
  235. do_pipe();
  236. pop_state();
  237. break;
  238. }
  239. if (ch == '"') {
  240. push_state(State::InDoubleQuotes);
  241. break;
  242. }
  243. if (ch == '\'') {
  244. push_state(State::InSingleQuotes);
  245. break;
  246. }
  247. if (ch == ' ')
  248. break;
  249. m_token.append(ch);
  250. break;
  251. case State::InSingleQuotes:
  252. if (ch == '\'') {
  253. if (!in_state(State::InRedirectionPath))
  254. commit_token(Token::SingleQuoted, AllowEmptyToken::Yes);
  255. pop_state();
  256. break;
  257. }
  258. m_token.append(ch);
  259. break;
  260. case State::InDoubleQuotes:
  261. if (ch == '\"') {
  262. if (!in_state(State::InRedirectionPath))
  263. commit_token(Token::DoubleQuoted, AllowEmptyToken::Yes);
  264. pop_state();
  265. break;
  266. }
  267. if (ch == '\\') {
  268. if (i == m_input.length() - 1) {
  269. fprintf(stderr, "Syntax error: Nothing to escape (\\)\n");
  270. return {};
  271. }
  272. char next_ch = m_input.characters()[i + 1];
  273. if (next_ch == '$' || next_ch == '`'
  274. || next_ch == '"' || next_ch == '\\') {
  275. m_token.append(next_ch);
  276. ++i;
  277. continue;
  278. }
  279. m_token.append('\\');
  280. break;
  281. }
  282. m_token.append(ch);
  283. break;
  284. };
  285. }
  286. while (m_state_stack.size() > 1) {
  287. if (state() == State::InDoubleQuotes) {
  288. commit_token(Token::UnterminatedDoubleQuoted, AllowEmptyToken::Yes);
  289. } else if (state() == State::InSingleQuotes) {
  290. commit_token(Token::UnterminatedSingleQuoted, AllowEmptyToken::Yes);
  291. } else {
  292. commit_token(Token::Bare, AllowEmptyToken::No);
  293. }
  294. pop_state();
  295. }
  296. ASSERT(state() == State::Free);
  297. commit_token(Token::Bare);
  298. commit_subcommand();
  299. commit_command();
  300. if (!m_subcommands.is_empty()) {
  301. for (auto& redirection : m_subcommands.last().redirections) {
  302. if (redirection.type == Redirection::Pipe) {
  303. fprintf(stderr, "Syntax error: Nothing after last pipe (|)\n");
  304. return {};
  305. }
  306. }
  307. }
  308. return move(m_commands);
  309. }