apps: Use os::FileSystem
This commit is contained in:
parent
5df16a9bff
commit
fb3430c56a
@ -1,5 +1,5 @@
|
|||||||
#include <os/ArgumentParser.h>
|
#include <os/ArgumentParser.h>
|
||||||
#include <stdio.h>
|
#include <os/FileSystem.h>
|
||||||
|
|
||||||
Result<int> luna_main(int argc, char** argv)
|
Result<int> luna_main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
@ -9,11 +9,7 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
parser.add_positional_argument(path, "path"_sv, true);
|
parser.add_positional_argument(path, "path"_sv, true);
|
||||||
parser.parse(argc, argv);
|
parser.parse(argc, argv);
|
||||||
|
|
||||||
if (remove(path.chars()) < 0)
|
TRY(os::FileSystem::remove(path));
|
||||||
{
|
|
||||||
perror("rm");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
72
apps/sh.cpp
72
apps/sh.cpp
@ -1,5 +1,8 @@
|
|||||||
|
#include <luna/String.h>
|
||||||
#include <luna/Vector.h>
|
#include <luna/Vector.h>
|
||||||
#include <os/ArgumentParser.h>
|
#include <os/ArgumentParser.h>
|
||||||
|
#include <os/File.h>
|
||||||
|
#include <os/FileSystem.h>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@ -9,9 +12,11 @@
|
|||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static Result<Vector<char*>> split_command_into_argv(const char* cmd)
|
using os::File;
|
||||||
|
|
||||||
|
static Result<Vector<char*>> split_command_into_argv(StringView cmd)
|
||||||
{
|
{
|
||||||
char* str = strdup(cmd);
|
char* str = strdup(cmd.chars());
|
||||||
|
|
||||||
Vector<char*> result;
|
Vector<char*> result;
|
||||||
|
|
||||||
@ -31,7 +36,7 @@ static Result<Vector<char*>> split_command_into_argv(const char* cmd)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[noreturn]] static void execute_command(const char* command)
|
[[noreturn]] static void execute_command(StringView command)
|
||||||
{
|
{
|
||||||
Vector<char*> argv;
|
Vector<char*> argv;
|
||||||
bool ok = split_command_into_argv(command).try_move_value_or_error(argv, errno);
|
bool ok = split_command_into_argv(command).try_move_value_or_error(argv, errno);
|
||||||
@ -49,48 +54,55 @@ static Result<Vector<char*>> split_command_into_argv(const char* cmd)
|
|||||||
|
|
||||||
Result<int> luna_main(int argc, char** argv)
|
Result<int> luna_main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
StringView file;
|
StringView path;
|
||||||
StringView command;
|
StringView command;
|
||||||
|
bool interactive { false };
|
||||||
|
|
||||||
FILE* f;
|
SharedPtr<File> input_file;
|
||||||
|
|
||||||
os::ArgumentParser parser;
|
os::ArgumentParser parser;
|
||||||
parser.add_positional_argument(file, "file"_sv, "-"_sv);
|
parser.add_positional_argument(path, "path"_sv, "-"_sv);
|
||||||
parser.add_value_argument(command, 'c', "command"_sv, true);
|
parser.add_value_argument(command, 'c', "command"_sv, true);
|
||||||
parser.parse(argc, argv);
|
parser.parse(argc, argv);
|
||||||
|
|
||||||
if (!command.is_empty()) { execute_command(command.chars()); }
|
if (!command.is_empty()) execute_command(command);
|
||||||
|
|
||||||
if (file == "-") f = stdin;
|
if (path == "-")
|
||||||
else
|
|
||||||
{
|
{
|
||||||
f = fopen(file.chars(), "r");
|
input_file = File::standard_input();
|
||||||
if (!f)
|
interactive = true;
|
||||||
{
|
|
||||||
perror("fopen");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
input_file = TRY(File::open(path, File::ReadOnly));
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
if (file == "-")
|
if (interactive)
|
||||||
{
|
{
|
||||||
char* cwd = getcwd(NULL, 0);
|
auto cwd = TRY(os::FileSystem::working_directory());
|
||||||
if (!cwd)
|
printf("sh %s%c ", cwd.chars(), getuid() == 0 ? '#' : '$');
|
||||||
{
|
|
||||||
perror("getcwd");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printf("sh %s%c ", cwd, getuid() == 0 ? '#' : '$');
|
|
||||||
free(cwd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char cmd[4096];
|
auto cmd = TRY(input_file->read_line());
|
||||||
char* rc = fgets(cmd, sizeof(cmd), f);
|
if (cmd.is_empty()) break;
|
||||||
if (!rc) return 0;
|
|
||||||
|
|
||||||
if (strspn(cmd, " \n") == strlen(cmd)) continue;
|
if (strspn(cmd.chars(), " \n") == cmd.length()) continue;
|
||||||
|
|
||||||
|
if (!strncmp(cmd.chars(), "cd", 2))
|
||||||
|
{
|
||||||
|
auto args = TRY(split_command_into_argv(cmd.view()));
|
||||||
|
check("cd"_sv == args[0]);
|
||||||
|
|
||||||
|
if (args.size() == 2)
|
||||||
|
{
|
||||||
|
auto home = TRY(os::FileSystem::home_directory());
|
||||||
|
if (chdir(home.chars()) < 0) perror("cd");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chdir(args[1]) < 0) perror("cd");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
pid_t child = fork();
|
pid_t child = fork();
|
||||||
if (child < 0)
|
if (child < 0)
|
||||||
@ -99,7 +111,7 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (child == 0) { execute_command(cmd); }
|
if (child == 0) { execute_command(cmd.view()); }
|
||||||
|
|
||||||
if (waitpid(child, NULL, 0) < 0)
|
if (waitpid(child, NULL, 0) < 0)
|
||||||
{
|
{
|
||||||
@ -107,4 +119,6 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user