40 pipeline->
status[0] =
'\0';
42 for (
uint64_t i = 0; i < tokenAmount; i++)
58 const char** currentArgv =
malloc(
sizeof(
char*) * (tokenAmount + 1));
59 if (currentArgv ==
NULL)
66 for (
uint64_t i = 0; i < tokenAmount; i++)
68 if (
strcmp(tokens[i],
"|") == 0)
72 printf(
"shell: empty command in pipeline\n");
78 goto token_parse_error;
81 currentArgv[currentArg] =
NULL;
84 cmd->
argv = currentArgv;
85 cmd->
argc = currentArg;
88 if (
open2(
"/dev/pipe/new", pipe) ==
ERR)
91 goto token_parse_error;
99 currentArgv =
malloc(
sizeof(
char*) * (tokenAmount + 1));
100 if (currentArgv ==
NULL)
102 printf(
"shell: out of memory\n");
103 goto token_parse_error;
106 cmd_t* nextCmd = &pipeline->
cmds[currentCmd];
110 else if (
strcmp(tokens[i],
"<") == 0)
112 if (i + 1 >= tokenAmount)
114 printf(
"shell: missing filename after <\n");
115 goto token_parse_error;
122 goto token_parse_error;
125 cmd_t* cmd = &pipeline->
cmds[currentCmd];
135 else if (
strcmp(tokens[i],
">") == 0)
137 if (i + 1 >= tokenAmount)
139 printf(
"shell: missing filename after >\n");
140 goto token_parse_error;
147 goto token_parse_error;
150 cmd_t* cmd = &pipeline->
cmds[currentCmd];
160 else if (
strcmp(tokens[i],
"2>") == 0)
162 if (i + 1 >= tokenAmount)
164 printf(
"shell: missing filename after 2>\n");
165 goto token_parse_error;
172 goto token_parse_error;
175 cmd_t* cmd = &pipeline->
cmds[currentCmd];
187 currentArgv[currentArg] =
strdup(tokens[i]);
188 if (currentArgv[currentArg] ==
NULL)
190 printf(
"shell: out of memory\n");
191 goto token_parse_error;
199 currentArgv[currentArg] =
NULL;
200 pipeline->
cmds[currentCmd].
argv = currentArgv;
201 pipeline->
cmds[currentCmd].
argc = currentArg;
207 if (pipeline->
amount > 0 && currentCmd > 0)
209 printf(
"shell: pipeline ends with empty command\n");
210 cmd_t* emptyCmd = &pipeline->
cmds[currentCmd];
216 goto token_parse_error_no_current_argv;
220 pipeline->
amount = currentCmd;
225 if (currentArgv !=
NULL)
227 for (
uint64_t k = 0; k < currentArg; k++)
229 free((
void*)currentArgv[k]);
234token_parse_error_no_current_argv:
235 for (
uint64_t j = 0; j < tokenAmount; j++)
301 if (originalStdin ==
ERR)
306 if (originalStdout ==
ERR)
308 close(originalStdin);
312 if (originalStderr ==
ERR)
314 close(originalStdin);
315 close(originalStdout);
322 close(originalStdin);
323 close(originalStdout);
324 close(originalStderr);
328 const char** argv = cmd->
argv;
350 printf(
"shell: %s not found\n", argv[0]);
356 bool isFound =
false;
357 char* pathEnv =
readfiles(
"/proc/self/env/PATH");
360 pathEnv =
strdup(
"/bin:/usr/bin");
365 char* token =
strtok(pathEnv,
":");
366 while (token !=
NULL)
374 const char* newArgv[argc + 1];
378 newArgv[k] = argv[k];
380 newArgv[argc] =
NULL;
404 close(originalStdin);
405 close(originalStdout);
406 close(originalStderr);
462 if (readCount ==
ERR)
#define MAX_PATH
Maximum length of filepaths.
uint64_t builtin_execute(uint64_t argc, const char **argv)
bool builtin_exists(const char *name)
#define errno
Error number variable.
const char ** argsplit(const char *str, uint64_t maxLen, uint64_t *count)
Standardized argument parsing function.
fd_t dup2(fd_t oldFd, fd_t newFd)
System call for duplicating file descriptors, with a destination.
uint64_t stat(const char *path, stat_t *stat)
System call for retrieving info about a file or directory.
fd_t open(const char *path)
System call for opening files.
#define PIPE_READ
Pipe read end.
uint64_t close(fd_t fd)
System call for closing files.
char * readfiles(const char *path)
Wrapper for reading an entire file directly into a null-terminated string.
fd_t dup(fd_t oldFd)
System call for duplicating file descriptors.
#define F(format,...)
Allocates a formatted string on the stack.
size_t read(fd_t fd, void *buffer, size_t count)
System call for reading from files.
#define PIPE_WRITE
Pipe write end.
#define STDOUT_FILENO
Standard output file descriptor.
uint64_t open2(const char *path, fd_t fd[2])
System call for opening 2 file descriptors from one file.
#define STDERR_FILENO
Standard error file descriptor.
#define RETRY_EINTR(expr)
Macro to automatically retry a function that returns an integer if it errors and errno == EINTR.
#define STDIN_FILENO
Standard input file descriptor.
pid_t spawn(const char **argv, spawn_flags_t flags)
System call for spawning new processes.
@ SPAWN_STDIO_FDS
Only inherit stdin, stdout and stderr from the parent process.
#define NULL
Pointer error value.
#define ERR
Integer error value.
__UINT64_TYPE__ fd_t
File descriptor type.
__UINT64_TYPE__ pid_t
Process Identifier.
static pid_t pipeline_execute_cmd(cmd_t *cmd)
void pipeline_deinit(pipeline_t *pipeline)
void pipeline_wait(pipeline_t *pipeline)
uint64_t pipeline_init(pipeline_t *pipeline, const char *cmdline, fd_t stdin, fd_t stdout, fd_t stderr)
void pipeline_execute(pipeline_t *pipeline)
_PUBLIC int printf(const char *_RESTRICT format,...)
_PUBLIC int fprintf(FILE *_RESTRICT stream, const char *_RESTRICT format,...)
_PUBLIC int snprintf(char *_RESTRICT s, size_t n, const char *_RESTRICT format,...)
_PUBLIC void * malloc(size_t size)
_PUBLIC void free(void *ptr)
_PUBLIC char * strerror(int errnum)
_PUBLIC char * strtok(char *_RESTRICT s1, const char *_RESTRICT s2)
char * strdup(const char *src)
_PUBLIC char * strcpy(char *_RESTRICT s1, const char *_RESTRICT s2)
_PUBLIC int strcmp(const char *s1, const char *s2)
_PUBLIC void * memset(void *s, int c, size_t n)
_PUBLIC char * strchr(const char *s, int c)
vtype_t type
The type of the entries vnode.