Commit 62b4a6d0 authored by Matthew Burket's avatar Matthew Burket
Browse files

Added more commnets

parent 03fb60f4
Loading
Loading
Loading
Loading
Loading
+28 −2
Original line number Diff line number Diff line
@@ -39,36 +39,53 @@ int main() {
            exit(EXIT_SUCCESS);
        }

        // Figure out if we need to pipe things
        char *doMore = strchr(readBuff, '|');
        if (doMore != NULL) {
            readBuff = strtok(readBuff, "|");
        }
        // Max of 10 commands in ; chains
        char commands[10][BUFFSIZE];
        int numCmds = 0;
        // If we have many command add them to the
        // command array
        char *doMany = strchr(readBuff, ';');
        if (doMany != NULL) {
            // Don't overrun the array
            if (numCmds >= 10) {
                break;
            }
            // Make it thread safe
            char *strtok_save;
            // ; seperate commands
            char *del = ";";
            // First time
            char * cmdTok = strtok_r(readBuff, del, &strtok_save);
            strcpy(commands[0], cmdTok);
            numCmds = 1;
            while (cmdTok != NULL) {
                // Per the docs null for all other calls to strtok_r
                cmdTok = strtok_r(NULL, del, &strtok_save);
                // Don't segfault
                // Do while might be better?
                if (cmdTok == NULL) {
                    break;
                }
                // Copy to commands array
                strcpy(commands[numCmds], cmdTok);
                // We got one more command
                numCmds++;
            }

        } else {
            // Default for one command
            strcpy(commands[0], readBuff);
            numCmds = 1;
        }

        // Run this loop for each command
        // This supports commands like the following:
        // date;ls;cal;ps
        for (int i = 0; i < numCmds; i++) {
            char **tokens = malloc(sizeof(char) * 5000);
            int numTokens = tokenize(commands[i], tokens);
@@ -93,6 +110,7 @@ int main() {
                }
            }

            // Logic for history
            if (strcmp(tokens[0], "history") == 0) {
                if (historyPos == -1) {
                    fprintf(stderr, "\nNo commands in history\n");
@@ -108,7 +126,7 @@ int main() {
                continue;
            }


            // Logic for !!
            if (strcmp(tokens[0], "!!") == 0) {
                if (historyPos == -1) {
                    fprintf(stderr, "\nbsh: No such command in history.\n");
@@ -120,6 +138,8 @@ int main() {
                }
            }


            // Logic for !N
            char *ptr;
            if (*tokens[0] == '!' && strcmp(tokens[0], "!!") != 0) {
                errno = 0;
@@ -163,20 +183,26 @@ int main() {
            // https://stackoverflow.com/a/13803962
            if (fork() == 0) {
                if (doMoreCheck != 0) {
                    // Close stdout
                    close(STDOUT_FILENO);
                    // DUP stdout to the pipe
                    dup(thePipe[1]);
                    // Close the pipes
                    close(thePipe[0]);
                    close(thePipe[1]);
                }
                // exec it!
                int returnCode = execvp(tokens[0], tokens);
                if (returnCode == -1) {
                    // Command not found
                    // Let the user know
                    fprintf(stdout, "bsh: Command not found\n");
                }
                // Clean up and we are done;
                printf("\n");
                exit(1);
            } else {
                // Wait where if no & and not a pipe
                // Wait where if no "&" and not a pipe
                if (runInBackground == 0 && doMoreCheck == 0) {
                    wait(NULL);
                }