libos+apps: Add some missing functionality to File and eliminate any trace of C from cat and edit
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-04-13 17:31:21 +02:00
parent 26ff964ec1
commit 60c6e764a4
Signed by: apio
GPG Key ID: B8A7D06E42258954
6 changed files with 156 additions and 33 deletions

View File

@ -1,34 +1,27 @@
#include <luna/String.h>
#include <os/ArgumentParser.h> #include <os/ArgumentParser.h>
#include <os/File.h>
#include <stdio.h> using os::File;
#include <stdlib.h>
#include <string.h>
static void do_cat(StringView path) static Result<void> do_cat(StringView path)
{ {
FILE* f; SharedPtr<File> f;
if (path == "-") f = stdin; auto out = File::standard_output();
if (path == "-") f = File::standard_input();
else else
{ f = TRY(File::open(path, File::ReadOnly));
f = fopen(path.chars(), "r");
if (!f)
{
perror(path.chars());
exit(1);
}
}
char buffer[4096];
while (1) while (1)
{ {
size_t nread = fread(buffer, 1, sizeof(buffer), f); String line = TRY(f->read_line());
if (nread == 0) return; if (line.is_empty()) break;
fwrite(buffer, 1, nread, stdout); out->write(line.view());
} }
if (f != stdin) fclose(f); return {};
} }
Result<int> luna_main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
@ -37,11 +30,11 @@ Result<int> luna_main(int argc, char** argv)
os::ArgumentParser parser; os::ArgumentParser parser;
parser.add_positional_argument(filename, "file"_sv, "-"_sv); parser.add_positional_argument(filename, "file"_sv, "-"_sv);
Vector<StringView> extra_files = parser.parse(argc, argv).value(); Vector<StringView> extra_files = TRY(parser.parse(argc, argv));
do_cat(filename); TRY(do_cat(filename));
for (auto file : extra_files) { do_cat(file); } for (auto file : extra_files) TRY(do_cat(file));
return 0; return 0;
} }

View File

@ -1,9 +1,8 @@
#include <luna/String.h>
#include <os/ArgumentParser.h> #include <os/ArgumentParser.h>
#include <os/File.h> #include <os/File.h>
#include <stdio.h> using os::File;
#include <stdlib.h>
#include <string.h>
Result<int> luna_main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
@ -13,15 +12,15 @@ Result<int> luna_main(int argc, char** argv)
parser.add_positional_argument(pathname, "path"_sv, true); parser.add_positional_argument(pathname, "path"_sv, true);
parser.parse(argc, argv); parser.parse(argc, argv);
auto file = TRY(os::File::open_or_create(pathname, os::File::WriteOnly)); auto file = TRY(File::open_or_create(pathname, File::WriteOnly));
char buffer[4096]; auto input = File::standard_input();
while (1) while (1)
{ {
char* rc = fgets(buffer, sizeof(buffer), stdin); String line = TRY(input->read_line());
if (rc == 0) break; if (line.is_empty()) break;
TRY(file->write(StringView { buffer })); TRY(file->write(line.view()));
} }
return 0; return 0;

View File

@ -1,6 +1,54 @@
typedef void* (*cxa_atexit_func_t)(void*);
struct cxa_atexit_entry
{
cxa_atexit_func_t function;
void* argument;
void* dso_handle;
};
#define CXA_ATEXIT_MAX 64
int cxa_atexit_entry_count = 0;
cxa_atexit_entry cxa_atexit_entries[CXA_ATEXIT_MAX];
extern "C" extern "C"
{ {
void __gxx_personality_v0() void __gxx_personality_v0()
{ {
} }
int __cxa_atexit(cxa_atexit_func_t func, void* arg, void* dso)
{
if (cxa_atexit_entry_count >= CXA_ATEXIT_MAX) return -1;
cxa_atexit_entries[cxa_atexit_entry_count].function = func;
cxa_atexit_entries[cxa_atexit_entry_count].argument = arg;
cxa_atexit_entries[cxa_atexit_entry_count].dso_handle = dso;
cxa_atexit_entry_count++;
return 0;
}
void __cxa_finalize(void* f)
{
int i = cxa_atexit_entry_count;
if (!f)
{
while (i--)
{
if (cxa_atexit_entries[i].function) { cxa_atexit_entries[i].function(cxa_atexit_entries[i].argument); }
}
}
else
{
while (i--)
{
if (cxa_atexit_entries[i].function == (cxa_atexit_func_t)f)
{
cxa_atexit_entries[i].function(cxa_atexit_entries[i].argument);
cxa_atexit_entries[i].function = 0;
}
}
}
}
} }

View File

@ -29,7 +29,7 @@ Result<void> StringBuilder::add(unsigned long value)
Result<void> StringBuilder::add(char value) Result<void> StringBuilder::add(char value)
{ {
return format("%c"_sv, value); return m_data.append_data((const u8*)&value, 1);
} }
Result<void> StringBuilder::format(StringView fmt, ...) Result<void> StringBuilder::format(StringView fmt, ...)

View File

@ -24,15 +24,24 @@ namespace os
static Result<SharedPtr<File>> open_or_create(StringView path, OpenMode flags, mode_t mode = 0644); static Result<SharedPtr<File>> open_or_create(StringView path, OpenMode flags, mode_t mode = 0644);
static Result<SharedPtr<File>> create(StringView path, OpenMode flags, mode_t mode = 0644); static Result<SharedPtr<File>> create(StringView path, OpenMode flags, mode_t mode = 0644);
static SharedPtr<File> standard_input();
static SharedPtr<File> standard_output();
static SharedPtr<File> standard_error();
void set_close_on_exec(); void set_close_on_exec();
Result<void> write(StringView str); Result<void> write(StringView str);
Result<String> read_line();
Result<int> getchar();
File(Badge<File>); File(Badge<File>);
~File(); ~File();
private: private:
static Result<SharedPtr<File>> construct(StringView path, int flags, mode_t mode); static Result<SharedPtr<File>> construct(StringView path, int flags, mode_t mode);
static void initialize_standard_streams();
Result<usize> raw_read(u8* buf, usize length); Result<usize> raw_read(u8* buf, usize length);
Result<usize> raw_write(const u8* buf, usize length); Result<usize> raw_write(const u8* buf, usize length);

View File

@ -1,8 +1,13 @@
#include <fcntl.h> #include <fcntl.h>
#include <luna/StringBuilder.h>
#include <os/File.h> #include <os/File.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
static SharedPtr<os::File> g_stdin = {};
static SharedPtr<os::File> g_stdout = {};
static SharedPtr<os::File> g_stderr = {};
namespace os namespace os
{ {
File::File(Badge<File>) File::File(Badge<File>)
@ -14,9 +19,45 @@ namespace os
if (m_fd >= 0) close(m_fd); if (m_fd >= 0) close(m_fd);
} }
void File::initialize_standard_streams()
{
g_stdin = adopt_shared_if_nonnull(new (std::nothrow) File({}))
.expect_release_value("Cannot open standard input stream");
g_stdin->m_fd = STDIN_FILENO;
g_stdout = adopt_shared_if_nonnull(new (std::nothrow) File({}))
.expect_release_value("Cannot open standard output stream");
g_stdout->m_fd = STDOUT_FILENO;
g_stderr = adopt_shared_if_nonnull(new (std::nothrow) File({}))
.expect_release_value("Cannot open standard error stream");
g_stderr->m_fd = STDERR_FILENO;
}
SharedPtr<File> File::standard_input()
{
if (!g_stdin) initialize_standard_streams();
return g_stdin;
}
SharedPtr<File> File::standard_output()
{
if (!g_stdout) initialize_standard_streams();
return g_stdout;
}
SharedPtr<File> File::standard_error()
{
if (!g_stderr) initialize_standard_streams();
return g_stderr;
}
Result<SharedPtr<File>> File::construct(StringView path, int flags, mode_t mode) Result<SharedPtr<File>> File::construct(StringView path, int flags, mode_t mode)
{ {
auto file = TRY(adopt_shared(new (std::nothrow) File({}))); auto file = TRY(adopt_shared_if_nonnull(new (std::nothrow) File({})));
long rc = syscall(SYS_open, path.chars(), flags, mode); long rc = syscall(SYS_open, path.chars(), flags, mode);
int fd = TRY(Result<int>::from_syscall(rc)); int fd = TRY(Result<int>::from_syscall(rc));
@ -60,6 +101,39 @@ namespace os
return {}; return {};
} }
Result<String> File::read_line()
{
Vector<char> data;
int current;
while (true)
{
current = TRY(getchar());
if (current == -1) break;
TRY(data.try_append((char)current));
if (current == '\n') break;
}
if (!data.size()) return String {};
TRY(data.try_append('\0'));
return String::from_cstring(data.data());
}
Result<int> File::getchar()
{
char value;
usize nread = TRY(raw_read((u8*)&value, 1));
if (!nread) return -1;
return value;
}
void File::set_close_on_exec() void File::set_close_on_exec()
{ {
fcntl(m_fd, F_SETFD, FD_CLOEXEC); fcntl(m_fd, F_SETFD, FD_CLOEXEC);