chaning main for shell script and adding some func in header

This commit is contained in:
Louis
2020-05-21 11:35:18 +02:00
parent 8367d5f274
commit 9595b992db
4 changed files with 35 additions and 19 deletions

3
.gitignore vendored
View File

@@ -7,4 +7,5 @@
unit_tests
test_shell/
func_tests
.vscode
.vscode
scri.sh

View File

@@ -43,8 +43,8 @@ void remove_duplicate_history(env_t *env);
int execute_command_history(history_t *old, history_t *new,
char **args, env_t *env);
//source
char *parse_source_cmd(char *cmd, char **argv, int len_argv);
void init_source_args(char **argv, int len_argv, env_t *env);
void reset_source_args(int len_argv, env_t *env);
//alias
int add_alias(alias_t **list, char *alias, char **command);
char *concatenate(char **command);

View File

@@ -16,27 +16,40 @@
#include "utility.h"
#include "shell.h"
static void start_script(char **argv, env_t *env, int len_argv)
char **get_arr_from_fd(int fd, char *filepath, char *str)
{
char *str = NULL;
char **arr = NULL;
int fd = open(argv[1], O_RDONLY);
bool should_close = false;
struct stat st_buff;
char **arr = NULL;
if (fd == -1)
return;
stat(argv[1], &st_buff);
stat(filepath, &st_buff);
str = malloc(st_buff.st_size + 1);
if (str) {
read(fd, str, st_buff.st_size);
arr = split_str(str, '\n');
if (arr)
for (int i = 0; arr[i] && !should_close; i++)
should_close = (eval_raw_cmd(parse_source_cmd(arr[i],
argv, len_argv), env) < 0);
free(str);
}
return (arr);
}
static void start_script(char **argv, env_t *env, int len_argv)
{
int fd = open(argv[1], O_RDONLY);
bool should_close = false;
char **arr = NULL;
char *str = NULL;
if (fd == -1)
return;
arr = get_arr_from_fd(fd, argv[1], str);
if (arr) {
for (int i = 0; arr[i] && !should_close; i++) {
if (arr[i][0] == '#')
continue;
should_close = (eval_raw_cmd(parse_source_cmd(arr[i],
argv, len_argv), env) < 0);
}
}
free(arr);
free(str);
close(fd);
}

View File

@@ -5,6 +5,7 @@
** main
*/
#include "builtin.h"
#include "shell.h"
#include "key_functions.h"
#include <stdlib.h>
@@ -38,9 +39,10 @@ int main(int argc, char **argv, char **env)
if (!envt)
return (ERROR);
start_shell(envt);
(void)argc;
(void)argv;
if (argc >= 2)
builtin_source(argv, envt);
else
start_shell(envt);
ret = get_return(my_getenv(envt->vars, "?"));
free_env(envt);
return (ret);