Shell.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. /*
  2. * Copyright (c) 2020, The SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Shell.h"
  27. #include "Execution.h"
  28. #include "Formatter.h"
  29. #include <AK/Function.h>
  30. #include <AK/LexicalPath.h>
  31. #include <AK/ScopeGuard.h>
  32. #include <AK/StringBuilder.h>
  33. #include <AK/TemporaryChange.h>
  34. #include <LibCore/ArgsParser.h>
  35. #include <LibCore/DirIterator.h>
  36. #include <LibCore/Event.h>
  37. #include <LibCore/EventLoop.h>
  38. #include <LibCore/File.h>
  39. #include <LibLine/Editor.h>
  40. #include <errno.h>
  41. #include <fcntl.h>
  42. #include <inttypes.h>
  43. #include <pwd.h>
  44. #include <signal.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <sys/mman.h>
  49. #include <sys/stat.h>
  50. #include <sys/utsname.h>
  51. #include <sys/wait.h>
  52. #include <termios.h>
  53. #include <unistd.h>
  54. static bool s_disable_hyperlinks = false;
  55. extern char** environ;
  56. //#define SH_DEBUG
  57. namespace Shell {
  58. // FIXME: This should eventually be removed once we've established that
  59. // waitpid() is not passed the same job twice.
  60. #ifdef __serenity__
  61. # define ENSURE_WAITID_ONCE
  62. #endif
  63. void Shell::setup_signals()
  64. {
  65. if (m_should_reinstall_signal_handlers) {
  66. Core::EventLoop::register_signal(SIGCHLD, [this](int) {
  67. #ifdef SH_DEBUG
  68. dbgln("SIGCHLD!");
  69. #endif
  70. notify_child_event();
  71. });
  72. Core::EventLoop::register_signal(SIGTSTP, [this](auto) {
  73. auto job = current_job();
  74. kill_job(job, SIGTSTP);
  75. if (job) {
  76. job->set_is_suspended(true);
  77. job->unblock();
  78. }
  79. });
  80. }
  81. }
  82. void Shell::print_path(const String& path)
  83. {
  84. if (s_disable_hyperlinks || !m_is_interactive) {
  85. printf("%s", path.characters());
  86. return;
  87. }
  88. printf("\033]8;;file://%s%s\033\\%s\033]8;;\033\\", hostname, path.characters(), path.characters());
  89. }
  90. String Shell::prompt() const
  91. {
  92. auto build_prompt = [&]() -> String {
  93. auto* ps1 = getenv("PROMPT");
  94. if (!ps1) {
  95. if (uid == 0)
  96. return "# ";
  97. StringBuilder builder;
  98. builder.appendf("\033]0;%s@%s:%s\007", username.characters(), hostname, cwd.characters());
  99. builder.appendf("\033[31;1m%s\033[0m@\033[37;1m%s\033[0m:\033[32;1m%s\033[0m$> ", username.characters(), hostname, cwd.characters());
  100. return builder.to_string();
  101. }
  102. StringBuilder builder;
  103. for (char* ptr = ps1; *ptr; ++ptr) {
  104. if (*ptr == '\\') {
  105. ++ptr;
  106. if (!*ptr)
  107. break;
  108. switch (*ptr) {
  109. case 'X':
  110. builder.append("\033]0;");
  111. break;
  112. case 'a':
  113. builder.append(0x07);
  114. break;
  115. case 'e':
  116. builder.append(0x1b);
  117. break;
  118. case 'u':
  119. builder.append(username);
  120. break;
  121. case 'h':
  122. builder.append(hostname);
  123. break;
  124. case 'w': {
  125. String home_path = getenv("HOME");
  126. if (cwd.starts_with(home_path)) {
  127. builder.append('~');
  128. builder.append(cwd.substring_view(home_path.length(), cwd.length() - home_path.length()));
  129. } else {
  130. builder.append(cwd);
  131. }
  132. break;
  133. }
  134. case 'p':
  135. builder.append(uid == 0 ? '#' : '$');
  136. break;
  137. }
  138. continue;
  139. }
  140. builder.append(*ptr);
  141. }
  142. return builder.to_string();
  143. };
  144. return build_prompt();
  145. }
  146. String Shell::expand_tilde(const String& expression)
  147. {
  148. ASSERT(expression.starts_with('~'));
  149. StringBuilder login_name;
  150. size_t first_slash_index = expression.length();
  151. for (size_t i = 1; i < expression.length(); ++i) {
  152. if (expression[i] == '/') {
  153. first_slash_index = i;
  154. break;
  155. }
  156. login_name.append(expression[i]);
  157. }
  158. StringBuilder path;
  159. for (size_t i = first_slash_index; i < expression.length(); ++i)
  160. path.append(expression[i]);
  161. if (login_name.is_empty()) {
  162. const char* home = getenv("HOME");
  163. if (!home) {
  164. auto passwd = getpwuid(getuid());
  165. ASSERT(passwd && passwd->pw_dir);
  166. return String::format("%s/%s", passwd->pw_dir, path.to_string().characters());
  167. }
  168. return String::format("%s/%s", home, path.to_string().characters());
  169. }
  170. auto passwd = getpwnam(login_name.to_string().characters());
  171. if (!passwd)
  172. return expression;
  173. ASSERT(passwd->pw_dir);
  174. return String::format("%s/%s", passwd->pw_dir, path.to_string().characters());
  175. }
  176. bool Shell::is_glob(const StringView& s)
  177. {
  178. for (size_t i = 0; i < s.length(); i++) {
  179. char c = s.characters_without_null_termination()[i];
  180. if (c == '*' || c == '?')
  181. return true;
  182. }
  183. return false;
  184. }
  185. Vector<StringView> Shell::split_path(const StringView& path)
  186. {
  187. Vector<StringView> parts;
  188. size_t substart = 0;
  189. for (size_t i = 0; i < path.length(); i++) {
  190. char ch = path[i];
  191. if (ch != '/')
  192. continue;
  193. size_t sublen = i - substart;
  194. if (sublen != 0)
  195. parts.append(path.substring_view(substart, sublen));
  196. substart = i + 1;
  197. }
  198. size_t taillen = path.length() - substart;
  199. if (taillen != 0)
  200. parts.append(path.substring_view(substart, taillen));
  201. return parts;
  202. }
  203. Vector<String> Shell::expand_globs(const StringView& path, StringView base)
  204. {
  205. auto explicitly_set_base = false;
  206. if (path.starts_with('/')) {
  207. base = "/";
  208. explicitly_set_base = true;
  209. }
  210. auto parts = split_path(path);
  211. String base_string = base;
  212. struct stat statbuf;
  213. if (lstat(base_string.characters(), &statbuf) < 0) {
  214. perror("lstat");
  215. return {};
  216. }
  217. StringBuilder resolved_base_path_builder;
  218. resolved_base_path_builder.append(Core::File::real_path_for(base));
  219. if (S_ISDIR(statbuf.st_mode))
  220. resolved_base_path_builder.append('/');
  221. auto resolved_base = resolved_base_path_builder.string_view();
  222. auto results = expand_globs(move(parts), resolved_base);
  223. if (explicitly_set_base && base == "/")
  224. resolved_base = resolved_base.substring_view(1, resolved_base.length() - 1);
  225. for (auto& entry : results) {
  226. entry = entry.substring(resolved_base.length(), entry.length() - resolved_base.length());
  227. if (entry.is_empty())
  228. entry = ".";
  229. }
  230. // Make the output predictable and nice.
  231. quick_sort(results);
  232. return results;
  233. }
  234. Vector<String> Shell::expand_globs(Vector<StringView> path_segments, const StringView& base)
  235. {
  236. if (path_segments.is_empty()) {
  237. String base_str = base;
  238. if (access(base_str.characters(), F_OK) == 0)
  239. return { move(base_str) };
  240. return {};
  241. }
  242. auto first_segment = path_segments.take_first();
  243. if (is_glob(first_segment)) {
  244. Vector<String> result;
  245. Core::DirIterator di(base, Core::DirIterator::SkipParentAndBaseDir);
  246. if (di.has_error())
  247. return {};
  248. while (di.has_next()) {
  249. String path = di.next_path();
  250. // Dotfiles have to be explicitly requested
  251. if (path[0] == '.' && first_segment[0] != '.')
  252. continue;
  253. if (path.matches(first_segment, CaseSensitivity::CaseSensitive)) {
  254. StringBuilder builder;
  255. builder.append(base);
  256. if (!base.ends_with('/'))
  257. builder.append('/');
  258. builder.append(path);
  259. result.append(expand_globs(path_segments, builder.string_view()));
  260. }
  261. }
  262. return result;
  263. } else {
  264. StringBuilder builder;
  265. builder.append(base);
  266. if (!base.ends_with('/'))
  267. builder.append('/');
  268. builder.append(first_segment);
  269. return expand_globs(move(path_segments), builder.string_view());
  270. }
  271. }
  272. Vector<AST::Command> Shell::expand_aliases(Vector<AST::Command> initial_commands)
  273. {
  274. Vector<AST::Command> commands;
  275. Function<void(AST::Command&)> resolve_aliases_and_append = [&](auto& command) {
  276. if (!command.argv.is_empty()) {
  277. auto alias = resolve_alias(command.argv[0]);
  278. if (!alias.is_null()) {
  279. auto argv0 = command.argv.take_first();
  280. auto subcommand_ast = Parser { alias }.parse();
  281. if (subcommand_ast) {
  282. while (subcommand_ast->is_execute()) {
  283. auto* ast = static_cast<AST::Execute*>(subcommand_ast.ptr());
  284. subcommand_ast = ast->command();
  285. }
  286. auto subcommand_nonnull = subcommand_ast.release_nonnull();
  287. NonnullRefPtr<AST::Node> substitute = adopt(*new AST::Join(subcommand_nonnull->position(),
  288. subcommand_nonnull,
  289. adopt(*new AST::CommandLiteral(subcommand_nonnull->position(), command))));
  290. auto res = substitute->run(*this);
  291. for (auto& subst_command : res->resolve_as_commands(*this)) {
  292. if (!subst_command.argv.is_empty() && subst_command.argv.first() == argv0) // Disallow an alias resolving to itself.
  293. commands.append(subst_command);
  294. else
  295. resolve_aliases_and_append(subst_command);
  296. }
  297. } else {
  298. commands.append(command);
  299. }
  300. } else {
  301. commands.append(command);
  302. }
  303. } else {
  304. commands.append(command);
  305. }
  306. };
  307. for (auto& command : initial_commands)
  308. resolve_aliases_and_append(command);
  309. return commands;
  310. }
  311. String Shell::resolve_path(String path) const
  312. {
  313. if (!path.starts_with('/'))
  314. path = String::format("%s/%s", cwd.characters(), path.characters());
  315. return Core::File::real_path_for(path);
  316. }
  317. Shell::LocalFrame* Shell::find_frame_containing_local_variable(const String& name)
  318. {
  319. for (size_t i = m_local_frames.size(); i > 0; --i) {
  320. auto& frame = m_local_frames[i - 1];
  321. if (frame.local_variables.contains(name))
  322. return &frame;
  323. }
  324. return nullptr;
  325. }
  326. RefPtr<AST::Value> Shell::lookup_local_variable(const String& name)
  327. {
  328. if (auto* frame = find_frame_containing_local_variable(name))
  329. return frame->local_variables.get(name).value();
  330. if (auto index = name.to_uint(); index.has_value())
  331. return get_argument(index.value());
  332. return nullptr;
  333. }
  334. RefPtr<AST::Value> Shell::get_argument(size_t index)
  335. {
  336. if (index == 0)
  337. return adopt(*new AST::StringValue(current_script));
  338. --index;
  339. if (auto argv = lookup_local_variable("ARGV")) {
  340. if (argv->is_list_without_resolution()) {
  341. AST::ListValue* list = static_cast<AST::ListValue*>(argv.ptr());
  342. if (list->values().size() <= index)
  343. return nullptr;
  344. return list->values().at(index);
  345. }
  346. if (index != 0)
  347. return nullptr;
  348. return argv;
  349. }
  350. return nullptr;
  351. }
  352. String Shell::local_variable_or(const String& name, const String& replacement)
  353. {
  354. auto value = lookup_local_variable(name);
  355. if (value) {
  356. StringBuilder builder;
  357. builder.join(" ", value->resolve_as_list(*this));
  358. return builder.to_string();
  359. }
  360. return replacement;
  361. }
  362. void Shell::set_local_variable(const String& name, RefPtr<AST::Value> value, bool only_in_current_frame)
  363. {
  364. if (!only_in_current_frame) {
  365. if (auto* frame = find_frame_containing_local_variable(name)) {
  366. frame->local_variables.set(name, move(value));
  367. return;
  368. }
  369. }
  370. m_local_frames.last().local_variables.set(name, move(value));
  371. }
  372. void Shell::unset_local_variable(const String& name, bool only_in_current_frame)
  373. {
  374. if (!only_in_current_frame) {
  375. if (auto* frame = find_frame_containing_local_variable(name))
  376. frame->local_variables.remove(name);
  377. return;
  378. }
  379. m_local_frames.last().local_variables.remove(name);
  380. }
  381. void Shell::define_function(String name, Vector<String> argnames, RefPtr<AST::Node> body)
  382. {
  383. add_entry_to_cache(name);
  384. m_functions.set(name, { name, move(argnames), move(body) });
  385. }
  386. bool Shell::has_function(const String& name)
  387. {
  388. return m_functions.contains(name);
  389. }
  390. bool Shell::invoke_function(const AST::Command& command, int& retval)
  391. {
  392. if (command.argv.is_empty())
  393. return false;
  394. StringView name = command.argv.first();
  395. TemporaryChange<String> script_change { current_script, name };
  396. auto function_option = m_functions.get(name);
  397. if (!function_option.has_value())
  398. return false;
  399. auto& function = function_option.value();
  400. if (!function.body) {
  401. retval = 0;
  402. return true;
  403. }
  404. if (command.argv.size() - 1 < function.arguments.size()) {
  405. fprintf(stderr, "Shell: expected at least %zu arguments to %s, but got %zu\n", function.arguments.size(), function.name.characters(), command.argv.size() - 1);
  406. retval = 1;
  407. return true;
  408. }
  409. auto frame = push_frame(String::formatted("function {}", function.name));
  410. size_t index = 0;
  411. for (auto& arg : function.arguments) {
  412. ++index;
  413. set_local_variable(arg, adopt(*new AST::StringValue(command.argv[index])));
  414. }
  415. auto argv = command.argv;
  416. argv.take_first();
  417. set_local_variable("ARGV", adopt(*new AST::ListValue(move(argv))), true);
  418. Core::EventLoop loop;
  419. setup_signals();
  420. function.body->run(*this);
  421. retval = last_return_code;
  422. return true;
  423. }
  424. String Shell::format(const StringView& source, ssize_t& cursor) const
  425. {
  426. Formatter formatter(source, cursor);
  427. auto result = formatter.format();
  428. cursor = formatter.cursor();
  429. return result;
  430. }
  431. Shell::Frame Shell::push_frame(String name)
  432. {
  433. m_local_frames.append(make<LocalFrame>(name, decltype(LocalFrame::local_variables) {}));
  434. #ifdef SH_DEBUG
  435. dbgln("New frame '{}' at {:p}", name, &m_local_frames.last());
  436. #endif
  437. return { m_local_frames, m_local_frames.last() };
  438. }
  439. void Shell::pop_frame()
  440. {
  441. ASSERT(m_local_frames.size() > 1);
  442. m_local_frames.take_last();
  443. }
  444. Shell::Frame::~Frame()
  445. {
  446. if (!should_destroy_frame)
  447. return;
  448. if (&frames.last() != &frame) {
  449. dbgln("Frame destruction order violation near {:p} (container = {:p}) in '{}'", &frame, this, frame.name);
  450. dbgln("Current frames:");
  451. for (auto& frame : frames)
  452. dbgln("- {:p}: {}", &frame, frame.name);
  453. ASSERT_NOT_REACHED();
  454. }
  455. frames.take_last();
  456. }
  457. String Shell::resolve_alias(const String& name) const
  458. {
  459. return m_aliases.get(name).value_or({});
  460. }
  461. bool Shell::is_runnable(const StringView& name)
  462. {
  463. if (access(name.to_string().characters(), X_OK) == 0)
  464. return true;
  465. return !!binary_search(cached_path.span(), name.to_string(), [](const String& name, const String& program) -> int {
  466. return strcmp(name.characters(), program.characters());
  467. });
  468. }
  469. int Shell::run_command(const StringView& cmd)
  470. {
  471. // The default-constructed mode of the shell
  472. // should not be used for execution!
  473. ASSERT(!m_default_constructed);
  474. if (cmd.is_empty())
  475. return 0;
  476. auto command = Parser(cmd).parse();
  477. if (!command)
  478. return 0;
  479. #ifdef SH_DEBUG
  480. dbgln("Command follows");
  481. command->dump(0);
  482. #endif
  483. if (command->is_syntax_error()) {
  484. auto& error_node = command->syntax_error_node();
  485. auto& position = error_node.position();
  486. fprintf(stderr, "Shell: Syntax error in command: %s\n", error_node.error_text().characters());
  487. fprintf(stderr, "Around '%.*s' at %zu:%zu to %zu:%zu\n",
  488. (int)min(position.end_offset - position.start_offset, (size_t)10),
  489. cmd.characters_without_null_termination() + position.start_offset,
  490. position.start_line.line_column, position.start_line.line_number,
  491. position.end_line.line_column, position.end_line.line_number);
  492. return 1;
  493. }
  494. tcgetattr(0, &termios);
  495. command->run(*this);
  496. return last_return_code;
  497. }
  498. RefPtr<Job> Shell::run_command(const AST::Command& command)
  499. {
  500. FileDescriptionCollector fds;
  501. if (options.verbose)
  502. warnln("+ {}", command);
  503. // If the command is empty, store the redirections and apply them to all later commands.
  504. if (command.argv.is_empty() && !command.should_immediately_execute_next) {
  505. m_global_redirections.append(command.redirections);
  506. return nullptr;
  507. }
  508. // Resolve redirections.
  509. NonnullRefPtrVector<AST::Rewiring> rewirings;
  510. auto resolve_redirection = [&](auto& redirection) -> IterationDecision {
  511. auto rewiring_result = redirection.apply();
  512. if (rewiring_result.is_error()) {
  513. if (!rewiring_result.error().is_empty())
  514. fprintf(stderr, "error: %s\n", rewiring_result.error().characters());
  515. return IterationDecision::Continue;
  516. }
  517. auto& rewiring = rewiring_result.value();
  518. if (rewiring->fd_action != AST::Rewiring::Close::ImmediatelyCloseNew)
  519. rewirings.append(*rewiring);
  520. if (rewiring->fd_action == AST::Rewiring::Close::Old) {
  521. fds.add(rewiring->old_fd);
  522. } else if (rewiring->fd_action == AST::Rewiring::Close::New) {
  523. if (rewiring->new_fd != -1)
  524. fds.add(rewiring->new_fd);
  525. } else if (rewiring->fd_action == AST::Rewiring::Close::ImmediatelyCloseNew) {
  526. fds.add(rewiring->new_fd);
  527. } else if (rewiring->fd_action == AST::Rewiring::Close::RefreshNew) {
  528. ASSERT(rewiring->other_pipe_end);
  529. int pipe_fd[2];
  530. int rc = pipe(pipe_fd);
  531. if (rc < 0) {
  532. perror("pipe(RedirRefresh)");
  533. return IterationDecision::Break;
  534. }
  535. rewiring->new_fd = pipe_fd[1];
  536. rewiring->other_pipe_end->new_fd = pipe_fd[0]; // This fd will be added to the collection on one of the next iterations.
  537. fds.add(pipe_fd[1]);
  538. } else if (rewiring->fd_action == AST::Rewiring::Close::RefreshOld) {
  539. ASSERT(rewiring->other_pipe_end);
  540. int pipe_fd[2];
  541. int rc = pipe(pipe_fd);
  542. if (rc < 0) {
  543. perror("pipe(RedirRefresh)");
  544. return IterationDecision::Break;
  545. }
  546. rewiring->old_fd = pipe_fd[1];
  547. rewiring->other_pipe_end->old_fd = pipe_fd[0]; // This fd will be added to the collection on one of the next iterations.
  548. fds.add(pipe_fd[1]);
  549. }
  550. return IterationDecision::Continue;
  551. };
  552. auto apply_rewirings = [&] {
  553. for (auto& rewiring : rewirings) {
  554. #ifdef SH_DEBUG
  555. dbgln("in {}<{}>, dup2({}, {})", command.argv.is_empty() ? "(<Empty>)" : command.argv[0].characters(), getpid(), rewiring.old_fd, rewiring.new_fd);
  556. #endif
  557. int rc = dup2(rewiring.old_fd, rewiring.new_fd);
  558. if (rc < 0) {
  559. perror("dup2(run)");
  560. return IterationDecision::Break;
  561. }
  562. // {new,old}_fd is closed via the `fds` collector, but rewiring.other_pipe_end->{new,old}_fd
  563. // isn't yet in that collector when the first child spawns.
  564. if (rewiring.other_pipe_end) {
  565. if (rewiring.fd_action == AST::Rewiring::Close::RefreshNew) {
  566. if (rewiring.other_pipe_end && close(rewiring.other_pipe_end->new_fd) < 0)
  567. perror("close other pipe end");
  568. } else if (rewiring.fd_action == AST::Rewiring::Close::RefreshOld) {
  569. if (rewiring.other_pipe_end && close(rewiring.other_pipe_end->old_fd) < 0)
  570. perror("close other pipe end");
  571. }
  572. }
  573. }
  574. return IterationDecision::Continue;
  575. };
  576. TemporaryChange signal_handler_install { m_should_reinstall_signal_handlers, false };
  577. for (auto& redirection : m_global_redirections) {
  578. if (resolve_redirection(redirection) == IterationDecision::Break)
  579. return nullptr;
  580. }
  581. for (auto& redirection : command.redirections) {
  582. if (resolve_redirection(redirection) == IterationDecision::Break)
  583. return nullptr;
  584. }
  585. if (command.should_wait && run_builtin(command, rewirings, last_return_code)) {
  586. for (auto& next_in_chain : command.next_chain)
  587. run_tail(command, next_in_chain, last_return_code);
  588. return nullptr;
  589. }
  590. auto can_be_run_in_current_process = command.should_wait && !command.pipeline;
  591. if (can_be_run_in_current_process && has_function(command.argv.first())) {
  592. SavedFileDescriptors fds { rewirings };
  593. for (auto& rewiring : rewirings) {
  594. int rc = dup2(rewiring.old_fd, rewiring.new_fd);
  595. if (rc < 0) {
  596. perror("dup2(run)");
  597. return nullptr;
  598. }
  599. }
  600. if (invoke_function(command, last_return_code)) {
  601. for (auto& next_in_chain : command.next_chain)
  602. run_tail(command, next_in_chain, last_return_code);
  603. return nullptr;
  604. }
  605. }
  606. Vector<const char*> argv;
  607. Vector<String> copy_argv = command.argv;
  608. argv.ensure_capacity(command.argv.size() + 1);
  609. for (auto& arg : copy_argv)
  610. argv.append(arg.characters());
  611. argv.append(nullptr);
  612. int sync_pipe[2];
  613. if (pipe(sync_pipe) < 0) {
  614. perror("pipe");
  615. return nullptr;
  616. }
  617. pid_t child = fork();
  618. if (child < 0) {
  619. perror("fork");
  620. return nullptr;
  621. }
  622. if (child == 0) {
  623. close(sync_pipe[1]);
  624. m_is_subshell = true;
  625. m_pid = getpid();
  626. Core::EventLoop::notify_forked(Core::EventLoop::ForkEvent::Child);
  627. TemporaryChange signal_handler_install { m_should_reinstall_signal_handlers, true };
  628. if (apply_rewirings() == IterationDecision::Break)
  629. _exit(126);
  630. fds.collect();
  631. u8 c;
  632. while (read(sync_pipe[0], &c, 1) < 0) {
  633. if (errno != EINTR) {
  634. perror("read");
  635. // There's nothing interesting we can do here.
  636. break;
  637. }
  638. }
  639. #ifdef SH_DEBUG
  640. dbgln("Synced up with parent, we're good to exec()");
  641. #endif
  642. close(sync_pipe[0]);
  643. if (!m_is_subshell && command.should_wait)
  644. tcsetattr(0, TCSANOW, &default_termios);
  645. if (command.should_immediately_execute_next) {
  646. ASSERT(command.argv.is_empty());
  647. Core::EventLoop mainloop;
  648. setup_signals();
  649. for (auto& next_in_chain : command.next_chain)
  650. run_tail(command, next_in_chain, 0);
  651. _exit(last_return_code);
  652. }
  653. if (run_builtin(command, {}, last_return_code))
  654. _exit(last_return_code);
  655. if (invoke_function(command, last_return_code))
  656. _exit(last_return_code);
  657. // We no longer need the jobs here.
  658. jobs.clear();
  659. int rc = execvp(argv[0], const_cast<char* const*>(argv.data()));
  660. if (rc < 0) {
  661. if (errno == ENOENT) {
  662. int shebang_fd = open(argv[0], O_RDONLY);
  663. auto close_argv = ScopeGuard([shebang_fd]() { if (shebang_fd >= 0) close(shebang_fd); });
  664. char shebang[256] {};
  665. ssize_t num_read = -1;
  666. if ((shebang_fd >= 0) && ((num_read = read(shebang_fd, shebang, sizeof(shebang))) >= 2) && (StringView(shebang).starts_with("#!"))) {
  667. StringView shebang_path_view(&shebang[2], num_read - 2);
  668. Optional<size_t> newline_pos = shebang_path_view.find_first_of("\n\r");
  669. shebang[newline_pos.has_value() ? (newline_pos.value() + 2) : num_read] = '\0';
  670. fprintf(stderr, "%s: Invalid interpreter \"%s\": %s\n", argv[0], &shebang[2], strerror(ENOENT));
  671. } else
  672. fprintf(stderr, "%s: Command not found.\n", argv[0]);
  673. } else {
  674. int saved_errno = errno;
  675. struct stat st;
  676. if (stat(argv[0], &st) == 0 && S_ISDIR(st.st_mode)) {
  677. fprintf(stderr, "Shell: %s: Is a directory\n", argv[0]);
  678. _exit(126);
  679. }
  680. fprintf(stderr, "execvp(%s): %s\n", argv[0], strerror(saved_errno));
  681. }
  682. _exit(126);
  683. }
  684. ASSERT_NOT_REACHED();
  685. }
  686. close(sync_pipe[0]);
  687. bool is_first = !command.pipeline || (command.pipeline && command.pipeline->pgid == -1);
  688. if (command.pipeline) {
  689. if (is_first) {
  690. command.pipeline->pgid = child;
  691. }
  692. }
  693. pid_t pgid = is_first ? child : (command.pipeline ? command.pipeline->pgid : child);
  694. if (!m_is_subshell || command.pipeline) {
  695. if (setpgid(child, pgid) < 0 && m_is_interactive)
  696. perror("setpgid");
  697. if (!m_is_subshell) {
  698. if (tcsetpgrp(STDOUT_FILENO, pgid) != 0 && m_is_interactive)
  699. perror("tcsetpgrp(OUT)");
  700. if (tcsetpgrp(STDIN_FILENO, pgid) != 0 && m_is_interactive)
  701. perror("tcsetpgrp(IN)");
  702. }
  703. }
  704. while (write(sync_pipe[1], "x", 1) < 0) {
  705. if (errno != EINTR) {
  706. perror("write");
  707. // There's nothing interesting we can do here.
  708. break;
  709. }
  710. }
  711. close(sync_pipe[1]);
  712. StringBuilder cmd;
  713. cmd.join(" ", command.argv);
  714. auto command_copy = AST::Command(command);
  715. // Clear the next chain if it's to be immediately executed
  716. // as the child will run this chain.
  717. if (command.should_immediately_execute_next)
  718. command_copy.next_chain.clear();
  719. auto job = Job::create(child, pgid, cmd.build(), find_last_job_id() + 1, move(command_copy));
  720. jobs.set((u64)child, job);
  721. job->on_exit = [this](auto job) {
  722. if (!job->exited())
  723. return;
  724. if (job->is_running_in_background() && job->should_announce_exit())
  725. warnln("Shell: Job {} ({}) exited\n", job->job_id(), job->cmd().characters());
  726. else if (job->signaled() && job->should_announce_signal())
  727. warnln("Shell: Job {} ({}) {}\n", job->job_id(), job->cmd().characters(), strsignal(job->termination_signal()));
  728. last_return_code = job->exit_code();
  729. job->disown();
  730. run_tail(job);
  731. };
  732. fds.collect();
  733. return *job;
  734. }
  735. void Shell::run_tail(const AST::Command& invoking_command, const AST::NodeWithAction& next_in_chain, int head_exit_code)
  736. {
  737. auto evaluate = [&] {
  738. if (next_in_chain.node->would_execute()) {
  739. next_in_chain.node->run(*this);
  740. return;
  741. }
  742. auto node = next_in_chain.node;
  743. if (!invoking_command.should_wait)
  744. node = adopt(static_cast<AST::Node&>(*new AST::Background(next_in_chain.node->position(), move(node))));
  745. adopt(static_cast<AST::Node&>(*new AST::Execute(next_in_chain.node->position(), move(node))))->run(*this);
  746. };
  747. switch (next_in_chain.action) {
  748. case AST::NodeWithAction::And:
  749. if (head_exit_code == 0)
  750. evaluate();
  751. break;
  752. case AST::NodeWithAction::Or:
  753. if (head_exit_code != 0)
  754. evaluate();
  755. break;
  756. case AST::NodeWithAction::Sequence:
  757. evaluate();
  758. break;
  759. }
  760. }
  761. void Shell::run_tail(RefPtr<Job> job)
  762. {
  763. if (auto cmd = job->command_ptr()) {
  764. deferred_invoke([=, this](auto&) {
  765. for (auto& next_in_chain : cmd->next_chain) {
  766. run_tail(*cmd, next_in_chain, job->exit_code());
  767. }
  768. });
  769. }
  770. }
  771. NonnullRefPtrVector<Job> Shell::run_commands(Vector<AST::Command>& commands)
  772. {
  773. NonnullRefPtrVector<Job> spawned_jobs;
  774. for (auto& command : commands) {
  775. #ifdef SH_DEBUG
  776. dbgln("Command");
  777. for (auto& arg : command.argv)
  778. dbgln("argv: {}", arg);
  779. for (auto& redir : command.redirections) {
  780. if (redir.is_path_redirection()) {
  781. auto path_redir = (const AST::PathRedirection*)&redir;
  782. dbgln("redir path '{}' <-({})-> {}", path_redir->path, (int)path_redir->direction, path_redir->fd);
  783. } else if (redir.is_fd_redirection()) {
  784. auto* fdredir = (const AST::FdRedirection*)&redir;
  785. dbgln("redir fd {} -> {}", fdredir->old_fd, fdredir->new_fd);
  786. } else if (redir.is_close_redirection()) {
  787. auto close_redir = (const AST::CloseRedirection*)&redir;
  788. dbgln("close fd {}", close_redir->fd);
  789. } else {
  790. ASSERT_NOT_REACHED();
  791. }
  792. }
  793. #endif
  794. auto job = run_command(command);
  795. if (!job)
  796. continue;
  797. spawned_jobs.append(*job);
  798. if (command.should_wait) {
  799. block_on_job(job);
  800. } else {
  801. job->set_running_in_background(true);
  802. if (!command.is_pipe_source && command.should_notify_if_in_background)
  803. job->set_should_announce_exit(true);
  804. }
  805. }
  806. return spawned_jobs;
  807. }
  808. bool Shell::run_file(const String& filename, bool explicitly_invoked)
  809. {
  810. TemporaryChange script_change { current_script, filename };
  811. TemporaryChange interactive_change { m_is_interactive, false };
  812. auto file_result = Core::File::open(filename, Core::File::ReadOnly);
  813. if (file_result.is_error()) {
  814. if (explicitly_invoked)
  815. fprintf(stderr, "Failed to open %s: %s\n", filename.characters(), file_result.error().characters());
  816. else
  817. dbgln("open() failed for '{}' with {}", filename, file_result.error());
  818. return false;
  819. }
  820. auto file = file_result.value();
  821. auto data = file->read_all();
  822. run_command(data);
  823. return true;
  824. }
  825. void Shell::restore_ios()
  826. {
  827. if (m_is_subshell)
  828. return;
  829. tcsetattr(0, TCSANOW, &termios);
  830. tcsetpgrp(STDOUT_FILENO, m_pid);
  831. tcsetpgrp(STDIN_FILENO, m_pid);
  832. }
  833. void Shell::block_on_pipeline(RefPtr<AST::Pipeline> pipeline)
  834. {
  835. if (!pipeline)
  836. return;
  837. for (auto& it : jobs) {
  838. if (auto cmd = it.value->command_ptr(); cmd->pipeline == pipeline && cmd->is_pipe_source)
  839. block_on_job(it.value);
  840. }
  841. }
  842. void Shell::block_on_job(RefPtr<Job> job)
  843. {
  844. TemporaryChange<const Job*> current_job { m_current_job, job.ptr() };
  845. if (!job)
  846. return;
  847. if (job->is_suspended())
  848. return; // We cannot wait for a suspended job.
  849. ScopeGuard io_restorer { [&]() {
  850. if (job->exited() && !job->is_running_in_background()) {
  851. restore_ios();
  852. }
  853. } };
  854. Core::EventLoop loop;
  855. job->on_exit = [&, old_exit = move(job->on_exit)](auto job) {
  856. if (old_exit)
  857. old_exit(job);
  858. loop.quit(0);
  859. };
  860. if (job->exited())
  861. return;
  862. loop.exec();
  863. // If the job is part of a pipeline, wait for the rest of the members too.
  864. if (auto command = job->command_ptr())
  865. block_on_pipeline(command->pipeline);
  866. }
  867. String Shell::get_history_path()
  868. {
  869. if (auto histfile = getenv("HISTFILE"))
  870. return { histfile };
  871. return String::formatted("{}/.history", home);
  872. }
  873. String Shell::escape_token(const String& token)
  874. {
  875. StringBuilder builder;
  876. for (auto c : token) {
  877. switch (c) {
  878. case '\'':
  879. case '"':
  880. case '$':
  881. case '|':
  882. case '>':
  883. case '<':
  884. case '&':
  885. case '\\':
  886. case ' ':
  887. builder.append('\\');
  888. break;
  889. default:
  890. break;
  891. }
  892. builder.append(c);
  893. }
  894. return builder.build();
  895. }
  896. String Shell::unescape_token(const String& token)
  897. {
  898. StringBuilder builder;
  899. enum {
  900. Free,
  901. Escaped
  902. } state { Free };
  903. for (auto c : token) {
  904. switch (state) {
  905. case Escaped:
  906. builder.append(c);
  907. state = Free;
  908. break;
  909. case Free:
  910. if (c == '\\')
  911. state = Escaped;
  912. else
  913. builder.append(c);
  914. break;
  915. }
  916. }
  917. if (state == Escaped)
  918. builder.append('\\');
  919. return builder.build();
  920. }
  921. void Shell::cache_path()
  922. {
  923. if (!cached_path.is_empty())
  924. cached_path.clear_with_capacity();
  925. // Add shell builtins to the cache.
  926. for (const auto& builtin_name : builtin_names)
  927. cached_path.append(escape_token(builtin_name));
  928. // Add aliases to the cache.
  929. for (const auto& alias : m_aliases) {
  930. auto name = escape_token(alias.key);
  931. if (cached_path.contains_slow(name))
  932. continue;
  933. cached_path.append(name);
  934. }
  935. String path = getenv("PATH");
  936. if (!path.is_empty()) {
  937. auto directories = path.split(':');
  938. for (const auto& directory : directories) {
  939. Core::DirIterator programs(directory.characters(), Core::DirIterator::SkipDots);
  940. while (programs.has_next()) {
  941. auto program = programs.next_path();
  942. String program_path = String::format("%s/%s", directory.characters(), program.characters());
  943. auto escaped_name = escape_token(program);
  944. if (cached_path.contains_slow(escaped_name))
  945. continue;
  946. if (access(program_path.characters(), X_OK) == 0)
  947. cached_path.append(escaped_name);
  948. }
  949. }
  950. }
  951. quick_sort(cached_path);
  952. }
  953. void Shell::add_entry_to_cache(const String& entry)
  954. {
  955. size_t index = 0;
  956. auto match = binary_search(
  957. cached_path.span(), entry, [](const String& name, const String& program) -> int {
  958. return strcmp(name.characters(), program.characters());
  959. },
  960. &index);
  961. if (match)
  962. return;
  963. while (index < cached_path.size() && strcmp(cached_path[index].characters(), entry.characters()) < 0) {
  964. index++;
  965. }
  966. cached_path.insert(index, entry);
  967. }
  968. void Shell::highlight(Line::Editor& editor) const
  969. {
  970. auto line = editor.line();
  971. Parser parser(line);
  972. auto ast = parser.parse();
  973. if (!ast)
  974. return;
  975. ast->highlight_in_editor(editor, const_cast<Shell&>(*this));
  976. }
  977. Vector<Line::CompletionSuggestion> Shell::complete()
  978. {
  979. auto line = m_editor->line(m_editor->cursor());
  980. Parser parser(line);
  981. auto ast = parser.parse();
  982. if (!ast)
  983. return {};
  984. return ast->complete_for_editor(*this, line.length());
  985. }
  986. Vector<Line::CompletionSuggestion> Shell::complete_path(const String& base, const String& part, size_t offset)
  987. {
  988. auto token = offset ? part.substring_view(0, offset) : "";
  989. StringView original_token = token;
  990. String path;
  991. ssize_t last_slash = token.length() - 1;
  992. while (last_slash >= 0 && token[last_slash] != '/')
  993. --last_slash;
  994. StringBuilder path_builder;
  995. auto init_slash_part = token.substring_view(0, last_slash + 1);
  996. auto last_slash_part = token.substring_view(last_slash + 1, token.length() - last_slash - 1);
  997. // Depending on the base, we will have to prepend cwd.
  998. if (base.is_empty()) {
  999. // '' /foo -> absolute
  1000. // '' foo -> relative
  1001. if (!token.starts_with('/'))
  1002. path_builder.append(cwd);
  1003. path_builder.append('/');
  1004. path_builder.append(init_slash_part);
  1005. } else {
  1006. // /foo * -> absolute
  1007. // foo * -> relative
  1008. if (!base.starts_with('/'))
  1009. path_builder.append(cwd);
  1010. path_builder.append('/');
  1011. path_builder.append(base);
  1012. path_builder.append('/');
  1013. path_builder.append(init_slash_part);
  1014. }
  1015. path = path_builder.build();
  1016. token = last_slash_part;
  1017. // the invariant part of the token is actually just the last segment
  1018. // e. in `cd /foo/bar', 'bar' is the invariant
  1019. // since we are not suggesting anything starting with
  1020. // `/foo/', but rather just `bar...'
  1021. auto token_length = escape_token(token).length();
  1022. if (m_editor)
  1023. m_editor->suggest(token_length, original_token.length() - token_length);
  1024. // only suggest dot-files if path starts with a dot
  1025. Core::DirIterator files(path,
  1026. token.starts_with('.') ? Core::DirIterator::SkipParentAndBaseDir : Core::DirIterator::SkipDots);
  1027. Vector<Line::CompletionSuggestion> suggestions;
  1028. while (files.has_next()) {
  1029. auto file = files.next_path();
  1030. if (file.starts_with(token)) {
  1031. struct stat program_status;
  1032. String file_path = String::format("%s/%s", path.characters(), file.characters());
  1033. int stat_error = stat(file_path.characters(), &program_status);
  1034. if (!stat_error) {
  1035. if (S_ISDIR(program_status.st_mode)) {
  1036. suggestions.append({ escape_token(file), "/" });
  1037. } else {
  1038. suggestions.append({ escape_token(file), " " });
  1039. }
  1040. suggestions.last().input_offset = token_length;
  1041. }
  1042. }
  1043. }
  1044. return suggestions;
  1045. }
  1046. Vector<Line::CompletionSuggestion> Shell::complete_program_name(const String& name, size_t offset)
  1047. {
  1048. auto match = binary_search(cached_path.span(), name, [](const String& name, const String& program) -> int {
  1049. return strncmp(name.characters(), program.characters(), name.length());
  1050. });
  1051. if (!match)
  1052. return complete_path("", name, offset);
  1053. String completion = *match;
  1054. auto token_length = escape_token(name).length();
  1055. if (m_editor)
  1056. m_editor->suggest(token_length, 0);
  1057. // Now that we have a program name starting with our token, we look at
  1058. // other program names starting with our token and cut off any mismatching
  1059. // characters.
  1060. Vector<Line::CompletionSuggestion> suggestions;
  1061. int index = match - cached_path.data();
  1062. for (int i = index - 1; i >= 0 && cached_path[i].starts_with(name); --i) {
  1063. suggestions.append({ cached_path[i], " " });
  1064. suggestions.last().input_offset = token_length;
  1065. }
  1066. for (size_t i = index + 1; i < cached_path.size() && cached_path[i].starts_with(name); ++i) {
  1067. suggestions.append({ cached_path[i], " " });
  1068. suggestions.last().input_offset = token_length;
  1069. }
  1070. suggestions.append({ cached_path[index], " " });
  1071. suggestions.last().input_offset = token_length;
  1072. return suggestions;
  1073. }
  1074. Vector<Line::CompletionSuggestion> Shell::complete_variable(const String& name, size_t offset)
  1075. {
  1076. Vector<Line::CompletionSuggestion> suggestions;
  1077. auto pattern = offset ? name.substring_view(0, offset) : "";
  1078. if (m_editor)
  1079. m_editor->suggest(offset);
  1080. // Look at local variables.
  1081. for (auto& frame : m_local_frames) {
  1082. for (auto& variable : frame.local_variables) {
  1083. if (variable.key.starts_with(pattern) && !suggestions.contains_slow(variable.key))
  1084. suggestions.append(variable.key);
  1085. }
  1086. }
  1087. // Look at the environment.
  1088. for (auto i = 0; environ[i]; ++i) {
  1089. auto entry = StringView { environ[i] };
  1090. if (entry.starts_with(pattern)) {
  1091. auto parts = entry.split_view('=');
  1092. if (parts.is_empty() || parts.first().is_empty())
  1093. continue;
  1094. String name = parts.first();
  1095. if (suggestions.contains_slow(name))
  1096. continue;
  1097. suggestions.append(move(name));
  1098. suggestions.last().input_offset = offset;
  1099. }
  1100. }
  1101. return suggestions;
  1102. }
  1103. Vector<Line::CompletionSuggestion> Shell::complete_user(const String& name, size_t offset)
  1104. {
  1105. Vector<Line::CompletionSuggestion> suggestions;
  1106. auto pattern = offset ? name.substring_view(0, offset) : "";
  1107. if (m_editor)
  1108. m_editor->suggest(offset);
  1109. Core::DirIterator di("/home", Core::DirIterator::SkipParentAndBaseDir);
  1110. if (di.has_error())
  1111. return suggestions;
  1112. while (di.has_next()) {
  1113. String name = di.next_path();
  1114. if (name.starts_with(pattern)) {
  1115. suggestions.append(name);
  1116. suggestions.last().input_offset = offset;
  1117. }
  1118. }
  1119. return suggestions;
  1120. }
  1121. Vector<Line::CompletionSuggestion> Shell::complete_option(const String& program_name, const String& option, size_t offset)
  1122. {
  1123. size_t start = 0;
  1124. while (start < option.length() && option[start] == '-' && start < 2)
  1125. ++start;
  1126. auto option_pattern = offset > start ? option.substring_view(start, offset - start) : "";
  1127. if (m_editor)
  1128. m_editor->suggest(offset);
  1129. Vector<Line::CompletionSuggestion> suggestions;
  1130. dbgln("Shell::complete_option({}, {})", program_name, option_pattern);
  1131. // FIXME: Figure out how to do this stuff.
  1132. if (has_builtin(program_name)) {
  1133. // Complete builtins.
  1134. if (program_name == "setopt") {
  1135. bool negate = false;
  1136. if (option_pattern.starts_with("no_")) {
  1137. negate = true;
  1138. option_pattern = option_pattern.substring_view(3, option_pattern.length() - 3);
  1139. }
  1140. auto maybe_negate = [&](const StringView& view) {
  1141. static StringBuilder builder;
  1142. builder.clear();
  1143. builder.append("--");
  1144. if (negate)
  1145. builder.append("no_");
  1146. builder.append(view);
  1147. return builder.to_string();
  1148. };
  1149. #define __ENUMERATE_SHELL_OPTION(name, d_, descr_) \
  1150. if (StringView { #name }.starts_with(option_pattern)) { \
  1151. suggestions.append(maybe_negate(#name)); \
  1152. suggestions.last().input_offset = offset; \
  1153. }
  1154. ENUMERATE_SHELL_OPTIONS();
  1155. #undef __ENUMERATE_SHELL_OPTION
  1156. return suggestions;
  1157. }
  1158. }
  1159. return suggestions;
  1160. }
  1161. void Shell::bring_cursor_to_beginning_of_a_line() const
  1162. {
  1163. struct winsize ws;
  1164. if (m_editor) {
  1165. ws = m_editor->terminal_size();
  1166. } else {
  1167. if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) < 0) {
  1168. // Very annoying assumptions.
  1169. ws.ws_col = 80;
  1170. ws.ws_row = 25;
  1171. }
  1172. }
  1173. // Black with Cyan background.
  1174. constexpr auto default_mark = "\e[30;46m%\e[0m";
  1175. String eol_mark = getenv("PROMPT_EOL_MARK");
  1176. if (eol_mark.is_null())
  1177. eol_mark = default_mark;
  1178. size_t eol_mark_length = Line::Editor::actual_rendered_string_metrics(eol_mark).line_lengths.last();
  1179. if (eol_mark_length >= ws.ws_col) {
  1180. eol_mark = default_mark;
  1181. eol_mark_length = 1;
  1182. }
  1183. fputs(eol_mark.characters(), stderr);
  1184. for (auto i = eol_mark_length; i < ws.ws_col; ++i)
  1185. putc(' ', stderr);
  1186. putc('\r', stderr);
  1187. }
  1188. bool Shell::read_single_line()
  1189. {
  1190. restore_ios();
  1191. bring_cursor_to_beginning_of_a_line();
  1192. auto line_result = m_editor->get_line(prompt());
  1193. if (line_result.is_error()) {
  1194. if (line_result.error() == Line::Editor::Error::Eof || line_result.error() == Line::Editor::Error::Empty) {
  1195. // Pretend the user tried to execute builtin_exit()
  1196. run_command("exit");
  1197. return read_single_line();
  1198. } else {
  1199. Core::EventLoop::current().quit(1);
  1200. return false;
  1201. }
  1202. }
  1203. auto& line = line_result.value();
  1204. if (line.is_empty())
  1205. return true;
  1206. run_command(line);
  1207. m_editor->add_to_history(line);
  1208. return true;
  1209. }
  1210. void Shell::custom_event(Core::CustomEvent& event)
  1211. {
  1212. if (event.custom_type() == ReadLine) {
  1213. if (read_single_line())
  1214. Core::EventLoop::current().post_event(*this, make<Core::CustomEvent>(ShellEventType::ReadLine));
  1215. return;
  1216. }
  1217. }
  1218. void Shell::notify_child_event()
  1219. {
  1220. #ifdef ENSURE_WAITID_ONCE
  1221. static HashTable<pid_t> s_waited_for_pids;
  1222. #endif
  1223. Vector<u64> disowned_jobs;
  1224. // Workaround the fact that we can't receive *who* exactly changed state.
  1225. // The child might still be alive (and even running) when this signal is dispatched to us
  1226. // so just...repeat until we find a suitable child.
  1227. // This, of course, will mean that someone can send us a SIGCHILD and we'd be spinning here
  1228. // until the next child event we can actually handle.
  1229. bool found_child = false;
  1230. do {
  1231. // Ignore stray SIGCHLD when there are no jobs.
  1232. if (jobs.is_empty())
  1233. return;
  1234. for (auto& it : jobs) {
  1235. auto job_id = it.key;
  1236. auto& job = *it.value;
  1237. #ifdef ENSURE_WAITID_ONCE
  1238. // Theoretically, this should never trip, as jobs are removed from
  1239. // the job table when waitpid() succeeds *and* the child is dead.
  1240. ASSERT(!s_waited_for_pids.contains(job.pid()));
  1241. #endif
  1242. int wstatus = 0;
  1243. #ifdef SH_DEBUG
  1244. dbgln("waitpid({}) = ...", job.pid());
  1245. #endif
  1246. auto child_pid = waitpid(job.pid(), &wstatus, WNOHANG | WUNTRACED);
  1247. #ifdef SH_DEBUG
  1248. dbgln("... = {} - {}", child_pid, wstatus);
  1249. #endif
  1250. if (child_pid < 0) {
  1251. if (errno == ECHILD) {
  1252. // The child process went away before we could process its death, just assume it exited all ok.
  1253. // FIXME: This should never happen, the child should stay around until we do the waitpid above.
  1254. dbgln("Child process gone, cannot get exit code for {}", job_id);
  1255. child_pid = job.pid();
  1256. } else {
  1257. ASSERT_NOT_REACHED();
  1258. }
  1259. }
  1260. if (child_pid == 0) {
  1261. // If the child existed, but wasn't dead.
  1262. continue;
  1263. }
  1264. if (child_pid == job.pid()) {
  1265. if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) {
  1266. job.set_signalled(WTERMSIG(wstatus));
  1267. } else if (WIFEXITED(wstatus)) {
  1268. job.set_has_exit(WEXITSTATUS(wstatus));
  1269. } else if (WIFSTOPPED(wstatus)) {
  1270. job.unblock();
  1271. job.set_is_suspended(true);
  1272. }
  1273. found_child = true;
  1274. #ifdef ENSURE_WAITID_ONCE
  1275. // NOTE: This check is here to find bugs about our assumptions about waitpid(),
  1276. // it does not hold in general, and it definitely does not hold in the long run.
  1277. // Reasons that we would call waitpid() more than once:
  1278. // - PID reuse/wraparound: This will simply fail the assertion, ignored here.
  1279. // - Non-terminating unblocks:
  1280. // - Suspension: (e.g. via ^Z)
  1281. // - ?
  1282. // - ?
  1283. if (job.exited())
  1284. s_waited_for_pids.set(child_pid);
  1285. #endif
  1286. }
  1287. if (job.should_be_disowned())
  1288. disowned_jobs.append(job_id);
  1289. }
  1290. for (auto job_id : disowned_jobs) {
  1291. jobs.remove(job_id);
  1292. }
  1293. } while (!found_child);
  1294. }
  1295. Shell::Shell()
  1296. : m_default_constructed(true)
  1297. {
  1298. push_frame("main").leak_frame();
  1299. int rc = gethostname(hostname, Shell::HostNameSize);
  1300. if (rc < 0)
  1301. perror("gethostname");
  1302. {
  1303. auto* pw = getpwuid(getuid());
  1304. if (pw) {
  1305. username = pw->pw_name;
  1306. home = pw->pw_dir;
  1307. setenv("HOME", pw->pw_dir, 1);
  1308. }
  1309. endpwent();
  1310. }
  1311. // For simplicity, start at the user's home directory.
  1312. this->cwd = home;
  1313. setenv("PWD", home.characters(), 1);
  1314. // Add the default PATH vars.
  1315. {
  1316. StringBuilder path;
  1317. path.append(getenv("PATH"));
  1318. if (path.length())
  1319. path.append(":");
  1320. path.append("/bin:/usr/bin:/usr/local/bin");
  1321. setenv("PATH", path.to_string().characters(), true);
  1322. }
  1323. cache_path();
  1324. }
  1325. Shell::Shell(Line::Editor& editor)
  1326. : m_editor(editor)
  1327. {
  1328. uid = getuid();
  1329. tcsetpgrp(0, getpgrp());
  1330. m_pid = getpid();
  1331. push_frame("main").leak_frame();
  1332. int rc = gethostname(hostname, Shell::HostNameSize);
  1333. if (rc < 0)
  1334. perror("gethostname");
  1335. auto istty = isatty(STDIN_FILENO);
  1336. m_is_interactive = istty;
  1337. if (istty) {
  1338. rc = ttyname_r(0, ttyname, Shell::TTYNameSize);
  1339. if (rc < 0)
  1340. perror("ttyname_r");
  1341. } else {
  1342. ttyname[0] = 0;
  1343. }
  1344. {
  1345. auto* cwd = getcwd(nullptr, 0);
  1346. this->cwd = cwd;
  1347. setenv("PWD", cwd, 1);
  1348. free(cwd);
  1349. }
  1350. {
  1351. auto* pw = getpwuid(getuid());
  1352. if (pw) {
  1353. username = pw->pw_name;
  1354. home = pw->pw_dir;
  1355. setenv("HOME", pw->pw_dir, 1);
  1356. }
  1357. endpwent();
  1358. }
  1359. directory_stack.append(cwd);
  1360. m_editor->load_history(get_history_path());
  1361. cache_path();
  1362. m_editor->register_key_input_callback('\n', [](Line::Editor& editor) {
  1363. auto ast = Parser(editor.line()).parse();
  1364. if (ast && ast->is_syntax_error() && ast->syntax_error_node().is_continuable())
  1365. return true;
  1366. return EDITOR_INTERNAL_FUNCTION(finish)(editor);
  1367. });
  1368. }
  1369. Shell::~Shell()
  1370. {
  1371. if (m_default_constructed)
  1372. return;
  1373. stop_all_jobs();
  1374. m_editor->save_history(get_history_path());
  1375. }
  1376. void Shell::stop_all_jobs()
  1377. {
  1378. if (!jobs.is_empty()) {
  1379. if (m_is_interactive && !m_is_subshell)
  1380. printf("Killing active jobs\n");
  1381. for (auto& entry : jobs) {
  1382. if (entry.value->is_suspended()) {
  1383. #ifdef SH_DEBUG
  1384. dbgln("Job {} is suspended", entry.value->pid());
  1385. #endif
  1386. kill_job(entry.value, SIGCONT);
  1387. }
  1388. kill_job(entry.value, SIGHUP);
  1389. }
  1390. usleep(10000); // Wait for a bit before killing the job
  1391. for (auto& entry : jobs) {
  1392. #ifdef SH_DEBUG
  1393. dbgln("Actively killing {} ({})", entry.value->pid(), entry.value->cmd());
  1394. #endif
  1395. kill_job(entry.value, SIGKILL);
  1396. }
  1397. jobs.clear();
  1398. }
  1399. }
  1400. u64 Shell::find_last_job_id() const
  1401. {
  1402. u64 job_id = 0;
  1403. for (auto& entry : jobs) {
  1404. if (entry.value->job_id() > job_id)
  1405. job_id = entry.value->job_id();
  1406. }
  1407. return job_id;
  1408. }
  1409. const Job* Shell::find_job(u64 id)
  1410. {
  1411. for (auto& entry : jobs) {
  1412. if (entry.value->job_id() == id)
  1413. return entry.value;
  1414. }
  1415. return nullptr;
  1416. }
  1417. void Shell::kill_job(const Job* job, int sig)
  1418. {
  1419. if (!job)
  1420. return;
  1421. if (killpg(job->pgid(), sig) < 0) {
  1422. if (kill(job->pid(), sig) < 0) {
  1423. perror("kill");
  1424. }
  1425. }
  1426. }
  1427. void Shell::save_to(JsonObject& object)
  1428. {
  1429. Core::Object::save_to(object);
  1430. object.set("working_directory", cwd);
  1431. object.set("username", username);
  1432. object.set("user_home_path", home);
  1433. object.set("user_id", uid);
  1434. object.set("directory_stack_size", directory_stack.size());
  1435. object.set("cd_history_size", cd_history.size());
  1436. // Jobs.
  1437. JsonArray job_objects;
  1438. for (auto& job_entry : jobs) {
  1439. JsonObject job_object;
  1440. job_object.set("pid", job_entry.value->pid());
  1441. job_object.set("pgid", job_entry.value->pgid());
  1442. job_object.set("running_time", job_entry.value->timer().elapsed());
  1443. job_object.set("command", job_entry.value->cmd());
  1444. job_object.set("is_running_in_background", job_entry.value->is_running_in_background());
  1445. job_objects.append(move(job_object));
  1446. }
  1447. object.set("jobs", move(job_objects));
  1448. }
  1449. void FileDescriptionCollector::collect()
  1450. {
  1451. for (auto fd : m_fds)
  1452. close(fd);
  1453. m_fds.clear();
  1454. }
  1455. FileDescriptionCollector::~FileDescriptionCollector()
  1456. {
  1457. collect();
  1458. }
  1459. void FileDescriptionCollector::add(int fd)
  1460. {
  1461. m_fds.append(fd);
  1462. }
  1463. SavedFileDescriptors::SavedFileDescriptors(const NonnullRefPtrVector<AST::Rewiring>& intended_rewirings)
  1464. {
  1465. for (auto& rewiring : intended_rewirings) {
  1466. int new_fd = dup(rewiring.new_fd);
  1467. if (new_fd < 0) {
  1468. if (errno != EBADF)
  1469. perror("dup");
  1470. // The fd that will be overwritten isn't open right now,
  1471. // it will be cleaned up by the exec()-side collector
  1472. // and we have nothing to do here, so just ignore this error.
  1473. continue;
  1474. }
  1475. auto flags = fcntl(new_fd, F_GETFL);
  1476. auto rc = fcntl(new_fd, F_SETFL, flags | FD_CLOEXEC);
  1477. ASSERT(rc == 0);
  1478. m_saves.append({ rewiring.new_fd, new_fd });
  1479. m_collector.add(new_fd);
  1480. }
  1481. }
  1482. SavedFileDescriptors::~SavedFileDescriptors()
  1483. {
  1484. for (auto& save : m_saves) {
  1485. if (dup2(save.saved, save.original) < 0) {
  1486. perror("dup2(~SavedFileDescriptors)");
  1487. continue;
  1488. }
  1489. }
  1490. }
  1491. }