#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <signal.h> #include <libgen.h> #include <stdlib.h> #include <stdarg.h> #include "utils.h" int main() { jobCount = 0; // Initializes the number of suspended jobs to 0 shell = getpid(); // Process id of the shell signal(SIGINT, SIG_IGN); // Ignores SIGINT signal in shell signal(SIGQUIT, SIG_IGN); // Ignores SIGQUIT signal in shell signal(SIGTSTP, SIG_IGN); // Ignores SIGTSTP signal in shell getcwd(baseDirectory, MAXDIRECTORY); // .. strcpy(curDirectory, baseDirectory); // Gets the current working directory while (1) { // Main loop char *curBaseName = strrchr(curDirectory, '/'); // Temporary variable for the current base directory printf("[nyush %s]$ ", (curBaseName && strcmp(curBaseName + 1, "")) ? curBaseName + 1 : curDirectory); // Print the prompt fflush(stdout); // Flushes prompt to STDODUT commandLine = malloc(sizeof(char) * MAXLENGTH); // Allocates memory for the command line readCommandLine(commandLine); // Reads the command line parsedTokens = malloc(sizeof(char) * MAXLENGTH * MAXLENGTH); // Allocates memory for the parsed tokens pipedCommands = malloc(sizeof(char) * MAXLENGTH * MAXLENGTH); // Allocates memory for the piped commands inputFileName = NULL; // Input file name outputFileName = NULL; // Output file name isBackground = 0; // Sets default be in background pipeCount = checkPipingAndRedirection(commandLine); // Checks piping and redirection, gets the number of pipes and updates global variables if (pipeCount < 0) { // Piping and redirection check failed freeAll("122", commandLine, parsedTokens, pipedCommands); // Frees all allocated memory } else if (pipeCount == 0) { // Command line has no piping char *tokenBuffer = malloc(sizeof(char) * MAXLENGTH); // Creates token buffer tokenCount = parseNoPiping(commandLine, tokenBuffer); // Parses the command line, gets the number of tokens executeCommandNoPiping(parsedTokens, tokenCount, tokenBuffer); // Executes the command line } else { // Command line has piping char *commandBuffer = malloc(sizeof(char) * MAXLENGTH); // Creates subcommand buffer subCommandCount = parseByPipe(commandLine, commandBuffer); // Parses the command line, gets the number of subcommands executeCommandWithPiping(pipedCommands, subCommandCount, commandBuffer); // Executes the command line } } return 0; // Ends and terminates }