Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added Code/.hist_list.txt
Empty file.
110 changes: 0 additions & 110 deletions Code/Task 1/main.c

This file was deleted.

16 changes: 0 additions & 16 deletions Code/Task 1/main.h

This file was deleted.

100 changes: 0 additions & 100 deletions Code/Task 2/helper.c

This file was deleted.

16 changes: 0 additions & 16 deletions Code/Task 2/helper.h

This file was deleted.

21 changes: 0 additions & 21 deletions Code/Task 2/main.c

This file was deleted.

86 changes: 86 additions & 0 deletions Code/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "utils/userinput.h"
#include "utils/history.h"
#include "utils/commands.h"

char original_path[1024];

/** @brief Setup the initial values for the shell */
void setupShell(void) {
char *path = getenv("PATH");

if(path != NULL) {
strcpy(original_path, path);
}

char *home = getenv("HOME");

if(home != NULL) {
if(chdir(home) != 0) {
printf("Error: Could not change to HOME directory\n");
}
}

char cwd[1024];

if(getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working directory: %s\n", cwd);
}

// TASK 6 GOES HERE
// Read saved history from file
// History is loaded using getCommandHistory() in main
}

// MAIN LOOP

/** @brief Display the shell prompt & process user input */
int main(void) {
// Set PATH initially
setupShell();

// Get command history
history = getCommandHistory(); // Makes use of history global variable

char input[1024];
char *tokens[512];

while(1) {
// Print prompt
printf("> ");

// Get & check user input for NULL
if(fgets(input, sizeof(input), stdin) == NULL) {
break; // EOF (Ctrl + D OR Ctrl + D)
}

// Input validation
if(strlen(input) > 512 || input[0] == '\n') {
printf("Error occurred with the input\n");
break;
}

// Duplicate input as tokenizeInput irreversibly changes input when used
char *dupe_input = malloc(strlen(input) + 1);
strcpy(dupe_input, input);

// Tokenize input String into tokens
tokenizeInput(input, tokens);

// Check for NULL command name
if(tokens[0] == NULL) {
printf("Command is NULL");
break;
}

// Check the tokens against known commands
checkForCommands(tokens, history, dupe_input);
free(dupe_input); // Free duplicated input upon completion
}

return 0;
}
Loading