AST.cpp 58 KB

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