Compare commits

..

No commits in common. "48df90e63652b1e4ae5c3b6ef277ac6677d20d8d" and "7fbc6447536054b3c0239021ce17ff0f966c9aa2" have entirely different histories.

9 changed files with 29 additions and 104 deletions

View File

@ -18,13 +18,16 @@ Result<int> luna_main(int argc, char** argv)
auto file = TRY(os::File::open_input_file(path));
auto output = os::File::standard_output();
if (!decode)
{
auto data = TRY(file->read_all());
auto encoded = TRY(Base64::encode(data));
os::println("%s", encoded.chars());
output->write(encoded.view());
output->write("\n"_sv);
}
else
{
@ -32,7 +35,7 @@ Result<int> luna_main(int argc, char** argv)
auto decoded = TRY(Base64::decode(data.view(), allow_garbage));
os::File::standard_output()->write(decoded);
output->write(decoded);
}
return 0;

View File

@ -1,11 +1,11 @@
#include <os/ArgumentParser.h>
#include <os/File.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Result<int> luna_main(int argc, char** argv)
int main(int argc, char** argv)
{
StringView date;
@ -20,7 +20,5 @@ Result<int> luna_main(int argc, char** argv)
if (date.is_empty()) { now = time(NULL); }
else { now = strtol(date.chars(), nullptr, 10); }
os::print("%s", ctime(&now));
return 0;
fputs(ctime(&now), stdout);
}

View File

@ -1,6 +1,8 @@
#include <os/ArgumentParser.h>
#include <os/Directory.h>
#include <os/File.h>
#include <stdio.h>
#include <unistd.h>
Result<int> luna_main(int argc, char** argv)
{
@ -25,8 +27,14 @@ Result<int> luna_main(int argc, char** argv)
auto files = TRY(dir->list(filter));
auto list = TRY(String::join(files, " "_sv));
os::println("%s", list.chars());
int first_ent = 1;
for (const auto& file : files)
{
printf(first_ent ? "%s" : " %s", file.chars());
first_ent = 0;
}
putchar('\n');
return 0;
}

View File

@ -80,7 +80,7 @@ Result<int> luna_main(int argc, char** argv)
if (interactive)
{
auto cwd = TRY(os::FileSystem::working_directory());
os::print("%s@%s:%s%c ", username, hostname, cwd.chars(), prompt_end);
printf("%s@%s:%s%c ", username, hostname, cwd.chars(), prompt_end);
}
auto cmd = TRY(input_file->read_line());

View File

@ -32,13 +32,10 @@ class String
return view().split_once(delim);
}
static Result<String> join(const Vector<String>& vec, StringView delim);
void trim(StringView delim);
static Result<String> format(const String& fmt, ...);
static Result<String> format(StringView fmt, ...);
static Result<String> vformat(StringView fmt, va_list ap);
static Result<String> from_cstring(const char* str);
@ -95,4 +92,6 @@ class String
usize m_length { 0 };
void empty();
static Result<String> vformat(StringView fmt, va_list ap);
};

View File

@ -2,7 +2,6 @@
#include <luna/CString.h>
#include <luna/Format.h>
#include <luna/String.h>
#include <luna/StringBuilder.h>
#include <luna/Vector.h>
String::String()
@ -164,20 +163,3 @@ Result<String> String::from_cstring(const char* str)
if (!dup) return err(ENOMEM);
return String { dup };
}
Result<String> String::join(const Vector<String>& vec, StringView delim)
{
if (vec.size() == 0) return String {};
if (vec.size() == 1) return vec[0].clone();
StringBuilder sb;
TRY(sb.add(vec[0].view()));
for (usize i = 1; i < vec.size(); i++)
{
TRY(sb.add(delim));
TRY(sb.add(vec[i].view()));
}
return sb.string();
}

View File

@ -66,9 +66,4 @@ namespace os
int m_fd { -1 };
};
Result<void> print(StringView fmt, ...);
Result<void> println(StringView fmt, ...);
Result<void> eprint(StringView fmt, ...);
Result<void> eprintln(StringView fmt, ...);
}

View File

@ -1,6 +1,5 @@
#include <luna/StringBuilder.h>
#include <os/ArgumentParser.h>
#include <os/File.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>
@ -166,7 +165,7 @@ namespace os
if (found) continue;
os::eprintln("%s: unrecognized option '%s'", program_name.chars(), arg.chars());
fprintf(stderr, "%s: unrecognized option '%s'\n", program_name.chars(), arg.chars());
short_usage(program_name);
}
else if (looks_like_short_flag(arg))
@ -214,7 +213,7 @@ namespace os
if (found) continue;
os::eprintln("%s: invalid option -- '%c'", program_name.chars(), c);
fprintf(stderr, "%s: invalid option -- '%c'\n", program_name.chars(), c);
short_usage(program_name);
}
@ -236,8 +235,8 @@ namespace os
{
if (current_value_argument->required)
{
os::eprintln("%s: option '--%s' requires an argument", program_name.chars(),
current_value_argument->long_flag.chars());
fprintf(stderr, "%s: option '--%s' requires an argument\n", program_name.chars(),
current_value_argument->long_flag.chars());
short_usage(program_name);
}
else { *current_value_argument->out = current_value_argument->fallback; }
@ -248,7 +247,7 @@ namespace os
{
if (arg.required)
{
os::eprintln("%s: required argument '%s' not provided", program_name.chars(), arg.name.chars());
fprintf(stderr, "%s: required argument '%s' not provided\n", program_name.chars(), arg.name.chars());
short_usage(program_name);
}
else { *arg.out = arg.fallback; }
@ -355,8 +354,8 @@ namespace os
void ArgumentParser::short_usage(StringView program_name)
{
if (m_add_short_help_flag || m_add_long_help_flag)
os::eprintln("Try running '%s %s' for more information.", program_name.chars(),
m_add_long_help_flag ? "--help" : "-h");
fprintf(stderr, "Try running '%s %s' for more information.\n", program_name.chars(),
m_add_long_help_flag ? "--help" : "-h");
exit(1);
}

View File

@ -193,63 +193,4 @@ namespace os
{
fcntl(m_fd, F_SETFD, FD_CLOEXEC);
}
// 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));
return f->write(str.view());
}
Result<void> print(StringView fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto rc = print_impl(File::standard_output(), fmt, ap);
va_end(ap);
return rc;
}
Result<void> println(StringView fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto rc = print_impl(File::standard_output(), fmt, ap);
va_end(ap);
TRY(rc);
return File::standard_output()->write("\n"_sv);
}
Result<void> eprint(StringView fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto rc = print_impl(File::standard_error(), fmt, ap);
va_end(ap);
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);
}
}