Compare commits

..

No commits in common. "7d69ac56e256400ab598f6be10e2a0a74619b739" and "59713279a0b08dcef801819c29f2883cd24060c6" have entirely different histories.

9 changed files with 55 additions and 96 deletions

View File

@ -95,7 +95,7 @@ Result<int> luna_main(int argc, char** argv)
if (fds[i + 1].revents & POLLIN) clients[i]->check_for_messages(); if (fds[i + 1].revents & POLLIN) clients[i]->check_for_messages();
if (fds[i + 1].revents & POLLHUP) if (fds[i + 1].revents & POLLHUP)
{ {
os::println("launch: Client %zu disconnected", i); os::println("launch: Client %d disconnected", i);
fds.remove_at(i + 1); fds.remove_at(i + 1);
auto client = clients.remove_at(i); auto client = clients.remove_at(i);
client->disconnect(); client->disconnect();

View File

@ -212,15 +212,14 @@ Result<int> luna_main(int argc, char** argv)
{ {
if (colors) if (colors)
{ {
os::println("%s %lu %4s %4s %10lu %s%s" RESET_COLORS "%s" SYMLINK_COLOR "%s" RESET_COLORS, os::println("%s %u %4s %4s %10lu %s%s" RESET_COLORS "%s" SYMLINK_COLOR "%s" RESET_COLORS,
formatted_mode, st.st_nlink, owner.chars(), group.chars(), st.st_size, formatted_mode, st.st_nlink, owner.chars(), group.chars(), st.st_size,
file_type_color(file), file.name.chars(), link.is_empty() ? "" : " -> ", link.chars()); file_type_color(file), file.name.chars(), link.is_empty() ? "" : " -> ", link.chars());
} }
else else
{ {
os::println("%s %lu %4s %4s %10lu %s%s%s", formatted_mode, st.st_nlink, owner.chars(), os::println("%s %u %4s %4s %10lu %s%s%s", formatted_mode, st.st_nlink, owner.chars(), group.chars(),
group.chars(), st.st_size, file.name.chars(), link.is_empty() ? "" : " -> ", st.st_size, file.name.chars(), link.is_empty() ? "" : " -> ", link.chars());
link.chars());
} }
} }
else else
@ -228,13 +227,13 @@ Result<int> luna_main(int argc, char** argv)
auto size = TRY(to_dynamic_unit(st.st_size, 10, false, si ? Unit::SI : Unit::Binary, false)); auto size = TRY(to_dynamic_unit(st.st_size, 10, false, si ? Unit::SI : Unit::Binary, false));
if (colors) if (colors)
{ {
os::println("%s %lu %4s %4s %6s %s%s" RESET_COLORS "%s" SYMLINK_COLOR "%s" RESET_COLORS, os::println("%s %u %4s %4s %6s %s%s" RESET_COLORS "%s" SYMLINK_COLOR "%s" RESET_COLORS,
formatted_mode, st.st_nlink, owner.chars(), group.chars(), size.chars(), formatted_mode, st.st_nlink, owner.chars(), group.chars(), size.chars(),
file_type_color(file), file.name.chars(), link.is_empty() ? "" : " -> ", link.chars()); file_type_color(file), file.name.chars(), link.is_empty() ? "" : " -> ", link.chars());
} }
else else
{ {
os::println("%s %lu %4s %4s %6s %s%s%s", formatted_mode, st.st_nlink, owner.chars(), group.chars(), os::println("%s %u %4s %4s %6s %s%s%s", formatted_mode, st.st_nlink, owner.chars(), group.chars(),
size.chars(), file.name.chars(), link.is_empty() ? "" : " -> ", link.chars()); size.chars(), file.name.chars(), link.is_empty() ? "" : " -> ", link.chars());
} }
} }

View File

@ -34,7 +34,7 @@ Result<int> luna_main(int argc, char** argv)
close(fd); close(fd);
} }
os::println("%s", str.chars()); os::println("%s"_sv, str.chars());
return 0; return 0;
} }

View File

@ -33,7 +33,7 @@ Result<int> luna_main(int argc, char** argv)
auto cmdline = TRY(String::join(command, " ")); auto cmdline = TRY(String::join(command, " "));
os::println("%s %ld.%.2lds user %ld.%.2lds system", cmdline.chars(), usage.ru_utime.tv_sec, os::println("%s %d.%.2ds user %d.%.2ds system"_sv, cmdline.chars(), usage.ru_utime.tv_sec,
usage.ru_utime.tv_usec / 10000, usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 10000); usage.ru_utime.tv_usec / 10000, usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 10000);
return 0; return 0;

View File

@ -9,7 +9,6 @@
#pragma once #pragma once
#include <fcntl.h> #include <fcntl.h>
#include <luna/Attributes.h>
#include <luna/Buffer.h> #include <luna/Buffer.h>
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/SharedPtr.h> #include <luna/SharedPtr.h>
@ -114,7 +113,7 @@ namespace os
/** /**
* @brief Write a string to this File. * @brief Write a string to this File.
* *
* @param str The string to write (doesn't have to be null-terminated). * @param str The string to write (can be not null-terminated).
* @return Result<void> Whether the operation succeeded. * @return Result<void> Whether the operation succeeded.
*/ */
Result<void> write(StringView str); Result<void> write(StringView str);
@ -127,24 +126,6 @@ namespace os
*/ */
Result<void> write(const Buffer& buf); Result<void> write(const Buffer& buf);
/**
* @brief Write a formatted string to this File.
*
* @param format The format string.
* @param ... The format arguments.
* @return Result<void> Whether the operation succeeded.
*/
Result<usize> write_formatted(const char* format, ...) _format(2, 3);
/**
* @brief Write a formatted string to this File.
*
* @param format The format string.
* @param args The format arguments.
* @return Result<void> Whether the operation succeeded.
*/
Result<usize> write_vformatted(const char* format, va_list args);
/** /**
* @brief Read a line from this File. * @brief Read a line from this File.
* *
@ -266,36 +247,36 @@ namespace os
/** /**
* @brief Print a formatted string to standard output. * @brief Print a formatted string to standard output.
* *
* @param format The format string (in the same format as printf(3)). * @param fmt The format string (in the same format as printf(3)).
* @param ... The format arguments. * @param ... The format arguments.
* @return Result<void> Whether the operation succeeded. * @return Result<void> Whether the operation succeeded.
*/ */
Result<void> print(const char* format, ...) _format(1, 2); Result<void> print(StringView fmt, ...);
/** /**
* @brief Print a newline-terminated formatted string to standard output. * @brief Print a newline-terminated formatted string to standard output.
* *
* @param format The format string (in the same format as printf(3)). * @param fmt The format string (in the same format as printf(3)).
* @param ... The format arguments. * @param ... The format arguments.
* @return Result<void> Whether the operation succeeded. * @return Result<void> Whether the operation succeeded.
*/ */
Result<void> println(const char* format, ...) _format(1, 2); Result<void> println(StringView fmt, ...);
/** /**
* @brief Print a formatted string to standard error. * @brief Print a formatted string to standard error.
* *
* @param format The format string (in the same format as printf(3)). * @param fmt The format string (in the same format as printf(3)).
* @param ... The format arguments. * @param ... The format arguments.
* @return Result<void> Whether the operation succeeded. * @return Result<void> Whether the operation succeeded.
*/ */
Result<void> eprint(const char* format, ...) _format(1, 2); Result<void> eprint(StringView fmt, ...);
/** /**
* @brief Print a newline-terminated formatted string to standard error. * @brief Print a newline-terminated formatted string to standard error.
* *
* @param format The format string (in the same format as printf(3)). * @param fmt The format string (in the same format as printf(3)).
* @param ... The format arguments. * @param ... The format arguments.
* @return Result<void> Whether the operation succeeded. * @return Result<void> Whether the operation succeeded.
*/ */
Result<void> eprintln(const char* format, ...) _format(1, 2); Result<void> eprintln(StringView fmt, ...);
} }

View File

@ -8,7 +8,6 @@
*/ */
#include <errno.h> #include <errno.h>
#include <luna/Format.h>
#include <luna/StringBuilder.h> #include <luna/StringBuilder.h>
#include <os/File.h> #include <os/File.h>
#include <unistd.h> #include <unistd.h>
@ -142,33 +141,6 @@ namespace os
return {}; return {};
} }
Result<usize> File::write_formatted(const char* format, ...)
{
va_list ap;
va_start(ap, format);
auto result = write_vformatted(format, ap);
va_end(ap);
return result;
}
Result<usize> File::write_vformatted(const char* format, va_list args)
{
auto rc = TRY(cstyle_format(
format,
[](char c, void* f) -> Result<void> {
TRY(((File*)f)->raw_write((const u8*)&c, 1));
return {};
},
this, args));
flush();
return rc;
}
Result<String> File::read_line() Result<String> File::read_line()
{ {
Vector<char> data; Vector<char> data;
@ -274,57 +246,64 @@ namespace os
setvbuf(m_file, NULL, mode, 0); setvbuf(m_file, NULL, mode, 0);
} }
Result<void> print(const char* format, ...) // FIXME: Do not allocate memory for printing.
Result<void> print_impl(SharedPtr<File> f, StringView fmt, va_list ap)
{ {
va_list ap; auto str = TRY(String::vformat(fmt, ap));
va_start(ap, format); auto rc = f->write(str.view());
f->flush();
TRY(File::standard_output()->write_vformatted(format, ap)); return rc;
va_end(ap);
return {};
} }
Result<void> println(const char* format, ...) Result<void> print(StringView fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, fmt);
auto file = File::standard_output(); auto rc = print_impl(File::standard_output(), fmt, ap);
TRY(file->write_vformatted(format, ap));
TRY(file->write("\n"_sv));
va_end(ap); va_end(ap);
return {}; return rc;
} }
Result<void> eprint(const char* format, ...) Result<void> println(StringView fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, fmt);
TRY(File::standard_error()->write_vformatted(format, ap)); auto rc = print_impl(File::standard_output(), fmt, ap);
va_end(ap); va_end(ap);
return {}; TRY(rc);
return File::standard_output()->write("\n"_sv);
} }
Result<void> eprintln(const char* format, ...) Result<void> eprint(StringView fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, fmt);
auto file = File::standard_error(); auto rc = print_impl(File::standard_error(), fmt, ap);
TRY(file->write_vformatted(format, ap));
TRY(file->write("\n"_sv));
va_end(ap); va_end(ap);
return {}; return rc;
}
Result<void> eprintln(StringView fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto rc = print_impl(File::standard_error(), fmt, ap);
va_end(ap);
TRY(rc);
return File::standard_error()->write("\n"_sv);
} }
} }

View File

@ -23,7 +23,7 @@ namespace os::SharedMemory
if (fd < 0) if (fd < 0)
{ {
int olderr = errno; int olderr = errno;
os::eprintln("os: could not create shared memory region: shm_open failed (%s) - %s", path.chars(), os::eprintln("os: could not create shared memory region: shm_open failed (%s) - %s", path,
strerror(olderr)); strerror(olderr));
return err(olderr); return err(olderr);
} }

View File

@ -144,7 +144,7 @@ Result<int> luna_main(int argc, char** argv)
{ {
if (maybe_cmd.error() == EINTR) if (maybe_cmd.error() == EINTR)
{ {
os::print("\n"); os::println("");
continue; continue;
} }
return maybe_cmd.release_error(); return maybe_cmd.release_error();
@ -200,7 +200,7 @@ Result<int> luna_main(int argc, char** argv)
int sig = WTERMSIG(status); int sig = WTERMSIG(status);
if (sig != SIGINT && sig != SIGQUIT) os::println("[sh] Process %d exited: %s", child, strsignal(sig)); if (sig != SIGINT && sig != SIGQUIT) os::println("[sh] Process %d exited: %s", child, strsignal(sig));
else else
os::print("\n"); os::println("");
} }
} }

View File

@ -186,7 +186,7 @@ Result<int> luna_main(int argc, char** argv)
if (fds[i + 4].revents & POLLHUP) clients[i]->should_be_disconnected = true; if (fds[i + 4].revents & POLLHUP) clients[i]->should_be_disconnected = true;
if (clients[i]->should_be_disconnected) if (clients[i]->should_be_disconnected)
{ {
os::println("wind: Client %zu disconnected", i); os::println("wind: Client %d disconnected", i);
fds.remove_at(i + 4); fds.remove_at(i + 4);
auto client = clients.remove_at(i); auto client = clients.remove_at(i);
client->conn->disconnect(); client->conn->disconnect();