2018-11-09 10:18:04 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <pwd.h>
|
2018-11-06 10:46:40 +01:00
|
|
|
#include <signal.h>
|
2018-11-09 10:24:41 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2018-11-17 15:56:09 +01:00
|
|
|
#include <fcntl.h>
|
2018-12-07 01:26:07 +01:00
|
|
|
#include <termios.h>
|
2018-11-09 10:24:41 +01:00
|
|
|
#include <sys/mman.h>
|
2019-02-08 02:38:21 +01:00
|
|
|
#include <sys/stat.h>
|
2018-11-09 10:24:41 +01:00
|
|
|
#include <sys/utsname.h>
|
2018-10-28 09:36:21 +01:00
|
|
|
#include <AK/FileSystemPath.h>
|
2018-10-26 14:24:11 +02:00
|
|
|
|
2018-10-28 09:36:21 +01:00
|
|
|
struct GlobalState {
|
|
|
|
String cwd;
|
2018-10-31 21:31:56 +01:00
|
|
|
String username;
|
2018-10-31 01:21:56 +01:00
|
|
|
char ttyname[32];
|
2018-10-28 09:36:21 +01:00
|
|
|
char hostname[32];
|
2018-11-02 12:56:51 +01:00
|
|
|
pid_t sid;
|
2018-11-13 00:17:30 +01:00
|
|
|
uid_t uid;
|
2019-02-22 10:45:32 +01:00
|
|
|
struct termios termios;
|
2019-01-25 15:49:54 +01:00
|
|
|
bool was_interrupted { false };
|
2018-10-28 09:36:21 +01:00
|
|
|
};
|
|
|
|
static GlobalState* g;
|
2018-10-23 10:12:50 +02:00
|
|
|
|
|
|
|
static void prompt()
|
|
|
|
{
|
2018-11-13 00:17:30 +01:00
|
|
|
if (g->uid == 0)
|
2018-10-23 10:12:50 +02:00
|
|
|
printf("# ");
|
2019-01-25 05:51:39 +01:00
|
|
|
else {
|
|
|
|
printf("\033]0;%s@%s:%s\007", g->username.characters(), g->hostname, g->cwd.characters());
|
2018-10-31 21:31:56 +01:00
|
|
|
printf("\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());
|
2019-01-25 05:51:39 +01:00
|
|
|
}
|
2018-11-08 01:23:23 +01:00
|
|
|
fflush(stdout);
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2019-01-23 17:03:14 +01:00
|
|
|
static int sh_pwd(int, char**)
|
2018-10-26 14:24:11 +02:00
|
|
|
{
|
2018-10-28 09:36:21 +01:00
|
|
|
printf("%s\n", g->cwd.characters());
|
2018-10-27 01:24:22 +02:00
|
|
|
return 0;
|
2018-10-26 14:24:11 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 19:03:44 +01:00
|
|
|
static volatile bool g_got_signal = false;
|
|
|
|
|
2018-11-06 10:46:40 +01:00
|
|
|
void did_receive_signal(int signum)
|
|
|
|
{
|
|
|
|
printf("\nMy word, I've received a signal with number %d\n", signum);
|
2018-11-07 19:03:44 +01:00
|
|
|
g_got_signal = true;
|
|
|
|
}
|
|
|
|
|
2018-11-09 10:18:04 +01:00
|
|
|
void handle_sigint(int)
|
2018-11-07 19:03:44 +01:00
|
|
|
{
|
2019-01-25 15:49:54 +01:00
|
|
|
g->was_interrupted = true;
|
2018-11-06 10:46:40 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 17:03:14 +01:00
|
|
|
static int sh_exit(int, char**)
|
2018-10-28 10:26:07 +01:00
|
|
|
{
|
|
|
|
printf("Good-bye!\n");
|
|
|
|
exit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-23 17:03:14 +01:00
|
|
|
static int sh_cd(int argc, char** argv)
|
2018-10-26 14:24:11 +02:00
|
|
|
{
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("usage: cd <path>\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char pathbuf[128];
|
2018-10-28 09:36:21 +01:00
|
|
|
if (argv[1][0] == '/')
|
|
|
|
memcpy(pathbuf, argv[1], strlen(argv[1]) + 1);
|
2018-10-26 14:24:11 +02:00
|
|
|
else
|
2018-10-28 09:36:21 +01:00
|
|
|
sprintf(pathbuf, "%s/%s", g->cwd.characters(), argv[1]);
|
|
|
|
|
2019-01-31 17:31:23 +01:00
|
|
|
FileSystemPath canonical_path(pathbuf);
|
|
|
|
if (!canonical_path.is_valid()) {
|
2018-10-28 09:36:21 +01:00
|
|
|
printf("FileSystemPath failed to canonicalize '%s'\n", pathbuf);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-01-31 17:31:23 +01:00
|
|
|
const char* path = canonical_path.string().characters();
|
2018-10-28 09:36:21 +01:00
|
|
|
|
2018-10-26 14:24:11 +02:00
|
|
|
struct stat st;
|
2018-11-10 18:16:21 +01:00
|
|
|
int rc = stat(path, &st);
|
2018-10-26 14:24:11 +02:00
|
|
|
if (rc < 0) {
|
2018-10-28 09:36:21 +01:00
|
|
|
printf("lstat(%s) failed: %s\n", path, strerror(errno));
|
2018-10-26 14:24:11 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
2018-10-28 09:36:21 +01:00
|
|
|
printf("Not a directory: %s\n", path);
|
2018-10-26 14:24:11 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-10-28 09:36:21 +01:00
|
|
|
rc = chdir(path);
|
2018-10-26 14:24:11 +02:00
|
|
|
if (rc < 0) {
|
2018-10-28 09:36:21 +01:00
|
|
|
printf("chdir(%s) failed: %s\n", path, strerror(errno));
|
2018-10-26 14:24:11 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2019-01-31 17:31:23 +01:00
|
|
|
g->cwd = canonical_path.string();
|
2018-10-26 14:24:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-23 17:03:14 +01:00
|
|
|
static bool handle_builtin(int argc, char** argv, int& retval)
|
2018-10-26 14:24:11 +02: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 10:26:07 +01:00
|
|
|
if (!strcmp(argv[0], "exit")) {
|
|
|
|
retval = sh_exit(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-26 14:24:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:13:36 +02:00
|
|
|
struct Redirection {
|
|
|
|
enum Type { Pipe, File, Rewire };
|
|
|
|
Type type;
|
2019-04-25 14:33:47 +02:00
|
|
|
int fd { -1 };
|
|
|
|
int rewire_fd { -1 };
|
2019-04-25 14:13:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Subcommand {
|
|
|
|
Vector<String> args;
|
|
|
|
Vector<Redirection> redirections;
|
|
|
|
};
|
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
class Parser {
|
|
|
|
public:
|
|
|
|
explicit Parser(const String& input) : m_input(input) { }
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
Vector<Subcommand> parse();
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
private:
|
|
|
|
void commit_token();
|
|
|
|
void commit_subcommand();
|
|
|
|
void do_pipe();
|
|
|
|
void begin_redirect_fd(int fd);
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
enum State {
|
|
|
|
Free,
|
|
|
|
InSingleQuotes,
|
|
|
|
InDoubleQuotes,
|
|
|
|
InRedirectionPath,
|
2019-04-25 14:13:36 +02:00
|
|
|
};
|
2019-04-25 14:33:47 +02:00
|
|
|
State m_state { Free };
|
|
|
|
String m_input;
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
Vector<Subcommand> m_subcommands;
|
|
|
|
Vector<String> m_tokens;
|
|
|
|
Vector<Redirection> m_redirections;
|
|
|
|
Vector<char> m_token;
|
|
|
|
};
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
void Parser::commit_token()
|
|
|
|
{
|
|
|
|
if (m_token.is_empty())
|
|
|
|
return;
|
|
|
|
m_tokens.append(String::copy(m_token));
|
|
|
|
m_token.clear_with_capacity();
|
|
|
|
};
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
void Parser::commit_subcommand()
|
|
|
|
{
|
|
|
|
if (m_tokens.is_empty())
|
|
|
|
return;
|
|
|
|
m_subcommands.append({ move(m_tokens), move(m_redirections) });
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::do_pipe()
|
|
|
|
{
|
|
|
|
m_redirections.append({ Redirection::Pipe, STDOUT_FILENO });
|
|
|
|
commit_subcommand();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::begin_redirect_fd(int fd)
|
|
|
|
{
|
|
|
|
m_redirections.append({ Redirection::File, fd });
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Subcommand> Parser::parse()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_input.length(); ++i) {
|
|
|
|
char ch = m_input.characters()[i];
|
|
|
|
switch (m_state) {
|
|
|
|
case State::Free:
|
|
|
|
if (ch == ' ') {
|
2019-04-25 14:13:36 +02:00
|
|
|
commit_token();
|
|
|
|
break;
|
|
|
|
}
|
2019-04-25 14:33:47 +02:00
|
|
|
if (ch == '|') {
|
2019-04-25 14:13:36 +02:00
|
|
|
commit_token();
|
2019-04-25 14:33:47 +02:00
|
|
|
if (m_tokens.is_empty()) {
|
2019-04-25 14:13:36 +02:00
|
|
|
fprintf(stderr, "Syntax error: Nothing before pipe (|)\n");
|
|
|
|
return { };
|
|
|
|
}
|
2019-04-25 14:33:47 +02:00
|
|
|
do_pipe();
|
2019-04-25 14:13:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-04-25 14:33:47 +02:00
|
|
|
if (ch == '>') {
|
|
|
|
begin_redirect_fd(STDOUT_FILENO);
|
|
|
|
m_state = State::InRedirectionPath;
|
2019-04-25 14:13:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-04-25 14:33:47 +02:00
|
|
|
if (ch == '<') {
|
|
|
|
begin_redirect_fd(STDIN_FILENO);
|
|
|
|
m_state = State::InRedirectionPath;
|
2019-04-25 14:13:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-04-25 14:33:47 +02:00
|
|
|
if (ch == '\'') {
|
|
|
|
m_state = State::InSingleQuotes;
|
2019-04-25 14:13:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-04-25 14:33:47 +02:00
|
|
|
if (ch == '\"') {
|
|
|
|
m_state = State::InDoubleQuotes;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_token.append(ch);
|
|
|
|
break;
|
|
|
|
case State::InRedirectionPath:
|
|
|
|
if (ch == ' ') {
|
|
|
|
commit_token();
|
2019-04-25 14:13:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2019-04-25 14:33:47 +02:00
|
|
|
case State::InSingleQuotes:
|
|
|
|
if (ch == '\'') {
|
2019-04-25 14:13:36 +02:00
|
|
|
commit_token();
|
2019-04-25 14:33:47 +02:00
|
|
|
m_state = State::Free;
|
2019-04-25 14:13:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-04-25 14:33:47 +02:00
|
|
|
m_token.append(ch);
|
2019-04-25 14:13:36 +02:00
|
|
|
break;
|
2019-04-25 14:33:47 +02:00
|
|
|
case State::InDoubleQuotes:
|
|
|
|
if (ch == '\"') {
|
2019-04-25 14:13:36 +02:00
|
|
|
commit_token();
|
2019-04-25 14:33:47 +02:00
|
|
|
m_state = State::Free;
|
2019-04-25 14:13:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-04-25 14:33:47 +02:00
|
|
|
m_token.append(ch);
|
2019-04-25 14:13:36 +02:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
commit_token();
|
|
|
|
commit_subcommand();
|
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
if (!m_subcommands.is_empty()) {
|
|
|
|
for (auto& redirection : m_subcommands.last().redirections) {
|
2019-04-25 14:13:36 +02:00
|
|
|
if (redirection.type == Redirection::Pipe) {
|
|
|
|
fprintf(stderr, "Syntax error: Nothing after last pipe (|)\n");
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
return move(m_subcommands);
|
2019-04-25 14:13:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 10:12:50 +02:00
|
|
|
static int runcmd(char* cmd)
|
|
|
|
{
|
2018-10-25 12:06:00 +02:00
|
|
|
if (cmd[0] == 0)
|
|
|
|
return 0;
|
2018-10-23 13:05:50 +02:00
|
|
|
char buf[128];
|
2018-10-26 14:24:11 +02:00
|
|
|
memcpy(buf, cmd, 128);
|
2018-10-26 11:16:56 +02:00
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
auto subcommands = Parser(cmd).parse();
|
2019-04-25 14:13:36 +02:00
|
|
|
|
|
|
|
#ifdef PARSE_DEBUG
|
|
|
|
for (int i = 0; i < subcommands.size(); ++i) {
|
|
|
|
for (int j = 0; j < i; ++j)
|
|
|
|
printf(" ");
|
|
|
|
for (auto& arg : subcommands[i].args) {
|
|
|
|
printf("<%s> ", arg.characters());
|
2018-10-26 11:16:56 +02:00
|
|
|
}
|
2019-04-25 14:13:36 +02:00
|
|
|
printf("\n");
|
2018-10-26 11:16:56 +02:00
|
|
|
}
|
2019-04-25 14:13:36 +02:00
|
|
|
#endif
|
2018-10-26 14:24:11 +02:00
|
|
|
|
2019-04-25 14:13:36 +02:00
|
|
|
if (subcommands.is_empty())
|
2018-10-26 14:24:11 +02:00
|
|
|
return 0;
|
2019-04-25 14:13:36 +02:00
|
|
|
|
|
|
|
Vector<int> fds;
|
|
|
|
|
|
|
|
for (int i = 0; i < subcommands.size(); ++i) {
|
|
|
|
auto& subcommand = subcommands[i];
|
|
|
|
for (auto& redirection : subcommand.redirections) {
|
|
|
|
if (redirection.type == Redirection::Pipe) {
|
|
|
|
int pipefd[2];
|
|
|
|
int rc = pipe(pipefd);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("pipe");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
subcommand.redirections.append({ Redirection::Rewire, STDOUT_FILENO, pipefd[1] });
|
|
|
|
auto& next_command = subcommands[i + 1];
|
|
|
|
next_command.redirections.append({ Redirection::Rewire, STDIN_FILENO, pipefd[0] });
|
|
|
|
fds.append(pipefd[0]);
|
|
|
|
fds.append(pipefd[1]);
|
|
|
|
}
|
|
|
|
}
|
2018-10-26 14:24:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-07 01:26:07 +01:00
|
|
|
struct termios trm;
|
|
|
|
tcgetattr(0, &trm);
|
|
|
|
|
2019-04-25 14:13:36 +02:00
|
|
|
Vector<pid_t> children;
|
|
|
|
|
|
|
|
for (int i = 0; i < subcommands.size(); ++i) {
|
|
|
|
auto& subcommand = subcommands[i];
|
|
|
|
Vector<const char*> argv;
|
|
|
|
for (auto& arg : subcommand.args)
|
|
|
|
argv.append(arg.characters());
|
|
|
|
argv.append(nullptr);
|
|
|
|
|
|
|
|
int retval = 0;
|
|
|
|
if (handle_builtin(argv.size() - 1, (char**)argv.data(), retval))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
pid_t child = fork();
|
|
|
|
if (!child) {
|
|
|
|
setpgid(0, 0);
|
|
|
|
tcsetpgrp(0, getpid());
|
|
|
|
for (auto& redirection : subcommand.redirections) {
|
|
|
|
if (redirection.type == Redirection::Rewire) {
|
|
|
|
dbgprintf("in %s<%d>, dup2(%d, %d)\n", argv[0], getpid(), redirection.rewire_fd, redirection.fd);
|
|
|
|
int rc = dup2(redirection.rewire_fd, redirection.fd);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("dup2");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& fd : fds)
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
int rc = execvp(argv[0], (char* const*)argv.data());
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("execvp");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
2018-11-03 02:04:36 +01:00
|
|
|
}
|
2019-04-25 14:13:36 +02:00
|
|
|
children.append(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
dbgprintf("Closing fds in shell process:\n");
|
|
|
|
for (auto& fd : fds) {
|
|
|
|
dbgprintf(" %d\n", fd);
|
|
|
|
close(fd);
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
2018-10-29 21:54:11 +01:00
|
|
|
|
2019-04-25 14:13:36 +02:00
|
|
|
dbgprintf("Now we gotta wait on children:\n");
|
|
|
|
for (auto& child : children)
|
|
|
|
dbgprintf(" %d\n", child);
|
|
|
|
|
2018-10-27 01:24:22 +02:00
|
|
|
int wstatus = 0;
|
2018-11-07 23:59:49 +01:00
|
|
|
int rc;
|
2019-04-25 14:13:36 +02:00
|
|
|
|
|
|
|
for (auto& child : children) {
|
|
|
|
do {
|
|
|
|
rc = waitpid(child, &wstatus, 0);
|
|
|
|
if (rc < 0 && errno != EINTR) {
|
|
|
|
perror("waitpid");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(errno == EINTR);
|
|
|
|
}
|
2018-10-27 01:24:22 +02:00
|
|
|
|
2018-11-03 02:04:36 +01: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 13:14:25 +01:00
|
|
|
tcsetpgrp(0, getpid());
|
|
|
|
|
2018-12-07 01:26:07 +01:00
|
|
|
tcsetattr(0, TCSANOW, &trm);
|
|
|
|
|
2018-10-27 01:24:22 +02:00
|
|
|
if (WIFEXITED(wstatus)) {
|
2018-11-11 15:36:40 +01:00
|
|
|
if (WEXITSTATUS(wstatus) != 0)
|
|
|
|
printf("Exited with status %d\n", WEXITSTATUS(wstatus));
|
2019-04-25 14:13:36 +02:00
|
|
|
return WEXITSTATUS(wstatus);
|
2018-10-27 01:24:22 +02:00
|
|
|
} else {
|
2018-11-01 01:11:00 +01:00
|
|
|
if (WIFSIGNALED(wstatus)) {
|
2018-11-02 14:06:48 +01:00
|
|
|
switch (WTERMSIG(wstatus)) {
|
|
|
|
case SIGINT:
|
|
|
|
printf("Interrupted\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("Terminated by signal %d\n", WTERMSIG(wstatus));
|
|
|
|
break;
|
|
|
|
}
|
2018-11-01 01:11:00 +01:00
|
|
|
} else {
|
|
|
|
printf("Exited abnormally\n");
|
2019-04-25 14:13:36 +02:00
|
|
|
return 1;
|
2018-11-01 01:11:00 +01:00
|
|
|
}
|
2018-10-27 01:24:22 +02:00
|
|
|
}
|
2019-04-25 14:13:36 +02:00
|
|
|
return 0;
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 04:32:31 +01:00
|
|
|
int main(int argc, char** argv)
|
2018-10-23 10:12:50 +02:00
|
|
|
{
|
2018-10-28 09:36:21 +01:00
|
|
|
g = new GlobalState;
|
2018-11-13 00:17:30 +01:00
|
|
|
g->uid = getuid();
|
2018-11-02 12:56:51 +01:00
|
|
|
g->sid = setsid();
|
2018-11-02 13:14:25 +01:00
|
|
|
tcsetpgrp(0, getpgrp());
|
2019-01-25 15:34:21 +01:00
|
|
|
tcgetattr(0, &g->termios);
|
|
|
|
|
2018-11-07 19:03:44 +01:00
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_handler = handle_sigint;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sa.sa_mask = 0;
|
|
|
|
int rc = sigaction(SIGINT, &sa, nullptr);
|
|
|
|
assert(rc == 0);
|
|
|
|
}
|
|
|
|
|
2018-10-28 09:36:21 +01:00
|
|
|
int rc = gethostname(g->hostname, sizeof(g->hostname));
|
2018-10-28 01:08:01 +02:00
|
|
|
if (rc < 0)
|
|
|
|
perror("gethostname");
|
2018-10-31 01:21:56 +01:00
|
|
|
rc = ttyname_r(0, g->ttyname, sizeof(g->ttyname));
|
|
|
|
if (rc < 0)
|
|
|
|
perror("ttyname_r");
|
2019-01-30 00:49:20 +01:00
|
|
|
|
2018-10-31 21:31:56 +01:00
|
|
|
{
|
|
|
|
auto* pw = getpwuid(getuid());
|
|
|
|
if (pw)
|
|
|
|
g->username = pw->pw_name;
|
|
|
|
endpwent();
|
|
|
|
}
|
2018-10-28 01:08:01 +02:00
|
|
|
|
2019-02-03 04:32:31 +01:00
|
|
|
if (argc > 1 && !strcmp(argv[1], "-c")) {
|
|
|
|
fprintf(stderr, "FIXME: Implement /bin/sh -c\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-23 10:12:50 +02:00
|
|
|
char linebuf[128];
|
|
|
|
int linedx = 0;
|
|
|
|
linebuf[0] = '\0';
|
|
|
|
|
2018-10-30 00:06:31 +01:00
|
|
|
{
|
|
|
|
char cwdbuf[1024];
|
|
|
|
getcwd(cwdbuf, sizeof(cwdbuf));
|
|
|
|
g->cwd = cwdbuf;
|
|
|
|
}
|
2018-10-23 10:12:50 +02:00
|
|
|
prompt();
|
|
|
|
for (;;) {
|
|
|
|
char keybuf[16];
|
2018-10-30 16:17:34 +01:00
|
|
|
ssize_t nread = read(0, keybuf, sizeof(keybuf));
|
2019-02-05 12:27:32 +01:00
|
|
|
if (nread == 0)
|
|
|
|
return 0;
|
2018-10-23 10:12:50 +02:00
|
|
|
if (nread < 0) {
|
2018-11-07 21:19:47 +01:00
|
|
|
if (errno == EINTR) {
|
2019-02-20 23:45:00 +01:00
|
|
|
if (g->was_interrupted) {
|
|
|
|
if (linedx != 0)
|
|
|
|
printf("^C");
|
|
|
|
}
|
2019-01-25 15:49:54 +01:00
|
|
|
g->was_interrupted = false;
|
|
|
|
linebuf[0] = '\0';
|
|
|
|
linedx = 0;
|
|
|
|
putchar('\n');
|
|
|
|
prompt();
|
|
|
|
continue;
|
2018-11-07 21:19:47 +01:00
|
|
|
} else {
|
|
|
|
perror("read failed");
|
|
|
|
return 2;
|
|
|
|
}
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
|
|
|
for (ssize_t i = 0; i < nread; ++i) {
|
2019-01-25 14:23:27 +01:00
|
|
|
char ch = keybuf[i];
|
2019-01-25 15:34:21 +01:00
|
|
|
if (ch == 0)
|
|
|
|
continue;
|
|
|
|
if (ch == 8 || ch == g->termios.c_cc[VERASE]) {
|
2019-01-25 14:23:27 +01:00
|
|
|
if (linedx == 0)
|
|
|
|
continue;
|
|
|
|
linebuf[--linedx] = '\0';
|
2019-01-25 15:34:21 +01:00
|
|
|
putchar(8);
|
|
|
|
fflush(stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == g->termios.c_cc[VKILL]) {
|
|
|
|
if (linedx == 0)
|
|
|
|
continue;
|
|
|
|
for (; linedx; --linedx)
|
|
|
|
putchar(0x8);
|
|
|
|
linebuf[0] = '\0';
|
2019-01-25 14:23:27 +01:00
|
|
|
fflush(stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
putchar(ch);
|
2018-11-08 01:23:23 +01:00
|
|
|
fflush(stdout);
|
2019-01-25 14:23:27 +01:00
|
|
|
if (ch != '\n') {
|
|
|
|
linebuf[linedx++] = ch;
|
2018-10-23 10:12:50 +02:00
|
|
|
linebuf[linedx] = '\0';
|
|
|
|
} else {
|
|
|
|
runcmd(linebuf);
|
|
|
|
linebuf[0] = '\0';
|
|
|
|
linedx = 0;
|
|
|
|
prompt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|