Parser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. Token token { String::copy(m_token), m_position, m_token.size(), type };
  35. if (state() == InRedirectionPath) {
  36. m_redirections.last().path = move(token);
  37. m_token.clear_with_capacity();
  38. return;
  39. }
  40. m_tokens.append(token);
  41. m_token.clear_with_capacity();
  42. };
  43. void Parser::commit_subcommand()
  44. {
  45. if (m_tokens.is_empty())
  46. return;
  47. m_subcommands.append({ move(m_tokens), move(m_redirections), {} });
  48. }
  49. void Parser::commit_command(Attributes attributes)
  50. {
  51. if (m_subcommands.is_empty())
  52. return;
  53. m_commands.append({ move(m_subcommands), attributes });
  54. }
  55. void Parser::do_pipe()
  56. {
  57. m_redirections.append({ Redirection::Pipe, STDOUT_FILENO });
  58. commit_subcommand();
  59. }
  60. void Parser::begin_redirect_read(int fd)
  61. {
  62. m_redirections.append({ Redirection::FileRead, fd });
  63. }
  64. void Parser::begin_redirect_write(int fd)
  65. {
  66. m_redirections.append({ Redirection::FileWrite, fd });
  67. }
  68. bool Parser::in_state(State state) const
  69. {
  70. for (auto s : m_state_stack)
  71. if (s == state)
  72. return true;
  73. return false;
  74. }
  75. Vector<Command> Parser::parse()
  76. {
  77. for (size_t i = 0; i < m_input.length(); ++i, m_position = i) {
  78. char ch = m_input.characters()[i];
  79. switch (state()) {
  80. case State::Free:
  81. if (ch == '#') {
  82. commit_token(Token::Bare);
  83. while (i < m_input.length()) {
  84. ch = m_input.characters()[++i];
  85. ++m_position;
  86. if (ch == '\n')
  87. break;
  88. m_token.append(ch);
  89. }
  90. commit_token(Token::Comment);
  91. break;
  92. }
  93. if (ch == ' ') {
  94. commit_token(Token::Bare);
  95. break;
  96. }
  97. if (ch == ';') {
  98. commit_token(Token::Special);
  99. commit_subcommand();
  100. commit_command();
  101. break;
  102. }
  103. if (ch == '&') {
  104. commit_token(Token::Special);
  105. if (i + 1 >= m_input.length()) {
  106. in_background:;
  107. // Nothing interesting past this token, commit with InBackground
  108. commit_subcommand();
  109. commit_command(Attributes::InBackground);
  110. break;
  111. }
  112. ch = m_input.characters()[++i];
  113. ++m_position;
  114. if (ch == '&') {
  115. // This is '&&', commit with ShortCircuit
  116. commit_subcommand();
  117. commit_command(Attributes::ShortCircuitOnFailure);
  118. break;
  119. }
  120. --i;
  121. --m_position;
  122. goto in_background;
  123. }
  124. if (ch == '|') {
  125. commit_token(Token::Special);
  126. if (m_tokens.is_empty()) {
  127. fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
  128. return {};
  129. }
  130. do_pipe();
  131. break;
  132. }
  133. if (ch == '>') {
  134. commit_token(Token::Special);
  135. begin_redirect_write(STDOUT_FILENO);
  136. // Search for another > for append.
  137. push_state(State::InWriteAppendOrRedirectionPath);
  138. break;
  139. }
  140. if (ch == '<') {
  141. commit_token(Token::Special);
  142. begin_redirect_read(STDIN_FILENO);
  143. push_state(State::InRedirectionPath);
  144. break;
  145. }
  146. if (ch == '\\') {
  147. if (i == m_input.length() - 1) {
  148. fprintf(stderr, "Syntax error: Nothing to escape (\\)\n");
  149. return {};
  150. }
  151. char next_ch = m_input.characters()[i + 1];
  152. m_token.append(next_ch);
  153. ++i;
  154. break;
  155. }
  156. if (ch == '\'') {
  157. push_state(State::InSingleQuotes);
  158. break;
  159. }
  160. if (ch == '\"') {
  161. push_state(State::InDoubleQuotes);
  162. break;
  163. }
  164. // redirection from zsh-style multi-digit fd, such as {10}>file
  165. if (ch == '{') {
  166. bool is_multi_fd_redirection = false;
  167. size_t redir_end = i + 1;
  168. while (redir_end < m_input.length()) {
  169. char lookahead_ch = m_input.characters()[redir_end];
  170. if (isdigit(lookahead_ch)) {
  171. ++redir_end;
  172. continue;
  173. }
  174. if (lookahead_ch == '}' && redir_end + 1 != m_input.length()) {
  175. // Disallow {}> and {}<
  176. if (redir_end == i + 1)
  177. break;
  178. ++redir_end;
  179. if (m_input.characters()[redir_end] == '>' || m_input.characters()[redir_end] == '<')
  180. is_multi_fd_redirection = true;
  181. break;
  182. }
  183. break;
  184. }
  185. if (is_multi_fd_redirection) {
  186. commit_token(Token::Special);
  187. int fd = atoi(&m_input.characters()[i + 1]);
  188. if (m_input.characters()[redir_end] == '>') {
  189. begin_redirect_write(fd);
  190. // Search for another > for append.
  191. push_state(State::InWriteAppendOrRedirectionPath);
  192. }
  193. if (m_input.characters()[redir_end] == '<') {
  194. begin_redirect_read(fd);
  195. push_state(State::InRedirectionPath);
  196. }
  197. i = redir_end;
  198. break;
  199. }
  200. }
  201. if (isdigit(ch)) {
  202. if (i != m_input.length() - 1) {
  203. char next_ch = m_input.characters()[i + 1];
  204. if (next_ch == '>') {
  205. commit_token(Token::Special);
  206. begin_redirect_write(ch - '0');
  207. ++i;
  208. // Search for another > for append.
  209. push_state(State::InWriteAppendOrRedirectionPath);
  210. break;
  211. }
  212. if (next_ch == '<') {
  213. commit_token(Token::Special);
  214. begin_redirect_read(ch - '0');
  215. ++i;
  216. push_state(State::InRedirectionPath);
  217. break;
  218. }
  219. }
  220. }
  221. m_token.append(ch);
  222. break;
  223. case State::InWriteAppendOrRedirectionPath:
  224. if (ch == '>') {
  225. commit_token(Token::Special);
  226. pop_state();
  227. push_state(State::InRedirectionPath);
  228. ASSERT(m_redirections.size());
  229. m_redirections[m_redirections.size() - 1].type = Redirection::FileWriteAppend;
  230. break;
  231. }
  232. // Not another > means that it's probably a path.
  233. pop_state();
  234. push_state(InRedirectionPath);
  235. [[fallthrough]];
  236. case State::InRedirectionPath:
  237. if (ch == '<') {
  238. commit_token(Token::Special);
  239. begin_redirect_read(STDIN_FILENO);
  240. pop_state();
  241. push_state(State::InRedirectionPath);
  242. break;
  243. }
  244. if (ch == '>') {
  245. commit_token(Token::Special);
  246. begin_redirect_read(STDOUT_FILENO);
  247. pop_state();
  248. push_state(State::InRedirectionPath);
  249. break;
  250. }
  251. if (ch == '|') {
  252. commit_token(Token::Special);
  253. if (m_tokens.is_empty()) {
  254. fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
  255. return {};
  256. }
  257. do_pipe();
  258. pop_state();
  259. break;
  260. }
  261. if (ch == '"') {
  262. push_state(State::InDoubleQuotes);
  263. break;
  264. }
  265. if (ch == '\'') {
  266. push_state(State::InSingleQuotes);
  267. break;
  268. }
  269. if (ch == ' ')
  270. break;
  271. m_token.append(ch);
  272. break;
  273. case State::InSingleQuotes:
  274. if (ch == '\'') {
  275. if (!in_state(State::InRedirectionPath))
  276. commit_token(Token::SingleQuoted, AllowEmptyToken::Yes);
  277. pop_state();
  278. break;
  279. }
  280. m_token.append(ch);
  281. break;
  282. case State::InDoubleQuotes:
  283. if (ch == '\"') {
  284. if (!in_state(State::InRedirectionPath))
  285. commit_token(Token::DoubleQuoted, AllowEmptyToken::Yes);
  286. pop_state();
  287. break;
  288. }
  289. if (ch == '\\') {
  290. if (i == m_input.length() - 1) {
  291. fprintf(stderr, "Syntax error: Nothing to escape (\\)\n");
  292. return {};
  293. }
  294. char next_ch = m_input.characters()[i + 1];
  295. if (next_ch == '$' || next_ch == '`'
  296. || next_ch == '"' || next_ch == '\\') {
  297. m_token.append(next_ch);
  298. ++i;
  299. continue;
  300. }
  301. m_token.append('\\');
  302. break;
  303. }
  304. m_token.append(ch);
  305. break;
  306. };
  307. }
  308. while (m_state_stack.size() > 1) {
  309. if (state() == State::InDoubleQuotes) {
  310. pop_state();
  311. commit_token(Token::UnterminatedDoubleQuoted, AllowEmptyToken::Yes);
  312. } else if (state() == State::InSingleQuotes) {
  313. pop_state();
  314. commit_token(Token::UnterminatedSingleQuoted, AllowEmptyToken::Yes);
  315. } else {
  316. commit_token(Token::Bare, AllowEmptyToken::No);
  317. pop_state();
  318. }
  319. }
  320. ASSERT(state() == State::Free);
  321. commit_token(Token::Bare);
  322. commit_subcommand();
  323. commit_command();
  324. if (!m_subcommands.is_empty()) {
  325. for (auto& redirection : m_subcommands.last().redirections) {
  326. if (redirection.type == Redirection::Pipe) {
  327. fprintf(stderr, "Syntax error: Nothing after last pipe (|)\n");
  328. return {};
  329. }
  330. }
  331. }
  332. return move(m_commands);
  333. }