mirror of
https://gitlab.com/klmp200/fish.git
synced 2025-07-11 12:19:23 +00:00
Support des commandes internes basiques et implémentation rudimentaire des commandes générales
This commit is contained in:
64
fish_shell/fish_commands.c
Normal file
64
fish_shell/fish_commands.c
Normal file
@ -0,0 +1,64 @@
|
||||
//
|
||||
// Created by Antoine Bartuccio on 14/05/2017.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include "fish_core.h"
|
||||
|
||||
/* Necessary global variables */
|
||||
char * builtinCommandsStr[] = {
|
||||
"cd",
|
||||
"help",
|
||||
"exit"
|
||||
};
|
||||
|
||||
|
||||
builtinCommand *builtinCommands[] = {
|
||||
&fishCd,
|
||||
&fishHelp,
|
||||
&fishExit
|
||||
};
|
||||
|
||||
char ** getBuiltinCommandsStr(){
|
||||
return builtinCommandsStr;
|
||||
}
|
||||
|
||||
builtinCommand **getBuiltinCommands(){
|
||||
return builtinCommands;
|
||||
}
|
||||
|
||||
int fishCd(WordArray *args) {
|
||||
if (args->size < 2){
|
||||
fprintf(stderr, "fish: Où sont les arguments de ta commande \"cd\" connard ?!\n");
|
||||
} else {
|
||||
if (chdir(args->words[1]) != 0){
|
||||
perror("fish");
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fishHelp(WordArray *args) {
|
||||
int i;
|
||||
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("Les commandes suivantes sont internes :\nls");
|
||||
for (i=0; i < getNbBuiltins(); i++){
|
||||
printf("\t%s\n", builtinCommandsStr[i]);
|
||||
}
|
||||
printf("Et sinon pour le reste, RTFM !");
|
||||
if (args->size > 0)
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fishExit(WordArray *args) {
|
||||
if (args->size != 1)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getNbBuiltins() {
|
||||
return sizeof(builtinCommandsStr) / sizeof(char*);
|
||||
}
|
24
fish_shell/fish_commands.h
Normal file
24
fish_shell/fish_commands.h
Normal file
@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by Antoine Bartuccio on 14/05/2017.
|
||||
//
|
||||
|
||||
#ifndef FISH_FISH_COMMANDS_H
|
||||
#define FISH_FISH_COMMANDS_H
|
||||
|
||||
#include "fish_core.h"
|
||||
|
||||
/* Getters */
|
||||
|
||||
char ** getBuiltinCommandsStr ();
|
||||
builtinCommand **getBuiltinCommands();
|
||||
int getNbBuiltins();
|
||||
|
||||
/* Built in shell commands */
|
||||
|
||||
int fishCd(WordArray * args);
|
||||
|
||||
int fishHelp(WordArray * args);
|
||||
|
||||
int fishExit(WordArray * args);
|
||||
|
||||
#endif //FISH_FISH_COMMANDS_H
|
181
fish_shell/fish_core.c
Normal file
181
fish_shell/fish_core.c
Normal file
@ -0,0 +1,181 @@
|
||||
//
|
||||
// Created by Antoine Bartuccio on 11/05/2017.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "fish_core.h"
|
||||
|
||||
#define FISH_BUFFER_SIZE 1024
|
||||
#define FISH_TOKENS " \t\r\n\a"
|
||||
|
||||
void fishLoop(Settings * settings){
|
||||
char * line = NULL;
|
||||
WordArray * splited = NULL;
|
||||
int status = 1;
|
||||
|
||||
do {
|
||||
printf("%s", settings->PS1);
|
||||
line = fishReadLine();
|
||||
line = fishExpand(line);
|
||||
|
||||
splited = split(line, FISH_TOKENS);
|
||||
|
||||
status = fishExecute(splited);
|
||||
|
||||
freeWordArray(splited);
|
||||
free(line);
|
||||
} while(status);
|
||||
}
|
||||
|
||||
int countSeparators(char *string, char *separators) {
|
||||
int nb = 0;
|
||||
int i = 0;
|
||||
int k = 0;
|
||||
while (string[i] != '\0'){
|
||||
while (separators[k] != '\0'){
|
||||
if (string[i] == separators[k]){
|
||||
nb++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
i++;
|
||||
k = 0;
|
||||
}
|
||||
return nb;
|
||||
}
|
||||
|
||||
WordArray * split(char *string, char *separator){
|
||||
int array_size = countSeparators(string, separator) + 1;
|
||||
WordArray *tokens = (WordArray*) malloc(sizeof(WordArray));
|
||||
char *to_delete = strdup(string);
|
||||
char *token = NULL;
|
||||
int i = 0;
|
||||
|
||||
if (tokens != NULL){
|
||||
tokens->words = (char **) malloc(sizeof(char*) * array_size);
|
||||
tokens->size = array_size;
|
||||
}
|
||||
|
||||
if (tokens == NULL || to_delete == NULL || tokens->words == NULL){
|
||||
fprintf(stderr, "fish: Error allocating fucking pointer !");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while((token = strsep(&to_delete, separator)) != NULL){
|
||||
tokens->words[i] = strdup(token);
|
||||
i++;
|
||||
}
|
||||
|
||||
free(to_delete);
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
void freeWordArray(WordArray *array) {
|
||||
int i;
|
||||
if (array != NULL) {
|
||||
for (i = 0; i < array->size; i++) {
|
||||
free(array->words[i]);
|
||||
}
|
||||
free(array);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
char *fishReadLine() {
|
||||
size_t bufferSize = FISH_BUFFER_SIZE;
|
||||
int position = 0;
|
||||
char *line = malloc(sizeof(char*) * bufferSize);
|
||||
int c;
|
||||
|
||||
if (line == NULL){
|
||||
fprintf(stderr, "fish: Error allocating fucking buffer shit !");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while (1){
|
||||
c = getchar();
|
||||
|
||||
switch (c){
|
||||
case '\n':
|
||||
line[position] = '\0';
|
||||
return line;
|
||||
case EOF:
|
||||
exit(EXIT_SUCCESS);
|
||||
default:
|
||||
line[position] = (char) c;
|
||||
}
|
||||
|
||||
position++;
|
||||
|
||||
if ((size_t) position > bufferSize){
|
||||
bufferSize+=bufferSize;
|
||||
line = realloc(line, bufferSize);
|
||||
if (line == NULL){
|
||||
fprintf(stderr, "fish: Error allocating fucking buffer shit !");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *fishExpand(char *line) {
|
||||
return line;
|
||||
}
|
||||
|
||||
Settings *getSettings() {
|
||||
Settings *s = (Settings*) malloc(sizeof(Settings));
|
||||
if (s == NULL){
|
||||
fprintf(stderr, "fish: Error allocating fucking settings");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
s->PS1 = strdup("\n~>");
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int fishLoad(WordArray *array) {
|
||||
pid_t pid;
|
||||
int status;
|
||||
|
||||
pid = fork();
|
||||
if (pid == 0){
|
||||
/* Executes only in the child process */
|
||||
if (execvp(array->words[0], array->words) == -1){
|
||||
/* Error during system call */
|
||||
perror("fish");
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
} else if (pid < 0){
|
||||
/* Fork failed */
|
||||
perror("fish");
|
||||
} else {
|
||||
/* Handle parent process */
|
||||
|
||||
/* Wait for the child process to finish */
|
||||
do {
|
||||
waitpid(pid, &status, WUNTRACED);
|
||||
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fishExecute(WordArray *array) {
|
||||
int i;
|
||||
if (array->size < 0)
|
||||
return 1;
|
||||
|
||||
for (i=0; i < getNbBuiltins(); i++){
|
||||
if (!strcmp(array->words[0], getBuiltinCommandsStr()[i])){
|
||||
return getBuiltinCommands()[i](array);
|
||||
}
|
||||
}
|
||||
|
||||
return fishLoad(array);
|
||||
}
|
36
fish_shell/fish_core.h
Normal file
36
fish_shell/fish_core.h
Normal file
@ -0,0 +1,36 @@
|
||||
//
|
||||
// Created by Antoine Bartuccio on 11/05/2017.
|
||||
//
|
||||
|
||||
#ifndef FISH_FISH_CORE_H
|
||||
#define FISH_FISH_CORE_H
|
||||
|
||||
|
||||
#include "fish_types.h"
|
||||
#include "fish_commands.h"
|
||||
|
||||
/* WordArray functions */
|
||||
|
||||
WordArray * split(char *string, char *separator);
|
||||
void freeWordArray(WordArray *array);
|
||||
|
||||
/* Settings functions */
|
||||
|
||||
Settings * getSettings();
|
||||
|
||||
|
||||
/* General purpose functions */
|
||||
|
||||
void fishLoop(Settings * settings);
|
||||
|
||||
char * fishReadLine();
|
||||
|
||||
int countSeparators(char *string, char *separators);
|
||||
|
||||
char * fishExpand(char* line);
|
||||
|
||||
int fishLoad(WordArray *array);
|
||||
|
||||
int fishExecute(WordArray *array);
|
||||
|
||||
#endif //FISH_FISH_CORE_H
|
22
fish_shell/fish_types.h
Normal file
22
fish_shell/fish_types.h
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by Antoine Bartuccio on 14/05/2017.
|
||||
//
|
||||
|
||||
#ifndef FISH_FISH_TYPES_H
|
||||
#define FISH_FISH_TYPES_H
|
||||
|
||||
/* Custom types */
|
||||
|
||||
typedef struct {
|
||||
char ** words;
|
||||
int size;
|
||||
} WordArray;
|
||||
|
||||
typedef struct {
|
||||
char *PS1;
|
||||
} Settings;
|
||||
|
||||
|
||||
typedef int (builtinCommand) (WordArray*);
|
||||
|
||||
#endif //FISH_FISH_TYPES_H
|
Reference in New Issue
Block a user