-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenise.c
147 lines (134 loc) · 3.13 KB
/
tokenise.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
#include "headers.h"
void removeWhitespaces(char * str, char * ret)
{
char * tok = strtok(str, " \t");
int i = 0;
while(tok != NULL)
{
int ind = 0;
while(tok[ind] != '\0')
{
ret[i++] = tok[ind++];
}
ret[i++] = ' ';
tok = strtok(NULL, " \t");
}
ret[--i] = '\0';
return;
}
void tokenise(char * command, char * foreground[], char * background[])
{
int fore = 0;
int back = 0;
char* tok1;
char delims[N];
char * tokens[N];
int ind = 0;
char * dup = strdup(command);
tok1 = strtok(dup, "&;");
while(tok1 != NULL)
{
char delim = command[tok1 - dup + strlen(tok1)];
delims[ind] = delim;
tokens[ind++] = tok1;
tok1 = strtok(NULL, "&;");
}
delims[ind] = '\0';
int start = 0;
while(start < ind)
{
if(delims[start] == ';' || delims[start] == '\0')
{
foreground[fore++] = strdup(tokens[start]);
}
else if(delims[start] == '&')
{
background[back++] = strdup(tokens[start]);
}
start++;
}
foreground[fore] = NULL;
background[back] = NULL;
for(int i = 0; i < fore; i++)
{
char ret[strlen(foreground[i]) + 1];
removeWhitespaces(foreground[i], ret);
free(foreground[i]);
foreground[i] = strdup(ret);
}
for(int i = 0; i < back; i++)
{
char ret[strlen(background[i]) + 1];
removeWhitespaces(background[i], ret);
free(background[i]);
background[i] = strdup(ret);
}
free(dup);
return;
}
int tokenise_for_piping(char * command, char * ordered[])
{
char * tok;
char * dup = strdup(command);
if(command[strlen(command) - 1] == '|')
{
fprintf(stderr, "ERROR: pipe with no outlet.\n");
return 2;
}
tok = strtok(dup, "|");
int ind = 0;
while(tok != NULL)
{
ordered[ind++] = strdup(tok);
// printf("%s\n", ordered[ind-1]);
tok = strtok(NULL, "|");
}
// printf("%d\n", ind);
for(int i = 0; i < ind; i++)
{
char ret[strlen(ordered[i]) + 1];
removeWhitespaces(ordered[i], ret);
ordered[i] = strdup(ret);
}
free(dup);
if(ind == 1)
return 0;
else
return 1;
}
int tokenise_for_redirection(char * command, char * ordered[], char delimiters[])
{
char * tok;
char delim;
int ind = 0;
char * dup = strdup(command);
tok = strtok(dup, "><");
while(tok != NULL)
{
delim = command[tok - dup + strlen(tok)];
if (delim == '>')
{
if (command[tok - dup + strlen(tok) + 1] == '>')
{
delim = '!';
}
}
delimiters[ind] = delim;
ordered[ind++] = strdup(tok);
tok = strtok(NULL, "><");
}
delimiters[ind] = '\0';
for(int i = 0; i < ind; i++)
{
char ret[strlen(ordered[i]) + 1];
removeWhitespaces(ordered[i], ret);
ordered[i] = strdup(ret);
}
if (ind == 1)
{
delimiters[0] = '\0';
return 0;
}
else
return 1;
}