Parser.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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(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));
  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) {
  77. char ch = m_input.characters()[i];
  78. switch (state()) {
  79. case State::Free:
  80. if (ch == ' ') {
  81. commit_token();
  82. break;
  83. }
  84. if (ch == ';') {
  85. commit_token();
  86. commit_subcommand();
  87. commit_command();
  88. break;
  89. }
  90. if (ch == '|') {
  91. commit_token();
  92. if (m_tokens.is_empty()) {
  93. fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
  94. return {};
  95. }
  96. do_pipe();
  97. break;
  98. }
  99. if (ch == '>') {
  100. commit_token();
  101. begin_redirect_write(STDOUT_FILENO);
  102. // Search for another > for append.
  103. push_state(State::InWriteAppendOrRedirectionPath);
  104. break;
  105. }
  106. if (ch == '<') {
  107. commit_token();
  108. begin_redirect_read(STDIN_FILENO);
  109. push_state(State::InRedirectionPath);
  110. break;
  111. }
  112. if (ch == '\\') {
  113. if (i == m_input.length() - 1) {
  114. fprintf(stderr, "Syntax error: Nothing to escape (\\)\n");
  115. return {};
  116. }
  117. char next_ch = m_input.characters()[i + 1];
  118. m_token.append(next_ch);
  119. ++i;
  120. break;
  121. }
  122. if (ch == '\'') {
  123. push_state(State::InSingleQuotes);
  124. break;
  125. }
  126. if (ch == '\"') {
  127. push_state(State::InDoubleQuotes);
  128. break;
  129. }
  130. // redirection from zsh-style multi-digit fd, such as {10}>file
  131. if (ch == '{') {
  132. bool is_multi_fd_redirection = false;
  133. size_t redir_end = i + 1;
  134. while (redir_end < m_input.length()) {
  135. char lookahead_ch = m_input.characters()[redir_end];
  136. if (isdigit(lookahead_ch)) {
  137. ++redir_end;
  138. continue;
  139. }
  140. if (lookahead_ch == '}' && redir_end + 1 != m_input.length()) {
  141. // Disallow {}> and {}<
  142. if (redir_end == i + 1)
  143. break;
  144. ++redir_end;
  145. if (m_input.characters()[redir_end] == '>' || m_input.characters()[redir_end] == '<')
  146. is_multi_fd_redirection = true;
  147. break;
  148. }
  149. break;
  150. }
  151. if (is_multi_fd_redirection) {
  152. commit_token();
  153. int fd = atoi(&m_input.characters()[i + 1]);
  154. if (m_input.characters()[redir_end] == '>') {
  155. begin_redirect_write(fd);
  156. // Search for another > for append.
  157. push_state(State::InWriteAppendOrRedirectionPath);
  158. }
  159. if (m_input.characters()[redir_end] == '<') {
  160. begin_redirect_read(fd);
  161. push_state(State::InRedirectionPath);
  162. }
  163. i = redir_end;
  164. break;
  165. }
  166. }
  167. if (isdigit(ch)) {
  168. if (i != m_input.length() - 1) {
  169. char next_ch = m_input.characters()[i + 1];
  170. if (next_ch == '>') {
  171. commit_token();
  172. begin_redirect_write(ch - '0');
  173. ++i;
  174. // Search for another > for append.
  175. push_state(State::InWriteAppendOrRedirectionPath);
  176. break;
  177. }
  178. if (next_ch == '<') {
  179. commit_token();
  180. begin_redirect_read(ch - '0');
  181. ++i;
  182. push_state(State::InRedirectionPath);
  183. break;
  184. }
  185. }
  186. }
  187. m_token.append(ch);
  188. break;
  189. case State::InWriteAppendOrRedirectionPath:
  190. if (ch == '>') {
  191. commit_token();
  192. pop_state();
  193. push_state(State::InRedirectionPath);
  194. ASSERT(m_redirections.size());
  195. m_redirections[m_redirections.size() - 1].type = Redirection::FileWriteAppend;
  196. break;
  197. }
  198. // Not another > means that it's probably a path.
  199. pop_state();
  200. push_state(InRedirectionPath);
  201. [[fallthrough]];
  202. case State::InRedirectionPath:
  203. if (ch == '<') {
  204. commit_token();
  205. begin_redirect_read(STDIN_FILENO);
  206. pop_state();
  207. push_state(State::InRedirectionPath);
  208. break;
  209. }
  210. if (ch == '>') {
  211. commit_token();
  212. begin_redirect_read(STDOUT_FILENO);
  213. pop_state();
  214. push_state(State::InRedirectionPath);
  215. break;
  216. }
  217. if (ch == '|') {
  218. commit_token();
  219. if (m_tokens.is_empty()) {
  220. fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
  221. return {};
  222. }
  223. do_pipe();
  224. pop_state();
  225. break;
  226. }
  227. if (ch == '"') {
  228. push_state(State::InDoubleQuotes);
  229. break;
  230. }
  231. if (ch == '\'') {
  232. push_state(State::InSingleQuotes);
  233. break;
  234. }
  235. if (ch == ' ')
  236. break;
  237. m_token.append(ch);
  238. break;
  239. case State::InSingleQuotes:
  240. if (ch == '\'') {
  241. if (!in_state(State::InRedirectionPath))
  242. commit_token(AllowEmptyToken::Yes);
  243. pop_state();
  244. break;
  245. }
  246. m_token.append(ch);
  247. break;
  248. case State::InDoubleQuotes:
  249. if (ch == '\"') {
  250. if (!in_state(State::InRedirectionPath))
  251. commit_token(AllowEmptyToken::Yes);
  252. pop_state();
  253. break;
  254. }
  255. if (ch == '\\') {
  256. if (i == m_input.length() - 1) {
  257. fprintf(stderr, "Syntax error: Nothing to escape (\\)\n");
  258. return {};
  259. }
  260. char next_ch = m_input.characters()[i + 1];
  261. if (next_ch == '$' || next_ch == '`'
  262. || next_ch == '"' || next_ch == '\\') {
  263. m_token.append(next_ch);
  264. ++i;
  265. continue;
  266. }
  267. m_token.append('\\');
  268. break;
  269. }
  270. m_token.append(ch);
  271. break;
  272. };
  273. }
  274. while (m_state_stack.size() > 1) {
  275. auto allow_empty = AllowEmptyToken::No;
  276. if (state() == State::InDoubleQuotes || state() == State::InSingleQuotes)
  277. allow_empty = AllowEmptyToken::Yes;
  278. commit_token(allow_empty);
  279. pop_state();
  280. }
  281. ASSERT(state() == State::Free);
  282. commit_token();
  283. commit_subcommand();
  284. commit_command();
  285. if (!m_subcommands.is_empty()) {
  286. for (auto& redirection : m_subcommands.last().redirections) {
  287. if (redirection.type == Redirection::Pipe) {
  288. fprintf(stderr, "Syntax error: Nothing after last pipe (|)\n");
  289. return {};
  290. }
  291. }
  292. }
  293. return move(m_commands);
  294. }