//this function does what's needed for each built-in command. Note: these commands cannot be //run in the background. Returns 1 if a built-in command is detected, 0 if not int builtInCmd(char *argList[], struct hist *histList, int count) { int cdCheck; int isBuiltIn = 1; //builtin exit command if (strcmp(argList[0], "exit") == 0) { printf("Exiting shell...\n\n"); exit(0); //builtin cd (change directory) command, implemented with chdir() system call } else if (strcmp(argList[0], "cd") == 0) { if ((cdCheck = chdir(argList[1]) == -1)) { printf("chdir() failure... now exiting shell"); _exit(EXIT_FAILURE); } //builtin command to display history } else if (strcmp(argList[0], "history") == 0) { printHistory(&histList[0], count); //builtin command to display currently running jobs } else if (strcmp(argList[0], "jobs") == 0) { updateJobs(); printJobs(); //builtin command to bring a background job to the foreground } else if (strcmp(argList[0], "fg") == 0) { if (argList[1] == NULL) printf("Invalid command: must give a Process ID number with the fg command.\n"); if (bringToForeground(argList) == 0) printf("The process with that PID is not currently running.\n"); } else { isBuiltIn = 0; } return isBuiltIn; }
//follower method void CmdLineHandler::sendMsgToLeader() { { HWND hwnd = FindWindowA("PuTTY-ND2_ConfigBox", NULL); if (hwnd) { bringToForeground(hwnd); return; } DWORD wait_result = WaitForSingleObject(sharedMemMutex_, 0); if (WAIT_OBJECT_0 != wait_result) { return; } memset(sharedBuffer_, 0, SHARED_MEM_SIZE); sharedBuffer_[0] = COMMAND_CMD_LINE; strncpy(sharedBuffer_+1, cmdLine_, SHARED_MEM_SIZE-2); ReleaseMutex(sharedMemMutex_); } }
//Run commands that are built-in shell functions static void RunBuiltInCmd(commandT* cmd) { //Send SIGCONT to a backgrounded job, but do not give it the foreground if (strncmp(cmd->argv[0], "bg", 2) == 0) { //If there are two arguments in the command... if (cmd->argc == 2) //Bring the process with the given jobNumber continueBgJob(strtol(cmd->argv[1],NULL,10)); //If there is one argument in the command... else if (cmd->argc == 1) //Bring the most recent background process to the foreground continueBgJob(-1); else { fprintf(stderr, "Too many arguments were given with bg.\n"); } } //Return a backgrounded job to the foreground else if (strncmp(cmd->argv[0], "fg", 2) == 0) { //If there are two arguments in the command... if (cmd->argc == 2) //Bring the process with the given jobNumber bringToForeground(strtol(cmd->argv[1],NULL,10)); //If there is one argument in the command... else if (cmd->argc == 1) //Bring the most recent background process to the foreground bringToForeground(-1); else { fprintf(stderr, "Too many arguments were given with fg.\n"); } } //makenew alias else if (strncmp(cmd->argv[0], "alias", 5) == 0) { //If there are two arguments in the command... if (cmd->argc == 2) //make new binding AddAlias(cmd); //show all bindings else if (cmd->argc == 1) PrintAliases(); } else if (strncmp(cmd->argv[0], "unalias", 7) == 0) { RemoveAlias(cmd->argv[1]); } else if (strncmp(cmd->argv[0], "cd", 2) == 0) { int err; //If a directory is given, go to that directory if (cmd->argc == 2) err = chdir(cmd->argv[1]); //If a directory isn't given, go to the user's home directory else err = chdir(getenv("HOME")); //If there was a problem changing directories, print an error if (err == -1) fprintf(stderr, "%s\n", "Invalid directory\n"); } //Print the list of background jobs (bgJobsHead) else if (strncmp(cmd->argv[0], "jobs", 4) == 0){ PrintBgJobList(); } else { fprintf(stderr, "%s is an unrecognized internal command\n", cmd->argv[0]); fflush(stdout); } }