Пример #1
0
//**************************************************************************************
// function name: main
// Description: main function of smash. get command from user and calls command functions
//**************************************************************************************
int main(int argc, char *argv[])
{
    char cmdString[MAX_LINE_SIZE]; 	   
    LIST_ELEMENT* VarList = NULL;
	
    if(signal(SIGTSTP,signal_handler) == SIG_ERR){
    	printf("SIGTSTP  == SIG_ERR\n");
    }
    if(signal(SIGINT,signal_handler) == SIG_ERR){
    	printf("SIGINT  == SIG_ERR\n");
    }
    if(signal(SIGCHLD,signal_handler) == SIG_ERR){
    	printf("SIGCHLD  == SIG_ERR\n");
    }

	/************************************/
	// Init globals 
	GPid = -1;
	Last_Bg_Pid = -1;
	Susp_Bg_Pid = -1;
	susp = 0;

	
	L_Fg_Cmd =(char*)malloc(sizeof(char)*(MAX_LINE_SIZE+1));
	if (L_Fg_Cmd == NULL) 
			exit (-1); 
	L_Fg_Cmd[0] = '\0';
	
    	while (1)
    	{
	 	printf("smash > ");
		fgets(lineSize, MAX_LINE_SIZE, stdin);
		strcpy(cmdString, lineSize);    	
		cmdString[strlen(lineSize)-1]='\0';
					// replace $variable with it's value:
		if(VarRplc(VarList, lineSize)) continue; 
					// perform a complicated Command
		if(!ExeComp(lineSize, &JobsList)) continue; 
					// background command	
	 	if(!BgCmd(lineSize, &JobsList)) continue; 
					// built in commands
		ExeCmd(&JobsList, &VarList, lineSize, cmdString);
		
		/* initialize for next line read*/
		lineSize[0]='\0';
		cmdString[0]='\0';
	}
    return 0;
}
Пример #2
0
//**************************************************************************************
// function name: main
// Description: main function of smash. get command from user and calls command functions
//**************************************************************************************
int main(int argc, char *argv[])
/*
 * argc stands for "argument count" - argc contains the number of arguments passed to the program
 * argv stands for "argument vector". A vector is a one-dimensional array,
 * and argv is a one-dimensional array of strings
 */
{
    char cmdString[MAX_LINE_SIZE]; 	   
    jobs = new list<job>;

	/* Adjusting new handlers for SIGTSTP, SIGINT & SIGCHLD */
    struct sigaction sa;
    sa.sa_handler = &signal_handle;
    sa.sa_flags = SA_RESTART;
    sigfillset(&sa.sa_mask);

    if (sigaction(SIGTSTP, &sa, NULL) == -1)
        perror("Error: cannot handle SIGTSTP"); 

    if (sigaction(SIGINT, &sa, NULL) == -1)
        perror("Error: cannot handle SIGINT");

    signal (SIGCHLD, &child_handle);
	
    	while (1)
    	{
	 	printf("smash > ");
		fgets(lineSize, MAX_LINE_SIZE, stdin);
		strcpy(cmdString, lineSize);    	
		cmdString[strlen(lineSize)-1]='\0';
		
	 	if(!BgCmd(lineSize)) continue;	// Checking if CMD is bg CMD, executing if it is (can be a complicated bg CMD) //
        if(!ExeComp(lineSize, false)) continue;		// Checking if CMD is complicated+not bg CMD, executing if it is //
		ExeCmd(lineSize, cmdString);	// Executing built-in/external CMD //
		
		lineSize[0]='\0';
		cmdString[0]='\0';
	}
    return 0;
}
Пример #3
0
//**************************************************************************************
// function name: main
// Description: main function of smash. get command from user and calls command functions
//**************************************************************************************
int main(int argc, char *argv[])
{
    char cmdString[MAX_LINE_SIZE]; 	   
    pJob* job_list_head_ptr;
    struct sigaction ctrl_z_act;
    struct sigaction ctrl_c_act;
    //struct sigaction sigchld_act;

	fill_action(&ctrl_z_act, &ctrl_z_handler);
	fill_action(&ctrl_c_act, &ctrl_c_handler);
	//fill_action(&sigchld_act, &sigchld_handler);

    sigaction(SIGTSTP, &ctrl_z_act,NULL);
    sigaction(SIGINT, &ctrl_c_act,NULL);
    //sigaction(SIGCHLD,&sigchld_act,NULL);
    int dead_pid;

	//signal declaretions
	//NOTE: the signal handlers and the function/s that sets the handler should be found in siganls.c
	 /* add your code here */
	
	/************************************/
	//NOTE: the signal handlers and the function/s that sets the handler should be found in siganls.c
	//set your signal handlers here
	/* add your code here */

	/************************************/

	/************************************/
    // Init globals
	GPid = -1;
	Last_Bg_Pid = -1;
	Susp_Bg_Pid = -1;
	susp = 0;
	job_list_head = NULL;
	job_list_head_ptr = &job_list_head;


#if 0
	L_Fg_Cmd =(char*)malloc(sizeof(char)*(MAX_LINE_SIZE+1));
	if (L_Fg_Cmd == NULL) 
			exit (-1); 
	L_Fg_Cmd[0] = '\0';
#endif
    	while (1)
    	{
    	printf("smash > ");

    		while((dead_pid=waitpid(-1,NULL,WNOHANG))>0)// This loop is in charge of handling zombies
    		{
    			remove_job(&job_list_head,dead_pid);
    		}
    		if((dead_pid==-1) && (errno=EINTR))
    		{
    			//refresh_list(job_list_head_ptr);
    		}

		fgets(lineSize, MAX_LINE_SIZE, stdin);
		strcpy(cmdString, lineSize);    	
		cmdString[strlen(lineSize)-1]='\0';
					// perform a complicated Command
		if(!ExeComp(lineSize)) continue; 
					// background command	
	 	if(!BgCmd(lineSize, job_list_head_ptr)) continue;
					// built in commands
		ExeCmd(&job_list_head, lineSize, cmdString);
		/* initialize for next line read*/
		lineSize[0]='\0';
		cmdString[0]='\0';
		fflush(stdout);
		fflush(stdin);
	}
    return 0;
}