Compare commits
3 Commits
7fbc644753
...
48df90e636
Author | SHA1 | Date | |
---|---|---|---|
48df90e636 | |||
a2303665fc | |||
df590f4e26 |
@ -18,16 +18,13 @@ 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));
|
||||
|
||||
output->write(encoded.view());
|
||||
output->write("\n"_sv);
|
||||
os::println("%s", encoded.chars());
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -35,7 +32,7 @@ Result<int> luna_main(int argc, char** argv)
|
||||
|
||||
auto decoded = TRY(Base64::decode(data.view(), allow_garbage));
|
||||
|
||||
output->write(decoded);
|
||||
os::File::standard_output()->write(decoded);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -1,11 +1,11 @@
|
||||
#include <os/ArgumentParser.h>
|
||||
#include <os/File.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
Result<int> luna_main(int argc, char** argv)
|
||||
{
|
||||
StringView date;
|
||||
|
||||
@ -20,5 +20,7 @@ int main(int argc, char** argv)
|
||||
if (date.is_empty()) { now = time(NULL); }
|
||||
else { now = strtol(date.chars(), nullptr, 10); }
|
||||
|
||||
fputs(ctime(&now), stdout);
|
||||
os::print("%s", ctime(&now));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
14
apps/ls.cpp
14
apps/ls.cpp
@ -1,8 +1,6 @@
|
||||
#include <os/ArgumentParser.h>
|
||||
#include <os/Directory.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <os/File.h>
|
||||
|
||||
Result<int> luna_main(int argc, char** argv)
|
||||
{
|
||||
@ -27,14 +25,8 @@ Result<int> luna_main(int argc, char** argv)
|
||||
|
||||
auto files = TRY(dir->list(filter));
|
||||
|
||||
int first_ent = 1;
|
||||
for (const auto& file : files)
|
||||
{
|
||||
printf(first_ent ? "%s" : " %s", file.chars());
|
||||
first_ent = 0;
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
auto list = TRY(String::join(files, " "_sv));
|
||||
os::println("%s", list.chars());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ Result<int> luna_main(int argc, char** argv)
|
||||
if (interactive)
|
||||
{
|
||||
auto cwd = TRY(os::FileSystem::working_directory());
|
||||
printf("%s@%s:%s%c ", username, hostname, cwd.chars(), prompt_end);
|
||||
os::print("%s@%s:%s%c ", username, hostname, cwd.chars(), prompt_end);
|
||||
}
|
||||
|
||||
auto cmd = TRY(input_file->read_line());
|
||||
|
@ -32,10 +32,13 @@ 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);
|
||||
|
||||
@ -92,6 +95,4 @@ class String
|
||||
usize m_length { 0 };
|
||||
|
||||
void empty();
|
||||
|
||||
static Result<String> vformat(StringView fmt, va_list ap);
|
||||
};
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <luna/CString.h>
|
||||
#include <luna/Format.h>
|
||||
#include <luna/String.h>
|
||||
#include <luna/StringBuilder.h>
|
||||
#include <luna/Vector.h>
|
||||
|
||||
String::String()
|
||||
@ -163,3 +164,20 @@ 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();
|
||||
}
|
||||
|
@ -66,4 +66,9 @@ 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, ...);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <luna/StringBuilder.h>
|
||||
#include <os/ArgumentParser.h>
|
||||
#include <os/File.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/utsname.h>
|
||||
@ -165,7 +166,7 @@ namespace os
|
||||
|
||||
if (found) continue;
|
||||
|
||||
fprintf(stderr, "%s: unrecognized option '%s'\n", program_name.chars(), arg.chars());
|
||||
os::eprintln("%s: unrecognized option '%s'", program_name.chars(), arg.chars());
|
||||
short_usage(program_name);
|
||||
}
|
||||
else if (looks_like_short_flag(arg))
|
||||
@ -213,7 +214,7 @@ namespace os
|
||||
|
||||
if (found) continue;
|
||||
|
||||
fprintf(stderr, "%s: invalid option -- '%c'\n", program_name.chars(), c);
|
||||
os::eprintln("%s: invalid option -- '%c'", program_name.chars(), c);
|
||||
short_usage(program_name);
|
||||
}
|
||||
|
||||
@ -235,7 +236,7 @@ namespace os
|
||||
{
|
||||
if (current_value_argument->required)
|
||||
{
|
||||
fprintf(stderr, "%s: option '--%s' requires an argument\n", program_name.chars(),
|
||||
os::eprintln("%s: option '--%s' requires an argument", program_name.chars(),
|
||||
current_value_argument->long_flag.chars());
|
||||
short_usage(program_name);
|
||||
}
|
||||
@ -247,7 +248,7 @@ namespace os
|
||||
{
|
||||
if (arg.required)
|
||||
{
|
||||
fprintf(stderr, "%s: required argument '%s' not provided\n", program_name.chars(), arg.name.chars());
|
||||
os::eprintln("%s: required argument '%s' not provided", program_name.chars(), arg.name.chars());
|
||||
short_usage(program_name);
|
||||
}
|
||||
else { *arg.out = arg.fallback; }
|
||||
@ -354,7 +355,7 @@ namespace os
|
||||
void ArgumentParser::short_usage(StringView program_name)
|
||||
{
|
||||
if (m_add_short_help_flag || m_add_long_help_flag)
|
||||
fprintf(stderr, "Try running '%s %s' for more information.\n", program_name.chars(),
|
||||
os::eprintln("Try running '%s %s' for more information.", program_name.chars(),
|
||||
m_add_long_help_flag ? "--help" : "-h");
|
||||
|
||||
exit(1);
|
||||
|
@ -193,4 +193,63 @@ 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user