1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-11-22 08:43:20 +00:00

Indentation avec tabulations

This commit is contained in:
Antoine Bartuccio 2017-05-15 11:22:53 +02:00
parent df2524f1fc
commit 5cef74481c
4 changed files with 163 additions and 159 deletions

View File

@ -7,58 +7,58 @@
/* Necessary global variables */ /* Necessary global variables */
char * builtinCommandsStr[] = { char * builtinCommandsStr[] = {
"cd", "cd",
"help", "help",
"exit" "exit"
}; };
builtinCommand *builtinCommands[] = { builtinCommand *builtinCommands[] = {
&fishCd, &fishCd,
&fishHelp, &fishHelp,
&fishExit &fishExit
}; };
char ** getBuiltinCommandsStr(){ char ** getBuiltinCommandsStr(){
return builtinCommandsStr; return builtinCommandsStr;
} }
builtinCommand **getBuiltinCommands(){ builtinCommand **getBuiltinCommands(){
return builtinCommands; return builtinCommands;
} }
int fishCd(WordArray *args) { int fishCd(WordArray *args) {
if (args->size < 2){ if (args->size < 2){
fprintf(stderr, "fish: Où sont les arguments de ta commande \"cd\" connard ?!\n"); fprintf(stderr, "fish: Où sont les arguments de ta commande \"cd\" connard ?!\n");
} else { } else {
if (chdir(args->words[1]) != 0){ if (chdir(args->words[1]) != 0){
perror("fish"); perror("fish");
} }
} }
return 1; return 1;
} }
int fishHelp(WordArray *args) { int fishHelp(WordArray *args) {
int i; int i;
printf("Bartuccio Antoine, Amalvy Arthur, Yann Chevanton\n"); printf("Bartuccio Antoine, Amalvy Arthur, Yann Chevanton\n");
printf("Tape tes putains de noms de programmes et tes arguments de merde et tabasse ENTER !\n"); printf("Tape tes putains de noms de programmes et tes arguments de merde et tabasse ENTER !\n");
printf("Les commandes suivantes sont internes :\nls"); printf("Les commandes suivantes sont internes :\nls");
for (i=0; i < getNbBuiltins(); i++){ for (i=0; i < getNbBuiltins(); i++){
printf("\t%s\n", builtinCommandsStr[i]); printf("\t%s\n", builtinCommandsStr[i]);
} }
printf("Et sinon pour le reste, RTFM !"); printf("Et sinon pour le reste, RTFM !");
if (args->size > 0) if (args->size > 0)
return 1; return 1;
return 1; return 1;
} }
int fishExit(WordArray *args) { int fishExit(WordArray *args) {
if (args->size != 1) if (args->size != 1)
return 1; return 1;
else else
return 0; return 0;
} }
int getNbBuiltins() { int getNbBuiltins() {
return sizeof(builtinCommandsStr) / sizeof(char*); return sizeof(builtinCommandsStr) / sizeof(char*);
} }

View File

@ -12,172 +12,172 @@
#define FISH_TOKENS " \t\r\n\a" #define FISH_TOKENS " \t\r\n\a"
void fishLoop(Settings * settings){ void fishLoop(Settings * settings){
char * line = NULL; char * line = NULL;
WordArray * splited = NULL; WordArray * splited = NULL;
int status = 1; int status = 1;
do { do {
printf("%s", settings->PS1); printf("%s", settings->PS1);
line = fishReadLine(); line = fishReadLine();
line = fishExpand(line); line = fishExpand(line);
splited = split(line, FISH_TOKENS); splited = split(line, FISH_TOKENS);
status = fishExecute(splited); status = fishExecute(splited);
freeWordArray(splited); freeWordArray(splited);
free(line); free(line);
} while(status); } while(status);
} }
int countSeparators(char *string, char *separators) { int countSeparators(char *string, char *separators) {
int nb = 0; int nb = 0;
int i = 0; int i = 0;
int k = 0; int k = 0;
while (string[i] != '\0'){ while (string[i] != '\0'){
while (separators[k] != '\0'){ while (separators[k] != '\0'){
if (string[i] == separators[k]){ if (string[i] == separators[k]){
nb++; nb++;
} }
k++; k++;
} }
i++; i++;
k = 0; k = 0;
} }
return nb; return nb;
} }
WordArray * split(char *string, char *separator){ WordArray * split(char *string, char *separator){
int array_size = countSeparators(string, separator) + 1; int array_size = countSeparators(string, separator) + 1;
WordArray *tokens = (WordArray*) malloc(sizeof(WordArray)); WordArray *tokens = (WordArray*) malloc(sizeof(WordArray));
char *to_delete = strdup(string); char *to_delete = strdup(string);
char *token = NULL; char *token = NULL;
int i = 0; int i = 0;
if (tokens != NULL){ if (tokens != NULL){
tokens->words = (char **) malloc(sizeof(char*) * (array_size + 1)); tokens->words = (char **) malloc(sizeof(char*) * (array_size + 1));
tokens->words[array_size] = NULL; tokens->words[array_size] = NULL;
tokens->size = array_size; tokens->size = array_size;
} }
if (tokens == NULL || to_delete == NULL || tokens->words == NULL){ if (tokens == NULL || to_delete == NULL || tokens->words == NULL){
fprintf(stderr, "fish: Error allocating fucking pointer !"); fprintf(stderr, "fish: Error allocating fucking pointer !");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
while((token = strsep(&to_delete, separator)) != NULL){ while((token = strsep(&to_delete, separator)) != NULL){
tokens->words[i] = strdup(token); tokens->words[i] = strdup(token);
i++; i++;
} }
free(to_delete); free(to_delete);
return tokens; return tokens;
} }
void freeWordArray(WordArray *array) { void freeWordArray(WordArray *array) {
int i; int i;
if (array != NULL) { if (array != NULL) {
for (i = 0; i < array->size; i++) { for (i = 0; i < array->size; i++) {
free(array->words[i]); free(array->words[i]);
} }
free(array); free(array);
} }
} }
char *fishReadLine() { char *fishReadLine() {
size_t bufferSize = FISH_BUFFER_SIZE; size_t bufferSize = FISH_BUFFER_SIZE;
int position = 0; int position = 0;
char *line = malloc(sizeof(char*) * bufferSize); char *line = malloc(sizeof(char*) * bufferSize);
int c; int c;
if (line == NULL){ if (line == NULL){
fprintf(stderr, "fish: Error allocating fucking buffer shit !"); fprintf(stderr, "fish: Error allocating fucking buffer shit !");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
while (1){ while (1){
c = getchar(); c = getchar();
switch (c){ switch (c){
case '\n': case '\n':
line[position] = '\0'; line[position] = '\0';
return line; return line;
case EOF: case EOF:
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
default: default:
line[position] = (char) c; line[position] = (char) c;
} }
position++; position++;
if ((size_t) position > bufferSize){ if ((size_t) position > bufferSize){
bufferSize+=bufferSize; bufferSize+=bufferSize;
line = realloc(line, bufferSize); line = realloc(line, bufferSize);
if (line == NULL){ if (line == NULL){
fprintf(stderr, "fish: Error allocating fucking buffer shit !"); fprintf(stderr, "fish: Error allocating fucking buffer shit !");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
} }
return NULL; return NULL;
} }
char *fishExpand(char *line) { char *fishExpand(char *line) {
return line; return line;
} }
Settings *getSettings() { Settings *getSettings() {
Settings *s = (Settings*) malloc(sizeof(Settings)); Settings *s = (Settings*) malloc(sizeof(Settings));
if (s == NULL){ if (s == NULL){
fprintf(stderr, "fish: Error allocating fucking settings"); fprintf(stderr, "fish: Error allocating fucking settings");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
s->PS1 = strdup("\n~>"); s->PS1 = strdup("\n~>");
return s; return s;
} }
int fishLoad(WordArray *array) { int fishLoad(WordArray *array) {
pid_t pid; pid_t pid;
int status; int status;
pid = fork(); pid = fork();
if (pid == 0){ if (pid == 0){
/* Executes only in the child process */ /* Executes only in the child process */
if (execvp(array->words[0], array->words) == -1){ if (execvp(array->words[0], array->words) == -1){
/* Error during system call */ /* Error during system call */
perror("fish"); perror("fish");
} }
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else if (pid < 0){ } else if (pid < 0){
/* Fork failed */ /* Fork failed */
perror("fish"); perror("fish");
} else { } else {
/* Handle parent process */ /* Handle parent process */
/* Wait for the child process to finish */ /* Wait for the child process to finish */
do { do {
waitpid(pid, &status, WUNTRACED); waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status)); } while (!WIFEXITED(status) && !WIFSIGNALED(status));
} }
return 1; return 1;
} }
int fishExecute(WordArray *array) { int fishExecute(WordArray *array) {
int i; int i;
if (array->size < 0) if (array->size < 0)
return 1; return 1;
for (i=0; i < getNbBuiltins(); i++){ for (i=0; i < getNbBuiltins(); i++){
if (!strcmp(array->words[0], getBuiltinCommandsStr()[i])){ if (!strcmp(array->words[0], getBuiltinCommandsStr()[i])){
return getBuiltinCommands()[i](array); return getBuiltinCommands()[i](array);
} }
} }
return fishLoad(array); return fishLoad(array);
} }

View File

@ -8,12 +8,12 @@
/* Custom types */ /* Custom types */
typedef struct { typedef struct {
char ** words; char ** words;
int size; int size;
} WordArray; } WordArray;
typedef struct { typedef struct {
char *PS1; char *PS1;
} Settings; } Settings;

10
main.c
View File

@ -1,9 +1,13 @@
#include <stdlib.h> #include <stdlib.h>
#include "fish_shell/fish_core.h" #include "fish_shell/fish_core.h"
#include "fish_shell/fish_types.h"
int main() { int main() {
/* todo load config file */ /* todo load config file */
fishLoop(getSettings()); Settings *s = getSettings();
fishLoop(s);
free(s->PS1);
free(s);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }