Shell.cpp 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Shell.h"
  7. #include "Execution.h"
  8. #include "Formatter.h"
  9. #include <AK/CharacterTypes.h>
  10. #include <AK/Debug.h>
  11. #include <AK/Function.h>
  12. #include <AK/GenericLexer.h>
  13. #include <AK/JsonParser.h>
  14. #include <AK/LexicalPath.h>
  15. #include <AK/QuickSort.h>
  16. #include <AK/ScopeGuard.h>
  17. #include <AK/ScopedValueRollback.h>
  18. #include <AK/StringBuilder.h>
  19. #include <AK/TemporaryChange.h>
  20. #include <AK/URL.h>
  21. #include <LibCore/DeprecatedFile.h>
  22. #include <LibCore/DirIterator.h>
  23. #include <LibCore/Event.h>
  24. #include <LibCore/EventLoop.h>
  25. #include <LibCore/File.h>
  26. #include <LibCore/System.h>
  27. #include <LibCore/Timer.h>
  28. #include <LibFileSystem/FileSystem.h>
  29. #include <LibLine/Editor.h>
  30. #include <Shell/PosixParser.h>
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <inttypes.h>
  34. #include <pwd.h>
  35. #include <signal.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sys/mman.h>
  40. #include <sys/stat.h>
  41. #include <sys/utsname.h>
  42. #include <sys/wait.h>
  43. #include <termios.h>
  44. #include <unistd.h>
  45. static bool s_disable_hyperlinks = false;
  46. extern char** environ;
  47. namespace Shell {
  48. void Shell::setup_signals()
  49. {
  50. if (m_should_reinstall_signal_handlers) {
  51. Core::EventLoop::register_signal(SIGCHLD, [this](int) {
  52. dbgln_if(SH_DEBUG, "SIGCHLD!");
  53. notify_child_event();
  54. });
  55. Core::EventLoop::register_signal(SIGTSTP, [this](auto) {
  56. auto job = current_job();
  57. kill_job(job, SIGTSTP);
  58. if (job) {
  59. job->set_is_suspended(true);
  60. job->unblock();
  61. }
  62. });
  63. }
  64. }
  65. void Shell::print_path(StringView path)
  66. {
  67. if (s_disable_hyperlinks || !m_is_interactive) {
  68. out("{}", path);
  69. return;
  70. }
  71. auto url = URL::create_with_file_scheme(path, {}, hostname);
  72. out("\033]8;;{}\033\\{}\033]8;;\033\\", url.serialize(), path);
  73. }
  74. DeprecatedString Shell::prompt() const
  75. {
  76. auto build_prompt = [&]() -> DeprecatedString {
  77. auto* ps1 = getenv("PROMPT");
  78. if (!ps1) {
  79. if (uid == 0)
  80. return "# ";
  81. StringBuilder builder;
  82. builder.appendff("\033]0;{}@{}:{}\007", username, hostname, cwd);
  83. builder.appendff("\033[31;1m{}\033[0m@\033[37;1m{}\033[0m:\033[32;1m{}\033[0m$> ", username, hostname, cwd);
  84. return builder.to_deprecated_string();
  85. }
  86. StringBuilder builder;
  87. for (char* ptr = ps1; *ptr; ++ptr) {
  88. if (*ptr == '\\') {
  89. ++ptr;
  90. if (!*ptr)
  91. break;
  92. switch (*ptr) {
  93. case 'X':
  94. builder.append("\033]0;"sv);
  95. break;
  96. case 'a':
  97. builder.append(0x07);
  98. break;
  99. case 'e':
  100. builder.append(0x1b);
  101. break;
  102. case 'u':
  103. builder.append(username);
  104. break;
  105. case 'h':
  106. builder.append({ hostname, strlen(hostname) });
  107. break;
  108. case 'w': {
  109. DeprecatedString home_path = getenv("HOME");
  110. if (cwd.starts_with(home_path)) {
  111. builder.append('~');
  112. builder.append(cwd.substring_view(home_path.length(), cwd.length() - home_path.length()));
  113. } else {
  114. builder.append(cwd);
  115. }
  116. break;
  117. }
  118. case 'p':
  119. builder.append(uid == 0 ? '#' : '$');
  120. break;
  121. }
  122. continue;
  123. }
  124. builder.append(*ptr);
  125. }
  126. return builder.to_deprecated_string();
  127. };
  128. return build_prompt();
  129. }
  130. DeprecatedString Shell::expand_tilde(StringView expression)
  131. {
  132. VERIFY(expression.starts_with('~'));
  133. StringBuilder login_name;
  134. size_t first_slash_index = expression.length();
  135. for (size_t i = 1; i < expression.length(); ++i) {
  136. if (expression[i] == '/') {
  137. first_slash_index = i;
  138. break;
  139. }
  140. login_name.append(expression[i]);
  141. }
  142. StringBuilder path;
  143. for (size_t i = first_slash_index; i < expression.length(); ++i)
  144. path.append(expression[i]);
  145. if (login_name.is_empty()) {
  146. char const* home = getenv("HOME");
  147. if (!home) {
  148. auto passwd = getpwuid(getuid());
  149. VERIFY(passwd && passwd->pw_dir);
  150. return DeprecatedString::formatted("{}/{}", passwd->pw_dir, path.to_deprecated_string());
  151. }
  152. return DeprecatedString::formatted("{}/{}", home, path.to_deprecated_string());
  153. }
  154. auto passwd = getpwnam(login_name.to_deprecated_string().characters());
  155. if (!passwd)
  156. return expression;
  157. VERIFY(passwd->pw_dir);
  158. return DeprecatedString::formatted("{}/{}", passwd->pw_dir, path.to_deprecated_string());
  159. }
  160. bool Shell::is_glob(StringView s)
  161. {
  162. for (size_t i = 0; i < s.length(); i++) {
  163. char c = s.characters_without_null_termination()[i];
  164. if (c == '*' || c == '?')
  165. return true;
  166. }
  167. return false;
  168. }
  169. Vector<StringView> Shell::split_path(StringView path)
  170. {
  171. Vector<StringView> parts;
  172. size_t substart = 0;
  173. for (size_t i = 0; i < path.length(); i++) {
  174. char ch = path[i];
  175. if (ch != '/')
  176. continue;
  177. size_t sublen = i - substart;
  178. if (sublen != 0)
  179. parts.append(path.substring_view(substart, sublen));
  180. substart = i + 1;
  181. }
  182. size_t taillen = path.length() - substart;
  183. if (taillen != 0)
  184. parts.append(path.substring_view(substart, taillen));
  185. return parts;
  186. }
  187. Vector<DeprecatedString> Shell::expand_globs(StringView path, StringView base)
  188. {
  189. auto explicitly_set_base = false;
  190. if (path.starts_with('/')) {
  191. base = "/"sv;
  192. explicitly_set_base = true;
  193. }
  194. auto parts = split_path(path);
  195. DeprecatedString base_string = base;
  196. struct stat statbuf;
  197. if (lstat(base_string.characters(), &statbuf) < 0) {
  198. perror("lstat");
  199. return {};
  200. }
  201. StringBuilder resolved_base_path_builder;
  202. resolved_base_path_builder.append(Core::DeprecatedFile::real_path_for(base));
  203. if (S_ISDIR(statbuf.st_mode))
  204. resolved_base_path_builder.append('/');
  205. auto resolved_base = resolved_base_path_builder.string_view();
  206. auto results = expand_globs(move(parts), resolved_base);
  207. if (explicitly_set_base && base == "/")
  208. resolved_base = resolved_base.substring_view(1, resolved_base.length() - 1);
  209. for (auto& entry : results) {
  210. entry = entry.substring(resolved_base.length(), entry.length() - resolved_base.length());
  211. if (entry.is_empty())
  212. entry = ".";
  213. }
  214. // Make the output predictable and nice.
  215. quick_sort(results);
  216. return results;
  217. }
  218. Vector<DeprecatedString> Shell::expand_globs(Vector<StringView> path_segments, StringView base)
  219. {
  220. if (path_segments.is_empty()) {
  221. DeprecatedString base_str = base;
  222. struct stat statbuf;
  223. if (lstat(base_str.characters(), &statbuf) < 0)
  224. return {};
  225. return { move(base_str) };
  226. }
  227. auto first_segment = path_segments.take_first();
  228. if (is_glob(first_segment)) {
  229. Vector<DeprecatedString> result;
  230. Core::DirIterator di(base, Core::DirIterator::SkipParentAndBaseDir);
  231. if (di.has_error())
  232. return {};
  233. while (di.has_next()) {
  234. DeprecatedString path = di.next_path();
  235. // Dotfiles have to be explicitly requested
  236. if (path[0] == '.' && first_segment[0] != '.')
  237. continue;
  238. if (path.matches(first_segment, CaseSensitivity::CaseSensitive)) {
  239. StringBuilder builder;
  240. builder.append(base);
  241. if (!base.ends_with('/'))
  242. builder.append('/');
  243. builder.append(path);
  244. result.extend(expand_globs(path_segments, builder.string_view()));
  245. }
  246. }
  247. return result;
  248. } else {
  249. StringBuilder builder;
  250. builder.append(base);
  251. if (!base.ends_with('/'))
  252. builder.append('/');
  253. builder.append(first_segment);
  254. return expand_globs(move(path_segments), builder.string_view());
  255. }
  256. }
  257. ErrorOr<Vector<AST::Command>> Shell::expand_aliases(Vector<AST::Command> initial_commands)
  258. {
  259. Vector<AST::Command> commands;
  260. Function<ErrorOr<void>(AST::Command&)> resolve_aliases_and_append = [&](auto& command) -> ErrorOr<void> {
  261. if (!command.argv.is_empty()) {
  262. auto alias = resolve_alias(command.argv[0]);
  263. if (!alias.is_null()) {
  264. auto argv0 = command.argv.take_first();
  265. auto subcommand_ast = parse(alias, false);
  266. if (subcommand_ast) {
  267. while (subcommand_ast->is_execute()) {
  268. auto* ast = static_cast<AST::Execute*>(subcommand_ast.ptr());
  269. subcommand_ast = ast->command();
  270. }
  271. auto subcommand_nonnull = subcommand_ast.release_nonnull();
  272. NonnullRefPtr<AST::Node> substitute = adopt_ref(*new AST::Join(subcommand_nonnull->position(),
  273. subcommand_nonnull,
  274. adopt_ref(*new AST::CommandLiteral(subcommand_nonnull->position(), command))));
  275. auto res = TRY(substitute->run(*this));
  276. for (auto& subst_command : TRY(res->resolve_as_commands(*this))) {
  277. if (!subst_command.argv.is_empty() && subst_command.argv.first() == argv0) // Disallow an alias resolving to itself.
  278. commands.append(subst_command);
  279. else
  280. TRY(resolve_aliases_and_append(subst_command));
  281. }
  282. } else {
  283. commands.append(command);
  284. }
  285. } else {
  286. commands.append(command);
  287. }
  288. } else {
  289. commands.append(command);
  290. }
  291. return {};
  292. };
  293. for (auto& command : initial_commands)
  294. TRY(resolve_aliases_and_append(command));
  295. return commands;
  296. }
  297. DeprecatedString Shell::resolve_path(DeprecatedString path) const
  298. {
  299. if (!path.starts_with('/'))
  300. path = DeprecatedString::formatted("{}/{}", cwd, path);
  301. return Core::DeprecatedFile::real_path_for(path);
  302. }
  303. Shell::LocalFrame* Shell::find_frame_containing_local_variable(StringView name)
  304. {
  305. for (size_t i = m_local_frames.size(); i > 0; --i) {
  306. auto& frame = m_local_frames[i - 1];
  307. if (frame->local_variables.contains(name))
  308. return frame;
  309. }
  310. return nullptr;
  311. }
  312. ErrorOr<RefPtr<AST::Value const>> Shell::look_up_local_variable(StringView name) const
  313. {
  314. if (auto* frame = find_frame_containing_local_variable(name))
  315. return frame->local_variables.get(name).value();
  316. if (auto index = name.to_uint(); index.has_value())
  317. return get_argument(index.value());
  318. return nullptr;
  319. }
  320. ErrorOr<RefPtr<AST::Value const>> Shell::get_argument(size_t index) const
  321. {
  322. if (index == 0)
  323. return adopt_ref(*new AST::StringValue(TRY(String::from_deprecated_string(current_script))));
  324. --index;
  325. if (auto argv = TRY(look_up_local_variable("ARGV"sv))) {
  326. if (argv->is_list_without_resolution()) {
  327. AST::ListValue const* list = static_cast<AST::ListValue const*>(argv.ptr());
  328. if (list->values().size() <= index)
  329. return nullptr;
  330. return list->values().at(index);
  331. }
  332. if (index != 0)
  333. return nullptr;
  334. return argv;
  335. }
  336. return nullptr;
  337. }
  338. ErrorOr<DeprecatedString> Shell::local_variable_or(StringView name, DeprecatedString const& replacement) const
  339. {
  340. auto value = TRY(look_up_local_variable(name));
  341. if (value) {
  342. StringBuilder builder;
  343. builder.join(' ', TRY(const_cast<AST::Value&>(*value).resolve_as_list(const_cast<Shell&>(*this))));
  344. return builder.to_deprecated_string();
  345. }
  346. return replacement;
  347. }
  348. void Shell::set_local_variable(DeprecatedString const& name, RefPtr<AST::Value> value, bool only_in_current_frame)
  349. {
  350. if (!only_in_current_frame) {
  351. if (auto* frame = find_frame_containing_local_variable(name)) {
  352. frame->local_variables.set(name, move(value));
  353. return;
  354. }
  355. }
  356. LocalFrame* selected_frame = nullptr;
  357. if (m_in_posix_mode) {
  358. // POSIX mode: Drop everything in the closest function frame (or the global frame if there is no function frame).
  359. auto& closest_function_frame = m_local_frames.last_matching([](auto& frame) { return frame->is_function_frame; }).value();
  360. selected_frame = closest_function_frame.ptr();
  361. } else {
  362. selected_frame = m_local_frames.last().ptr();
  363. }
  364. selected_frame->local_variables.set(name, move(value));
  365. }
  366. void Shell::unset_local_variable(StringView name, bool only_in_current_frame)
  367. {
  368. if (!only_in_current_frame) {
  369. if (auto* frame = find_frame_containing_local_variable(name))
  370. frame->local_variables.remove(name);
  371. return;
  372. }
  373. m_local_frames.last()->local_variables.remove(name);
  374. }
  375. void Shell::define_function(DeprecatedString name, Vector<DeprecatedString> argnames, RefPtr<AST::Node> body)
  376. {
  377. add_entry_to_cache({ RunnablePath::Kind::Function, name });
  378. m_functions.set(name, { name, move(argnames), move(body) });
  379. }
  380. bool Shell::has_function(StringView name)
  381. {
  382. return m_functions.contains(name);
  383. }
  384. bool Shell::invoke_function(const AST::Command& command, int& retval)
  385. {
  386. if (command.argv.is_empty())
  387. return false;
  388. StringView name = command.argv.first();
  389. TemporaryChange<DeprecatedString> script_change { current_script, name };
  390. auto function_option = m_functions.get(name);
  391. if (!function_option.has_value())
  392. return false;
  393. auto& function = function_option.value();
  394. if (!function.body) {
  395. retval = 0;
  396. return true;
  397. }
  398. if (command.argv.size() - 1 < function.arguments.size()) {
  399. raise_error(ShellError::EvaluatedSyntaxError, DeprecatedString::formatted("Expected at least {} arguments to {}, but got {}", function.arguments.size(), function.name, command.argv.size() - 1), command.position);
  400. retval = 1;
  401. return true;
  402. }
  403. auto frame = push_frame(DeprecatedString::formatted("function {}", function.name), LocalFrameKind::FunctionOrGlobal);
  404. size_t index = 0;
  405. for (auto& arg : function.arguments) {
  406. ++index;
  407. set_local_variable(arg, adopt_ref(*new AST::StringValue(command.argv[index])), true);
  408. }
  409. auto argv = command.argv;
  410. argv.take_first();
  411. set_local_variable("ARGV", adopt_ref(*new AST::ListValue(move(argv))), true);
  412. Core::EventLoop loop;
  413. setup_signals();
  414. (void)function.body->run(*this);
  415. retval = last_return_code.value_or(0);
  416. return true;
  417. }
  418. DeprecatedString Shell::format(StringView source, ssize_t& cursor) const
  419. {
  420. Formatter formatter(source, cursor, m_in_posix_mode);
  421. auto result = formatter.format();
  422. cursor = formatter.cursor();
  423. return result;
  424. }
  425. Shell::Frame Shell::push_frame(DeprecatedString name, Shell::LocalFrameKind kind)
  426. {
  427. m_local_frames.append(make<LocalFrame>(name, decltype(LocalFrame::local_variables) {}, kind));
  428. dbgln_if(SH_DEBUG, "New frame '{}' at {:p}", name, &m_local_frames.last());
  429. return { m_local_frames, *m_local_frames.last() };
  430. }
  431. void Shell::pop_frame()
  432. {
  433. VERIFY(m_local_frames.size() > 1);
  434. (void)m_local_frames.take_last();
  435. }
  436. Shell::Frame::~Frame()
  437. {
  438. if (!should_destroy_frame)
  439. return;
  440. if (frames.last() != &frame) {
  441. dbgln("Frame destruction order violation near {:p} (container = {:p}) in '{}'", &frame, this, frame.name);
  442. dbgln("Current frames:");
  443. for (auto& frame : frames)
  444. dbgln("- {:p}: {}", &frame, frame->name);
  445. VERIFY_NOT_REACHED();
  446. }
  447. (void)frames.take_last();
  448. }
  449. DeprecatedString Shell::resolve_alias(StringView name) const
  450. {
  451. return m_aliases.get(name).value_or({});
  452. }
  453. Optional<Shell::RunnablePath> Shell::runnable_path_for(StringView name)
  454. {
  455. auto parts = name.find('/');
  456. if (parts.has_value()) {
  457. auto file_or_error = Core::File::open(name, Core::File::OpenMode::Read);
  458. if (!file_or_error.is_error()
  459. && !FileSystem::is_directory(file_or_error.value()->fd())
  460. && !Core::System::access(name, X_OK).is_error())
  461. return RunnablePath { RunnablePath::Kind::Executable, name };
  462. }
  463. auto* found = binary_search(cached_path.span(), name, nullptr, RunnablePathComparator {});
  464. if (!found)
  465. return {};
  466. return *found;
  467. }
  468. Optional<DeprecatedString> Shell::help_path_for(Vector<RunnablePath> visited, Shell::RunnablePath const& runnable_path)
  469. {
  470. switch (runnable_path.kind) {
  471. case RunnablePath::Kind::Executable: {
  472. LexicalPath lexical_path(runnable_path.path);
  473. return lexical_path.basename();
  474. }
  475. case RunnablePath::Kind::Alias: {
  476. if (visited.contains_slow(runnable_path))
  477. return {}; // Break out of an alias loop
  478. auto resolved = resolve_alias(runnable_path.path);
  479. auto* runnable = binary_search(cached_path.span(), resolved, nullptr, RunnablePathComparator {});
  480. if (!runnable)
  481. return {};
  482. visited.append(runnable_path);
  483. return help_path_for(visited, *runnable);
  484. }
  485. default:
  486. return {};
  487. }
  488. }
  489. int Shell::run_command(StringView cmd, Optional<SourcePosition> source_position_override)
  490. {
  491. // The default-constructed mode of the shell
  492. // should not be used for execution!
  493. VERIFY(!m_default_constructed);
  494. take_error();
  495. if (!last_return_code.has_value())
  496. last_return_code = 0;
  497. ScopedValueRollback source_position_rollback { m_source_position };
  498. if (source_position_override.has_value())
  499. m_source_position = move(source_position_override);
  500. if (!m_source_position.has_value())
  501. m_source_position = SourcePosition { .source_file = {}, .literal_source_text = cmd, .position = {} };
  502. if (cmd.is_empty())
  503. return 0;
  504. auto command = parse(cmd, m_is_interactive);
  505. if (!command)
  506. return 0;
  507. if constexpr (SH_DEBUG) {
  508. dbgln("Command follows");
  509. (void)command->dump(0);
  510. }
  511. if (command->is_syntax_error()) {
  512. auto& error_node = command->syntax_error_node();
  513. auto& position = error_node.position();
  514. raise_error(ShellError::EvaluatedSyntaxError, error_node.error_text().bytes_as_string_view(), position);
  515. }
  516. if (!has_error(ShellError::None)) {
  517. possibly_print_error();
  518. take_error();
  519. return 1;
  520. }
  521. tcgetattr(0, &termios);
  522. (void)command->run(*this);
  523. if (!has_error(ShellError::None)) {
  524. possibly_print_error();
  525. take_error();
  526. return 1;
  527. }
  528. return last_return_code.value_or(0);
  529. }
  530. ErrorOr<RefPtr<Job>> Shell::run_command(const AST::Command& command)
  531. {
  532. FileDescriptionCollector fds;
  533. if (options.verbose)
  534. warnln("+ {}", command);
  535. // If the command is empty, store the redirections and apply them to all later commands.
  536. if (command.argv.is_empty() && !command.should_immediately_execute_next) {
  537. m_global_redirections.extend(command.redirections);
  538. for (auto& next_in_chain : command.next_chain)
  539. run_tail(command, next_in_chain, last_return_code.value_or(0));
  540. return nullptr;
  541. }
  542. // Resolve redirections.
  543. Vector<NonnullRefPtr<AST::Rewiring>> rewirings;
  544. auto resolve_redirection = [&](auto& redirection) -> ErrorOr<void> {
  545. auto rewiring = TRY(redirection->apply());
  546. if (rewiring->fd_action != AST::Rewiring::Close::ImmediatelyCloseNew)
  547. rewirings.append(*rewiring);
  548. if (rewiring->fd_action == AST::Rewiring::Close::Old) {
  549. fds.add(rewiring->old_fd);
  550. } else if (rewiring->fd_action == AST::Rewiring::Close::New) {
  551. if (rewiring->new_fd != -1)
  552. fds.add(rewiring->new_fd);
  553. } else if (rewiring->fd_action == AST::Rewiring::Close::ImmediatelyCloseNew) {
  554. fds.add(rewiring->new_fd);
  555. } else if (rewiring->fd_action == AST::Rewiring::Close::RefreshNew) {
  556. VERIFY(rewiring->other_pipe_end);
  557. int pipe_fd[2];
  558. int rc = pipe(pipe_fd);
  559. if (rc < 0)
  560. return Error::from_syscall("pipe"sv, rc);
  561. rewiring->new_fd = pipe_fd[1];
  562. rewiring->other_pipe_end->new_fd = pipe_fd[0]; // This fd will be added to the collection on one of the next iterations.
  563. fds.add(pipe_fd[1]);
  564. } else if (rewiring->fd_action == AST::Rewiring::Close::RefreshOld) {
  565. VERIFY(rewiring->other_pipe_end);
  566. int pipe_fd[2];
  567. int rc = pipe(pipe_fd);
  568. if (rc < 0)
  569. return Error::from_syscall("pipe"sv, rc);
  570. rewiring->old_fd = pipe_fd[1];
  571. rewiring->other_pipe_end->old_fd = pipe_fd[0]; // This fd will be added to the collection on one of the next iterations.
  572. fds.add(pipe_fd[1]);
  573. }
  574. return {};
  575. };
  576. auto apply_rewirings = [&]() -> ErrorOr<void> {
  577. for (auto& rewiring : rewirings) {
  578. dbgln_if(SH_DEBUG, "in {}<{}>, dup2({}, {})", command.argv.is_empty() ? "(<Empty>)"sv : command.argv[0], getpid(), rewiring->old_fd, rewiring->new_fd);
  579. int rc = dup2(rewiring->old_fd, rewiring->new_fd);
  580. if (rc < 0)
  581. return Error::from_syscall("dup2"sv, rc);
  582. // {new,old}_fd is closed via the `fds` collector, but rewiring->other_pipe_end->{new,old}_fd
  583. // isn't yet in that collector when the first child spawns.
  584. if (rewiring->other_pipe_end) {
  585. if (rewiring->fd_action == AST::Rewiring::Close::RefreshNew) {
  586. if (rewiring->other_pipe_end && close(rewiring->other_pipe_end->new_fd) < 0)
  587. perror("close other pipe end");
  588. } else if (rewiring->fd_action == AST::Rewiring::Close::RefreshOld) {
  589. if (rewiring->other_pipe_end && close(rewiring->other_pipe_end->old_fd) < 0)
  590. perror("close other pipe end");
  591. }
  592. }
  593. }
  594. return {};
  595. };
  596. TemporaryChange signal_handler_install { m_should_reinstall_signal_handlers, false };
  597. for (auto& redirection : m_global_redirections)
  598. TRY(resolve_redirection(redirection));
  599. for (auto& redirection : command.redirections)
  600. TRY(resolve_redirection(redirection));
  601. if (int local_return_code = 0; command.should_wait && TRY(run_builtin(command, rewirings, local_return_code))) {
  602. last_return_code = local_return_code;
  603. for (auto& next_in_chain : command.next_chain)
  604. run_tail(command, next_in_chain, *last_return_code);
  605. return nullptr;
  606. }
  607. auto can_be_run_in_current_process = command.should_wait && !command.pipeline && !command.argv.is_empty();
  608. if (can_be_run_in_current_process && has_function(command.argv.first())) {
  609. SavedFileDescriptors fds { rewirings };
  610. for (auto& rewiring : rewirings)
  611. TRY(Core::System::dup2(rewiring->old_fd, rewiring->new_fd));
  612. if (int local_return_code = 0; invoke_function(command, local_return_code)) {
  613. last_return_code = local_return_code;
  614. for (auto& next_in_chain : command.next_chain)
  615. run_tail(command, next_in_chain, *last_return_code);
  616. return nullptr;
  617. }
  618. }
  619. if (command.argv.is_empty()
  620. && !command.next_chain.is_empty()
  621. && command.should_immediately_execute_next
  622. && command.redirections.is_empty()
  623. && command.next_chain.first().node->should_override_execution_in_current_process()) {
  624. for (auto& next_in_chain : command.next_chain)
  625. run_tail(command, next_in_chain, last_return_code.value_or(0));
  626. return nullptr;
  627. }
  628. Vector<char const*> argv;
  629. Vector<DeprecatedString> copy_argv;
  630. argv.ensure_capacity(command.argv.size() + 1);
  631. for (auto& arg : command.argv) {
  632. copy_argv.append(arg.to_deprecated_string());
  633. argv.append(copy_argv.last().characters());
  634. }
  635. argv.append(nullptr);
  636. auto sync_pipe = TRY(Core::System::pipe2(0));
  637. auto child = TRY(Core::System::fork());
  638. if (child == 0) {
  639. close(sync_pipe[1]);
  640. m_pid = getpid();
  641. Core::EventLoop::notify_forked(Core::EventLoop::ForkEvent::Child);
  642. TemporaryChange signal_handler_install { m_should_reinstall_signal_handlers, true };
  643. if (auto result = apply_rewirings(); result.is_error()) {
  644. warnln("Shell: Failed to apply rewirings in {}: {}", copy_argv[0], result.error());
  645. _exit(126);
  646. }
  647. fds.collect();
  648. u8 c;
  649. while (read(sync_pipe[0], &c, 1) < 0) {
  650. if (errno != EINTR) {
  651. warnln("Shell: Failed to sync in {}: {}", copy_argv[0], Error::from_syscall("read"sv, -errno));
  652. // There's nothing interesting we can do here.
  653. break;
  654. }
  655. }
  656. dbgln_if(SH_DEBUG, "Synced up with parent, we're good to exec()");
  657. close(sync_pipe[0]);
  658. if (!m_is_subshell && command.should_wait)
  659. tcsetattr(0, TCSANOW, &default_termios);
  660. m_is_subshell = true;
  661. if (command.should_immediately_execute_next) {
  662. VERIFY(command.argv.is_empty());
  663. Core::EventLoop mainloop;
  664. setup_signals();
  665. for (auto& next_in_chain : command.next_chain)
  666. run_tail(command, next_in_chain, 0);
  667. _exit(last_return_code.value_or(0));
  668. }
  669. if (int local_return_code = 0; TRY(run_builtin(command, {}, local_return_code)))
  670. _exit(local_return_code);
  671. if (int local_return_code = 0; invoke_function(command, local_return_code))
  672. _exit(local_return_code);
  673. // We no longer need the jobs here.
  674. jobs.clear();
  675. execute_process(move(argv));
  676. VERIFY_NOT_REACHED();
  677. }
  678. close(sync_pipe[0]);
  679. bool is_first = !command.pipeline || (command.pipeline && command.pipeline->pgid == -1);
  680. if (command.pipeline) {
  681. if (is_first) {
  682. command.pipeline->pgid = child;
  683. }
  684. }
  685. pid_t pgid = is_first ? child : (command.pipeline ? command.pipeline->pgid : child);
  686. if (!m_is_subshell || command.pipeline) {
  687. auto result = Core::System::setpgid(child, pgid);
  688. if (result.is_error() && m_is_interactive)
  689. warnln("Shell: {}", result.error());
  690. if (!m_is_subshell) {
  691. // There's no reason to care about the errors here
  692. // either we're in a tty, we're interactive, and this works
  693. // or we're not, and it fails - in which case, we don't need
  694. // stdin/stdout handoff to child processes anyway.
  695. tcsetpgrp(STDOUT_FILENO, pgid);
  696. tcsetpgrp(STDIN_FILENO, pgid);
  697. }
  698. }
  699. while (write(sync_pipe[1], "x", 1) < 0) {
  700. if (errno != EINTR) {
  701. warnln("Shell: Failed to sync with {}: {}", copy_argv[0], Error::from_syscall("write"sv, -errno));
  702. // There's nothing interesting we can do here.
  703. break;
  704. }
  705. }
  706. close(sync_pipe[1]);
  707. StringBuilder cmd;
  708. cmd.join(' ', command.argv);
  709. auto command_copy = AST::Command(command);
  710. // Clear the next chain if it's to be immediately executed
  711. // as the child will run this chain.
  712. if (command.should_immediately_execute_next)
  713. command_copy.next_chain.clear();
  714. auto job = Job::create(child, pgid, cmd.to_deprecated_string(), find_last_job_id() + 1, move(command_copy));
  715. jobs.set((u64)child, job);
  716. job->on_exit = [this](auto job) {
  717. if (!job->exited())
  718. return;
  719. if (job->is_running_in_background() && job->should_announce_exit())
  720. warnln("Shell: Job {} ({}) exited\n", job->job_id(), job->cmd());
  721. else if (job->signaled() && job->should_announce_signal())
  722. warnln("Shell: Job {} ({}) {}\n", job->job_id(), job->cmd(), strsignal(job->termination_signal()));
  723. last_return_code = job->exit_code();
  724. job->disown();
  725. if (m_editor && job->exit_code() == 0 && is_allowed_to_modify_termios(job->command())) {
  726. m_editor->refetch_default_termios();
  727. default_termios = m_editor->default_termios();
  728. termios = m_editor->termios();
  729. }
  730. run_tail(job);
  731. };
  732. fds.collect();
  733. return *job;
  734. }
  735. ErrorOr<void> Shell::execute_process(Span<StringView> argv)
  736. {
  737. Vector<DeprecatedString> strings;
  738. Vector<char const*> args;
  739. TRY(strings.try_ensure_capacity(argv.size()));
  740. TRY(args.try_ensure_capacity(argv.size() + 1));
  741. for (auto& entry : argv) {
  742. strings.unchecked_append(entry);
  743. args.unchecked_append(strings.last().characters());
  744. }
  745. args.append(nullptr);
  746. // NOTE: noreturn.
  747. execute_process(move(args));
  748. }
  749. void Shell::execute_process(Vector<char const*>&& argv)
  750. {
  751. for (auto& promise : m_active_promises) {
  752. MUST(Core::System::pledge("stdio rpath exec"sv, promise.data.exec_promises));
  753. for (auto& item : promise.data.unveils)
  754. MUST(Core::System::unveil(item.path, item.access));
  755. }
  756. int rc = execvp(argv[0], const_cast<char* const*>(argv.data()));
  757. if (rc < 0) {
  758. auto parts = StringView { argv[0], strlen(argv[0]) }.split_view('/');
  759. if (parts.size() == 1) {
  760. // If this is a path in the current directory and it caused execvp() to fail,
  761. // simply don't attempt to execute it, see #6774.
  762. warnln("{}: Command not found.", argv[0]);
  763. _exit(127);
  764. }
  765. int saved_errno = errno;
  766. struct stat st;
  767. if (stat(argv[0], &st)) {
  768. warnln("stat({}): {}", argv[0], strerror(errno));
  769. // Return code 127 on command not found.
  770. _exit(127);
  771. }
  772. if (!(st.st_mode & S_IXUSR)) {
  773. warnln("{}: Not executable", argv[0]);
  774. // Return code 126 when file is not executable.
  775. _exit(126);
  776. }
  777. if (saved_errno == ENOENT) {
  778. do {
  779. auto file_result = Core::DeprecatedFile::open(argv[0], Core::OpenMode::ReadOnly);
  780. if (file_result.is_error())
  781. break;
  782. auto& file = file_result.value();
  783. auto line = file->read_line();
  784. if (!line.starts_with("#!"sv))
  785. break;
  786. GenericLexer shebang_lexer { line.substring_view(2) };
  787. auto shebang = shebang_lexer.consume_until(is_any_of("\n\r"sv)).to_deprecated_string();
  788. argv.prepend(shebang.characters());
  789. int rc = execvp(argv[0], const_cast<char* const*>(argv.data()));
  790. if (rc < 0) {
  791. warnln("{}: Invalid interpreter \"{}\": {}", argv[0], shebang.characters(), strerror(errno));
  792. _exit(126);
  793. }
  794. } while (false);
  795. warnln("{}: Command not found.", argv[0]);
  796. } else {
  797. if (S_ISDIR(st.st_mode)) {
  798. warnln("Shell: {}: Is a directory", argv[0]);
  799. _exit(126);
  800. }
  801. warnln("execvp({}): {}", argv[0], strerror(saved_errno));
  802. }
  803. _exit(126);
  804. }
  805. VERIFY_NOT_REACHED();
  806. }
  807. void Shell::run_tail(const AST::Command& invoking_command, const AST::NodeWithAction& next_in_chain, int head_exit_code)
  808. {
  809. if (m_error != ShellError::None) {
  810. possibly_print_error();
  811. if (!is_control_flow(m_error))
  812. take_error();
  813. return;
  814. }
  815. auto evaluate = [&] {
  816. if (next_in_chain.node->would_execute()) {
  817. (void)next_in_chain.node->run(*this);
  818. return;
  819. }
  820. auto node = next_in_chain.node;
  821. if (!invoking_command.should_wait)
  822. node = adopt_ref(static_cast<AST::Node&>(*new AST::Background(next_in_chain.node->position(), move(node))));
  823. (void)adopt_ref(static_cast<AST::Node&>(*new AST::Execute(next_in_chain.node->position(), move(node))))->run(*this);
  824. };
  825. switch (next_in_chain.action) {
  826. case AST::NodeWithAction::And:
  827. if (head_exit_code == 0)
  828. evaluate();
  829. break;
  830. case AST::NodeWithAction::Or:
  831. if (head_exit_code != 0)
  832. evaluate();
  833. break;
  834. case AST::NodeWithAction::Sequence:
  835. evaluate();
  836. break;
  837. }
  838. }
  839. void Shell::run_tail(RefPtr<Job> job)
  840. {
  841. if (auto cmd = job->command_ptr()) {
  842. deferred_invoke([=, this] {
  843. for (auto& next_in_chain : cmd->next_chain) {
  844. run_tail(*cmd, next_in_chain, job->exit_code());
  845. }
  846. });
  847. }
  848. }
  849. Vector<NonnullRefPtr<Job>> Shell::run_commands(Vector<AST::Command>& commands)
  850. {
  851. if (m_error != ShellError::None) {
  852. possibly_print_error();
  853. if (!is_control_flow(m_error))
  854. take_error();
  855. return {};
  856. }
  857. Vector<NonnullRefPtr<Job>> spawned_jobs;
  858. for (auto& command : commands) {
  859. if constexpr (SH_DEBUG) {
  860. dbgln("Command");
  861. for (auto& arg : command.argv)
  862. dbgln("argv: {}", arg);
  863. for (auto& redir : command.redirections) {
  864. if (redir->is_path_redirection()) {
  865. auto path_redir = (const AST::PathRedirection*)&redir;
  866. dbgln("redir path '{}' <-({})-> {}", path_redir->path, (int)path_redir->direction, path_redir->fd);
  867. } else if (redir->is_fd_redirection()) {
  868. auto* fdredir = (const AST::FdRedirection*)&redir;
  869. dbgln("redir fd {} -> {}", fdredir->old_fd, fdredir->new_fd);
  870. } else if (redir->is_close_redirection()) {
  871. auto close_redir = (const AST::CloseRedirection*)&redir;
  872. dbgln("close fd {}", close_redir->fd);
  873. } else {
  874. VERIFY_NOT_REACHED();
  875. }
  876. }
  877. }
  878. auto job_result = run_command(command);
  879. if (job_result.is_error()) {
  880. raise_error(ShellError::LaunchError, DeprecatedString::formatted("{} while running '{}'", job_result.error(), command.argv.first()), command.position);
  881. break;
  882. }
  883. auto job = job_result.release_value();
  884. if (!job)
  885. continue;
  886. spawned_jobs.append(*job);
  887. if (command.should_wait) {
  888. block_on_job(job);
  889. } else {
  890. job->set_running_in_background(true);
  891. if (!command.is_pipe_source && command.should_notify_if_in_background)
  892. job->set_should_announce_exit(true);
  893. }
  894. }
  895. if (m_error != ShellError::None) {
  896. possibly_print_error();
  897. if (!is_control_flow(m_error))
  898. take_error();
  899. }
  900. return spawned_jobs;
  901. }
  902. bool Shell::run_file(DeprecatedString const& filename, bool explicitly_invoked)
  903. {
  904. TemporaryChange script_change { current_script, filename };
  905. TemporaryChange interactive_change { m_is_interactive, false };
  906. TemporaryChange<Optional<SourcePosition>> source_change { m_source_position, SourcePosition { .source_file = filename, .literal_source_text = {}, .position = {} } };
  907. auto file_or_error = Core::File::open(filename, Core::File::OpenMode::Read);
  908. if (file_or_error.is_error()) {
  909. auto error = DeprecatedString::formatted("'{}': {}", escape_token_for_single_quotes(filename), file_or_error.error());
  910. if (explicitly_invoked)
  911. raise_error(ShellError::OpenFailure, error);
  912. else
  913. dbgln("open() failed for {}", error);
  914. return false;
  915. }
  916. auto file = file_or_error.release_value();
  917. auto data_or_error = file->read_until_eof();
  918. if (data_or_error.is_error()) {
  919. auto error = DeprecatedString::formatted("'{}': {}", escape_token_for_single_quotes(filename), data_or_error.error());
  920. if (explicitly_invoked)
  921. raise_error(ShellError::OpenFailure, error);
  922. else
  923. dbgln("reading after open() failed for {}", error);
  924. return false;
  925. }
  926. return run_command(data_or_error.value()) == 0;
  927. }
  928. bool Shell::is_allowed_to_modify_termios(const AST::Command& command) const
  929. {
  930. if (command.argv.is_empty())
  931. return false;
  932. auto value = look_up_local_variable("PROGRAMS_ALLOWED_TO_MODIFY_DEFAULT_TERMIOS"sv);
  933. if (value.is_error())
  934. return false;
  935. if (!value.value())
  936. return false;
  937. auto result = const_cast<AST::Value&>(*value.value()).resolve_as_list(const_cast<Shell&>(*this));
  938. if (result.is_error())
  939. return false;
  940. return result.value().contains_slow(command.argv[0]);
  941. }
  942. void Shell::restore_ios()
  943. {
  944. if (m_is_subshell)
  945. return;
  946. tcsetattr(0, TCSANOW, &termios);
  947. tcsetpgrp(STDOUT_FILENO, m_pid);
  948. tcsetpgrp(STDIN_FILENO, m_pid);
  949. }
  950. void Shell::block_on_pipeline(RefPtr<AST::Pipeline> pipeline)
  951. {
  952. if (!pipeline)
  953. return;
  954. for (auto& it : jobs) {
  955. if (auto cmd = it.value->command_ptr(); cmd->pipeline == pipeline && cmd->is_pipe_source)
  956. block_on_job(it.value);
  957. }
  958. }
  959. void Shell::block_on_job(RefPtr<Job> job)
  960. {
  961. TemporaryChange<Job*> current_job { m_current_job, job.ptr() };
  962. if (!job)
  963. return;
  964. if (job->is_suspended() && !job->shell_did_continue())
  965. return; // We cannot wait for a suspended job.
  966. ScopeGuard io_restorer { [&]() {
  967. if (job->exited() && !job->is_running_in_background()) {
  968. restore_ios();
  969. }
  970. } };
  971. bool job_exited { false };
  972. job->on_exit = [&, old_exit = move(job->on_exit)](auto job) {
  973. if (old_exit)
  974. old_exit(job);
  975. job_exited = true;
  976. };
  977. if (job->exited())
  978. return;
  979. while (!job_exited)
  980. Core::EventLoop::current().pump();
  981. // If the job is part of a pipeline, wait for the rest of the members too.
  982. if (auto command = job->command_ptr())
  983. block_on_pipeline(command->pipeline);
  984. }
  985. DeprecatedString Shell::get_history_path()
  986. {
  987. if (auto histfile = getenv("HISTFILE"))
  988. return { histfile };
  989. return DeprecatedString::formatted("{}/.history", home);
  990. }
  991. DeprecatedString Shell::escape_token_for_single_quotes(StringView token)
  992. {
  993. // `foo bar \n '` -> `'foo bar \n '"'"`
  994. StringBuilder builder;
  995. builder.append("'"sv);
  996. auto started_single_quote = true;
  997. for (auto c : token) {
  998. switch (c) {
  999. case '\'':
  1000. builder.append("\"'\""sv);
  1001. started_single_quote = false;
  1002. continue;
  1003. default:
  1004. builder.append(c);
  1005. if (!started_single_quote) {
  1006. started_single_quote = true;
  1007. builder.append("'"sv);
  1008. }
  1009. break;
  1010. }
  1011. }
  1012. if (started_single_quote)
  1013. builder.append("'"sv);
  1014. return builder.to_deprecated_string();
  1015. }
  1016. DeprecatedString Shell::escape_token_for_double_quotes(StringView token)
  1017. {
  1018. // `foo bar \n $x 'blah "hello` -> `"foo bar \\n $x 'blah \"hello"`
  1019. StringBuilder builder;
  1020. builder.append('"');
  1021. for (auto c : token) {
  1022. switch (c) {
  1023. case '\"':
  1024. builder.append("\\\""sv);
  1025. continue;
  1026. case '\\':
  1027. builder.append("\\\\"sv);
  1028. continue;
  1029. default:
  1030. builder.append(c);
  1031. break;
  1032. }
  1033. }
  1034. builder.append('"');
  1035. return builder.to_deprecated_string();
  1036. }
  1037. Shell::SpecialCharacterEscapeMode Shell::special_character_escape_mode(u32 code_point, EscapeMode mode)
  1038. {
  1039. switch (code_point) {
  1040. case '\'':
  1041. if (mode == EscapeMode::DoubleQuotedString)
  1042. return SpecialCharacterEscapeMode::Untouched;
  1043. return SpecialCharacterEscapeMode::Escaped;
  1044. case '"':
  1045. case '$':
  1046. case '\\':
  1047. if (mode == EscapeMode::SingleQuotedString)
  1048. return SpecialCharacterEscapeMode::Untouched;
  1049. return SpecialCharacterEscapeMode::Escaped;
  1050. case '|':
  1051. case '>':
  1052. case '<':
  1053. case '(':
  1054. case ')':
  1055. case '{':
  1056. case '}':
  1057. case '&':
  1058. case ';':
  1059. case '?':
  1060. case '*':
  1061. case ' ':
  1062. if (mode == EscapeMode::SingleQuotedString || mode == EscapeMode::DoubleQuotedString)
  1063. return SpecialCharacterEscapeMode::Untouched;
  1064. return SpecialCharacterEscapeMode::Escaped;
  1065. case '\n':
  1066. case '\t':
  1067. case '\r':
  1068. return SpecialCharacterEscapeMode::QuotedAsEscape;
  1069. default:
  1070. // FIXME: Should instead use unicode's "graphic" property (categories L, M, N, P, S, Zs)
  1071. if (is_ascii(code_point))
  1072. return is_ascii_printable(code_point) ? SpecialCharacterEscapeMode::Untouched : SpecialCharacterEscapeMode::QuotedAsHex;
  1073. return SpecialCharacterEscapeMode::Untouched;
  1074. }
  1075. }
  1076. static DeprecatedString do_escape(Shell::EscapeMode escape_mode, auto& token)
  1077. {
  1078. StringBuilder builder;
  1079. for (auto c : token) {
  1080. static_assert(sizeof(c) == sizeof(u32) || sizeof(c) == sizeof(u8));
  1081. switch (Shell::special_character_escape_mode(c, escape_mode)) {
  1082. case Shell::SpecialCharacterEscapeMode::Untouched:
  1083. if constexpr (sizeof(c) == sizeof(u8))
  1084. builder.append(c);
  1085. else
  1086. builder.append(Utf32View { &c, 1 });
  1087. break;
  1088. case Shell::SpecialCharacterEscapeMode::Escaped:
  1089. if (escape_mode == Shell::EscapeMode::SingleQuotedString)
  1090. builder.append("'"sv);
  1091. builder.append('\\');
  1092. builder.append(c);
  1093. if (escape_mode == Shell::EscapeMode::SingleQuotedString)
  1094. builder.append("'"sv);
  1095. break;
  1096. case Shell::SpecialCharacterEscapeMode::QuotedAsEscape:
  1097. if (escape_mode == Shell::EscapeMode::SingleQuotedString)
  1098. builder.append("'"sv);
  1099. if (escape_mode != Shell::EscapeMode::DoubleQuotedString)
  1100. builder.append("\""sv);
  1101. switch (c) {
  1102. case '\n':
  1103. builder.append(R"(\n)"sv);
  1104. break;
  1105. case '\t':
  1106. builder.append(R"(\t)"sv);
  1107. break;
  1108. case '\r':
  1109. builder.append(R"(\r)"sv);
  1110. break;
  1111. default:
  1112. VERIFY_NOT_REACHED();
  1113. }
  1114. if (escape_mode != Shell::EscapeMode::DoubleQuotedString)
  1115. builder.append("\""sv);
  1116. if (escape_mode == Shell::EscapeMode::SingleQuotedString)
  1117. builder.append("'"sv);
  1118. break;
  1119. case Shell::SpecialCharacterEscapeMode::QuotedAsHex:
  1120. if (escape_mode == Shell::EscapeMode::SingleQuotedString)
  1121. builder.append("'"sv);
  1122. if (escape_mode != Shell::EscapeMode::DoubleQuotedString)
  1123. builder.append("\""sv);
  1124. if (c <= NumericLimits<u8>::max())
  1125. builder.appendff(R"(\x{:0>2x})", static_cast<u8>(c));
  1126. else
  1127. builder.appendff(R"(\u{:0>8x})", static_cast<u32>(c));
  1128. if (escape_mode != Shell::EscapeMode::DoubleQuotedString)
  1129. builder.append("\""sv);
  1130. if (escape_mode == Shell::EscapeMode::SingleQuotedString)
  1131. builder.append("'"sv);
  1132. break;
  1133. }
  1134. }
  1135. return builder.to_deprecated_string();
  1136. }
  1137. DeprecatedString Shell::escape_token(Utf32View token, EscapeMode escape_mode)
  1138. {
  1139. return do_escape(escape_mode, token);
  1140. }
  1141. DeprecatedString Shell::escape_token(StringView token, EscapeMode escape_mode)
  1142. {
  1143. Utf8View view { token };
  1144. if (view.validate())
  1145. return do_escape(escape_mode, view);
  1146. return do_escape(escape_mode, token);
  1147. }
  1148. DeprecatedString Shell::unescape_token(StringView token)
  1149. {
  1150. StringBuilder builder;
  1151. enum {
  1152. Free,
  1153. Escaped
  1154. } state { Free };
  1155. for (auto c : token) {
  1156. switch (state) {
  1157. case Escaped:
  1158. builder.append(c);
  1159. state = Free;
  1160. break;
  1161. case Free:
  1162. if (c == '\\')
  1163. state = Escaped;
  1164. else
  1165. builder.append(c);
  1166. break;
  1167. }
  1168. }
  1169. if (state == Escaped)
  1170. builder.append('\\');
  1171. return builder.to_deprecated_string();
  1172. }
  1173. void Shell::cache_path()
  1174. {
  1175. if (!m_is_interactive)
  1176. return;
  1177. if (!cached_path.is_empty())
  1178. cached_path.clear_with_capacity();
  1179. // Add shell builtins to the cache.
  1180. for (auto const& builtin_name : builtin_names)
  1181. cached_path.append({ RunnablePath::Kind::Builtin, escape_token(builtin_name) });
  1182. // Add functions to the cache.
  1183. for (auto& function : m_functions) {
  1184. auto name = escape_token(function.key);
  1185. if (cached_path.contains_slow(name))
  1186. continue;
  1187. cached_path.append({ RunnablePath::Kind::Function, name });
  1188. }
  1189. // Add aliases to the cache.
  1190. for (auto const& alias : m_aliases) {
  1191. auto name = escape_token(alias.key);
  1192. if (cached_path.contains_slow(name))
  1193. continue;
  1194. cached_path.append({ RunnablePath::Kind::Alias, name });
  1195. }
  1196. // TODO: Can we make this rely on Core::DeprecatedFile::resolve_executable_from_environment()?
  1197. DeprecatedString path = getenv("PATH");
  1198. if (!path.is_empty()) {
  1199. auto directories = path.split(':');
  1200. for (auto const& directory : directories) {
  1201. Core::DirIterator programs(directory.characters(), Core::DirIterator::SkipDots);
  1202. while (programs.has_next()) {
  1203. auto program = programs.next_path();
  1204. auto program_path = DeprecatedString::formatted("{}/{}", directory, program);
  1205. auto escaped_name = escape_token(program);
  1206. if (cached_path.contains_slow(escaped_name))
  1207. continue;
  1208. if (access(program_path.characters(), X_OK) == 0)
  1209. cached_path.append({ RunnablePath::Kind::Executable, escaped_name });
  1210. }
  1211. }
  1212. }
  1213. quick_sort(cached_path);
  1214. }
  1215. void Shell::add_entry_to_cache(RunnablePath const& entry)
  1216. {
  1217. size_t index = 0;
  1218. auto match = binary_search(cached_path.span(), entry, &index, RunnablePathComparator {});
  1219. if (match)
  1220. return;
  1221. while (index < cached_path.size() && strcmp(cached_path[index].path.characters(), entry.path.characters()) < 0) {
  1222. index++;
  1223. }
  1224. cached_path.insert(index, entry);
  1225. }
  1226. void Shell::remove_entry_from_cache(StringView entry)
  1227. {
  1228. size_t index { 0 };
  1229. auto match = binary_search(cached_path.span(), entry, &index, RunnablePathComparator {});
  1230. if (match)
  1231. cached_path.remove(index);
  1232. }
  1233. ErrorOr<void> Shell::highlight(Line::Editor& editor) const
  1234. {
  1235. auto line = editor.line();
  1236. auto ast = parse(line, m_is_interactive);
  1237. if (!ast)
  1238. return {};
  1239. return ast->highlight_in_editor(editor, const_cast<Shell&>(*this));
  1240. }
  1241. Vector<Line::CompletionSuggestion> Shell::complete()
  1242. {
  1243. m_completion_stack_info = {};
  1244. return complete(m_editor->line(m_editor->cursor()));
  1245. }
  1246. Vector<Line::CompletionSuggestion> Shell::complete(StringView line)
  1247. {
  1248. auto ast = parse(line, m_is_interactive);
  1249. if (!ast)
  1250. return {};
  1251. return ast->complete_for_editor(*this, line.length()).release_value_but_fixme_should_propagate_errors();
  1252. }
  1253. Vector<Line::CompletionSuggestion> Shell::complete_path(StringView base, StringView part, size_t offset, ExecutableOnly executable_only, AST::Node const* command_node, AST::Node const* node, EscapeMode escape_mode)
  1254. {
  1255. auto token = offset ? part.substring_view(0, offset) : ""sv;
  1256. DeprecatedString path;
  1257. ssize_t last_slash = token.length() - 1;
  1258. while (last_slash >= 0 && token[last_slash] != '/')
  1259. --last_slash;
  1260. if (command_node) {
  1261. auto program_results = complete_via_program_itself(offset, command_node, node, escape_mode, {});
  1262. if (!program_results.is_error())
  1263. return program_results.release_value();
  1264. }
  1265. StringBuilder path_builder;
  1266. auto init_slash_part = token.substring_view(0, last_slash + 1);
  1267. auto last_slash_part = token.substring_view(last_slash + 1, token.length() - last_slash - 1);
  1268. bool allow_direct_children = true;
  1269. // Depending on the base, we will have to prepend cwd.
  1270. if (base.is_empty()) {
  1271. // '' /foo -> absolute
  1272. // '' foo -> relative
  1273. if (!token.starts_with('/'))
  1274. path_builder.append(cwd);
  1275. path_builder.append('/');
  1276. path_builder.append(init_slash_part);
  1277. if (executable_only == ExecutableOnly::Yes && init_slash_part.is_empty())
  1278. allow_direct_children = false;
  1279. } else {
  1280. // /foo * -> absolute
  1281. // foo * -> relative
  1282. if (!base.starts_with('/'))
  1283. path_builder.append(cwd);
  1284. path_builder.append('/');
  1285. path_builder.append(base);
  1286. path_builder.append('/');
  1287. path_builder.append(init_slash_part);
  1288. }
  1289. path = path_builder.to_deprecated_string();
  1290. token = last_slash_part;
  1291. // the invariant part of the token is actually just the last segment
  1292. // e. in `cd /foo/bar', 'bar' is the invariant
  1293. // since we are not suggesting anything starting with
  1294. // `/foo/', but rather just `bar...'
  1295. auto token_length = escape_token(token, escape_mode).length();
  1296. size_t static_offset = 0;
  1297. auto invariant_offset = token_length;
  1298. if (m_editor)
  1299. m_editor->transform_suggestion_offsets(invariant_offset, static_offset);
  1300. // only suggest dot-files if path starts with a dot
  1301. Core::DirIterator files(path,
  1302. token.starts_with('.') ? Core::DirIterator::SkipParentAndBaseDir : Core::DirIterator::SkipDots);
  1303. Vector<Line::CompletionSuggestion> suggestions;
  1304. while (files.has_next()) {
  1305. auto file = files.next_path();
  1306. if (file.starts_with(token)) {
  1307. struct stat program_status;
  1308. auto file_path = DeprecatedString::formatted("{}/{}", path, file);
  1309. int stat_error = stat(file_path.characters(), &program_status);
  1310. if (!stat_error && (executable_only == ExecutableOnly::No || access(file_path.characters(), X_OK) == 0)) {
  1311. if (S_ISDIR(program_status.st_mode)) {
  1312. suggestions.append({ escape_token(file, escape_mode), "/"sv });
  1313. } else {
  1314. if (!allow_direct_children && !file.contains('/'))
  1315. continue;
  1316. suggestions.append({ escape_token(file, escape_mode), " "sv });
  1317. }
  1318. suggestions.last().input_offset = token_length;
  1319. suggestions.last().invariant_offset = invariant_offset;
  1320. suggestions.last().static_offset = static_offset;
  1321. }
  1322. }
  1323. }
  1324. // The results of DirIterator are in the order they appear on-disk.
  1325. // Instead, return suggestions in lexicographical order.
  1326. quick_sort(suggestions, [](auto& a, auto& b) { return a.text_string < b.text_string; });
  1327. return suggestions;
  1328. }
  1329. Vector<Line::CompletionSuggestion> Shell::complete_program_name(StringView name, size_t offset, EscapeMode escape_mode)
  1330. {
  1331. auto match = binary_search(
  1332. cached_path.span(),
  1333. name,
  1334. nullptr,
  1335. [](auto& name, auto& program) {
  1336. return strncmp(
  1337. name.characters_without_null_termination(),
  1338. program.path.characters(),
  1339. name.length());
  1340. });
  1341. if (!match)
  1342. return complete_path(""sv, name, offset, ExecutableOnly::Yes, nullptr, nullptr, escape_mode);
  1343. DeprecatedString completion = match->path;
  1344. auto token_length = escape_token(name, escape_mode).length();
  1345. auto invariant_offset = token_length;
  1346. size_t static_offset = 0;
  1347. if (m_editor)
  1348. m_editor->transform_suggestion_offsets(invariant_offset, static_offset);
  1349. // Now that we have a program name starting with our token, we look at
  1350. // other program names starting with our token and cut off any mismatching
  1351. // characters.
  1352. Vector<Line::CompletionSuggestion> suggestions;
  1353. int index = match - cached_path.data();
  1354. for (int i = index - 1; i >= 0 && cached_path[i].path.starts_with(name); --i)
  1355. suggestions.append({ cached_path[i].path, " "sv });
  1356. for (size_t i = index + 1; i < cached_path.size() && cached_path[i].path.starts_with(name); ++i)
  1357. suggestions.append({ cached_path[i].path, " "sv });
  1358. suggestions.append({ cached_path[index].path, " "sv });
  1359. for (auto& entry : suggestions) {
  1360. entry.input_offset = token_length;
  1361. entry.invariant_offset = invariant_offset;
  1362. entry.static_offset = static_offset;
  1363. }
  1364. return suggestions;
  1365. }
  1366. Vector<Line::CompletionSuggestion> Shell::complete_variable(StringView name, size_t offset)
  1367. {
  1368. Vector<Line::CompletionSuggestion> suggestions;
  1369. auto pattern = offset ? name.substring_view(0, offset) : ""sv;
  1370. auto invariant_offset = offset;
  1371. size_t static_offset = 0;
  1372. if (m_editor)
  1373. m_editor->transform_suggestion_offsets(invariant_offset, static_offset);
  1374. // Look at local variables.
  1375. for (auto& frame : m_local_frames) {
  1376. for (auto& variable : frame->local_variables) {
  1377. if (variable.key.starts_with(pattern) && !suggestions.contains_slow(variable.key))
  1378. suggestions.append(variable.key);
  1379. }
  1380. }
  1381. // Look at the environment.
  1382. for (auto i = 0; environ[i]; ++i) {
  1383. StringView entry { environ[i], strlen(environ[i]) };
  1384. if (entry.starts_with(pattern)) {
  1385. auto parts = entry.split_view('=');
  1386. if (parts.is_empty() || parts.first().is_empty())
  1387. continue;
  1388. DeprecatedString name = parts.first();
  1389. if (suggestions.contains_slow(name))
  1390. continue;
  1391. suggestions.append(move(name));
  1392. }
  1393. }
  1394. for (auto& entry : suggestions) {
  1395. entry.input_offset = offset;
  1396. entry.invariant_offset = invariant_offset;
  1397. entry.static_offset = static_offset;
  1398. }
  1399. return suggestions;
  1400. }
  1401. Vector<Line::CompletionSuggestion> Shell::complete_user(StringView name, size_t offset)
  1402. {
  1403. Vector<Line::CompletionSuggestion> suggestions;
  1404. auto pattern = offset ? name.substring_view(0, offset) : ""sv;
  1405. auto invariant_offset = offset;
  1406. size_t static_offset = 0;
  1407. if (m_editor)
  1408. m_editor->transform_suggestion_offsets(invariant_offset, static_offset);
  1409. Core::DirIterator di("/home", Core::DirIterator::SkipParentAndBaseDir);
  1410. if (di.has_error())
  1411. return suggestions;
  1412. while (di.has_next()) {
  1413. DeprecatedString name = di.next_path();
  1414. if (name.starts_with(pattern)) {
  1415. suggestions.append(name);
  1416. auto& suggestion = suggestions.last();
  1417. suggestion.input_offset = offset;
  1418. suggestion.invariant_offset = invariant_offset;
  1419. suggestion.static_offset = static_offset;
  1420. }
  1421. }
  1422. return suggestions;
  1423. }
  1424. Vector<Line::CompletionSuggestion> Shell::complete_option(StringView program_name, StringView option, size_t offset, AST::Node const* command_node, AST::Node const* node)
  1425. {
  1426. if (command_node) {
  1427. auto program_results = complete_via_program_itself(offset, command_node, node, EscapeMode::Bareword, program_name);
  1428. if (!program_results.is_error())
  1429. return program_results.release_value();
  1430. }
  1431. size_t start = 0;
  1432. while (start < option.length() && option[start] == '-' && start < 2)
  1433. ++start;
  1434. auto option_pattern = offset > start ? option.substring_view(start, offset - start) : ""sv;
  1435. auto invariant_offset = offset;
  1436. size_t static_offset = 0;
  1437. if (m_editor)
  1438. m_editor->transform_suggestion_offsets(invariant_offset, static_offset);
  1439. dbgln("Shell::complete_option({}, {})", program_name, option_pattern);
  1440. return {};
  1441. }
  1442. ErrorOr<Vector<Line::CompletionSuggestion>> Shell::complete_via_program_itself(size_t, AST::Node const* command_node, AST::Node const* node, EscapeMode, StringView known_program_name)
  1443. {
  1444. if (!command_node)
  1445. return Error::from_string_literal("Cannot complete null command");
  1446. if (command_node->would_execute())
  1447. return Error::from_string_literal("Refusing to complete nodes that would execute");
  1448. String program_name_storage;
  1449. if (known_program_name.is_null()) {
  1450. auto node = command_node->leftmost_trivial_literal();
  1451. if (!node)
  1452. return Error::from_string_literal("Cannot complete");
  1453. program_name_storage = TRY(TRY(const_cast<AST::Node&>(*node).run(*this))->resolve_as_string(*this));
  1454. known_program_name = program_name_storage;
  1455. }
  1456. AST::Command completion_command;
  1457. completion_command.argv.append(program_name_storage);
  1458. completion_command = TRY(expand_aliases({ completion_command })).last();
  1459. auto completion_utility_name = TRY(String::formatted("_complete_{}", completion_command.argv[0]));
  1460. if (binary_search(cached_path.span(), completion_utility_name, nullptr, RunnablePathComparator {}) != nullptr)
  1461. completion_command.argv[0] = completion_utility_name;
  1462. else if (!options.invoke_program_for_autocomplete)
  1463. return Error::from_string_literal("Refusing to use the program itself as completion source");
  1464. completion_command.argv.extend({ TRY("--complete"_string), "--"_short_string });
  1465. struct Visitor : public AST::NodeVisitor {
  1466. Visitor(Shell& shell, AST::Position position)
  1467. : shell(shell)
  1468. , completion_position(position)
  1469. {
  1470. lists.empend();
  1471. }
  1472. Shell& shell;
  1473. AST::Position completion_position;
  1474. Vector<Vector<String>> lists;
  1475. bool fail { false };
  1476. void push_list() { lists.empend(); }
  1477. Vector<String> pop_list() { return lists.take_last(); }
  1478. Vector<String>& list() { return lists.last(); }
  1479. bool should_include(AST::Node const* node) const { return node->position().end_offset <= completion_position.end_offset; }
  1480. virtual void visit(AST::BarewordLiteral const* node) override
  1481. {
  1482. if (should_include(node))
  1483. list().append(node->text());
  1484. }
  1485. virtual void visit(AST::BraceExpansion const* node) override
  1486. {
  1487. if (should_include(node)) {
  1488. auto value = static_cast<AST::Node*>(const_cast<AST::BraceExpansion*>(node))->run(shell).release_value_but_fixme_should_propagate_errors();
  1489. auto entries = value->resolve_as_list(shell).release_value_but_fixme_should_propagate_errors();
  1490. list().extend(move(entries));
  1491. }
  1492. }
  1493. virtual void visit(AST::CommandLiteral const* node) override
  1494. {
  1495. if (should_include(node))
  1496. list().extend(node->command().argv);
  1497. }
  1498. virtual void visit(AST::DynamicEvaluate const* node) override
  1499. {
  1500. if (should_include(node))
  1501. fail = true;
  1502. }
  1503. virtual void visit(AST::DoubleQuotedString const* node) override
  1504. {
  1505. if (!should_include(node))
  1506. return;
  1507. push_list();
  1508. AST::NodeVisitor::visit(node);
  1509. auto list = pop_list();
  1510. StringBuilder builder;
  1511. builder.join(""sv, list);
  1512. this->list().append(builder.to_string().release_value_but_fixme_should_propagate_errors());
  1513. }
  1514. virtual void visit(AST::Glob const* node) override
  1515. {
  1516. if (should_include(node))
  1517. list().append(node->text());
  1518. }
  1519. virtual void visit(AST::Heredoc const* node) override
  1520. {
  1521. if (!should_include(node))
  1522. return;
  1523. push_list();
  1524. AST::NodeVisitor::visit(node);
  1525. auto list = pop_list();
  1526. StringBuilder builder;
  1527. builder.join(""sv, list);
  1528. this->list().append(builder.to_string().release_value_but_fixme_should_propagate_errors());
  1529. }
  1530. virtual void visit(AST::ImmediateExpression const* node) override
  1531. {
  1532. if (should_include(node))
  1533. fail = true;
  1534. }
  1535. virtual void visit(AST::Range const* node) override
  1536. {
  1537. if (!should_include(node))
  1538. return;
  1539. push_list();
  1540. node->start()->visit(*this);
  1541. list().append(pop_list().first());
  1542. }
  1543. virtual void visit(AST::SimpleVariable const* node) override
  1544. {
  1545. if (should_include(node)) {
  1546. auto values = static_cast<AST::Node*>(const_cast<AST::SimpleVariable*>(node))->run(shell).release_value_but_fixme_should_propagate_errors();
  1547. auto entries = values->resolve_as_list(shell).release_value_but_fixme_should_propagate_errors();
  1548. list().extend(move(entries));
  1549. }
  1550. }
  1551. virtual void visit(AST::SpecialVariable const* node) override
  1552. {
  1553. if (should_include(node)) {
  1554. auto values = static_cast<AST::Node*>(const_cast<AST::SpecialVariable*>(node))->run(shell).release_value_but_fixme_should_propagate_errors();
  1555. auto entries = values->resolve_as_list(shell).release_value_but_fixme_should_propagate_errors();
  1556. list().extend(move(entries));
  1557. }
  1558. }
  1559. virtual void visit(AST::Juxtaposition const* node) override
  1560. {
  1561. if (!should_include(node))
  1562. return;
  1563. push_list();
  1564. node->left()->visit(*this);
  1565. auto left = pop_list();
  1566. push_list();
  1567. node->right()->visit(*this);
  1568. auto right = pop_list();
  1569. StringBuilder builder;
  1570. for (auto& left_entry : left) {
  1571. for (auto& right_entry : right) {
  1572. builder.append(left_entry);
  1573. builder.append(right_entry);
  1574. list().append(builder.to_string().release_value_but_fixme_should_propagate_errors());
  1575. builder.clear();
  1576. }
  1577. }
  1578. }
  1579. virtual void visit(AST::StringLiteral const* node) override
  1580. {
  1581. if (should_include(node))
  1582. list().append(node->text());
  1583. }
  1584. virtual void visit(AST::Tilde const* node) override
  1585. {
  1586. if (should_include(node)) {
  1587. auto values = static_cast<AST::Node*>(const_cast<AST::Tilde*>(node))->run(shell).release_value_but_fixme_should_propagate_errors();
  1588. auto entries = values->resolve_as_list(shell).release_value_but_fixme_should_propagate_errors();
  1589. list().extend(move(entries));
  1590. }
  1591. }
  1592. virtual void visit(AST::PathRedirectionNode const*) override { }
  1593. virtual void visit(AST::CloseFdRedirection const*) override { }
  1594. virtual void visit(AST::Fd2FdRedirection const*) override { }
  1595. virtual void visit(AST::Execute const*) override { }
  1596. virtual void visit(AST::ReadRedirection const*) override { }
  1597. virtual void visit(AST::ReadWriteRedirection const*) override { }
  1598. virtual void visit(AST::WriteAppendRedirection const*) override { }
  1599. virtual void visit(AST::WriteRedirection const*) override { }
  1600. } visitor { *this, node ? node->position() : AST::Position() };
  1601. command_node->visit(visitor);
  1602. if (visitor.fail)
  1603. return Error::from_string_literal("Cannot complete");
  1604. completion_command.argv.extend(visitor.list());
  1605. auto devnull = TRY("/dev/null"_string);
  1606. completion_command.should_wait = true;
  1607. completion_command.redirections.append(AST::PathRedirection::create(devnull, STDERR_FILENO, AST::PathRedirection::Write));
  1608. completion_command.redirections.append(AST::PathRedirection::create(devnull, STDIN_FILENO, AST::PathRedirection::Read));
  1609. auto execute_node = make_ref_counted<AST::Execute>(
  1610. AST::Position {},
  1611. make_ref_counted<AST::CommandLiteral>(AST::Position {}, move(completion_command)),
  1612. true);
  1613. Vector<Line::CompletionSuggestion> suggestions;
  1614. auto timer = TRY(Core::Timer::create_single_shot(300, [&] {
  1615. Core::EventLoop::current().quit(1);
  1616. }));
  1617. timer->start();
  1618. // Restrict the process to effectively readonly access to the FS.
  1619. auto scoped_promise = promise({
  1620. .exec_promises = "stdio rpath prot_exec map_fixed no_error",
  1621. .unveils = {
  1622. { "/", "rx" },
  1623. },
  1624. });
  1625. {
  1626. TemporaryChange change(m_is_interactive, false);
  1627. TRY(execute_node->for_each_entry(*this, [&](NonnullRefPtr<AST::Value> entry) -> ErrorOr<IterationDecision> {
  1628. auto result = TRY(entry->resolve_as_string(*this));
  1629. JsonParser parser(result);
  1630. auto parsed_result = parser.parse();
  1631. if (parsed_result.is_error())
  1632. return IterationDecision::Continue;
  1633. auto parsed = parsed_result.release_value();
  1634. if (parsed.is_object()) {
  1635. auto& object = parsed.as_object();
  1636. auto kind = object.get_deprecated_string("kind"sv).value_or("plain");
  1637. if (kind == "path") {
  1638. auto base = object.get_deprecated_string("base"sv).value_or("");
  1639. auto part = object.get_deprecated_string("part"sv).value_or("");
  1640. auto executable_only = object.get_bool("executable_only"sv).value_or(false) ? ExecutableOnly::Yes : ExecutableOnly::No;
  1641. suggestions.extend(complete_path(base, part, part.length(), executable_only, nullptr, nullptr));
  1642. } else if (kind == "program") {
  1643. auto name = object.get_deprecated_string("name"sv).value_or("");
  1644. suggestions.extend(complete_program_name(name, name.length()));
  1645. } else if (kind == "proxy") {
  1646. if (m_completion_stack_info.size_free() < 4 * KiB) {
  1647. dbgln("Not enough stack space, recursion?");
  1648. return IterationDecision::Continue;
  1649. }
  1650. auto argv = object.get_deprecated_string("argv"sv).value_or("");
  1651. dbgln("Proxy completion for {}", argv);
  1652. suggestions.extend(complete(argv));
  1653. } else if (kind == "plain") {
  1654. Line::CompletionSuggestion suggestion {
  1655. object.get_deprecated_string("completion"sv).value_or(""),
  1656. object.get_deprecated_string("trailing_trivia"sv).value_or(""),
  1657. object.get_deprecated_string("display_trivia"sv).value_or(""),
  1658. };
  1659. suggestion.static_offset = object.get_u64("static_offset"sv).value_or(0);
  1660. suggestion.invariant_offset = object.get_u64("invariant_offset"sv).value_or(0);
  1661. suggestion.allow_commit_without_listing = object.get_bool("allow_commit_without_listing"sv).value_or(true);
  1662. suggestions.append(move(suggestion));
  1663. } else {
  1664. dbgln("LibLine: Unhandled completion kind: {}", kind);
  1665. }
  1666. } else {
  1667. suggestions.append(parsed.to_deprecated_string());
  1668. }
  1669. return IterationDecision::Continue;
  1670. }));
  1671. }
  1672. auto pgid = getpgrp();
  1673. tcsetpgrp(STDOUT_FILENO, pgid);
  1674. tcsetpgrp(STDIN_FILENO, pgid);
  1675. if (suggestions.is_empty())
  1676. return Error::from_string_literal("No results");
  1677. return suggestions;
  1678. }
  1679. Vector<Line::CompletionSuggestion> Shell::complete_immediate_function_name(StringView name, size_t offset)
  1680. {
  1681. Vector<Line::CompletionSuggestion> suggestions;
  1682. auto invariant_offset = offset;
  1683. size_t static_offset = 0;
  1684. if (m_editor)
  1685. m_editor->transform_suggestion_offsets(invariant_offset, static_offset);
  1686. #define __ENUMERATE_SHELL_IMMEDIATE_FUNCTION(fn_name) \
  1687. if (auto name_view = #fn_name##sv; name_view.starts_with(name)) \
  1688. suggestions.append({ name_view, " "sv });
  1689. ENUMERATE_SHELL_IMMEDIATE_FUNCTIONS();
  1690. #undef __ENUMERATE_SHELL_IMMEDIATE_FUNCTION
  1691. for (auto& entry : suggestions) {
  1692. entry.input_offset = offset;
  1693. entry.invariant_offset = invariant_offset;
  1694. entry.static_offset = static_offset;
  1695. }
  1696. return suggestions;
  1697. }
  1698. void Shell::bring_cursor_to_beginning_of_a_line() const
  1699. {
  1700. struct winsize ws;
  1701. if (m_editor) {
  1702. ws = m_editor->terminal_size();
  1703. } else {
  1704. if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) < 0) {
  1705. // Very annoying assumptions.
  1706. ws.ws_col = 80;
  1707. ws.ws_row = 25;
  1708. }
  1709. }
  1710. // Black with Cyan background.
  1711. constexpr auto default_mark = "\e[30;46m%\e[0m";
  1712. DeprecatedString eol_mark = getenv("PROMPT_EOL_MARK");
  1713. if (eol_mark.is_null())
  1714. eol_mark = default_mark;
  1715. size_t eol_mark_length = Line::Editor::actual_rendered_string_metrics(eol_mark).line_metrics.last().total_length();
  1716. if (eol_mark_length >= ws.ws_col) {
  1717. eol_mark = default_mark;
  1718. eol_mark_length = 1;
  1719. }
  1720. fputs(eol_mark.characters(), stderr);
  1721. // We write a line's worth of whitespace to the terminal. This way, we ensure that
  1722. // the prompt ends up on a new line even if there is dangling output on the current line.
  1723. size_t fill_count = ws.ws_col - eol_mark_length;
  1724. auto fill_buffer = DeprecatedString::repeated(' ', fill_count);
  1725. fwrite(fill_buffer.characters(), 1, fill_count, stderr);
  1726. putc('\r', stderr);
  1727. }
  1728. bool Shell::has_history_event(StringView source)
  1729. {
  1730. struct : public AST::NodeVisitor {
  1731. virtual void visit(const AST::HistoryEvent* node) override
  1732. {
  1733. has_history_event = true;
  1734. AST::NodeVisitor::visit(node);
  1735. }
  1736. bool has_history_event { false };
  1737. } visitor;
  1738. auto ast = Parser { source, true }.parse();
  1739. if (!ast)
  1740. return false;
  1741. ast->visit(visitor);
  1742. return visitor.has_history_event;
  1743. }
  1744. bool Shell::read_single_line()
  1745. {
  1746. while (true) {
  1747. restore_ios();
  1748. bring_cursor_to_beginning_of_a_line();
  1749. auto line_result = m_editor->get_line(prompt());
  1750. if (line_result.is_error()) {
  1751. auto is_eof = line_result.error() == Line::Editor::Error::Eof;
  1752. auto is_empty = line_result.error() == Line::Editor::Error::Empty;
  1753. if (is_eof || is_empty) {
  1754. // Pretend the user tried to execute builtin_exit()
  1755. auto exit_code = run_command("exit"sv);
  1756. if (exit_code != 0) {
  1757. // If we didn't end up actually calling exit(), and the command didn't succeed, just pretend it's all okay
  1758. // unless we can't, then just quit anyway.
  1759. if (!is_empty)
  1760. continue;
  1761. }
  1762. }
  1763. Core::EventLoop::current().quit(1);
  1764. return false;
  1765. }
  1766. auto& line = line_result.value();
  1767. if (line.is_empty())
  1768. return true;
  1769. run_command(line);
  1770. if (!has_history_event(line))
  1771. m_editor->add_to_history(line);
  1772. return true;
  1773. }
  1774. }
  1775. void Shell::custom_event(Core::CustomEvent& event)
  1776. {
  1777. if (event.custom_type() == ReadLine) {
  1778. if (read_single_line())
  1779. Core::EventLoop::current().post_event(*this, make<Core::CustomEvent>(ShellEventType::ReadLine));
  1780. return;
  1781. }
  1782. }
  1783. void Shell::notify_child_event()
  1784. {
  1785. Vector<u64> disowned_jobs;
  1786. // Workaround the fact that we can't receive *who* exactly changed state.
  1787. // The child might still be alive (and even running) when this signal is dispatched to us
  1788. // so just...repeat until we find a suitable child.
  1789. // This, of course, will mean that someone can send us a SIGCHILD and we'd be spinning here
  1790. // until the next child event we can actually handle, so stop after spending a total of 5110us (~5ms) on it.
  1791. bool found_child = false;
  1792. size_t const max_tries = 10;
  1793. size_t valid_attempts = max_tries;
  1794. useconds_t backoff_usec = 20;
  1795. int backoff_multiplier = 2;
  1796. for (;;) {
  1797. if (found_child || --valid_attempts == 0)
  1798. break;
  1799. // Ignore stray SIGCHLD when there are no jobs.
  1800. if (jobs.is_empty())
  1801. return;
  1802. if (valid_attempts < max_tries - 1) {
  1803. usleep(backoff_usec);
  1804. backoff_usec *= backoff_multiplier;
  1805. }
  1806. for (auto& it : jobs) {
  1807. auto job_id = it.key;
  1808. auto& job = *it.value;
  1809. int wstatus = 0;
  1810. dbgln_if(SH_DEBUG, "waitpid({} = {}) = ...", job.pid(), job.cmd());
  1811. auto child_pid = waitpid(job.pid(), &wstatus, WNOHANG | WUNTRACED);
  1812. dbgln_if(SH_DEBUG, "... = {} - exited: {}, suspended: {}", child_pid, WIFEXITED(wstatus), WIFSTOPPED(wstatus));
  1813. if (child_pid < 0) {
  1814. if (errno == ECHILD) {
  1815. // The child process went away before we could process its death, just assume it exited all ok.
  1816. // FIXME: This should never happen, the child should stay around until we do the waitpid above.
  1817. child_pid = job.pid();
  1818. } else {
  1819. VERIFY_NOT_REACHED();
  1820. }
  1821. }
  1822. if (child_pid == 0) {
  1823. // If the child existed, but wasn't dead.
  1824. if (job.is_suspended() || job.shell_did_continue()) {
  1825. // The job was suspended, and someone sent it a SIGCONT.
  1826. job.set_is_suspended(false);
  1827. if (job.shell_did_continue())
  1828. job.set_shell_did_continue(false);
  1829. found_child = true;
  1830. }
  1831. continue;
  1832. }
  1833. if (child_pid == job.pid()) {
  1834. if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) {
  1835. auto signal = WTERMSIG(wstatus);
  1836. job.set_signalled(signal);
  1837. if (signal == SIGINT)
  1838. raise_error(ShellError::InternalControlFlowInterrupted, "Interrupted"sv, job.command().position);
  1839. else if (signal == SIGKILL)
  1840. raise_error(ShellError::InternalControlFlowKilled, "Interrupted"sv, job.command().position);
  1841. } else if (WIFEXITED(wstatus)) {
  1842. job.set_has_exit(WEXITSTATUS(wstatus));
  1843. } else if (WIFSTOPPED(wstatus)) {
  1844. job.unblock();
  1845. job.set_is_suspended(true);
  1846. }
  1847. found_child = true;
  1848. }
  1849. if (job.should_be_disowned())
  1850. disowned_jobs.append(job_id);
  1851. }
  1852. for (auto job_id : disowned_jobs) {
  1853. jobs.remove(job_id);
  1854. }
  1855. }
  1856. }
  1857. Shell::Shell()
  1858. : m_default_constructed(true)
  1859. {
  1860. push_frame("main", LocalFrameKind::FunctionOrGlobal).leak_frame();
  1861. int rc = gethostname(hostname, Shell::HostNameSize);
  1862. if (rc < 0)
  1863. perror("gethostname");
  1864. {
  1865. auto* pw = getpwuid(getuid());
  1866. if (pw) {
  1867. username = pw->pw_name;
  1868. home = pw->pw_dir;
  1869. setenv("HOME", pw->pw_dir, 1);
  1870. }
  1871. endpwent();
  1872. }
  1873. // For simplicity, start at the user's home directory.
  1874. this->cwd = home;
  1875. setenv("PWD", home.characters(), 1);
  1876. // Add the default PATH vars.
  1877. {
  1878. StringBuilder path;
  1879. auto const* path_env_ptr = getenv("PATH");
  1880. if (path_env_ptr != NULL)
  1881. path.append({ path_env_ptr, strlen(path_env_ptr) });
  1882. if (path.length())
  1883. path.append(":"sv);
  1884. path.append(DEFAULT_PATH_SV);
  1885. setenv("PATH", path.to_deprecated_string().characters(), true);
  1886. }
  1887. cache_path();
  1888. }
  1889. Shell::Shell(Line::Editor& editor, bool attempt_interactive, bool posix_mode)
  1890. : m_in_posix_mode(posix_mode)
  1891. , m_editor(editor)
  1892. {
  1893. uid = getuid();
  1894. tcsetpgrp(0, getpgrp());
  1895. m_pid = getpid();
  1896. push_frame("main", LocalFrameKind::FunctionOrGlobal).leak_frame();
  1897. int rc = gethostname(hostname, Shell::HostNameSize);
  1898. if (rc < 0)
  1899. perror("gethostname");
  1900. auto istty = isatty(STDIN_FILENO);
  1901. m_is_interactive = attempt_interactive && istty;
  1902. if (istty) {
  1903. rc = ttyname_r(0, ttyname, Shell::TTYNameSize);
  1904. if (rc < 0)
  1905. perror("ttyname_r");
  1906. } else {
  1907. ttyname[0] = 0;
  1908. }
  1909. {
  1910. auto* cwd = getcwd(nullptr, 0);
  1911. this->cwd = cwd;
  1912. setenv("PWD", cwd, 1);
  1913. free(cwd);
  1914. }
  1915. {
  1916. auto* pw = getpwuid(getuid());
  1917. if (pw) {
  1918. username = pw->pw_name;
  1919. home = pw->pw_dir;
  1920. setenv("HOME", pw->pw_dir, 1);
  1921. }
  1922. endpwent();
  1923. }
  1924. directory_stack.append(cwd);
  1925. if (m_is_interactive) {
  1926. m_editor->load_history(get_history_path());
  1927. cache_path();
  1928. }
  1929. m_editor->register_key_input_callback('\n', [this](Line::Editor& editor) {
  1930. auto ast = parse(editor.line(), false);
  1931. if (ast && ast->is_syntax_error() && ast->syntax_error_node().is_continuable())
  1932. return true;
  1933. return EDITOR_INTERNAL_FUNCTION(finish)(editor);
  1934. });
  1935. start_timer(3000);
  1936. }
  1937. Shell::~Shell()
  1938. {
  1939. if (m_default_constructed)
  1940. return;
  1941. stop_all_jobs();
  1942. if (!m_is_interactive)
  1943. return;
  1944. m_editor->save_history(get_history_path());
  1945. }
  1946. void Shell::stop_all_jobs()
  1947. {
  1948. if (!jobs.is_empty()) {
  1949. if (m_is_interactive && !m_is_subshell)
  1950. printf("Killing active jobs\n");
  1951. for (auto& entry : jobs) {
  1952. if (entry.value->is_suspended()) {
  1953. dbgln_if(SH_DEBUG, "Job {} is suspended", entry.value->pid());
  1954. kill_job(entry.value, SIGCONT);
  1955. }
  1956. kill_job(entry.value, SIGHUP);
  1957. }
  1958. usleep(10000); // Wait for a bit before killing the job
  1959. for (auto& entry : jobs) {
  1960. dbgln_if(SH_DEBUG, "Actively killing {} ({})", entry.value->pid(), entry.value->cmd());
  1961. kill_job(entry.value, SIGKILL);
  1962. }
  1963. jobs.clear();
  1964. }
  1965. }
  1966. u64 Shell::find_last_job_id() const
  1967. {
  1968. u64 job_id = 0;
  1969. for (auto& entry : jobs) {
  1970. if (entry.value->job_id() > job_id)
  1971. job_id = entry.value->job_id();
  1972. }
  1973. return job_id;
  1974. }
  1975. Job* Shell::find_job(u64 id, bool is_pid)
  1976. {
  1977. for (auto& entry : jobs) {
  1978. if (is_pid) {
  1979. if (entry.value->pid() == static_cast<int>(id))
  1980. return entry.value;
  1981. } else {
  1982. if (entry.value->job_id() == id)
  1983. return entry.value;
  1984. }
  1985. }
  1986. return nullptr;
  1987. }
  1988. void Shell::kill_job(Job const* job, int sig)
  1989. {
  1990. if (!job)
  1991. return;
  1992. if (killpg(job->pgid(), sig) < 0) {
  1993. if (kill(job->pid(), sig) < 0) {
  1994. if (errno != ESRCH)
  1995. perror("kill");
  1996. }
  1997. }
  1998. }
  1999. void Shell::possibly_print_error() const
  2000. {
  2001. switch (m_error) {
  2002. case ShellError::EvaluatedSyntaxError:
  2003. warnln("Shell Syntax Error: {}", m_error_description);
  2004. break;
  2005. case ShellError::InvalidSliceContentsError:
  2006. case ShellError::InvalidGlobError:
  2007. case ShellError::NonExhaustiveMatchRules:
  2008. warnln("Shell: {}", m_error_description);
  2009. break;
  2010. case ShellError::OpenFailure:
  2011. warnln("Shell: Open failed for {}", m_error_description);
  2012. break;
  2013. case ShellError::OutOfMemory:
  2014. warnln("Shell: Hit an OOM situation");
  2015. break;
  2016. case ShellError::LaunchError:
  2017. warnln("Shell: {}", m_error_description);
  2018. break;
  2019. case ShellError::PipeFailure:
  2020. warnln("Shell: pipe() failed for {}", m_error_description);
  2021. break;
  2022. case ShellError::WriteFailure:
  2023. warnln("Shell: write() failed for {}", m_error_description);
  2024. break;
  2025. case ShellError::InternalControlFlowBreak:
  2026. case ShellError::InternalControlFlowContinue:
  2027. case ShellError::InternalControlFlowInterrupted:
  2028. case ShellError::InternalControlFlowKilled:
  2029. return;
  2030. case ShellError::None:
  2031. return;
  2032. }
  2033. if (m_source_position.has_value() && m_source_position->position.has_value()) {
  2034. auto& source_position = m_source_position.value();
  2035. auto do_line = [&](auto line, auto& current_line) {
  2036. auto is_in_range = line >= (i64)source_position.position->start_line.line_number && line <= (i64)source_position.position->end_line.line_number;
  2037. warnln("{:>3}| {}", line, current_line);
  2038. if (is_in_range) {
  2039. warn("\x1b[31m");
  2040. size_t length_written_so_far = 0;
  2041. if (line == (i64)source_position.position->start_line.line_number) {
  2042. warn("{:~>{}}", "", 5 + source_position.position->start_line.line_column);
  2043. length_written_so_far += source_position.position->start_line.line_column;
  2044. } else {
  2045. warn("{:~>{}}", "", 5);
  2046. }
  2047. if (line == (i64)source_position.position->end_line.line_number) {
  2048. warn("{:^>{}}", "", source_position.position->end_line.line_column - length_written_so_far);
  2049. length_written_so_far += source_position.position->start_line.line_column;
  2050. } else {
  2051. warn("{:^>{}}", "", current_line.length() - length_written_so_far);
  2052. }
  2053. warnln("\x1b[0m");
  2054. }
  2055. };
  2056. int line = -1;
  2057. // FIXME: Support arbitrarily long lines?
  2058. auto line_buf_or_error = ByteBuffer::create_uninitialized(4 * KiB);
  2059. if (line_buf_or_error.is_error()) {
  2060. warnln("Shell: Internal error while trying to display source information: {} (while allocating line buffer for {})", line_buf_or_error.error(), source_position.source_file);
  2061. return;
  2062. }
  2063. auto line_buf = line_buf_or_error.release_value();
  2064. StringView current_line;
  2065. i64 line_to_skip_to = max(source_position.position->start_line.line_number, 2ul) - 2;
  2066. if (!source_position.source_file.is_null()) {
  2067. auto file_or_error = Core::File::open(source_position.source_file, Core::File::OpenMode::Read);
  2068. if (file_or_error.is_error()) {
  2069. warnln("Shell: Internal error while trying to display source information: {} (while reading '{}')", file_or_error.error(), source_position.source_file);
  2070. return;
  2071. }
  2072. auto file = Core::InputBufferedFile::create(file_or_error.release_value());
  2073. while (line < line_to_skip_to) {
  2074. if (file.value()->is_eof())
  2075. return;
  2076. auto current_line_or_error = file.value()->read_line(line_buf);
  2077. if (current_line_or_error.is_error()) {
  2078. warnln("Shell: Internal error while trying to display source information: {} (while reading line {} of '{}')", current_line_or_error.error(), line, source_position.source_file);
  2079. return;
  2080. }
  2081. current_line = current_line_or_error.release_value();
  2082. ++line;
  2083. }
  2084. for (; line < (i64)source_position.position->end_line.line_number + 2; ++line) {
  2085. do_line(line, current_line);
  2086. if (file.value()->is_eof()) {
  2087. current_line = ""sv;
  2088. } else {
  2089. auto current_line_or_error = file.value()->read_line(line_buf);
  2090. if (current_line_or_error.is_error()) {
  2091. warnln("Shell: Internal error while trying to display source information: {} (while reading line {} of '{}')", current_line_or_error.error(), line, source_position.source_file);
  2092. return;
  2093. }
  2094. current_line = current_line_or_error.release_value();
  2095. }
  2096. }
  2097. } else if (!source_position.literal_source_text.is_empty()) {
  2098. GenericLexer lexer { source_position.literal_source_text };
  2099. while (line < line_to_skip_to) {
  2100. if (lexer.is_eof())
  2101. return;
  2102. current_line = lexer.consume_line();
  2103. ++line;
  2104. }
  2105. for (; line < (i64)source_position.position->end_line.line_number + 2; ++line) {
  2106. do_line(line, current_line);
  2107. if (lexer.is_eof())
  2108. current_line = ""sv;
  2109. else
  2110. current_line = lexer.consume_line();
  2111. }
  2112. }
  2113. }
  2114. warnln();
  2115. }
  2116. Optional<int> Shell::resolve_job_spec(StringView str)
  2117. {
  2118. if (!str.starts_with('%'))
  2119. return {};
  2120. // %number -> job id <number>
  2121. if (auto number = str.substring_view(1).to_uint(); number.has_value())
  2122. return number.value();
  2123. // '%?str' -> iterate jobs and pick one with `str' in its command
  2124. // Note: must be quoted, since '?' will turn it into a glob - pretty ugly...
  2125. GenericLexer lexer { str.substring_view(1) };
  2126. if (!lexer.consume_specific('?'))
  2127. return {};
  2128. auto search_term = lexer.remaining();
  2129. for (auto& it : jobs) {
  2130. if (it.value->cmd().contains(search_term))
  2131. return it.key;
  2132. }
  2133. return {};
  2134. }
  2135. void Shell::timer_event(Core::TimerEvent& event)
  2136. {
  2137. event.accept();
  2138. if (m_is_subshell)
  2139. return;
  2140. auto const* autosave_env_ptr = getenv("HISTORY_AUTOSAVE_TIME_MS");
  2141. auto option = autosave_env_ptr != NULL ? StringView { autosave_env_ptr, strlen(autosave_env_ptr) } : StringView {};
  2142. auto time = option.to_uint();
  2143. if (!time.has_value() || time.value() == 0) {
  2144. m_history_autosave_time.clear();
  2145. stop_timer();
  2146. start_timer(3000);
  2147. return;
  2148. }
  2149. if (m_history_autosave_time != time) {
  2150. m_history_autosave_time = time.value();
  2151. stop_timer();
  2152. start_timer(m_history_autosave_time.value());
  2153. }
  2154. if (!m_history_autosave_time.has_value())
  2155. return;
  2156. if (m_editor && m_editor->is_history_dirty())
  2157. m_editor->save_history(get_history_path());
  2158. }
  2159. RefPtr<AST::Node> Shell::parse(StringView input, bool interactive, bool as_command) const
  2160. {
  2161. if (m_in_posix_mode) {
  2162. Posix::Parser parser(input);
  2163. if (as_command) {
  2164. auto node = parser.parse();
  2165. if constexpr (SHELL_POSIX_PARSER_DEBUG) {
  2166. dbgln("Parsed with the POSIX Parser:");
  2167. (void)node->dump(0);
  2168. }
  2169. return node;
  2170. }
  2171. return parser.parse_word_list();
  2172. }
  2173. Parser parser { input, interactive };
  2174. if (as_command)
  2175. return parser.parse();
  2176. auto nodes = parser.parse_as_multiple_expressions();
  2177. return make_ref_counted<AST::ListConcatenate>(
  2178. nodes.is_empty() ? AST::Position { 0, 0, { 0, 0 }, { 0, 0 } } : nodes.first()->position(),
  2179. move(nodes));
  2180. }
  2181. void FileDescriptionCollector::collect()
  2182. {
  2183. for (auto fd : m_fds)
  2184. close(fd);
  2185. m_fds.clear();
  2186. }
  2187. FileDescriptionCollector::~FileDescriptionCollector()
  2188. {
  2189. collect();
  2190. }
  2191. void FileDescriptionCollector::add(int fd)
  2192. {
  2193. m_fds.append(fd);
  2194. }
  2195. SavedFileDescriptors::SavedFileDescriptors(Vector<NonnullRefPtr<AST::Rewiring>> const& intended_rewirings)
  2196. {
  2197. for (auto& rewiring : intended_rewirings) {
  2198. int new_fd = dup(rewiring->new_fd);
  2199. if (new_fd < 0) {
  2200. if (errno != EBADF)
  2201. perror("dup");
  2202. // The fd that will be overwritten isn't open right now,
  2203. // it will be cleaned up by the exec()-side collector
  2204. // and we have nothing to do here, so just ignore this error.
  2205. continue;
  2206. }
  2207. auto flags = fcntl(new_fd, F_GETFD);
  2208. auto rc = fcntl(new_fd, F_SETFD, flags | FD_CLOEXEC);
  2209. VERIFY(rc == 0);
  2210. m_saves.append({ rewiring->new_fd, new_fd });
  2211. m_collector.add(new_fd);
  2212. }
  2213. }
  2214. SavedFileDescriptors::~SavedFileDescriptors()
  2215. {
  2216. for (auto& save : m_saves) {
  2217. if (dup2(save.saved, save.original) < 0) {
  2218. perror("dup2(~SavedFileDescriptors)");
  2219. continue;
  2220. }
  2221. }
  2222. }
  2223. }