Compare commits

...

5 Commits
1.1.0 ... main

Author SHA1 Message Date
starnakin
ac03fb9ac1 update bozolib 2023-12-02 19:02:39 +01:00
Alexis
fd31384dca
Update README.md 2023-09-21 10:22:41 +02:00
starnakin
d311595352 fix: add ~ as env var name delimiter 2023-07-05 21:14:09 +02:00
starnakin
a5b42a6c7f clean: remove trash file 2023-07-05 21:12:25 +02:00
starnakin
a36e2a9bc4 add: env var iterpretation 2023-07-05 21:12:04 +02:00
9 changed files with 96 additions and 12 deletions

View File

@ -13,7 +13,7 @@ Il suffit de télécharger une release qui possède un fichier précompilé
```
mais il est aussi possible de le compiler soit même ! (à la racine)
```bash
git clone --recursive
git clone --recursive https://github.com/Guamss/zzsh.git
make
```
### /!\ Note /!\

@ -1 +1 @@
Subproject commit 6d2b0a74c4c1c7a2bc62aa8797c504e42f5c7721
Subproject commit 3ed6ae7c279f0583a0ddbd1e19f7f7a151369ca0

View File

@ -40,11 +40,12 @@ lst** aliases_init(lst** env)
close(fd);
fd = open(file_path, O_RDONLY);
char buffer[buff_size];
buffer[0] = '\0';
read(fd, buffer, buff_size);
char** assignation;
char** line = split_quoted_charset(buffer, "\n");
aliases = malloc(sizeof(lst*));
*aliases = NULL;
aliases = malloc(sizeof(lst*));
*aliases = NULL;
for(int i = 0; line[i]!=NULL; i++)
{
if (strchr(line[i], '=') != NULL)
@ -53,7 +54,7 @@ lst** aliases_init(lst** env)
add_alias(aliases, assignation[0], assignation[1]);
}
}
return aliases;
return aliases;
}
int add_alias(lst** root, const char* key, const char* value)

73
src/env/env.c vendored
View File

@ -1,7 +1,4 @@
#include "env.h"
#include "../../lib/bozolib/bozolib.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
void env_del(void *ptr)
@ -174,3 +171,73 @@ char **get_env_str(lst** root)
tab[i] = NULL;
return tab;
}
static char* get_key(const char* key_start)
{
if (strncmp(key_start, "~", 1) == 0)
return strdup("~");
size_t i = 1;
while (key_start[i] != '\0' && strchr("\t $\"\'~", key_start[i]) == NULL)
i++;
return (strndup(key_start, i));
}
static char* get_value(data_t* data, const char* key)
{
char *out;
if (strcmp(key, "~") == 0)
out = get_env_variable(data->env, "HOME");
else if (strcmp(key, "$?") == 0)
out = itoA(data->status_code);
else
out = get_env_variable(data->env, key + 1);
if (out == NULL)
out = "";
return out;
}
char* interpret_env_var(data_t *data, const char* str)
{
char symbols[3] = "$~";
char* out;
char* key;
char* value;
char* tmp2;
const char* tmp;
out = strdup(str);
if (out == NULL)
return NULL;
for (size_t i = 0; symbols[i] != '\0'; i++)
{
tmp = strchr(out,symbols[i]);
while (tmp != NULL && is_in_quote(tmp, str - tmp) == 1)
tmp = strchr(tmp, symbols[i]);
while (tmp != NULL)
{
if (tmp == NULL)
continue;
key = get_key(tmp);
if (key == NULL)
{
free(out);
return NULL;
}
value = get_value(data, key);
if (value == NULL)
{
free(key);
free(out);
return NULL;
}
tmp2 = str_replace(out, value, tmp - out, tmp - out + strlen(key));
free(key);
free(out);
out = tmp2;
tmp = strchr(out,symbols[i]);
while (tmp != NULL && is_in_quote(tmp, str - tmp) == 1)
tmp = strchr(tmp, symbols[i]);
}
}
return out;
}

8
src/env/env.h vendored
View File

@ -1,5 +1,12 @@
#pragma once
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../lib/bozolib/bozolib.h"
#include "../data/data.h"
#include "../utils/utils.h"
typedef struct s_env
{
@ -13,4 +20,5 @@ void env_del(void *ptr);
char *get_env_variable(lst** root, const char* key);
int add_env_variable(lst** root, const char *key, const char *value);
int edit_env_variable(lst** root, const char *key, const char *new_value);
char* interpret_env_var(data_t*, const char*);
char** get_env_str(lst** root);

1
src/env/envs.c vendored
View File

@ -1 +0,0 @@

View File

@ -35,6 +35,7 @@ int main(int ac, char **av, char **env_str)
lst_clear(data.env, &env_del);
return 1;
}
data.status_code = 0;
signal(SIGINT, ctrlc);
signal(SIGQUIT, SIG_IGN);
line = get_user_input(&data);
@ -56,7 +57,7 @@ int main(int ac, char **av, char **env_str)
}
line = get_user_input(&data);
}
alias_save(data.aliases, data.env);
alias_save(data.aliases, data.env);
lst_clear(data.aliases, &alias_del);
lst_clear(data.env, &env_del);
return (0);

View File

@ -14,9 +14,16 @@ int parsing_cmd(char *str, cmd_t* command, data_t *data)
char* tmp;
if (cmd_init(command))
return 1;
if (get_redirections(str, command))
tmp = interpret_env_var(data, str);
if (tmp == NULL)
return 1;
command->args = split_quoted_charset(str, "\t ");
if (get_redirections(tmp, command))
{
free(tmp);
return 1;
}
command->args = split_quoted_charset(tmp, "\t ");
free(tmp);
if (command->args == NULL)
return 1;
for (size_t i = 0; command->args[i] != NULL; i++)

View File

@ -5,5 +5,6 @@
#include "../redirection/redirection.h"
#include "../data/data.h"
#include "../alias/alias.h"
#include "../env/env.h"
lst*** parsing(const char* str, data_t *data);