Пример #1
0
//**************************************************************************
// function to parse/manage/execute commands
//**************************************************************************
void manager(command_t* cmd ){
	 char* temp [MAX_COMMAND_LENGTH];
	 strcpy(temp, cmd->cmdstr);
	 char* tokens = strtok(temp, " ");

	 //cd if the first two chars are cd
	 if (!strcmp(tokens, "cd")){
	 	run_cd( tokens );
	 }
	 //set
	 else if (!strcmp(tokens, "set")){
	 	run_set( tokens);
	 }
	 //echo
	 else if (!strcmp(tokens, "echo")){
	 	run_echo( tokens );
	 }
	 //pwd
	 else if (!strcmp(tokens, "pwd")){
	 	run_pwd( tokens );
	 }
	 //jobs
	 else if (!strcmp(tokens, "jobs")){
	 	run_jobs( cmd, tokens );
	 }
	 //exit command
	 else if (!strcmp(tokens, "exit") || !strcmp(tokens, "quit") )
      terminate(); // Exit Quash
  //run an executable
  else{
    	run_exec(cmd, tokens);
    }
}
Пример #2
0
void cmd_handler(int cmd, char *arg)
{
    if (cmd < 6) /* handles first lot of commands as they all can be implemented by forking a child process easily */
    {
        pid_t pid = fork();

        if (pid > CHILD_PID)        /* parent process wait for child to finish before exiting function and going back to prompt */
        {
            wait(&pid);
        } else if (pid == CHILD_PID)  /* forked child process runs entered command */
        {
            switch (cmd)
            {
                case 0:
                    run_clr();
                    break;
                case 1:
                    run_dir(arg);
                    break;
                case 2:
                    run_echo(arg);
                    break;
                case 3:
                    run_environ();
                    break;
                case 4:
                    run_help();
                    break;
                case 5:
                    run_pause();
                    break;
                default:
                    printf("Congratulations! You've broken my code.");  /* I really don't think it's possible to get here... */
            }
        } else
        {
            fprintf(stderr, "Failed to fork process. Fatal error, exiting program.\n");
            exit(1);
        }
    } else    /* handles last commands: because of the way I've implemented the execution of the shell, it would be very inconvenient to handle them in a child process (without using shared memory) */
    {
        switch (cmd)
        {
            case 6:
                run_quit();
                break;
            case 7:
                run_cd(arg);
                break;
            default:
                printf("Again! You did it again!");         /* again, not really possible */
        }
    }
}