AST.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  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 "AST.h"
  27. #include "Shell.h"
  28. #include <AK/String.h>
  29. #include <AK/StringBuilder.h>
  30. #include <AK/URL.h>
  31. #include <LibCore/File.h>
  32. //#define EXECUTE_DEBUG
  33. namespace AST {
  34. template<typename T, typename... Args>
  35. static inline RefPtr<T> create(Args... args)
  36. {
  37. return adopt(*new T(args...));
  38. }
  39. template<typename T>
  40. static inline RefPtr<T> create(std::initializer_list<RefPtr<Value>> arg)
  41. {
  42. return adopt(*new T(arg));
  43. }
  44. static inline void print_indented(const String& str, int indent)
  45. {
  46. for (auto i = 0; i < indent; ++i)
  47. dbgprintf(" ");
  48. dbgprintf("%s\n", str.characters());
  49. }
  50. static inline Vector<Command> join_commands(Vector<Command> left, Vector<Command> right)
  51. {
  52. Command command;
  53. auto last_in_left = left.take_last();
  54. auto first_in_right = right.take_first();
  55. command.argv.append(last_in_left.argv);
  56. command.argv.append(first_in_right.argv);
  57. command.redirections.append(last_in_left.redirections);
  58. command.redirections.append(first_in_right.redirections);
  59. command.should_wait = first_in_right.should_wait && last_in_left.should_wait;
  60. command.is_pipe_source = first_in_right.is_pipe_source;
  61. command.should_notify_if_in_background = first_in_right.should_wait && last_in_left.should_notify_if_in_background;
  62. Vector<Command> commands;
  63. commands.append(left);
  64. commands.append(command);
  65. commands.append(right);
  66. return commands;
  67. }
  68. void Node::dump(int level) const
  69. {
  70. print_indented(String::format("%s at %d:%d", class_name().characters(), m_position.start_offset, m_position.end_offset), level);
  71. }
  72. Node::Node(Position position)
  73. : m_position(position)
  74. {
  75. }
  76. Vector<Line::CompletionSuggestion> Node::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
  77. {
  78. auto matching_node = hit_test_result.matching_node;
  79. if (matching_node) {
  80. if (matching_node->is_bareword()) {
  81. auto corrected_offset = offset - matching_node->position().start_offset;
  82. auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
  83. if (corrected_offset > node->text().length())
  84. return {};
  85. auto& text = node->text();
  86. // If the literal isn't an option, treat it as a path.
  87. if (!(text.starts_with("-") || text == "--" || text == "-"))
  88. return shell.complete_path("", text, corrected_offset);
  89. // If the literal is an option, we have to know the program name
  90. // should we have no way to get that, bail early.
  91. if (!hit_test_result.closest_command_node)
  92. return {};
  93. auto program_name_node = hit_test_result.closest_command_node->leftmost_trivial_literal();
  94. if (!program_name_node)
  95. return {};
  96. String program_name;
  97. if (program_name_node->is_bareword())
  98. program_name = static_cast<BarewordLiteral*>(program_name_node.ptr())->text();
  99. else
  100. program_name = static_cast<StringLiteral*>(program_name_node.ptr())->text();
  101. return shell.complete_option(program_name, text, corrected_offset);
  102. }
  103. return {};
  104. }
  105. auto result = hit_test_position(offset);
  106. if (!result.matching_node)
  107. return {};
  108. auto node = result.matching_node;
  109. if (node->is_bareword() || node != result.closest_node_with_semantic_meaning)
  110. node = result.closest_node_with_semantic_meaning;
  111. if (!node)
  112. return {};
  113. return node->complete_for_editor(shell, offset, result);
  114. }
  115. Vector<Line::CompletionSuggestion> Node::complete_for_editor(Shell& shell, size_t offset)
  116. {
  117. return Node::complete_for_editor(shell, offset, { nullptr, nullptr, nullptr });
  118. }
  119. Node::~Node()
  120. {
  121. }
  122. void And::dump(int level) const
  123. {
  124. Node::dump(level);
  125. m_left->dump(level + 1);
  126. m_right->dump(level + 1);
  127. }
  128. RefPtr<Value> And::run(RefPtr<Shell> shell)
  129. {
  130. auto left = m_left->run(shell);
  131. ASSERT(left->is_job());
  132. auto* job_value = static_cast<JobValue*>(left.ptr());
  133. const auto job = job_value->job();
  134. if (!job) {
  135. // Something has gone wrong, let's just pretend that the job failed.
  136. return job_value;
  137. }
  138. shell->block_on_job(job);
  139. if (job->exit_code() == 0)
  140. return m_right->run(shell);
  141. return job_value;
  142. }
  143. void And::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  144. {
  145. metadata.is_first_in_list = true;
  146. m_left->highlight_in_editor(editor, shell, metadata);
  147. m_right->highlight_in_editor(editor, shell, metadata);
  148. }
  149. HitTestResult And::hit_test_position(size_t offset)
  150. {
  151. if (!position().contains(offset))
  152. return {};
  153. auto result = m_left->hit_test_position(offset);
  154. if (result.matching_node) {
  155. if (!result.closest_command_node)
  156. result.closest_command_node = m_right;
  157. return result;
  158. }
  159. result = m_right->hit_test_position(offset);
  160. if (!result.closest_command_node)
  161. result.closest_command_node = m_right;
  162. return result;
  163. }
  164. And::And(Position position, RefPtr<Node> left, RefPtr<Node> right)
  165. : Node(move(position))
  166. , m_left(move(left))
  167. , m_right(move(right))
  168. {
  169. if (m_left->is_syntax_error())
  170. set_is_syntax_error(m_left->syntax_error_node());
  171. else if (m_right->is_syntax_error())
  172. set_is_syntax_error(m_right->syntax_error_node());
  173. }
  174. And::~And()
  175. {
  176. }
  177. void ListConcatenate::dump(int level) const
  178. {
  179. Node::dump(level);
  180. m_element->dump(level + 1);
  181. m_list->dump(level + 1);
  182. }
  183. RefPtr<Value> ListConcatenate::run(RefPtr<Shell> shell)
  184. {
  185. auto list = m_list->run(shell)->resolve_without_cast(shell);
  186. auto element = m_element->run(shell)->resolve_without_cast(shell);
  187. if (list->is_command() || element->is_command()) {
  188. auto joined_commands = join_commands(element->resolve_as_commands(shell), list->resolve_as_commands(shell));
  189. if (joined_commands.size() == 1)
  190. return create<CommandValue>(joined_commands[0]);
  191. return create<CommandSequenceValue>(move(joined_commands));
  192. }
  193. return create<ListValue>({ move(element), move(list) });
  194. }
  195. void ListConcatenate::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  196. {
  197. auto first = metadata.is_first_in_list;
  198. metadata.is_first_in_list = false;
  199. m_list->highlight_in_editor(editor, shell, metadata);
  200. metadata.is_first_in_list = first;
  201. m_element->highlight_in_editor(editor, shell, metadata);
  202. }
  203. HitTestResult ListConcatenate::hit_test_position(size_t offset)
  204. {
  205. if (!position().contains(offset))
  206. return {};
  207. auto result = m_element->hit_test_position(offset);
  208. if (result.matching_node)
  209. return result;
  210. result = m_list->hit_test_position(offset);
  211. if (!result.closest_node_with_semantic_meaning)
  212. result.closest_node_with_semantic_meaning = this;
  213. return result;
  214. }
  215. RefPtr<Node> ListConcatenate::leftmost_trivial_literal() const
  216. {
  217. return m_element->leftmost_trivial_literal();
  218. }
  219. ListConcatenate::ListConcatenate(Position position, RefPtr<Node> element, RefPtr<Node> list)
  220. : Node(move(position))
  221. , m_element(move(element))
  222. , m_list(move(list))
  223. {
  224. if (m_element->is_syntax_error())
  225. set_is_syntax_error(m_element->syntax_error_node());
  226. else if (m_list->is_syntax_error())
  227. set_is_syntax_error(m_list->syntax_error_node());
  228. }
  229. ListConcatenate::~ListConcatenate()
  230. {
  231. }
  232. void Background::dump(int level) const
  233. {
  234. Node::dump(level);
  235. m_command->dump(level + 1);
  236. }
  237. RefPtr<Value> Background::run(RefPtr<Shell> shell)
  238. {
  239. auto commands = m_command->run(shell)->resolve_as_commands(shell);
  240. auto& last = commands.last();
  241. last.should_wait = false;
  242. return create<CommandSequenceValue>(move(commands));
  243. }
  244. void Background::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  245. {
  246. m_command->highlight_in_editor(editor, shell, metadata);
  247. }
  248. HitTestResult Background::hit_test_position(size_t offset)
  249. {
  250. if (!position().contains(offset))
  251. return {};
  252. return m_command->hit_test_position(offset);
  253. }
  254. Background::Background(Position position, RefPtr<Node> command)
  255. : Node(move(position))
  256. , m_command(move(command))
  257. {
  258. if (m_command->is_syntax_error())
  259. set_is_syntax_error(m_command->syntax_error_node());
  260. }
  261. Background::~Background()
  262. {
  263. }
  264. void BarewordLiteral::dump(int level) const
  265. {
  266. Node::dump(level);
  267. print_indented(m_text, level + 1);
  268. }
  269. RefPtr<Value> BarewordLiteral::run(RefPtr<Shell>)
  270. {
  271. return create<StringValue>(m_text);
  272. }
  273. void BarewordLiteral::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  274. {
  275. if (metadata.is_first_in_list) {
  276. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Bold });
  277. return;
  278. }
  279. if (m_text.starts_with('-')) {
  280. if (m_text == "--") {
  281. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Green) });
  282. return;
  283. }
  284. if (m_text == "-")
  285. return;
  286. if (m_text.starts_with("--")) {
  287. auto index = m_text.index_of("=").value_or(m_text.length() - 1) + 1;
  288. editor.stylize({ m_position.start_offset, m_position.start_offset + index }, { Line::Style::Foreground(Line::Style::XtermColor::Cyan) });
  289. } else {
  290. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Cyan) });
  291. }
  292. }
  293. if (Core::File::exists(m_text)) {
  294. auto realpath = shell.resolve_path(m_text);
  295. auto url = URL::create_with_file_protocol(realpath);
  296. url.set_host(shell.hostname);
  297. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Hyperlink(url.to_string()) });
  298. }
  299. }
  300. BarewordLiteral::BarewordLiteral(Position position, String text)
  301. : Node(move(position))
  302. , m_text(move(text))
  303. {
  304. }
  305. BarewordLiteral::~BarewordLiteral()
  306. {
  307. }
  308. void CastToCommand::dump(int level) const
  309. {
  310. Node::dump(level);
  311. m_inner->dump(level + 1);
  312. }
  313. RefPtr<Value> CastToCommand::run(RefPtr<Shell> shell)
  314. {
  315. if (m_inner->is_command())
  316. return m_inner->run(shell);
  317. auto value = m_inner->run(shell)->resolve_without_cast(shell);
  318. if (value->is_command())
  319. return value;
  320. auto argv = value->resolve_as_list(shell);
  321. return create<CommandValue>(move(argv));
  322. }
  323. void CastToCommand::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  324. {
  325. m_inner->highlight_in_editor(editor, shell, metadata);
  326. }
  327. HitTestResult CastToCommand::hit_test_position(size_t offset)
  328. {
  329. if (!position().contains(offset))
  330. return {};
  331. auto result = m_inner->hit_test_position(offset);
  332. if (!result.closest_node_with_semantic_meaning)
  333. result.closest_node_with_semantic_meaning = this;
  334. return result;
  335. }
  336. Vector<Line::CompletionSuggestion> CastToCommand::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
  337. {
  338. auto matching_node = hit_test_result.matching_node;
  339. if (!matching_node)
  340. return {};
  341. ASSERT(matching_node->is_bareword());
  342. auto corrected_offset = offset - matching_node->position().start_offset;
  343. auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
  344. if (corrected_offset > node->text().length())
  345. return {};
  346. return shell.complete_program_name(node->text(), corrected_offset);
  347. }
  348. RefPtr<Node> CastToCommand::leftmost_trivial_literal() const
  349. {
  350. return m_inner->leftmost_trivial_literal();
  351. }
  352. CastToCommand::CastToCommand(Position position, RefPtr<Node> inner)
  353. : Node(move(position))
  354. , m_inner(move(inner))
  355. {
  356. if (m_inner->is_syntax_error())
  357. set_is_syntax_error(m_inner->syntax_error_node());
  358. }
  359. CastToCommand::~CastToCommand()
  360. {
  361. }
  362. void CastToList::dump(int level) const
  363. {
  364. Node::dump(level);
  365. if (m_inner)
  366. m_inner->dump(level + 1);
  367. else
  368. print_indented("(empty)", level + 1);
  369. }
  370. RefPtr<Value> CastToList::run(RefPtr<Shell> shell)
  371. {
  372. if (!m_inner)
  373. return create<ListValue>({});
  374. auto inner_value = m_inner->run(shell);
  375. if (inner_value->is_command())
  376. return inner_value;
  377. auto values = inner_value->resolve_as_list(shell);
  378. Vector<RefPtr<Value>> cast_values;
  379. for (auto& value : values)
  380. cast_values.append(create<StringValue>(value));
  381. return create<ListValue>(cast_values);
  382. }
  383. void CastToList::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  384. {
  385. if (m_inner)
  386. m_inner->highlight_in_editor(editor, shell, metadata);
  387. }
  388. HitTestResult CastToList::hit_test_position(size_t offset)
  389. {
  390. if (!position().contains(offset))
  391. return {};
  392. if (!m_inner)
  393. return {};
  394. return m_inner->hit_test_position(offset);
  395. }
  396. RefPtr<Node> CastToList::leftmost_trivial_literal() const
  397. {
  398. return m_inner->leftmost_trivial_literal();
  399. }
  400. CastToList::CastToList(Position position, RefPtr<Node> inner)
  401. : Node(move(position))
  402. , m_inner(move(inner))
  403. {
  404. if (m_inner && m_inner->is_syntax_error())
  405. set_is_syntax_error(m_inner->syntax_error_node());
  406. }
  407. CastToList::~CastToList()
  408. {
  409. }
  410. void CloseFdRedirection::dump(int level) const
  411. {
  412. Node::dump(level);
  413. print_indented(String::format("%d -> Close", m_fd), level);
  414. }
  415. RefPtr<Value> CloseFdRedirection::run(RefPtr<Shell>)
  416. {
  417. Command command;
  418. command.redirections.append(*new CloseRedirection(m_fd));
  419. return create<CommandValue>(move(command));
  420. }
  421. void CloseFdRedirection::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
  422. {
  423. editor.stylize({ m_position.start_offset, m_position.end_offset - 1 }, { Line::Style::Foreground(0x87, 0x9b, 0xcd) }); // 25% Darkened Periwinkle
  424. editor.stylize({ m_position.end_offset - 1, m_position.end_offset }, { Line::Style::Foreground(0xff, 0x7e, 0x00) }); // Amber
  425. }
  426. CloseFdRedirection::CloseFdRedirection(Position position, int fd)
  427. : Node(move(position))
  428. , m_fd(fd)
  429. {
  430. }
  431. CloseFdRedirection::~CloseFdRedirection()
  432. {
  433. }
  434. void CommandLiteral::dump(int level) const
  435. {
  436. Node::dump(level);
  437. print_indented("(Generated command literal)", level + 1);
  438. }
  439. RefPtr<Value> CommandLiteral::run(RefPtr<Shell>)
  440. {
  441. return create<CommandValue>(m_command);
  442. }
  443. CommandLiteral::CommandLiteral(Position position, Command command)
  444. : Node(move(position))
  445. , m_command(move(command))
  446. {
  447. }
  448. CommandLiteral::~CommandLiteral()
  449. {
  450. }
  451. void Comment::dump(int level) const
  452. {
  453. Node::dump(level);
  454. print_indented(m_text, level + 1);
  455. }
  456. RefPtr<Value> Comment::run(RefPtr<Shell>)
  457. {
  458. return create<ListValue>({});
  459. }
  460. void Comment::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
  461. {
  462. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(150, 150, 150) }); // Light gray
  463. }
  464. Comment::Comment(Position position, String text)
  465. : Node(move(position))
  466. , m_text(move(text))
  467. {
  468. }
  469. Comment::~Comment()
  470. {
  471. }
  472. void DoubleQuotedString::dump(int level) const
  473. {
  474. Node::dump(level);
  475. m_inner->dump(level + 1);
  476. }
  477. RefPtr<Value> DoubleQuotedString::run(RefPtr<Shell> shell)
  478. {
  479. StringBuilder builder;
  480. auto values = m_inner->run(shell)->resolve_as_list(shell);
  481. builder.join("", values);
  482. return create<StringValue>(builder.to_string());
  483. }
  484. void DoubleQuotedString::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  485. {
  486. Line::Style style { Line::Style::Foreground(Line::Style::XtermColor::Yellow) };
  487. if (metadata.is_first_in_list)
  488. style.unify_with({ Line::Style::Bold });
  489. editor.stylize({ m_position.start_offset, m_position.end_offset }, style);
  490. metadata.is_first_in_list = false;
  491. m_inner->highlight_in_editor(editor, shell, metadata);
  492. }
  493. HitTestResult DoubleQuotedString::hit_test_position(size_t offset)
  494. {
  495. if (!position().contains(offset))
  496. return {};
  497. return m_inner->hit_test_position(offset);
  498. }
  499. DoubleQuotedString::DoubleQuotedString(Position position, RefPtr<Node> inner)
  500. : Node(move(position))
  501. , m_inner(move(inner))
  502. {
  503. if (m_inner->is_syntax_error())
  504. set_is_syntax_error(m_inner->syntax_error_node());
  505. }
  506. DoubleQuotedString::~DoubleQuotedString()
  507. {
  508. }
  509. void DynamicEvaluate::dump(int level) const
  510. {
  511. Node::dump(level);
  512. m_inner->dump(level + 1);
  513. }
  514. RefPtr<Value> DynamicEvaluate::run(RefPtr<Shell> shell)
  515. {
  516. auto result = m_inner->run(shell)->resolve_without_cast(shell);
  517. // Dynamic Evaluation behaves differently between strings and lists.
  518. // Strings are treated as variables, and Lists are treated as commands.
  519. if (result->is_string()) {
  520. auto name_part = result->resolve_as_list(shell);
  521. ASSERT(name_part.size() == 1);
  522. return create<SimpleVariableValue>(name_part[0]);
  523. }
  524. // If it's anything else, we're just gonna cast it to a list.
  525. auto list = result->resolve_as_list(shell);
  526. return create<CommandValue>(move(list));
  527. }
  528. void DynamicEvaluate::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  529. {
  530. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
  531. m_inner->highlight_in_editor(editor, shell, metadata);
  532. }
  533. HitTestResult DynamicEvaluate::hit_test_position(size_t offset)
  534. {
  535. if (!position().contains(offset))
  536. return {};
  537. return m_inner->hit_test_position(offset);
  538. }
  539. DynamicEvaluate::DynamicEvaluate(Position position, RefPtr<Node> inner)
  540. : Node(move(position))
  541. , m_inner(move(inner))
  542. {
  543. if (m_inner->is_syntax_error())
  544. set_is_syntax_error(m_inner->syntax_error_node());
  545. }
  546. DynamicEvaluate::~DynamicEvaluate()
  547. {
  548. }
  549. void Fd2FdRedirection::dump(int level) const
  550. {
  551. Node::dump(level);
  552. print_indented(String::format("%d -> %d", source_fd, dest_fd), level);
  553. }
  554. RefPtr<Value> Fd2FdRedirection::run(RefPtr<Shell>)
  555. {
  556. Command command;
  557. command.redirections.append(*new FdRedirection(source_fd, dest_fd, Rewiring::Close::None));
  558. return create<CommandValue>(move(command));
  559. }
  560. void Fd2FdRedirection::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
  561. {
  562. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(0x87, 0x9b, 0xcd) }); // 25% Darkened Periwinkle
  563. }
  564. Fd2FdRedirection::Fd2FdRedirection(Position position, int src, int dst)
  565. : Node(move(position))
  566. , source_fd(src)
  567. , dest_fd(dst)
  568. {
  569. }
  570. Fd2FdRedirection::~Fd2FdRedirection()
  571. {
  572. }
  573. void Glob::dump(int level) const
  574. {
  575. Node::dump(level);
  576. print_indented(m_text, level + 1);
  577. }
  578. RefPtr<Value> Glob::run(RefPtr<Shell>)
  579. {
  580. return create<GlobValue>(m_text);
  581. }
  582. void Glob::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata metadata)
  583. {
  584. Line::Style style { Line::Style::Foreground(Line::Style::XtermColor::Cyan) };
  585. if (metadata.is_first_in_list)
  586. style.unify_with({ Line::Style::Bold });
  587. editor.stylize({ m_position.start_offset, m_position.end_offset }, move(style));
  588. }
  589. Glob::Glob(Position position, String text)
  590. : Node(move(position))
  591. , m_text(move(text))
  592. {
  593. }
  594. Glob::~Glob()
  595. {
  596. }
  597. void Execute::dump(int level) const
  598. {
  599. Node::dump(level);
  600. if (m_capture_stdout)
  601. print_indented("(Capturing stdout)", level + 1);
  602. m_command->dump(level + 1);
  603. }
  604. RefPtr<Value> Execute::run(RefPtr<Shell> shell)
  605. {
  606. RefPtr<Job> job;
  607. if (m_command->would_execute())
  608. return m_command->run(shell);
  609. auto initial_commands = m_command->run(shell)->resolve_as_commands(shell);
  610. decltype(initial_commands) commands;
  611. Function<void(Command&)> resolve_aliases_and_append = [&](auto& command) {
  612. if (!command.argv.is_empty()) {
  613. auto alias = shell->resolve_alias(command.argv[0]);
  614. if (!alias.is_null()) {
  615. auto argv0 = command.argv.take_first();
  616. auto subcommand_ast = Parser { alias }.parse();
  617. if (subcommand_ast) {
  618. while (subcommand_ast->is_execute()) {
  619. auto* ast = static_cast<Execute*>(subcommand_ast.ptr());
  620. subcommand_ast = ast->command();
  621. }
  622. RefPtr<Node> substitute = create<Join>(position(), move(subcommand_ast), create<CommandLiteral>(position(), command));
  623. for (auto& subst_command : substitute->run(shell)->resolve_as_commands(shell)) {
  624. if (!subst_command.argv.is_empty() && subst_command.argv.first() == argv0) // Disallow an alias resolving to itself.
  625. commands.append(subst_command);
  626. else
  627. resolve_aliases_and_append(subst_command);
  628. }
  629. } else {
  630. commands.append(command);
  631. }
  632. } else {
  633. commands.append(command);
  634. }
  635. } else {
  636. commands.append(command);
  637. }
  638. };
  639. for (auto& command : initial_commands)
  640. resolve_aliases_and_append(command);
  641. Vector<RefPtr<Job>> jobs_to_wait_for;
  642. auto run_commands = [&](auto& commands) {
  643. for (auto& command : commands) {
  644. #ifdef EXECUTE_DEBUG
  645. dbg() << "Command";
  646. for (auto& arg : command.argv)
  647. dbg() << "argv: " << arg;
  648. for (auto& redir : command.redirections) {
  649. if (redir->is_path_redirection()) {
  650. auto path_redir = (const PathRedirection*)redir.ptr();
  651. dbg() << "redir path " << (int)path_redir->direction << " " << path_redir->path << " <-> " << path_redir->fd;
  652. } else if (redir->is_fd_redirection()) {
  653. auto fd_redir = (const FdRedirection*)redir.ptr();
  654. dbg() << "redir fd " << fd_redir->source_fd << " -> " << fd_redir->dest_fd;
  655. } else if (redir->is_close_redirection()) {
  656. auto close_redir = (const CloseRedirection*)redir.ptr();
  657. dbg() << "close fd " << close_redir->fd;
  658. } else {
  659. ASSERT_NOT_REACHED();
  660. }
  661. }
  662. #endif
  663. job = shell->run_command(command);
  664. if (command.should_wait) {
  665. shell->block_on_job(job);
  666. } else {
  667. if (command.is_pipe_source) {
  668. jobs_to_wait_for.append(job);
  669. } else if (command.should_notify_if_in_background) {
  670. if (job)
  671. job->set_running_in_background(true);
  672. shell->restore_stdin();
  673. }
  674. }
  675. }
  676. };
  677. if (m_capture_stdout) {
  678. int pipefd[2];
  679. int rc = pipe(pipefd);
  680. if (rc < 0) {
  681. dbg() << "Error: cannot pipe(): " << strerror(errno);
  682. return create<StringValue>("");
  683. }
  684. auto& last_in_commands = commands.last();
  685. last_in_commands.redirections.prepend(*new FdRedirection(STDOUT_FILENO, pipefd[1], Rewiring::Close::Destination));
  686. last_in_commands.should_wait = true;
  687. last_in_commands.should_notify_if_in_background = false;
  688. last_in_commands.is_pipe_source = false;
  689. auto notifier = Core::Notifier::construct(pipefd[0], Core::Notifier::Read);
  690. StringBuilder builder;
  691. auto try_read = [&] {
  692. u8 buffer[4096];
  693. size_t remaining_size = 4096;
  694. for (;;) {
  695. if (remaining_size == 0)
  696. break;
  697. auto read_size = read(pipefd[0], buffer, remaining_size);
  698. if (read_size < 0) {
  699. if (errno == EINTR)
  700. continue;
  701. if (errno == 0)
  702. break;
  703. dbg() << "read() failed: " << strerror(errno);
  704. break;
  705. }
  706. if (read_size == 0)
  707. break;
  708. remaining_size -= read_size;
  709. }
  710. builder.append(StringView { buffer, 4096 - remaining_size });
  711. };
  712. notifier->on_ready_to_read = [&] {
  713. try_read();
  714. };
  715. run_commands(commands);
  716. for (auto job : jobs_to_wait_for) {
  717. shell->block_on_job(job);
  718. }
  719. notifier->on_ready_to_read = nullptr;
  720. try_read();
  721. if (close(pipefd[0]) < 0) {
  722. dbg() << "close() failed: " << strerror(errno);
  723. }
  724. return create<StringValue>(builder.build(), shell->local_variable_or("IFS", "\n"), shell->options.inline_exec_keep_empty_segments);
  725. }
  726. run_commands(commands);
  727. for (auto job : jobs_to_wait_for) {
  728. shell->block_on_job(job);
  729. }
  730. return create<JobValue>(move(job));
  731. }
  732. void Execute::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  733. {
  734. if (m_capture_stdout)
  735. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Green) });
  736. metadata.is_first_in_list = true;
  737. m_command->highlight_in_editor(editor, shell, metadata);
  738. }
  739. HitTestResult Execute::hit_test_position(size_t offset)
  740. {
  741. if (!position().contains(offset))
  742. return {};
  743. auto result = m_command->hit_test_position(offset);
  744. if (!result.closest_node_with_semantic_meaning)
  745. result.closest_node_with_semantic_meaning = this;
  746. if (!result.closest_command_node)
  747. result.closest_command_node = m_command;
  748. return result;
  749. }
  750. Vector<Line::CompletionSuggestion> Execute::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
  751. {
  752. auto matching_node = hit_test_result.matching_node;
  753. if (!matching_node)
  754. return {};
  755. ASSERT(matching_node->is_bareword());
  756. auto corrected_offset = offset - matching_node->position().start_offset;
  757. auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
  758. if (corrected_offset > node->text().length())
  759. return {};
  760. return shell.complete_program_name(node->text(), corrected_offset);
  761. }
  762. Execute::Execute(Position position, RefPtr<Node> command, bool capture_stdout)
  763. : Node(move(position))
  764. , m_command(move(command))
  765. , m_capture_stdout(capture_stdout)
  766. {
  767. if (m_command->is_syntax_error())
  768. set_is_syntax_error(m_command->syntax_error_node());
  769. }
  770. Execute::~Execute()
  771. {
  772. }
  773. void Join::dump(int level) const
  774. {
  775. Node::dump(level);
  776. m_left->dump(level + 1);
  777. m_right->dump(level + 1);
  778. }
  779. RefPtr<Value> Join::run(RefPtr<Shell> shell)
  780. {
  781. auto left = m_left->run(shell)->resolve_as_commands(shell);
  782. auto right = m_right->run(shell)->resolve_as_commands(shell);
  783. return create<CommandSequenceValue>(join_commands(move(left), move(right)));
  784. }
  785. void Join::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  786. {
  787. m_left->highlight_in_editor(editor, shell, metadata);
  788. if (m_left->is_list() || m_left->is_command())
  789. metadata.is_first_in_list = false;
  790. m_right->highlight_in_editor(editor, shell, metadata);
  791. }
  792. HitTestResult Join::hit_test_position(size_t offset)
  793. {
  794. if (!position().contains(offset))
  795. return {};
  796. auto result = m_left->hit_test_position(offset);
  797. if (result.matching_node)
  798. return result;
  799. return m_right->hit_test_position(offset);
  800. }
  801. RefPtr<Node> Join::leftmost_trivial_literal() const
  802. {
  803. if (auto value = m_left->leftmost_trivial_literal())
  804. return value;
  805. return m_right->leftmost_trivial_literal();
  806. }
  807. Join::Join(Position position, RefPtr<Node> left, RefPtr<Node> right)
  808. : Node(move(position))
  809. , m_left(move(left))
  810. , m_right(move(right))
  811. {
  812. if (m_left->is_syntax_error())
  813. set_is_syntax_error(m_left->syntax_error_node());
  814. else if (m_right->is_syntax_error())
  815. set_is_syntax_error(m_right->syntax_error_node());
  816. }
  817. Join::~Join()
  818. {
  819. }
  820. void Or::dump(int level) const
  821. {
  822. Node::dump(level);
  823. m_left->dump(level + 1);
  824. m_right->dump(level + 1);
  825. }
  826. RefPtr<Value> Or::run(RefPtr<Shell> shell)
  827. {
  828. auto left = m_left->run(shell);
  829. ASSERT(left->is_job());
  830. auto* job_value = static_cast<JobValue*>(left.ptr());
  831. const auto job = job_value->job();
  832. if (!job) {
  833. // Something has gone wrong, let's just pretend that the job failed.
  834. return m_right->run(shell);
  835. }
  836. shell->block_on_job(job);
  837. if (job->exit_code() == 0)
  838. return job_value;
  839. return m_right->run(shell);
  840. }
  841. void Or::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  842. {
  843. m_left->highlight_in_editor(editor, shell, metadata);
  844. m_right->highlight_in_editor(editor, shell, metadata);
  845. }
  846. HitTestResult Or::hit_test_position(size_t offset)
  847. {
  848. if (!position().contains(offset))
  849. return {};
  850. auto result = m_left->hit_test_position(offset);
  851. if (result.matching_node) {
  852. if (!result.closest_command_node)
  853. result.closest_command_node = m_right;
  854. return result;
  855. }
  856. result = m_right->hit_test_position(offset);
  857. if (!result.closest_command_node)
  858. result.closest_command_node = m_right;
  859. return result;
  860. }
  861. Or::Or(Position position, RefPtr<Node> left, RefPtr<Node> right)
  862. : Node(move(position))
  863. , m_left(move(left))
  864. , m_right(move(right))
  865. {
  866. if (m_left->is_syntax_error())
  867. set_is_syntax_error(m_left->syntax_error_node());
  868. else if (m_right->is_syntax_error())
  869. set_is_syntax_error(m_right->syntax_error_node());
  870. }
  871. Or::~Or()
  872. {
  873. }
  874. void Pipe::dump(int level) const
  875. {
  876. Node::dump(level);
  877. m_left->dump(level + 1);
  878. m_right->dump(level + 1);
  879. }
  880. RefPtr<Value> Pipe::run(RefPtr<Shell> shell)
  881. {
  882. auto left = m_left->run(shell)->resolve_as_commands(shell);
  883. auto right = m_right->run(shell)->resolve_as_commands(shell);
  884. auto last_in_left = left.take_last();
  885. auto first_in_right = right.take_first();
  886. auto pipe_write_end = new FdRedirection(STDIN_FILENO, -1, Rewiring::Close::Destination);
  887. auto pipe_read_end = new FdRedirection(STDOUT_FILENO, -1, pipe_write_end, Rewiring::Close::RefreshDestination);
  888. first_in_right.redirections.append(*pipe_write_end);
  889. last_in_left.redirections.append(*pipe_read_end);
  890. last_in_left.should_wait = false;
  891. last_in_left.is_pipe_source = true;
  892. Vector<Command> commands;
  893. commands.append(left);
  894. commands.append(last_in_left);
  895. commands.append(first_in_right);
  896. commands.append(right);
  897. return create<CommandSequenceValue>(move(commands));
  898. }
  899. void Pipe::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  900. {
  901. m_left->highlight_in_editor(editor, shell, metadata);
  902. m_right->highlight_in_editor(editor, shell, metadata);
  903. }
  904. HitTestResult Pipe::hit_test_position(size_t offset)
  905. {
  906. if (!position().contains(offset))
  907. return {};
  908. auto result = m_left->hit_test_position(offset);
  909. if (result.matching_node)
  910. return result;
  911. return m_right->hit_test_position(offset);
  912. }
  913. Pipe::Pipe(Position position, RefPtr<Node> left, RefPtr<Node> right)
  914. : Node(move(position))
  915. , m_left(move(left))
  916. , m_right(move(right))
  917. {
  918. if (m_left->is_syntax_error())
  919. set_is_syntax_error(m_left->syntax_error_node());
  920. else if (m_right->is_syntax_error())
  921. set_is_syntax_error(m_right->syntax_error_node());
  922. }
  923. Pipe::~Pipe()
  924. {
  925. }
  926. PathRedirectionNode::PathRedirectionNode(Position position, int fd, RefPtr<Node> path)
  927. : Node(move(position))
  928. , m_fd(fd)
  929. , m_path(move(path))
  930. {
  931. }
  932. void PathRedirectionNode::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  933. {
  934. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(0x87, 0x9b, 0xcd) }); // 25% Darkened Periwinkle
  935. metadata.is_first_in_list = false;
  936. m_path->highlight_in_editor(editor, shell, metadata);
  937. if (m_path->is_bareword()) {
  938. auto path_text = m_path->run(nullptr)->resolve_as_list(nullptr);
  939. ASSERT(path_text.size() == 1);
  940. // Apply a URL to the path.
  941. auto& position = m_path->position();
  942. auto& path = path_text[0];
  943. if (!path.starts_with('/'))
  944. path = String::format("%s/%s", shell.cwd.characters(), path.characters());
  945. auto url = URL::create_with_file_protocol(path);
  946. url.set_host(shell.hostname);
  947. editor.stylize({ position.start_offset, position.end_offset }, { Line::Style::Hyperlink(url.to_string()) });
  948. }
  949. }
  950. HitTestResult PathRedirectionNode::hit_test_position(size_t offset)
  951. {
  952. if (!position().contains(offset))
  953. return {};
  954. auto result = m_path->hit_test_position(offset);
  955. if (!result.closest_node_with_semantic_meaning)
  956. result.closest_node_with_semantic_meaning = this;
  957. return result;
  958. }
  959. Vector<Line::CompletionSuggestion> PathRedirectionNode::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
  960. {
  961. auto matching_node = hit_test_result.matching_node;
  962. if (!matching_node)
  963. return {};
  964. ASSERT(matching_node->is_bareword());
  965. auto corrected_offset = offset - matching_node->position().start_offset;
  966. auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
  967. if (corrected_offset > node->text().length())
  968. return {};
  969. return shell.complete_path("", node->text(), corrected_offset);
  970. }
  971. PathRedirectionNode::~PathRedirectionNode()
  972. {
  973. }
  974. void ReadRedirection::dump(int level) const
  975. {
  976. Node::dump(level);
  977. m_path->dump(level + 1);
  978. print_indented(String::format("To %d", m_fd), level + 1);
  979. }
  980. RefPtr<Value> ReadRedirection::run(RefPtr<Shell> shell)
  981. {
  982. Command command;
  983. auto path_segments = m_path->run(shell)->resolve_as_list(shell);
  984. StringBuilder builder;
  985. builder.join(" ", path_segments);
  986. command.redirections.append(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::Read));
  987. return create<CommandValue>(move(command));
  988. }
  989. ReadRedirection::ReadRedirection(Position position, int fd, RefPtr<Node> path)
  990. : PathRedirectionNode(move(position), fd, move(path))
  991. {
  992. }
  993. ReadRedirection::~ReadRedirection()
  994. {
  995. }
  996. void ReadWriteRedirection::dump(int level) const
  997. {
  998. Node::dump(level);
  999. m_path->dump(level + 1);
  1000. print_indented(String::format("To/From %d", m_fd), level + 1);
  1001. }
  1002. RefPtr<Value> ReadWriteRedirection::run(RefPtr<Shell> shell)
  1003. {
  1004. Command command;
  1005. auto path_segments = m_path->run(shell)->resolve_as_list(shell);
  1006. StringBuilder builder;
  1007. builder.join(" ", path_segments);
  1008. command.redirections.append(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::ReadWrite));
  1009. return create<CommandValue>(move(command));
  1010. }
  1011. ReadWriteRedirection::ReadWriteRedirection(Position position, int fd, RefPtr<Node> path)
  1012. : PathRedirectionNode(move(position), fd, move(path))
  1013. {
  1014. }
  1015. ReadWriteRedirection::~ReadWriteRedirection()
  1016. {
  1017. }
  1018. void Sequence::dump(int level) const
  1019. {
  1020. Node::dump(level);
  1021. m_left->dump(level + 1);
  1022. m_right->dump(level + 1);
  1023. }
  1024. RefPtr<Value> Sequence::run(RefPtr<Shell> shell)
  1025. {
  1026. // If we are to return a job, block on the left one then return the right one.
  1027. if (would_execute()) {
  1028. RefPtr<AST::Node> execute_node = create<AST::Execute>(m_left->position(), m_left);
  1029. auto left_job = execute_node->run(shell);
  1030. ASSERT(left_job->is_job());
  1031. shell->block_on_job(static_cast<JobValue*>(left_job.ptr())->job());
  1032. if (m_right->would_execute())
  1033. return m_right->run(shell);
  1034. execute_node = create<AST::Execute>(m_right->position(), m_right);
  1035. return execute_node->run(shell);
  1036. }
  1037. auto left = m_left->run(shell)->resolve_as_commands(shell);
  1038. // This could happen if a comment is next to a command.
  1039. if (left.size() == 1) {
  1040. auto& command = left.first();
  1041. if (command.argv.is_empty() && command.redirections.is_empty())
  1042. return m_right->run(shell);
  1043. }
  1044. auto right = m_right->run(shell)->resolve_as_commands(shell);
  1045. Vector<Command> commands;
  1046. commands.append(left);
  1047. commands.append(right);
  1048. return create<CommandSequenceValue>(move(commands));
  1049. }
  1050. void Sequence::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  1051. {
  1052. m_left->highlight_in_editor(editor, shell, metadata);
  1053. m_right->highlight_in_editor(editor, shell, metadata);
  1054. }
  1055. HitTestResult Sequence::hit_test_position(size_t offset)
  1056. {
  1057. if (!position().contains(offset))
  1058. return {};
  1059. auto result = m_left->hit_test_position(offset);
  1060. if (result.matching_node)
  1061. return result;
  1062. return m_right->hit_test_position(offset);
  1063. }
  1064. Sequence::Sequence(Position position, RefPtr<Node> left, RefPtr<Node> right)
  1065. : Node(move(position))
  1066. , m_left(move(left))
  1067. , m_right(move(right))
  1068. {
  1069. if (m_left->is_syntax_error())
  1070. set_is_syntax_error(m_left->syntax_error_node());
  1071. else if (m_right->is_syntax_error())
  1072. set_is_syntax_error(m_right->syntax_error_node());
  1073. }
  1074. Sequence::~Sequence()
  1075. {
  1076. }
  1077. void SimpleVariable::dump(int level) const
  1078. {
  1079. Node::dump(level);
  1080. print_indented(m_name, level + 1);
  1081. }
  1082. RefPtr<Value> SimpleVariable::run(RefPtr<Shell>)
  1083. {
  1084. return create<SimpleVariableValue>(m_name);
  1085. }
  1086. void SimpleVariable::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata metadata)
  1087. {
  1088. Line::Style style { Line::Style::Foreground(214, 112, 214) };
  1089. if (metadata.is_first_in_list)
  1090. style.unify_with({ Line::Style::Bold });
  1091. editor.stylize({ m_position.start_offset, m_position.end_offset }, move(style));
  1092. }
  1093. HitTestResult SimpleVariable::hit_test_position(size_t offset)
  1094. {
  1095. if (!position().contains(offset))
  1096. return {};
  1097. return { this, this, nullptr };
  1098. }
  1099. Vector<Line::CompletionSuggestion> SimpleVariable::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
  1100. {
  1101. auto matching_node = hit_test_result.matching_node;
  1102. if (!matching_node)
  1103. return {};
  1104. if (matching_node != this)
  1105. return {};
  1106. auto corrected_offset = offset - matching_node->position().start_offset - 1;
  1107. if (corrected_offset > m_name.length() + 1)
  1108. return {};
  1109. return shell.complete_variable(m_name, corrected_offset);
  1110. }
  1111. SimpleVariable::SimpleVariable(Position position, String name)
  1112. : Node(move(position))
  1113. , m_name(move(name))
  1114. {
  1115. }
  1116. SimpleVariable::~SimpleVariable()
  1117. {
  1118. }
  1119. void SpecialVariable::dump(int level) const
  1120. {
  1121. Node::dump(level);
  1122. print_indented(String { &m_name, 1 }, level + 1);
  1123. }
  1124. RefPtr<Value> SpecialVariable::run(RefPtr<Shell>)
  1125. {
  1126. return create<SpecialVariableValue>(m_name);
  1127. }
  1128. void SpecialVariable::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
  1129. {
  1130. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(214, 112, 214) });
  1131. }
  1132. Vector<Line::CompletionSuggestion> SpecialVariable::complete_for_editor(Shell&, size_t, const HitTestResult&)
  1133. {
  1134. return {};
  1135. }
  1136. HitTestResult SpecialVariable::hit_test_position(size_t offset)
  1137. {
  1138. if (!position().contains(offset))
  1139. return {};
  1140. return { this, this, nullptr };
  1141. }
  1142. SpecialVariable::SpecialVariable(Position position, char name)
  1143. : Node(move(position))
  1144. , m_name(name)
  1145. {
  1146. }
  1147. SpecialVariable::~SpecialVariable()
  1148. {
  1149. }
  1150. void Juxtaposition::dump(int level) const
  1151. {
  1152. Node::dump(level);
  1153. m_left->dump(level + 1);
  1154. m_right->dump(level + 1);
  1155. }
  1156. RefPtr<Value> Juxtaposition::run(RefPtr<Shell> shell)
  1157. {
  1158. auto left_value = m_left->run(shell)->resolve_without_cast(shell);
  1159. auto right_value = m_right->run(shell)->resolve_without_cast(shell);
  1160. auto left = left_value->resolve_as_list(shell);
  1161. auto right = right_value->resolve_as_list(shell);
  1162. if (left_value->is_string() && right_value->is_string()) {
  1163. ASSERT(left.size() == 1);
  1164. ASSERT(right.size() == 1);
  1165. StringBuilder builder;
  1166. builder.append(left[0]);
  1167. builder.append(right[0]);
  1168. return create<StringValue>(builder.to_string());
  1169. }
  1170. // Otherwise, treat them as lists and create a list product.
  1171. if (left.is_empty() || right.is_empty())
  1172. return create<ListValue>({});
  1173. Vector<String> result;
  1174. result.ensure_capacity(left.size() * right.size());
  1175. StringBuilder builder;
  1176. for (auto& left_element : left) {
  1177. for (auto& right_element : right) {
  1178. builder.append(left_element);
  1179. builder.append(right_element);
  1180. result.append(builder.to_string());
  1181. builder.clear();
  1182. }
  1183. }
  1184. return create<ListValue>(move(result));
  1185. }
  1186. void Juxtaposition::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  1187. {
  1188. m_left->highlight_in_editor(editor, shell, metadata);
  1189. // '~/foo/bar' is special, we have to actually resolve the tilde
  1190. // since that resolution is a pure operation, we can just go ahead
  1191. // and do it to get the value :)
  1192. if (m_right->is_bareword() && m_left->is_tilde()) {
  1193. auto tilde_value = m_left->run(shell)->resolve_as_list(shell)[0];
  1194. auto bareword_value = m_right->run(shell)->resolve_as_list(shell)[0];
  1195. StringBuilder path_builder;
  1196. path_builder.append(tilde_value);
  1197. path_builder.append("/");
  1198. path_builder.append(bareword_value);
  1199. auto path = path_builder.to_string();
  1200. if (Core::File::exists(path)) {
  1201. auto realpath = shell.resolve_path(path);
  1202. auto url = URL::create_with_file_protocol(realpath);
  1203. url.set_host(shell.hostname);
  1204. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Hyperlink(url.to_string()) });
  1205. }
  1206. } else {
  1207. m_right->highlight_in_editor(editor, shell, metadata);
  1208. }
  1209. }
  1210. Vector<Line::CompletionSuggestion> Juxtaposition::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
  1211. {
  1212. auto matching_node = hit_test_result.matching_node;
  1213. // '~/foo/bar' is special, we have to actually resolve the tilde
  1214. // then complete the bareword with that path prefix.
  1215. if (m_right->is_bareword() && m_left->is_tilde()) {
  1216. auto tilde_value = m_left->run(shell)->resolve_as_list(shell)[0];
  1217. auto corrected_offset = offset - matching_node->position().start_offset;
  1218. auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
  1219. if (corrected_offset > node->text().length())
  1220. return {};
  1221. auto text = node->text().substring(1, node->text().length() - 1);
  1222. return shell.complete_path(tilde_value, text, corrected_offset - 1);
  1223. }
  1224. return Node::complete_for_editor(shell, offset, hit_test_result);
  1225. }
  1226. HitTestResult Juxtaposition::hit_test_position(size_t offset)
  1227. {
  1228. if (!position().contains(offset))
  1229. return {};
  1230. auto result = m_left->hit_test_position(offset);
  1231. if (!result.closest_node_with_semantic_meaning)
  1232. result.closest_node_with_semantic_meaning = this;
  1233. if (result.matching_node)
  1234. return result;
  1235. result = m_right->hit_test_position(offset);
  1236. if (!result.closest_node_with_semantic_meaning)
  1237. result.closest_node_with_semantic_meaning = this;
  1238. return result;
  1239. }
  1240. Juxtaposition::Juxtaposition(Position position, RefPtr<Node> left, RefPtr<Node> right)
  1241. : Node(move(position))
  1242. , m_left(move(left))
  1243. , m_right(move(right))
  1244. {
  1245. if (m_left->is_syntax_error())
  1246. set_is_syntax_error(m_left->syntax_error_node());
  1247. else if (m_right->is_syntax_error())
  1248. set_is_syntax_error(m_right->syntax_error_node());
  1249. }
  1250. Juxtaposition::~Juxtaposition()
  1251. {
  1252. }
  1253. void StringLiteral::dump(int level) const
  1254. {
  1255. Node::dump(level);
  1256. print_indented(m_text, level + 1);
  1257. }
  1258. RefPtr<Value> StringLiteral::run(RefPtr<Shell>)
  1259. {
  1260. return create<StringValue>(m_text);
  1261. }
  1262. void StringLiteral::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata metadata)
  1263. {
  1264. Line::Style style { Line::Style::Foreground(Line::Style::XtermColor::Yellow) };
  1265. if (metadata.is_first_in_list)
  1266. style.unify_with({ Line::Style::Bold });
  1267. editor.stylize({ m_position.start_offset, m_position.end_offset }, move(style));
  1268. }
  1269. StringLiteral::StringLiteral(Position position, String text)
  1270. : Node(move(position))
  1271. , m_text(move(text))
  1272. {
  1273. }
  1274. StringLiteral::~StringLiteral()
  1275. {
  1276. }
  1277. void StringPartCompose::dump(int level) const
  1278. {
  1279. Node::dump(level);
  1280. m_left->dump(level + 1);
  1281. m_right->dump(level + 1);
  1282. }
  1283. RefPtr<Value> StringPartCompose::run(RefPtr<Shell> shell)
  1284. {
  1285. auto left = m_left->run(shell)->resolve_as_list(shell);
  1286. auto right = m_right->run(shell)->resolve_as_list(shell);
  1287. StringBuilder builder;
  1288. builder.join(" ", left);
  1289. builder.join(" ", right);
  1290. return create<StringValue>(builder.to_string());
  1291. }
  1292. void StringPartCompose::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  1293. {
  1294. m_left->highlight_in_editor(editor, shell, metadata);
  1295. m_right->highlight_in_editor(editor, shell, metadata);
  1296. }
  1297. HitTestResult StringPartCompose::hit_test_position(size_t offset)
  1298. {
  1299. if (!position().contains(offset))
  1300. return {};
  1301. auto result = m_left->hit_test_position(offset);
  1302. if (result.matching_node)
  1303. return result;
  1304. return m_right->hit_test_position(offset);
  1305. }
  1306. StringPartCompose::StringPartCompose(Position position, RefPtr<Node> left, RefPtr<Node> right)
  1307. : Node(move(position))
  1308. , m_left(move(left))
  1309. , m_right(move(right))
  1310. {
  1311. if (m_left->is_syntax_error())
  1312. set_is_syntax_error(m_left->syntax_error_node());
  1313. else if (m_right->is_syntax_error())
  1314. set_is_syntax_error(m_right->syntax_error_node());
  1315. }
  1316. StringPartCompose::~StringPartCompose()
  1317. {
  1318. }
  1319. void SyntaxError::dump(int level) const
  1320. {
  1321. Node::dump(level);
  1322. }
  1323. RefPtr<Value> SyntaxError::run(RefPtr<Shell>)
  1324. {
  1325. dbg() << "SYNTAX ERROR AAAA";
  1326. return create<StringValue>("");
  1327. }
  1328. void SyntaxError::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
  1329. {
  1330. editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Red), Line::Style::Bold });
  1331. }
  1332. SyntaxError::SyntaxError(Position position, String error)
  1333. : Node(move(position))
  1334. , m_syntax_error_text(move(error))
  1335. {
  1336. m_is_syntax_error = true;
  1337. }
  1338. const SyntaxError& SyntaxError::syntax_error_node() const
  1339. {
  1340. return *this;
  1341. }
  1342. SyntaxError::~SyntaxError()
  1343. {
  1344. }
  1345. void Tilde::dump(int level) const
  1346. {
  1347. Node::dump(level);
  1348. print_indented(m_username, level + 1);
  1349. }
  1350. RefPtr<Value> Tilde::run(RefPtr<Shell>)
  1351. {
  1352. return create<TildeValue>(m_username);
  1353. }
  1354. void Tilde::highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata)
  1355. {
  1356. }
  1357. HitTestResult Tilde::hit_test_position(size_t offset)
  1358. {
  1359. if (!position().contains(offset))
  1360. return {};
  1361. return { this, this, nullptr };
  1362. }
  1363. Vector<Line::CompletionSuggestion> Tilde::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
  1364. {
  1365. auto matching_node = hit_test_result.matching_node;
  1366. if (!matching_node)
  1367. return {};
  1368. if (matching_node != this)
  1369. return {};
  1370. auto corrected_offset = offset - matching_node->position().start_offset - 1;
  1371. if (corrected_offset > m_username.length() + 1)
  1372. return {};
  1373. return shell.complete_user(m_username, corrected_offset);
  1374. }
  1375. String Tilde::text() const
  1376. {
  1377. StringBuilder builder;
  1378. builder.append('~');
  1379. builder.append(m_username);
  1380. return builder.to_string();
  1381. }
  1382. Tilde::Tilde(Position position, String username)
  1383. : Node(move(position))
  1384. , m_username(move(username))
  1385. {
  1386. }
  1387. Tilde::~Tilde()
  1388. {
  1389. }
  1390. void WriteAppendRedirection::dump(int level) const
  1391. {
  1392. Node::dump(level);
  1393. m_path->dump(level + 1);
  1394. print_indented(String::format("From %d", m_fd), level + 1);
  1395. }
  1396. RefPtr<Value> WriteAppendRedirection::run(RefPtr<Shell> shell)
  1397. {
  1398. Command command;
  1399. auto path_segments = m_path->run(shell)->resolve_as_list(shell);
  1400. StringBuilder builder;
  1401. builder.join(" ", path_segments);
  1402. command.redirections.append(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::WriteAppend));
  1403. return create<CommandValue>(move(command));
  1404. }
  1405. WriteAppendRedirection::WriteAppendRedirection(Position position, int fd, RefPtr<Node> path)
  1406. : PathRedirectionNode(move(position), fd, move(path))
  1407. {
  1408. }
  1409. WriteAppendRedirection::~WriteAppendRedirection()
  1410. {
  1411. }
  1412. void WriteRedirection::dump(int level) const
  1413. {
  1414. Node::dump(level);
  1415. m_path->dump(level + 1);
  1416. print_indented(String::format("From %d", m_fd), level + 1);
  1417. }
  1418. RefPtr<Value> WriteRedirection::run(RefPtr<Shell> shell)
  1419. {
  1420. Command command;
  1421. auto path_segments = m_path->run(shell)->resolve_as_list(shell);
  1422. StringBuilder builder;
  1423. builder.join(" ", path_segments);
  1424. command.redirections.append(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::Write));
  1425. return create<CommandValue>(move(command));
  1426. }
  1427. WriteRedirection::WriteRedirection(Position position, int fd, RefPtr<Node> path)
  1428. : PathRedirectionNode(move(position), fd, move(path))
  1429. {
  1430. }
  1431. WriteRedirection::~WriteRedirection()
  1432. {
  1433. }
  1434. void VariableDeclarations::dump(int level) const
  1435. {
  1436. Node::dump(level);
  1437. for (auto& var : m_variables) {
  1438. print_indented("Set", level + 1);
  1439. var.name->dump(level + 2);
  1440. var.value->dump(level + 2);
  1441. }
  1442. }
  1443. RefPtr<Value> VariableDeclarations::run(RefPtr<Shell> shell)
  1444. {
  1445. for (auto& var : m_variables) {
  1446. auto name_value = var.name->run(shell)->resolve_as_list(shell);
  1447. ASSERT(name_value.size() == 1);
  1448. auto name = name_value[0];
  1449. auto value = var.value->run(shell);
  1450. if (value->is_list()) {
  1451. auto parts = value->resolve_as_list(shell);
  1452. shell->set_local_variable(name, adopt(*new ListValue(move(parts))));
  1453. } else if (value->is_command()) {
  1454. shell->set_local_variable(name, value);
  1455. } else {
  1456. auto part = value->resolve_as_list(shell);
  1457. shell->set_local_variable(name, adopt(*new StringValue(part[0])));
  1458. }
  1459. }
  1460. return create<ListValue>({});
  1461. }
  1462. void VariableDeclarations::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
  1463. {
  1464. metadata.is_first_in_list = false;
  1465. for (auto& var : m_variables) {
  1466. var.name->highlight_in_editor(editor, shell, metadata);
  1467. // Highlight the '='.
  1468. editor.stylize({ var.name->position().end_offset - 1, var.name->position().end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Blue) });
  1469. var.value->highlight_in_editor(editor, shell, metadata);
  1470. }
  1471. }
  1472. HitTestResult VariableDeclarations::hit_test_position(size_t offset)
  1473. {
  1474. if (!position().contains(offset))
  1475. return {};
  1476. for (auto decl : m_variables) {
  1477. auto result = decl.value->hit_test_position(offset);
  1478. if (result.matching_node)
  1479. return result;
  1480. }
  1481. return { nullptr, nullptr, nullptr };
  1482. }
  1483. VariableDeclarations::VariableDeclarations(Position position, Vector<Variable> variables)
  1484. : Node(move(position))
  1485. , m_variables(move(variables))
  1486. {
  1487. for (auto& decl : m_variables) {
  1488. if (decl.name->is_syntax_error()) {
  1489. set_is_syntax_error(decl.name->syntax_error_node());
  1490. break;
  1491. }
  1492. if (decl.value->is_syntax_error()) {
  1493. set_is_syntax_error(decl.value->syntax_error_node());
  1494. break;
  1495. }
  1496. }
  1497. }
  1498. VariableDeclarations::~VariableDeclarations()
  1499. {
  1500. }
  1501. Value::~Value()
  1502. {
  1503. }
  1504. Vector<AST::Command> Value::resolve_as_commands(RefPtr<Shell> shell)
  1505. {
  1506. Command command;
  1507. command.argv = resolve_as_list(shell);
  1508. return { command };
  1509. }
  1510. ListValue::ListValue(Vector<String> values)
  1511. {
  1512. m_contained_values.ensure_capacity(values.size());
  1513. for (auto& str : values)
  1514. m_contained_values.append(adopt(*new StringValue(move(str))));
  1515. }
  1516. ListValue::~ListValue()
  1517. {
  1518. }
  1519. Vector<String> ListValue::resolve_as_list(RefPtr<Shell> shell)
  1520. {
  1521. Vector<String> values;
  1522. for (auto& value : m_contained_values)
  1523. values.append(value->resolve_as_list(shell));
  1524. return values;
  1525. }
  1526. CommandValue::~CommandValue()
  1527. {
  1528. }
  1529. CommandSequenceValue::~CommandSequenceValue()
  1530. {
  1531. }
  1532. Vector<String> CommandSequenceValue::resolve_as_list(RefPtr<Shell>)
  1533. {
  1534. // TODO: Somehow raise an "error".
  1535. return {};
  1536. }
  1537. Vector<Command> CommandSequenceValue::resolve_as_commands(RefPtr<Shell>)
  1538. {
  1539. return m_contained_values;
  1540. }
  1541. Vector<String> CommandValue::resolve_as_list(RefPtr<Shell>)
  1542. {
  1543. // TODO: Somehow raise an "error".
  1544. return {};
  1545. }
  1546. Vector<Command> CommandValue::resolve_as_commands(RefPtr<Shell>)
  1547. {
  1548. return { m_command };
  1549. }
  1550. JobValue::~JobValue()
  1551. {
  1552. }
  1553. StringValue::~StringValue()
  1554. {
  1555. }
  1556. Vector<String> StringValue::resolve_as_list(RefPtr<Shell>)
  1557. {
  1558. if (is_list()) {
  1559. auto parts = StringView(m_string).split_view(m_split, m_keep_empty);
  1560. Vector<String> result;
  1561. result.ensure_capacity(parts.size());
  1562. for (auto& part : parts)
  1563. result.append(part);
  1564. return result;
  1565. }
  1566. return { m_string };
  1567. }
  1568. GlobValue::~GlobValue()
  1569. {
  1570. }
  1571. Vector<String> GlobValue::resolve_as_list(RefPtr<Shell> shell)
  1572. {
  1573. return shell->expand_globs(m_glob, shell->cwd);
  1574. }
  1575. SimpleVariableValue::~SimpleVariableValue()
  1576. {
  1577. }
  1578. Vector<String> SimpleVariableValue::resolve_as_list(RefPtr<Shell> shell)
  1579. {
  1580. if (auto value = resolve_without_cast(shell); value != this)
  1581. return value->resolve_as_list(shell);
  1582. char* env_value = getenv(m_name.characters());
  1583. if (env_value == nullptr)
  1584. return { "" };
  1585. Vector<String> res;
  1586. String str_env_value = String(env_value);
  1587. const auto& split_text = str_env_value.split_view(' ');
  1588. for (auto& part : split_text)
  1589. res.append(part);
  1590. return res;
  1591. }
  1592. RefPtr<Value> SimpleVariableValue::resolve_without_cast(RefPtr<Shell> shell)
  1593. {
  1594. if (auto value = shell->lookup_local_variable(m_name))
  1595. return value;
  1596. return this;
  1597. }
  1598. SpecialVariableValue::~SpecialVariableValue()
  1599. {
  1600. }
  1601. Vector<String> SpecialVariableValue::resolve_as_list(RefPtr<Shell> shell)
  1602. {
  1603. switch (m_name) {
  1604. case '?':
  1605. return { String::number(shell->last_return_code) };
  1606. case '$':
  1607. return { String::number(getpid()) };
  1608. default:
  1609. return { "" };
  1610. }
  1611. }
  1612. TildeValue::~TildeValue()
  1613. {
  1614. }
  1615. Vector<String> TildeValue::resolve_as_list(RefPtr<Shell> shell)
  1616. {
  1617. StringBuilder builder;
  1618. builder.append("~");
  1619. builder.append(m_username);
  1620. return { shell->expand_tilde(builder.to_string()) };
  1621. }
  1622. Result<RefPtr<Rewiring>, String> CloseRedirection::apply()
  1623. {
  1624. return static_cast<RefPtr<Rewiring>>((adopt(*new Rewiring(fd, fd, Rewiring::Close::ImmediatelyCloseDestination))));
  1625. }
  1626. CloseRedirection::~CloseRedirection()
  1627. {
  1628. }
  1629. Result<RefPtr<Rewiring>, String> PathRedirection::apply()
  1630. {
  1631. auto check_fd_and_return = [my_fd = this->fd](int fd, const String& path) -> Result<RefPtr<Rewiring>, String> {
  1632. if (fd < 0) {
  1633. String error = strerror(errno);
  1634. dbg() << "open() failed for '" << path << "' with " << error;
  1635. return error;
  1636. }
  1637. return static_cast<RefPtr<Rewiring>>((adopt(*new Rewiring(my_fd, fd, Rewiring::Close::Destination))));
  1638. };
  1639. switch (direction) {
  1640. case AST::PathRedirection::WriteAppend:
  1641. return check_fd_and_return(open(path.characters(), O_WRONLY | O_CREAT | O_APPEND, 0666), path);
  1642. case AST::PathRedirection::Write:
  1643. return check_fd_and_return(open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666), path);
  1644. case AST::PathRedirection::Read:
  1645. return check_fd_and_return(open(path.characters(), O_RDONLY), path);
  1646. case AST::PathRedirection::ReadWrite:
  1647. return check_fd_and_return(open(path.characters(), O_RDWR | O_CREAT, 0666), path);
  1648. }
  1649. ASSERT_NOT_REACHED();
  1650. }
  1651. PathRedirection::~PathRedirection()
  1652. {
  1653. }
  1654. FdRedirection::~FdRedirection()
  1655. {
  1656. }
  1657. Redirection::~Redirection()
  1658. {
  1659. }
  1660. }