-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_input.c
134 lines (131 loc) · 3.04 KB
/
format_input.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
#include "main.h"
/**
* handle_comments - removes comments from the vector of strings
* @vector: an array of strings
* Description: the function iterates through the array of strings and removes
* any string starting with a '#' character
* Return:modified vector with comments removed, Null if a comment
* is found at the beginning.
*/
char **handle_comments(char **vector)
{
int i, j;
for (i = 0; vector[i]; i++)
{
if (vector[i][0] == '#')
{
if (i == 0)
return (NULL);
j = i;
while (vector[j])
{
free(vector[j]);
j++;
}
vector[i] = NULL;
return (vector);
}
}
return (vector);
}
int format_spaces(char **vector, char **path, envlist_t **envlist,
char *program, int co, char **mode)
{
int i, j, k;
char **vec;
char **comments;
for (i = 0; vector[i]; i++)
{
vec = strtow(vector[i], ' ');
if (!vec)
continue;
comments = handle_comments(vec);
if (!comments)
{
free_vec(vec);
continue;
}
k = execute_built_in(envlist, vec);
if (!_strcmp(vec[0], "exit"))
{
if (k == -1)
{
printerr("%s: %d: %s: Illegal number: %s\n", program, co, vec[0], vec[1]);
free_vec(vec);
if (!vector[i + 1])
return (2);
continue;
}
free_vec(vector);
free_list(*envlist);
free_vec(path);
if (mode)
free(mode);
if (i && !vec[1])
{
free_vec(vec);
exit(j);
}
free_vec(vec);
exit(k);
}
if (!k)
{
free_vec(vec);
continue;
}
if (!_strcmp(vec[0], "cd"))
{
printerr("%s: %d: %s: can't cd to %s\n", program, co, vec[0], vec[1]);
free_vec(vec);
continue;
}
j = fork_cmd(vec, path);
if (!j)
{
printerr("%s: %d: %s: not found\n", program, co, vec[0]);
free_vec(vec);
}
if (j == 2 && !vector[i + 1])
return (2);
}
return (1);
}
/**
* execute - executes a string containing commands
* @string: string containing commands
* @num:number of commands in the strings
* @path: array of strings rep the search path
* @envlist: pointet to env list
* @program: name of program
* @co:current command line number
* @mode: array of strings rep the execution mode
* Return: 2 on success.
*/
int execute(char *string, int num, char **path, envlist_t **envlist,
char *program, int co, char **mode)
{
char **vector;
int i;
if (num == 1)
{
free(string);
return (num);
}
if (!num)
{
free(string);
free_vec(path);
free_list(*envlist);
return (-1);
}
vector = strtow(string, ';');
free(string);
if (!vector || !vector[0])
return (0);
i = format_spaces(vector, path, envlist, program, co, mode);
free_vec(vector);
if (i == 2)
return (2);
return (3);
}