mirror of
https://github.com/zoriya/ash.git
synced 2026-06-01 10:15:10 +00:00
if one liners working, need to check the syntax of the expression though, will do later
This commit is contained in:
@@ -27,6 +27,8 @@ SRC = src/shell.c \
|
||||
src/builtin/builtin_alias.c \
|
||||
src/builtin/builtin_unalias.c \
|
||||
src/builtin/builtin_echo.c \
|
||||
src/builtin/builtin_if.c \
|
||||
src/builtin/builtin_if_two.c \
|
||||
src/signal.c \
|
||||
src/free_env.c \
|
||||
src/utility/same_var.c \
|
||||
|
||||
+7
-1
@@ -29,6 +29,7 @@ int builtin_history(char **args, env_t *env);
|
||||
int builtin_alias(char **args, env_t *env);
|
||||
int builtin_unalias(char **args, env_t *env);
|
||||
int builtin_echo(char **args, env_t *env);
|
||||
int builtin_if(char **argv, env_t *env);
|
||||
//utility
|
||||
bool find_path_in_builtins(char *cmd);
|
||||
char **get_envpath(env_t *env);
|
||||
@@ -58,4 +59,9 @@ int add_alias_to_list(alias_t **list, alias_t *elem, char *alias);
|
||||
void remove_alias(char *alias, alias_t **list);
|
||||
|
||||
//echo
|
||||
int print_char(char *str, int i);
|
||||
int print_char(char *str, int i);
|
||||
|
||||
//if
|
||||
|
||||
int get_test_return_value(char **argv, env_t *env);
|
||||
int get_max_cmd_len(char **argv);
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2020
|
||||
** 42sh
|
||||
** File description:
|
||||
** if builtin
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "shell.h"
|
||||
#include "builtin.h"
|
||||
|
||||
int get_max_cmd_len(char **argv)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
for (int i = 0; argv[i]; i++) {
|
||||
if (i)
|
||||
len++;
|
||||
for (int j = 0; argv[i][j]; j++)
|
||||
len++;
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
int execute_one_liner(char **argv, env_t *env)
|
||||
{
|
||||
bool after_exp = false;
|
||||
char *cmd = malloc(sizeof(char) * get_max_cmd_len(argv));
|
||||
int counter = 0;
|
||||
|
||||
if (!cmd || get_test_return_value(argv, env))
|
||||
return (0);
|
||||
for (int i = 0; argv[i]; i++) {
|
||||
for (int j = 0; argv[i][j]; j++) {
|
||||
if (after_exp)
|
||||
cmd[counter++] = argv[i][j];
|
||||
if (argv[i][j] == ')')
|
||||
after_exp = true;
|
||||
}
|
||||
if (after_exp && counter)
|
||||
cmd[counter++] = ' ';
|
||||
}
|
||||
cmd[counter] = 0;
|
||||
eval_raw_cmd(cmd, env);
|
||||
free(cmd);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int is_valid_expression(char **argv)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
for (; argv[len]; len++);
|
||||
if (len < 1)
|
||||
return (0);
|
||||
if (argv[1][0] != '(')
|
||||
return (0);
|
||||
for (int i = 1; argv[i]; i++)
|
||||
for (int j = 0; argv[i][j]; j++)
|
||||
if (argv[i][j] == ')')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int builtin_if(char **argv, env_t *env)
|
||||
{
|
||||
if (is_valid_expression(argv)) {
|
||||
execute_one_liner(argv, env);
|
||||
} else {
|
||||
printf("if: Expression Syntax.\n");
|
||||
fflush(stdout);
|
||||
env->vars = my_setenv(env->vars, "?", "1");
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2020
|
||||
** 42sh
|
||||
** File description:
|
||||
** builtin_if_two
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "shell.h"
|
||||
#include "builtin.h"
|
||||
|
||||
char *get_expr(char **argv)
|
||||
{
|
||||
bool in_expr = false;
|
||||
char *res = malloc(sizeof(char) * get_max_cmd_len(argv) + 5);
|
||||
int counter = 5;
|
||||
|
||||
if (!res) return (NULL);
|
||||
strcpy(res, "test ");
|
||||
for (int i = 0; argv[i]; i++) {
|
||||
for (int j = 0; argv[i][j]; j++) {
|
||||
if (argv[i][j] == ')')
|
||||
in_expr = false;
|
||||
if (in_expr)
|
||||
res[counter++] = argv[i][j];
|
||||
if (argv[i][j] == '(')
|
||||
in_expr = true;
|
||||
}
|
||||
if (in_expr && counter)
|
||||
res[counter++] = ' ';
|
||||
}
|
||||
res[counter] = 0;
|
||||
return (res);
|
||||
}
|
||||
|
||||
int get_test_return_value(char **argv, env_t *env)
|
||||
{
|
||||
int res = 0;
|
||||
char *exp = get_expr(argv);
|
||||
|
||||
eval_raw_cmd(exp, env);
|
||||
res = my_getenv(env->vars, "?") ? atoi(my_getenv(env->vars, "?")) : 1;
|
||||
free(exp);
|
||||
return (res);
|
||||
}
|
||||
@@ -10,7 +10,6 @@
|
||||
#include <string.h>
|
||||
#include "utility.h"
|
||||
|
||||
|
||||
char *my_getenv(char **env, char *name)
|
||||
{
|
||||
if (!env)
|
||||
|
||||
@@ -58,7 +58,7 @@ int delete_char_command(int key, buffer_t *buffer, env_t *env)
|
||||
|
||||
int newline_command(int key, buffer_t *buffer, env_t *env)
|
||||
{
|
||||
int ret;
|
||||
int ret = 0;
|
||||
|
||||
env->vars = my_unsetenv(env->vars, "eof");
|
||||
if (env->window)
|
||||
|
||||
@@ -28,6 +28,7 @@ const builtin builtins[] = {
|
||||
{"alias", &builtin_alias},
|
||||
{"unalias", &builtin_unalias},
|
||||
{"echo", &builtin_echo},
|
||||
{"if", &builtin_if},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user