Compare commits
No commits in common. "fbb7de7156bd02e3655a1e89d327805b3334b763" and "e466c51e9f27094d14b30d2cce99f5d0127631e0" have entirely different histories.
fbb7de7156
...
e466c51e9f
@ -14,12 +14,11 @@ static Result<void> do_cat(StringView path)
|
|||||||
else
|
else
|
||||||
f = TRY(File::open(path, File::ReadOnly));
|
f = TRY(File::open(path, File::ReadOnly));
|
||||||
|
|
||||||
auto buf = TRY(Buffer::create_sized(4096));
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
TRY(f->read(buf, 4096));
|
String line = TRY(f->read_line());
|
||||||
if (buf.is_empty()) break;
|
if (line.is_empty()) break;
|
||||||
out->write(buf);
|
out->write(line.view());
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
|
15
apps/sh.cpp
15
apps/sh.cpp
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
using os::File;
|
using os::File;
|
||||||
|
|
||||||
static Result<Vector<char*>> split_command_into_argv(StringView cmd, char** out)
|
static Result<Vector<char*>> split_command_into_argv(StringView cmd)
|
||||||
{
|
{
|
||||||
char* str = strdup(cmd.chars());
|
char* str = strdup(cmd.chars());
|
||||||
|
|
||||||
@ -33,15 +33,13 @@ static Result<Vector<char*>> split_command_into_argv(StringView cmd, char** out)
|
|||||||
if (segment == NULL) return result;
|
if (segment == NULL) return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (out) *out = str;
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[noreturn]] static void execute_command(StringView command)
|
[[noreturn]] static void execute_command(StringView command)
|
||||||
{
|
{
|
||||||
Vector<char*> argv;
|
Vector<char*> argv;
|
||||||
bool ok = split_command_into_argv(command, nullptr).try_move_value_or_error(argv, errno);
|
bool ok = split_command_into_argv(command).try_move_value_or_error(argv, errno);
|
||||||
if (!ok)
|
if (!ok)
|
||||||
{
|
{
|
||||||
perror("failed to parse command");
|
perror("failed to parse command");
|
||||||
@ -75,10 +73,7 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
interactive = true;
|
interactive = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
|
||||||
input_file = TRY(File::open(path, File::ReadOnly));
|
input_file = TRY(File::open(path, File::ReadOnly));
|
||||||
input_file->set_close_on_exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
@ -95,21 +90,17 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
|
|
||||||
if (!strncmp(cmd.chars(), "cd", 2))
|
if (!strncmp(cmd.chars(), "cd", 2))
|
||||||
{
|
{
|
||||||
char* copy = nullptr;
|
auto args = TRY(split_command_into_argv(cmd.view()));
|
||||||
|
|
||||||
auto args = TRY(split_command_into_argv(cmd.view(), ©));
|
|
||||||
check("cd"_sv == args[0]);
|
check("cd"_sv == args[0]);
|
||||||
|
|
||||||
if (args.size() == 2)
|
if (args.size() == 2)
|
||||||
{
|
{
|
||||||
auto home = TRY(os::FileSystem::home_directory());
|
auto home = TRY(os::FileSystem::home_directory());
|
||||||
if (chdir(home.chars()) < 0) perror("cd");
|
if (chdir(home.chars()) < 0) perror("cd");
|
||||||
free(copy);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chdir(args[1]) < 0) perror("cd");
|
if (chdir(args[1]) < 0) perror("cd");
|
||||||
free(copy);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,11 +46,6 @@ class Buffer
|
|||||||
return m_size;
|
return m_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_empty() const
|
|
||||||
{
|
|
||||||
return m_size == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Buffer(u8* data, usize size);
|
Buffer(u8* data, usize size);
|
||||||
|
|
||||||
|
@ -31,12 +31,9 @@ namespace os
|
|||||||
void set_close_on_exec();
|
void set_close_on_exec();
|
||||||
|
|
||||||
Result<void> write(StringView str);
|
Result<void> write(StringView str);
|
||||||
Result<void> write(const Buffer& buf);
|
|
||||||
|
|
||||||
Result<String> read_line();
|
Result<String> read_line();
|
||||||
|
|
||||||
Result<void> read(Buffer& buf, usize size);
|
|
||||||
|
|
||||||
Result<int> getchar();
|
Result<int> getchar();
|
||||||
|
|
||||||
File(Badge<File>);
|
File(Badge<File>);
|
||||||
|
@ -101,13 +101,6 @@ namespace os
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> File::write(const Buffer& buf)
|
|
||||||
{
|
|
||||||
TRY(raw_write(buf.data(), buf.size()));
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
Result<String> File::read_line()
|
Result<String> File::read_line()
|
||||||
{
|
{
|
||||||
Vector<char> data;
|
Vector<char> data;
|
||||||
@ -131,17 +124,6 @@ namespace os
|
|||||||
return String::from_cstring(data.data());
|
return String::from_cstring(data.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> File::read(Buffer& buf, usize size)
|
|
||||||
{
|
|
||||||
u8* slice = TRY(buf.slice(0, size));
|
|
||||||
|
|
||||||
usize nread = TRY(raw_read(slice, size));
|
|
||||||
|
|
||||||
TRY(buf.try_resize(nread));
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
Result<int> File::getchar()
|
Result<int> File::getchar()
|
||||||
{
|
{
|
||||||
char value;
|
char value;
|
||||||
|
Loading…
Reference in New Issue
Block a user