add: alias
This commit is contained in:
parent
b0c936e8ec
commit
b073baa06b
61
src/alias/alias.c
Normal file
61
src/alias/alias.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include "../../lib/bozolib/bozolib.h"
|
||||
#include "./alias.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
lst** aliases_init()
|
||||
{
|
||||
lst** aliases;
|
||||
|
||||
aliases = malloc(sizeof(lst*));
|
||||
*aliases = NULL;
|
||||
return aliases;
|
||||
}
|
||||
|
||||
int add_alias(lst** root, const char* key, const char* value)
|
||||
{
|
||||
lst* nouveau;
|
||||
alias_t* alias;
|
||||
|
||||
alias = malloc(sizeof(alias_t));
|
||||
if (alias == NULL)
|
||||
return 1;
|
||||
nouveau = malloc(sizeof(lst));
|
||||
if (nouveau == NULL)
|
||||
{
|
||||
free(alias);
|
||||
return 1;
|
||||
}
|
||||
alias->key = strdup(key);
|
||||
if (alias->key == NULL)
|
||||
{
|
||||
free(alias->key);
|
||||
free(alias);
|
||||
return 1;
|
||||
}
|
||||
alias->value = strdup(value);
|
||||
if (alias->key == NULL)
|
||||
{
|
||||
free(alias->key);
|
||||
free(alias->value);
|
||||
free(alias);
|
||||
return 1;
|
||||
}
|
||||
nouveau->content = alias;
|
||||
lst_addback(root, nouveau);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* get_alias(lst** aliases, const char* key)
|
||||
{
|
||||
lst* current = *aliases;
|
||||
alias_t* content;
|
||||
|
||||
while (current != NULL)
|
||||
{
|
||||
content = current->content;
|
||||
if (strcmp(key, content->key) == 0)
|
||||
return content->value;
|
||||
current = current->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
14
src/alias/alias.h
Normal file
14
src/alias/alias.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../lib/bozolib/bozolib.h"
|
||||
|
||||
typedef struct s_alias
|
||||
{
|
||||
char* key;
|
||||
char* value;
|
||||
} alias_t;
|
||||
|
||||
lst** aliases_init();
|
||||
lst** aliases_save(lst** aliases);
|
||||
char* get_alias(lst** aliases, const char* key);
|
||||
int add_alias(lst** root, const char* key, const char* value);
|
@ -1,18 +1,8 @@
|
||||
#include <sys/wait.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "../cmd/cmd.h"
|
||||
#include <unistd.h>
|
||||
#include "../env/env.h"
|
||||
#include "../utils/utils.h"
|
||||
#include "../../lib/bozolib/bozolib.h"
|
||||
#include "../exec/exec.h"
|
||||
#include "./builtin.h"
|
||||
|
||||
char* builtin_path(const char* executable)
|
||||
{
|
||||
if (strcmp(executable, "cd") == 0 || strcmp(executable, "where") == 0);
|
||||
if (strcmp(executable, "cd") == 0 || strcmp(executable, "where") == 0 || strcmp(executable, "alias") == 0);
|
||||
else
|
||||
return NULL;
|
||||
return strdup(executable);
|
||||
@ -22,12 +12,12 @@ int change_directory(char** args, lst** env)
|
||||
{
|
||||
char cwd[PATH_MAX];
|
||||
char* oldpwd = get_env_variable(env, "PWD");
|
||||
if(len((void**)args)==1)
|
||||
if (tablen((const void**)args)==1)
|
||||
{
|
||||
char* path = get_env_variable(env, "HOME");
|
||||
chdir(path);
|
||||
}
|
||||
else if (len((void**)args)>2)
|
||||
else if (tablen((const void**)args)>2)
|
||||
{
|
||||
dprintf(2, "cd : Trop d'arguments!\n");
|
||||
return 1;
|
||||
@ -45,7 +35,7 @@ int change_directory(char** args, lst** env)
|
||||
|
||||
int where(char** args, lst** env)
|
||||
{
|
||||
if (len((void**)args)==1)
|
||||
if (tablen((const void**)args)==1)
|
||||
{
|
||||
dprintf(2, "where : Il faut au moins 1 argument!\n");
|
||||
return 1;
|
||||
@ -65,18 +55,20 @@ int where(char** args, lst** env)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int builtin_execute(cmd* input, lst** env)
|
||||
int builtin_execute(cmd* input, data_t *data, int fd_in, int fd_out)
|
||||
{
|
||||
if (strcmp(input->executable, "cd") == 0)
|
||||
{
|
||||
change_directory(input->args, env);
|
||||
change_directory(input->args, data->env);
|
||||
return 0;
|
||||
}
|
||||
else if(strcmp(input->executable, "where") == 0)
|
||||
{
|
||||
where(input->args, env);
|
||||
where(input->args, data->env);
|
||||
return 0;
|
||||
}
|
||||
else if (strcmp(input->executable, "alias") == 0)
|
||||
return builtin_alias(fd_in, fd_out, data->aliases, input->args);
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
|
@ -1,9 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../env/env.h"
|
||||
#include "../cmd/cmd.h"
|
||||
#include "../data/data.h"
|
||||
#include "../alias/alias.h"
|
||||
#include "../exec/exec.h"
|
||||
#include "../../lib/bozolib/bozolib.h"
|
||||
|
||||
char* builtin_path(const char* executable);
|
||||
int builtin_execute(cmd* input, data_t* data, int fd_in, int fd_out);
|
||||
|
||||
int builtin_alias(int fd_in, int fd_out, lst** aliases, char** args);
|
||||
int change_directory(char** args, lst** env);
|
||||
int where(char** args);
|
||||
int builtin_execute(cmd* input, lst** env);
|
||||
int where(char** args, lst** env);
|
||||
|
||||
|
49
src/builtin-exec/builtin_alias.c
Normal file
49
src/builtin-exec/builtin_alias.c
Normal file
@ -0,0 +1,49 @@
|
||||
#include "./builtin.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static void print_aliases(lst** aliases, int fd_out)
|
||||
{
|
||||
lst* current = *aliases;
|
||||
alias_t* content;
|
||||
|
||||
while (current != NULL)
|
||||
{
|
||||
content = current->content;
|
||||
dprintf(fd_out, "%s='%s'\n", content->key, content->value);
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
int builtin_alias(int fd_in, int fd_out, lst** aliases, char **args)
|
||||
{
|
||||
const char* equal;
|
||||
char* result;
|
||||
char* key;
|
||||
(void)fd_in;
|
||||
if (tablen((const void**) args) == 1)
|
||||
print_aliases(aliases, fd_out);
|
||||
for (size_t i = 1; args[i] != NULL; i++)
|
||||
{
|
||||
equal = strchr(args[i], '=');
|
||||
if (equal == NULL)
|
||||
{
|
||||
result = get_alias(aliases, args[i]);
|
||||
if (result != NULL)
|
||||
dprintf(fd_out, "%s=%s\n", args[i], result);
|
||||
}
|
||||
else
|
||||
{
|
||||
key = strndup(args[i], equal - args[i]);
|
||||
if (key == NULL)
|
||||
return 1;
|
||||
if (add_alias(aliases, key, equal + 1))
|
||||
{
|
||||
free(key);
|
||||
return 1;
|
||||
}
|
||||
free(key);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
4
src/config.h
Normal file
4
src/config.h
Normal file
@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#define ZZSH_HISTORY_PATH "~/.config/history"
|
||||
#define ALIASES_SAVE_PATH "~/.config/aliases"
|
11
src/data/data.h
Normal file
11
src/data/data.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../lib/bozolib/bozolib.h"
|
||||
|
||||
typedef struct s_data
|
||||
{
|
||||
const char* computor_name;
|
||||
int status_code;
|
||||
lst** env;
|
||||
lst** aliases;
|
||||
} data_t;
|
@ -9,13 +9,7 @@
|
||||
#include "../utils/utils.h"
|
||||
#include "../../lib/bozolib/bozolib.h"
|
||||
#include "../builtin-exec/builtin.h"
|
||||
|
||||
int len(void** list)
|
||||
{
|
||||
int index;
|
||||
for (index = 0; list[index]!=NULL; index++);
|
||||
return index;
|
||||
}
|
||||
#include "../data/data.h"
|
||||
|
||||
int execute(cmd* input, lst** env)
|
||||
{
|
||||
@ -78,7 +72,7 @@ char* get_executable_path(const char* executable, lst** env)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int cmds_list_exec(lst** cmds, lst** env)
|
||||
int cmds_list_exec(lst** cmds, data_t *data)
|
||||
{
|
||||
lst* current = *cmds;
|
||||
cmd* content;
|
||||
@ -97,8 +91,8 @@ int cmds_list_exec(lst** cmds, lst** env)
|
||||
;
|
||||
else if (content->executable == NULL)
|
||||
dprintf(2, "zzsh: command not found: %s\n", content->args[0]);
|
||||
else if (builtin_execute(content, env) == 1)
|
||||
execute(content, env);
|
||||
else if (builtin_execute(content, data, fds[0], fds[1]) == 1)
|
||||
execute(content, data->env);
|
||||
current = current->next;
|
||||
}
|
||||
return 0;
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
#include "../cmd/cmd.h"
|
||||
#include "../../lib/bozolib/bozolib.h"
|
||||
#include "../data/data.h"
|
||||
|
||||
int cmds_list_exec(lst** cmds, lst** env);
|
||||
int cmds_list_exec(lst** cmds, data_t *data);
|
||||
int execute(cmd* input, lst** env);
|
||||
int builtin_execute(cmd* input, lst** env);
|
||||
int len(void** list);
|
||||
int builtin_execute(cmd* input, data_t* data, int fd_int, int fd_out);
|
||||
char* get_executable_path(const char* executable, lst** env);
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "./input.h"
|
||||
#include <readline/readline.h>
|
||||
|
||||
static char* get_prompt(lst** env)
|
||||
static char* get_prompt(data_t *data)
|
||||
{
|
||||
char* out;
|
||||
char cwd[PATH_MAX];
|
||||
@ -18,8 +18,8 @@ static char* get_prompt(lst** env)
|
||||
{
|
||||
*first_line_return = '\0';
|
||||
}
|
||||
char* user = get_env_variable(env, "USER");
|
||||
char* home = get_env_variable(env, "HOME");
|
||||
char* user = get_env_variable(data->env, "USER");
|
||||
char* home = get_env_variable(data->env, "HOME");
|
||||
if (strncmp(home, cwd, strlen(home)) == 0)
|
||||
{
|
||||
strcpy(cwd, "~");
|
||||
@ -58,12 +58,12 @@ static void finisher(char** strs)
|
||||
}
|
||||
}
|
||||
|
||||
char *get_user_input(lst** env)
|
||||
char *get_user_input(data_t *data)
|
||||
{
|
||||
char *prompt;
|
||||
char *input;
|
||||
|
||||
prompt = get_prompt(env);
|
||||
prompt = get_prompt(data);
|
||||
if (prompt == NULL)
|
||||
return NULL;
|
||||
input = readline(prompt);
|
||||
|
@ -10,5 +10,6 @@
|
||||
#include "../../lib/bozolib/bozolib.h"
|
||||
#include "../utils/utils.h"
|
||||
#include "../env/env.h"
|
||||
#include "../data/data.h"
|
||||
|
||||
char *get_user_input(lst** env);
|
||||
char *get_user_input(data_t *data);
|
||||
|
26
src/main.c
26
src/main.c
@ -2,9 +2,11 @@
|
||||
#include "./input/input.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include "alias/alias.h"
|
||||
#include "cmd/cmd.h"
|
||||
#include "./exec/exec.h"
|
||||
#include "./parsing/parsing.h"
|
||||
#include "./data/data.h"
|
||||
|
||||
void cmds_list_destroyer(lst*** cmds_list)
|
||||
{
|
||||
@ -16,34 +18,40 @@ void cmds_list_destroyer(lst*** cmds_list)
|
||||
int main(int ac, char **av, char **env_str)
|
||||
{
|
||||
char *line;
|
||||
lst** env;
|
||||
data_t data;
|
||||
lst*** cmds_list;
|
||||
|
||||
(void) av;
|
||||
(void) ac;
|
||||
env = env_init((const char **) env_str);
|
||||
if (env == NULL)
|
||||
data.env = env_init((const char **) env_str);
|
||||
if (data.env == NULL)
|
||||
return (1);
|
||||
line = get_user_input(env);
|
||||
data.aliases = aliases_init();
|
||||
if (data.aliases == NULL)
|
||||
{
|
||||
lst_clear(data.env, &env_del);
|
||||
return 1;
|
||||
}
|
||||
line = get_user_input(&data);
|
||||
while (line != NULL)
|
||||
{
|
||||
cmds_list = parsing(line, env);
|
||||
cmds_list = parsing(line, &data);
|
||||
free(line);
|
||||
if (cmds_list != NULL)
|
||||
{
|
||||
for (size_t i = 0; cmds_list[i] != NULL; i++)
|
||||
{
|
||||
if (cmds_list_exec(cmds_list[i], env))
|
||||
if (cmds_list_exec(cmds_list[i], &data))
|
||||
{
|
||||
lst_clear(env, &env_del);
|
||||
lst_clear(data.env, &env_del);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
cmds_list_destroyer(cmds_list);
|
||||
}
|
||||
line = get_user_input(env);
|
||||
line = get_user_input(&data);
|
||||
}
|
||||
lst_clear(env, &env_del);
|
||||
lst_clear(data.env, &env_del);
|
||||
return (0);
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,24 @@
|
||||
#include "parsing.h"
|
||||
char* parsing_executable(const char* executable, lst** env)
|
||||
|
||||
char* parsing_executable(const char* executable, data_t *data)
|
||||
{
|
||||
const char* tmp;
|
||||
if (executable == NULL)
|
||||
return NULL;
|
||||
if (strchr("./", executable[0]))
|
||||
return strdup(executable);
|
||||
return (get_executable_path(executable, env));
|
||||
if (executable[0] == '\\')
|
||||
tmp = executable + 1;
|
||||
else
|
||||
{
|
||||
tmp = get_alias(data->aliases, executable);
|
||||
if (tmp == NULL)
|
||||
tmp = executable;
|
||||
}
|
||||
if (strchr("./", tmp[0]))
|
||||
return strdup(tmp);
|
||||
return (get_executable_path(tmp, data->env));
|
||||
}
|
||||
|
||||
int parsing_cmd(char *str, cmd* command, lst** env)
|
||||
int parsing_cmd(char *str, cmd* command, data_t *data)
|
||||
{
|
||||
if (get_redirections(str, command))
|
||||
return 1;
|
||||
@ -17,11 +27,11 @@ int parsing_cmd(char *str, cmd* command, lst** env)
|
||||
return 1;
|
||||
for (size_t i = 0; command->args[i]; i++)
|
||||
quote_remover(command->args[i]);
|
||||
command->executable = parsing_executable(command->args[0], env);
|
||||
command->executable = parsing_executable(command->args[0], data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
lst **parsing_pipe(const char *str, lst** env)
|
||||
lst **parsing_pipe(const char *str, data_t *data)
|
||||
{
|
||||
char** cmds_str;
|
||||
lst** cmds;
|
||||
@ -39,7 +49,7 @@ lst **parsing_pipe(const char *str, lst** env)
|
||||
current = *cmds;
|
||||
for (size_t i = 0; cmds_str[i] != NULL; i++)
|
||||
{
|
||||
if (parsing_cmd(cmds_str[i], current->content, env))
|
||||
if (parsing_cmd(cmds_str[i], current->content, data))
|
||||
{
|
||||
tab_free((void**)cmds_str);
|
||||
return NULL;
|
||||
@ -50,7 +60,7 @@ lst **parsing_pipe(const char *str, lst** env)
|
||||
return (cmds);
|
||||
}
|
||||
|
||||
lst*** parsing(const char *line, lst** env)
|
||||
lst*** parsing(const char *line, data_t *data)
|
||||
{
|
||||
char** line_commas;
|
||||
lst*** tab;
|
||||
@ -67,7 +77,7 @@ lst*** parsing(const char *line, lst** env)
|
||||
size_t i;
|
||||
for (i = 0; line_commas[i] != NULL; i++)
|
||||
{
|
||||
tab[i] = parsing_pipe(line_commas[i], env);
|
||||
tab[i] = parsing_pipe(line_commas[i], data);
|
||||
if (tab[i] == NULL)
|
||||
{
|
||||
tab_free((void**) line_commas);
|
||||
|
@ -3,4 +3,7 @@
|
||||
#include "../exec/exec.h"
|
||||
#include "../../lib/bozolib/bozolib.h"
|
||||
#include "../redirection/redirection.h"
|
||||
lst*** parsing(const char* str, lst** env);
|
||||
#include "../data/data.h"
|
||||
#include "../alias/alias.h"
|
||||
|
||||
lst*** parsing(const char* str, data_t *data);
|
||||
|
Loading…
Reference in New Issue
Block a user