123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- /*
- * File: mysh.c
- * Author: Arthur Brandao
- *
- * Created on 31 octobre 2018, 12:43
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include "error.h"
- #include "str.h"
- #include "parser.h"
- #include "command.h"
- #include "mysh.h"
- #include <sys/types.h>
- #include <sys/stat.h>
- /* --- Extern --- */
- extern Error error;
- /* --- Fonctions privées --- */
- void test_write() {
- char* a = "azerty\n";
- int tmp = write(1, a, strlen(a));
- printf("%d\n", tmp);
- }
- /* --- Fonctions utilitaires --- */
- void show_current_dir(const char* before, const char* after) {
- char buffer[BUFFER_SIZE];
- if (getcwd(buffer, sizeof (buffer)) == NULL) {
- addperror("Erreur getcwd()");
- } else {
- if(before == NULL && after == NULL){
- printf("%s", buffer);
- } else if(before == NULL){
- printf("%s%s", buffer, after);
- } else if(after == NULL){
- printf("%s%s", before, buffer);
- } else {
- printf("%s%s%s", before, buffer, after);
- }
- }
- }
- /* --- Main --- */
- int main(int argc, char* argv[]) {
- CommandTab ct;
- char str[BUFFER_SIZE];
- int a;
- //Initialisation erreur
- error_finit("mysh.log");
- //Recup ligne
- //printf("%s\n", fgets(str, 500, stdin));&
- memset(str, 0, 500);
- a = read(STDIN, str, 500);
- printf("%s\n", str);
- //Separe les commandes
- a = parse_line(&ct, str);
- if(a == SHELL_ERR){
- addserror("Erreur lors du parse de la ligne");
- error.exit_err();
- }
- //Parse les commandes
- a = parse_all_command(&ct);
- if(a == SHELL_FAIL){
- addserror("Erreur lors du parse des commandes");
- error.exit_err();
- }
- //Affiche resultat
- for (int i = 0; i < ct.length; i++) {
- Command* c = ct.cmd[i];
- printf("Commande %d (%s) : \n", i, c->name);
- for (int j = 0; j < c->argc; j++) {
- printf(" Argument %d : %s\n", j, c->argv[j]);
- }
- printf("Commande en fond : %d\n\n", ct.bck);
- //Si c'est une commande interne on l'execute
- if(is_internal_cmd(ct.cmd[i]->name)){
- show_current_dir(NULL, "\n");
- printf("Result : %d\n", launch_internal_command(ct.cmd[i]));
- show_current_dir(NULL, "\n");
- }
- }
- //Supprime
- clean_command(&ct);
- error.exit();
- }
|