Compare commits
No commits in common. "main" and "1.0.0" have entirely different histories.
@ -7,13 +7,13 @@ que des variables d'environnement dans un terminal.
|
|||||||
|
|
||||||
## Intallation :
|
## Intallation :
|
||||||
|
|
||||||
Il suffit de télécharger une release qui possède un fichier précompilé
|
Il suffit de clone le repos, celui-ci possède un fichier déjà 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 https://github.com/Guamss/zzsh.git
|
git clone --recursive
|
||||||
make
|
make
|
||||||
```
|
```
|
||||||
### /!\ Note /!\
|
### /!\ Note /!\
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 3ed6ae7c279f0583a0ddbd1e19f7f7a151369ca0
|
Subproject commit 6d2b0a74c4c1c7a2bc62aa8797c504e42f5c7721
|
@ -1,142 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
#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,8 +1,18 @@
|
|||||||
#include "./builtin.h"
|
#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"
|
||||||
|
|
||||||
char* builtin_path(const char* executable)
|
char* builtin_path(const char* executable)
|
||||||
{
|
{
|
||||||
if (strcmp(executable, "cd") == 0 || strcmp(executable, "where") == 0 || strcmp(executable, "alias") == 0);
|
if (strcmp(executable, "cd") == 0 || strcmp(executable, "where") == 0);
|
||||||
else
|
else
|
||||||
return NULL;
|
return NULL;
|
||||||
return strdup(executable);
|
return strdup(executable);
|
||||||
@ -12,12 +22,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 (tablen((const void**)args)==1)
|
if(len((void**)args)==1)
|
||||||
{
|
{
|
||||||
char* path = get_env_variable(env, "HOME");
|
char* path = get_env_variable(env, "HOME");
|
||||||
chdir(path);
|
chdir(path);
|
||||||
}
|
}
|
||||||
else if (tablen((const void**)args)>2)
|
else if (len((void**)args)>2)
|
||||||
{
|
{
|
||||||
dprintf(2, "cd : Trop d'arguments!\n");
|
dprintf(2, "cd : Trop d'arguments!\n");
|
||||||
return 1;
|
return 1;
|
||||||
@ -35,7 +45,7 @@ int change_directory(char** args, lst** env)
|
|||||||
|
|
||||||
int where(char** args, lst** env)
|
int where(char** args, lst** env)
|
||||||
{
|
{
|
||||||
if (tablen((const void**)args)==1)
|
if (len((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;
|
||||||
@ -55,22 +65,18 @@ int where(char** args, lst** env)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int builtin_execute(data_t* data, cmd_t* input)
|
int builtin_execute(cmd* input, lst** env)
|
||||||
{
|
{
|
||||||
if (input->executable == NULL)
|
|
||||||
return 1;
|
|
||||||
if (strcmp(input->executable, "cd") == 0)
|
if (strcmp(input->executable, "cd") == 0)
|
||||||
{
|
{
|
||||||
change_directory(input->args, data->env);
|
change_directory(input->args, env);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else if(strcmp(input->executable, "where") == 0)
|
else if(strcmp(input->executable, "where") == 0)
|
||||||
{
|
{
|
||||||
where(input->args, data->env);
|
where(input->args, env);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else if (strcmp(input->executable, "alias") == 0)
|
|
||||||
return builtin_alias(data, input);
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1,20 +1,9 @@
|
|||||||
#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, lst** env);
|
int where(char** args);
|
||||||
|
int builtin_execute(cmd* input, lst** env);
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
#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,38 +2,9 @@
|
|||||||
|
|
||||||
void cmd_del(void *ptr)
|
void cmd_del(void *ptr)
|
||||||
{
|
{
|
||||||
cmd_t* content = ptr;
|
cmd* 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,7 +2,6 @@
|
|||||||
|
|
||||||
#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"
|
||||||
|
|
||||||
@ -10,11 +9,11 @@ typedef struct s_cmd
|
|||||||
{
|
{
|
||||||
char *executable;
|
char *executable;
|
||||||
char **args;
|
char **args;
|
||||||
int input[2];
|
int fd_in;
|
||||||
int output[2];
|
int fd_out;
|
||||||
int pid;
|
int pid;
|
||||||
} cmd_t;
|
} cmd;
|
||||||
|
|
||||||
void cmd_del(void* ptr);
|
void cmd_del(void* ptr);
|
||||||
int cmd_init(cmd_t* command);
|
|
||||||
void cmd_close(void* ptr);
|
lst** cmds_init(size_t len);
|
||||||
|
34
src/cmd/cmds.c
Normal file
34
src/cmd/cmds.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#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);
|
||||||
|
}
|
@ -1,11 +0,0 @@
|
|||||||
#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,4 +1,7 @@
|
|||||||
#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)
|
||||||
@ -171,73 +174,3 @@ 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,12 +1,5 @@
|
|||||||
#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
|
||||||
{
|
{
|
||||||
@ -20,5 +13,4 @@ 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
Normal file
1
src/env/envs.c
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
129
src/exec/exec.c
129
src/exec/exec.c
@ -1,48 +1,52 @@
|
|||||||
#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 <unistd.h>
|
|
||||||
|
|
||||||
#include "../signal/signal.h"
|
|
||||||
#include "../cmd/cmd.h"
|
#include "../cmd/cmd.h"
|
||||||
|
#include <unistd.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 execute(lst** cmds, cmd_t* cmd, lst** env)
|
int len(void** list)
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
for (index = 0; list[index]!=NULL; index++);
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
int execute(cmd* input, lst** env)
|
||||||
{
|
{
|
||||||
int pid;
|
int pid;
|
||||||
if (cmd->executable == NULL)
|
int exitcode;
|
||||||
|
if (input->executable != NULL)
|
||||||
|
pid = fork();
|
||||||
|
else
|
||||||
{
|
{
|
||||||
dprintf(2, "zzsh: command not found: %s\n", cmd->args[0]);
|
dprintf(2, "Executable inconnu\n");
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
pid = fork();
|
if (pid == 0)
|
||||||
if (pid == -1)
|
|
||||||
return 1;
|
|
||||||
else if (pid == 0)
|
|
||||||
{
|
{
|
||||||
char** env_str = get_env_str(env);
|
dup2(input->fd_out, 1);
|
||||||
if (env_str == NULL)
|
dup2(input->fd_in, 0);
|
||||||
return 1;
|
if (input->fd_in > 2)
|
||||||
dup2(cmd->output[0], 1);
|
close(input->fd_in);
|
||||||
dup2(cmd->input[0], 0);
|
if (input->fd_out > 2)
|
||||||
lst_iter(cmds, &cmd_close);
|
close(input->fd_out);
|
||||||
signal(SIGINT, SIG_DFL);
|
exitcode = execve(input->executable, input->args, get_env_str(env));
|
||||||
signal(SIGQUIT, SIG_DFL);
|
|
||||||
execve(cmd->executable, cmd->args, env_str);
|
|
||||||
dprintf(2, "execve failed\n");
|
|
||||||
free((void**)env_str);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
cmd->pid = pid;
|
else
|
||||||
signal(SIGINT, SIG_IGN);
|
{
|
||||||
return 0;
|
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)
|
||||||
@ -74,73 +78,28 @@ char* get_executable_path(const char* executable, lst** env)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_fd(int fds[2], int fd)
|
int cmds_list_exec(lst** cmds, lst** env)
|
||||||
{
|
|
||||||
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_t* content;
|
cmd* 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)
|
||||||
{
|
{
|
||||||
if (pipe(fds) == -1)
|
content->fd_out = fds[1];
|
||||||
return 1;
|
((cmd*)current->next->content)->fd_in = fds[0];
|
||||||
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,7 +2,9 @@
|
|||||||
|
|
||||||
#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, data_t *data);
|
int cmds_list_exec(lst** cmds, lst** env);
|
||||||
|
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,7 +1,14 @@
|
|||||||
#include "./input.h"
|
#include <stdio.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(data_t *data)
|
static char* get_prompt(lst** env)
|
||||||
{
|
{
|
||||||
char* out;
|
char* out;
|
||||||
char cwd[PATH_MAX];
|
char cwd[PATH_MAX];
|
||||||
@ -18,8 +25,8 @@ static char* get_prompt(data_t *data)
|
|||||||
{
|
{
|
||||||
*first_line_return = '\0';
|
*first_line_return = '\0';
|
||||||
}
|
}
|
||||||
char* user = get_env_variable(data->env, "USER");
|
char* user = get_env_variable(env, "USER");
|
||||||
char* home = get_env_variable(data->env, "HOME");
|
char* home = get_env_variable(env, "HOME");
|
||||||
if (strncmp(home, cwd, strlen(home)) == 0)
|
if (strncmp(home, cwd, strlen(home)) == 0)
|
||||||
{
|
{
|
||||||
strcpy(cwd, "~");
|
strcpy(cwd, "~");
|
||||||
@ -35,40 +42,16 @@ static char* get_prompt(data_t *data)
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void finisher(char** strs)
|
char *get_user_input(lst** env)
|
||||||
{
|
|
||||||
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(data);
|
prompt = get_prompt(env);
|
||||||
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,15 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <readline/readline.h>
|
|
||||||
#include <readline/history.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
#include "../../lib/bozolib/bozolib.h"
|
#include "../../lib/bozolib/bozolib.h"
|
||||||
#include "../utils/utils.h"
|
|
||||||
#include "../env/env.h"
|
|
||||||
#include "../data/data.h"
|
|
||||||
|
|
||||||
char *get_user_input(data_t *data);
|
char *get_user_input(lst** env);
|
||||||
|
38
src/main.c
38
src/main.c
@ -1,15 +1,10 @@
|
|||||||
|
#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)
|
||||||
{
|
{
|
||||||
@ -21,45 +16,34 @@ 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;
|
||||||
data_t data;
|
lst** env;
|
||||||
lst*** cmds_list;
|
lst*** cmds_list;
|
||||||
|
|
||||||
(void) av;
|
(void) av;
|
||||||
(void) ac;
|
(void) ac;
|
||||||
data.env = env_init((const char **) env_str);
|
env = env_init((const char **) env_str);
|
||||||
if (data.env == NULL)
|
if (env == NULL)
|
||||||
return (1);
|
return (1);
|
||||||
data.aliases = aliases_init(data.env);
|
line = get_user_input(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, &data);
|
cmds_list = parsing(line, env);
|
||||||
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], &data))
|
if (cmds_list_exec(cmds_list[i], env))
|
||||||
{
|
{
|
||||||
lst_clear(data.env, &env_del);
|
lst_clear(env, &env_del);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cmds_list_destroyer(cmds_list);
|
cmds_list_destroyer(cmds_list);
|
||||||
}
|
}
|
||||||
line = get_user_input(&data);
|
line = get_user_input(env);
|
||||||
}
|
}
|
||||||
alias_save(data.aliases, data.env);
|
lst_clear(env, &env_del);
|
||||||
lst_clear(data.aliases, &alias_del);
|
|
||||||
lst_clear(data.env, &env_del);
|
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,61 +1,27 @@
|
|||||||
#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, data->env));
|
return (get_executable_path(executable, env));
|
||||||
}
|
}
|
||||||
|
|
||||||
int parsing_cmd(char *str, cmd_t* command, data_t *data)
|
int parsing_cmd(char *str, cmd* command, lst** env)
|
||||||
{
|
{
|
||||||
char* tmp;
|
if (get_redirections(str, command))
|
||||||
if (cmd_init(command))
|
|
||||||
return 1;
|
return 1;
|
||||||
tmp = interpret_env_var(data, str);
|
command->args = split_quoted_charset(str, "\t ");
|
||||||
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] != NULL; i++)
|
for (size_t i = 0; command->args[i]; i++)
|
||||||
quote_remover(command->args[i]);
|
quote_remover(command->args[i]);
|
||||||
if (command->args[0] != NULL)
|
command->executable = parsing_executable(command->args[0], env);
|
||||||
{
|
|
||||||
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, data_t *data)
|
lst **parsing_pipe(const char *str, lst** env)
|
||||||
{
|
{
|
||||||
char** cmds_str;
|
char** cmds_str;
|
||||||
lst** cmds;
|
lst** cmds;
|
||||||
@ -64,63 +30,27 @@ lst **parsing_pipe(const char *str, data_t *data)
|
|||||||
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 = malloc(sizeof(lst*));
|
cmds = cmds_init(tablen((const void**)cmds_str));
|
||||||
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)
|
current = *cmds;
|
||||||
{
|
|
||||||
*cmds = malloc(sizeof(lst));
|
|
||||||
if (*cmds == NULL)
|
|
||||||
{
|
|
||||||
free(cmds);
|
|
||||||
tab_free((void**)cmds_str);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
current = *cmds;
|
|
||||||
}
|
|
||||||
for (size_t i = 0; cmds_str[i] != NULL; i++)
|
for (size_t i = 0; cmds_str[i] != NULL; i++)
|
||||||
{
|
{
|
||||||
current->content = malloc(sizeof(cmd_t));
|
if (parsing_cmd(cmds_str[i], current->content, env))
|
||||||
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, data_t *data)
|
lst*** parsing(const char *line, lst** env)
|
||||||
{
|
{
|
||||||
char** line_commas;
|
char** line_commas;
|
||||||
lst*** tab;
|
lst*** tab;
|
||||||
@ -137,7 +67,7 @@ lst*** parsing(const char *line, data_t *data)
|
|||||||
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], data);
|
tab[i] = parsing_pipe(line_commas[i], env);
|
||||||
if (tab[i] == NULL)
|
if (tab[i] == NULL)
|
||||||
{
|
{
|
||||||
tab_free((void**) line_commas);
|
tab_free((void**) line_commas);
|
||||||
|
@ -3,8 +3,4 @@
|
|||||||
#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"
|
||||||
#include "../data/data.h"
|
lst*** parsing(const char* str, lst** env);
|
||||||
#include "../alias/alias.h"
|
|
||||||
#include "../env/env.h"
|
|
||||||
|
|
||||||
lst*** parsing(const char* str, data_t *data);
|
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
#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)
|
||||||
{
|
{
|
||||||
@ -37,40 +36,32 @@ static size_t get_path_size(const char* str)
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
void redirection_fill(const char* str, size_t nb_symbols, cmd_t* command, const char *path)
|
int redirection_fill(const char* str, size_t nb_symbols, cmd* command, const char *path)
|
||||||
{
|
{
|
||||||
int fd;
|
int problem = 0;
|
||||||
if (str[0] == '>')
|
if (str[0] == '>')
|
||||||
{
|
{
|
||||||
|
if (command->fd_out > 2)
|
||||||
|
close(command->fd_out);
|
||||||
if (nb_symbols == 1)
|
if (nb_symbols == 1)
|
||||||
fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644);
|
command->fd_out = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644);
|
||||||
else
|
else
|
||||||
fd = open(path, O_APPEND | O_CREAT | O_WRONLY, 0644);
|
command->fd_out = open(path, O_APPEND | O_CREAT | O_WRONLY, 0644);
|
||||||
if (command->output[0] > 2)
|
problem = command->fd_out == -1;
|
||||||
close(command->output[0]);
|
|
||||||
if (fd == -1)
|
|
||||||
{
|
|
||||||
dprintf(2, "zzsh: permission denied: %s\n", path);
|
|
||||||
fd = -2;
|
|
||||||
}
|
|
||||||
command->output[0] = fd;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (access(path, F_OK))
|
if (command->fd_in > 2)
|
||||||
dprintf(2, "zzsh: no such file or directory: %s\n", path);
|
close(command->fd_in);
|
||||||
else if (access(path, R_OK))
|
|
||||||
dprintf(2, "zzsh: permission denied: %s\n", path);
|
|
||||||
if (nb_symbols == 1)
|
if (nb_symbols == 1)
|
||||||
fd = open(path, O_RDONLY);
|
command->fd_in = open(path, O_RDONLY);
|
||||||
else
|
else
|
||||||
fd = open(path, O_RDONLY);
|
command->fd_in = open(path, O_RDONLY);
|
||||||
if (command->output[0] > 2)
|
problem = command->fd_in == -1;
|
||||||
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)
|
||||||
@ -80,7 +71,7 @@ static size_t get_concecutive(const char *str)
|
|||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_redirections(char *str, cmd_t* command)
|
int get_redirections(char *str, cmd* command)
|
||||||
{
|
{
|
||||||
char *redirection;
|
char *redirection;
|
||||||
char redirection_symbol[3] = "<>";
|
char redirection_symbol[3] = "<>";
|
||||||
@ -94,7 +85,7 @@ int get_redirections(char *str, cmd_t* 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 + 2, redirection_symbol[i]);
|
redirection = strchr(redirection, '>');
|
||||||
if (redirection == NULL)
|
if (redirection == NULL)
|
||||||
break;
|
break;
|
||||||
redirection_type = get_concecutive(redirection);
|
redirection_type = get_concecutive(redirection);
|
||||||
@ -108,7 +99,11 @@ int get_redirections(char *str, cmd_t* command)
|
|||||||
path = get_path(redirection + redirection_type);
|
path = get_path(redirection + redirection_type);
|
||||||
if (path == NULL)
|
if (path == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
redirection_fill(redirection, redirection_type, command, path);
|
if (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_t* command);
|
int get_redirections(char *str, cmd* command);
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
#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");
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
|||||||
#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