forked from glyif/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.c
135 lines (121 loc) · 3.29 KB
/
pipeline.c
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
#include "header.h"
/**
* init_pipeline_count_processes - count amount of token_string in a ptree
* @tree: pointer to ptree
*
* Return: number of processes
*/
unsigned int init_pipeline_count_processes(ptree_t *tree)
{
if (!tree)
return (0);
if (is_redirection(tree->token_id))
return (1);
if (tree->token_id == TOKEN_STRING)
return (1);
return (init_pipeline_count_processes(tree->left) +
init_pipeline_count_processes(tree->right));
}
/**
* init_pipeline_push_processes - fetches everything & puts to pipeline struct
* @pipeline: pointer to pipeline
* @tree: ptree to fetch from
*
* Return: 0
*/
int init_pipeline_push_processes(pipeline_t *pipeline, ptree_t *tree)
{
char *filename;
if (!tree)
return (0);
if (is_redirection(tree->token_id))
{
pipeline->processes[pipeline->processesN].ptree = tree;
pipeline->processes[pipeline->processesN].io_redir = tree->token_id;
filename = safe_malloc(strlen(tree->right->strings[0]) + 1);
_strcpy(filename, (char *)tree->right->strings[0]);
pipeline->processes[pipeline->processesN].filename = filename;
pipeline->processesN++;
tree->token_id = TOKEN_STRING;
tree->strings = tree->left->strings;
tree->stringsN = tree->left->stringsN;
tree->left->strings = NULL;
tree->left->stringsN = 0;
delete_ptree(tree->left);
delete_ptree(tree->right);
tree->left = NULL, tree->right = NULL;
return (0);
}
if (tree->token_id == TOKEN_STRING)
{
pipeline->processes[pipeline->processesN].ptree = tree;
pipeline->processes[pipeline->processesN].io_redir = 0;
pipeline->processes[pipeline->processesN].filename = NULL;
pipeline->processesN++;
init_pipeline_push_processes(pipeline, tree->left);
init_pipeline_push_processes(pipeline, tree->right);
return (0);
}
if (tree->token_id == TOKEN_PIPE)
{
init_pipeline_push_processes(pipeline, tree->left);
init_pipeline_push_processes(pipeline, tree->right);
return (0);
}
return (0);
}
/**
* init_pipeline - initializes pipeline struct
* @pipeline: pointer to pipeline struct
* @ptree: ptree to put into pipeline
*
* Return: 0
*/
int init_pipeline(pipeline_t *pipeline, ptree_t *ptree)
{
unsigned int i, mem_needed;
/* At first, we need to understand how much memory we'll need */
mem_needed = init_pipeline_count_processes(ptree);
if (mem_needed == 0)
{
_perror("No elements present in the pipeline!\n");
exit(1);
}
/* allocate memory */
pipeline->processes = safe_malloc(mem_needed * sizeof(process_t));
if (!pipeline->processes)
{
_perror("Memory allocation failed!\n");
exit(1);
}
/* initializes background service values */
pipeline->background = 0;
pipeline->background_pid = 0;
/* initalizes actual process */
pipeline->processesN = 0;
init_pipeline_push_processes(pipeline, ptree);
if (pipeline->processesN != mem_needed)
{
_perror("uhhhhh number of commands does not match\n");
exit(1);
}
for (i = 0; i < pipeline->processesN; i++)
pipeline->processes[i].pid = 0;
return (0);
}
/**
* delete_pipeline - deletes pipeline
* @pipeline: pointer to pipeline
*
* Return: 0
*/
int delete_pipeline(pipeline_t *pipeline)
{
unsigned int i;
for (i = 0; i < pipeline->processesN; i++)
if (pipeline->processes[i].filename != NULL)
free(pipeline->processes[i].filename);
free(pipeline->processes);
pipeline->processes = NULL;
return (0);
}