-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsing.c
More file actions
153 lines (131 loc) · 3.16 KB
/
parsing.c
File metadata and controls
153 lines (131 loc) · 3.16 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shutility.h"
#include "parsing.h"
#include "hcommand.h"
#define max_len 20
int parsing(char *buffer)
{
char command[max_len] = {'\0'};
char argument[max_len] = {'\0'};
char pipearg1[max_len] = {'\0'};
char pipearg2[max_len] = {'\0'};
char word1[max_len] = {'\0'};
char word2[max_len] = {'\0'};
char *ret;
char *token;
char *inptcpy;
int inptlen;
int check=1;
/* Copy buffer for parsing */
inptlen = strlen(buffer);
inptcpy = (char*) calloc(inptlen + 1, sizeof(char));
strncpy(inptcpy, buffer, inptlen);
/* Check for redirection (>) */
ret = strchr(buffer, '>');
if (ret) {
/* word1 = left of pipe, word2 = right of pipe */
token = strtok(buffer, ">");
strcpy(word1, token);
/* walk through other tokens */
while( token != NULL ) {
token = strtok(NULL, ">");
if(token)
strcpy(word2, token);
}
/* word1 = first command
* argument = first argument
*/
token = strtok(word1, " ");
token = strtok(NULL, " ");
if(token)
strcpy(argument, token);
remove_spaces(word1);
remove_spaces(word2);
if(strcmp(argument, " ") != 0)
remove_spaces(argument);
printf("%s %s %s\n",word1, argument, word2);
redirection_output(word1, argument, word2);
check = 0;
}
/* Check for redirection (<) */
ret = strchr(buffer, '<');
if (ret) {
/*
* Get the first token, copy it to word1,
* get the second token, copy it to word2.
*/
token = strtok(buffer, "<");
strcpy(word1, token);
token = strtok(NULL, "<");
strcpy(word2, token);
remove_spaces(word1);
remove_spaces(word2);
redirection_input(word1, word2);
check = 0;
}
/* Check for pipe (|) */
ret = strchr(buffer, '|');
if (ret) {
/* word1 = left of pipe, word2 = right of pipe */
token = strtok(buffer, "|");
strcpy(word1, token);
/* walk through other tokens */
while( token != NULL ) {
token = strtok(NULL, "|");
if(token)
strcpy(word2, token);
}
/* word1 = first command
* pipearg1 = first argument
*/
token = strtok(word1, " ");
token = strtok(NULL, " ");
if(token)
strcpy(pipearg1, token);
/* word2 = second command
* pipearg2 = second argument
*/
token = strtok(word2, " ");
token = strtok(NULL, " ");
if(token)
strcpy(pipearg2, token);
/* Remove spaces to handle edge cases */
remove_spaces(word1);
if(strcmp(pipearg1, " ") != 0)
remove_spaces(pipearg1);
remove_spaces(word2);
if(strcmp(pipearg2, " ") != 0)
remove_spaces(pipearg2);
handle_pipe(word1, pipearg1, word2, pipearg2);
check = 0;
}
/* Isolate command */
strcpy(command, buffer);
strtok(command, " ");
/* Isolate argument */
token = strtok(inptcpy, " ");
token = strtok(NULL, " ");
if (token)
strcpy(argument, token);
/* If there is no redirection or pipe, handle command. */
if (check)
handle_command(command, argument, buffer, ret);
check = 1;
if (inptcpy)
free(inptcpy);
return 1;
}
void remove_spaces(char* source)
{
char *i = source;
char *j = source;
while (*j != 0) {
*i = *j++;
if (*i != ' ')
i++;
}
*i = 0;
}