12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851 |
- /*
- * Copyright (c) 2020, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include "AST.h"
- #include "Shell.h"
- #include <AK/DeprecatedString.h>
- #include <AK/MemoryStream.h>
- #include <AK/ScopeGuard.h>
- #include <AK/ScopedValueRollback.h>
- #include <AK/StringBuilder.h>
- #include <AK/URL.h>
- #include <LibCore/DeprecatedFile.h>
- #include <LibCore/EventLoop.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <unistd.h>
- ErrorOr<void> AK::Formatter<Shell::AST::Command>::format(FormatBuilder& builder, Shell::AST::Command const& 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()) {
- TRY(builder.put_literal("(ShellInternal)"sv));
- } else {
- bool first = true;
- for (auto& arg : value.argv) {
- if (!first)
- TRY(builder.put_literal(" "sv));
- first = false;
- TRY(builder.put_literal(arg));
- }
- }
- for (auto& redir : value.redirections) {
- TRY(builder.put_padding(' ', 1));
- if (redir.is_path_redirection()) {
- auto path_redir = (Shell::AST::PathRedirection const*)&redir;
- TRY(builder.put_i64(path_redir->fd));
- switch (path_redir->direction) {
- case Shell::AST::PathRedirection::Read:
- TRY(builder.put_literal("<"sv));
- break;
- case Shell::AST::PathRedirection::Write:
- TRY(builder.put_literal(">"sv));
- break;
- case Shell::AST::PathRedirection::WriteAppend:
- TRY(builder.put_literal(">>"sv));
- break;
- case Shell::AST::PathRedirection::ReadWrite:
- TRY(builder.put_literal("<>"sv));
- break;
- }
- TRY(builder.put_literal(path_redir->path));
- } else if (redir.is_fd_redirection()) {
- auto* fdredir = (Shell::AST::FdRedirection const*)&redir;
- TRY(builder.put_i64(fdredir->new_fd));
- TRY(builder.put_literal(">"sv));
- TRY(builder.put_i64(fdredir->old_fd));
- } else if (redir.is_close_redirection()) {
- auto close_redir = (Shell::AST::CloseRedirection const*)&redir;
- TRY(builder.put_i64(close_redir->fd));
- TRY(builder.put_literal(">&-"sv));
- } else {
- VERIFY_NOT_REACHED();
- }
- }
- if (!value.next_chain.is_empty()) {
- for (auto& command : value.next_chain) {
- switch (command.action) {
- case Shell::AST::NodeWithAction::And:
- TRY(builder.put_literal(" && "sv));
- break;
- case Shell::AST::NodeWithAction::Or:
- TRY(builder.put_literal(" || "sv));
- break;
- case Shell::AST::NodeWithAction::Sequence:
- TRY(builder.put_literal("; "sv));
- break;
- }
- TRY(builder.put_literal("("sv));
- TRY(builder.put_literal(command.node->class_name()));
- TRY(builder.put_literal("...)"sv));
- }
- }
- if (!value.should_wait)
- TRY(builder.put_literal("&"sv));
- return {};
- }
- namespace Shell::AST {
- static inline void print_indented(StringView str, int indent)
- {
- dbgln("{}{}", DeprecatedString::repeated(' ', indent * 2), str);
- }
- static inline Optional<Position> merge_positions(Optional<Position> const& left, Optional<Position> const& 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.extend(last_in_left.argv);
- command.argv.extend(first_in_right.argv);
- command.redirections.extend(last_in_left.redirections);
- command.redirections.extend(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.extend(left);
- commands.append(command);
- commands.extend(right);
- return commands;
- }
- static DeprecatedString resolve_slices(RefPtr<Shell> shell, DeprecatedString&& input_value, NonnullRefPtrVector<Slice> slices)
- {
- if (slices.is_empty())
- return move(input_value);
- for (auto& slice : slices) {
- auto value = slice.run(shell);
- if (shell && shell->has_any_error())
- break;
- 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, DeprecatedString::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, DeprecatedString::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.to_deprecated_string();
- }
- return move(input_value);
- }
- static Vector<DeprecatedString> resolve_slices(RefPtr<Shell> shell, Vector<DeprecatedString>&& values, NonnullRefPtrVector<Slice> slices)
- {
- if (slices.is_empty())
- return move(values);
- for (auto& slice : slices) {
- auto value = slice.run(shell);
- if (shell && shell->has_any_error())
- break;
- 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, DeprecatedString::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, DeprecatedString::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<DeprecatedString> 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(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 (shell && shell->has_any_error())
- return;
- 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(make_ref_counted<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(DeprecatedString::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, HitTestResult const& hit_test_result) const
- {
- auto matching_node = hit_test_result.matching_node;
- if (matching_node) {
- auto kind = matching_node->kind();
- StringLiteral::EnclosureType enclosure_type = StringLiteral::EnclosureType::None;
- if (kind == Kind::StringLiteral)
- enclosure_type = static_cast<StringLiteral const*>(matching_node.ptr())->enclosure_type();
- auto set_results_trivia = [enclosure_type](Vector<Line::CompletionSuggestion>&& suggestions) {
- if (enclosure_type != StringLiteral::EnclosureType::None) {
- for (auto& entry : suggestions)
- entry.trailing_trivia = { static_cast<u32>(enclosure_type == StringLiteral::EnclosureType::SingleQuotes ? '\'' : '"') };
- }
- return suggestions;
- };
- if (kind == Kind::BarewordLiteral || kind == Kind::StringLiteral) {
- Shell::EscapeMode escape_mode;
- StringView text;
- size_t corrected_offset;
- if (kind == Kind::BarewordLiteral) {
- auto* node = static_cast<BarewordLiteral const*>(matching_node.ptr());
- text = node->text();
- escape_mode = Shell::EscapeMode::Bareword;
- corrected_offset = find_offset_into_node(text, offset - matching_node->position().start_offset, escape_mode);
- } else {
- auto* node = static_cast<StringLiteral const*>(matching_node.ptr());
- text = node->text();
- escape_mode = enclosure_type == StringLiteral::EnclosureType::SingleQuotes ? Shell::EscapeMode::SingleQuotedString : Shell::EscapeMode::DoubleQuotedString;
- corrected_offset = find_offset_into_node(text, offset - matching_node->position().start_offset + 1, escape_mode);
- }
- if (corrected_offset > text.length())
- return {};
- // If the literal isn't an option, treat it as a path.
- if (!(text.starts_with('-') || text == "--" || text == "-"))
- return set_results_trivia(shell.complete_path(""sv, text, corrected_offset, Shell::ExecutableOnly::No, hit_test_result.closest_command_node.ptr(), hit_test_result.matching_node, escape_mode));
- // 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 {};
- DeprecatedString program_name;
- if (program_name_node->is_bareword())
- program_name = static_cast<BarewordLiteral const*>(program_name_node.ptr())->text();
- else
- program_name = static_cast<StringLiteral const*>(program_name_node.ptr())->text();
- return set_results_trivia(shell.complete_option(program_name, text, corrected_offset, hit_test_result.closest_command_node.ptr(), hit_test_result.matching_node));
- }
- return {};
- }
- auto result = hit_test_position(offset);
- if (!result.matching_node)
- return shell.complete_path(""sv, ""sv, 0, Shell::ExecutableOnly::No, result.closest_command_node.ptr(), nullptr, Shell::EscapeMode::Bareword);
- 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 });
- }
- 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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- commands.last().next_chain.append(NodeWithAction { *m_right, NodeWithAction::And });
- return make_ref_counted<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());
- }
- 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 (shell && shell->has_any_error())
- break;
- if (!result) {
- result = make_ref_counted<ListValue>({ element->run(shell)->resolve_without_cast(shell) });
- continue;
- }
- auto element_value = element->run(shell)->resolve_without_cast(shell);
- if (shell && shell->has_any_error())
- break;
- 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 = make_ref_counted<CommandValue>(command);
- } else {
- result = make_ref_counted<CommandSequenceValue>(move(joined_commands));
- }
- } else {
- NonnullRefPtrVector<Value> values;
- if (result->is_list_without_resolution()) {
- values.extend(static_cast<ListValue*>(result.ptr())->values());
- } else {
- for (auto& result : result->resolve_as_list(shell))
- values.append(make_ref_counted<StringValue>(result));
- }
- values.append(element_value);
- result = make_ref_counted<ListValue>(move(values));
- }
- }
- if (!result)
- return make_ref_counted<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 (shell && shell->has_any_error())
- break;
- 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 const> 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;
- }
- }
- }
- 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 make_ref_counted<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());
- }
- void BarewordLiteral::dump(int level) const
- {
- Node::dump(level);
- print_indented(m_text, level + 1);
- }
- RefPtr<Value> BarewordLiteral::run(RefPtr<Shell>)
- {
- return make_ref_counted<StringValue>(m_text);
- }
- void BarewordLiteral::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
- {
- if (metadata.is_first_in_list) {
- auto runnable = shell.runnable_path_for(m_text);
- if (runnable.has_value()) {
- Line::Style bold = { Line::Style::Bold };
- Line::Style style = bold;
- #ifdef AK_OS_SERENITY
- if (runnable->kind == Shell::RunnablePath::Kind::Executable || runnable->kind == Shell::RunnablePath::Kind::Alias) {
- auto name = shell.help_path_for({}, *runnable);
- if (name.has_value()) {
- auto url = URL::create_with_help_scheme(name.release_value(), shell.hostname);
- style = bold.unified_with(Line::Style::Hyperlink(url.to_deprecated_string()));
- }
- }
- #endif
- editor.stylize({ m_position.start_offset, m_position.end_offset }, style);
- } 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("--"sv)) {
- auto index = m_text.find('=').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::DeprecatedFile::exists(m_text)) {
- auto realpath = shell.resolve_path(m_text);
- auto url = URL::create_with_file_scheme(realpath);
- url.set_host(shell.hostname);
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Hyperlink(url.to_deprecated_string()) });
- }
- }
- BarewordLiteral::BarewordLiteral(Position position, DeprecatedString text)
- : Node(move(position))
- , m_text(move(text))
- {
- }
- 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) {
- if (shell && shell->has_any_error())
- break;
- auto value = entry.run(shell);
- if (value)
- values.append(value.release_nonnull());
- }
- return make_ref_counted<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;
- }
- }
- }
- 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 (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- if (value->is_command())
- return value;
- auto argv = value->resolve_as_list(shell);
- return make_ref_counted<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;
- if (!result.closest_command_node && position().contains(offset))
- result.closest_command_node = this;
- return result;
- }
- Vector<Line::CompletionSuggestion> CastToCommand::complete_for_editor(Shell& shell, size_t offset, HitTestResult const& hit_test_result) const
- {
- 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 const*>(matching_node.ptr());
- if (corrected_offset > node->text().length())
- return {};
- return shell.complete_program_name(node->text(), corrected_offset);
- }
- RefPtr<Node const> 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());
- }
- void CastToList::dump(int level) const
- {
- Node::dump(level);
- if (m_inner)
- m_inner->dump(level + 1);
- else
- print_indented("(empty)"sv, level + 1);
- }
- RefPtr<Value> CastToList::run(RefPtr<Shell> shell)
- {
- if (!m_inner)
- return make_ref_counted<ListValue>({});
- auto inner_value = m_inner->run(shell)->resolve_without_cast(shell);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- 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(make_ref_counted<StringValue>(value));
- return make_ref_counted<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 const> 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());
- }
- void CloseFdRedirection::dump(int level) const
- {
- Node::dump(level);
- print_indented(DeprecatedString::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 make_ref_counted<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(DeprecatedString::formatted("(Generated command literal: {})", m_command), level + 1);
- }
- RefPtr<Value> CommandLiteral::run(RefPtr<Shell>)
- {
- return make_ref_counted<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 make_ref_counted<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, DeprecatedString 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)"sv : "(Break)"sv, 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 make_ref_counted<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(""sv, values);
- return make_ref_counted<StringValue>(builder.to_deprecated_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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- // 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 make_ref_counted<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 make_ref_counted<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(DeprecatedString::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 make_ref_counted<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(DeprecatedString::formatted("(name: {})\n", m_name.name), level + 1);
- print_indented("(argument names)"sv, level + 1);
- for (auto& arg : m_arguments)
- print_indented(DeprecatedString::formatted("(name: {})\n", arg.name), level + 2);
- print_indented("(body)"sv, level + 1);
- if (m_block)
- m_block->dump(level + 2);
- else
- print_indented("(null)"sv, level + 2);
- }
- RefPtr<Value> FunctionDeclaration::run(RefPtr<Shell> shell)
- {
- Vector<DeprecatedString> args;
- for (auto& arg : m_arguments)
- args.append(arg.name);
- shell->define_function(m_name.name, move(args), m_block);
- return make_ref_counted<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, HitTestResult const& hit_test_result) const
- {
- 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 const*>(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.extend(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(DeprecatedString::formatted("iterating with {} in", m_variable->name), level + 1);
- if (m_index_variable.has_value())
- print_indented(DeprecatedString::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)"sv, level + 2);
- print_indented("Running"sv, level + 1);
- if (m_block)
- m_block->dump(level + 2);
- else
- print_indented("(null)"sv, level + 2);
- }
- RefPtr<Value> ForLoop::run(RefPtr<Shell> shell)
- {
- if (!m_block)
- return make_ref_counted<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_any_error() && !shell->has_error(Shell::ShellError::InternalControlFlowInterrupted))
- 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 (shell->has_any_error()) {
- if (shell->has_error(Shell::ShellError::InternalControlFlowInterrupted))
- ++consecutive_interruptions;
- if (shell->has_error(Shell::ShellError::InternalControlFlowKilled))
- return IterationDecision::Break;
- }
- }
- 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;
- if (shell) {
- if (shell->has_error(Shell::ShellError::InternalControlFlowInterrupted))
- shell->take_error();
- if (shell->has_any_error())
- return IterationDecision::Break;
- }
- RefPtr<Value> block_value;
- {
- auto frame = shell->push_frame(DeprecatedString::formatted("for ({})", this));
- shell->set_local_variable(variable_name, value, true);
- if (index_name.has_value())
- shell->set_local_variable(index_name.value(), make_ref_counted<AST::StringValue>(DeprecatedString::number(i)), true);
- ++i;
- block_value = m_block->run(shell);
- }
- return run(block_value);
- });
- } else {
- for (;;) {
- if (consecutive_interruptions >= 2)
- break;
- if (shell) {
- if (shell->has_error(Shell::ShellError::InternalControlFlowInterrupted))
- shell->take_error();
- if (shell->has_any_error())
- break;
- }
- RefPtr<Value> block_value = m_block->run(shell);
- if (run(block_value) == IterationDecision::Break)
- break;
- }
- }
- return make_ref_counted<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 make_ref_counted<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, DeprecatedString text)
- : Node(move(position))
- , m_text(move(text))
- {
- }
- Glob::~Glob()
- {
- }
- void Heredoc::dump(int level) const
- {
- Node::dump(level);
- print_indented("(End Key)"sv, level + 1);
- print_indented(m_end, level + 2);
- print_indented("(Allows Interpolation)"sv, level + 1);
- print_indented(DeprecatedString::formatted("{}", m_allows_interpolation), level + 2);
- if (!evaluates_to_string()) {
- print_indented("(Target FD)"sv, level + 1);
- print_indented(DeprecatedString::number(*m_target_fd), level + 2);
- }
- print_indented("(Contents)"sv, level + 1);
- if (m_contents)
- m_contents->dump(level + 2);
- else
- print_indented("(null)"sv, level + 2);
- }
- RefPtr<Value> Heredoc::run(RefPtr<Shell> shell)
- {
- if (!m_contents) {
- if (shell)
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "Attempt to evaluate an unresolved heredoc"sv, position());
- return nullptr;
- }
- auto value = [&]() -> RefPtr<Value> {
- if (!m_deindent)
- return m_contents->run(shell);
- // To deindent, first split to lines...
- auto value = m_contents->run(shell);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- 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 make_ref_counted<StringValue>(builder.to_deprecated_string());
- }();
- if (evaluates_to_string())
- return value;
- int fds[2];
- auto rc = pipe(fds);
- if (rc != 0) {
- // pipe() failed for {}
- if (shell)
- shell->raise_error(Shell::ShellError::PipeFailure, DeprecatedString::formatted("heredoc: {}", strerror(errno)), position());
- return nullptr;
- }
- auto read_end = fds[0];
- auto write_end = fds[1];
- // Dump all of 'value' into the pipe.
- auto* file = fdopen(write_end, "wb");
- if (!file) {
- if (shell)
- shell->raise_error(Shell::ShellError::OpenFailure, "heredoc"sv, position());
- return nullptr;
- }
- auto text = value->resolve_as_string(shell);
- auto written = fwrite(text.characters(), 1, text.length(), file);
- fflush(file);
- if (written != text.length()) {
- if (shell)
- shell->raise_error(Shell::ShellError::WriteFailure, "heredoc"sv, position());
- }
- fclose(file);
- Command command;
- command.position = position();
- command.redirections.append(FdRedirection::create(read_end, *target_fd(), Rewiring::Close::None));
- return make_ref_counted<CommandValue>(move(command));
- }
- 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, DeprecatedString end, bool allow_interpolation, bool deindent, Optional<int> target_fd)
- : Node(move(position))
- , m_end(move(end))
- , m_allows_interpolation(allow_interpolation)
- , m_deindent(deindent)
- , m_target_fd(target_fd)
- {
- }
- Heredoc::~Heredoc()
- {
- }
- void HistoryEvent::dump(int level) const
- {
- Node::dump(level);
- print_indented("Event Selector"sv, level + 1);
- switch (m_selector.event.kind) {
- case HistorySelector::EventKind::IndexFromStart:
- print_indented("IndexFromStart"sv, level + 2);
- break;
- case HistorySelector::EventKind::IndexFromEnd:
- print_indented("IndexFromEnd"sv, level + 2);
- break;
- case HistorySelector::EventKind::ContainingStringLookup:
- print_indented("ContainingStringLookup"sv, level + 2);
- break;
- case HistorySelector::EventKind::StartingStringLookup:
- print_indented("StartingStringLookup"sv, level + 2);
- break;
- }
- print_indented(DeprecatedString::formatted("{}({})", m_selector.event.index, m_selector.event.text), level + 3);
- print_indented("Word Selector"sv, level + 1);
- auto print_word_selector = [&](HistorySelector::WordSelector const& selector) {
- switch (selector.kind) {
- case HistorySelector::WordSelectorKind::Index:
- print_indented(DeprecatedString::formatted("Index {}", selector.selector), level + 3);
- break;
- case HistorySelector::WordSelectorKind::Last:
- print_indented(DeprecatedString::formatted("Last"), level + 3);
- break;
- }
- };
- if (m_selector.word_selector_range.end.has_value()) {
- print_indented("Range Start"sv, level + 2);
- print_word_selector(m_selector.word_selector_range.start);
- print_indented("Range End"sv, level + 2);
- print_word_selector(m_selector.word_selector_range.end.value());
- } else {
- print_indented("Direct Address"sv, level + 2);
- print_word_selector(m_selector.word_selector_range.start);
- }
- }
- RefPtr<Value> HistoryEvent::run(RefPtr<Shell> shell)
- {
- if (!shell)
- return make_ref_counted<AST::ListValue>({});
- auto editor = shell->editor();
- if (!editor) {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "No history available!", position());
- return make_ref_counted<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.
- DeprecatedString 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 make_ref_counted<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 make_ref_counted<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 make_ref_counted<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 make_ref_counted<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 make_ref_counted<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 make_ref_counted<AST::ListValue>({});
- }
- decltype(nodes) resolved_nodes;
- resolved_nodes.append(nodes.data() + start_index, end_index - start_index + 1);
- NonnullRefPtr<AST::Node> list = make_ref_counted<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 make_ref_counted<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)"sv, 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 unexpanded_commands = m_command->run(shell)->resolve_as_commands(shell);
- if (shell && shell->has_any_error())
- return;
- if (!shell)
- return;
- auto commands = shell->expand_aliases(move(unexpanded_commands));
- if (m_capture_stdout) {
- // Make sure that we're going to be running _something_.
- auto has_one_command = false;
- for (auto& command : commands) {
- if (command.argv.is_empty() && !command.pipeline && command.next_chain.is_empty())
- continue;
- has_one_command = true;
- break;
- }
- if (!has_one_command) {
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "Cannot capture standard output when no command is being executed", m_position);
- return;
- }
- 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);
- AllocatingMemoryStream stream;
- enum {
- Continue,
- Break,
- NothingLeft,
- };
- auto check_and_call = [&] {
- auto ifs = shell->local_variable_or("IFS"sv, "\n"sv);
- if (auto offset = stream.offset_of(ifs.bytes()).release_value_but_fixme_should_propagate_errors(); offset.has_value()) {
- auto line_end = offset.value();
- if (line_end == 0) {
- stream.discard(ifs.length()).release_value_but_fixme_should_propagate_errors();
- if (shell->options.inline_exec_keep_empty_segments)
- if (callback(make_ref_counted<StringValue>("")) == IterationDecision::Break) {
- loop.quit(Break);
- notifier->set_enabled(false);
- return Break;
- }
- } else {
- auto entry_result = ByteBuffer::create_uninitialized(line_end + ifs.length());
- if (entry_result.is_error()) {
- loop.quit(Break);
- notifier->set_enabled(false);
- return Break;
- }
- auto entry = entry_result.release_value();
- stream.read_entire_buffer(entry).release_value_but_fixme_should_propagate_errors();
- auto str = StringView(entry.data(), entry.size() - ifs.length());
- if (callback(make_ref_counted<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_entire_buffer({ buffer, (size_t)read_size }).release_value_but_fixme_should_propagate_errors();
- }
- 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.is_eof()) {
- auto action = Continue;
- do {
- action = check_and_call();
- if (action == Break)
- return;
- } while (action == Continue);
- if (!stream.is_eof()) {
- auto entry_result = ByteBuffer::create_uninitialized(stream.used_buffer_size());
- if (entry_result.is_error()) {
- shell->raise_error(Shell::ShellError::OutOfMemory, {}, position());
- return;
- }
- auto entry = entry_result.release_value();
- stream.read_entire_buffer(entry).release_value_but_fixme_should_propagate_errors();
- callback(make_ref_counted<StringValue>(DeprecatedString::copy(entry)));
- }
- }
- return;
- }
- auto jobs = shell->run_commands(commands);
- if (!jobs.is_empty())
- callback(make_ref_counted<JobValue>(&jobs.last()));
- }
- RefPtr<Value> Execute::run(RefPtr<Shell> shell)
- {
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- 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 make_ref_counted<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, HitTestResult const& hit_test_result) const
- {
- 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 const*>(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"sv, ++level);
- m_condition->dump(level + 1);
- print_indented("True Branch"sv, level);
- if (m_true_branch)
- m_true_branch->dump(level + 1);
- else
- print_indented("(empty)"sv, level + 1);
- print_indented("False Branch"sv, level);
- if (m_false_branch)
- m_false_branch->dump(level + 1);
- else
- print_indented("(empty)"sv, level + 1);
- }
- RefPtr<Value> IfCond::run(RefPtr<Shell> shell)
- {
- auto cond = m_condition->run(shell)->resolve_without_cast(shell);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- // The condition could be a builtin, in which case it has already run and exited.
- if (cond->is_job()) {
- auto cond_job_value = static_cast<JobValue const*>(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 make_ref_counted<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 = make_ref_counted<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)"sv, level + 1);
- print_indented(m_function.name, level + 2);
- print_indented("(arguments)"sv, 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 make_ref_counted<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, HitTestResult const& hit_test_result) const
- {
- 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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- if (left.last().should_wait && !left.last().next_chain.is_empty()) {
- // Join (C0s*; C1) X -> (C0s*; Join C1 X)
- auto& lhs_node = left.last().next_chain.last().node;
- lhs_node = make_ref_counted<Join>(m_position, lhs_node, m_right);
- return make_ref_counted<CommandSequenceValue>(move(left));
- }
- auto right = m_right->to_lazy_evaluated_commands(shell);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- return make_ref_counted<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 const> 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(DeprecatedString::formatted("(expression: {})", m_expr_name.characters()), level + 1);
- m_matched_expr->dump(level + 2);
- print_indented(DeprecatedString::formatted("(named: {})", m_expr_name.characters()), level + 1);
- print_indented("(entries)"sv, level + 1);
- for (auto& entry : m_entries) {
- StringBuilder builder;
- builder.append("(match"sv);
- if (entry.match_names.has_value()) {
- builder.append(" to names ("sv);
- bool first = true;
- for (auto& name : entry.match_names.value()) {
- if (!first)
- builder.append(' ');
- first = false;
- builder.append(name);
- }
- builder.append("))"sv);
- } else {
- builder.append(')');
- }
- print_indented(builder.string_view(), level + 2);
- entry.options.visit(
- [&](NonnullRefPtrVector<Node> const& options) {
- for (auto& option : options)
- option.dump(level + 3);
- },
- [&](Vector<Regex<ECMA262>> const& options) {
- for (auto& option : options)
- print_indented(DeprecatedString::formatted("(regex: {})", option.pattern_value), level + 3);
- });
- print_indented("(execute)"sv, level + 2);
- if (entry.body)
- entry.body->dump(level + 3);
- else
- print_indented("(nothing)"sv, level + 3);
- }
- }
- RefPtr<Value> MatchExpr::run(RefPtr<Shell> shell)
- {
- auto value = m_matched_expr->run(shell)->resolve_without_cast(shell);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- auto list = value->resolve_as_list(shell);
- auto list_matches = [&](auto&& pattern, auto& spans) {
- if constexpr (IsSame<RemoveCVReference<decltype(pattern)>, Regex<ECMA262>>) {
- if (list.size() != 1)
- return false;
- auto& subject = list.first();
- auto match = pattern.match(subject);
- if (!match.success)
- return false;
- spans.ensure_capacity(match.n_capture_groups);
- for (size_t i = 0; i < match.n_capture_groups; ++i) {
- auto& capture = match.capture_group_matches[0][i];
- spans.append(capture.view.to_deprecated_string());
- }
- return true;
- } else {
- 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) -> decltype(auto) {
- if constexpr (IsSame<RemoveCVReference<decltype(option)>, Regex<ECMA262>>) {
- return option;
- } else {
- Vector<DeprecatedString> 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);
- if (shell && shell->has_any_error())
- return pattern;
- option.for_each_entry(shell, [&](auto&& value) {
- pattern.extend(value->resolve_as_list(nullptr)); // Note: 'nullptr' incurs special behavior,
- // asking the node for a 'raw' value.
- return IterationDecision::Continue;
- });
- }
- return pattern;
- }
- };
- auto frame = shell->push_frame(DeprecatedString::formatted("match ({})", this));
- if (!m_expr_name.is_empty())
- shell->set_local_variable(m_expr_name, value, true);
- for (auto& entry : m_entries) {
- auto result = entry.options.visit([&](auto& options) -> Variant<IterationDecision, RefPtr<Value>> {
- for (auto& option : options) {
- Vector<DeprecatedString> 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, make_ref_counted<AST::StringValue>(spans[i]), true);
- ++i;
- }
- }
- return entry.body->run(shell);
- }
- return RefPtr<Value>(make_ref_counted<AST::ListValue>({}));
- }
- }
- return IterationDecision::Continue;
- });
- if (result.has<IterationDecision>() && result.get<IterationDecision>() == IterationDecision::Break)
- break;
- if (result.has<RefPtr<Value>>())
- return move(result).get<RefPtr<Value>>();
- }
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "Non-exhaustive match rules!", position());
- return make_ref_counted<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;
- entry.options.visit(
- [&](NonnullRefPtrVector<Node>& node_options) {
- for (auto& option : node_options)
- option.highlight_in_editor(editor, shell, metadata);
- },
- [](auto&) {});
- 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, DeprecatedString 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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- commands.last().next_chain.empend(*m_right, NodeWithAction::Or);
- return make_ref_counted<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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- auto right = m_right->to_lazy_evaluated_commands(shell);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- 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 = make_ref_counted<Pipeline>();
- last_in_left.pipeline = pipeline;
- first_in_right.pipeline = pipeline;
- }
- Vector<Command> commands;
- commands.extend(left);
- commands.append(last_in_left);
- commands.append(first_in_right);
- commands.extend(right);
- return make_ref_counted<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 = DeprecatedString::formatted("{}/{}", shell.cwd, path);
- auto url = URL::create_with_file_scheme(path);
- url.set_host(shell.hostname);
- editor.stylize({ position.start_offset, position.end_offset }, { Line::Style::Hyperlink(url.to_deprecated_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, HitTestResult const& hit_test_result) const
- {
- 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 const*>(matching_node.ptr());
- if (corrected_offset > node->text().length())
- return {};
- return shell.complete_path(""sv, node->text(), corrected_offset, Shell::ExecutableOnly::No, nullptr, nullptr);
- }
- PathRedirectionNode::~PathRedirectionNode()
- {
- }
- void Range::dump(int level) const
- {
- Node::dump(level);
- print_indented("(From)"sv, level + 1);
- m_start->dump(level + 2);
- print_indented("(To)"sv, 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(make_ref_counted<StringValue>(builder.to_deprecated_string()));
- }
- // Append the ending code point too, most shells treat this as inclusive.
- builder.clear();
- builder.append_code_point(end_code_point);
- values.append(make_ref_counted<StringValue>(builder.to_deprecated_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(make_ref_counted<StringValue>(DeprecatedString::number(value)));
- // Append the range end too, most shells treat this as inclusive.
- values.append(make_ref_counted<StringValue>(DeprecatedString::number(end)));
- } else {
- goto yield_start_end;
- }
- }
- } else {
- yield_start_end:;
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, DeprecatedString::formatted("Cannot interpolate between '{}' and '{}'!", start_str, end_str), position);
- // We can't really interpolate between the two, so just yield both.
- values.append(make_ref_counted<StringValue>(move(start_str)));
- values.append(make_ref_counted<StringValue>(move(end_str)));
- }
- return values;
- }
- warnln("Shell: Cannot apply the requested interpolation");
- return values;
- };
- auto start_value = m_start->run(shell);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- auto end_value = m_end->run(shell);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- if (!start_value || !end_value)
- return make_ref_counted<ListValue>({});
- return make_ref_counted<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(DeprecatedString::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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- StringBuilder builder;
- builder.join(' ', path_segments);
- command.redirections.append(PathRedirection::create(builder.to_deprecated_string(), m_fd, PathRedirection::Read));
- return make_ref_counted<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(DeprecatedString::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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- StringBuilder builder;
- builder.join(' ', path_segments);
- command.redirections.append(PathRedirection::create(builder.to_deprecated_string(), m_fd, PathRedirection::ReadWrite));
- return make_ref_counted<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 (shell && shell->has_any_error())
- break;
- if (!last_command_in_sequence) {
- auto commands = entry.to_lazy_evaluated_commands(shell);
- all_commands.extend(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.extend(entry.to_lazy_evaluated_commands(shell));
- last_command_in_sequence = &all_commands.last();
- }
- }
- return make_ref_counted<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 {};
- }
- RefPtr<Node const> Sequence::leftmost_trivial_literal() const
- {
- for (auto& entry : m_entries) {
- if (auto node = entry.leftmost_trivial_literal())
- return node;
- }
- return nullptr;
- }
- 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 make_ref_counted<ListValue>({});
- return make_ref_counted<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, HitTestResult const& hit_test_result) const
- {
- // 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)"sv, level + 1);
- print_indented(m_name, level + 2);
- print_indented("(Slice)"sv, level + 1);
- if (m_slice)
- m_slice->dump(level + 2);
- else
- print_indented("(None)"sv, level + 2);
- }
- RefPtr<Value> SimpleVariable::run(RefPtr<Shell>)
- {
- NonnullRefPtr<Value> value = make_ref_counted<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 (!position().contains(offset))
- return {};
- 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, HitTestResult const& hit_test_result) const
- {
- 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, DeprecatedString name)
- : VariableNode(move(position))
- , m_name(move(name))
- {
- }
- SimpleVariable::~SimpleVariable()
- {
- }
- void SpecialVariable::dump(int level) const
- {
- Node::dump(level);
- print_indented("(Name)"sv, level + 1);
- print_indented(DeprecatedString { &m_name, 1 }, level + 1);
- print_indented("(Slice)"sv, level + 1);
- if (m_slice)
- m_slice->dump(level + 2);
- else
- print_indented("(None)"sv, level + 2);
- }
- RefPtr<Value> SpecialVariable::run(RefPtr<Shell>)
- {
- NonnullRefPtr<Value> value = make_ref_counted<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, HitTestResult const&) const
- {
- 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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- auto right_value = m_right->run(shell)->resolve_without_cast(shell);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- auto left = left_value->resolve_as_list(shell);
- auto right = right_value->resolve_as_list(shell);
- if (m_mode == Mode::StringExpand) {
- Vector<DeprecatedString> result;
- result.ensure_capacity(left.size() + right.size());
- for (auto& left_item : left)
- result.append(left_item);
- if (!result.is_empty() && !right.is_empty()) {
- auto& last = result.last();
- last = DeprecatedString::formatted("{}{}", last, right.first());
- right.take_first();
- }
- for (auto& right_item : right)
- result.append(right_item);
- return make_ref_counted<ListValue>(move(result));
- }
- 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 make_ref_counted<StringValue>(builder.to_deprecated_string());
- }
- // Otherwise, treat them as lists and create a list product (or just append).
- if (left.is_empty() || right.is_empty())
- return make_ref_counted<ListValue>({});
- Vector<DeprecatedString> 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_deprecated_string());
- builder.clear();
- }
- }
- return make_ref_counted<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_deprecated_string();
- if (Core::DeprecatedFile::exists(path)) {
- auto realpath = shell.resolve_path(path);
- auto url = URL::create_with_file_scheme(realpath);
- url.set_host(shell.hostname);
- editor.stylize({ m_position.start_offset, m_position.end_offset }, { Line::Style::Hyperlink(url.to_deprecated_string()) });
- }
- } else {
- m_right->highlight_in_editor(editor, shell, metadata);
- }
- }
- Vector<Line::CompletionSuggestion> Juxtaposition::complete_for_editor(Shell& shell, size_t offset, HitTestResult const& hit_test_result) const
- {
- auto matching_node = hit_test_result.matching_node;
- if (m_left->would_execute() || m_right->would_execute()) {
- return {};
- }
- // '~/foo/bar' is special, we have to actually resolve the tilde
- // then complete the bareword with that path prefix.
- auto left_values = m_left->run(shell)->resolve_as_list(shell);
- if (left_values.is_empty())
- return m_right->complete_for_editor(shell, offset, hit_test_result);
- auto& left_value = left_values.first();
- auto right_values = m_right->run(shell)->resolve_as_list(shell);
- StringView right_value {};
- auto corrected_offset = offset - matching_node->position().start_offset;
- if (!right_values.is_empty())
- right_value = right_values.first();
- if (m_left->is_tilde() && !right_value.is_empty()) {
- right_value = right_value.substring_view(1);
- corrected_offset--;
- }
- if (corrected_offset > right_value.length())
- return {};
- return shell.complete_path(left_value, right_value, corrected_offset, Shell::ExecutableOnly::No, hit_test_result.closest_command_node.ptr(), hit_test_result.matching_node);
- }
- 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, Juxtaposition::Mode mode)
- : Node(move(position))
- , m_left(move(left))
- , m_right(move(right))
- , m_mode(mode)
- {
- 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 make_ref_counted<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, DeprecatedString text, EnclosureType enclosure_type)
- : Node(move(position))
- , m_text(move(text))
- , m_enclosure_type(enclosure_type)
- {
- }
- 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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- auto right = m_right->run(shell)->resolve_as_list(shell);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- StringBuilder builder;
- builder.join(' ', left);
- builder.join(' ', right);
- return make_ref_counted<StringValue>(builder.to_deprecated_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)"sv, level + 1);
- print_indented(m_syntax_error_text, level + 2);
- print_indented("(Can be recovered from)"sv, level + 1);
- print_indented(DeprecatedString::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 make_ref_counted<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, DeprecatedString error, bool is_continuable)
- : Node(move(position))
- , m_syntax_error_text(move(error))
- , m_is_continuable(is_continuable)
- {
- }
- SyntaxError& SyntaxError::syntax_error_node()
- {
- 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 make_ref_counted<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, HitTestResult const& hit_test_result) const
- {
- 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);
- }
- DeprecatedString Tilde::text() const
- {
- StringBuilder builder;
- builder.append('~');
- builder.append(m_username);
- return builder.to_deprecated_string();
- }
- Tilde::Tilde(Position position, DeprecatedString 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(DeprecatedString::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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- StringBuilder builder;
- builder.join(' ', path_segments);
- command.redirections.append(PathRedirection::create(builder.to_deprecated_string(), m_fd, PathRedirection::WriteAppend));
- return make_ref_counted<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(DeprecatedString::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);
- if (shell && shell->has_any_error())
- return make_ref_counted<ListValue>({});
- StringBuilder builder;
- builder.join(' ', path_segments);
- command.redirections.append(PathRedirection::create(builder.to_deprecated_string(), m_fd, PathRedirection::Write));
- return make_ref_counted<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"sv, 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);
- if (shell && shell->has_any_error())
- break;
- VERIFY(name_value.size() == 1);
- auto name = name_value[0];
- auto value = var.value->run(shell);
- if (shell && shell->has_any_error())
- break;
- value = value->resolve_without_cast(shell);
- shell->set_local_variable(name, value.release_nonnull());
- }
- return make_ref_counted<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()
- {
- }
- DeprecatedString Value::resolve_as_string(RefPtr<Shell> shell)
- {
- if (shell)
- shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "Conversion to string not allowed");
- return {};
- }
- Vector<AST::Command> Value::resolve_as_commands(RefPtr<Shell> shell)
- {
- Command command;
- command.argv = resolve_as_list(shell);
- return { command };
- }
- ListValue::ListValue(Vector<DeprecatedString> 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.extend(move(slices));
- return value;
- }
- ListValue::~ListValue()
- {
- }
- Vector<DeprecatedString> ListValue::resolve_as_list(RefPtr<Shell> shell)
- {
- Vector<DeprecatedString> values;
- for (auto& value : m_contained_values)
- values.extend(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 = make_ref_counted<ListValue>(move(values));
- if (!m_slices.is_empty())
- value = value->with_slices(m_slices);
- return value;
- }
- CommandValue::~CommandValue()
- {
- }
- CommandSequenceValue::~CommandSequenceValue()
- {
- }
- Vector<DeprecatedString> 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<DeprecatedString> CommandValue::resolve_as_list(RefPtr<Shell>)
- {
- return m_command.argv;
- }
- Vector<Command> CommandValue::resolve_as_commands(RefPtr<Shell>)
- {
- return { m_command };
- }
- JobValue::~JobValue()
- {
- }
- StringValue::~StringValue()
- {
- }
- DeprecatedString StringValue::resolve_as_string(RefPtr<Shell> shell)
- {
- if (m_split.is_null())
- return m_string;
- return Value::resolve_as_string(shell);
- }
- Vector<DeprecatedString> StringValue::resolve_as_list(RefPtr<Shell> shell)
- {
- if (is_list()) {
- auto parts = StringView(m_string).split_view(m_split, m_keep_empty ? SplitBehavior::KeepEmpty : SplitBehavior::Nothing);
- Vector<DeprecatedString> 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, DeprecatedString { m_string }, m_slices) };
- }
- NonnullRefPtr<Value> StringValue::resolve_without_cast(RefPtr<Shell> shell)
- {
- if (is_list())
- return make_ref_counted<AST::ListValue>(resolve_as_list(shell)); // No need to reapply the slices.
- return *this;
- }
- GlobValue::~GlobValue()
- {
- }
- Vector<DeprecatedString> GlobValue::resolve_as_list(RefPtr<Shell> shell)
- {
- if (!shell)
- return { resolve_slices(shell, DeprecatedString { 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()
- {
- }
- DeprecatedString SimpleVariableValue::resolve_as_string(RefPtr<Shell> shell)
- {
- if (!shell)
- return resolve_slices(shell, DeprecatedString {}, m_slices);
- if (auto value = resolve_without_cast(shell); value != this)
- return value->resolve_as_string(shell);
- char* env_value = getenv(m_name.characters());
- return resolve_slices(shell, env_value, m_slices);
- }
- Vector<DeprecatedString> SimpleVariableValue::resolve_as_list(RefPtr<Shell> shell)
- {
- if (!shell)
- return resolve_slices(shell, Vector<DeprecatedString> {}, 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, DeprecatedString { 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 const_cast<Value&>(*result);
- }
- return *this;
- }
- SpecialVariableValue::~SpecialVariableValue()
- {
- }
- DeprecatedString SpecialVariableValue::resolve_as_string(RefPtr<Shell> shell)
- {
- if (!shell)
- return {};
- auto result = resolve_as_list(shell);
- if (result.size() == 1)
- return result[0];
- if (result.is_empty())
- return {};
- return Value::resolve_as_string(shell);
- }
- Vector<DeprecatedString> SpecialVariableValue::resolve_as_list(RefPtr<Shell> shell)
- {
- if (!shell)
- return {};
- switch (m_name) {
- case '?':
- return { resolve_slices(shell, DeprecatedString::number(shell->last_return_code.value_or(0)), m_slices) };
- case '$':
- return { resolve_slices(shell, DeprecatedString::number(getpid()), m_slices) };
- case '*':
- if (auto argv = shell->lookup_local_variable("ARGV"sv))
- return resolve_slices(shell, const_cast<Value&>(*argv).resolve_as_list(shell), m_slices);
- return resolve_slices(shell, Vector<DeprecatedString> {}, m_slices);
- case '#':
- if (auto argv = shell->lookup_local_variable("ARGV"sv)) {
- if (argv->is_list()) {
- auto list_argv = static_cast<AST::ListValue const*>(argv.ptr());
- return { resolve_slices(shell, DeprecatedString::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) };
- }
- }
- NonnullRefPtr<Value> SpecialVariableValue::resolve_without_cast(RefPtr<Shell> shell)
- {
- if (!shell)
- return *this;
- return make_ref_counted<ListValue>(resolve_as_list(shell));
- }
- TildeValue::~TildeValue()
- {
- }
- DeprecatedString TildeValue::resolve_as_string(RefPtr<Shell> shell)
- {
- return resolve_as_list(shell).first();
- }
- Vector<DeprecatedString> TildeValue::resolve_as_list(RefPtr<Shell> shell)
- {
- StringBuilder builder;
- builder.append('~');
- builder.append(m_username);
- if (!shell)
- return { resolve_slices(shell, builder.to_deprecated_string(), m_slices) };
- return { resolve_slices(shell, shell->expand_tilde(builder.to_deprecated_string()), m_slices) };
- }
- ErrorOr<NonnullRefPtr<Rewiring>> CloseRedirection::apply() const
- {
- return adopt_nonnull_ref_or_enomem(new (nothrow) Rewiring(fd, fd, Rewiring::Close::ImmediatelyCloseNew));
- }
- CloseRedirection::~CloseRedirection()
- {
- }
- ErrorOr<NonnullRefPtr<Rewiring>> PathRedirection::apply() const
- {
- auto check_fd_and_return = [my_fd = this->fd](int fd, DeprecatedString const& path) -> ErrorOr<NonnullRefPtr<Rewiring>> {
- if (fd < 0) {
- auto error = Error::from_errno(errno);
- dbgln("open() failed for '{}' with {}", path, error);
- return error;
- }
- return adopt_nonnull_ref_or_enomem(new (nothrow) 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()
- {
- }
- }
|