Added spawn with commands. ex. "arsh -c 'rm -rf /'"
and fixed a spelling mistake
This commit is contained in:
parent
1de31ff6a5
commit
2a908210a1
1 changed files with 19 additions and 7 deletions
26
main.c
26
main.c
|
|
@ -8,7 +8,7 @@
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
constexpr size_t max_lenght = 1024;
|
constexpr size_t max_length = 1024;
|
||||||
constexpr size_t max_env_var_lenght = 256;
|
constexpr size_t max_env_var_lenght = 256;
|
||||||
const char* token_separators = " \t\n\r";
|
const char* token_separators = " \t\n\r";
|
||||||
|
|
||||||
|
|
@ -99,12 +99,12 @@ struct pipeline
|
||||||
struct command* cmds[];
|
struct command* cmds[];
|
||||||
};
|
};
|
||||||
|
|
||||||
void* parser_memory[max_lenght] = {0};
|
void* parser_memory[max_length] = {0};
|
||||||
size_t parser_memory_index = 0;
|
size_t parser_memory_index = 0;
|
||||||
void parser_allocated(void* ptr)
|
void parser_allocated(void* ptr)
|
||||||
{
|
{
|
||||||
parser_memory[parser_memory_index] = ptr;
|
parser_memory[parser_memory_index] = ptr;
|
||||||
if(parser_memory_index == max_lenght)
|
if(parser_memory_index == max_length)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Not enough memory for parser: Memory will leak!");
|
fprintf(stderr, "Not enough memory for parser: Memory will leak!");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
@ -138,7 +138,7 @@ void parser_free()
|
||||||
{
|
{
|
||||||
free(parser_memory[i]);
|
free(parser_memory[i]);
|
||||||
}
|
}
|
||||||
memset(parser_memory, 0, max_lenght);
|
memset(parser_memory, 0, max_length);
|
||||||
parser_memory_index = 0;
|
parser_memory_index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -349,7 +349,7 @@ char* parse_token(char** input)
|
||||||
struct command* parse_command(char* str)
|
struct command* parse_command(char* str)
|
||||||
{
|
{
|
||||||
/* Copy the input line in case the caller wants it later. */
|
/* Copy the input line in case the caller wants it later. */
|
||||||
char* copy = strndup(str, max_lenght);
|
char* copy = strndup(str, max_length);
|
||||||
parser_allocated(copy);
|
parser_allocated(copy);
|
||||||
char* token;
|
char* token;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
@ -384,7 +384,7 @@ struct command* parse_command(char* str)
|
||||||
*/
|
*/
|
||||||
struct pipeline* parse_pipeline(char *str)
|
struct pipeline* parse_pipeline(char *str)
|
||||||
{
|
{
|
||||||
char* copy = strndup(str, max_lenght);
|
char* copy = strndup(str, max_length);
|
||||||
parser_allocated(copy);
|
parser_allocated(copy);
|
||||||
|
|
||||||
size_t cap = 4;
|
size_t cap = 4;
|
||||||
|
|
@ -564,7 +564,7 @@ int execute_pipeline(struct pipeline* pl, int capture, char** out)
|
||||||
char* path = cmd->argv[1] ? cmd->argv[1] : getenv("HOME");
|
char* path = cmd->argv[1] ? cmd->argv[1] : getenv("HOME");
|
||||||
if (cd(path) < 0)
|
if (cd(path) < 0)
|
||||||
{
|
{
|
||||||
char err_buf[max_lenght] = {0};
|
char err_buf[max_length] = {0};
|
||||||
sprintf(err_buf, "cd: %s", path);
|
sprintf(err_buf, "cd: %s", path);
|
||||||
perror(err_buf);
|
perror(err_buf);
|
||||||
}
|
}
|
||||||
|
|
@ -622,6 +622,18 @@ int main(int argc, const char* argv[])
|
||||||
{
|
{
|
||||||
FILE* stream = stdin;
|
FILE* stream = stdin;
|
||||||
char* prompt = "arsh> ";
|
char* prompt = "arsh> ";
|
||||||
|
const bool spawn_command = argc >= 2 && strcmp(argv[1], "-c") == 0;
|
||||||
|
if(spawn_command)
|
||||||
|
{
|
||||||
|
if(argc !=3)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "There must be exactly 3 arguments, when starting with a command '-c', but provided %d\n", argc);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
char* input = strdup(argv[2]);
|
||||||
|
struct pipeline* pipeline = parse_pipeline(input);
|
||||||
|
exit(execute_pipeline(pipeline, 0, NULL));
|
||||||
|
}
|
||||||
if(argc == 2)
|
if(argc == 2)
|
||||||
{
|
{
|
||||||
FILE* file = fopen(argv[1], "r");
|
FILE* file = fopen(argv[1], "r");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue