|
|
|
|
@ -3,15 +3,15 @@ |
|
|
|
|
#include "src/lib/util.h" |
|
|
|
|
#include "src/user/syscall.h" |
|
|
|
|
|
|
|
|
|
static void help(uint8_t argc, char **argv); |
|
|
|
|
static void cd(uint8_t argc, char **argv); |
|
|
|
|
static void help(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); |
|
|
|
|
static void cd(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); |
|
|
|
|
|
|
|
|
|
#define PATH "/bin/" |
|
|
|
|
|
|
|
|
|
#define PRINT_S(s) write(1, s, sizeof(s) - 1); |
|
|
|
|
#define PRINT_D(s) write(1, s, string_length(s)); |
|
|
|
|
#define PRINT_S(o, s) write(o, s, sizeof(s) - 1); |
|
|
|
|
#define PRINT_D(o, s) write(o, s, string_length(s)); |
|
|
|
|
|
|
|
|
|
typedef void (*app_t)(uint8_t argc, char **argv); |
|
|
|
|
typedef void (*app_t)(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); |
|
|
|
|
|
|
|
|
|
typedef struct { |
|
|
|
|
const char *name; |
|
|
|
|
@ -23,27 +23,27 @@ static app_entry_t builtins[] = { |
|
|
|
|
{"cd", cd}, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static void help(uint8_t argc, __attribute__((unused)) char **argv) { |
|
|
|
|
static void help(uint8_t argc, __attribute__((unused)) char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) { |
|
|
|
|
if (argc > 1) { |
|
|
|
|
PRINT_S("help: expects no arguments"); |
|
|
|
|
PRINT_S(stdout, "help: expects no arguments\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
PRINT_S("Available commands:\n"); |
|
|
|
|
PRINT_S(stdout, "Available commands:\n"); |
|
|
|
|
for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) { |
|
|
|
|
PRINT_D(builtins[i].name); |
|
|
|
|
PRINT_S("\n"); |
|
|
|
|
PRINT_D(stdout, builtins[i].name); |
|
|
|
|
PRINT_S(stdout, "\n"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void cd(uint8_t argc, char **argv) { |
|
|
|
|
static void cd(uint8_t argc, char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) { |
|
|
|
|
if (argc != 2) { |
|
|
|
|
PRINT_S("cd: requires a single path\n"); |
|
|
|
|
PRINT_S(stdout, "cd: requires a single path\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (chdir(argv[1]) == (uint64_t)-1) { |
|
|
|
|
PRINT_S("cd: path does not exist\n"); |
|
|
|
|
PRINT_S(stdout, "cd: path does not exist\n"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -53,16 +53,23 @@ static void print_prompt() { |
|
|
|
|
char *components[16]; |
|
|
|
|
uint8_t cl = string_split(path, '/', 16, components); |
|
|
|
|
|
|
|
|
|
PRINT_S("[") |
|
|
|
|
PRINT_D(pl == 1 ? "/" : components[cl - 1]); |
|
|
|
|
PRINT_S("]$ "); |
|
|
|
|
PRINT_S(1, "[") |
|
|
|
|
PRINT_D(1, pl == 1 ? "/" : components[cl - 1]); |
|
|
|
|
PRINT_S(1, "]$ "); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void execute(char *command) { |
|
|
|
|
char *argv[16]; |
|
|
|
|
uint8_t argc = (uint8_t)string_split(command, ' ', 16, argv); |
|
|
|
|
static void free_fds(uint64_t *fds, uint8_t fds_count) { |
|
|
|
|
for (uint8_t i = 0; i < fds_count; i++) { |
|
|
|
|
close(fds[i]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
PRINT_S("\n"); |
|
|
|
|
static void kill_pids(__attribute__((unused)) uint64_t *pids, __attribute__((unused)) uint8_t pid_count) { |
|
|
|
|
// TODO.
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void execute(char *command) { |
|
|
|
|
PRINT_S(1, "\n"); |
|
|
|
|
|
|
|
|
|
if (string_equal(command, "^C")) { |
|
|
|
|
print_prompt(); |
|
|
|
|
@ -71,34 +78,85 @@ static void execute(char *command) { |
|
|
|
|
|
|
|
|
|
if (string_equal(command, "^D")) { |
|
|
|
|
exit(EXIT_CODE_OK); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
char *subcommands[16]; |
|
|
|
|
uint8_t subcommands_count = (uint8_t)string_split(command, '|', 16, subcommands); |
|
|
|
|
|
|
|
|
|
uint64_t fds[32]; |
|
|
|
|
uint8_t fds_count = 0; |
|
|
|
|
|
|
|
|
|
uint64_t pids[16]; |
|
|
|
|
uint8_t pids_count = 0; |
|
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < subcommands_count; i++) { |
|
|
|
|
string_trim(subcommands[i], ' ', &subcommands[i]); |
|
|
|
|
|
|
|
|
|
char *argv[16]; |
|
|
|
|
uint8_t argc = (uint8_t)string_split(subcommands[i], ' ', 16, argv); |
|
|
|
|
|
|
|
|
|
uint64_t stdin = 0; |
|
|
|
|
uint64_t stdout = 1; |
|
|
|
|
|
|
|
|
|
if (i > 0) { |
|
|
|
|
stdin = fds[fds_count - 1]; |
|
|
|
|
} |
|
|
|
|
if (i < subcommands_count - 1) { |
|
|
|
|
if (pipe(&fds[fds_count], &fds[fds_count + 1]) == (uint64_t)-1) { |
|
|
|
|
kill_pids(pids, pids_count); |
|
|
|
|
free_fds(fds, fds_count); |
|
|
|
|
print_prompt(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
fds_count += 2; |
|
|
|
|
stdout = fds[fds_count - 2]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uint8_t i; |
|
|
|
|
for (i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) { |
|
|
|
|
if (string_equal(argv[0], builtins[i].name)) { |
|
|
|
|
builtins[i].app(argc, argv); |
|
|
|
|
uint8_t j; |
|
|
|
|
for (j = 0; j < sizeof(builtins) / sizeof(app_entry_t); j++) { |
|
|
|
|
if (string_equal(argv[0], builtins[j].name)) { |
|
|
|
|
builtins[j].app(argc, argv, stdin, stdout); |
|
|
|
|
pids[pids_count++] = 0; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (i == sizeof(builtins) / sizeof(app_entry_t)) { |
|
|
|
|
if (j == sizeof(builtins) / sizeof(app_entry_t)) { |
|
|
|
|
uint64_t size = sizeof(PATH) + string_length(argv[0]) + 1; |
|
|
|
|
char *path = memory_allocate(size); |
|
|
|
|
memory_copy(PATH, sizeof(PATH), path); |
|
|
|
|
memory_copy(argv[0], size - sizeof(PATH), path + sizeof(PATH) - 1); |
|
|
|
|
uint64_t pid = spawn(path, argc, (const char **)argv); |
|
|
|
|
uint64_t pid = spawn(path, argc, (const char **)argv, stdin, stdout); |
|
|
|
|
memory_free(path); |
|
|
|
|
|
|
|
|
|
if (pid == EXIT_CODE_NOT_FOUND) { |
|
|
|
|
PRINT_S("shell: program not found\n"); |
|
|
|
|
if (pid == (uint64_t)-1) { |
|
|
|
|
kill_pids(pids, pids_count); |
|
|
|
|
free_fds(fds, fds_count); |
|
|
|
|
print_prompt(); |
|
|
|
|
return; |
|
|
|
|
} else { |
|
|
|
|
waitpid(pid); |
|
|
|
|
pids[pids_count++] = pid; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < pids_count; i++) { |
|
|
|
|
if (pids[i]) { |
|
|
|
|
waitpid(pids[i]); |
|
|
|
|
} |
|
|
|
|
if (i > 0) { |
|
|
|
|
close(fds[(i - 1) * 2 + 1]); |
|
|
|
|
} |
|
|
|
|
if (i < pids_count - 1) { |
|
|
|
|
close(fds[i * 2]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
print_prompt(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uint64_t main() { |
|
|
|
|
PRINT_D("Welcome to FreywarOS v" VERSION "!\n\n"); |
|
|
|
|
PRINT_D(1, "Welcome to FreywarOS v" VERSION "!\n\n"); |
|
|
|
|
|
|
|
|
|
print_prompt(); |
|
|
|
|
|
|
|
|
|
|