-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatternmatch.c
More file actions
146 lines (127 loc) · 5.31 KB
/
patternmatch.c
File metadata and controls
146 lines (127 loc) · 5.31 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
#include <stdbool.h>
#include <regex.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>
#include "helper.h"
char *commandRegex[7] = {
"^cd ([^ \t><|*!`'\"]+)$", // [cd] [dir]
"^fg ([0-9]+)$", // [fg] [arg]
"^([^ \t><|*!`'\"]+)( ([^ \t><|*!`'\"]+))* < ([^ \t><|*!`'\"]+)(|( > ([^ \t><|*!`'\"]+))|( >> ([^ \t><|*!`'\"]+)))$", // [cmd] '<' [filename] [terminate]
"^([^ \t><|*!`'\"]+)( [^ \t><|*!`'\"]+)*(|( > [^ \t><|*!`'\"]+)|( >> [^ \t><|*!`'\"]+)) < ([^ \t><|*!`'\"]+)$", // [cmd] [terminate] '<' [filename]
"^([^ \t><|*!`'\"]+)( ([^ \t><|*!`'\"]+))*(|( > ([^ \t><|*!`'\"]+))|( >> ([^ \t><|*!`'\"]+)))$", // [cmd] [terminate]
"^(([^ \t><|*!`'\"]+)( ([^ \t><|*!`'\"]+))*)( \\| (([^ \t><|*!`'\"]+)( ([^ \t><|*!`'\"]+))*))*( \\| (([^ \t><|*!`'\"]+)( ([^ \t><|*!`'\"]+))*)(|( > ([^ \t><|*!`'\"]+))|( >> ([^ \t><|*!`'\"]+))))$", // [cmd] | [cmd] | ... | [cmd] [terminate]
"^(([^ \t><|*!`'\"]+)( ([^ \t><|*!`'\"]+))*) < ([^ \t><|*!`'\"]+)( \\| (([^ \t><|*!`'\"]+)( ([^ \t><|*!`'\"]+))*))*( \\| (([^ \t><|*!`'\"]+)( ([^ \t><|*!`'\"]+))*)(|( > ([^ \t><|*!`'\"]+))|( >> ([^ \t><|*!`'\"]+))))$", // [cmd] < [filename] | [cmd] | ... | [cmd] [terminate]
};
bool isMatch(const char *pattern, const char *str) {
regex_t regex;
int reti = regcomp(®ex, pattern, REG_EXTENDED);
if (reti) {
printf("%d", reti);
printf("Could not compile regex. Error no: %d\n", errno);
return false;
}
reti = regexec(®ex, str, 0, NULL, 0);
regfree(®ex);
if (!reti) { // check if reti is 0
return true;
} else if (reti == REG_NOMATCH) {
return false;
} else {
printf("Regex match failed\n");
return false;
}
}
bool isBlankCommand(const char *str) { return strcmp(str, "") == 0; }
bool isCdCommand(const char *str) { return isMatch(commandRegex[0], str); }
bool isExitCommand(const char *str) {
return strcmp(str, "exit") == 0;
}
bool isFgCommand(const char *str) { return isMatch(commandRegex[1], str); }
bool isJobsCommand(const char *str) { return strcmp(str, "jobs") == 0; }
// [cmd] '<' [filename] [terminate]
bool isCmdWithRedirectAndTerminate(const char *str) {
char *strCopy = malloc(strlen(str) + 1);
strcpy(strCopy, str);
// first word before space mustn't be cd, fg, exit, jobs
// POSIX regex doesn't support lookaround
char *firstWord = strtok(strCopy, " ");
if (strcmp(firstWord, "cd") == 0 || strcmp(firstWord, "fg") == 0 || strcmp(firstWord, "exit") == 0 || strcmp(firstWord, "jobs") == 0) {
free(strCopy);
return false;
}
free(strCopy);
return isMatch(commandRegex[2], str);
}
// [cmd] [terminate] '<' [filename]
bool isCmdWithTerminateAndRedirect(const char *str) {
char *strCopy = malloc(strlen(str) + 1);
strcpy(strCopy, str);
// first word before space mustn't be cd, fg, exit, jobs
// POSIX regex doesn't support lookaround
char *firstWord = strtok(strCopy, " ");
if (strcmp(firstWord, "cd") == 0 || strcmp(firstWord, "fg") == 0 || strcmp(firstWord, "exit") == 0 || strcmp(firstWord, "jobs") == 0) {
free(strCopy);
return false;
}
free(strCopy);
return isMatch(commandRegex[3], str);
}
// [cmd] [terminate]
bool isCmdWithTerminate(const char *str) {
char *strCopy = malloc(strlen(str) + 1);
strcpy(strCopy, str);
// first word before space mustn't be cd, fg, exit, jobs
// POSIX regex doesn't support lookaround
char *firstWord = strtok(strCopy, " ");
if (strcmp(firstWord, "cd") == 0 || strcmp(firstWord, "fg") == 0 || strcmp(firstWord, "exit") == 0 || strcmp(firstWord, "jobs") == 0) {
free(strCopy);
return false;
}
free(strCopy);
return isMatch(commandRegex[4], str);
}
// [cmd] | [cmd] | ... | [cmd] [terminate]
bool isCmdWithRecursive(const char *str) {
char *strCopy = malloc(strlen(str) + 1);
strcpy(strCopy, str);
// first word before space mustn't be cd, fg, exit, jobs
// POSIX regex doesn't support lookaround
bool isValid = true;
struct StringSplit split = splitString(strCopy, '|');
for (int i = 0; i < split.size; i++){
char *currentWord = split.str[i];
char *firstWord = strtok(currentWord, " ");
if (strcmp(firstWord, "cd") == 0 || strcmp(firstWord, "fg") == 0 || strcmp(firstWord, "exit") == 0 || strcmp(firstWord, "jobs") == 0) {
isValid = false;
break;
}
}
free(strCopy);
for (int i = 0; i < split.size; i++) {
free(split.str[i]);
}
return isValid && isMatch(commandRegex[5], str);
}
// [cmd] < [filename] | [cmd] | ... | [cmd] [terminate]
bool isCmdWithRedirectRecursive(const char *str) {
char *strCopy = malloc(strlen(str) + 1);
strcpy(strCopy, str);
// first word before space mustn't be cd, fg, exit, jobs
bool isValid = true;
struct StringSplit split = splitString(strCopy, '|');
for (int i = 0; i < split.size; i++){
char *currentWord = split.str[i];
char *firstWord = strtok(currentWord, " ");
if (strcmp(firstWord, "cd") == 0 || strcmp(firstWord, "fg") == 0 || strcmp(firstWord, "exit") == 0 || strcmp(firstWord, "jobs") == 0) {
isValid = false;
break;
}
}
free(strCopy);
for (int i = 0; i < split.size; i++) {
free(split.str[i]);
}
return isValid && isMatch(commandRegex[6], str);
}