-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmx_parser_pipes.c
More file actions
214 lines (188 loc) · 5.47 KB
/
mx_parser_pipes.c
File metadata and controls
214 lines (188 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <mx_cmd_parser.h>
#include <mx_linkedlist.h>
#include <mx_debug.h>
#include <string.h>
#include <errno.h>
#define PIPE_READ_FD STDIN_FILENO
#define PIPE_WRITE_FD STDOUT_FILENO
/*
* Global array of pipes.
*/
int *g_pipefds = NULL;
/*
* The total number of commands.
*/
int g_total_cmds = 0;
extern linked_list_t cmd_precedence_array[MAX_PRECEDENCE_LEVEL];
extern void print_cmd_precedence_array();
/*
* Close and free the allocated pipes.
*/
void parser_close_clean_pipes ()
{
int index = 0;
if (!g_pipefds) {
return;
}
while (index < (g_total_cmds-1)) {
if (*(g_pipefds+index*2+PIPE_READ_FD) != -1) {
close(*(g_pipefds+index*2+PIPE_READ_FD));
}
if (*(g_pipefds+index*2+PIPE_WRITE_FD) != -1) {
close(*(g_pipefds+index*2+PIPE_WRITE_FD));
}
index++;
}
free(g_pipefds);
g_pipefds = NULL;
g_total_cmds = 0;
}
/*
* Create the required number of pipes.
* and connect the parser command tokens.
*/
bool parser_pipe_init ()
{
int index = 0;
int precedence_index = 0;
list_node_t *node = NULL;
list_node_t *next_node = NULL;
cmd_token_t *curr_token = NULL, *next_token = NULL;
linked_list_t *list = NULL;
g_total_cmds = parser_get_total_commands();
if (!g_total_cmds) {
log("No commands enetered");
return true;
}
if (!g_total_cmds == 1) {
// Just one commands entered.
log("Only one command entered");
return true;
}
g_pipefds = (int *)malloc((g_total_cmds - 1) * 2 * sizeof(int));
if (!g_pipefds) {
log("Failed to allocate memory for pipe");
return false;
}
memset(g_pipefds, -1, (g_total_cmds - 1) * 2 * sizeof(int));
// printf("\nRFD %d, WFD %d",g_pipefds[0][PIPE_READ_FD],
// g_pipefds[0][PIPE_WRITE_FD]);
while (index < (g_total_cmds - 1)) {
if (pipe(g_pipefds+index*2) < 0) {
log("Failed to initialize pipes");
parser_close_clean_pipes();
return false;
}
index++;
}
precedence_index = 0;
index = 0;
node = NULL;
while (node == NULL &&
precedence_index < MAX_PRECEDENCE_LEVEL) {
list = &cmd_precedence_array[precedence_index];
node = list->head;
precedence_index++;
}
while (node) {
next_node = node->next;
while (next_node == NULL &&
precedence_index < MAX_PRECEDENCE_LEVEL) {
list = &cmd_precedence_array[precedence_index];
next_node = list->head;
precedence_index++;
}
if (next_node) {
if (index >= (g_total_cmds - 1)) {
log("Mismatch in the total commands and the allocated FDs");
parser_close_clean_pipes();
return false;
}
curr_token = (cmd_token_t *)node->data;
next_token = next_node->data;
curr_token->pipefds[PIPE_WRITE_FD] =
*(g_pipefds+index*2+PIPE_WRITE_FD);
next_token->pipefds[PIPE_READ_FD] =
*(g_pipefds+index*2+PIPE_READ_FD);
index++;
}
node = next_node;
}
#ifdef DEBUG
print_cmd_precedence_array();
#endif
return true;
}
/*
* Execute the commands in parallel.
*/
bool parser_pipe_execute_cmds ()
{
list_node_t *node = NULL, *next_node = NULL;
cmd_token_t *curr_token = NULL;
linked_list_t *list = NULL;
int precedence_index = 0;
int status;
int pid = 0;
while (node == NULL &&
precedence_index < MAX_PRECEDENCE_LEVEL) {
list = &cmd_precedence_array[precedence_index];
node = list->head;
precedence_index++;
}
while (node) {
curr_token = (cmd_token_t *) node->data;
pid = fork();
if (pid == 0) {
if (curr_token->pipefds[PIPE_READ_FD] >= 0) {
close(STDIN_FILENO);
dup(curr_token->pipefds[PIPE_READ_FD]);
}
if (curr_token->pipefds[PIPE_WRITE_FD] >= 0) {
close(STDOUT_FILENO);
dup(curr_token->pipefds[PIPE_WRITE_FD]);
}
parser_close_clean_pipes();
if (execvp(curr_token->args[0], curr_token->args) < 0) {
printf("\nFailed to execute command \'%s\'\n", curr_token->args[0]);
exit(1);
}
} else {
next_node = node->next;
curr_token->pid = pid;
while (next_node == NULL &&
precedence_index < MAX_PRECEDENCE_LEVEL) {
list = &cmd_precedence_array[precedence_index];
next_node = list->head;
precedence_index++;
}
node = next_node;
}
}
precedence_index = 0;
node = NULL;
while (node == NULL &&
precedence_index < MAX_PRECEDENCE_LEVEL) {
list = &cmd_precedence_array[precedence_index];
node = list->head;
precedence_index++;
}
while (node) {
next_node = node->next;
curr_token = node->data;
// wait(curr_token->pid, &status, 0);
while (next_node == NULL &&
precedence_index < MAX_PRECEDENCE_LEVEL) {
list = &cmd_precedence_array[precedence_index];
next_node = list->head;
precedence_index++;
}
node = next_node;
}
return true;
}