Compare commits
2 Commits
0eab03848c
...
eb3fb04734
Author | SHA1 | Date | |
---|---|---|---|
eb3fb04734 | |||
95a93a7f66 |
@ -7,9 +7,11 @@ function(luna_app SOURCE_FILE APP_NAME)
|
|||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
luna_app(init.cpp init)
|
luna_app(init.cpp init)
|
||||||
luna_app(sh.cpp sh)
|
|
||||||
luna_app(env.cpp env)
|
luna_app(env.cpp env)
|
||||||
|
|
||||||
|
luna_app(sh.cpp sh)
|
||||||
|
target_link_libraries(sh PRIVATE os)
|
||||||
|
|
||||||
luna_app(cat.cpp cat)
|
luna_app(cat.cpp cat)
|
||||||
target_link_libraries(cat PRIVATE os)
|
target_link_libraries(cat PRIVATE os)
|
||||||
|
|
||||||
|
66
apps/sh.cpp
66
apps/sh.cpp
@ -1,4 +1,5 @@
|
|||||||
#include <luna/Vector.h>
|
#include <luna/Vector.h>
|
||||||
|
#include <os/ArgumentParser.h>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@ -30,17 +31,56 @@ static Result<Vector<char*>> split_command_into_argv(const char* cmd)
|
|||||||
return result;
|
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)
|
while (1)
|
||||||
{
|
{
|
||||||
fputs("sh$ ", stdout);
|
if (file == "-") fputs("sh$ ", stdout);
|
||||||
|
|
||||||
char command[4096];
|
char cmd[4096];
|
||||||
char* rc = fgets(command, sizeof(command), stdin);
|
char* rc = fgets(cmd, sizeof(cmd), f);
|
||||||
if (!rc) return 0;
|
if (!rc) return 0;
|
||||||
|
|
||||||
if (strspn(command, " \n") == strlen(command)) continue;
|
if (strspn(cmd, " \n") == strlen(cmd)) continue;
|
||||||
|
|
||||||
pid_t child = fork();
|
pid_t child = fork();
|
||||||
if (child < 0)
|
if (child < 0)
|
||||||
@ -49,21 +89,7 @@ int main()
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (child == 0)
|
if (child == 0) { execute_command(cmd); }
|
||||||
{
|
|
||||||
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 (waitpid(child, NULL, 0) < 0)
|
if (waitpid(child, NULL, 0) < 0)
|
||||||
{
|
{
|
||||||
|
@ -100,7 +100,8 @@ extern "C"
|
|||||||
/* Exit the program abnormally, without performing any registered cleanup actions. */
|
/* Exit the program abnormally, without performing any registered cleanup actions. */
|
||||||
__noreturn void _Exit(int status);
|
__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. */
|
/* Get the value of an environment variable. */
|
||||||
char* getenv(const char* key);
|
char* getenv(const char* key);
|
||||||
|
@ -6,6 +6,9 @@
|
|||||||
#include <bits/waitpid.h>
|
#include <bits/waitpid.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#define WIFEXITED(ret) ((ret) | 0)
|
||||||
|
#define WEXITSTATUS(ret) ((ret)&0xff)
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include <luna/Utf8.h>
|
#include <luna/Utf8.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
template <typename ArgT, typename ResultT> static inline ResultT __generic_div(ArgT a, ArgT b)
|
template <typename ArgT, typename ResultT> static inline ResultT __generic_div(ArgT a, ArgT b)
|
||||||
@ -175,4 +176,28 @@ extern "C"
|
|||||||
{
|
{
|
||||||
free_impl(ptr);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user