2019-05-30 13:12:09 +00:00
|
|
|
#include "GlobalState.h"
|
|
|
|
#include "LineEditor.h"
|
|
|
|
#include "Parser.h"
|
|
|
|
#include <AK/FileSystemPath.h>
|
2019-06-13 13:38:18 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-05-30 13:12:09 +00:00
|
|
|
#include <LibCore/CDirIterator.h>
|
|
|
|
#include <LibCore/CElapsedTimer.h>
|
2019-06-19 09:08:51 +00:00
|
|
|
#include <LibCore/CFile.h>
|
2018-11-09 09:18:04 +00:00
|
|
|
#include <errno.h>
|
2019-05-30 13:12:09 +00:00
|
|
|
#include <fcntl.h>
|
2018-11-09 09:18:04 +00:00
|
|
|
#include <pwd.h>
|
2018-11-06 09:46:40 +00:00
|
|
|
#include <signal.h>
|
2018-11-09 09:24:41 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
2019-02-08 01:38:21 +00:00
|
|
|
#include <sys/stat.h>
|
2018-11-09 09:24:41 +00:00
|
|
|
#include <sys/utsname.h>
|
2019-05-30 13:12:09 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2018-10-26 12:24:11 +00:00
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
#define SH_DEBUG
|
2019-04-25 14:10:16 +00:00
|
|
|
|
2019-05-06 23:39:10 +00:00
|
|
|
GlobalState g;
|
2019-05-06 23:43:21 +00:00
|
|
|
static LineEditor editor;
|
2018-10-23 08:12:50 +00:00
|
|
|
|
2019-07-19 18:01:46 +00:00
|
|
|
static String prompt()
|
2018-10-23 08:12:50 +00:00
|
|
|
{
|
2019-05-06 23:39:10 +00:00
|
|
|
if (g.uid == 0)
|
2019-07-19 18:01:46 +00:00
|
|
|
return "# ";
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.appendf("\033]0;%s@%s:%s\007", g.username.characters(), g.hostname, g.cwd.characters());
|
|
|
|
builder.appendf("\033[31;1m%s\033[0m@\033[37;1m%s\033[0m:\033[32;1m%s\033[0m$> ", g.username.characters(), g.hostname, g.cwd.characters());
|
|
|
|
return builder.to_string();
|
2018-10-23 08:12:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:03:14 +00:00
|
|
|
static int sh_pwd(int, char**)
|
2018-10-26 12:24:11 +00:00
|
|
|
{
|
2019-05-06 23:39:10 +00:00
|
|
|
printf("%s\n", g.cwd.characters());
|
2018-10-26 23:24:22 +00:00
|
|
|
return 0;
|
2018-10-26 12:24:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:03:14 +00:00
|
|
|
static int sh_exit(int, char**)
|
2018-10-28 09:26:07 +00:00
|
|
|
{
|
|
|
|
printf("Good-bye!\n");
|
|
|
|
exit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-26 01:23:27 +00:00
|
|
|
static int sh_export(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc == 1) {
|
|
|
|
for (int i = 0; environ[i]; ++i)
|
|
|
|
puts(environ[i]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto parts = String(argv[1]).split('=');
|
|
|
|
if (parts.size() != 2) {
|
|
|
|
fprintf(stderr, "usage: export variable=value\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-05-16 11:04:47 +00:00
|
|
|
|
2019-05-30 00:51:15 +00:00
|
|
|
return setenv(parts[0].characters(), parts[1].characters(), 1);
|
2019-04-26 01:23:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 12:22:12 +00:00
|
|
|
static int sh_unset(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "usage: unset variable\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsetenv(argv[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:03:14 +00:00
|
|
|
static int sh_cd(int argc, char** argv)
|
2018-10-26 12:24:11 +00:00
|
|
|
{
|
2019-04-25 21:09:21 +00:00
|
|
|
char pathbuf[PATH_MAX];
|
|
|
|
|
2018-10-26 12:24:11 +00:00
|
|
|
if (argc == 1) {
|
2019-05-06 23:39:10 +00:00
|
|
|
strcpy(pathbuf, g.home.characters());
|
2019-04-25 21:09:21 +00:00
|
|
|
} else {
|
2019-09-12 01:46:38 +00:00
|
|
|
if (strcmp(argv[1], "-") == 0) {
|
|
|
|
char* oldpwd = getenv("OLDPWD");
|
2019-09-13 12:42:47 +00:00
|
|
|
if (oldpwd == nullptr)
|
|
|
|
return 1;
|
2019-09-12 01:46:38 +00:00
|
|
|
size_t len = strlen(oldpwd);
|
|
|
|
ASSERT(len + 1 <= PATH_MAX);
|
|
|
|
memcpy(pathbuf, oldpwd, len + 1);
|
|
|
|
} else if (argv[1][0] == '/') {
|
2019-04-25 21:09:21 +00:00
|
|
|
memcpy(pathbuf, argv[1], strlen(argv[1]) + 1);
|
2019-09-12 01:46:38 +00:00
|
|
|
} else {
|
2019-05-06 23:39:10 +00:00
|
|
|
sprintf(pathbuf, "%s/%s", g.cwd.characters(), argv[1]);
|
2019-09-12 01:46:38 +00:00
|
|
|
}
|
2018-10-26 12:24:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
FileSystemPath canonical_path(pathbuf);
|
|
|
|
if (!canonical_path.is_valid()) {
|
2018-10-28 08:36:21 +00:00
|
|
|
printf("FileSystemPath failed to canonicalize '%s'\n", pathbuf);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-01-31 16:31:23 +00:00
|
|
|
const char* path = canonical_path.string().characters();
|
2018-10-28 08:36:21 +00:00
|
|
|
|
2018-10-26 12:24:11 +00:00
|
|
|
struct stat st;
|
2018-11-10 17:16:21 +00:00
|
|
|
int rc = stat(path, &st);
|
2018-10-26 12:24:11 +00:00
|
|
|
if (rc < 0) {
|
2019-05-31 04:07:09 +00:00
|
|
|
printf("stat(%s) failed: %s\n", path, strerror(errno));
|
2018-10-26 12:24:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
2018-10-28 08:36:21 +00:00
|
|
|
printf("Not a directory: %s\n", path);
|
2018-10-26 12:24:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-10-28 08:36:21 +00:00
|
|
|
rc = chdir(path);
|
2018-10-26 12:24:11 +00:00
|
|
|
if (rc < 0) {
|
2018-10-28 08:36:21 +00:00
|
|
|
printf("chdir(%s) failed: %s\n", path, strerror(errno));
|
2018-10-26 12:24:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-09-12 01:46:38 +00:00
|
|
|
setenv("OLDPWD", g.cwd.characters(), 1);
|
2019-05-06 23:39:10 +00:00
|
|
|
g.cwd = canonical_path.string();
|
2019-09-12 01:46:38 +00:00
|
|
|
setenv("PWD", g.cwd.characters(), 1);
|
2018-10-26 12:24:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:43:21 +00:00
|
|
|
static int sh_history(int, char**)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < editor.history().size(); ++i) {
|
|
|
|
printf("%6d %s\n", i, editor.history()[i].characters());
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-10 15:39:30 +00:00
|
|
|
static int sh_umask(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc == 1) {
|
|
|
|
mode_t old_mask = umask(0);
|
|
|
|
printf("%#o\n", old_mask);
|
|
|
|
umask(old_mask);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (argc == 2) {
|
|
|
|
unsigned mask;
|
|
|
|
int matches = sscanf(argv[1], "%o", &mask);
|
|
|
|
if (matches == 1) {
|
|
|
|
umask(mask);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("usage: umask <octal-mask>\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:03:14 +00:00
|
|
|
static bool handle_builtin(int argc, char** argv, int& retval)
|
2018-10-26 12:24:11 +00:00
|
|
|
{
|
|
|
|
if (argc == 0)
|
|
|
|
return false;
|
|
|
|
if (!strcmp(argv[0], "cd")) {
|
|
|
|
retval = sh_cd(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[0], "pwd")) {
|
|
|
|
retval = sh_pwd(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-28 09:26:07 +00:00
|
|
|
if (!strcmp(argv[0], "exit")) {
|
|
|
|
retval = sh_exit(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-26 01:23:27 +00:00
|
|
|
if (!strcmp(argv[0], "export")) {
|
|
|
|
retval = sh_export(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-16 12:22:12 +00:00
|
|
|
if (!strcmp(argv[0], "unset")) {
|
|
|
|
retval = sh_unset(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-06 23:43:21 +00:00
|
|
|
if (!strcmp(argv[0], "history")) {
|
|
|
|
retval = sh_history(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-10 15:39:30 +00:00
|
|
|
if (!strcmp(argv[0], "umask")) {
|
|
|
|
retval = sh_umask(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-26 12:24:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-07 07:36:51 +00:00
|
|
|
class FileDescriptionCollector {
|
2019-04-25 14:10:16 +00:00
|
|
|
public:
|
2019-06-07 09:49:21 +00:00
|
|
|
FileDescriptionCollector() {}
|
2019-06-07 07:36:51 +00:00
|
|
|
~FileDescriptionCollector() { collect(); }
|
2019-04-25 14:10:16 +00:00
|
|
|
|
|
|
|
void collect()
|
|
|
|
{
|
|
|
|
for (auto fd : m_fds)
|
|
|
|
close(fd);
|
|
|
|
m_fds.clear();
|
|
|
|
}
|
|
|
|
void add(int fd) { m_fds.append(fd); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Vector<int, 32> m_fds;
|
|
|
|
};
|
|
|
|
|
2019-05-02 00:22:28 +00:00
|
|
|
struct CommandTimer {
|
|
|
|
CommandTimer()
|
|
|
|
{
|
|
|
|
timer.start();
|
|
|
|
}
|
|
|
|
~CommandTimer()
|
|
|
|
{
|
|
|
|
dbgprintf("sh: command finished in %d ms\n", timer.elapsed());
|
|
|
|
}
|
|
|
|
|
|
|
|
CElapsedTimer timer;
|
|
|
|
};
|
|
|
|
|
2019-06-13 13:38:18 +00:00
|
|
|
static bool is_glob(const StringView& s)
|
2019-05-26 18:36:16 +00:00
|
|
|
{
|
2019-06-13 13:38:18 +00:00
|
|
|
for (int i = 0; i < s.length(); i++) {
|
2019-07-08 13:38:44 +00:00
|
|
|
char c = s.characters_without_null_termination()[i];
|
2019-06-13 13:38:18 +00:00
|
|
|
if (c == '*' || c == '?')
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vector<StringView> split_path(const StringView &path)
|
|
|
|
{
|
|
|
|
Vector<StringView> parts;
|
|
|
|
|
|
|
|
ssize_t substart = 0;
|
|
|
|
for (ssize_t i = 0; i < path.length(); i++) {
|
2019-07-08 13:38:44 +00:00
|
|
|
char ch = path.characters_without_null_termination()[i];
|
2019-06-13 13:38:18 +00:00
|
|
|
if (ch != '/')
|
|
|
|
continue;
|
|
|
|
ssize_t sublen = i - substart;
|
|
|
|
if (sublen != 0)
|
|
|
|
parts.append(path.substring_view(substart, sublen));
|
|
|
|
parts.append(path.substring_view(i, 1));
|
|
|
|
substart = i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t taillen = path.length() - substart;
|
|
|
|
if (taillen != 0)
|
|
|
|
parts.append(path.substring_view(substart, taillen));
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vector<String> expand_globs(const StringView& path, const StringView& base)
|
|
|
|
{
|
|
|
|
auto parts = split_path(path);
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(base);
|
|
|
|
Vector<String> res;
|
|
|
|
|
|
|
|
for (int i = 0; i < parts.size(); ++i) {
|
|
|
|
auto& part = parts[i];
|
|
|
|
if (!is_glob(part)) {
|
|
|
|
builder.append(part);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a glob.
|
|
|
|
String new_base = builder.to_string();
|
|
|
|
StringView new_base_v = new_base;
|
|
|
|
if (new_base_v.is_empty())
|
|
|
|
new_base_v = ".";
|
|
|
|
CDirIterator di(new_base_v, CDirIterator::NoFlags);
|
|
|
|
|
|
|
|
if (di.has_error()) {
|
|
|
|
return res;
|
2019-05-26 18:36:16 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 13:38:18 +00:00
|
|
|
while (di.has_next()) {
|
|
|
|
String name = di.next_path();
|
|
|
|
|
|
|
|
// Dotfiles have to be explicitly requested
|
|
|
|
if (name[0] == '.' && part[0] != '.')
|
2019-05-26 18:36:16 +00:00
|
|
|
continue;
|
|
|
|
|
2019-06-13 13:38:18 +00:00
|
|
|
// And even if they are, skip . and ..
|
|
|
|
if (name == "." || name == "..")
|
|
|
|
continue;
|
2019-05-26 18:36:16 +00:00
|
|
|
|
2019-06-13 13:38:18 +00:00
|
|
|
if (name.matches(part, String::CaseSensitivity::CaseSensitive)) {
|
2019-05-26 18:36:16 +00:00
|
|
|
|
2019-06-13 13:38:18 +00:00
|
|
|
StringBuilder nested_base;
|
|
|
|
nested_base.append(new_base);
|
|
|
|
nested_base.append(name);
|
2019-05-26 18:36:16 +00:00
|
|
|
|
2019-06-13 13:38:18 +00:00
|
|
|
StringView remaining_path = path.substring_view_starting_after_substring(part);
|
|
|
|
Vector<String> nested_res = expand_globs(remaining_path, nested_base.to_string());
|
|
|
|
for (auto& s : nested_res)
|
|
|
|
res.append(s);
|
2019-05-26 18:36:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-13 13:38:18 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found no globs.
|
|
|
|
String new_path = builder.to_string();
|
|
|
|
if (access(new_path.characters(), F_OK) == 0)
|
|
|
|
res.append(new_path);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:41:57 +00:00
|
|
|
static Vector<String> expand_parameters(const StringView& param)
|
|
|
|
{
|
|
|
|
bool is_variable = param.length() > 1 && param[0] == '$';
|
|
|
|
if (!is_variable)
|
|
|
|
return { param };
|
|
|
|
|
|
|
|
String variable_name = String(param.substring_view(1, param.length() - 1));
|
2019-09-09 01:00:35 +00:00
|
|
|
if (variable_name == "?")
|
|
|
|
return { String::number(g.last_return_code) };
|
2019-09-09 21:48:04 +00:00
|
|
|
else if (variable_name == "$")
|
|
|
|
return { String::number(getpid()) };
|
2019-09-08 22:41:57 +00:00
|
|
|
|
|
|
|
char* env_value = getenv(variable_name.characters());
|
|
|
|
if (env_value == nullptr)
|
|
|
|
return { "" };
|
|
|
|
|
|
|
|
Vector<String> res;
|
|
|
|
String str_env_value = String(env_value);
|
|
|
|
const auto& split_text = str_env_value.split_view(' ');
|
|
|
|
for (auto& part : split_text)
|
|
|
|
res.append(part);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-06-13 13:38:18 +00:00
|
|
|
static Vector<String> process_arguments(const Vector<String>& args)
|
|
|
|
{
|
|
|
|
Vector<String> argv_string;
|
|
|
|
for (auto& arg : args) {
|
2019-09-08 22:41:57 +00:00
|
|
|
// This will return the text passed in if it wasn't a variable
|
|
|
|
// This lets us just loop over its values
|
|
|
|
auto expanded_parameters = expand_parameters(arg);
|
|
|
|
|
|
|
|
for (auto& exp_arg : expanded_parameters) {
|
|
|
|
auto expanded_globs = expand_globs(exp_arg, "");
|
|
|
|
for (auto& path : expanded_globs)
|
2019-06-13 13:38:18 +00:00
|
|
|
argv_string.append(path);
|
2019-09-08 22:41:57 +00:00
|
|
|
|
|
|
|
if (expanded_globs.is_empty())
|
|
|
|
argv_string.append(exp_arg);
|
|
|
|
}
|
2019-05-26 18:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return argv_string;
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:01:50 +00:00
|
|
|
static int run_command(const String& cmd)
|
2018-10-23 08:12:50 +00:00
|
|
|
{
|
2019-05-06 23:01:50 +00:00
|
|
|
if (cmd.is_empty())
|
2018-10-25 10:06:00 +00:00
|
|
|
return 0;
|
2018-10-26 09:16:56 +00:00
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
auto commands = Parser(cmd).parse();
|
2019-04-25 12:13:36 +00:00
|
|
|
|
2019-04-25 14:10:16 +00:00
|
|
|
#ifdef SH_DEBUG
|
2019-08-30 04:54:05 +00:00
|
|
|
for (auto& command : commands) {
|
|
|
|
for (int i = 0; i < command.subcommands.size(); ++i) {
|
2019-04-25 14:10:16 +00:00
|
|
|
for (int j = 0; j < i; ++j)
|
2019-05-25 22:38:11 +00:00
|
|
|
dbgprintf(" ");
|
2019-08-30 04:54:05 +00:00
|
|
|
for (auto& arg : command.subcommands[i].args) {
|
|
|
|
dbgprintf("<%s> ", arg.characters());
|
2019-06-07 09:49:21 +00:00
|
|
|
}
|
2019-08-30 04:54:05 +00:00
|
|
|
dbgprintf("\n");
|
|
|
|
for (auto& redirecton : command.subcommands[i].redirections) {
|
|
|
|
for (int j = 0; j < i; ++j)
|
|
|
|
dbgprintf(" ");
|
|
|
|
dbgprintf(" ");
|
|
|
|
switch (redirecton.type) {
|
|
|
|
case Redirection::Pipe:
|
|
|
|
dbgprintf("Pipe\n");
|
|
|
|
break;
|
|
|
|
case Redirection::FileRead:
|
|
|
|
dbgprintf("fd:%d = FileRead: %s\n", redirecton.fd, redirecton.path.characters());
|
|
|
|
break;
|
|
|
|
case Redirection::FileWrite:
|
|
|
|
dbgprintf("fd:%d = FileWrite: %s\n", redirecton.fd, redirecton.path.characters());
|
|
|
|
break;
|
|
|
|
case Redirection::FileWriteAppend:
|
|
|
|
dbgprintf("fd:%d = FileWriteAppend: %s\n", redirecton.fd, redirecton.path.characters());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-05-25 22:38:11 +00:00
|
|
|
}
|
2019-04-25 12:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-30 04:54:05 +00:00
|
|
|
dbgprintf("\n");
|
2018-10-26 12:24:11 +00:00
|
|
|
}
|
2019-08-30 04:54:05 +00:00
|
|
|
#endif
|
2018-10-26 12:24:11 +00:00
|
|
|
|
2018-12-07 00:26:07 +00:00
|
|
|
struct termios trm;
|
|
|
|
tcgetattr(0, &trm);
|
|
|
|
|
2019-06-06 08:57:51 +00:00
|
|
|
struct SpawnedProcess {
|
|
|
|
String name;
|
|
|
|
pid_t pid;
|
|
|
|
};
|
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
int return_value = 0;
|
2019-04-25 12:13:36 +00:00
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
for (auto& command : commands) {
|
|
|
|
if (command.subcommands.is_empty())
|
|
|
|
continue;
|
2019-05-02 00:22:28 +00:00
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
FileDescriptionCollector fds;
|
|
|
|
|
|
|
|
for (int i = 0; i < command.subcommands.size(); ++i) {
|
|
|
|
auto& subcommand = command.subcommands[i];
|
|
|
|
for (auto& redirection : subcommand.redirections) {
|
|
|
|
switch (redirection.type) {
|
|
|
|
case Redirection::Pipe: {
|
|
|
|
int pipefd[2];
|
|
|
|
int rc = pipe(pipefd);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("pipe");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
subcommand.rewirings.append({ STDOUT_FILENO, pipefd[1] });
|
|
|
|
auto& next_command = command.subcommands[i + 1];
|
|
|
|
next_command.rewirings.append({ STDIN_FILENO, pipefd[0] });
|
|
|
|
fds.add(pipefd[0]);
|
|
|
|
fds.add(pipefd[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Redirection::FileWriteAppend: {
|
|
|
|
int fd = open(redirection.path.characters(), O_WRONLY | O_CREAT | O_APPEND, 0666);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
subcommand.rewirings.append({ redirection.fd, fd });
|
|
|
|
fds.add(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Redirection::FileWrite: {
|
|
|
|
int fd = open(redirection.path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
subcommand.rewirings.append({ redirection.fd, fd });
|
|
|
|
fds.add(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Redirection::FileRead: {
|
|
|
|
int fd = open(redirection.path.characters(), O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
subcommand.rewirings.append({ redirection.fd, fd });
|
|
|
|
fds.add(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 18:36:16 +00:00
|
|
|
}
|
2019-08-30 04:54:05 +00:00
|
|
|
|
|
|
|
Vector<SpawnedProcess> children;
|
|
|
|
|
|
|
|
CommandTimer timer;
|
|
|
|
|
|
|
|
for (int i = 0; i < command.subcommands.size(); ++i) {
|
|
|
|
auto& subcommand = command.subcommands[i];
|
|
|
|
Vector<String> argv_string = process_arguments(subcommand.args);
|
|
|
|
Vector<const char*> argv;
|
|
|
|
argv.ensure_capacity(argv_string.size());
|
|
|
|
for (const auto& s : argv_string) {
|
|
|
|
argv.append(s.characters());
|
|
|
|
}
|
|
|
|
argv.append(nullptr);
|
2019-04-25 12:13:36 +00:00
|
|
|
|
2019-05-26 18:36:16 +00:00
|
|
|
#ifdef SH_DEBUG
|
2019-08-30 04:54:05 +00:00
|
|
|
for (auto& arg : argv) {
|
|
|
|
dbgprintf("<%s> ", arg);
|
|
|
|
}
|
|
|
|
dbgprintf("\n");
|
2019-05-26 18:36:16 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
int retval = 0;
|
|
|
|
if (handle_builtin(argv.size() - 1, const_cast<char**>(argv.data()), retval))
|
|
|
|
return retval;
|
2019-04-25 12:13:36 +00:00
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
pid_t child = fork();
|
|
|
|
if (!child) {
|
|
|
|
setpgid(0, 0);
|
|
|
|
tcsetpgrp(0, getpid());
|
|
|
|
for (auto& rewiring : subcommand.rewirings) {
|
2019-06-04 18:36:08 +00:00
|
|
|
#ifdef SH_DEBUG
|
2019-08-30 04:54:05 +00:00
|
|
|
dbgprintf("in %s<%d>, dup2(%d, %d)\n", argv[0], getpid(), rewiring.rewire_fd, rewiring.fd);
|
2019-04-25 14:10:16 +00:00
|
|
|
#endif
|
2019-08-30 04:54:05 +00:00
|
|
|
int rc = dup2(rewiring.rewire_fd, rewiring.fd);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("dup2");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-04-25 12:13:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
fds.collect();
|
2019-04-25 12:13:36 +00:00
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
int rc = execvp(argv[0], const_cast<char* const*>(argv.data()));
|
|
|
|
if (rc < 0) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
fprintf(stderr, "%s: Command not found.\n", argv[0]);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "execvp(%s): %s\n", argv[0], strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
2019-04-25 12:13:36 +00:00
|
|
|
}
|
2019-08-30 04:54:05 +00:00
|
|
|
children.append({ argv[0], child });
|
2018-11-03 01:04:36 +00:00
|
|
|
}
|
2019-04-25 12:13:36 +00:00
|
|
|
|
2019-04-25 14:10:16 +00:00
|
|
|
#ifdef SH_DEBUG
|
2019-08-30 04:54:05 +00:00
|
|
|
dbgprintf("Closing fds in shell process:\n");
|
2019-04-25 14:10:16 +00:00
|
|
|
#endif
|
2019-08-30 04:54:05 +00:00
|
|
|
fds.collect();
|
2018-10-29 20:54:11 +00:00
|
|
|
|
2019-04-25 14:10:16 +00:00
|
|
|
#ifdef SH_DEBUG
|
2019-08-30 04:54:05 +00:00
|
|
|
dbgprintf("Now we gotta wait on children:\n");
|
|
|
|
for (auto& child : children)
|
|
|
|
dbgprintf(" %d\n", child);
|
2019-04-26 15:33:06 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
int wstatus = 0;
|
2019-04-25 12:13:36 +00:00
|
|
|
|
2019-08-30 04:54:05 +00:00
|
|
|
for (int i = 0; i < children.size(); ++i) {
|
|
|
|
auto& child = children[i];
|
|
|
|
do {
|
|
|
|
int rc = waitpid(child.pid, &wstatus, WEXITED | WSTOPPED);
|
|
|
|
if (rc < 0 && errno != EINTR) {
|
|
|
|
if (errno != ECHILD)
|
|
|
|
perror("waitpid");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (WIFEXITED(wstatus)) {
|
|
|
|
if (WEXITSTATUS(wstatus) != 0)
|
|
|
|
dbg() << "Shell: " << child.name << ":" << child.pid << " exited with status " << WEXITSTATUS(wstatus);
|
|
|
|
if (i == 0)
|
|
|
|
return_value = WEXITSTATUS(wstatus);
|
|
|
|
} else if (WIFSTOPPED(wstatus)) {
|
|
|
|
printf("Shell: %s(%d) stopped.\n", child.name.characters(), child.pid);
|
2019-06-06 08:54:54 +00:00
|
|
|
} else {
|
2019-08-30 04:54:05 +00:00
|
|
|
if (WIFSIGNALED(wstatus)) {
|
|
|
|
printf("Shell: %s(%d) exited due to signal '%s'\n", child.name.characters(), child.pid, strsignal(WTERMSIG(wstatus)));
|
|
|
|
} else {
|
|
|
|
printf("Shell: %s(%d) exited abnormally\n", child.name.characters(), child.pid);
|
|
|
|
}
|
2019-06-06 08:54:54 +00:00
|
|
|
}
|
2019-08-30 04:54:05 +00:00
|
|
|
} while (errno == EINTR);
|
|
|
|
}
|
2019-04-25 12:13:36 +00:00
|
|
|
}
|
2018-10-26 23:24:22 +00:00
|
|
|
|
2019-09-09 01:00:35 +00:00
|
|
|
g.last_return_code = return_value;
|
|
|
|
|
2018-11-03 01:04:36 +00:00
|
|
|
// FIXME: Should I really have to tcsetpgrp() after my child has exited?
|
|
|
|
// Is the terminal controlling pgrp really still the PGID of the dead process?
|
2018-11-02 12:14:25 +00:00
|
|
|
tcsetpgrp(0, getpid());
|
2018-12-07 00:26:07 +00:00
|
|
|
tcsetattr(0, TCSANOW, &trm);
|
2019-06-06 08:54:54 +00:00
|
|
|
return return_value;
|
2018-10-23 08:12:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:00:01 +00:00
|
|
|
static String get_history_path()
|
2019-06-19 09:08:51 +00:00
|
|
|
{
|
2019-09-04 17:00:01 +00:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(g.home);
|
|
|
|
builder.append("/.history");
|
|
|
|
return builder.to_string();
|
2019-06-19 09:08:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void load_history()
|
|
|
|
{
|
2019-09-04 17:00:01 +00:00
|
|
|
CFile history_file(get_history_path());
|
|
|
|
if (!history_file.open(CIODevice::ReadOnly))
|
2019-09-04 13:14:12 +00:00
|
|
|
return;
|
2019-06-19 09:08:51 +00:00
|
|
|
while (history_file.can_read_line()) {
|
2019-09-04 13:14:12 +00:00
|
|
|
auto b = history_file.read_line(1024);
|
2019-06-19 09:08:51 +00:00
|
|
|
// skip the newline and terminating bytes
|
|
|
|
editor.add_to_history(String(reinterpret_cast<const char*>(b.pointer()), b.size() - 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void save_history()
|
|
|
|
{
|
2019-09-04 17:00:01 +00:00
|
|
|
CFile history_file(get_history_path());
|
|
|
|
if (!history_file.open(CIODevice::WriteOnly))
|
2019-09-04 13:14:12 +00:00
|
|
|
return;
|
2019-06-19 09:08:51 +00:00
|
|
|
for (const auto& line : editor.history()) {
|
|
|
|
history_file.write(line);
|
|
|
|
history_file.write("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 03:32:31 +00:00
|
|
|
int main(int argc, char** argv)
|
2018-10-23 08:12:50 +00:00
|
|
|
{
|
2019-05-06 23:39:10 +00:00
|
|
|
g.uid = getuid();
|
|
|
|
g.sid = setsid();
|
2018-11-02 12:14:25 +00:00
|
|
|
tcsetpgrp(0, getpgrp());
|
2019-05-06 23:39:10 +00:00
|
|
|
tcgetattr(0, &g.termios);
|
2019-01-25 14:34:21 +00:00
|
|
|
|
2019-07-08 17:04:13 +00:00
|
|
|
signal(SIGINT, [](int) {
|
|
|
|
g.was_interrupted = true;
|
|
|
|
});
|
2018-11-07 18:03:44 +00:00
|
|
|
|
2019-07-08 17:04:13 +00:00
|
|
|
signal(SIGHUP, [](int) {
|
|
|
|
save_history();
|
|
|
|
});
|
|
|
|
|
|
|
|
signal(SIGWINCH, [](int) {
|
|
|
|
g.was_resized = true;
|
|
|
|
});
|
2019-06-19 09:08:51 +00:00
|
|
|
|
2019-05-06 23:39:10 +00:00
|
|
|
int rc = gethostname(g.hostname, sizeof(g.hostname));
|
2018-10-27 23:08:01 +00:00
|
|
|
if (rc < 0)
|
|
|
|
perror("gethostname");
|
2019-05-06 23:39:10 +00:00
|
|
|
rc = ttyname_r(0, g.ttyname, sizeof(g.ttyname));
|
2018-10-31 00:21:56 +00:00
|
|
|
if (rc < 0)
|
|
|
|
perror("ttyname_r");
|
2019-01-29 23:49:20 +00:00
|
|
|
|
2018-10-31 20:31:56 +00:00
|
|
|
{
|
|
|
|
auto* pw = getpwuid(getuid());
|
2019-04-25 21:09:21 +00:00
|
|
|
if (pw) {
|
2019-05-06 23:39:10 +00:00
|
|
|
g.username = pw->pw_name;
|
|
|
|
g.home = pw->pw_dir;
|
2019-05-30 00:51:15 +00:00
|
|
|
setenv("HOME", pw->pw_dir, 1);
|
2019-04-25 21:09:21 +00:00
|
|
|
}
|
2018-10-31 20:31:56 +00:00
|
|
|
endpwent();
|
|
|
|
}
|
2018-10-27 23:08:01 +00:00
|
|
|
|
2019-05-13 02:52:55 +00:00
|
|
|
if (argc > 2 && !strcmp(argv[1], "-c")) {
|
|
|
|
dbgprintf("sh -c '%s'\n", argv[2]);
|
|
|
|
run_command(argv[2]);
|
|
|
|
return 0;
|
2019-02-03 03:32:31 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 23:06:31 +00:00
|
|
|
{
|
2019-05-06 23:01:50 +00:00
|
|
|
auto* cwd = getcwd(nullptr, 0);
|
2019-05-06 23:39:10 +00:00
|
|
|
g.cwd = cwd;
|
2019-09-12 01:46:38 +00:00
|
|
|
setenv("PWD", cwd, 1);
|
2019-05-06 23:01:50 +00:00
|
|
|
free(cwd);
|
2018-10-29 23:06:31 +00:00
|
|
|
}
|
2019-05-06 23:39:10 +00:00
|
|
|
|
2019-06-19 09:08:51 +00:00
|
|
|
load_history();
|
|
|
|
atexit(save_history);
|
|
|
|
|
2018-10-23 08:12:50 +00:00
|
|
|
for (;;) {
|
2019-07-19 18:01:46 +00:00
|
|
|
auto line = editor.get_line(prompt());
|
2019-05-06 23:39:10 +00:00
|
|
|
if (line.is_empty())
|
|
|
|
continue;
|
|
|
|
run_command(line);
|
|
|
|
editor.add_to_history(line);
|
2018-10-23 08:12:50 +00:00
|
|
|
}
|
2019-05-06 23:39:10 +00:00
|
|
|
|
2018-10-23 08:12:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|