1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508 |
- /*
- * Copyright (c) 2020, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include "AST.h"
- #include "Shell.h"
- #include <AK/MemoryStream.h>
- #include <AK/ScopeGuard.h>
- #include <AK/ScopedValueRollback.h>
- #include <AK/String.h>
- #include <AK/StringBuilder.h>
- #include <AK/URL.h>
- #include <LibCore/EventLoop.h>
- #include <LibCore/File.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <unistd.h>
- void AK::Formatter<Shell::AST::Command>::format(FormatBuilder& builder, const Shell::AST::Command& value)
- {
- if (m_sign_mode != FormatBuilder::SignMode::Default)
- VERIFY_NOT_REACHED();
- if (m_alternative_form)
- VERIFY_NOT_REACHED();
- if (m_zero_pad)
- VERIFY_NOT_REACHED();
- if (m_mode != Mode::Default && m_mode != Mode::String)
- VERIFY_NOT_REACHED();
- if (m_width.has_value())
- VERIFY_NOT_REACHED();
- if (m_precision.has_value())
- VERIFY_NOT_REACHED();
- if (value.argv.is_empty()) {
- builder.put_literal("(ShellInternal)");
- } else {
- bool first = true;
- for (auto& arg : value.argv) {
- if (!first)
- builder.put_literal(" ");
- first = false;
- builder.put_literal(arg);
- }
- }
- for (auto& redir : value.redirections) {
- builder.put_padding(' ', 1);
- if (redir.is_path_redirection()) {
- auto path_redir = (const Shell::AST::PathRedirection*)&redir;
- builder.put_i64(path_redir->fd);
- switch (path_redir->direction) {
- case Shell::AST::PathRedirection::Read:
- builder.put_literal("<");
- break;
- case Shell::AST::PathRedirection::Write:
- builder.put_literal(">");
- break;
- case Shell::AST::PathRedirection::WriteAppend:
- builder.put_literal(">>");
- break;
- case Shell::AST::PathRedirection::ReadWrite:
- builder.put_literal("<>");
- break;
- }
- builder.put_literal(path_redir->path);
- } else if (redir.is_fd_redirection()) {
- auto* fdredir = (const Shell::AST::FdRedirection*)&redir;
- builder.put_i64(fdredir->new_fd);
- builder.put_literal(">");
- builder.put_i64(fdredir->old_fd);
- } else if (redir.is_close_redirection()) {
- auto close_redir = (const Shell::AST::CloseRedirection*)&redir;
- builder.put_i64(close_redir->fd);
- builder.put_literal(">&-");
- } else {
- VERIFY_NOT_REACHED();
- }
- }
- if (!value.next_chain.is_empty()) {
- for (auto& command : value.next_chain) {
- switch (command.action) {
- case Shell::AST::NodeWithAction::And:
- builder.put_literal(" && ");
- break;
- case Shell::AST::NodeWithAction::Or:
- builder.put_literal(" || ");
- break;
- case Shell::AST::NodeWithAction::Sequence:
- builder.put_literal("; ");
- break;
- }
- builder.put_literal("(");
- builder.put_literal(command.node->class_name());
- builder.put_literal("...)");
- }
- }
- if (!value.should_wait)
- builder.put_literal("&");
- }
- namespace Shell::AST {
- static inline void print_indented(const String& str, int indent)
- {
- dbgln("{}{}", String::repeated(' ', indent * 2), str);
- }
- static inline Optional<Position> merge_positions(const Optional<Position>& left, const Optional<Position>& right)
- {
- if (!left.has_value())
- return right;
- if (!right.has_value())
- return left;
- return Position {
- .start_offset = left->start_offset,
- .end_offset = right->end_offset,
- .start_line = left->start_line,
- .end_line = right->end_line,
- };
- }
- static inline Vector<Command> join_commands(Vector<Command> left, Vector<Command> right)
- {
- Command command;
- auto last_in_left = left.take_last();
- auto first_in_right = right.take_first();
- command.argv.append(last_in_left.argv);
- command.argv.append(first_in_right.argv);
- command.redirections.append(last_in_left.redirections);
- command.redirections.append(first_in_right.redirections);
- command.should_wait = first_in_right.should_wait && last_in_left.should_wait;
- command.is_pipe_source = first_in_right.is_pipe_source;
- command.should_notify_if_in_background = first_in_right.should_notify_if_in_background || last_in_left.should_notify_if_in_background;
- command.position = merge_positions(last_in_left.position, first_in_right.position);
- Vector<Command> commands;
- commands.append(left);
- commands.append(command);
- commands.append(right);
- return commands;
- }
- static String resolve_slices(RefPtr<Shell> shell, String&& input_value, NonnullRefPtrVector<Slice> slices)
- {
- if (slices.is_empty())
- return move(input_value);
- for (auto& slice : slices) {
- auto value = slice.run(shell);
- if (!value) {
- shell->raise_error(Shell::ShellError::InvalidSliceContentsError, "Invalid slice contents", slice.position());
- return move(input_value);
- }
- auto index_values = value->resolve_as_list(shell);
- Vector<size_t> indices;
- indices.ensure_capacity(index_values.size());
- size_t i = 0;
- for (auto& value : index_values) {
- auto maybe_index = value.to_int();
- if (!maybe_index.has_value()) {
- shell->raise_error(Shell::ShellError::InvalidSliceContentsError, String::formatted("Invalid value in slice index {}: {} (expected a number)", i, value), slice.position());
- return move(input_value);
- }
- ++i;
- auto index = maybe_index.value();
- auto original_index = index;
- if (index < 0)
- index += input_value.length();
- if (index < 0 || (size_t)index >= input_value.length()) {
- shell->raise_error(Shell::ShellError::InvalidSliceContentsError, String::formatted("Slice index {} (evaluated as {}) out of value bounds [0-{})", index, original_index, input_value.length()), slice.position());
- return move(input_value);
- }
- indices.unchecked_append(index);
- }
- StringBuilder builder { indices.size() };
- for (auto& index : indices)
- builder.append(input_value[index]);
- input_value = builder.build();
- }
- return move(input_value);
- }
- static Vector<String> resolve_slices(RefPtr<Shell> shell, Vector<String>&& values, NonnullRefPtrVector<Slice> slices)
- {
- if (slices.is_empty())
- return move(values);
- for (auto& slice : slices) {
- auto value = slice.run(shell);
- if (!value) {
- shell->raise_error(Shell::ShellError::InvalidSliceContentsError, "Invalid slice contents", slice.position());
- return move(values);
- }
- auto index_values = value->resolve_as_list(shell);
- Vector<size_t> indices;
- indices.ensure_capacity(index_values.size());
- size_t i = 0;
- for (auto& value : index_values) {
- auto maybe_index = value.to_int();
- if (!maybe_index.has_value()) {
- shell->raise_error(Shell::ShellError::InvalidSliceContentsError, String::formatted("Invalid value in slice index {}: {} (expected a number)", i, value), slice.position());
- return move(values);
- }
- ++i;
- auto index = maybe_index.value();
- auto original_index = index;
- if (index < 0)
- index += values.size();
- if (index < 0 || (size_t)index >= values.size()) {
- shell->raise_error(Shell::ShellError::InvalidSliceContentsError, String::formatted("Slice index {} (evaluated as {}) out of value bounds [0-{})", index, original_index, values.size()), slice.position());
- return move(values);
- }
- indices.unchecked_append(index);
- }
- Vector<String> result;
- result.ensure_capacity(indices.size());
- for (auto& index : indices)
- result.unchecked_append(values[index]);
- values = move(result);
- }
- return move(values);
- }
- void Node::clear_syntax_error()
- {
- m_syntax_error_node->clear_syntax_error();
- }
- void Node::set_is_syntax_error(const SyntaxError& error_node)
- {
- if (!m_syntax_error_node) {
- m_syntax_error_node = error_node;
- } else {
- m_syntax_error_node->set_is_syntax_error(error_node);
- }
- }
- bool Node::is_syntax_error() const
- {
- return m_syntax_error_node && m_syntax_error_node->is_syntax_error();
- }
- void Node::for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(NonnullRefPtr<Value>)> callback)
- {
- auto value = run(shell)->resolve_without_cast(shell);
- if (value->is_job()) {
- callback(value);
- return;
- }
- if (value->is_list_without_resolution()) {
- auto list = value->resolve_without_cast(shell);
- for (auto& element : static_cast<ListValue*>(list.ptr())->values()) {
- if (callback(element) == IterationDecision::Break)
- break;
- }
- return;
- }
- auto list = value->resolve_as_list(shell);
- for (auto& element : list) {
- if (callback(create<StringValue>(move(element))) == IterationDecision::Break)
- break;
- }
- }
- Vector<Command> Node::to_lazy_evaluated_commands(RefPtr<Shell> shell)
- {
- if (would_execute()) {
- // Wrap the node in a "should immediately execute next" command.
- return {
- Command { {}, {}, true, false, true, true, {}, { NodeWithAction(*this, NodeWithAction::Sequence) }, position() }
- };
- }
- return run(shell)->resolve_as_commands(shell);
- }
- void Node::dump(int level) const
- {
- print_indented(String::formatted("{} at {}:{} (from {}.{} to {}.{})",
- class_name().characters(),
- m_position.start_offset,
- m_position.end_offset,
- m_position.start_line.line_number,
- m_position.start_line.line_column,
- m_position.end_line.line_number,
- m_position.end_line.line_column),
- level);
- }
- Node::Node(Position position)
- : m_position(position)
- {
- }
- Vector<Line::CompletionSuggestion> Node::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
- {
- auto matching_node = hit_test_result.matching_node;
- if (matching_node) {
- if (matching_node->is_bareword()) {
- auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
- auto corrected_offset = find_offset_into_node(node->text(), offset - matching_node->position().start_offset);
- if (corrected_offset > node->text().length())
- return {};
- auto& text = node->text();
- // If the literal isn't an option, treat it as a path.
- if (!(text.starts_with("-") || text == "--" || text == "-"))
- return shell.complete_path("", text, corrected_offset, Shell::ExecutableOnly::No);
- // If the literal is an option, we have to know the program name
- // should we have no way to get that, bail early.
- if (!hit_test_result.closest_command_node)
- return {};
- auto program_name_node = hit_test_result.closest_command_node->leftmost_trivial_literal();
- if (!program_name_node)
- return {};
- String program_name;
- if (program_name_node->is_bareword())
- program_name = static_cast<BarewordLiteral*>(program_name_node.ptr())->text();
- else
- program_name = static_cast<StringLiteral*>(program_name_node.ptr())->text();
- return shell.complete_option(program_name, text, corrected_offset);
- }
- return {};
- }
- auto result = hit_test_position(offset);
- if (!result.matching_node)
- return {};
- auto node = result.matching_node;
- if (node->is_bareword() || node != result.closest_node_with_semantic_meaning)
- node = result.closest_node_with_semantic_meaning;
- if (!node)
- return {};
- return node->complete_for_editor(shell, offset, result);
- }
- Vector<Line::CompletionSuggestion> Node::complete_for_editor(Shell& shell, size_t offset)
- {
- return Node::complete_for_editor(shell, offset, { nullptr, nullptr, nullptr });
- }
- Node::~Node()
- {
- }
- void And::dump(int level) const
- {
- Node::dump(level);
- m_left->dump(level + 1);
- m_right->dump(level + 1);
- }
- RefPtr<Value> And::run(RefPtr<Shell> shell)
- {
- auto commands = m_left->to_lazy_evaluated_commands(shell);
- commands.last().next_chain.append(NodeWithAction { *m_right, NodeWithAction::And });
- return create<CommandSequenceValue>(move(commands));
- }
- void And::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- metadata.is_first_in_list = true;
- m_left->highlight_in_editor(editor, shell, metadata);
- m_right->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult And::hit_test_position(size_t offset) const
- {
- auto result = m_left->hit_test_position(offset);
- if (result.matching_node) {
- if (!result.closest_command_node)
- result.closest_command_node = m_right;
- return result;
- }
- result = m_right->hit_test_position(offset);
- if (!result.closest_command_node)
- result.closest_command_node = m_right;
- return result;
- }
- And::And(Position position, NonnullRefPtr<Node> left, NonnullRefPtr<Node> right, Position and_position)
- : Node(move(position))
- , m_left(move(left))
- , m_right(move(right))
- , m_and_position(and_position)
- {
- if (m_left->is_syntax_error())
- set_is_syntax_error(m_left->syntax_error_node());
- else if (m_right->is_syntax_error())
- set_is_syntax_error(m_right->syntax_error_node());
- }
- And::~And()
- {
- }
- void ListConcatenate::dump(int level) const
- {
- Node::dump(level);
- for (auto& element : m_list)
- element->dump(level + 1);
- }
- RefPtr<Value> ListConcatenate::run(RefPtr<Shell> shell)
- {
- RefPtr<Value> result = nullptr;
- for (auto& element : m_list) {
- if (!result) {
- result = create<ListValue>({ element->run(shell)->resolve_without_cast(shell) });
- continue;
- }
- auto element_value = element->run(shell)->resolve_without_cast(shell);
- if (result->is_command() || element_value->is_command()) {
- auto joined_commands = join_commands(result->resolve_as_commands(shell), element_value->resolve_as_commands(shell));
- if (joined_commands.size() == 1) {
- auto& command = joined_commands[0];
- command.position = position();
- result = create<CommandValue>(command);
- } else {
- result = create<CommandSequenceValue>(move(joined_commands));
- }
- } else {
- NonnullRefPtrVector<Value> values;
- if (result->is_list_without_resolution()) {
- values.append(static_cast<ListValue*>(result.ptr())->values());
- } else {
- for (auto& result : result->resolve_as_list(shell))
- values.append(create<StringValue>(result));
- }
- values.append(element_value);
- result = create<ListValue>(move(values));
- }
- }
- if (!result)
- return create<ListValue>({});
- return result;
- }
- void ListConcatenate::for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(NonnullRefPtr<Value>)> callback)
- {
- for (auto& entry : m_list) {
- auto value = entry->run(shell);
- if (!value)
- continue;
- if (callback(value.release_nonnull()) == IterationDecision::Break)
- break;
- }
- }
- void ListConcatenate::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- auto first = metadata.is_first_in_list;
- metadata.is_first_in_list = false;
- metadata.is_first_in_list = first;
- for (auto& element : m_list) {
- element->highlight_in_editor(editor, shell, metadata);
- metadata.is_first_in_list = false;
- }
- }
- HitTestResult ListConcatenate::hit_test_position(size_t offset) const
- {
- bool first = true;
- for (auto& element : m_list) {
- auto result = element->hit_test_position(offset);
- if (!result.closest_node_with_semantic_meaning && !first)
- result.closest_node_with_semantic_meaning = this;
- if (result.matching_node)
- return result;
- first = false;
- }
- return {};
- }
- RefPtr<Node> ListConcatenate::leftmost_trivial_literal() const
- {
- if (m_list.is_empty())
- return nullptr;
- return m_list.first()->leftmost_trivial_literal();
- }
- ListConcatenate::ListConcatenate(Position position, Vector<NonnullRefPtr<Node>> list)
- : Node(move(position))
- , m_list(move(list))
- {
- for (auto& element : m_list) {
- if (element->is_syntax_error()) {
- set_is_syntax_error(element->syntax_error_node());
- break;
- }
- }
- }
- ListConcatenate::~ListConcatenate()
- {
- }
- void Background::dump(int level) const
- {
- Node::dump(level);
- m_command->dump(level + 1);
- }
- RefPtr<Value> Background::run(RefPtr<Shell> shell)
- {
- auto commands = m_command->to_lazy_evaluated_commands(shell);
- for (auto& command : commands)
- command.should_wait = false;
- return create<CommandSequenceValue>(move(commands));
- }
- void Background::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- m_command->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult Background::hit_test_position(size_t offset) const
- {
- return m_command->hit_test_position(offset);
- }
- Background::Background(Position position, NonnullRefPtr<Node> command)
- : Node(move(position))
- , m_command(move(command))
- {
- if (m_command->is_syntax_error())
- set_is_syntax_error(m_command->syntax_error_node());
- }
- Background::~Background()
- {
- }
- void BarewordLiteral::dump(int level) const
- {
- Node::dump(level);
- print_indented(m_text, level + 1);
- }
- RefPtr<Value> BarewordLiteral::run(RefPtr<Shell>)
- {
- return create<StringValue>(m_text);
- }
- void BarewordLiteral::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- if (metadata.is_first_in_list) {
- if (shell.is_runnable(m_text)) {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Bold });
- } else {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Red) });
- }
- return;
- }
- if (m_text.starts_with('-')) {
- if (m_text == "--") {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Green) });
- return;
- }
- if (m_text == "-")
- return;
- if (m_text.starts_with("--")) {
- auto index = m_text.index_of("=").value_or(m_text.length() - 1) + 1;
- editor.stylize({ m_position.start_offset, m_position.start_offset + index }, { Line::Style::Foreground(Line::Style::XtermColor::Cyan) });
- } else {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Cyan) });
- }
- }
- if (Core::File::exists(m_text)) {
- auto realpath = shell.resolve_path(m_text);
- auto url = URL::create_with_file_protocol(realpath);
- url.set_host(shell.hostname);
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Hyperlink(url.to_string()) });
- }
- }
- BarewordLiteral::BarewordLiteral(Position position, String text)
- : Node(move(position))
- , m_text(move(text))
- {
- }
- BarewordLiteral::~BarewordLiteral()
- {
- }
- void BraceExpansion::dump(int level) const
- {
- Node::dump(level);
- for (auto& entry : m_entries)
- entry.dump(level + 1);
- }
- RefPtr<Value> BraceExpansion::run(RefPtr<Shell> shell)
- {
- NonnullRefPtrVector<Value> values;
- for (auto& entry : m_entries) {
- auto value = entry.run(shell);
- if (value)
- values.append(value.release_nonnull());
- }
- return create<ListValue>(move(values));
- }
- HitTestResult BraceExpansion::hit_test_position(size_t offset) const
- {
- for (auto& entry : m_entries) {
- auto result = entry.hit_test_position(offset);
- if (result.matching_node) {
- if (!result.closest_command_node)
- result.closest_command_node = &entry;
- return result;
- }
- }
- return {};
- }
- void BraceExpansion::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- for (auto& entry : m_entries) {
- entry.highlight_in_editor(editor, shell, metadata);
- metadata.is_first_in_list = false;
- }
- }
- BraceExpansion::BraceExpansion(Position position, NonnullRefPtrVector<Node> entries)
- : Node(move(position))
- , m_entries(move(entries))
- {
- for (auto& entry : m_entries) {
- if (entry.is_syntax_error()) {
- set_is_syntax_error(entry.syntax_error_node());
- break;
- }
- }
- }
- BraceExpansion::~BraceExpansion()
- {
- }
- void CastToCommand::dump(int level) const
- {
- Node::dump(level);
- m_inner->dump(level + 1);
- }
- RefPtr<Value> CastToCommand::run(RefPtr<Shell> shell)
- {
- if (m_inner->is_command())
- return m_inner->run(shell);
- auto value = m_inner->run(shell)->resolve_without_cast(shell);
- if (value->is_command())
- return value;
- auto argv = value->resolve_as_list(shell);
- return create<CommandValue>(move(argv), position());
- }
- void CastToCommand::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- m_inner->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult CastToCommand::hit_test_position(size_t offset) const
- {
- auto result = m_inner->hit_test_position(offset);
- if (!result.closest_node_with_semantic_meaning)
- result.closest_node_with_semantic_meaning = this;
- return result;
- }
- Vector<Line::CompletionSuggestion> CastToCommand::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
- {
- auto matching_node = hit_test_result.matching_node;
- if (!matching_node || !matching_node->is_bareword())
- return {};
- auto corrected_offset = offset - matching_node->position().start_offset;
- auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
- if (corrected_offset > node->text().length())
- return {};
- return shell.complete_program_name(node->text(), corrected_offset);
- }
- RefPtr<Node> CastToCommand::leftmost_trivial_literal() const
- {
- return m_inner->leftmost_trivial_literal();
- }
- CastToCommand::CastToCommand(Position position, NonnullRefPtr<Node> inner)
- : Node(move(position))
- , m_inner(move(inner))
- {
- if (m_inner->is_syntax_error())
- set_is_syntax_error(m_inner->syntax_error_node());
- }
- CastToCommand::~CastToCommand()
- {
- }
- void CastToList::dump(int level) const
- {
- Node::dump(level);
- if (m_inner)
- m_inner->dump(level + 1);
- else
- print_indented("(empty)", level + 1);
- }
- RefPtr<Value> CastToList::run(RefPtr<Shell> shell)
- {
- if (!m_inner)
- return create<ListValue>({});
- auto inner_value = m_inner->run(shell)->resolve_without_cast(shell);
- if (inner_value->is_command() || inner_value->is_list())
- return inner_value;
- auto values = inner_value->resolve_as_list(shell);
- NonnullRefPtrVector<Value> cast_values;
- for (auto& value : values)
- cast_values.append(create<StringValue>(value));
- return create<ListValue>(cast_values);
- }
- void CastToList::for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(NonnullRefPtr<Value>)> callback)
- {
- if (m_inner)
- m_inner->for_each_entry(shell, move(callback));
- }
- void CastToList::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- if (m_inner)
- m_inner->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult CastToList::hit_test_position(size_t offset) const
- {
- if (!m_inner)
- return {};
- return m_inner->hit_test_position(offset);
- }
- RefPtr<Node> CastToList::leftmost_trivial_literal() const
- {
- return m_inner->leftmost_trivial_literal();
- }
- CastToList::CastToList(Position position, RefPtr<Node> inner)
- : Node(move(position))
- , m_inner(move(inner))
- {
- if (m_inner && m_inner->is_syntax_error())
- set_is_syntax_error(m_inner->syntax_error_node());
- }
- CastToList::~CastToList()
- {
- }
- void CloseFdRedirection::dump(int level) const
- {
- Node::dump(level);
- print_indented(String::formatted("{} -> Close", m_fd), level);
- }
- RefPtr<Value> CloseFdRedirection::run(RefPtr<Shell>)
- {
- Command command;
- command.position = position();
- command.redirections.append(adopt_ref(*new CloseRedirection(m_fd)));
- return create<CommandValue>(move(command));
- }
- void CloseFdRedirection::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
- {
- editor.stylize({ m_position.start_offset, m_position.end_offset - 1 }, { Line::Style::Foreground(0x87, 0x9b, 0xcd) }); // 25% Darkened Periwinkle
- editor.stylize({ m_position.end_offset - 1, m_position.end_offset }, { Line::Style::Foreground(0xff, 0x7e, 0x00) }); // Amber
- }
- CloseFdRedirection::CloseFdRedirection(Position position, int fd)
- : Node(move(position))
- , m_fd(fd)
- {
- }
- CloseFdRedirection::~CloseFdRedirection()
- {
- }
- void CommandLiteral::dump(int level) const
- {
- Node::dump(level);
- print_indented("(Generated command literal)", level + 1);
- }
- RefPtr<Value> CommandLiteral::run(RefPtr<Shell>)
- {
- return create<CommandValue>(m_command);
- }
- CommandLiteral::CommandLiteral(Position position, Command command)
- : Node(move(position))
- , m_command(move(command))
- {
- }
- CommandLiteral::~CommandLiteral()
- {
- }
- void Comment::dump(int level) const
- {
- Node::dump(level);
- print_indented(m_text, level + 1);
- }
- RefPtr<Value> Comment::run(RefPtr<Shell>)
- {
- return create<ListValue>({});
- }
- void Comment::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
- {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(150, 150, 150) }); // Light gray
- }
- Comment::Comment(Position position, String text)
- : Node(move(position))
- , m_text(move(text))
- {
- }
- Comment::~Comment()
- {
- }
- void ContinuationControl::dump(int level) const
- {
- Node::dump(level);
- print_indented(m_kind == Continue ? "(Continue)" : "(Break)", level + 1);
- }
- RefPtr<Value> ContinuationControl::run(RefPtr<Shell> shell)
- {
- if (m_kind == Break)
- shell->raise_error(Shell::ShellError::InternalControlFlowBreak, {}, position());
- else if (m_kind == Continue)
- shell->raise_error(Shell::ShellError::InternalControlFlowContinue, {}, position());
- else
- VERIFY_NOT_REACHED();
- return create<ListValue>({});
- }
- void ContinuationControl::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
- {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- }
- void DoubleQuotedString::dump(int level) const
- {
- Node::dump(level);
- m_inner->dump(level + 1);
- }
- RefPtr<Value> DoubleQuotedString::run(RefPtr<Shell> shell)
- {
- StringBuilder builder;
- auto values = m_inner->run(shell)->resolve_as_list(shell);
- builder.join("", values);
- return create<StringValue>(builder.to_string());
- }
- void DoubleQuotedString::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- Line::Style style { Line::Style::Foreground(Line::Style::XtermColor::Yellow) };
- if (metadata.is_first_in_list)
- style.unify_with({ Line::Style::Bold });
- editor.stylize({ m_position.start_offset, m_position.end_offset }, style);
- metadata.is_first_in_list = false;
- m_inner->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult DoubleQuotedString::hit_test_position(size_t offset) const
- {
- return m_inner->hit_test_position(offset);
- }
- DoubleQuotedString::DoubleQuotedString(Position position, RefPtr<Node> inner)
- : Node(move(position))
- , m_inner(move(inner))
- {
- if (m_inner->is_syntax_error())
- set_is_syntax_error(m_inner->syntax_error_node());
- }
- DoubleQuotedString::~DoubleQuotedString()
- {
- }
- void DynamicEvaluate::dump(int level) const
- {
- Node::dump(level);
- m_inner->dump(level + 1);
- }
- RefPtr<Value> DynamicEvaluate::run(RefPtr<Shell> shell)
- {
- auto result = m_inner->run(shell)->resolve_without_cast(shell);
- // Dynamic Evaluation behaves differently between strings and lists.
- // Strings are treated as variables, and Lists are treated as commands.
- if (result->is_string()) {
- auto name_part = result->resolve_as_list(shell);
- VERIFY(name_part.size() == 1);
- return create<SimpleVariableValue>(name_part[0]);
- }
- // If it's anything else, we're just gonna cast it to a list.
- auto list = result->resolve_as_list(shell);
- return create<CommandValue>(move(list), position());
- }
- void DynamicEvaluate::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- m_inner->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult DynamicEvaluate::hit_test_position(size_t offset) const
- {
- return m_inner->hit_test_position(offset);
- }
- DynamicEvaluate::DynamicEvaluate(Position position, NonnullRefPtr<Node> inner)
- : Node(move(position))
- , m_inner(move(inner))
- {
- if (m_inner->is_syntax_error())
- set_is_syntax_error(m_inner->syntax_error_node());
- }
- DynamicEvaluate::~DynamicEvaluate()
- {
- }
- void Fd2FdRedirection::dump(int level) const
- {
- Node::dump(level);
- print_indented(String::formatted("{} -> {}", m_old_fd, m_new_fd), level);
- }
- RefPtr<Value> Fd2FdRedirection::run(RefPtr<Shell>)
- {
- Command command;
- command.position = position();
- command.redirections.append(FdRedirection::create(m_new_fd, m_old_fd, Rewiring::Close::None));
- return create<CommandValue>(move(command));
- }
- void Fd2FdRedirection::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
- {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(0x87, 0x9b, 0xcd) }); // 25% Darkened Periwinkle
- }
- Fd2FdRedirection::Fd2FdRedirection(Position position, int src, int dst)
- : Node(move(position))
- , m_old_fd(src)
- , m_new_fd(dst)
- {
- }
- Fd2FdRedirection::~Fd2FdRedirection()
- {
- }
- void FunctionDeclaration::dump(int level) const
- {
- Node::dump(level);
- print_indented(String::formatted("(name: {})\n", m_name.name), level + 1);
- print_indented("(argument names)", level + 1);
- for (auto& arg : m_arguments)
- print_indented(String::formatted("(name: {})\n", arg.name), level + 2);
- print_indented("(body)", level + 1);
- if (m_block)
- m_block->dump(level + 2);
- else
- print_indented("(null)", level + 2);
- }
- RefPtr<Value> FunctionDeclaration::run(RefPtr<Shell> shell)
- {
- Vector<String> args;
- for (auto& arg : m_arguments)
- args.append(arg.name);
- shell->define_function(m_name.name, move(args), m_block);
- return create<ListValue>({});
- }
- void FunctionDeclaration::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- editor.stylize({ m_name.position.start_offset, m_name.position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Blue) });
- for (auto& arg : m_arguments)
- editor.stylize({ arg.position.start_offset, arg.position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Blue), Line::Style::Italic });
- metadata.is_first_in_list = true;
- if (m_block)
- m_block->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult FunctionDeclaration::hit_test_position(size_t offset) const
- {
- if (!m_block)
- return {};
- auto result = m_block->hit_test_position(offset);
- if (result.matching_node && result.matching_node->is_simple_variable())
- result.closest_node_with_semantic_meaning = this;
- return result;
- }
- Vector<Line::CompletionSuggestion> FunctionDeclaration::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
- {
- auto matching_node = hit_test_result.matching_node;
- if (!matching_node)
- return {};
- if (!matching_node->is_simple_variable())
- return matching_node->complete_for_editor(shell, offset, hit_test_result);
- auto corrected_offset = offset - matching_node->position().start_offset - 1; // Skip the first '$'
- auto* node = static_cast<SimpleVariable*>(matching_node.ptr());
- auto name = node->name().substring_view(0, corrected_offset);
- Vector<Line::CompletionSuggestion> results;
- for (auto& arg : m_arguments) {
- if (arg.name.starts_with(name))
- results.append(arg.name);
- }
- results.append(matching_node->complete_for_editor(shell, offset, hit_test_result));
- return results;
- }
- FunctionDeclaration::FunctionDeclaration(Position position, NameWithPosition name, Vector<NameWithPosition> arguments, RefPtr<AST::Node> body)
- : Node(move(position))
- , m_name(move(name))
- , m_arguments(arguments)
- , m_block(move(body))
- {
- if (m_block && m_block->is_syntax_error())
- set_is_syntax_error(m_block->syntax_error_node());
- }
- FunctionDeclaration::~FunctionDeclaration()
- {
- }
- void ForLoop::dump(int level) const
- {
- Node::dump(level);
- if (m_variable.has_value())
- print_indented(String::formatted("iterating with {} in", m_variable->name), level + 1);
- if (m_index_variable.has_value())
- print_indented(String::formatted("with index name {} in", m_index_variable->name), level + 1);
- if (m_iterated_expression)
- m_iterated_expression->dump(level + 2);
- else
- print_indented("(ever)", level + 2);
- print_indented("Running", level + 1);
- if (m_block)
- m_block->dump(level + 2);
- else
- print_indented("(null)", level + 2);
- }
- RefPtr<Value> ForLoop::run(RefPtr<Shell> shell)
- {
- if (!m_block)
- return create<ListValue>({});
- size_t consecutive_interruptions = 0;
- auto run = [&](auto& block_value) {
- if (shell->has_error(Shell::ShellError::InternalControlFlowBreak)) {
- shell->take_error();
- return IterationDecision::Break;
- }
- if (shell->has_error(Shell::ShellError::InternalControlFlowContinue)) {
- shell->take_error();
- return IterationDecision::Continue;
- }
- if (!shell->has_error(Shell::ShellError::None))
- return IterationDecision::Break;
- if (block_value->is_job()) {
- auto job = static_cast<JobValue*>(block_value.ptr())->job();
- if (!job || job->is_running_in_background())
- return IterationDecision::Continue;
- shell->block_on_job(job);
- if (job->signaled()) {
- if (job->termination_signal() == SIGINT)
- ++consecutive_interruptions;
- else
- return IterationDecision::Break;
- } else {
- consecutive_interruptions = 0;
- }
- }
- return IterationDecision::Continue;
- };
- if (m_iterated_expression) {
- auto variable_name = m_variable.has_value() ? m_variable->name : "it";
- Optional<StringView> index_name = m_index_variable.has_value() ? Optional<StringView>(m_index_variable->name) : Optional<StringView>();
- size_t i = 0;
- m_iterated_expression->for_each_entry(shell, [&](auto value) {
- if (consecutive_interruptions == 2)
- return IterationDecision::Break;
- RefPtr<Value> block_value;
- {
- auto frame = shell->push_frame(String::formatted("for ({})", this));
- shell->set_local_variable(variable_name, value, true);
- if (index_name.has_value())
- shell->set_local_variable(index_name.value(), create<AST::StringValue>(String::number(i)), true);
- ++i;
- block_value = m_block->run(shell);
- }
- return run(block_value);
- });
- } else {
- for (;;) {
- if (consecutive_interruptions == 2)
- break;
- RefPtr<Value> block_value = m_block->run(shell);
- if (run(block_value) == IterationDecision::Break)
- break;
- }
- }
- return create<ListValue>({});
- }
- void ForLoop::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- auto is_loop = m_iterated_expression.is_null();
- editor.stylize({ m_position.start_offset, m_position.start_offset + (is_loop ? 4 : 3) }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- if (!is_loop) {
- if (m_in_kw_position.has_value())
- editor.stylize({ m_in_kw_position.value().start_offset, m_in_kw_position.value().end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- if (m_index_kw_position.has_value())
- editor.stylize({ m_index_kw_position.value().start_offset, m_index_kw_position.value().end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- metadata.is_first_in_list = false;
- m_iterated_expression->highlight_in_editor(editor, shell, metadata);
- }
- if (m_index_variable.has_value())
- editor.stylize({ m_index_variable->position.start_offset, m_index_variable->position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Blue), Line::Style::Italic });
- if (m_variable.has_value())
- editor.stylize({ m_variable->position.start_offset, m_variable->position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Blue), Line::Style::Italic });
- metadata.is_first_in_list = true;
- if (m_block)
- m_block->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult ForLoop::hit_test_position(size_t offset) const
- {
- if (m_iterated_expression) {
- if (auto result = m_iterated_expression->hit_test_position(offset); result.matching_node)
- return result;
- }
- if (!m_block)
- return {};
- return m_block->hit_test_position(offset);
- }
- ForLoop::ForLoop(Position position, Optional<NameWithPosition> variable, Optional<NameWithPosition> index_variable, RefPtr<AST::Node> iterated_expr, RefPtr<AST::Node> block, Optional<Position> in_kw_position, Optional<Position> index_kw_position)
- : Node(move(position))
- , m_variable(move(variable))
- , m_index_variable(move(index_variable))
- , m_iterated_expression(move(iterated_expr))
- , m_block(move(block))
- , m_in_kw_position(move(in_kw_position))
- , m_index_kw_position(move(index_kw_position))
- {
- if (m_iterated_expression && m_iterated_expression->is_syntax_error())
- set_is_syntax_error(m_iterated_expression->syntax_error_node());
- else if (m_block && m_block->is_syntax_error())
- set_is_syntax_error(m_block->syntax_error_node());
- }
- ForLoop::~ForLoop()
- {
- }
- void Glob::dump(int level) const
- {
- Node::dump(level);
- print_indented(m_text, level + 1);
- }
- RefPtr<Value> Glob::run(RefPtr<Shell>)
- {
- return create<GlobValue>(m_text, position());
- }
- void Glob::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata metadata)
- {
- Line::Style style { Line::Style::Foreground(Line::Style::XtermColor::Cyan) };
- if (metadata.is_first_in_list)
- style.unify_with({ Line::Style::Bold });
- editor.stylize({ m_position.start_offset, m_position.end_offset }, move(style));
- }
- Glob::Glob(Position position, String text)
- : Node(move(position))
- , m_text(move(text))
- {
- }
- Glob::~Glob()
- {
- }
- void Heredoc::dump(int level) const
- {
- Node::dump(level);
- print_indented("(End Key)", level + 1);
- print_indented(m_end, level + 2);
- print_indented("(Allows Interpolation)", level + 1);
- print_indented(String::formatted("{}", m_allows_interpolation), level + 2);
- print_indented("(Contents)", level + 1);
- if (m_contents)
- m_contents->dump(level + 2);
- else
- print_indented("(null)", level + 2);
- }
- RefPtr<Value> Heredoc::run(RefPtr<Shell> shell)
- {
- if (!m_deindent)
- return m_contents->run(shell);
- // To deindent, first split to lines...
- auto value = m_contents->run(shell);
- if (!value)
- return value;
- auto list = value->resolve_as_list(shell);
- // The list better have one entry, otherwise we've put the wrong kind of node inside this heredoc
- VERIFY(list.size() == 1);
- auto lines = list.first().split_view('\n');
- // Now just trim each line and put them back in a string
- StringBuilder builder { list.first().length() };
- for (auto& line : lines) {
- builder.append(line.trim_whitespace(TrimMode::Left));
- builder.append('\n');
- }
- return create<StringValue>(builder.to_string());
- }
- void Heredoc::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- Line::Style content_style { Line::Style::Foreground(Line::Style::XtermColor::Yellow) };
- if (metadata.is_first_in_list)
- content_style.unify_with({ Line::Style::Bold });
- if (!m_contents)
- content_style.unify_with({ Line::Style::Foreground(Line::Style::XtermColor::Red) }, true);
- editor.stylize({ m_position.start_offset, m_position.end_offset }, content_style);
- if (m_contents)
- m_contents->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult Heredoc::hit_test_position(size_t offset) const
- {
- if (!m_contents)
- return {};
- return m_contents->hit_test_position(offset);
- }
- Heredoc::Heredoc(Position position, String end, bool allow_interpolation, bool deindent)
- : Node(move(position))
- , m_end(move(end))
- , m_allows_interpolation(allow_interpolation)
- , m_deindent(deindent)
- {
- }
- Heredoc::~Heredoc()
- {
- }
- void HistoryEvent::dump(int level) const
- {
- Node::dump(level);
- print_indented("Event Selector", level + 1);
- switch (m_selector.event.kind) {
- case HistorySelector::EventKind::IndexFromStart:
- print_indented("IndexFromStart", level + 2);
- break;
- case HistorySelector::EventKind::IndexFromEnd:
- print_indented("IndexFromEnd", level + 2);
- break;
- case HistorySelector::EventKind::ContainingStringLookup:
- print_indented("ContainingStringLookup", level + 2);
- break;
- case HistorySelector::EventKind::StartingStringLookup:
- print_indented("StartingStringLookup", level + 2);
- break;
- }
- print_indented(String::formatted("{}({})", m_selector.event.index, m_selector.event.text), level + 3);
- print_indented("Word Selector", level + 1);
- auto print_word_selector = [&](const HistorySelector::WordSelector& selector) {
- switch (selector.kind) {
- case HistorySelector::WordSelectorKind::Index:
- print_indented(String::formatted("Index {}", selector.selector), level + 3);
- break;
- case HistorySelector::WordSelectorKind::Last:
- print_indented(String::formatted("Last"), level + 3);
- break;
- }
- };
- if (m_selector.word_selector_range.end.has_value()) {
- print_indented("Range Start", level + 2);
- print_word_selector(m_selector.word_selector_range.start);
- print_indented("Range End", level + 2);
- print_word_selector(m_selector.word_selector_range.end.value());
- } else {
- print_indented("Direct Address", level + 2);
- print_word_selector(m_selector.word_selector_range.start);
- }
- }
- RefPtr<Value> HistoryEvent::run(RefPtr<Shell> shell)
- {
- if (!shell)
- return create<AST::ListValue>({});
- auto editor = shell->editor();
- if (!editor) {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "No history available!", position());
- return create<AST::ListValue>({});
- }
- auto& history = editor->history();
- // FIXME: Implement reverse iterators and find()?
- auto find_reverse = [](auto it_start, auto it_end, auto finder) {
- auto it = it_end;
- while (it != it_start) {
- --it;
- if (finder(*it))
- return it;
- }
- return it_end;
- };
- // First, resolve the event itself.
- String resolved_history;
- switch (m_selector.event.kind) {
- case HistorySelector::EventKind::IndexFromStart:
- if (m_selector.event.index >= history.size()) {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "History event index out of bounds", m_selector.event.text_position);
- return create<AST::ListValue>({});
- }
- resolved_history = history[m_selector.event.index].entry;
- break;
- case HistorySelector::EventKind::IndexFromEnd:
- if (m_selector.event.index >= history.size()) {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "History event index out of bounds", m_selector.event.text_position);
- return create<AST::ListValue>({});
- }
- resolved_history = history[history.size() - m_selector.event.index - 1].entry;
- break;
- case HistorySelector::EventKind::ContainingStringLookup: {
- auto it = find_reverse(history.begin(), history.end(), [&](auto& entry) { return entry.entry.contains(m_selector.event.text); });
- if (it.is_end()) {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "History event did not match any entry", m_selector.event.text_position);
- return create<AST::ListValue>({});
- }
- resolved_history = it->entry;
- break;
- }
- case HistorySelector::EventKind::StartingStringLookup: {
- auto it = find_reverse(history.begin(), history.end(), [&](auto& entry) { return entry.entry.starts_with(m_selector.event.text); });
- if (it.is_end()) {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "History event did not match any entry", m_selector.event.text_position);
- return create<AST::ListValue>({});
- }
- resolved_history = it->entry;
- break;
- }
- }
- // Then, split it up to "words".
- auto nodes = Parser { resolved_history }.parse_as_multiple_expressions();
- // Now take the "words" as described by the word selectors.
- bool is_range = m_selector.word_selector_range.end.has_value();
- if (is_range) {
- auto start_index = m_selector.word_selector_range.start.resolve(nodes.size());
- auto end_index = m_selector.word_selector_range.end->resolve(nodes.size());
- if (start_index >= nodes.size()) {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "History word index out of bounds", m_selector.word_selector_range.start.position);
- return create<AST::ListValue>({});
- }
- if (end_index >= nodes.size()) {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "History word index out of bounds", m_selector.word_selector_range.end->position);
- return create<AST::ListValue>({});
- }
- decltype(nodes) resolved_nodes;
- resolved_nodes.append(nodes.data() + start_index, end_index - start_index + 1);
- NonnullRefPtr<AST::Node> list = create<AST::ListConcatenate>(position(), move(resolved_nodes));
- return list->run(shell);
- }
- auto index = m_selector.word_selector_range.start.resolve(nodes.size());
- if (index >= nodes.size()) {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "History word index out of bounds", m_selector.word_selector_range.start.position);
- return create<AST::ListValue>({});
- }
- return nodes[index].run(shell);
- }
- void HistoryEvent::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata metadata)
- {
- Line::Style style { Line::Style::Foreground(Line::Style::XtermColor::Green) };
- if (metadata.is_first_in_list)
- style.unify_with({ Line::Style::Bold });
- editor.stylize({ m_position.start_offset, m_position.end_offset }, move(style));
- }
- HistoryEvent::HistoryEvent(Position position, HistorySelector selector)
- : Node(move(position))
- , m_selector(move(selector))
- {
- if (m_selector.word_selector_range.start.syntax_error_node)
- set_is_syntax_error(*m_selector.word_selector_range.start.syntax_error_node);
- else if (m_selector.word_selector_range.end.has_value() && m_selector.word_selector_range.end->syntax_error_node)
- set_is_syntax_error(*m_selector.word_selector_range.end->syntax_error_node);
- }
- HistoryEvent::~HistoryEvent()
- {
- }
- void Execute::dump(int level) const
- {
- Node::dump(level);
- if (m_capture_stdout)
- print_indented("(Capturing stdout)", level + 1);
- m_command->dump(level + 1);
- }
- void Execute::for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(NonnullRefPtr<Value>)> callback)
- {
- if (m_command->would_execute())
- return m_command->for_each_entry(shell, move(callback));
- auto commands = shell->expand_aliases(m_command->run(shell)->resolve_as_commands(shell));
- if (m_capture_stdout) {
- int pipefd[2];
- int rc = pipe(pipefd);
- if (rc < 0) {
- dbgln("Error: cannot pipe(): {}", strerror(errno));
- return;
- }
- auto& last_in_commands = commands.last();
- last_in_commands.redirections.prepend(FdRedirection::create(pipefd[1], STDOUT_FILENO, Rewiring::Close::Old));
- last_in_commands.should_wait = false;
- last_in_commands.should_notify_if_in_background = false;
- last_in_commands.is_pipe_source = false;
- Core::EventLoop loop;
- auto notifier = Core::Notifier::construct(pipefd[0], Core::Notifier::Read);
- DuplexMemoryStream stream;
- enum {
- Continue,
- Break,
- NothingLeft,
- };
- auto check_and_call = [&] {
- auto ifs = shell->local_variable_or("IFS", "\n");
- if (auto offset = stream.offset_of(ifs.bytes()); offset.has_value()) {
- auto line_end = offset.value();
- if (line_end == 0) {
- auto rc = stream.discard_or_error(ifs.length());
- VERIFY(rc);
- if (shell->options.inline_exec_keep_empty_segments)
- if (callback(create<StringValue>("")) == IterationDecision::Break) {
- loop.quit(Break);
- notifier->set_enabled(false);
- return Break;
- }
- } else {
- auto entry = ByteBuffer::create_uninitialized(line_end + ifs.length());
- auto rc = stream.read_or_error(entry);
- VERIFY(rc);
- auto str = StringView(entry.data(), entry.size() - ifs.length());
- if (callback(create<StringValue>(str)) == IterationDecision::Break) {
- loop.quit(Break);
- notifier->set_enabled(false);
- return Break;
- }
- }
- return Continue;
- }
- return NothingLeft;
- };
- notifier->on_ready_to_read = [&] {
- constexpr static auto buffer_size = 16;
- u8 buffer[buffer_size];
- size_t remaining_size = buffer_size;
- for (;;) {
- notifier->set_event_mask(Core::Notifier::None);
- bool should_enable_notifier = false;
- ScopeGuard notifier_enabler { [&] {
- if (should_enable_notifier)
- notifier->set_event_mask(Core::Notifier::Read);
- } };
- if (check_and_call() == Break) {
- loop.quit(Break);
- return;
- }
- auto read_size = read(pipefd[0], buffer, remaining_size);
- if (read_size < 0) {
- int saved_errno = errno;
- if (saved_errno == EINTR) {
- should_enable_notifier = true;
- continue;
- }
- if (saved_errno == 0)
- continue;
- dbgln("read() failed: {}", strerror(saved_errno));
- break;
- }
- if (read_size == 0)
- break;
- should_enable_notifier = true;
- stream.write({ buffer, (size_t)read_size });
- }
- loop.quit(NothingLeft);
- };
- auto jobs = shell->run_commands(commands);
- ScopeGuard kill_jobs_if_around { [&] {
- for (auto& job : jobs) {
- if (job.is_running_in_background() && !job.exited() && !job.signaled()) {
- job.set_should_announce_signal(false); // We're explicitly killing it here.
- shell->kill_job(&job, SIGTERM);
- }
- }
- } };
- auto exit_reason = loop.exec();
- notifier->on_ready_to_read = nullptr;
- if (close(pipefd[0]) < 0) {
- dbgln("close() failed: {}", strerror(errno));
- }
- if (exit_reason != Break && !stream.eof()) {
- auto action = Continue;
- do {
- action = check_and_call();
- if (action == Break)
- return;
- } while (action == Continue);
- if (!stream.eof()) {
- auto entry = ByteBuffer::create_uninitialized(stream.size());
- auto rc = stream.read_or_error(entry);
- VERIFY(rc);
- callback(create<StringValue>(String::copy(entry)));
- }
- }
- return;
- }
- auto jobs = shell->run_commands(commands);
- if (!jobs.is_empty())
- callback(create<JobValue>(&jobs.last()));
- }
- RefPtr<Value> Execute::run(RefPtr<Shell> shell)
- {
- if (m_command->would_execute())
- return m_command->run(shell);
- NonnullRefPtrVector<Value> values;
- for_each_entry(shell, [&](auto value) {
- values.append(*value);
- return IterationDecision::Continue;
- });
- if (values.size() == 1 && values.first().is_job())
- return values.first();
- return create<ListValue>(move(values));
- }
- void Execute::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- if (m_capture_stdout)
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Green) });
- metadata.is_first_in_list = true;
- m_command->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult Execute::hit_test_position(size_t offset) const
- {
- auto result = m_command->hit_test_position(offset);
- if (!result.closest_node_with_semantic_meaning)
- result.closest_node_with_semantic_meaning = this;
- if (!result.closest_command_node)
- result.closest_command_node = m_command;
- return result;
- }
- Vector<Line::CompletionSuggestion> Execute::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
- {
- auto matching_node = hit_test_result.matching_node;
- if (!matching_node || !matching_node->is_bareword())
- return {};
- auto corrected_offset = offset - matching_node->position().start_offset;
- auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
- if (corrected_offset > node->text().length())
- return {};
- return shell.complete_program_name(node->text(), corrected_offset);
- }
- Execute::Execute(Position position, NonnullRefPtr<Node> command, bool capture_stdout)
- : Node(move(position))
- , m_command(move(command))
- , m_capture_stdout(capture_stdout)
- {
- if (m_command->is_syntax_error())
- set_is_syntax_error(m_command->syntax_error_node());
- }
- Execute::~Execute()
- {
- }
- void IfCond::dump(int level) const
- {
- Node::dump(level);
- print_indented("Condition", ++level);
- m_condition->dump(level + 1);
- print_indented("True Branch", level);
- if (m_true_branch)
- m_true_branch->dump(level + 1);
- else
- print_indented("(empty)", level + 1);
- print_indented("False Branch", level);
- if (m_false_branch)
- m_false_branch->dump(level + 1);
- else
- print_indented("(empty)", level + 1);
- }
- RefPtr<Value> IfCond::run(RefPtr<Shell> shell)
- {
- auto cond = m_condition->run(shell)->resolve_without_cast(shell);
- // The condition could be a builtin, in which case it has already run and exited.
- if (cond && cond->is_job()) {
- auto cond_job_value = static_cast<const JobValue*>(cond.ptr());
- auto cond_job = cond_job_value->job();
- shell->block_on_job(cond_job);
- }
- if (shell->last_return_code == 0) {
- if (m_true_branch)
- return m_true_branch->run(shell);
- } else {
- if (m_false_branch)
- return m_false_branch->run(shell);
- }
- return create<ListValue>({});
- }
- void IfCond::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- metadata.is_first_in_list = true;
- editor.stylize({ m_position.start_offset, m_position.start_offset + 2 }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- if (m_else_position.has_value())
- editor.stylize({ m_else_position.value().start_offset, m_else_position.value().start_offset + 4 }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- m_condition->highlight_in_editor(editor, shell, metadata);
- if (m_true_branch)
- m_true_branch->highlight_in_editor(editor, shell, metadata);
- if (m_false_branch)
- m_false_branch->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult IfCond::hit_test_position(size_t offset) const
- {
- if (auto result = m_condition->hit_test_position(offset); result.matching_node)
- return result;
- if (m_true_branch) {
- if (auto result = m_true_branch->hit_test_position(offset); result.matching_node)
- return result;
- }
- if (m_false_branch) {
- if (auto result = m_false_branch->hit_test_position(offset); result.matching_node)
- return result;
- }
- return {};
- }
- IfCond::IfCond(Position position, Optional<Position> else_position, NonnullRefPtr<Node> condition, RefPtr<Node> true_branch, RefPtr<Node> false_branch)
- : Node(move(position))
- , m_condition(move(condition))
- , m_true_branch(move(true_branch))
- , m_false_branch(move(false_branch))
- , m_else_position(move(else_position))
- {
- if (m_condition->is_syntax_error())
- set_is_syntax_error(m_condition->syntax_error_node());
- else if (m_true_branch && m_true_branch->is_syntax_error())
- set_is_syntax_error(m_true_branch->syntax_error_node());
- else if (m_false_branch && m_false_branch->is_syntax_error())
- set_is_syntax_error(m_false_branch->syntax_error_node());
- m_condition = create<AST::Execute>(m_condition->position(), m_condition);
- if (m_true_branch) {
- auto true_branch = m_true_branch.release_nonnull();
- if (true_branch->is_execute())
- m_true_branch = static_ptr_cast<AST::Execute>(true_branch)->command();
- else
- m_true_branch = move(true_branch);
- }
- if (m_false_branch) {
- auto false_branch = m_false_branch.release_nonnull();
- if (false_branch->is_execute())
- m_false_branch = static_ptr_cast<AST::Execute>(false_branch)->command();
- else
- m_false_branch = move(false_branch);
- }
- }
- IfCond::~IfCond()
- {
- }
- void ImmediateExpression::dump(int level) const
- {
- Node::dump(level);
- print_indented("(function)", level + 1);
- print_indented(m_function.name, level + 2);
- print_indented("(arguments)", level + 1);
- for (auto& argument : arguments())
- argument.dump(level + 2);
- }
- RefPtr<Value> ImmediateExpression::run(RefPtr<Shell> shell)
- {
- auto node = shell->run_immediate_function(m_function.name, *this, arguments());
- if (node)
- return node->run(shell);
- return create<ListValue>({});
- }
- void ImmediateExpression::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- // '${' - FIXME: This could also be '$\\\n{'
- editor.stylize({ m_position.start_offset, m_position.start_offset + 2 }, { Line::Style::Foreground(Line::Style::XtermColor::Green) });
- // Function name
- Line::Style function_style { Line::Style::Foreground(Line::Style::XtermColor::Red) };
- if (shell.has_immediate_function(function_name()))
- function_style = { Line::Style::Foreground(Line::Style::XtermColor::Green) };
- editor.stylize({ m_function.position.start_offset, m_function.position.end_offset }, move(function_style));
- // Arguments
- for (auto& argument : m_arguments) {
- metadata.is_first_in_list = false;
- argument.highlight_in_editor(editor, shell, metadata);
- }
- // Closing brace
- if (m_closing_brace_position.has_value())
- editor.stylize({ m_closing_brace_position->start_offset, m_closing_brace_position->end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Green) });
- }
- Vector<Line::CompletionSuggestion> ImmediateExpression::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
- {
- auto matching_node = hit_test_result.matching_node;
- if (!matching_node || matching_node != this)
- return {};
- auto corrected_offset = offset - m_function.position.start_offset;
- if (corrected_offset > m_function.name.length())
- return {};
- return shell.complete_immediate_function_name(m_function.name, corrected_offset);
- }
- HitTestResult ImmediateExpression::hit_test_position(size_t offset) const
- {
- if (m_function.position.contains(offset))
- return { this, this, this };
- for (auto& argument : m_arguments) {
- if (auto result = argument.hit_test_position(offset); result.matching_node)
- return result;
- }
- return {};
- }
- ImmediateExpression::ImmediateExpression(Position position, NameWithPosition function, NonnullRefPtrVector<AST::Node> arguments, Optional<Position> closing_brace_position)
- : Node(move(position))
- , m_arguments(move(arguments))
- , m_function(move(function))
- , m_closing_brace_position(move(closing_brace_position))
- {
- if (is_syntax_error())
- return;
- for (auto& argument : m_arguments) {
- if (argument.is_syntax_error()) {
- set_is_syntax_error(argument.syntax_error_node());
- return;
- }
- }
- }
- ImmediateExpression::~ImmediateExpression()
- {
- }
- void Join::dump(int level) const
- {
- Node::dump(level);
- m_left->dump(level + 1);
- m_right->dump(level + 1);
- }
- RefPtr<Value> Join::run(RefPtr<Shell> shell)
- {
- auto left = m_left->to_lazy_evaluated_commands(shell);
- auto right = m_right->to_lazy_evaluated_commands(shell);
- return create<CommandSequenceValue>(join_commands(move(left), move(right)));
- }
- void Join::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- m_left->highlight_in_editor(editor, shell, metadata);
- if (m_left->is_list() || m_left->is_command())
- metadata.is_first_in_list = false;
- m_right->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult Join::hit_test_position(size_t offset) const
- {
- auto result = m_left->hit_test_position(offset);
- if (result.matching_node)
- return result;
- return m_right->hit_test_position(offset);
- }
- RefPtr<Node> Join::leftmost_trivial_literal() const
- {
- if (auto value = m_left->leftmost_trivial_literal())
- return value;
- return m_right->leftmost_trivial_literal();
- }
- Join::Join(Position position, NonnullRefPtr<Node> left, NonnullRefPtr<Node> right)
- : Node(move(position))
- , m_left(move(left))
- , m_right(move(right))
- {
- if (m_left->is_syntax_error())
- set_is_syntax_error(m_left->syntax_error_node());
- else if (m_right->is_syntax_error())
- set_is_syntax_error(m_right->syntax_error_node());
- }
- Join::~Join()
- {
- }
- void MatchExpr::dump(int level) const
- {
- Node::dump(level);
- print_indented(String::formatted("(expression: {})", m_expr_name.characters()), level + 1);
- m_matched_expr->dump(level + 2);
- print_indented(String::formatted("(named: {})", m_expr_name.characters()), level + 1);
- print_indented("(entries)", level + 1);
- for (auto& entry : m_entries) {
- StringBuilder builder;
- builder.append("(match");
- if (entry.match_names.has_value()) {
- builder.append(" to names (");
- bool first = true;
- for (auto& name : entry.match_names.value()) {
- if (!first)
- builder.append(' ');
- first = false;
- builder.append(name);
- }
- builder.append("))");
- } else {
- builder.append(')');
- }
- print_indented(builder.string_view(), level + 2);
- for (auto& node : entry.options)
- node.dump(level + 3);
- print_indented("(execute)", level + 2);
- if (entry.body)
- entry.body->dump(level + 3);
- else
- print_indented("(nothing)", level + 3);
- }
- }
- RefPtr<Value> MatchExpr::run(RefPtr<Shell> shell)
- {
- auto value = m_matched_expr->run(shell)->resolve_without_cast(shell);
- auto list = value->resolve_as_list(shell);
- auto list_matches = [&](auto&& pattern, auto& spans) {
- if (pattern.size() != list.size())
- return false;
- for (size_t i = 0; i < pattern.size(); ++i) {
- Vector<AK::MaskSpan> mask_spans;
- if (!list[i].matches(pattern[i], mask_spans))
- return false;
- for (auto& span : mask_spans)
- spans.append(list[i].substring(span.start, span.length));
- }
- return true;
- };
- auto resolve_pattern = [&](auto& option) {
- Vector<String> pattern;
- if (option.is_glob()) {
- pattern.append(static_cast<const Glob*>(&option)->text());
- } else if (option.is_bareword()) {
- pattern.append(static_cast<const BarewordLiteral*>(&option)->text());
- } else {
- auto list = option.run(shell);
- option.for_each_entry(shell, [&](auto&& value) {
- pattern.append(value->resolve_as_list(nullptr)); // Note: 'nullptr' incurs special behaviour,
- // asking the node for a 'raw' value.
- return IterationDecision::Continue;
- });
- }
- return pattern;
- };
- auto frame = shell->push_frame(String::formatted("match ({})", this));
- if (!m_expr_name.is_empty())
- shell->set_local_variable(m_expr_name, value, true);
- for (auto& entry : m_entries) {
- for (auto& option : entry.options) {
- Vector<String> spans;
- if (list_matches(resolve_pattern(option), spans)) {
- if (entry.body) {
- if (entry.match_names.has_value()) {
- size_t i = 0;
- for (auto& name : entry.match_names.value()) {
- if (spans.size() > i)
- shell->set_local_variable(name, create<AST::StringValue>(spans[i]), true);
- ++i;
- }
- }
- return entry.body->run(shell);
- } else {
- return create<AST::ListValue>({});
- }
- }
- }
- }
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "Non-exhaustive match rules!", position());
- return create<AST::ListValue>({});
- }
- void MatchExpr::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- editor.stylize({ m_position.start_offset, m_position.start_offset + 5 }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- if (m_as_position.has_value())
- editor.stylize({ m_as_position.value().start_offset, m_as_position.value().end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- metadata.is_first_in_list = false;
- m_matched_expr->highlight_in_editor(editor, shell, metadata);
- for (auto& entry : m_entries) {
- metadata.is_first_in_list = false;
- for (auto& option : entry.options)
- option.highlight_in_editor(editor, shell, metadata);
- metadata.is_first_in_list = true;
- if (entry.body)
- entry.body->highlight_in_editor(editor, shell, metadata);
- for (auto& position : entry.pipe_positions)
- editor.stylize({ position.start_offset, position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- if (entry.match_as_position.has_value())
- editor.stylize({ entry.match_as_position.value().start_offset, entry.match_as_position.value().end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- }
- }
- HitTestResult MatchExpr::hit_test_position(size_t offset) const
- {
- auto result = m_matched_expr->hit_test_position(offset);
- if (result.matching_node)
- return result;
- for (auto& entry : m_entries) {
- if (!entry.body)
- continue;
- auto result = entry.body->hit_test_position(offset);
- if (result.matching_node)
- return result;
- }
- return {};
- }
- MatchExpr::MatchExpr(Position position, NonnullRefPtr<Node> expr, String name, Optional<Position> as_position, Vector<MatchEntry> entries)
- : Node(move(position))
- , m_matched_expr(move(expr))
- , m_expr_name(move(name))
- , m_as_position(move(as_position))
- , m_entries(move(entries))
- {
- if (m_matched_expr->is_syntax_error()) {
- set_is_syntax_error(m_matched_expr->syntax_error_node());
- } else {
- for (auto& entry : m_entries) {
- if (!entry.body)
- continue;
- if (entry.body->is_syntax_error()) {
- set_is_syntax_error(entry.body->syntax_error_node());
- break;
- }
- }
- }
- }
- MatchExpr::~MatchExpr()
- {
- }
- void Or::dump(int level) const
- {
- Node::dump(level);
- m_left->dump(level + 1);
- m_right->dump(level + 1);
- }
- RefPtr<Value> Or::run(RefPtr<Shell> shell)
- {
- auto commands = m_left->to_lazy_evaluated_commands(shell);
- commands.last().next_chain.empend(*m_right, NodeWithAction::Or);
- return create<CommandSequenceValue>(move(commands));
- }
- void Or::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- m_left->highlight_in_editor(editor, shell, metadata);
- m_right->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult Or::hit_test_position(size_t offset) const
- {
- auto result = m_left->hit_test_position(offset);
- if (result.matching_node) {
- if (!result.closest_command_node)
- result.closest_command_node = m_right;
- return result;
- }
- result = m_right->hit_test_position(offset);
- if (!result.closest_command_node)
- result.closest_command_node = m_right;
- return result;
- }
- Or::Or(Position position, NonnullRefPtr<Node> left, NonnullRefPtr<Node> right, Position or_position)
- : Node(move(position))
- , m_left(move(left))
- , m_right(move(right))
- , m_or_position(or_position)
- {
- if (m_left->is_syntax_error())
- set_is_syntax_error(m_left->syntax_error_node());
- else if (m_right->is_syntax_error())
- set_is_syntax_error(m_right->syntax_error_node());
- }
- Or::~Or()
- {
- }
- void Pipe::dump(int level) const
- {
- Node::dump(level);
- m_left->dump(level + 1);
- m_right->dump(level + 1);
- }
- RefPtr<Value> Pipe::run(RefPtr<Shell> shell)
- {
- auto left = m_left->to_lazy_evaluated_commands(shell);
- auto right = m_right->to_lazy_evaluated_commands(shell);
- auto last_in_left = left.take_last();
- auto first_in_right = right.take_first();
- auto pipe_read_end = FdRedirection::create(-1, STDIN_FILENO, Rewiring::Close::Old);
- auto pipe_write_end = FdRedirection::create(-1, STDOUT_FILENO, pipe_read_end, Rewiring::Close::RefreshOld);
- auto insert_at_start_or_after_last_pipe = [&](auto& pipe, auto& command) {
- size_t insert_index = 0;
- auto& redirections = command.redirections;
- for (ssize_t i = redirections.size() - 1; i >= 0; --i) {
- auto& redirection = redirections[i];
- if (!redirection.is_fd_redirection())
- continue;
- auto& fd_redirection = static_cast<FdRedirection&>(redirection);
- if (fd_redirection.old_fd == -1) {
- insert_index = i;
- break;
- }
- }
- redirections.insert(insert_index, pipe);
- };
- insert_at_start_or_after_last_pipe(pipe_read_end, first_in_right);
- insert_at_start_or_after_last_pipe(pipe_write_end, last_in_left);
- last_in_left.should_wait = false;
- last_in_left.is_pipe_source = true;
- if (first_in_right.pipeline) {
- last_in_left.pipeline = first_in_right.pipeline;
- } else {
- auto pipeline = create<Pipeline>();
- last_in_left.pipeline = pipeline;
- first_in_right.pipeline = pipeline;
- }
- Vector<Command> commands;
- commands.append(left);
- commands.append(last_in_left);
- commands.append(first_in_right);
- commands.append(right);
- return create<CommandSequenceValue>(move(commands));
- }
- void Pipe::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- m_left->highlight_in_editor(editor, shell, metadata);
- m_right->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult Pipe::hit_test_position(size_t offset) const
- {
- auto result = m_left->hit_test_position(offset);
- if (result.matching_node) {
- if (!result.closest_command_node)
- result.closest_command_node = m_right;
- return result;
- }
- result = m_right->hit_test_position(offset);
- if (!result.closest_command_node)
- result.closest_command_node = m_right;
- return result;
- }
- Pipe::Pipe(Position position, NonnullRefPtr<Node> left, NonnullRefPtr<Node> right)
- : Node(move(position))
- , m_left(move(left))
- , m_right(move(right))
- {
- if (m_left->is_syntax_error())
- set_is_syntax_error(m_left->syntax_error_node());
- else if (m_right->is_syntax_error())
- set_is_syntax_error(m_right->syntax_error_node());
- }
- Pipe::~Pipe()
- {
- }
- PathRedirectionNode::PathRedirectionNode(Position position, int fd, NonnullRefPtr<Node> path)
- : Node(move(position))
- , m_fd(fd)
- , m_path(move(path))
- {
- }
- void PathRedirectionNode::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(0x87, 0x9b, 0xcd) }); // 25% Darkened Periwinkle
- metadata.is_first_in_list = false;
- m_path->highlight_in_editor(editor, shell, metadata);
- if (m_path->is_bareword()) {
- auto path_text = m_path->run(nullptr)->resolve_as_list(nullptr);
- VERIFY(path_text.size() == 1);
- // Apply a URL to the path.
- auto& position = m_path->position();
- auto& path = path_text[0];
- if (!path.starts_with('/'))
- path = String::formatted("{}/{}", shell.cwd, path);
- auto url = URL::create_with_file_protocol(path);
- url.set_host(shell.hostname);
- editor.stylize({ position.start_offset, position.end_offset }, { Line::Style::Hyperlink(url.to_string()) });
- }
- }
- HitTestResult PathRedirectionNode::hit_test_position(size_t offset) const
- {
- auto result = m_path->hit_test_position(offset);
- if (!result.closest_node_with_semantic_meaning)
- result.closest_node_with_semantic_meaning = this;
- return result;
- }
- Vector<Line::CompletionSuggestion> PathRedirectionNode::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
- {
- auto matching_node = hit_test_result.matching_node;
- if (!matching_node || !matching_node->is_bareword())
- return {};
- auto corrected_offset = offset - matching_node->position().start_offset;
- auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
- if (corrected_offset > node->text().length())
- return {};
- return shell.complete_path("", node->text(), corrected_offset, Shell::ExecutableOnly::No);
- }
- PathRedirectionNode::~PathRedirectionNode()
- {
- }
- void Range::dump(int level) const
- {
- Node::dump(level);
- print_indented("(From)", level + 1);
- m_start->dump(level + 2);
- print_indented("(To)", level + 1);
- m_end->dump(level + 2);
- }
- RefPtr<Value> Range::run(RefPtr<Shell> shell)
- {
- auto interpolate = [position = position()](RefPtr<Value> start, RefPtr<Value> end, RefPtr<Shell> shell) -> NonnullRefPtrVector<Value> {
- NonnullRefPtrVector<Value> values;
- if (start->is_string() && end->is_string()) {
- auto start_str = start->resolve_as_list(shell)[0];
- auto end_str = end->resolve_as_list(shell)[0];
- Utf8View start_view { start_str }, end_view { end_str };
- if (start_view.validate() && end_view.validate()) {
- if (start_view.length() == 1 && end_view.length() == 1) {
- // Interpolate between two code points.
- auto start_code_point = *start_view.begin();
- auto end_code_point = *end_view.begin();
- auto step = start_code_point > end_code_point ? -1 : 1;
- StringBuilder builder;
- for (u32 code_point = start_code_point; code_point != end_code_point; code_point += step) {
- builder.clear();
- builder.append_code_point(code_point);
- values.append(create<StringValue>(builder.to_string()));
- }
- // Append the ending code point too, most shells treat this as inclusive.
- builder.clear();
- builder.append_code_point(end_code_point);
- values.append(create<StringValue>(builder.to_string()));
- } else {
- // Could be two numbers?
- auto start_int = start_str.to_int();
- auto end_int = end_str.to_int();
- if (start_int.has_value() && end_int.has_value()) {
- auto start = start_int.value();
- auto end = end_int.value();
- auto step = start > end ? -1 : 1;
- for (int value = start; value != end; value += step)
- values.append(create<StringValue>(String::number(value)));
- // Append the range end too, most shells treat this as inclusive.
- values.append(create<StringValue>(String::number(end)));
- } else {
- goto yield_start_end;
- }
- }
- } else {
- yield_start_end:;
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, String::formatted("Cannot interpolate between '{}' and '{}'!", start_str, end_str), position);
- // We can't really interpolate between the two, so just yield both.
- values.append(create<StringValue>(move(start_str)));
- values.append(create<StringValue>(move(end_str)));
- }
- return values;
- }
- warnln("Shell: Cannot apply the requested interpolation");
- return values;
- };
- auto start_value = m_start->run(shell);
- auto end_value = m_end->run(shell);
- if (!start_value || !end_value)
- return create<ListValue>({});
- return create<ListValue>(interpolate(*start_value, *end_value, shell));
- }
- void Range::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- m_start->highlight_in_editor(editor, shell, metadata);
- // Highlight the '..'
- editor.stylize({ m_start->position().end_offset, m_end->position().start_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow) });
- metadata.is_first_in_list = false;
- m_end->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult Range::hit_test_position(size_t offset) const
- {
- auto result = m_start->hit_test_position(offset);
- if (result.matching_node) {
- if (!result.closest_command_node)
- result.closest_command_node = m_start;
- return result;
- }
- result = m_end->hit_test_position(offset);
- if (!result.closest_command_node)
- result.closest_command_node = m_end;
- return result;
- }
- Range::Range(Position position, NonnullRefPtr<Node> start, NonnullRefPtr<Node> end)
- : Node(move(position))
- , m_start(move(start))
- , m_end(move(end))
- {
- if (m_start->is_syntax_error())
- set_is_syntax_error(m_start->syntax_error_node());
- else if (m_end->is_syntax_error())
- set_is_syntax_error(m_end->syntax_error_node());
- }
- Range::~Range()
- {
- }
- void ReadRedirection::dump(int level) const
- {
- Node::dump(level);
- m_path->dump(level + 1);
- print_indented(String::formatted("To {}", m_fd), level + 1);
- }
- RefPtr<Value> ReadRedirection::run(RefPtr<Shell> shell)
- {
- Command command;
- auto path_segments = m_path->run(shell)->resolve_as_list(shell);
- StringBuilder builder;
- builder.join(" ", path_segments);
- command.redirections.append(PathRedirection::create(builder.to_string(), m_fd, PathRedirection::Read));
- return create<CommandValue>(move(command));
- }
- ReadRedirection::ReadRedirection(Position position, int fd, NonnullRefPtr<Node> path)
- : PathRedirectionNode(move(position), fd, move(path))
- {
- }
- ReadRedirection::~ReadRedirection()
- {
- }
- void ReadWriteRedirection::dump(int level) const
- {
- Node::dump(level);
- m_path->dump(level + 1);
- print_indented(String::formatted("To/From {}", m_fd), level + 1);
- }
- RefPtr<Value> ReadWriteRedirection::run(RefPtr<Shell> shell)
- {
- Command command;
- auto path_segments = m_path->run(shell)->resolve_as_list(shell);
- StringBuilder builder;
- builder.join(" ", path_segments);
- command.redirections.append(PathRedirection::create(builder.to_string(), m_fd, PathRedirection::ReadWrite));
- return create<CommandValue>(move(command));
- }
- ReadWriteRedirection::ReadWriteRedirection(Position position, int fd, NonnullRefPtr<Node> path)
- : PathRedirectionNode(move(position), fd, move(path))
- {
- }
- ReadWriteRedirection::~ReadWriteRedirection()
- {
- }
- void Sequence::dump(int level) const
- {
- Node::dump(level);
- for (auto& entry : m_entries)
- entry.dump(level + 1);
- }
- RefPtr<Value> Sequence::run(RefPtr<Shell> shell)
- {
- Vector<Command> all_commands;
- Command* last_command_in_sequence = nullptr;
- for (auto& entry : m_entries) {
- if (!last_command_in_sequence) {
- auto commands = entry.to_lazy_evaluated_commands(shell);
- all_commands.append(move(commands));
- last_command_in_sequence = &all_commands.last();
- continue;
- }
- if (last_command_in_sequence->should_wait) {
- last_command_in_sequence->next_chain.append(NodeWithAction { entry, NodeWithAction::Sequence });
- } else {
- all_commands.append(entry.to_lazy_evaluated_commands(shell));
- last_command_in_sequence = &all_commands.last();
- }
- }
- return create<CommandSequenceValue>(move(all_commands));
- }
- void Sequence::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- for (auto& entry : m_entries)
- entry.highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult Sequence::hit_test_position(size_t offset) const
- {
- for (auto& entry : m_entries) {
- auto result = entry.hit_test_position(offset);
- if (result.matching_node) {
- if (!result.closest_command_node)
- result.closest_command_node = entry;
- return result;
- }
- }
- return {};
- }
- Sequence::Sequence(Position position, NonnullRefPtrVector<Node> entries, Vector<Position> separator_positions)
- : Node(move(position))
- , m_entries(move(entries))
- , m_separator_positions(separator_positions)
- {
- for (auto& entry : m_entries) {
- if (entry.is_syntax_error()) {
- set_is_syntax_error(entry.syntax_error_node());
- break;
- }
- }
- }
- Sequence::~Sequence()
- {
- }
- void Subshell::dump(int level) const
- {
- Node::dump(level);
- if (m_block)
- m_block->dump(level + 1);
- }
- RefPtr<Value> Subshell::run(RefPtr<Shell> shell)
- {
- if (!m_block)
- return create<ListValue>({});
- return create<AST::CommandSequenceValue>(m_block->to_lazy_evaluated_commands(shell));
- }
- void Subshell::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- metadata.is_first_in_list = true;
- if (m_block)
- m_block->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult Subshell::hit_test_position(size_t offset) const
- {
- if (m_block)
- return m_block->hit_test_position(offset);
- return {};
- }
- Subshell::Subshell(Position position, RefPtr<Node> block)
- : Node(move(position))
- , m_block(block)
- {
- if (m_block && m_block->is_syntax_error())
- set_is_syntax_error(m_block->syntax_error_node());
- }
- Subshell::~Subshell()
- {
- }
- void Slice::dump(int level) const
- {
- Node::dump(level);
- m_selector->dump(level + 1);
- }
- RefPtr<Value> Slice::run(RefPtr<Shell> shell)
- {
- return m_selector->run(shell);
- }
- void Slice::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- m_selector->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult Slice::hit_test_position(size_t offset) const
- {
- return m_selector->hit_test_position(offset);
- }
- Vector<Line::CompletionSuggestion> Slice::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
- {
- // TODO: Maybe intercept this, and suggest values in range?
- return m_selector->complete_for_editor(shell, offset, hit_test_result);
- }
- Slice::Slice(Position position, NonnullRefPtr<AST::Node> selector)
- : Node(move(position))
- , m_selector(move(selector))
- {
- if (m_selector->is_syntax_error())
- set_is_syntax_error(m_selector->syntax_error_node());
- }
- Slice::~Slice()
- {
- }
- void SimpleVariable::dump(int level) const
- {
- Node::dump(level);
- print_indented("(Name)", level + 1);
- print_indented(m_name, level + 2);
- print_indented("(Slice)", level + 1);
- if (m_slice)
- m_slice->dump(level + 2);
- else
- print_indented("(None)", level + 2);
- }
- RefPtr<Value> SimpleVariable::run(RefPtr<Shell>)
- {
- NonnullRefPtr<Value> value = create<SimpleVariableValue>(m_name);
- if (m_slice)
- value = value->with_slices(*m_slice);
- return value;
- }
- void SimpleVariable::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- Line::Style style { Line::Style::Foreground(214, 112, 214) };
- if (metadata.is_first_in_list)
- style.unify_with({ Line::Style::Bold });
- editor.stylize({ m_position.start_offset, m_position.end_offset }, move(style));
- if (m_slice)
- m_slice->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult SimpleVariable::hit_test_position(size_t offset) const
- {
- if (m_slice && m_slice->position().contains(offset))
- return m_slice->hit_test_position(offset);
- return { this, this, nullptr };
- }
- Vector<Line::CompletionSuggestion> SimpleVariable::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
- {
- auto matching_node = hit_test_result.matching_node;
- if (!matching_node)
- return {};
- if (matching_node != this)
- return {};
- auto corrected_offset = offset - matching_node->position().start_offset - 1;
- if (corrected_offset > m_name.length() + 1)
- return {};
- return shell.complete_variable(m_name, corrected_offset);
- }
- SimpleVariable::SimpleVariable(Position position, String name)
- : VariableNode(move(position))
- , m_name(move(name))
- {
- }
- SimpleVariable::~SimpleVariable()
- {
- }
- void SpecialVariable::dump(int level) const
- {
- Node::dump(level);
- print_indented("(Name)", level + 1);
- print_indented(String { &m_name, 1 }, level + 1);
- print_indented("(Slice)", level + 1);
- if (m_slice)
- m_slice->dump(level + 2);
- else
- print_indented("(None)", level + 2);
- }
- RefPtr<Value> SpecialVariable::run(RefPtr<Shell>)
- {
- NonnullRefPtr<Value> value = create<SpecialVariableValue>(m_name);
- if (m_slice)
- value = value->with_slices(*m_slice);
- return value;
- }
- void SpecialVariable::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(214, 112, 214) });
- if (m_slice)
- m_slice->highlight_in_editor(editor, shell, metadata);
- }
- Vector<Line::CompletionSuggestion> SpecialVariable::complete_for_editor(Shell&, size_t, const HitTestResult&)
- {
- return {};
- }
- HitTestResult SpecialVariable::hit_test_position(size_t offset) const
- {
- if (m_slice && m_slice->position().contains(offset))
- return m_slice->hit_test_position(offset);
- return { this, this, nullptr };
- }
- SpecialVariable::SpecialVariable(Position position, char name)
- : VariableNode(move(position))
- , m_name(name)
- {
- }
- SpecialVariable::~SpecialVariable()
- {
- }
- void Juxtaposition::dump(int level) const
- {
- Node::dump(level);
- m_left->dump(level + 1);
- m_right->dump(level + 1);
- }
- RefPtr<Value> Juxtaposition::run(RefPtr<Shell> shell)
- {
- auto left_value = m_left->run(shell)->resolve_without_cast(shell);
- auto right_value = m_right->run(shell)->resolve_without_cast(shell);
- auto left = left_value->resolve_as_list(shell);
- auto right = right_value->resolve_as_list(shell);
- if (left_value->is_string() && right_value->is_string()) {
- VERIFY(left.size() == 1);
- VERIFY(right.size() == 1);
- StringBuilder builder;
- builder.append(left[0]);
- builder.append(right[0]);
- return create<StringValue>(builder.to_string());
- }
- // Otherwise, treat them as lists and create a list product.
- if (left.is_empty() || right.is_empty())
- return create<ListValue>({});
- Vector<String> result;
- result.ensure_capacity(left.size() * right.size());
- StringBuilder builder;
- for (auto& left_element : left) {
- for (auto& right_element : right) {
- builder.append(left_element);
- builder.append(right_element);
- result.append(builder.to_string());
- builder.clear();
- }
- }
- return create<ListValue>(move(result));
- }
- void Juxtaposition::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- m_left->highlight_in_editor(editor, shell, metadata);
- // '~/foo/bar' is special, we have to actually resolve the tilde
- // since that resolution is a pure operation, we can just go ahead
- // and do it to get the value :)
- if (m_right->is_bareword() && m_left->is_tilde()) {
- auto tilde_value = m_left->run(shell)->resolve_as_list(shell)[0];
- auto bareword_value = m_right->run(shell)->resolve_as_list(shell)[0];
- StringBuilder path_builder;
- path_builder.append(tilde_value);
- path_builder.append("/");
- path_builder.append(bareword_value);
- auto path = path_builder.to_string();
- if (Core::File::exists(path)) {
- auto realpath = shell.resolve_path(path);
- auto url = URL::create_with_file_protocol(realpath);
- url.set_host(shell.hostname);
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Hyperlink(url.to_string()) });
- }
- } else {
- m_right->highlight_in_editor(editor, shell, metadata);
- }
- }
- Vector<Line::CompletionSuggestion> Juxtaposition::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
- {
- auto matching_node = hit_test_result.matching_node;
- // '~/foo/bar' is special, we have to actually resolve the tilde
- // then complete the bareword with that path prefix.
- if (m_right->is_bareword() && m_left->is_tilde()) {
- auto tilde_value = m_left->run(shell)->resolve_as_list(shell)[0];
- auto corrected_offset = offset - matching_node->position().start_offset;
- auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
- if (corrected_offset > node->text().length())
- return {};
- auto text = node->text().substring(1, node->text().length() - 1);
- return shell.complete_path(tilde_value, text, corrected_offset - 1, Shell::ExecutableOnly::No);
- }
- return Node::complete_for_editor(shell, offset, hit_test_result);
- }
- HitTestResult Juxtaposition::hit_test_position(size_t offset) const
- {
- auto result = m_left->hit_test_position(offset);
- if (!result.closest_node_with_semantic_meaning)
- result.closest_node_with_semantic_meaning = this;
- if (result.matching_node)
- return result;
- result = m_right->hit_test_position(offset);
- if (!result.closest_node_with_semantic_meaning)
- result.closest_node_with_semantic_meaning = this;
- return result;
- }
- Juxtaposition::Juxtaposition(Position position, NonnullRefPtr<Node> left, NonnullRefPtr<Node> right)
- : Node(move(position))
- , m_left(move(left))
- , m_right(move(right))
- {
- if (m_left->is_syntax_error())
- set_is_syntax_error(m_left->syntax_error_node());
- else if (m_right->is_syntax_error())
- set_is_syntax_error(m_right->syntax_error_node());
- }
- Juxtaposition::~Juxtaposition()
- {
- }
- void StringLiteral::dump(int level) const
- {
- Node::dump(level);
- print_indented(m_text, level + 1);
- }
- RefPtr<Value> StringLiteral::run(RefPtr<Shell>)
- {
- return create<StringValue>(m_text);
- }
- void StringLiteral::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata metadata)
- {
- if (m_text.is_empty())
- return;
- Line::Style style { Line::Style::Foreground(Line::Style::XtermColor::Yellow) };
- if (metadata.is_first_in_list)
- style.unify_with({ Line::Style::Bold });
- editor.stylize({ m_position.start_offset, m_position.end_offset }, move(style));
- }
- StringLiteral::StringLiteral(Position position, String text)
- : Node(move(position))
- , m_text(move(text))
- {
- }
- StringLiteral::~StringLiteral()
- {
- }
- void StringPartCompose::dump(int level) const
- {
- Node::dump(level);
- m_left->dump(level + 1);
- m_right->dump(level + 1);
- }
- RefPtr<Value> StringPartCompose::run(RefPtr<Shell> shell)
- {
- auto left = m_left->run(shell)->resolve_as_list(shell);
- auto right = m_right->run(shell)->resolve_as_list(shell);
- StringBuilder builder;
- builder.join(" ", left);
- builder.join(" ", right);
- return create<StringValue>(builder.to_string());
- }
- void StringPartCompose::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- m_left->highlight_in_editor(editor, shell, metadata);
- m_right->highlight_in_editor(editor, shell, metadata);
- }
- HitTestResult StringPartCompose::hit_test_position(size_t offset) const
- {
- auto result = m_left->hit_test_position(offset);
- if (result.matching_node)
- return result;
- return m_right->hit_test_position(offset);
- }
- StringPartCompose::StringPartCompose(Position position, NonnullRefPtr<Node> left, NonnullRefPtr<Node> right)
- : Node(move(position))
- , m_left(move(left))
- , m_right(move(right))
- {
- if (m_left->is_syntax_error())
- set_is_syntax_error(m_left->syntax_error_node());
- else if (m_right->is_syntax_error())
- set_is_syntax_error(m_right->syntax_error_node());
- }
- StringPartCompose::~StringPartCompose()
- {
- }
- void SyntaxError::dump(int level) const
- {
- Node::dump(level);
- print_indented("(Error text)", level + 1);
- print_indented(m_syntax_error_text, level + 2);
- print_indented("(Can be recovered from)", level + 1);
- print_indented(String::formatted("{}", m_is_continuable), level + 2);
- }
- RefPtr<Value> SyntaxError::run(RefPtr<Shell> shell)
- {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, m_syntax_error_text, position());
- return create<StringValue>("");
- }
- void SyntaxError::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
- {
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Red), Line::Style::Bold });
- }
- SyntaxError::SyntaxError(Position position, String error, bool is_continuable)
- : Node(move(position))
- , m_syntax_error_text(move(error))
- , m_is_continuable(is_continuable)
- {
- }
- const SyntaxError& SyntaxError::syntax_error_node() const
- {
- return *this;
- }
- SyntaxError::~SyntaxError()
- {
- }
- void SyntheticNode::dump(int level) const
- {
- Node::dump(level);
- }
- RefPtr<Value> SyntheticNode::run(RefPtr<Shell>)
- {
- return m_value;
- }
- void SyntheticNode::highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata)
- {
- }
- SyntheticNode::SyntheticNode(Position position, NonnullRefPtr<Value> value)
- : Node(move(position))
- , m_value(move(value))
- {
- }
- void Tilde::dump(int level) const
- {
- Node::dump(level);
- print_indented(m_username, level + 1);
- }
- RefPtr<Value> Tilde::run(RefPtr<Shell>)
- {
- return create<TildeValue>(m_username);
- }
- void Tilde::highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata)
- {
- }
- HitTestResult Tilde::hit_test_position(size_t offset) const
- {
- if (!position().contains(offset))
- return {};
- return { this, this, nullptr };
- }
- Vector<Line::CompletionSuggestion> Tilde::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
- {
- auto matching_node = hit_test_result.matching_node;
- if (!matching_node)
- return {};
- if (matching_node != this)
- return {};
- auto corrected_offset = offset - matching_node->position().start_offset - 1;
- if (corrected_offset > m_username.length() + 1)
- return {};
- return shell.complete_user(m_username, corrected_offset);
- }
- String Tilde::text() const
- {
- StringBuilder builder;
- builder.append('~');
- builder.append(m_username);
- return builder.to_string();
- }
- Tilde::Tilde(Position position, String username)
- : Node(move(position))
- , m_username(move(username))
- {
- }
- Tilde::~Tilde()
- {
- }
- void WriteAppendRedirection::dump(int level) const
- {
- Node::dump(level);
- m_path->dump(level + 1);
- print_indented(String::formatted("From {}", m_fd), level + 1);
- }
- RefPtr<Value> WriteAppendRedirection::run(RefPtr<Shell> shell)
- {
- Command command;
- auto path_segments = m_path->run(shell)->resolve_as_list(shell);
- StringBuilder builder;
- builder.join(" ", path_segments);
- command.redirections.append(PathRedirection::create(builder.to_string(), m_fd, PathRedirection::WriteAppend));
- return create<CommandValue>(move(command));
- }
- WriteAppendRedirection::WriteAppendRedirection(Position position, int fd, NonnullRefPtr<Node> path)
- : PathRedirectionNode(move(position), fd, move(path))
- {
- }
- WriteAppendRedirection::~WriteAppendRedirection()
- {
- }
- void WriteRedirection::dump(int level) const
- {
- Node::dump(level);
- m_path->dump(level + 1);
- print_indented(String::formatted("From {}", m_fd), level + 1);
- }
- RefPtr<Value> WriteRedirection::run(RefPtr<Shell> shell)
- {
- Command command;
- auto path_segments = m_path->run(shell)->resolve_as_list(shell);
- StringBuilder builder;
- builder.join(" ", path_segments);
- command.redirections.append(PathRedirection::create(builder.to_string(), m_fd, PathRedirection::Write));
- return create<CommandValue>(move(command));
- }
- WriteRedirection::WriteRedirection(Position position, int fd, NonnullRefPtr<Node> path)
- : PathRedirectionNode(move(position), fd, move(path))
- {
- }
- WriteRedirection::~WriteRedirection()
- {
- }
- void VariableDeclarations::dump(int level) const
- {
- Node::dump(level);
- for (auto& var : m_variables) {
- print_indented("Set", level + 1);
- var.name->dump(level + 2);
- var.value->dump(level + 2);
- }
- }
- RefPtr<Value> VariableDeclarations::run(RefPtr<Shell> shell)
- {
- for (auto& var : m_variables) {
- auto name_value = var.name->run(shell)->resolve_as_list(shell);
- VERIFY(name_value.size() == 1);
- auto name = name_value[0];
- auto value = var.value->run(shell);
- shell->set_local_variable(name, value.release_nonnull());
- }
- return create<ListValue>({});
- }
- void VariableDeclarations::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- metadata.is_first_in_list = false;
- for (auto& var : m_variables) {
- var.name->highlight_in_editor(editor, shell, metadata);
- // Highlight the '='.
- editor.stylize({ var.name->position().end_offset - 1, var.name->position().end_offset }, { Line::Style::Foreground(Line::Style::XtermColor::Blue) });
- var.value->highlight_in_editor(editor, shell, metadata);
- }
- }
- HitTestResult VariableDeclarations::hit_test_position(size_t offset) const
- {
- for (auto decl : m_variables) {
- auto result = decl.value->hit_test_position(offset);
- if (result.matching_node)
- return result;
- }
- return { nullptr, nullptr, nullptr };
- }
- VariableDeclarations::VariableDeclarations(Position position, Vector<Variable> variables)
- : Node(move(position))
- , m_variables(move(variables))
- {
- for (auto& decl : m_variables) {
- if (decl.name->is_syntax_error()) {
- set_is_syntax_error(decl.name->syntax_error_node());
- break;
- }
- if (decl.value->is_syntax_error()) {
- set_is_syntax_error(decl.value->syntax_error_node());
- break;
- }
- }
- }
- VariableDeclarations::~VariableDeclarations()
- {
- }
- Value::~Value()
- {
- }
- Vector<AST::Command> Value::resolve_as_commands(RefPtr<Shell> shell)
- {
- Command command;
- command.argv = resolve_as_list(shell);
- return { command };
- }
- ListValue::ListValue(Vector<String> values)
- {
- if (values.is_empty())
- return;
- m_contained_values.ensure_capacity(values.size());
- for (auto& str : values)
- m_contained_values.append(adopt_ref(*new StringValue(move(str))));
- }
- NonnullRefPtr<Value> Value::with_slices(NonnullRefPtr<Slice> slice) const&
- {
- auto value = clone();
- value->m_slices.append(move(slice));
- return value;
- }
- NonnullRefPtr<Value> Value::with_slices(NonnullRefPtrVector<Slice> slices) const&
- {
- auto value = clone();
- value->m_slices.append(move(slices));
- return value;
- }
- ListValue::~ListValue()
- {
- }
- Vector<String> ListValue::resolve_as_list(RefPtr<Shell> shell)
- {
- Vector<String> values;
- for (auto& value : m_contained_values)
- values.append(value.resolve_as_list(shell));
- return resolve_slices(shell, move(values), m_slices);
- }
- NonnullRefPtr<Value> ListValue::resolve_without_cast(RefPtr<Shell> shell)
- {
- NonnullRefPtrVector<Value> values;
- for (auto& value : m_contained_values)
- values.append(value.resolve_without_cast(shell));
- NonnullRefPtr<Value> value = create<ListValue>(move(values));
- if (!m_slices.is_empty())
- value = value->with_slices(m_slices);
- return value;
- }
- CommandValue::~CommandValue()
- {
- }
- CommandSequenceValue::~CommandSequenceValue()
- {
- }
- Vector<String> CommandSequenceValue::resolve_as_list(RefPtr<Shell> shell)
- {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "Unexpected cast of a command sequence to a list");
- return {};
- }
- Vector<Command> CommandSequenceValue::resolve_as_commands(RefPtr<Shell>)
- {
- return m_contained_values;
- }
- Vector<String> CommandValue::resolve_as_list(RefPtr<Shell> shell)
- {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "Unexpected cast of a command to a list");
- return {};
- }
- Vector<Command> CommandValue::resolve_as_commands(RefPtr<Shell>)
- {
- return { m_command };
- }
- JobValue::~JobValue()
- {
- }
- StringValue::~StringValue()
- {
- }
- Vector<String> StringValue::resolve_as_list(RefPtr<Shell> shell)
- {
- if (is_list()) {
- auto parts = StringView(m_string).split_view(m_split, m_keep_empty);
- Vector<String> result;
- result.ensure_capacity(parts.size());
- for (auto& part : parts)
- result.append(part);
- return resolve_slices(shell, move(result), m_slices);
- }
- return { resolve_slices(shell, String { m_string }, m_slices) };
- }
- NonnullRefPtr<Value> StringValue::resolve_without_cast(RefPtr<Shell> shell)
- {
- if (is_list())
- return create<AST::ListValue>(resolve_as_list(shell)); // No need to reapply the slices.
- return *this;
- }
- GlobValue::~GlobValue()
- {
- }
- Vector<String> GlobValue::resolve_as_list(RefPtr<Shell> shell)
- {
- if (!shell)
- return { resolve_slices(shell, String { m_glob }, m_slices) };
- auto results = shell->expand_globs(m_glob, shell->cwd);
- if (results.is_empty())
- shell->raise_error(Shell::ShellError::InvalidGlobError, "Glob did not match anything!", m_generation_position);
- return resolve_slices(shell, move(results), m_slices);
- }
- SimpleVariableValue::~SimpleVariableValue()
- {
- }
- Vector<String> SimpleVariableValue::resolve_as_list(RefPtr<Shell> shell)
- {
- if (!shell)
- return resolve_slices(shell, Vector<String> {}, m_slices);
- if (auto value = resolve_without_cast(shell); value != this)
- return value->resolve_as_list(shell);
- char* env_value = getenv(m_name.characters());
- if (env_value == nullptr)
- return { resolve_slices(shell, "", m_slices) };
- return { resolve_slices(shell, String { env_value }, m_slices) };
- }
- NonnullRefPtr<Value> SimpleVariableValue::resolve_without_cast(RefPtr<Shell> shell)
- {
- VERIFY(shell);
- if (auto value = shell->lookup_local_variable(m_name)) {
- auto result = value.release_nonnull();
- // If a slice is applied, add it.
- if (!m_slices.is_empty())
- result = result->with_slices(m_slices);
- return result;
- }
- return *this;
- }
- SpecialVariableValue::~SpecialVariableValue()
- {
- }
- Vector<String> SpecialVariableValue::resolve_as_list(RefPtr<Shell> shell)
- {
- if (!shell)
- return {};
- switch (m_name) {
- case '?':
- return { resolve_slices(shell, String::number(shell->last_return_code), m_slices) };
- case '$':
- return { resolve_slices(shell, String::number(getpid()), m_slices) };
- case '*':
- if (auto argv = shell->lookup_local_variable("ARGV"))
- return resolve_slices(shell, argv->resolve_as_list(shell), m_slices);
- return resolve_slices(shell, Vector<String> {}, m_slices);
- case '#':
- if (auto argv = shell->lookup_local_variable("ARGV")) {
- if (argv->is_list()) {
- auto list_argv = static_cast<AST::ListValue*>(argv.ptr());
- return { resolve_slices(shell, String::number(list_argv->values().size()), m_slices) };
- }
- return { resolve_slices(shell, "1", m_slices) };
- }
- return { resolve_slices(shell, "0", m_slices) };
- default:
- return { resolve_slices(shell, "", m_slices) };
- }
- }
- TildeValue::~TildeValue()
- {
- }
- Vector<String> TildeValue::resolve_as_list(RefPtr<Shell> shell)
- {
- StringBuilder builder;
- builder.append("~");
- builder.append(m_username);
- if (!shell)
- return { resolve_slices(shell, builder.to_string(), m_slices) };
- return { resolve_slices(shell, shell->expand_tilde(builder.to_string()), m_slices) };
- }
- Result<NonnullRefPtr<Rewiring>, String> CloseRedirection::apply() const
- {
- return adopt_ref(*new Rewiring(fd, fd, Rewiring::Close::ImmediatelyCloseNew));
- }
- CloseRedirection::~CloseRedirection()
- {
- }
- Result<NonnullRefPtr<Rewiring>, String> PathRedirection::apply() const
- {
- auto check_fd_and_return = [my_fd = this->fd](int fd, const String& path) -> Result<NonnullRefPtr<Rewiring>, String> {
- if (fd < 0) {
- String error = strerror(errno);
- dbgln("open() failed for '{}' with {}", path, error);
- return error;
- }
- return adopt_ref(*new Rewiring(fd, my_fd, Rewiring::Close::Old));
- };
- switch (direction) {
- case AST::PathRedirection::WriteAppend:
- return check_fd_and_return(open(path.characters(), O_WRONLY | O_CREAT | O_APPEND, 0666), path);
- case AST::PathRedirection::Write:
- return check_fd_and_return(open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666), path);
- case AST::PathRedirection::Read:
- return check_fd_and_return(open(path.characters(), O_RDONLY), path);
- case AST::PathRedirection::ReadWrite:
- return check_fd_and_return(open(path.characters(), O_RDWR | O_CREAT, 0666), path);
- }
- VERIFY_NOT_REACHED();
- }
- PathRedirection::~PathRedirection()
- {
- }
- FdRedirection::~FdRedirection()
- {
- }
- Redirection::~Redirection()
- {
- }
- }
|