Commit f9aa2b3d authored by Matthew Burket's avatar Matthew Burket
Browse files

More work on history

parent a4dface0
Loading
Loading
Loading
Loading
+71 −6
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>


#include "tokenize.h"
#include "file_operations.h"
@@ -11,10 +13,14 @@
#define READ 0


void storeHist(char *readBuff, int*  historyPos, char historyBuff[50][BUFFSIZE]);

int main() {
    // Setup Some Vars
    int should_run = 1;
    char *readBuff = malloc(BUFFSIZE);
    char historyBuff[50][BUFFSIZE];
    int historyPos = -1;
    char *HOME = getenv("HOME");
    // Copyright statement
    fprintf(stdout, "Welcome to Burket Shell (bsh)\n");
@@ -26,10 +32,6 @@ int main() {
        fprintf(stdout, "bsh # ");
        // Getting User Input
        fgets(readBuff, sizeof(char) * 5000, stdin);
        // History store stuff
        if (strlen(readBuff) > 0) {
            //
        }
        // Remove the newline from fgets
        strtok(readBuff, "\n");
        if (strcmp("exit", readBuff) == TRUE) {
@@ -37,12 +39,16 @@ int main() {
            free(readBuff);
            exit(EXIT_SUCCESS);
        }

        char **tokens = malloc(sizeof(char) * 5000);
        int numTokens = tokenize(readBuff, tokens);
        char *lastToken = tokens[numTokens - 2];

        if (strcmp(tokens[0], "cd") == TRUE) {
            if (tokens[1] == NULL) {
                fprintf(stderr, "bsh: expected one argument for cd");
                free(tokens);
                storeHist(readBuff, &historyPos, historyBuff);
                continue;
            } else {
                if (strcmp(tokens[1], "~") == 0) {
@@ -51,13 +57,62 @@ int main() {
                } else if (strcmp(tokens[1], "-") == 0) {
                    chdir(getenv("OLDPWD"));
                    strcpy(tokens[1], getenv("OLDPWD"));
                }
                else {
                } else {
                    chdir(tokens[1]);
                }
            }
        }

        if (strcmp(tokens[0], "history") == 0) {
            if (historyPos == -1) {
                fprintf(stderr, "\nNo commands in history\n");
                storeHist(readBuff, &historyPos, historyBuff);
                free(tokens);
                continue;
            }
            for (int i = historyPos; i >= 0; i--) {
                fprintf(stdout, "%2d %s\n", i + 1, historyBuff[i]);
            }
            storeHist(readBuff, &historyPos, historyBuff);
            free(tokens);
            continue;
        }


        char *ptr = malloc(sizeof(char) * 10);
        if (*tokens[0] == '!') {
            errno = 0;
            // Using strtol allows us to do error checking
            long cmdNum = strtol(tokens[0] + 1, &ptr, 10);
            if (errno != 0) {
                printf("Invalid history command lookup!");
                free(tokens);
                free(ptr);
                continue;
            }
            if (cmdNum <= 0 || cmdNum > historyPos) {
                printf("Invalid history command lookup!");
                free(tokens);
                free(ptr);
                continue;
            }
            free(ptr);
        }


        if (strcmp(tokens[0], "!!") == 0) {
            if (historyPos == -1) {
                fprintf(stderr, "\nbsh: No such command in history.\n");
                storeHist(readBuff, &historyPos, historyBuff);
                free(tokens);
                continue;
            } else {
                strcpy(readBuff, historyBuff[historyPos]);
            }
        }

        storeHist(readBuff, &historyPos, historyBuff);

        int runInBackground = 0;
        if (strcmp(lastToken, "&") == TRUE) {
            runInBackground = 1;
@@ -77,3 +132,13 @@ int main() {
        free(tokens);
    }
}

void storeHist(char *readBuff, int*  historyPos, char historyBuff[50][BUFFSIZE]) {
    // History store stuff
    if (strlen(readBuff) > 0) {
            if (*historyPos < 49) {
                *historyPos = *historyPos + 1;
                strcpy(historyBuff[*historyPos], readBuff);
            }
        }
}
 No newline at end of file