Skip to content

Commit 65239d2

Browse files
committed
add pipes support through commands array passing to pipeline handlers
1 parent 292efb7 commit 65239d2

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

main.c

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@
44
#include "include/parser.h"
55
#include "include/sh_history.h"
66
#include "include/utils.h"
7-
7+
#include "include/pipeline.h"
88

99

1010

1111

1212
/*
13-
SAVING SESSION HISTORY TO THE FILE
14-
LOADING HISTORY FROM THE FILE BY A GIVEN ID
13+
PIPES HANDLING
14+
UNKNOWN COMMANDS HANDLING WITH EXECVP
15+
WRITE CURSOR MOVEMENT MACROSES
16+
STREAM REDIRECTION HANDLING
17+
FIX EXIT, I THINK THE REASON IS OTHER PROCESSES CREATED IN HANDLERS
1518
*/
1619

1720

1821

19-
20-
21-
22-
23-
2422
int main(int argc, char** argv)
2523
{
2624
set_session_id();
@@ -32,12 +30,12 @@ int main(int argc, char** argv)
3230

3331
input_t* input = NULL;
3432
command_t* cmd = NULL;
35-
his_entry_t* entry_to_retrieve = NULL;
33+
pipe_cmds_t* pipeline_commands = NULL;
3634

3735
int free_prompt_base = 0, free_hist_filename = 0;
3836
int cursor_pos_in_command = 0, non_canon_curr;
3937
unsigned previous_watched_command_len;
40-
char* esc_seq = (char*)sh_malloc(ESC_SEQUENCE_MAX_LEN);
38+
char* esc_seq = (char*)sh_malloc(ESC_SEQUENCE_MAX_LEN), *ptr;
4139

4240
while (TRUE)
4341
{
@@ -156,23 +154,39 @@ int main(int argc, char** argv)
156154
if (!input->size) { printf("\n"); continue; }
157155

158156
putchar(non_canon_curr);
157+
input_buffer_push(input, '\0');
159158

160159
// check if the prompt basename has been changed. If so, we should free memory that
161160
// has been allocated for a new basename to avoid memory leaks
162161
if (!strcmp(prompt_basename, DEFAULT_PROMPT_BASENAME)) free_prompt_base = 1;
163162
if (!strcmp(hist_file_name, DEFAULT_HIST_FILENAME)) free_hist_filename = 1;
164-
if (!strcmp(input->buffer, "exit")) { input_free(input); break; }
165-
166-
input_buffer_push(input, '\0');
167-
cmd = command_parse_new(input->buffer);
168-
if (cmd) {
169-
shell_history_prepend(history, cmd);
170-
int status = command_handle(cmd);
163+
if (!strcmp(input->buffer, "exit")) { break; }
164+
165+
cmd = command_parse_new(input->buffer, FALSE);
166+
shell_history_prepend(history, cmd);
167+
168+
// if the whole command is valid then it is splitted by pipes
169+
// and joined in a chain of subcommands
170+
if (cmd->is_valid) {
171+
char* buffer = strdup(input->buffer);
172+
pipeline_commands = pipeline_commands_new();
173+
do {
174+
cmd = command_parse_new(buffer, TRUE);
175+
176+
ptr = strchr(buffer, VERTICAL_BAR_SYMBOL);
177+
if (ptr) buffer = ptr + 1;
178+
else buffer += strlen(buffer);
179+
180+
if (cmd->is_valid) {
181+
pipeline_commands_push(pipeline_commands, cmd);
182+
}
183+
} while (*buffer);
184+
int status = pipeline_commands_handle(pipeline_commands);
185+
pipeline_commands_free(pipeline_commands);
171186
}
172-
173187
input_free(input);
174188
}
175-
command_free(cmd);
189+
176190
shell_history_free(history);
177191

178192
if (free_prompt_base) free(prompt_basename);

0 commit comments

Comments
 (0)