AST.cpp 61 KB

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