AST.cpp 47 KB

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