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-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;
|
|
|
|
char hostname[32];
|
|
|
|
};
|
|
|
|
static GlobalState* g;
|
2018-10-23 10:12:50 +02:00
|
|
|
|
|
|
|
static void prompt()
|
|
|
|
{
|
|
|
|
if (getuid() == 0)
|
|
|
|
printf("# ");
|
|
|
|
else
|
2018-10-29 21:54:11 +01:00
|
|
|
printf("(%u) \033[32;1m%s\033[0m:\033[34;1m%s\033[0m$> ", getpid(), 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-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-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-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);
|
|
|
|
|
|
|
|
if (WIFEXITED(wstatus)) {
|
|
|
|
//printf("Exited normally with status %d\n", WEXITSTATUS(wstatus));
|
|
|
|
} else {
|
|
|
|
printf("Exited abnormally\n");
|
|
|
|
}
|
2018-10-26 14:24:11 +02:00
|
|
|
return retval;
|
2018-10-23 10:12:50 +02: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;
|
|
|
|
int rc = gethostname(g->hostname, sizeof(g->hostname));
|
2018-10-28 01:08:01 +02:00
|
|
|
if (rc < 0)
|
|
|
|
perror("gethostname");
|
|
|
|
|
2018-10-23 10:12:50 +02:00
|
|
|
char linebuf[128];
|
|
|
|
int linedx = 0;
|
|
|
|
linebuf[0] = '\0';
|
|
|
|
|
2018-10-28 14:11:51 +01:00
|
|
|
int fd = open("/dev/keyboard", O_RDONLY);
|
2018-10-23 10:12:50 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
printf("failed to open /dev/keyboard :(\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-10-28 09:36:21 +01:00
|
|
|
g->cwd = "/";
|
2018-10-23 10:12:50 +02:00
|
|
|
prompt();
|
|
|
|
for (;;) {
|
|
|
|
char keybuf[16];
|
|
|
|
ssize_t nread = read(fd, keybuf, sizeof(keybuf));
|
|
|
|
if (nread < 0) {
|
|
|
|
printf("failed to read :(\n");
|
|
|
|
return 2;
|
|
|
|
}
|
2018-10-23 13:57:17 +02:00
|
|
|
if (nread > 2)
|
|
|
|
printf("read %u bytes\n", nread);
|
|
|
|
if (nread > (ssize_t)sizeof(keybuf)) {
|
|
|
|
printf("read() overran the buffer i gave it!\n");
|
|
|
|
return 3;
|
|
|
|
}
|
2018-10-23 10:12:50 +02:00
|
|
|
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;
|
|
|
|
}
|