2018-10-23 10:12:50 +02:00
|
|
|
#include <LibC/stdio.h>
|
|
|
|
#include <LibC/unistd.h>
|
|
|
|
#include <LibC/process.h>
|
2018-10-25 12:06:00 +02:00
|
|
|
#include <LibC/errno.h>
|
|
|
|
#include <LibC/string.h>
|
2018-10-26 14:24:11 +02:00
|
|
|
#include <LibC/stdlib.h>
|
2018-10-30 22:03:02 +01:00
|
|
|
#include <LibC/utsname.h>
|
2018-10-31 21:31:56 +01:00
|
|
|
#include <LibC/pwd.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 21:45:36 +01:00
|
|
|
const char* ttyname_short { nullptr };
|
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-10-28 09:36:21 +01:00
|
|
|
};
|
|
|
|
static GlobalState* g;
|
2018-10-23 10:12:50 +02:00
|
|
|
|
|
|
|
static void prompt()
|
|
|
|
{
|
|
|
|
if (getuid() == 0)
|
|
|
|
printf("# ");
|
|
|
|
else
|
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());
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 14:24:11 +02:00
|
|
|
static int sh_pwd(int, const char**)
|
|
|
|
{
|
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-02 20:41:58 +01:00
|
|
|
static int sh_fork(int, const char**)
|
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
printf("getpid()=%d, fork()=%d\n", getpid(), pid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-03 01:49:40 +01:00
|
|
|
static int sh_fe(int, const char**)
|
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (!pid) {
|
|
|
|
int rc = execve("/bin/ps", nullptr, nullptr);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("execve");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sh_fef(int, const char**)
|
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (!pid) {
|
|
|
|
int rc = execve("/bin/psx", nullptr, nullptr);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("execve");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-28 10:26:07 +01:00
|
|
|
static int sh_exit(int, const char**)
|
|
|
|
{
|
|
|
|
printf("Good-bye!\n");
|
|
|
|
exit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-26 14:24:11 +02:00
|
|
|
static int sh_cd(int argc, const char** argv)
|
|
|
|
{
|
|
|
|
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]);
|
|
|
|
|
|
|
|
FileSystemPath canonicalPath(pathbuf);
|
|
|
|
if (!canonicalPath.isValid()) {
|
|
|
|
printf("FileSystemPath failed to canonicalize '%s'\n", pathbuf);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const char* path = canonicalPath.string().characters();
|
|
|
|
|
2018-10-26 14:24:11 +02:00
|
|
|
struct stat st;
|
2018-10-28 09:36:21 +01:00
|
|
|
int rc = lstat(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;
|
|
|
|
}
|
2018-10-28 09:36:21 +01:00
|
|
|
g->cwd = canonicalPath.string();
|
2018-10-26 14:24:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool handle_builtin(int argc, const char** argv, int& retval)
|
|
|
|
{
|
|
|
|
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-11-03 01:49:40 +01:00
|
|
|
if (!strcmp(argv[0], "fe")) {
|
|
|
|
retval = sh_fe(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[0], "fef")) {
|
|
|
|
retval = sh_fef(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-02 20:41:58 +01:00
|
|
|
if (!strcmp(argv[0], "fork")) {
|
|
|
|
retval = sh_fork(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-26 14:24:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-29 21:54:11 +01:00
|
|
|
static int try_spawn(const char* path, const char** argv)
|
|
|
|
{
|
|
|
|
int ret = spawn(path, argv);
|
|
|
|
if (ret >= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
const char* search_path = "/bin";
|
|
|
|
char pathbuf[128];
|
|
|
|
sprintf(pathbuf, "%s/%s", search_path, argv[0]);
|
|
|
|
ret = spawn(pathbuf, argv);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
const char* argv[32];
|
2018-10-26 15:04:20 +02:00
|
|
|
size_t argc = 1;
|
2018-10-26 11:16:56 +02:00
|
|
|
argv[0] = &buf[0];
|
|
|
|
size_t buflen = strlen(buf);
|
|
|
|
for (size_t i = 0; i < buflen; ++i) {
|
|
|
|
if (buf[i] == ' ') {
|
|
|
|
buf[i] = '\0';
|
2018-10-26 15:04:20 +02:00
|
|
|
argv[argc++] = &buf[i + 1];
|
2018-10-26 11:16:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-26 15:04:20 +02:00
|
|
|
argv[argc] = nullptr;
|
2018-10-26 14:24:11 +02:00
|
|
|
|
|
|
|
int retval = 0;
|
2018-10-26 15:04:20 +02:00
|
|
|
if (handle_builtin(argc, argv, retval)) {
|
2018-10-26 14:24:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-29 21:54:11 +01:00
|
|
|
int ret = try_spawn(argv[0], argv);
|
|
|
|
if (ret < 0) {
|
2018-10-25 12:06:00 +02:00
|
|
|
printf("spawn failed: %s (%s)\n", cmd, strerror(errno));
|
2018-10-24 00:20:34 +02:00
|
|
|
return 1;
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
2018-10-29 21:54:11 +01:00
|
|
|
|
2018-11-02 13:14:25 +01:00
|
|
|
// FIXME: This is racy, since spawn has already started the new process.
|
|
|
|
// Once I have fork()+exec(), pgrp setup can be done in the child before exec()ing.
|
|
|
|
tcsetpgrp(0, ret);
|
|
|
|
|
2018-10-26 14:24:11 +02:00
|
|
|
// FIXME: waitpid should give us the spawned process's exit status
|
2018-10-27 01:24:22 +02:00
|
|
|
int wstatus = 0;
|
|
|
|
waitpid(ret, &wstatus, 0);
|
|
|
|
|
2018-11-02 13:14:25 +01:00
|
|
|
// FIXME: Racy as mentioned above. Rework once we have fork()+exec().
|
|
|
|
tcsetpgrp(0, getpid());
|
|
|
|
|
2018-10-27 01:24:22 +02:00
|
|
|
if (WIFEXITED(wstatus)) {
|
|
|
|
//printf("Exited normally with status %d\n", WEXITSTATUS(wstatus));
|
|
|
|
} 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");
|
|
|
|
}
|
2018-10-27 01:24:22 +02:00
|
|
|
}
|
2018-10-26 14:24:11 +02:00
|
|
|
return retval;
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 22:03:02 +01:00
|
|
|
static void greeting()
|
|
|
|
{
|
|
|
|
utsname uts;
|
|
|
|
int rc = uname(&uts);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("uname");
|
|
|
|
return;
|
|
|
|
}
|
2018-10-31 21:45:36 +01:00
|
|
|
printf("\n%s/%s on %s\n\n", uts.sysname, uts.machine, g->ttyname_short);
|
2018-10-30 22:03:02 +01:00
|
|
|
}
|
|
|
|
|
2018-10-26 14:24:11 +02:00
|
|
|
int main(int, char**)
|
2018-10-23 10:12:50 +02:00
|
|
|
{
|
2018-10-28 09:36:21 +01:00
|
|
|
g = new GlobalState;
|
2018-11-02 12:56:51 +01:00
|
|
|
g->sid = setsid();
|
2018-11-02 13:14:25 +01:00
|
|
|
tcsetpgrp(0, getpgrp());
|
|
|
|
|
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");
|
2018-10-31 21:45:36 +01:00
|
|
|
else
|
|
|
|
g->ttyname_short = strrchr(g->ttyname, '/') + 1;
|
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
|
|
|
|
2018-10-30 22:03:02 +01:00
|
|
|
greeting();
|
|
|
|
|
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));
|
2018-10-23 10:12:50 +02:00
|
|
|
if (nread < 0) {
|
|
|
|
printf("failed to read :(\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
for (ssize_t i = 0; i < nread; ++i) {
|
|
|
|
putchar(keybuf[i]);
|
|
|
|
if (keybuf[i] != '\n') {
|
|
|
|
linebuf[linedx++] = keybuf[i];
|
|
|
|
linebuf[linedx] = '\0';
|
|
|
|
} else {
|
|
|
|
runcmd(linebuf);
|
|
|
|
linebuf[0] = '\0';
|
|
|
|
linedx = 0;
|
|
|
|
prompt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|