Compare commits

...

2 Commits

Author SHA1 Message Date
eb3fb04734
libc: Add system()
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-08 12:18:52 +02:00
95a93a7f66
sh: Parse arguments 2023-04-08 12:09:43 +02:00
5 changed files with 79 additions and 22 deletions

View File

@ -7,9 +7,11 @@ function(luna_app SOURCE_FILE APP_NAME)
endfunction()
luna_app(init.cpp init)
luna_app(sh.cpp sh)
luna_app(env.cpp env)
luna_app(sh.cpp sh)
target_link_libraries(sh PRIVATE os)
luna_app(cat.cpp cat)
target_link_libraries(cat PRIVATE os)

View File

@ -1,4 +1,5 @@
#include <luna/Vector.h>
#include <os/ArgumentParser.h>
#include <errno.h>
#include <stddef.h>
@ -30,17 +31,56 @@ static Result<Vector<char*>> split_command_into_argv(const char* cmd)
return result;
}
int main()
[[noreturn]] static void execute_command(const char* command)
{
Vector<char*> argv;
bool ok = split_command_into_argv(command).try_move_value_or_error(argv, errno);
if (!ok)
{
perror("failed to parse command");
exit(1);
}
if (argv[0] == NULL) exit(0);
execvp(argv[0], argv.data());
perror(argv[0]);
exit(1);
}
int main(int argc, char** argv)
{
StringView file;
StringView command;
FILE* f;
os::ArgumentParser parser;
parser.add_positional_argument(file, "file"_sv, "-"_sv);
parser.add_value_argument(command, 'c', "command"_sv, true);
parser.parse(argc, argv);
if (!command.is_empty()) { execute_command(command.chars()); }
if (file == "-") f = stdin;
else
{
f = fopen(file.chars(), "r");
if (!f)
{
perror("fopen");
return 1;
}
}
while (1)
{
fputs("sh$ ", stdout);
if (file == "-") fputs("sh$ ", stdout);
char command[4096];
char* rc = fgets(command, sizeof(command), stdin);
char cmd[4096];
char* rc = fgets(cmd, sizeof(cmd), f);
if (!rc) return 0;
if (strspn(command, " \n") == strlen(command)) continue;
if (strspn(cmd, " \n") == strlen(cmd)) continue;
pid_t child = fork();
if (child < 0)
@ -49,21 +89,7 @@ int main()
return 1;
}
if (child == 0)
{
Vector<char*> argv;
bool ok = split_command_into_argv(command).try_move_value_or_error(argv, errno);
if (!ok)
{
perror("failed to parse command");
return 1;
}
if (argv[0] == NULL) return 0;
execvp(argv[0], argv.data());
perror(argv[0]);
return 1;
}
if (child == 0) { execute_command(cmd); }
if (waitpid(child, NULL, 0) < 0)
{

View File

@ -100,7 +100,8 @@ extern "C"
/* Exit the program abnormally, without performing any registered cleanup actions. */
__noreturn void _Exit(int status);
int system(const char*);
/* Execute a shell command. */
int system(const char* cmd);
/* Get the value of an environment variable. */
char* getenv(const char* key);

View File

@ -6,6 +6,9 @@
#include <bits/waitpid.h>
#include <sys/types.h>
#define WIFEXITED(ret) ((ret) | 0)
#define WEXITSTATUS(ret) ((ret)&0xff)
#ifdef __cplusplus
extern "C"
{

View File

@ -6,6 +6,7 @@
#include <luna/Utf8.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>
template <typename ArgT, typename ResultT> static inline ResultT __generic_div(ArgT a, ArgT b)
@ -175,4 +176,28 @@ extern "C"
{
free_impl(ptr);
}
int system(const char* cmd)
{
if (!cmd)
{
// FIXME: Check if there is a shell available in the system.
errno = ENOTSUP;
return -1;
}
pid_t child = fork();
if (child == 0)
{
execl("/bin/sh", "sh", "-c", cmd, NULL);
_Exit(127);
}
if (child < 0) return -1;
int status;
waitpid(child, &status, 0);
return status;
}
}