Beispiel #1
0
void receive_str()
{
	char * history_1 = (char *)malloc(255 * sizeof(char));
	int history_count = 1;
	int history_index = 0;
	int up_count = 0;

	char str_buffer[INPUT_STR_MAX];
	int index = 0;
	print_str("\nInput something or \"help\"\n");
	print_str("mini-shell:$");
	while(1) {
		if(*(USART2_SR) & USART_FLAG_RXNE) {
			str_buffer[index] =( *(USART2_DR) & 0xFF);

			if((int)str_buffer[index]==0xD){//'\r' is 0xD in ascii
				print_char("\n");
				release_Mutex(print_Mutex);
				if(!check_commands(str_buffer,index)){
					print_str("Your input:");
					print_str_specific(str_buffer,index);
				}
				/*
				Record input history
				*/
				switch(history_count){
					case 1:
						strncpy(history_1,str_buffer,index);
						history_index = index;
						break;
				}
				history_count = 0;
				history_count++;

				print_str("\n");
				index = 0;
				print_str("\nmini-shell:$");
			}else if(!strncmp(str_buffer,"A",1)){
				up_count++;
				switch(up_count){
					case 1:
						index = history_index;
						strncpy(str_buffer,history_1,history_index);
						print_str_specific(str_buffer,index);
						break;
				}
				up_count = 0;
			}else{
				if(index == 0 ){
					print_str("\nmini-shell:$");
					while(acquire_Mutex(print_Mutex) != xMutex_success);//busy wait
				}
				print_char(&str_buffer[index]);
				index++;
			}
		}
	}
	free(history_1);
}
Beispiel #2
0
int main(int argc, char *argv[]) 
{
	FILE *input = stdin;
		// Used to keep track of which buffer should be read in (added for scripting purposes)
	char *tokens[100]; 
		// An array storing argvs for the program to be executed
	char commands[80]; // Stores the string entered on the 'command line'
		// Note - command must be kept intact due to the way strtok functions!
	int times_run = 0;

	printf("Welcome to the terminal.\n");
	catch_signals();


	do
	{

		if (times_run >= 1) 
		{
			commands[strlen(commands) - 1] = '\0';
			tokenize(commands, tokens);
			check_commands(tokens);
		}
		pid_t pid = fork();
		if(pid > 0) 
		{ 	// Parent; prompt for new command
			printf(">");
			if (argc > 1 && times_run == 0) 
			{
				FILE *script = fopen(argv[1], "r");
				input = script;
				times_run++;
				continue;
			}
			
			
			int status;
			wait(&status);
			times_run++;
		}

		else if (pid == 0 && times_run >=1) 
		{ 	// This program is child process
			if (execvp(tokens[0], tokens) == -1) 
			{
				error("Unable to run command, please try again.");
			}
		}	

		else //if (pid < 0)
		{				// Error, quit child process
			exit(1);
		}

	} while (fgets(commands, 100, input) != NULL);

	return 0;
}
Beispiel #3
0
void cli_send()
{
	char message[MAXLINE];
	puts("\n--->you are in DEFAULT mode type:\n"
			"--->'monitor'-for MONITORING mode\n"
			"--->'calculate'-for CALCULATING mode\n");

	while(work)
	{
		printf("\n>>");
		scanf("%s" , message);
		if(!check_commands(message))
		{
			printf("--->err: '%s' is not valid command\n\n",message);
			continue;
		}
		double m_res=atof(message);
		if(m_res>0||strcmp(message,"0")==0)
			send_num(sockfd, m_res);
		else
			send_str(sockfd, message);

		if(strcmp(message, "terminate")==0)
			kill(0,SIGINT);
		else if(strcmp(message, "monitor")==0)
		{
			puts("--->you are in monitoring mode\n");
			status=MONITORING;
		}
		else if(strcmp(message, "calculate")==0)
		{
			puts("--->you are in calculating mode\n");
			status=CALCULATING;
		}
		else usleep(5000);

	}
}
Beispiel #4
0
void		read_args(t_env *e, int ac, char **av)
{
	check_commands(e, ac, av);
	check_args(e, ac, av);
}