AST.cpp 59 KB

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