-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_line.c
145 lines (138 loc) · 2.77 KB
/
split_line.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
136
137
138
139
140
141
142
143
144
145
#include "shell.h"
/**
* splitLine - Splits a line into tokens
*
* @line: commands on commandline
* @status: The last command's exit status
*
* Return: An array of pointers to tokens
*/
char **splitLine(char *line, int status)
{
char *lineCopy, *token, **tokens, *modifiedToken = NULL;
int i;
lineCopy = myCustomStrdup(line);
token = strtok(line, DELIM);
if (myCustomStrcmp(token, "exit") == 0) /*Implement exit*/
return (implementExit(token, lineCopy));
i = 0;
while (token != NULL) /*Count the tokens*/
{
i++;
token = strtok(NULL, DELIM);
}
tokens = malloc((i + 1) * sizeof(char *));
if (tokens == NULL)
{
free(lineCopy);
return (NULL);
}
i = 0;
token = strtok(lineCopy, DELIM); /*Add tokens to the tokens array*/
while (token != NULL)
{
if (token[0] == '$')
{
modifiedToken = handleSubstitution(token, status);
tokens[i] = myCustomStrdup(modifiedToken);
}
else
tokens[i] = myCustomStrdup(token);
i++;
token = strtok(NULL, DELIM);
}
tokens[i] = NULL;
if (lineCopy != NULL)
free(lineCopy);
if (modifiedToken != NULL)
free(modifiedToken);
return (tokens);
}
/**
* splitLogicalLine - Splits a line with logical operators
*
* @line: A pointer to the line
*
* Return: 0 on success, nonzero on failure
*/
int splitLogicalLine(char *line)
{
char token[1024], **tokens;
int ret, i = 0, j = 0;
while (line[i] != '\0')
{
if ((line[i] == '&' && line[i + 1] == '&')
|| (line[i] == '|' && line[i + 1] == '|'))
{
token[j] = '\0';
tokens = splitLine(token, 0);
ret = execute_command(tokens, token);
freeTokens(tokens);
j = 0;
if (ret == 0 && line[i] == '|' && line[i + 1] == '|')
{
break;
}
if (ret != 0 && line[i] == '&' && line[i + 1] == '&')
{
break;
}
i += 2;
}
token[j] = line[i];
i++, j++;
if (line[i] == '\0')
{
token[j] = '\0';
tokens = splitLine(token, 0);
ret = execute_command(tokens, token);
freeTokens(tokens);
}
}
return (ret);
}
/**
* splitSeparator - splits line with semi colon
*
* @line: pointer to the line
*
* Return: 0 on success, -1 on failure
*/
int splitSeparator(char *line)
{
char token[1024], **tokens;
int ret = 0, i = 0, j = 0;
while (line[i] != '\0')
{
if (line[i] == ';' /*&& line[i + 1] == '\0'*/)
{
token[j] = '\0';
if (token[0] != '\0')
{
tokens = splitLine(token, 1);
if (tokens != NULL)
{
ret = execute_command(tokens, token);
freeTokens(tokens); } }
if (line[i + 1] == '\0' || line[i + 2] == '\0')
{
break;
}
j = 0;
i++;
}
token[j] = line[i];
i++, j++;
if (line[i] == '\0')
{
token[j] = '\0';
if (token[0] != '\0')
{
tokens = splitLine(token, 0);
if (tokens != NULL)
{
ret = execute_command(tokens, token);
freeTokens(tokens); } } }
}
return (ret);
}