mirror of
https://github.com/zoriya/ash.git
synced 2025-12-06 06:36:17 +00:00
Merge branch 'master' of github.com:AnonymusRaccoon/ash into fixalias
This commit is contained in:
2
Makefile
2
Makefile
@@ -26,6 +26,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 \
|
||||
|
||||
@@ -30,6 +30,7 @@ int builtin_alias(char **args, env_t *env);
|
||||
int builtin_unalias(char **args, env_t *env);
|
||||
int builtin_bindkey(char **argv, env_t *env);
|
||||
int builtin_echo(char **args, env_t *env);
|
||||
int builtin_if(char **argv, env_t *env);
|
||||
int builtin_set(char **argv, env_t *env);
|
||||
int builtin_unset(char **argv, env_t *env);
|
||||
|
||||
@@ -65,4 +66,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
|
||||
char *get_expr(char **argv);
|
||||
int get_test_return_value(char **argv, env_t *env);
|
||||
int get_max_cmd_len(char **argv);
|
||||
96
src/builtin/builtin_if.c
Normal file
96
src/builtin/builtin_if.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2020
|
||||
** 42sh
|
||||
** File description:
|
||||
** if builtin
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "shell.h"
|
||||
#include "builtin.h"
|
||||
|
||||
int matched_parenthesis(char **argv)
|
||||
{
|
||||
int left_p = 0;
|
||||
int right_p = 0;
|
||||
|
||||
for (int i = 0; argv[i]; i++)
|
||||
for (int j = 0; argv[i][j]; j++) {
|
||||
left_p = argv[i][j] == '(' ? left_p + 1 : left_p;
|
||||
right_p = argv[i][j] == ')' ? right_p + 1 : right_p;
|
||||
}
|
||||
if (left_p > right_p)
|
||||
printf("Too many ('s.\n");
|
||||
if (right_p > left_p)
|
||||
printf("Too many )'s.\n");
|
||||
if (left_p == right_p)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
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_if_cmd(char **argv)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
for (; argv[len]; len++);
|
||||
if (len < 1) {
|
||||
printf("if: Too few arguments\n");
|
||||
return (0);
|
||||
}
|
||||
if (!matched_parenthesis(argv))
|
||||
return (0);
|
||||
if (argv[1][0] != '(') {
|
||||
printf("if: Expression Syntax.\n");
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int builtin_if(char **argv, env_t *env)
|
||||
{
|
||||
if (is_valid_if_cmd(argv)) {
|
||||
execute_one_liner(argv, env);
|
||||
env->vars = my_setenv(env->vars, "?", "0");
|
||||
} else
|
||||
env->vars = my_setenv(env->vars, "?", "1");
|
||||
return (0);
|
||||
}
|
||||
46
src/builtin/builtin_if_two.c
Normal file
46
src/builtin/builtin_if_two.c
Normal file
@@ -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);
|
||||
}
|
||||
@@ -47,9 +47,9 @@ int builtin_cd(char **argv, env_t *env)
|
||||
char *old = getcwd(NULL, 0);
|
||||
|
||||
if (get_argc(argv) > 2) {
|
||||
write(2, "cd: Too many arguments.\n", 25);
|
||||
dprintf(2, "cd: Too many arguments.\n");
|
||||
env->vars = my_setenv(env->vars, "?", "1");
|
||||
return (0);
|
||||
return (-1 * !env->window);
|
||||
}
|
||||
env->vars = my_setenv(env->vars, "?", "0");
|
||||
if (!argv[1])
|
||||
@@ -59,6 +59,7 @@ int builtin_cd(char **argv, env_t *env)
|
||||
if (chdir(path) < 0) {
|
||||
printf("%s: %s.\n", path, strerror(errno));
|
||||
env->vars = my_setenv(env->vars, "?", "1");
|
||||
return (-1 * !env->window);
|
||||
} else
|
||||
env->env = my_setenv(env->env, "OLDPWD", old);
|
||||
free(old);
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include <string.h>
|
||||
#include "utility.h"
|
||||
|
||||
|
||||
char *my_getenv(char **env, char *name)
|
||||
{
|
||||
if (!env)
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <malloc.h>
|
||||
|
||||
char *strdup(const char *);
|
||||
int dprintf(int, const char *, ...);
|
||||
|
||||
int count_trailing_spaces(char *cmd)
|
||||
{
|
||||
@@ -42,6 +43,7 @@ char **remove_leading_entries(char **cmds)
|
||||
char *process_aliases(char *cmd, env_t *env)
|
||||
{
|
||||
int bin_len;
|
||||
int len;
|
||||
|
||||
for (bin_len = 0; cmd[bin_len]; bin_len++) {
|
||||
if (cmd[bin_len] == ' ' || cmd[bin_len] == '\t')
|
||||
@@ -49,10 +51,11 @@ char *process_aliases(char *cmd, env_t *env)
|
||||
}
|
||||
for (alias_t *al = env->alias; al; al = al->next) {
|
||||
if (!strncmp(al->alias, cmd, bin_len)) {
|
||||
cmd = realloc(cmd, strlen(cmd) + strlen(al->command) + 1 - bin_len);
|
||||
len = strlen(cmd) + strlen(al->command) + 1 - bin_len;
|
||||
rm_n_char(cmd, bin_len);
|
||||
cmd = realloc(cmd, len);
|
||||
if (!cmd)
|
||||
return (NULL);
|
||||
rm_n_char(cmd, bin_len);
|
||||
insert_substring(cmd, al->command, 1);
|
||||
}
|
||||
}
|
||||
@@ -66,8 +69,17 @@ char **parse_input(char *cmd, env_t *env, wordexp_t *parser)
|
||||
if (!(cmd = process_vars(cmd, env)))
|
||||
return (NULL);
|
||||
cmd = process_aliases(cmd, env);
|
||||
if (wordexp(cmd, parser, WRDE_SHOWERR)) {
|
||||
perror(SHELL_NAME);
|
||||
switch (wordexp(cmd, parser, WRDE_SHOWERR)) {
|
||||
case 0:
|
||||
break;
|
||||
case WRDE_BADCHAR:
|
||||
dprintf(2, "Illegal occurrence of one of |, &, ;, <, >, (, ), {, }.\n");
|
||||
return (NULL);
|
||||
case WRDE_SYNTAX:
|
||||
dprintf(2, "Shell syntax error\n");
|
||||
return (NULL);
|
||||
default:
|
||||
dprintf(2, "Unknonw parsing error.\n");
|
||||
return (NULL);
|
||||
}
|
||||
free(cmd);
|
||||
|
||||
@@ -29,6 +29,7 @@ const builtin builtins[] = {
|
||||
{"unalias", &builtin_unalias},
|
||||
{"bindkey", &builtin_bindkey},
|
||||
{"echo", &builtin_echo},
|
||||
{"if", &builtin_if},
|
||||
{"set", &builtin_set},
|
||||
{"unset", &builtin_unset},
|
||||
{NULL, NULL}
|
||||
|
||||
@@ -278,16 +278,6 @@ TESTS=
|
||||
echo 'echo "$"'
|
||||
[928-END]
|
||||
|
||||
[929]
|
||||
NAME="929"
|
||||
SETUP=""
|
||||
CLEAN=""
|
||||
TESTS=
|
||||
echo 'echo "\t"'
|
||||
echo 'echo $'
|
||||
echo 'echo $?'
|
||||
[929-END]
|
||||
|
||||
[930]
|
||||
NAME="930"
|
||||
SETUP=""
|
||||
|
||||
Reference in New Issue
Block a user