mirror of
				https://gitlab.com/klmp200/fish.git
				synced 2025-10-31 09:13:05 +00:00 
			
		
		
		
	Grep infinis :D
This commit is contained in:
		| @@ -19,10 +19,15 @@ pipe_redirection * getRedirection(){ | |||||||
| 	if (redirection == NULL){ | 	if (redirection == NULL){ | ||||||
| 		redirection = (pipe_redirection*) malloc(sizeof(pipe_redirection)); | 		redirection = (pipe_redirection*) malloc(sizeof(pipe_redirection)); | ||||||
| 		if (redirection == NULL) crash(); | 		if (redirection == NULL) crash(); | ||||||
| 		redirection->file_name = strdup((char*) "/tmp/fishXXXXXX"); | 		redirection->files_name[0] = strdup((char*) "/tmp/fishXXXXXX"); | ||||||
| 		redirection->tmp_file = mkstemp(redirection->file_name); | 		redirection->files_name[1] = strdup((char*) "/tmp/fishXXXXXX"); | ||||||
|  | 		redirection->tmp_files[0] = mkstemp(redirection->files_name[0]); | ||||||
|  | 		redirection->tmp_files[1] = mkstemp(redirection->files_name[1]); | ||||||
| 		redirection->to_use = 0; | 		redirection->to_use = 0; | ||||||
| 		redirection->read = 0; | 		redirection->read = WRITE; | ||||||
|  | 		redirection->nb = 0; | ||||||
|  | 		redirection->nb_max = 0; | ||||||
|  | 		redirection->file_use = 0; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return redirection; | 	return redirection; | ||||||
| @@ -31,10 +36,14 @@ pipe_redirection * getRedirection(){ | |||||||
|  |  | ||||||
| void freeRedirection(){ | void freeRedirection(){ | ||||||
| 	pipe_redirection * redirection = getRedirection(); | 	pipe_redirection * redirection = getRedirection(); | ||||||
| 	close(redirection->tmp_file); | 	close(redirection->tmp_files[0]); | ||||||
| 	unlink(redirection->file_name); | 	close(redirection->tmp_files[1]); | ||||||
| 	remove(redirection->file_name); | 	unlink(redirection->files_name[0]); | ||||||
| 	free(redirection->file_name); | 	unlink(redirection->files_name[1]); | ||||||
|  | 	remove(redirection->files_name[0]); | ||||||
|  | 	remove(redirection->files_name[1]); | ||||||
|  | 	free(redirection->files_name[0]); | ||||||
|  | 	free(redirection->files_name[1]); | ||||||
| 	free(redirection); | 	free(redirection); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -50,7 +59,8 @@ void fishLoop(Settings * settings){ | |||||||
|         line = fishReadLine(); |         line = fishReadLine(); | ||||||
|  |  | ||||||
| 		r->to_use = 0; | 		r->to_use = 0; | ||||||
| 		r->read = 0; | 		r->nb = countSeparators(line, "\\|[^\\|]"); | ||||||
|  | 		r->nb_max = r->nb; | ||||||
| 		splited = split(line, (char*) FISH_TOKENS); | 		splited = split(line, (char*) FISH_TOKENS); | ||||||
| 		splited = fishExpand(splited); | 		splited = fishExpand(splited); | ||||||
|  |  | ||||||
| @@ -148,10 +158,13 @@ int fishLoad(WordArray *array) { | |||||||
| 	pid = fork(); | 	pid = fork(); | ||||||
| 	if (pid == 0){ | 	if (pid == 0){ | ||||||
| 		if (redirection->to_use){ | 		if (redirection->to_use){ | ||||||
| 			if (redirection->read){ | 			if (redirection->read == READ){ | ||||||
| 				dup2(redirection->tmp_file, STDIN_FILENO); | 				dup2(redirection->tmp_files[redirection->file_use], STDIN_FILENO); | ||||||
|  | 			} else if (redirection->read == WRITE) { | ||||||
|  | 				dup2(redirection->tmp_files[redirection->file_use], STDOUT_FILENO); | ||||||
| 			} else { | 			} else { | ||||||
| 				dup2(redirection->tmp_file, STDOUT_FILENO); | 				dup2(redirection->tmp_files[!redirection->file_use], STDIN_FILENO); | ||||||
|  | 				dup2(redirection->tmp_files[redirection->file_use], STDOUT_FILENO); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		/* Executes only in the child process */ | 		/* Executes only in the child process */ | ||||||
| @@ -208,14 +221,38 @@ int fishExecute(WordList *list) { | |||||||
| 			break; | 			break; | ||||||
| 		case PIPE: | 		case PIPE: | ||||||
| 			redirection = getRedirection(); | 			redirection = getRedirection(); | ||||||
|  |  | ||||||
|  | 			if (redirection->nb == redirection->nb_max){ | ||||||
|  | 				redirection->read = WRITE; | ||||||
|  | 				redirection->file_use = 0; | ||||||
|  | 			} else if (redirection->nb > 0){ | ||||||
|  | 				redirection->read = READ_AND_WRITE; | ||||||
|  | 				redirection->file_use = !redirection->file_use; | ||||||
|  | 			} else { | ||||||
|  | 				redirection->read = READ; | ||||||
|  | 				redirection->file_use = !redirection->file_use; | ||||||
|  | 			} | ||||||
| 			redirection->to_use = 1; | 			redirection->to_use = 1; | ||||||
| 			redirection->read = 0; | 			--redirection->nb; | ||||||
|  |  | ||||||
|  | 			if (redirection->read == WRITE) ftruncate(redirection->tmp_files[redirection->file_use], 0); | ||||||
|  | 			lseek(redirection->tmp_files[0], 0L, 0); | ||||||
|  | 			lseek(redirection->tmp_files[1], 0L, 0); | ||||||
|  |  | ||||||
| 			fishExecute(list); | 			fishExecute(list); | ||||||
| 			lseek(redirection->tmp_file, 0L, 0); |  | ||||||
| 			redirection->read = 1; | 			redirection->read = WRITE; | ||||||
|  | 			if (redirection->nb > 0){ | ||||||
|  | 				redirection->read = READ_AND_WRITE; | ||||||
|  | 			} else { | ||||||
|  | 				redirection->read = READ; | ||||||
|  | 			} | ||||||
|  | 			if (redirection->read == WRITE) ftruncate(redirection->tmp_files[redirection->file_use], 0); | ||||||
|  | 			lseek(redirection->tmp_files[0], 0L, 0); | ||||||
|  | 			lseek(redirection->tmp_files[1], 0L, 0); | ||||||
|  |  | ||||||
| 			signal = fishExecute(splited); | 			signal = fishExecute(splited); | ||||||
| 			lseek(redirection->tmp_file, 0L, 0); |  | ||||||
| 			ftruncate(redirection->tmp_file, 0); |  | ||||||
| 			redirection->to_use = 0; | 			redirection->to_use = 0; | ||||||
| 			break; | 			break; | ||||||
| 		case BACKGROUND_PROCESS: | 		case BACKGROUND_PROCESS: | ||||||
|   | |||||||
| @@ -23,6 +23,12 @@ typedef enum { | |||||||
| 	REVERSE_AND | 	REVERSE_AND | ||||||
| } shell_operator ; | } shell_operator ; | ||||||
|  |  | ||||||
|  | typedef enum { | ||||||
|  | 	READ, | ||||||
|  | 	WRITE, | ||||||
|  | 	READ_AND_WRITE | ||||||
|  | } redirection_mode ; | ||||||
|  |  | ||||||
| typedef struct { | typedef struct { | ||||||
| 	char ** words; | 	char ** words; | ||||||
| 	int size; | 	int size; | ||||||
| @@ -49,9 +55,12 @@ typedef struct { | |||||||
|  |  | ||||||
| typedef struct { | typedef struct { | ||||||
| 	int to_use; | 	int to_use; | ||||||
| 	int read; | 	int nb; | ||||||
| 	int tmp_file; | 	int nb_max; | ||||||
| 	char * file_name; | 	int file_use; | ||||||
|  | 	int tmp_files[2]; | ||||||
|  | 	char * files_name[2]; | ||||||
|  | 	redirection_mode read; | ||||||
| } pipe_redirection; | } pipe_redirection; | ||||||
|  |  | ||||||
| typedef int (builtinCommand) (WordArray*); | typedef int (builtinCommand) (WordArray*); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user