mirror of
https://github.com/zoriya/ash.git
synced 2025-12-06 06:36:17 +00:00
chaning main for shell script and adding some func in header
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -7,4 +7,5 @@
|
||||
unit_tests
|
||||
test_shell/
|
||||
func_tests
|
||||
.vscode
|
||||
.vscode
|
||||
scri.sh
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user