libos: Add a new main() function

This commit is contained in:
apio 2023-04-13 17:04:59 +02:00
parent d97bf991d1
commit 26ff964ec1
Signed by: apio
GPG Key ID: B8A7D06E42258954
9 changed files with 42 additions and 8 deletions

View File

@ -31,7 +31,7 @@ static void do_cat(StringView path)
if (f != stdin) fclose(f); if (f != stdin) fclose(f);
} }
int main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
StringView filename; StringView filename;
@ -42,4 +42,6 @@ int main(int argc, char** argv)
do_cat(filename); do_cat(filename);
for (auto file : extra_files) { do_cat(file); } for (auto file : extra_files) { do_cat(file); }
return 0;
} }

View File

@ -5,7 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
StringView pathname; StringView pathname;
@ -13,7 +13,7 @@ int 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 = os::File::open_or_create(pathname, os::File::WriteOnly).release_value(); auto file = TRY(os::File::open_or_create(pathname, os::File::WriteOnly));
char buffer[4096]; char buffer[4096];
@ -21,7 +21,7 @@ int main(int argc, char** argv)
{ {
char* rc = fgets(buffer, sizeof(buffer), stdin); char* rc = fgets(buffer, sizeof(buffer), stdin);
if (rc == 0) break; if (rc == 0) break;
file->write(StringView { buffer }); TRY(file->write(StringView { buffer }));
} }
return 0; return 0;

View File

@ -5,7 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
int main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
StringView pathname; StringView pathname;
bool show_all { false }; bool show_all { false };

View File

@ -1,7 +1,7 @@
#include <os/ArgumentParser.h> #include <os/ArgumentParser.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
StringView path; StringView path;
@ -14,4 +14,6 @@ int main(int argc, char** argv)
perror("rm"); perror("rm");
return 1; return 1;
} }
return 0;
} }

View File

@ -47,7 +47,7 @@ static Result<Vector<char*>> split_command_into_argv(const char* cmd)
exit(1); exit(1);
} }
int main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
StringView file; StringView file;
StringView command; StringView command;

View File

@ -51,7 +51,7 @@ char* getpass()
return buf; return buf;
} }
int main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
StringView name; StringView name;
@ -92,4 +92,6 @@ int main(int argc, char** argv)
chdir(entry->pw_dir); chdir(entry->pw_dir);
execl(entry->pw_shell, entry->pw_shell, NULL); execl(entry->pw_shell, entry->pw_shell, NULL);
return 1;
} }

View File

@ -6,6 +6,7 @@ set(SOURCES
${HEADERS} ${HEADERS}
src/ArgumentParser.cpp src/ArgumentParser.cpp
src/File.cpp src/File.cpp
src/Main.cpp
) )
add_library(os ${SOURCES}) add_library(os ${SOURCES})

4
libos/include/os/Main.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include <luna/Result.h>
Result<int> luna_main(int argc, char** argv);

23
libos/src/Main.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <os/Main.h>
#include <errno.h>
#include <stdio.h>
int g_argc;
char** g_argv;
__attribute__((weak)) int main(int argc, char** argv)
{
g_argc = argc;
g_argv = argv;
int result;
bool ok = luna_main(argc, argv).try_set_value_or_error(result, errno);
if (!ok)
{
perror(g_argv[0]);
return 1;
}
return result;
}