-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_2.c
192 lines (182 loc) · 3.73 KB
/
helpers_2.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
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
/*
* File: helpers_2.c
* Auth: Wandile DLamini
* Lwazi
*/
#include "shell.h"
void handle_line(char **line, ssize_t read);
ssize_t get_new_len(char *line);
void logical_ops(char *line, ssize_t *new_len);
/**
* handle_line - Partitions a line read from standard input as needed.
* @line: A pointer to a line read from standard input.
* @read: The length of line.
*
* Description: Spaces are inserted to separate ";", "||", and "&&".
* Replaces "#" with '\0'.
*/
void handle_line(char **line, ssize_t read)
{
char *old_line, *new_line;
char previous, current, next;
size_t i, j;
ssize_t new_len;
new_len = get_new_len(*line);
if (new_len == read - 1)
return;
new_line = malloc(new_len + 1);
if (!new_line)
return;
j = 0;
old_line = *line;
for (i = 0; old_line[i]; i++)
{
current = old_line[i];
next = old_line[i + 1];
if (i != 0)
{
previous = old_line[i - 1];
if (current == ';')
{
if (next == ';' && previous != ' ' && previous != ';')
{
new_line[j++] = ' ';
new_line[j++] = ';';
continue;
}
else if (previous == ';' && next != ' ')
{
new_line[j++] = ';';
new_line[j++] = ' ';
continue;
}
if (previous != ' ')
new_line[j++] = ' ';
new_line[j++] = ';';
if (next != ' ')
new_line[j++] = ' ';
continue;
}
else if (current == '&')
{
if (next == '&' && previous != ' ')
new_line[j++] = ' ';
else if (previous == '&' && next != ' ')
{
new_line[j++] = '&';
new_line[j++] = ' ';
continue;
}
}
else if (current == '|')
{
if (next == '|' && previous != ' ')
new_line[j++] = ' ';
else if (previous == '|' && next != ' ')
{
new_line[j++] = '|';
new_line[j++] = ' ';
continue;
}
}
}
else if (current == ';')
{
if (i != 0 && old_line[i - 1] != ' ')
new_line[j++] = ' ';
new_line[j++] = ';';
if (next != ' ' && next != ';')
new_line[j++] = ' ';
continue;
}
new_line[j++] = old_line[i];
}
new_line[j] = '\0';
free(*line);
*line = new_line;
}
/**
* get_new_len - Gets the new length of a line partitioned
* by ";", "||", "&&&", or "#".
* @line: The line to check.
*
* Return: The new length of the line.
*
* Description: Cuts short lines containing '#' comments with '\0'.
*/
ssize_t get_new_len(char *line)
{
size_t i;
ssize_t new_len = 0;
char current, next;
for (i = 0; line[i]; i++)
{
current = line[i];
next = line[i + 1];
if (current == '#')
{
if (i == 0 || line[i - 1] == ' ')
{
line[i] = '\0';
break;
}
}
else if (i != 0)
{
if (current == ';')
{
if (next == ';' && line[i - 1] != ' ' && line[i - 1] != ';')
{
new_len += 2;
continue;
}
else if (line[i - 1] == ';' && next != ' ')
{
new_len += 2;
continue;
}
if (line[i - 1] != ' ')
new_len++;
if (next != ' ')
new_len++;
}
else
logical_ops(&line[i], &new_len);
}
else if (current == ';')
{
if (i != 0 && line[i - 1] != ' ')
new_len++;
if (next != ' ' && next != ';')
new_len++;
}
new_len++;
}
return (new_len);
}
/**
* logical_ops - Checks a line for logical operators "||" or "&&".
* @line: A pointer to the character to check in the line.
* @new_len: Pointer to new_len in get_new_len function.
*/
void logical_ops(char *line, ssize_t *new_len)
{
char previous, current, next;
previous = *(line - 1);
current = *line;
next = *(line + 1);
if (current == '&')
{
if (next == '&' && previous != ' ')
(*new_len)++;
else if (previous == '&' && next != ' ')
(*new_len)++;
}
else if (current == '|')
{
if (next == '|' && previous != ' ')
(*new_len)++;
else if (previous == '|' && next != ' ')
(*new_len)++;
}
}