sh: Parse arguments
This commit is contained in:
parent
0eab03848c
commit
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)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user