-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.c
132 lines (108 loc) · 3 KB
/
parsing.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
#include "parsing.h"
#include "utils.c"
int freeArgs(char **parsed) { //Currently unused
for(int i = MAXLIST -1; i >= 0; i--) {
free(parsed[i]);
}
return 0;
}
/**
* @brief mallocs a string of length MAXCOM
*
* @param parsedString
* @return char*
*/
char* parseMalloc(char *parsedString) {
char* temp = malloc(sizeof(char) * MAXCOM);
if (parsedString != NULL) {
strcpy(temp, parsedString);
}
return temp;
}
void parseCharToArgs(char **parsed, char splitter) {
int argLen = getArgLen(parsed);
for(int i = 0; i < argLen ; i++ ) {
//Do someting, checking all elements of arglist for the splitter symbol
if(parsed[i][0] == '\0') break;
if (strlen(parsed[i]) > 1) { //as long as it's more than one char to check
if(parsed[i][0] == splitter && parsed[i][1] == '\0') goto parseEnd;
char *p_char = strchr(parsed[i], (int) splitter);
if (p_char != NULL) {
for (int j = argLen -1 ; j > i ; j--) { //then move up all follwing indexes (except the last)
memcpy(parsed[j+1], parsed[j], sizeof(char *));
}
argLen++;
//then split
if (parsed[i][0] == splitter) { //case: first char is splitter
char symbols[2] = {splitter, '\0'};
strcpy(parsed[i+1], &parsed[i][1]);
strcpy(parsed[i], symbols);
} else { //splitter is later in the string:
strcpy(parsed[i+1], p_char);
*p_char = '\0';
}
parseEnd:
if(parsed[i +1][0] == '\0') break;
}
}
}
}
// function for finding pipe
int parsePipe(char* str, char** strpiped) {
for (int i = 0; i < MAXPIPE; i++) {
strpiped[i] = strsep(&str, "|");
if (strpiped[i][0] == '\0')
break;
}
if (strpiped[1][0] == '\0')
return 0; // returns zero if no pipe is found.
else {
return 1;
}
}
int parseChar(char* str, char** parsed, char* splitter, int max) {
char* token;
token = strtok(str, splitter);
for (int i = 0; i < max; i++) {
if (token == NULL) {
break;
}
strcpy(parsed[i], token);
if (strlen(parsed[i]) == 0) {
i--;
}
token = strtok(NULL, splitter);
}
if (parsed[1][0] == '\0')
return 0; // returns zero if no pipe is found.
else {
return 1;
}
}
/**
* @brief Checks if the last non-space (' ') character is '&'
*
* @param str The string to check
* @return int true if last non-space character is '&'
*/
int parseDaemon(char *str) {
int isDaemon = 0;
for(int i = strlen(str)-1; i > 0; i--) {
char end = str[i];
if (end == '&' ){
isDaemon = 1;
str[i] = ' ';
break;
} else if (end == ' ') {
continue;
} else {
break;
}
}
return isDaemon;
}
void parseIO(char **parsed) {
parseCharToArgs(parsed, '<');
parseCharToArgs(parsed, '>');
return;
}