Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
ac03fb9ac1 | ||
![]() |
fd31384dca | ||
![]() |
d311595352 | ||
![]() |
a5b42a6c7f | ||
![]() |
a36e2a9bc4 | ||
![]() |
e15216ce85 | ||
![]() |
96f7108555 | ||
![]() |
e9e80c192e | ||
![]() |
3cc5d4cdce | ||
![]() |
0e4fa1d6db | ||
![]() |
e4099c2621 | ||
![]() |
f1b1ff0ede | ||
![]() |
a36b4ecdab | ||
![]() |
449bc574b1 | ||
![]() |
867d239d1a | ||
![]() |
1d322cf4d3 | ||
![]() |
db60e7adba | ||
![]() |
98341b62bc | ||
![]() |
52612b9aa5 | ||
![]() |
b073baa06b | ||
![]() |
b0c936e8ec | ||
![]() |
b30bda13dd | ||
![]() |
15eada8c35 |
@ -7,13 +7,13 @@ que des variables d'environnement dans un terminal.
|
|||||||
|
|
||||||
## Intallation :
|
## Intallation :
|
||||||
|
|
||||||
Il suffit de clone le repos, celui-ci possède un fichier déjà compilé
|
Il suffit de télécharger une release qui possède un fichier précompilé
|
||||||
```bash
|
```bash
|
||||||
./zzsh
|
./zzsh
|
||||||
```
|
```
|
||||||
mais il est aussi possible de le compiler soit même ! (à la racine)
|
mais il est aussi possible de le compiler soit même ! (à la racine)
|
||||||
```bash
|
```bash
|
||||||
git clone --recursive
|
git clone --recursive https://github.com/Guamss/zzsh.git
|
||||||
make
|
make
|
||||||
```
|
```
|
||||||
### /!\ Note /!\
|
### /!\ Note /!\
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 6d2b0a74c4c1c7a2bc62aa8797c504e42f5c7721
|
Subproject commit 3ed6ae7c279f0583a0ddbd1e19f7f7a151369ca0
|
142
src/alias/alias.c
Normal file
142
src/alias/alias.c
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
#include "../../lib/bozolib/bozolib.h"
|
||||||
|
#include "./alias.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "../env/env.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static char* get_zzsh_aliases(lst** env)
|
||||||
|
{
|
||||||
|
char* home;
|
||||||
|
ssize_t file_name_size;
|
||||||
|
char* file_path;
|
||||||
|
ssize_t size_file_path;
|
||||||
|
file_name_size = strlen("/.zzsh_aliases");
|
||||||
|
home = get_env_variable(env, "HOME");
|
||||||
|
size_file_path = strlen(home)+file_name_size;
|
||||||
|
file_path = malloc((size_file_path+1)*sizeof(char));
|
||||||
|
strcpy(file_path, home);
|
||||||
|
strcat(file_path, "/.zzsh_aliases");
|
||||||
|
return file_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
lst** aliases_init(lst** env)
|
||||||
|
{
|
||||||
|
lst** aliases;
|
||||||
|
int fd;
|
||||||
|
char* file_path = get_zzsh_aliases(env);
|
||||||
|
fd = open(file_path, O_CREAT, 0644);
|
||||||
|
if (fd == -1)
|
||||||
|
dprintf(2, "Erreur lors la création du fichier .zzsh_aliases\n");
|
||||||
|
close(fd);
|
||||||
|
fd = open(file_path, O_RDONLY);
|
||||||
|
if (fd == -1)
|
||||||
|
dprintf(2, "Erreur lors de la lecture du fichier .zzsh_aliases\n");
|
||||||
|
int buff_size;
|
||||||
|
buff_size = lseek(fd, 0, SEEK_END);
|
||||||
|
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;
|
||||||
|
for(int i = 0; line[i]!=NULL; i++)
|
||||||
|
{
|
||||||
|
if (strchr(line[i], '=') != NULL)
|
||||||
|
{
|
||||||
|
assignation = split_quoted_charset(line[i], "=");
|
||||||
|
add_alias(aliases, assignation[0], assignation[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void alias_del(void *ptr)
|
||||||
|
{
|
||||||
|
alias_t *alias = ptr;
|
||||||
|
free(alias->key);
|
||||||
|
free(alias->value);
|
||||||
|
free(alias);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 alias_save(lst** aliases, lst** env)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char* file_path = get_zzsh_aliases(env);
|
||||||
|
fd = open(file_path, O_WRONLY);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
dprintf(2, "Une erreur est survenue lors de l'écriture des aliases\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
print_aliases(aliases, fd);
|
||||||
|
return 0;
|
||||||
|
}
|
17
src/alias/alias.h
Normal file
17
src/alias/alias.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#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);
|
||||||
|
void alias_del(void *ptr);
|
||||||
|
void print_aliases(lst** aliases, int fd_out);
|
||||||
|
char* get_alias(lst** aliases, const char* key);
|
||||||
|
int add_alias(lst** root, const char* key, const char* value);
|
||||||
|
int alias_save(lst** aliases, lst** env);
|
@ -1,18 +1,8 @@
|
|||||||
#include <sys/wait.h>
|
#include "./builtin.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"
|
|
||||||
|
|
||||||
char* builtin_path(const char* executable)
|
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
|
else
|
||||||
return NULL;
|
return NULL;
|
||||||
return strdup(executable);
|
return strdup(executable);
|
||||||
@ -22,12 +12,12 @@ int change_directory(char** args, lst** env)
|
|||||||
{
|
{
|
||||||
char cwd[PATH_MAX];
|
char cwd[PATH_MAX];
|
||||||
char* oldpwd = get_env_variable(env, "PWD");
|
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");
|
char* path = get_env_variable(env, "HOME");
|
||||||
chdir(path);
|
chdir(path);
|
||||||
}
|
}
|
||||||
else if (len((void**)args)>2)
|
else if (tablen((const void**)args)>2)
|
||||||
{
|
{
|
||||||
dprintf(2, "cd : Trop d'arguments!\n");
|
dprintf(2, "cd : Trop d'arguments!\n");
|
||||||
return 1;
|
return 1;
|
||||||
@ -45,7 +35,7 @@ int change_directory(char** args, lst** env)
|
|||||||
|
|
||||||
int where(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");
|
dprintf(2, "where : Il faut au moins 1 argument!\n");
|
||||||
return 1;
|
return 1;
|
||||||
@ -65,18 +55,22 @@ int where(char** args, lst** env)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int builtin_execute(cmd* input, lst** env)
|
int builtin_execute(data_t* data, cmd_t* input)
|
||||||
{
|
{
|
||||||
|
if (input->executable == NULL)
|
||||||
|
return 1;
|
||||||
if (strcmp(input->executable, "cd") == 0)
|
if (strcmp(input->executable, "cd") == 0)
|
||||||
{
|
{
|
||||||
change_directory(input->args, env);
|
change_directory(input->args, data->env);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else if(strcmp(input->executable, "where") == 0)
|
else if(strcmp(input->executable, "where") == 0)
|
||||||
{
|
{
|
||||||
where(input->args, env);
|
where(input->args, data->env);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
else if (strcmp(input->executable, "alias") == 0)
|
||||||
|
return builtin_alias(data, input);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1,9 +1,20 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "../env/env.h"
|
||||||
#include "../cmd/cmd.h"
|
#include "../cmd/cmd.h"
|
||||||
|
#include "../data/data.h"
|
||||||
|
#include "../alias/alias.h"
|
||||||
|
#include "../exec/exec.h"
|
||||||
#include "../../lib/bozolib/bozolib.h"
|
#include "../../lib/bozolib/bozolib.h"
|
||||||
|
|
||||||
char* builtin_path(const char* executable);
|
char* builtin_path(const char* executable);
|
||||||
|
int builtin_execute(data_t* data, cmd_t* cmd);
|
||||||
|
|
||||||
|
int builtin_alias(data_t* data, cmd_t* cmd);
|
||||||
int change_directory(char** args, lst** env);
|
int change_directory(char** args, lst** env);
|
||||||
int where(char** args);
|
int where(char** args, lst** env);
|
||||||
int builtin_execute(cmd* input, lst** env);
|
|
||||||
|
36
src/builtin-exec/builtin_alias.c
Normal file
36
src/builtin-exec/builtin_alias.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include "./builtin.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int builtin_alias(data_t* data, cmd_t* cmd)
|
||||||
|
{
|
||||||
|
const char* equal;
|
||||||
|
char* result;
|
||||||
|
char* key;
|
||||||
|
if (tablen((const void**) cmd->args) == 1)
|
||||||
|
print_aliases(data->aliases, cmd->output[0]);
|
||||||
|
for (size_t i = 1; cmd->args[i] != NULL; i++)
|
||||||
|
{
|
||||||
|
equal = strchr(cmd->args[i], '=');
|
||||||
|
if (equal == NULL)
|
||||||
|
{
|
||||||
|
result = get_alias(data->aliases, cmd->args[i]);
|
||||||
|
if (result != NULL)
|
||||||
|
dprintf(cmd->output[0], "%s=%s\n", cmd->args[i], result);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
key = strndup(cmd->args[i], equal - cmd->args[i]);
|
||||||
|
if (key == NULL)
|
||||||
|
return 1;
|
||||||
|
if (add_alias(data->aliases, key, equal + 1))
|
||||||
|
{
|
||||||
|
free(key);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
free(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
@ -2,9 +2,38 @@
|
|||||||
|
|
||||||
void cmd_del(void *ptr)
|
void cmd_del(void *ptr)
|
||||||
{
|
{
|
||||||
cmd* content = ptr;
|
cmd_t* content = ptr;
|
||||||
|
|
||||||
tab_free((void**)content->args);
|
tab_free((void**)content->args);
|
||||||
|
if (content->executable)
|
||||||
free(content->executable);
|
free(content->executable);
|
||||||
free(content);
|
free(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int cmd_init(cmd_t* command)
|
||||||
|
{
|
||||||
|
command->pid = -1;
|
||||||
|
command->input[0] = -1;
|
||||||
|
command->input[1] = -1;
|
||||||
|
command->output[0] = -1;
|
||||||
|
command->output[1] = -1;
|
||||||
|
command->executable = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cmd_close(void* ptr)
|
||||||
|
{
|
||||||
|
cmd_t* content = ptr;
|
||||||
|
if (content->input[0] > 2)
|
||||||
|
close(content->input[0]);
|
||||||
|
// content->input[0] = -1;
|
||||||
|
if (content->input[1] > 2)
|
||||||
|
close(content->input[1]);
|
||||||
|
// content->input[1] = -1;
|
||||||
|
if (content->output[0] > 2)
|
||||||
|
close(content->output[0]);
|
||||||
|
// content->output[0] = -1;
|
||||||
|
if (content->output[1] > 2)
|
||||||
|
close(content->output[1]);
|
||||||
|
// content->output[1] = -1;
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../../lib/bozolib/bozolib.h"
|
#include "../../lib/bozolib/bozolib.h"
|
||||||
|
|
||||||
@ -9,11 +10,11 @@ typedef struct s_cmd
|
|||||||
{
|
{
|
||||||
char *executable;
|
char *executable;
|
||||||
char **args;
|
char **args;
|
||||||
int fd_in;
|
int input[2];
|
||||||
int fd_out;
|
int output[2];
|
||||||
int pid;
|
int pid;
|
||||||
} cmd;
|
} cmd_t;
|
||||||
|
|
||||||
void cmd_del(void* ptr);
|
void cmd_del(void* ptr);
|
||||||
|
int cmd_init(cmd_t* command);
|
||||||
lst** cmds_init(size_t len);
|
void cmd_close(void* ptr);
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
#include "./cmd.h"
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
lst** cmds_init(size_t len)
|
|
||||||
{
|
|
||||||
lst** root;
|
|
||||||
lst* current;
|
|
||||||
cmd* content;
|
|
||||||
|
|
||||||
root = calloc(sizeof(lst*), 1);
|
|
||||||
if (root == NULL)
|
|
||||||
return (NULL);
|
|
||||||
for (size_t i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
current = malloc(sizeof(lst));
|
|
||||||
if (current == NULL)
|
|
||||||
{
|
|
||||||
lst_clear(root, &free);
|
|
||||||
free(root);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
lst_addback(root, current);
|
|
||||||
content = calloc(sizeof(cmd), 1);
|
|
||||||
if (content == NULL)
|
|
||||||
{
|
|
||||||
lst_clear(root, &free);
|
|
||||||
free(root);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
current->content = content;
|
|
||||||
}
|
|
||||||
return (root);
|
|
||||||
}
|
|
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;
|
73
src/env/env.c
vendored
73
src/env/env.c
vendored
@ -1,7 +1,4 @@
|
|||||||
#include "env.h"
|
#include "env.h"
|
||||||
#include "../../lib/bozolib/bozolib.h"
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void env_del(void *ptr)
|
void env_del(void *ptr)
|
||||||
@ -174,3 +171,73 @@ char **get_env_str(lst** root)
|
|||||||
tab[i] = NULL;
|
tab[i] = NULL;
|
||||||
return tab;
|
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
8
src/env/env.h
vendored
@ -1,5 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "../../lib/bozolib/bozolib.h"
|
#include "../../lib/bozolib/bozolib.h"
|
||||||
|
#include "../data/data.h"
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
|
||||||
typedef struct s_env
|
typedef struct s_env
|
||||||
{
|
{
|
||||||
@ -13,4 +20,5 @@ void env_del(void *ptr);
|
|||||||
char *get_env_variable(lst** root, const char* key);
|
char *get_env_variable(lst** root, const char* key);
|
||||||
int add_env_variable(lst** root, const char *key, const char *value);
|
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);
|
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);
|
char** get_env_str(lst** root);
|
||||||
|
1
src/env/envs.c
vendored
1
src/env/envs.c
vendored
@ -1 +0,0 @@
|
|||||||
|
|
131
src/exec/exec.c
131
src/exec/exec.c
@ -1,52 +1,48 @@
|
|||||||
|
#include <signal.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "../cmd/cmd.h"
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "../signal/signal.h"
|
||||||
|
#include "../cmd/cmd.h"
|
||||||
#include "../env/env.h"
|
#include "../env/env.h"
|
||||||
#include "../utils/utils.h"
|
#include "../utils/utils.h"
|
||||||
#include "../../lib/bozolib/bozolib.h"
|
#include "../../lib/bozolib/bozolib.h"
|
||||||
#include "../builtin-exec/builtin.h"
|
#include "../builtin-exec/builtin.h"
|
||||||
|
#include "../data/data.h"
|
||||||
|
|
||||||
int len(void** list)
|
int execute(lst** cmds, cmd_t* cmd, lst** env)
|
||||||
{
|
|
||||||
int index;
|
|
||||||
for (index = 0; list[index]!=NULL; index++);
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
int execute(cmd* input, lst** env)
|
|
||||||
{
|
{
|
||||||
int pid;
|
int pid;
|
||||||
int exitcode;
|
if (cmd->executable == NULL)
|
||||||
if (input->executable != NULL)
|
|
||||||
pid = fork();
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
dprintf(2, "Executable inconnu\n");
|
dprintf(2, "zzsh: command not found: %s\n", cmd->args[0]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
pid = fork();
|
||||||
|
if (pid == -1)
|
||||||
|
return 1;
|
||||||
|
else if (pid == 0)
|
||||||
|
{
|
||||||
|
char** env_str = get_env_str(env);
|
||||||
|
if (env_str == NULL)
|
||||||
|
return 1;
|
||||||
|
dup2(cmd->output[0], 1);
|
||||||
|
dup2(cmd->input[0], 0);
|
||||||
|
lst_iter(cmds, &cmd_close);
|
||||||
|
signal(SIGINT, SIG_DFL);
|
||||||
|
signal(SIGQUIT, SIG_DFL);
|
||||||
|
execve(cmd->executable, cmd->args, env_str);
|
||||||
|
dprintf(2, "execve failed\n");
|
||||||
|
free((void**)env_str);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (pid == 0)
|
cmd->pid = pid;
|
||||||
{
|
signal(SIGINT, SIG_IGN);
|
||||||
dup2(input->fd_out, 1);
|
return 0;
|
||||||
dup2(input->fd_in, 0);
|
|
||||||
if (input->fd_in > 2)
|
|
||||||
close(input->fd_in);
|
|
||||||
if (input->fd_out > 2)
|
|
||||||
close(input->fd_out);
|
|
||||||
exitcode = execve(input->executable, input->args, get_env_str(env));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (input->fd_in > 2)
|
|
||||||
close(input->fd_in);
|
|
||||||
if (input->fd_out > 2)
|
|
||||||
close(input->fd_out);
|
|
||||||
waitpid(pid, &exitcode, 0);
|
|
||||||
}
|
|
||||||
return exitcode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char* get_executable_path(const char* executable, lst** env)
|
char* get_executable_path(const char* executable, lst** env)
|
||||||
@ -78,28 +74,73 @@ char* get_executable_path(const char* executable, lst** env)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmds_list_exec(lst** cmds, lst** env)
|
void add_fd(int fds[2], int fd)
|
||||||
|
{
|
||||||
|
if (fds[0] == -1)
|
||||||
|
fds[0] = fd;
|
||||||
|
else
|
||||||
|
fds[1] = fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cmds_wait(lst** cmds, data_t* data)
|
||||||
{
|
{
|
||||||
lst* current = *cmds;
|
lst* current = *cmds;
|
||||||
cmd* content;
|
cmd_t* content;
|
||||||
|
int exit_status;
|
||||||
|
|
||||||
|
while (current)
|
||||||
|
{
|
||||||
|
content = current->content;
|
||||||
|
if (content->pid != -1 && content->input[0] != -2 && content->output[0] != -2)
|
||||||
|
{
|
||||||
|
waitpid(content->pid, &exit_status, 0);
|
||||||
|
if (WIFSIGNALED(exit_status))
|
||||||
|
{
|
||||||
|
if (exit_status == 131)
|
||||||
|
{
|
||||||
|
printf("Quit (core dumped)");
|
||||||
|
data->status_code = 131;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
data->status_code = 130;
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
data->status_code = WEXITSTATUS(exit_status);
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int cmds_list_exec(lst** cmds, data_t *data)
|
||||||
|
{
|
||||||
|
lst* current = *cmds;
|
||||||
|
cmd_t* content;
|
||||||
int fds[2];
|
int fds[2];
|
||||||
while (current != NULL)
|
while (current != NULL)
|
||||||
{
|
{
|
||||||
content = current->content;
|
content = current->content;
|
||||||
if (pipe(fds) < 0)
|
|
||||||
return 1;
|
|
||||||
if (current->next != NULL)
|
if (current->next != NULL)
|
||||||
{
|
{
|
||||||
content->fd_out = fds[1];
|
if (pipe(fds) == -1)
|
||||||
((cmd*)current->next->content)->fd_in = fds[0];
|
return 1;
|
||||||
|
add_fd(content->output, fds[1]);
|
||||||
|
add_fd(((cmd_t*)current->next->content)->input, fds[0]);
|
||||||
|
}
|
||||||
|
if (content->input[0] == -2 || content->input[0] == -2 || content->args[0] == NULL)
|
||||||
|
content->pid = -1;
|
||||||
|
else if (builtin_execute(data, content) == 1)
|
||||||
|
{
|
||||||
|
if (execute(cmds, content, data->env))
|
||||||
|
{
|
||||||
|
cmd_close(content);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
cmd_close(content);
|
||||||
}
|
}
|
||||||
if (content->args[0] == NULL)
|
|
||||||
;
|
|
||||||
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);
|
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
cmds_wait(cmds, data);
|
||||||
|
signal(SIGINT, ctrlc);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
#include "../cmd/cmd.h"
|
#include "../cmd/cmd.h"
|
||||||
#include "../../lib/bozolib/bozolib.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);
|
|
||||||
char* get_executable_path(const char* executable, lst** env);
|
char* get_executable_path(const char* executable, lst** env);
|
||||||
|
@ -1,14 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include "./input.h"
|
||||||
#include <readline/readline.h>
|
#include <readline/readline.h>
|
||||||
#include <readline/history.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include "../../lib/bozolib/bozolib.h"
|
|
||||||
#include "../../lib/bozolib/src/str/str.h"
|
|
||||||
#include "../env/env.h"
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
static char* get_prompt(lst** env)
|
static char* get_prompt(data_t *data)
|
||||||
{
|
{
|
||||||
char* out;
|
char* out;
|
||||||
char cwd[PATH_MAX];
|
char cwd[PATH_MAX];
|
||||||
@ -25,8 +18,8 @@ static char* get_prompt(lst** env)
|
|||||||
{
|
{
|
||||||
*first_line_return = '\0';
|
*first_line_return = '\0';
|
||||||
}
|
}
|
||||||
char* user = get_env_variable(env, "USER");
|
char* user = get_env_variable(data->env, "USER");
|
||||||
char* home = get_env_variable(env, "HOME");
|
char* home = get_env_variable(data->env, "HOME");
|
||||||
if (strncmp(home, cwd, strlen(home)) == 0)
|
if (strncmp(home, cwd, strlen(home)) == 0)
|
||||||
{
|
{
|
||||||
strcpy(cwd, "~");
|
strcpy(cwd, "~");
|
||||||
@ -42,16 +35,40 @@ static char* get_prompt(lst** env)
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_user_input(lst** env)
|
static void finisher(char** strs)
|
||||||
|
{
|
||||||
|
char* tmp;
|
||||||
|
char* tmp2;
|
||||||
|
(void)tmp2;
|
||||||
|
if (strs[0] == NULL)
|
||||||
|
return;
|
||||||
|
while (strs[0] != NULL && is_in_quote(strs[0], 0 - 1))
|
||||||
|
{
|
||||||
|
tmp2 = readline("> ");
|
||||||
|
if (tmp2 == NULL)
|
||||||
|
return;
|
||||||
|
tmp = str_merger(3, "\n", strs[0], tmp2);
|
||||||
|
free(strs[0]);
|
||||||
|
if (tmp == NULL)
|
||||||
|
{
|
||||||
|
strs[0] = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
strs[0] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_user_input(data_t *data)
|
||||||
{
|
{
|
||||||
char *prompt;
|
char *prompt;
|
||||||
char *input;
|
char *input;
|
||||||
|
|
||||||
prompt = get_prompt(env);
|
prompt = get_prompt(data);
|
||||||
if (prompt == NULL)
|
if (prompt == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
input = readline(prompt);
|
input = readline(prompt);
|
||||||
free(prompt);
|
free(prompt);
|
||||||
|
finisher(&input);
|
||||||
if (input == NULL)
|
if (input == NULL)
|
||||||
printf("exit");
|
printf("exit");
|
||||||
if (input != NULL && str_contain_only(input, "\t ") == 0)
|
if (input != NULL && str_contain_only(input, "\t ") == 0)
|
||||||
|
@ -1,5 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../../lib/bozolib/bozolib.h"
|
#include <stdio.h>
|
||||||
|
#include <readline/readline.h>
|
||||||
|
#include <readline/history.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
char *get_user_input(lst** env);
|
#include "../../lib/bozolib/bozolib.h"
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
#include "../env/env.h"
|
||||||
|
#include "../data/data.h"
|
||||||
|
|
||||||
|
char *get_user_input(data_t *data);
|
||||||
|
38
src/main.c
38
src/main.c
@ -1,10 +1,15 @@
|
|||||||
#include "./env/env.h"
|
|
||||||
#include "./input/input.h"
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
#include "./signal/signal.h"
|
||||||
|
#include "./input/input.h"
|
||||||
|
#include "alias/alias.h"
|
||||||
#include "cmd/cmd.h"
|
#include "cmd/cmd.h"
|
||||||
|
#include "./env/env.h"
|
||||||
#include "./exec/exec.h"
|
#include "./exec/exec.h"
|
||||||
#include "./parsing/parsing.h"
|
#include "./parsing/parsing.h"
|
||||||
|
#include "./data/data.h"
|
||||||
|
|
||||||
void cmds_list_destroyer(lst*** cmds_list)
|
void cmds_list_destroyer(lst*** cmds_list)
|
||||||
{
|
{
|
||||||
@ -16,34 +21,45 @@ void cmds_list_destroyer(lst*** cmds_list)
|
|||||||
int main(int ac, char **av, char **env_str)
|
int main(int ac, char **av, char **env_str)
|
||||||
{
|
{
|
||||||
char *line;
|
char *line;
|
||||||
lst** env;
|
data_t data;
|
||||||
lst*** cmds_list;
|
lst*** cmds_list;
|
||||||
|
|
||||||
(void) av;
|
(void) av;
|
||||||
(void) ac;
|
(void) ac;
|
||||||
env = env_init((const char **) env_str);
|
data.env = env_init((const char **) env_str);
|
||||||
if (env == NULL)
|
if (data.env == NULL)
|
||||||
return (1);
|
return (1);
|
||||||
line = get_user_input(env);
|
data.aliases = aliases_init(data.env);
|
||||||
|
if (data.aliases == NULL)
|
||||||
|
{
|
||||||
|
lst_clear(data.env, &env_del);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
data.status_code = 0;
|
||||||
|
signal(SIGINT, ctrlc);
|
||||||
|
signal(SIGQUIT, SIG_IGN);
|
||||||
|
line = get_user_input(&data);
|
||||||
while (line != NULL)
|
while (line != NULL)
|
||||||
{
|
{
|
||||||
cmds_list = parsing(line, env);
|
cmds_list = parsing(line, &data);
|
||||||
free(line);
|
free(line);
|
||||||
if (cmds_list != NULL)
|
if (cmds_list != NULL)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; cmds_list[i] != NULL; i++)
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cmds_list_destroyer(cmds_list);
|
cmds_list_destroyer(cmds_list);
|
||||||
}
|
}
|
||||||
line = get_user_input(env);
|
line = get_user_input(&data);
|
||||||
}
|
}
|
||||||
lst_clear(env, &env_del);
|
alias_save(data.aliases, data.env);
|
||||||
|
lst_clear(data.aliases, &alias_del);
|
||||||
|
lst_clear(data.env, &env_del);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,61 @@
|
|||||||
#include "parsing.h"
|
#include "parsing.h"
|
||||||
char* parsing_executable(const char* executable, lst** env)
|
|
||||||
|
char* parsing_executable(const char* executable, data_t *data)
|
||||||
{
|
{
|
||||||
if (executable == NULL)
|
if (executable == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (strchr("./", executable[0]))
|
if (strchr("./", executable[0]))
|
||||||
return strdup(executable);
|
return strdup(executable);
|
||||||
return (get_executable_path(executable, env));
|
return (get_executable_path(executable, data->env));
|
||||||
}
|
}
|
||||||
|
|
||||||
int parsing_cmd(char *str, cmd* command, lst** env)
|
int parsing_cmd(char *str, cmd_t* command, data_t *data)
|
||||||
{
|
{
|
||||||
if (get_redirections(str, command))
|
char* tmp;
|
||||||
|
if (cmd_init(command))
|
||||||
return 1;
|
return 1;
|
||||||
command->args = split_quoted_charset(str, "\t ");
|
tmp = interpret_env_var(data, str);
|
||||||
|
if (tmp == NULL)
|
||||||
|
return 1;
|
||||||
|
if (get_redirections(tmp, command))
|
||||||
|
{
|
||||||
|
free(tmp);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
command->args = split_quoted_charset(tmp, "\t ");
|
||||||
|
free(tmp);
|
||||||
if (command->args == NULL)
|
if (command->args == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
for (size_t i = 0; command->args[i]; i++)
|
for (size_t i = 0; command->args[i] != NULL; i++)
|
||||||
quote_remover(command->args[i]);
|
quote_remover(command->args[i]);
|
||||||
command->executable = parsing_executable(command->args[0], env);
|
if (command->args[0] != NULL)
|
||||||
|
{
|
||||||
|
if (command->args[0][0] == '\\')
|
||||||
|
{
|
||||||
|
tmp = strdup(command->args[0] + 1);
|
||||||
|
if (tmp != NULL)
|
||||||
|
{
|
||||||
|
free(command->args[0]);
|
||||||
|
command->args[0] = strdup(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tmp = get_alias(data->aliases, command->args[0]);
|
||||||
|
if (tmp != NULL)
|
||||||
|
{
|
||||||
|
free(command->args[0]);
|
||||||
|
command->args[0] = strdup(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (command->args[0] == NULL)
|
||||||
|
return 1;
|
||||||
|
command->executable = parsing_executable(command->args[0], data);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
lst **parsing_pipe(const char *str, lst** env)
|
lst **parsing_pipe(const char *str, data_t *data)
|
||||||
{
|
{
|
||||||
char** cmds_str;
|
char** cmds_str;
|
||||||
lst** cmds;
|
lst** cmds;
|
||||||
@ -30,27 +64,63 @@ lst **parsing_pipe(const char *str, lst** env)
|
|||||||
cmds_str = split_quoted_charset(str, "|");
|
cmds_str = split_quoted_charset(str, "|");
|
||||||
if (cmds_str == NULL)
|
if (cmds_str == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
cmds = cmds_init(tablen((const void**)cmds_str));
|
cmds = malloc(sizeof(lst*));
|
||||||
if (cmds == NULL)
|
if (cmds == NULL)
|
||||||
{
|
{
|
||||||
tab_free((void**)cmds_str);
|
tab_free((void**)cmds_str);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
if (cmds_str[0] != NULL)
|
||||||
|
{
|
||||||
|
*cmds = malloc(sizeof(lst));
|
||||||
|
if (*cmds == NULL)
|
||||||
|
{
|
||||||
|
free(cmds);
|
||||||
|
tab_free((void**)cmds_str);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
current = *cmds;
|
current = *cmds;
|
||||||
|
}
|
||||||
for (size_t i = 0; cmds_str[i] != NULL; i++)
|
for (size_t i = 0; cmds_str[i] != NULL; i++)
|
||||||
{
|
{
|
||||||
if (parsing_cmd(cmds_str[i], current->content, env))
|
current->content = malloc(sizeof(cmd_t));
|
||||||
|
if (current->content == NULL)
|
||||||
{
|
{
|
||||||
tab_free((void**)cmds_str);
|
tab_free((void**)cmds_str);
|
||||||
|
lst_clear(cmds, &cmd_del);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
if (parsing_cmd(cmds_str[i], current->content, data))
|
||||||
|
{
|
||||||
|
tab_free((void**)cmds_str);
|
||||||
|
lst_clear(cmds, &cmd_del);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (cmds_str[i + 1] != NULL)
|
||||||
|
{
|
||||||
|
current->next = malloc(sizeof(lst));
|
||||||
|
if (current->next == NULL)
|
||||||
|
{
|
||||||
|
tab_free((void**)cmds_str);
|
||||||
|
lst_clear(cmds, &cmd_del);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
current->next = NULL;
|
||||||
|
if (i == 0)
|
||||||
|
if (((cmd_t*) current->content)->input[0] == -1)
|
||||||
|
((cmd_t*) current->content)->input[0] = 0;
|
||||||
|
if (current->next == NULL)
|
||||||
|
if (((cmd_t*) current->content)->output[0] == -1)
|
||||||
|
((cmd_t*) current->content)->output[0] = 1;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
tab_free((void**)cmds_str);
|
tab_free((void**)cmds_str);
|
||||||
return (cmds);
|
return (cmds);
|
||||||
}
|
}
|
||||||
|
|
||||||
lst*** parsing(const char *line, lst** env)
|
lst*** parsing(const char *line, data_t *data)
|
||||||
{
|
{
|
||||||
char** line_commas;
|
char** line_commas;
|
||||||
lst*** tab;
|
lst*** tab;
|
||||||
@ -67,7 +137,7 @@ lst*** parsing(const char *line, lst** env)
|
|||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; line_commas[i] != NULL; 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)
|
if (tab[i] == NULL)
|
||||||
{
|
{
|
||||||
tab_free((void**) line_commas);
|
tab_free((void**) line_commas);
|
||||||
|
@ -3,4 +3,8 @@
|
|||||||
#include "../exec/exec.h"
|
#include "../exec/exec.h"
|
||||||
#include "../../lib/bozolib/bozolib.h"
|
#include "../../lib/bozolib/bozolib.h"
|
||||||
#include "../redirection/redirection.h"
|
#include "../redirection/redirection.h"
|
||||||
lst*** parsing(const char* str, lst** env);
|
#include "../data/data.h"
|
||||||
|
#include "../alias/alias.h"
|
||||||
|
#include "../env/env.h"
|
||||||
|
|
||||||
|
lst*** parsing(const char* str, data_t *data);
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
char* get_path(const char* start_path)
|
char* get_path(const char* start_path)
|
||||||
{
|
{
|
||||||
@ -36,32 +37,40 @@ static size_t get_path_size(const char* str)
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
int redirection_fill(const char* str, size_t nb_symbols, cmd* command, const char *path)
|
void redirection_fill(const char* str, size_t nb_symbols, cmd_t* command, const char *path)
|
||||||
{
|
{
|
||||||
int problem = 0;
|
int fd;
|
||||||
if (str[0] == '>')
|
if (str[0] == '>')
|
||||||
{
|
{
|
||||||
if (command->fd_out > 2)
|
|
||||||
close(command->fd_out);
|
|
||||||
if (nb_symbols == 1)
|
if (nb_symbols == 1)
|
||||||
command->fd_out = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644);
|
fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644);
|
||||||
else
|
else
|
||||||
command->fd_out = open(path, O_APPEND | O_CREAT | O_WRONLY, 0644);
|
fd = open(path, O_APPEND | O_CREAT | O_WRONLY, 0644);
|
||||||
problem = command->fd_out == -1;
|
if (command->output[0] > 2)
|
||||||
|
close(command->output[0]);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
dprintf(2, "zzsh: permission denied: %s\n", path);
|
||||||
|
fd = -2;
|
||||||
|
}
|
||||||
|
command->output[0] = fd;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (command->fd_in > 2)
|
if (access(path, F_OK))
|
||||||
close(command->fd_in);
|
dprintf(2, "zzsh: no such file or directory: %s\n", path);
|
||||||
|
else if (access(path, R_OK))
|
||||||
|
dprintf(2, "zzsh: permission denied: %s\n", path);
|
||||||
if (nb_symbols == 1)
|
if (nb_symbols == 1)
|
||||||
command->fd_in = open(path, O_RDONLY);
|
fd = open(path, O_RDONLY);
|
||||||
else
|
else
|
||||||
command->fd_in = open(path, O_RDONLY);
|
fd = open(path, O_RDONLY);
|
||||||
problem = command->fd_in == -1;
|
if (command->output[0] > 2)
|
||||||
|
close(command->output[0]);
|
||||||
|
if (fd == -1)
|
||||||
|
fd = -2;
|
||||||
|
command->output[0] = fd;
|
||||||
}
|
}
|
||||||
if (problem)
|
|
||||||
dprintf(2, "zzsh: \"%s\": file error\n", path);
|
|
||||||
return problem;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t get_concecutive(const char *str)
|
static size_t get_concecutive(const char *str)
|
||||||
@ -71,7 +80,7 @@ static size_t get_concecutive(const char *str)
|
|||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_redirections(char *str, cmd* command)
|
int get_redirections(char *str, cmd_t* command)
|
||||||
{
|
{
|
||||||
char *redirection;
|
char *redirection;
|
||||||
char redirection_symbol[3] = "<>";
|
char redirection_symbol[3] = "<>";
|
||||||
@ -85,7 +94,7 @@ int get_redirections(char *str, cmd* command)
|
|||||||
{
|
{
|
||||||
redirection = strchr(redirection, redirection_symbol[i]);
|
redirection = strchr(redirection, redirection_symbol[i]);
|
||||||
while (redirection != NULL && is_in_quote(str, redirection - str))
|
while (redirection != NULL && is_in_quote(str, redirection - str))
|
||||||
redirection = strchr(redirection, '>');
|
redirection = strchr(redirection + 2, redirection_symbol[i]);
|
||||||
if (redirection == NULL)
|
if (redirection == NULL)
|
||||||
break;
|
break;
|
||||||
redirection_type = get_concecutive(redirection);
|
redirection_type = get_concecutive(redirection);
|
||||||
@ -99,11 +108,7 @@ int get_redirections(char *str, cmd* command)
|
|||||||
path = get_path(redirection + redirection_type);
|
path = get_path(redirection + redirection_type);
|
||||||
if (path == NULL)
|
if (path == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
if (redirection_fill(redirection, redirection_type, command, path))
|
redirection_fill(redirection, redirection_type, command, path);
|
||||||
{
|
|
||||||
free(path);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
str_shift(redirection, (get_path_size(redirection + redirection_type) + redirection_type) * -1);
|
str_shift(redirection, (get_path_size(redirection + redirection_type) + redirection_type) * -1);
|
||||||
free(path);
|
free(path);
|
||||||
}
|
}
|
||||||
|
@ -8,4 +8,4 @@
|
|||||||
#include "../cmd/cmd.h"
|
#include "../cmd/cmd.h"
|
||||||
#include "../utils/utils.h"
|
#include "../utils/utils.h"
|
||||||
|
|
||||||
int get_redirections(char *str, cmd* command);
|
int get_redirections(char *str, cmd_t* command);
|
||||||
|
17
src/signal/signal.c
Normal file
17
src/signal/signal.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "./signal.h"
|
||||||
|
|
||||||
|
void ctrlc(int num)
|
||||||
|
{
|
||||||
|
printf("\n");
|
||||||
|
rl_replace_line("", 0);
|
||||||
|
rl_on_new_line();
|
||||||
|
rl_redisplay();
|
||||||
|
(void) num;
|
||||||
|
}
|
||||||
|
|
||||||
|
void quit(int num)
|
||||||
|
{
|
||||||
|
(void) num;
|
||||||
|
// printf("Quit (core dumped)\n");
|
||||||
|
}
|
||||||
|
|
7
src/signal/signal.h
Normal file
7
src/signal/signal.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <readline/readline.h>
|
||||||
|
|
||||||
|
void quit(int num);
|
||||||
|
void ctrlc(int num);
|
Loading…
Reference in New Issue
Block a user