3
3
#include "../include/pipeline.h"
4
4
#include "../include/sh_history.h"
5
5
6
+
7
+ /*
8
+ *
9
+ */
10
+ pipe_cmds_t *
11
+ pipeline_commands_new ()
12
+ {
13
+ pipe_cmds_t * pcmds = (pipe_cmds_t * )sh_malloc (sizeof (pipe_cmds_t ));
14
+ pcmds -> cmds_list = (command_t * * )sh_malloc (sizeof (command_t * ));
15
+ pcmds -> capacity = DEFAULT_PIPELINE_COMMANDS_CAP ;
16
+ // for (int i = 0; i < pcmds->capacity; i++) {
17
+ // pcmds->cmds_list[i] = (command_t*)sh_malloc(sizeof(command_t));
18
+ // }
19
+ pcmds -> size = 0 ;
20
+
21
+ return pcmds ;
22
+ }
23
+
24
+ /*
25
+ *
26
+ */
27
+ void
28
+ pipeline_commands_push (pipe_cmds_t * commands , command_t * cmd )
29
+ {
30
+ if (commands ) {
31
+ if (commands -> size >= commands -> capacity )
32
+ pipeline_commands_reallocate (commands , commands -> capacity * PIPELINE_COMMANDS_CAP_EXP_VAL );
33
+
34
+ commands -> cmds_list [commands -> size ++ ] = cmd ;
35
+ }
36
+ }
37
+
38
+ /*
39
+ *
40
+ */
41
+ void
42
+ pipeline_commands_reallocate (pipe_cmds_t * commands , unsigned bytes )
43
+ {
44
+ commands -> cmds_list = realloc ((void * )commands -> cmds_list , bytes );
45
+ commands -> capacity = bytes ;
46
+ }
47
+
48
+ /*
49
+ *
50
+ */
51
+ void
52
+ pipeline_commands_free (pipe_cmds_t * commands )
53
+ {
54
+ if (commands ) {
55
+ for (int i = 0 ; i < commands -> size ; i ++ ) {
56
+ free (commands -> cmds_list [i ]);
57
+ }
58
+ free (commands -> cmds_list );
59
+ free (commands );
60
+ }
61
+ }
62
+
6
63
/*
7
64
*
8
65
*/
9
66
int
10
- command_handle ( command_t * command )
67
+ pipeline_commands_handle ( pipe_cmds_t * commands )
11
68
{
12
- COMMANDS_ALIASES cmd_alias = (COMMANDS_ALIASES )(command -> table_index );
13
- char * * args = command_args_to_string_array (command );
14
- int status ;
69
+ if (commands -> size == 1 ) {
70
+ command_t * whole_command = commands -> cmds_list [0 ];
71
+ COMMANDS_ALIASES cmd_alias = (COMMANDS_ALIASES )(whole_command -> table_index );
72
+ char * * args = command_args_to_string_array (whole_command );
73
+ int status ;
15
74
16
- int ret ;
17
- if (fork () == 0 ) {
18
- switch (cmd_alias ) {
19
- case CMDS_LIST_CMD :
20
- commands_show ();
21
- break ;
22
- case CHANGE_PB_CMD :
23
- if (command -> args_num > 1 )
24
- set_prompt_basename (command -> arguments [1 ]-> name );
25
- else
26
- usage (command );
27
- break ;
28
- case HIST_SHOW_CMD :
29
- shell_history_show (history );
30
- break ;
31
- case HIST_SAVE_CMD :
32
- status = file_save_history (history );
33
- if (status == STATUS_FAILURE )
34
- throw_error ("! Unable to open the file to save history\n" );
35
- break ;
36
- case HIST_LOAD_CMD :
37
- if (command -> args_num == 2 ) {
38
- status = file_load_history (command -> arguments [1 ]-> name );
75
+ int ret ;
76
+ if (fork () == 0 ) {
77
+ switch (cmd_alias ) {
78
+ case CMDS_LIST_CMD :
79
+ commands_show ();
80
+ break ;
81
+ case CHANGE_PB_CMD :
82
+ if (whole_command -> args_num > 1 )
83
+ set_prompt_basename (whole_command -> arguments [1 ]-> name );
84
+ else
85
+ usage (whole_command );
86
+ break ;
87
+ case HIST_SHOW_CMD :
88
+ shell_history_show (history );
89
+ break ;
90
+ case HIST_SAVE_CMD :
91
+ status = file_save_history (history );
39
92
if (status == STATUS_FAILURE )
40
- throw_error ("! Unable to open the file by a given id to load history\n" );
41
- } else
42
- usage (command );
43
- break ;
44
- case HIST_FREE_CMD :
45
- shell_history_free (history );
46
- history = shell_history_new ();
47
- break ;
48
- default :
49
- return execvp (command -> name , args );
93
+ throw_error ("! Unable to open the file to save history\n" );
94
+ break ;
95
+ case HIST_LOAD_CMD :
96
+ if (whole_command -> args_num == 2 ) {
97
+ status = file_load_history (whole_command -> arguments [1 ]-> name );
98
+ if (status == STATUS_FAILURE )
99
+ throw_error ("! Unable to open the file by a given id to load history\n" );
100
+ } else
101
+ usage (whole_command );
102
+ break ;
103
+ case HIST_FREE_CMD :
104
+ shell_history_free (history );
105
+ history = shell_history_new ();
106
+ break ;
107
+ default :
108
+ return execvp (whole_command -> name , args );
109
+ }
50
110
}
111
+ wait (NULL );
112
+ string_array_free (args , whole_command -> args_num );
51
113
}
52
- wait (NULL );
53
- string_array_free (args , command -> args_num );
114
+ else {
115
+ pipeline_pipes_handle (commands );
116
+ }
117
+
118
+ return STATUS_SUCCESS ;
119
+ }
54
120
55
- return STATUS_SUCCESS ;
121
+ /*
122
+ *
123
+ */
124
+ int
125
+ pipeline_pipes_handle (pipe_cmds_t * pipe_commands )
126
+ {
127
+ int pipefd [2 ];
128
+ command_t * comm1 = pipe_commands -> cmds_list [0 ];
129
+ char * * args1 = command_args_to_string_array (comm1 );
130
+ command_t * comm2 = pipe_commands -> cmds_list [1 ];
131
+ char * * args2 = command_args_to_string_array (comm2 );
132
+ pipe (pipefd );
133
+ if (fork () == 0 ) {
134
+ //child
135
+ //write
136
+ close (pipefd [0 ]);
137
+ dup2 (pipefd [1 ], STDOUT_FILENO );
138
+ execvp (comm1 -> name , args1 );
139
+ } else {
140
+ //parent
141
+ close (pipefd [1 ]);
142
+ dup2 (pipefd [0 ], STDIN_FILENO );
143
+ execvp (comm2 -> name , args2 );
144
+ }
145
+ return 0 ;
56
146
}
0 commit comments