Added spawn with commands. ex. "arsh -c 'rm -rf /'"

and fixed a spelling mistake
This commit is contained in:
Aleksandr Lebedev 2026-02-20 01:32:15 +01:00
parent 1de31ff6a5
commit 2a908210a1

26
main.c
View file

@ -8,7 +8,7 @@
#include <setjmp.h>
#include <ctype.h>
constexpr size_t max_lenght = 1024;
constexpr size_t max_length = 1024;
constexpr size_t max_env_var_lenght = 256;
const char* token_separators = " \t\n\r";
@ -99,12 +99,12 @@ struct pipeline
struct command* cmds[];
};
void* parser_memory[max_lenght] = {0};
void* parser_memory[max_length] = {0};
size_t parser_memory_index = 0;
void parser_allocated(void* 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!");
exit(1);
@ -138,7 +138,7 @@ void parser_free()
{
free(parser_memory[i]);
}
memset(parser_memory, 0, max_lenght);
memset(parser_memory, 0, max_length);
parser_memory_index = 0;
}
@ -349,7 +349,7 @@ char* parse_token(char** input)
struct command* parse_command(char* str)
{
/* 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);
char* token;
int i = 0;
@ -384,7 +384,7 @@ struct command* parse_command(char* str)
*/
struct pipeline* parse_pipeline(char *str)
{
char* copy = strndup(str, max_lenght);
char* copy = strndup(str, max_length);
parser_allocated(copy);
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");
if (cd(path) < 0)
{
char err_buf[max_lenght] = {0};
char err_buf[max_length] = {0};
sprintf(err_buf, "cd: %s", path);
perror(err_buf);
}
@ -622,6 +622,18 @@ int main(int argc, const char* argv[])
{
FILE* stream = stdin;
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)
{
FILE* file = fopen(argv[1], "r");