mirror of
https://gitlab.com/klmp200/fish.git
synced 2024-11-22 00:33:20 +00:00
Support des commandes internes basiques et implémentation rudimentaire des commandes générales
This commit is contained in:
parent
aed934b3a7
commit
d089b4c783
@ -4,5 +4,5 @@ project(fish)
|
|||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra")
|
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})
|
add_executable(fish ${SOURCE_FILES})
|
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
|
@ -4,7 +4,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "fish_lib.h"
|
#include <unistd.h>
|
||||||
|
#include "fish_core.h"
|
||||||
|
|
||||||
#define FISH_BUFFER_SIZE 1024
|
#define FISH_BUFFER_SIZE 1024
|
||||||
#define FISH_TOKENS " \t\r\n\a"
|
#define FISH_TOKENS " \t\r\n\a"
|
||||||
@ -12,24 +13,20 @@
|
|||||||
void fishLoop(Settings * settings){
|
void fishLoop(Settings * settings){
|
||||||
char * line = NULL;
|
char * line = NULL;
|
||||||
WordArray * splited = NULL;
|
WordArray * splited = NULL;
|
||||||
int exited = 0;
|
int status = 1;
|
||||||
int i;
|
|
||||||
|
|
||||||
while (!exited) {
|
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);
|
||||||
for (i = 0; i < splited->size; i++) {
|
|
||||||
printf("%s\n", splited->words[i]);
|
status = fishExecute(splited);
|
||||||
if(!strcmp(splited->words[i], "exit"))
|
|
||||||
exited = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
freeWordArray(splited);
|
freeWordArray(splited);
|
||||||
free(line);
|
free(line);
|
||||||
}
|
} while(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
int countSeparators(char *string, char *separators) {
|
int countSeparators(char *string, char *separators) {
|
||||||
@ -101,11 +98,14 @@ char *fishReadLine() {
|
|||||||
while (1){
|
while (1){
|
||||||
c = getchar();
|
c = getchar();
|
||||||
|
|
||||||
if (c == EOF || c == '\n'){
|
switch (c){
|
||||||
line[position] = '\0';
|
case '\n':
|
||||||
return line;
|
line[position] = '\0';
|
||||||
} else {
|
return line;
|
||||||
line[position] = (char) c;
|
case EOF:
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
default:
|
||||||
|
line[position] = (char) c;
|
||||||
}
|
}
|
||||||
|
|
||||||
position++;
|
position++;
|
||||||
@ -134,8 +134,48 @@ Settings *getSettings() {
|
|||||||
fprintf(stderr, "fish: Error allocating fucking settings");
|
fprintf(stderr, "fish: Error allocating fucking settings");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
s->PS1 = strdup("~>");
|
s->PS1 = strdup("\n~>");
|
||||||
|
|
||||||
return s;
|
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);
|
||||||
|
}
|
@ -2,17 +2,12 @@
|
|||||||
// Created by Antoine Bartuccio on 11/05/2017.
|
// Created by Antoine Bartuccio on 11/05/2017.
|
||||||
//
|
//
|
||||||
|
|
||||||
#ifndef FISH_FISH_LIB_H
|
#ifndef FISH_FISH_CORE_H
|
||||||
#define FISH_FISH_LIB_H
|
#define FISH_FISH_CORE_H
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
char ** words;
|
|
||||||
int size;
|
|
||||||
} WordArray;
|
|
||||||
|
|
||||||
typedef struct {
|
#include "fish_types.h"
|
||||||
char *PS1;
|
#include "fish_commands.h"
|
||||||
} Settings;
|
|
||||||
|
|
||||||
/* WordArray functions */
|
/* WordArray functions */
|
||||||
|
|
||||||
@ -34,4 +29,8 @@ int countSeparators(char *string, char *separators);
|
|||||||
|
|
||||||
char * fishExpand(char* line);
|
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
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
|
Loading…
Reference in New Issue
Block a user