Compare commits
No commits in common. "eb3fb047346288142e0337843b807fc3dd0cb0ea" and "0eab03848c83c03cb989adfbaf0ac187f0ac9ca8" have entirely different histories.
eb3fb04734
...
0eab03848c
@ -7,10 +7,8 @@ function(luna_app SOURCE_FILE APP_NAME)
|
|||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
luna_app(init.cpp init)
|
luna_app(init.cpp init)
|
||||||
luna_app(env.cpp env)
|
|
||||||
|
|
||||||
luna_app(sh.cpp sh)
|
luna_app(sh.cpp sh)
|
||||||
target_link_libraries(sh PRIVATE os)
|
luna_app(env.cpp env)
|
||||||
|
|
||||||
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,5 +1,4 @@
|
|||||||
#include <luna/Vector.h>
|
#include <luna/Vector.h>
|
||||||
#include <os/ArgumentParser.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@ -31,56 +30,17 @@ static Result<Vector<char*>> split_command_into_argv(const char* cmd)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[noreturn]] static void execute_command(const char* command)
|
int main()
|
||||||
{
|
{
|
||||||
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)
|
||||||
{
|
{
|
||||||
if (file == "-") fputs("sh$ ", stdout);
|
fputs("sh$ ", stdout);
|
||||||
|
|
||||||
char cmd[4096];
|
char command[4096];
|
||||||
char* rc = fgets(cmd, sizeof(cmd), f);
|
char* rc = fgets(command, sizeof(command), stdin);
|
||||||
if (!rc) return 0;
|
if (!rc) return 0;
|
||||||
|
|
||||||
if (strspn(cmd, " \n") == strlen(cmd)) continue;
|
if (strspn(command, " \n") == strlen(command)) continue;
|
||||||
|
|
||||||
pid_t child = fork();
|
pid_t child = fork();
|
||||||
if (child < 0)
|
if (child < 0)
|
||||||
@ -89,7 +49,21 @@ int main(int argc, char** argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (child == 0) { execute_command(cmd); }
|
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 (waitpid(child, NULL, 0) < 0)
|
if (waitpid(child, NULL, 0) < 0)
|
||||||
{
|
{
|
||||||
|
@ -100,8 +100,7 @@ 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);
|
||||||
|
|
||||||
/* Execute a shell command. */
|
int system(const char*);
|
||||||
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,9 +6,6 @@
|
|||||||
#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,7 +6,6 @@
|
|||||||
#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)
|
||||||
@ -176,28 +175,4 @@ 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…
x
Reference in New Issue
Block a user