Compare commits

...

3 Commits

Author SHA1 Message Date
7d69ac56e2
apps+libos+shell+wind: Correct a bunch of format strings
All checks were successful
Build and test / build (push) Successful in 1m41s
2024-03-29 14:42:38 +01:00
f9b39c5ff3
libos: Add ways to format output to a File 2024-03-29 14:41:45 +01:00
d70effd1db
libos: Clarify requirements for File::write 2024-03-29 14:25:14 +01:00
9 changed files with 91 additions and 50 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 & POLLHUP)
{
os::println("launch: Client %d disconnected", i);
os::println("launch: Client %zu disconnected", i);
fds.remove_at(i + 1);
auto client = clients.remove_at(i);
client->disconnect();

View File

@ -212,14 +212,15 @@ Result<int> luna_main(int argc, char** argv)
{
if (colors)
{
os::println("%s %u %4s %4s %10lu %s%s" RESET_COLORS "%s" SYMLINK_COLOR "%s" RESET_COLORS,
os::println("%s %lu %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,
file_type_color(file), file.name.chars(), link.is_empty() ? "" : " -> ", link.chars());
}
else
{
os::println("%s %u %4s %4s %10lu %s%s%s", formatted_mode, st.st_nlink, owner.chars(), group.chars(),
st.st_size, file.name.chars(), link.is_empty() ? "" : " -> ", link.chars());
os::println("%s %lu %4s %4s %10lu %s%s%s", formatted_mode, st.st_nlink, owner.chars(),
group.chars(), st.st_size, file.name.chars(), link.is_empty() ? "" : " -> ",
link.chars());
}
}
else
@ -227,13 +228,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));
if (colors)
{
os::println("%s %u %4s %4s %6s %s%s" RESET_COLORS "%s" SYMLINK_COLOR "%s" RESET_COLORS,
os::println("%s %lu %4s %4s %6s %s%s" RESET_COLORS "%s" SYMLINK_COLOR "%s" RESET_COLORS,
formatted_mode, st.st_nlink, owner.chars(), group.chars(), size.chars(),
file_type_color(file), file.name.chars(), link.is_empty() ? "" : " -> ", link.chars());
}
else
{
os::println("%s %u %4s %4s %6s %s%s%s", formatted_mode, st.st_nlink, owner.chars(), group.chars(),
os::println("%s %lu %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());
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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 (clients[i]->should_be_disconnected)
{
os::println("wind: Client %d disconnected", i);
os::println("wind: Client %zu disconnected", i);
fds.remove_at(i + 4);
auto client = clients.remove_at(i);
client->conn->disconnect();