1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-12-22 15:51:09 +00:00

Support des commandes internes basiques et implémentation rudimentaire des commandes générales

This commit is contained in:
Antoine Bartuccio 2017-05-14 20:54:39 +02:00
parent aed934b3a7
commit d089b4c783
7 changed files with 177 additions and 28 deletions

View File

@ -4,5 +4,5 @@ project(fish)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra")
set(SOURCE_FILES main.c fish_lib/fish_lib.c fish_lib/fish_lib.h)
set(SOURCE_FILES main.c fish_shell/fish_core.c fish_shell/fish_core.h fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_types.h)
add_executable(fish ${SOURCE_FILES})

View 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*);
}

View 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

View File

@ -4,7 +4,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fish_lib.h"
#include <unistd.h>
#include "fish_core.h"
#define FISH_BUFFER_SIZE 1024
#define FISH_TOKENS " \t\r\n\a"
@ -12,24 +13,20 @@
void fishLoop(Settings * settings){
char * line = NULL;
WordArray * splited = NULL;
int exited = 0;
int i;
int status = 1;
while (!exited) {
do {
printf("%s", settings->PS1);
line = fishReadLine();
line = fishExpand(line);
splited = split(line, FISH_TOKENS);
for (i = 0; i < splited->size; i++) {
printf("%s\n", splited->words[i]);
if(!strcmp(splited->words[i], "exit"))
exited = 1;
}
status = fishExecute(splited);
freeWordArray(splited);
free(line);
}
} while(status);
}
int countSeparators(char *string, char *separators) {
@ -101,11 +98,14 @@ char *fishReadLine() {
while (1){
c = getchar();
if (c == EOF || c == '\n'){
line[position] = '\0';
return line;
} else {
line[position] = (char) c;
switch (c){
case '\n':
line[position] = '\0';
return line;
case EOF:
exit(EXIT_SUCCESS);
default:
line[position] = (char) c;
}
position++;
@ -134,8 +134,48 @@ Settings *getSettings() {
fprintf(stderr, "fish: Error allocating fucking settings");
exit(EXIT_FAILURE);
}
s->PS1 = strdup("~>");
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);
}

View File

@ -2,17 +2,12 @@
// Created by Antoine Bartuccio on 11/05/2017.
//
#ifndef FISH_FISH_LIB_H
#define FISH_FISH_LIB_H
#ifndef FISH_FISH_CORE_H
#define FISH_FISH_CORE_H
typedef struct {
char ** words;
int size;
} WordArray;
typedef struct {
char *PS1;
} Settings;
#include "fish_types.h"
#include "fish_commands.h"
/* WordArray functions */
@ -34,4 +29,8 @@ int countSeparators(char *string, char *separators);
char * fishExpand(char* line);
#endif //FISH_FISH_LIB_H
int fishLoad(WordArray *array);
int fishExecute(WordArray *array);
#endif //FISH_FISH_CORE_H

22
fish_shell/fish_types.h Normal file
View 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

2
main.c
View File

@ -1,5 +1,5 @@
#include <stdlib.h>
#include "fish_lib/fish_lib.h"
#include "fish_shell/fish_core.h"
int main() {
/* todo load config file */