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

More work in the history

parent b922752f
Loading
Loading
Loading
Loading
Loading
+34 −28
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
#define READ 0


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

int main() {
    // Setup Some Vars
@@ -48,7 +48,7 @@ int main() {
            if (tokens[1] == NULL) {
                fprintf(stderr, "bsh: expected one argument for cd");
                free(tokens);
                storeHist(readBuff, &historyPos, historyBuff);
                storeHist(tokens, &historyPos, historyBuff, numTokens);
                continue;
            } else {
                if (strcmp(tokens[1], "~") == 0) {
@@ -66,52 +66,53 @@ int main() {
        if (strcmp(tokens[0], "history") == 0) {
            if (historyPos == -1) {
                fprintf(stderr, "\nNo commands in history\n");
                storeHist(readBuff, &historyPos, historyBuff);
                storeHist(readBuff, &historyPos, historyBuff, 0);
                free(tokens);
                continue;
            }
            for (int i = historyPos; i >= 0; i--) {
                fprintf(stdout, "%2d %s\n", i + 1, historyBuff[i]);
            }
            storeHist(readBuff, &historyPos, historyBuff);
            storeHist(readBuff, &historyPos, historyBuff, 0);
            free(tokens);
            continue;
        }


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

        char *ptr;
        if (*tokens[0] == '!' && strcmp(tokens[0], "!!") != 0) {
            errno = 0;
            // Using strtol allows us to do error checking
            long cmdNum = strtol(tokens[0] + 1, &ptr, 10);
            *tokens[0] = '0';
            long cmdNum = strtol(tokens[0], &ptr, 10);
            if (errno != 0) {
                printf("Invalid history command lookup!");
                printf("\nInvalid history command lookup!\n");
                free(tokens);
                free(ptr);
                continue;
            }
            if (cmdNum <= 0 || cmdNum > historyPos) {
                printf("Invalid history command lookup!");
                printf("\nInvalid history command lookup!\n");
                free(tokens);
                free(ptr);
                continue;
            } else {
                numTokens = tokenize(historyBuff[cmdNum - 1], tokens);
            }
            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);
        storeHist(tokens, &historyPos, historyBuff, numTokens);

        int runInBackground = 0;
        if (strcmp(lastToken, "&") == TRUE) {
@@ -133,12 +134,17 @@ int main() {
    }
}

void storeHist(char *readBuff, int*  historyPos, char historyBuff[50][BUFFSIZE]) {
void storeHist(char **readBuff, int *historyPos, char historyBuff[50][5000], int numTokens) {
    // History store stuff
    if (strlen(readBuff) > 0) {
    if (*historyPos < 49) {
        *historyPos = *historyPos + 1;
                strcpy(historyBuff[*historyPos], readBuff);
        for (int i = 0; i < numTokens - 1; i++) {
            strcat(historyBuff[*historyPos], readBuff[i]);
            int nextIter = i+1;
            if (nextIter < numTokens - 1) {
                strcat(historyBuff[*historyPos], " ");
            }
        }

    }
}
 No newline at end of file