Skip to content
Snippets Groups Projects
Commit 7bd37d2c authored by krasimir's avatar krasimir
Browse files

Stage 5

parent 958ad600
No related branches found
No related tags found
No related merge requests found
......@@ -7,12 +7,18 @@
#include "shell.h"
#include <sys/wait.h>
int counter = 0;
int historySize = 0;
Command history[MAX_HISTORY_LENGTH];
void run_shell() {
int status;
String input, arguments[MAX_INPUT_LENGTH], originalPath;
char directory[MAX_PATH_LENGTH];
for (int i = 0; i < MAX_HISTORY_LENGTH; i++) {
history[i].line = malloc(sizeof(char) * MAX_INPUT_LENGTH);
}
// Set the current directory to HOME
if(getenv("HOME") != NULL) {
......@@ -33,6 +39,7 @@ void run_shell() {
// Alocate memory for the input & read user's input
input = malloc(MAX_INPUT_LENGTH * sizeof(char));
read_input(input);
add_history(input);
// Tokenize to get the arguments
tokenize(input, arguments);
......@@ -55,6 +62,23 @@ void read_input(String input) {
exit(EXIT_STATUS);
}
}
void add_history(String input) {
if (strcspn(input, "!") > 0) {
history[counter].number = counter + 1;
strcpy(history[counter].line, input);
counter = (counter + 1) % MAX_HISTORY_LENGTH;
if (historySize <= 20) {
historySize++;
}
}
}
// 1 - ls
// 2 - cd ..
// 3 - getpath
// ...
// 20 - ...
void tokenize(String input, String arguments[]) {
String token = strtok(input, TOKENIZE_DELIM);
int index = 1;
......@@ -91,17 +115,22 @@ int run_command(String arguments[]) {
setenv("/usr", "PATH", 1);
}
//Implement the CD - command that changes the current directory.
// if(strcmp(arguments[0], "cd") == 0) {
// if(arguments[1] == NULL) {
// chdir(getenv("HOME"));
// }
if (strcmp(arguments[0], "history") == 0) {
print_history();
return SUCCESS_STATUS;
}
// if(chdir(arguments[1]) < 0) {
// perror("ERROR");
// }
// return SUCCESS_STATUS;
// }
//Implement the CD - command that changes the current directory.
if(strcmp(arguments[0], "cd") == 0) {
if(arguments[1] == NULL) {
chdir(getenv("HOME"));
}
if(chdir(arguments[1]) < 0) {
perror("ERROR");
}
return SUCCESS_STATUS;
}
// If the command was not found above, fork a child process and run the command there.
......@@ -117,6 +146,21 @@ void get_directory(char directory[]) {
}
}
void print_history() {
int index = 0;
int commandCounter = 0;
while (commandCounter < historySize && history[index].number != 1) {
index++;
commandCounter++;
}
commandCounter = 1;
while (commandCounter < historySize) {
printf("%d - %s", commandCounter, history[index].line);
index = (index + 1) % MAX_HISTORY_LENGTH;
commandCounter++;
}
}
int execute_system_commands(String arguments[]) {
int status;
......@@ -136,4 +180,4 @@ int execute_system_commands(String arguments[]) {
}
return SUCCESS_STATUS;
}
\ No newline at end of file
}
/* shell.h */
#define EXIT_STATUS 0
#define SUCCESS_STATUS 1
#define MAX_HISTORY_LENGTH 20
#define MAX_INPUT_LENGTH 512
#define MAX_PATH_LENGTH 512
#define TOKENIZE_DELIM " \t\n|><&;"
......@@ -18,6 +19,14 @@ typedef enum {
true = 1
} Boolean;
/*
* Define command history struct
*/
typedef struct {
String line;
int number;
} Command;
/**
* Run the shell program
*/
......@@ -28,6 +37,10 @@ void get_directory(char directory[]);
*/
int execute_system_commands(String argruments[]);
/*
* Prints history
*/
void print_history();
/**
* Run the shell program
......@@ -39,6 +52,11 @@ int run_command(String argruments[]);
*/
void tokenize(String input, String argruments[]);
/*
* Add input to history
*/
void add_history(String input);
/**
* Run the shell program
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment