-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.c
206 lines (172 loc) · 4.62 KB
/
validate.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Alina Hlazkova (346316979)
* Sergey Nazaryev (342537685)
*/
#include <stdio.h>
#include <string.h>
#include "cmd.h"
#include "validate.h"
#include "helper.h"
static int validate_cd(command_t *cmd);
static int validate_cat(command_t *cmd);
static int validate_nano(command_t *cmd);
static int validate_wc(command_t *cmd);
static int validate_cp(command_t *cmd);
static int validate_grep(command_t *cmd);
static int validate_man(command_t *cmd);
static int validate_ls(command_t *cmd);
static int validate_zero_args(command_t *cmd);
static int validate_noop(command_t *cmd);
static struct validate_info cmds[] = {
{ "pwd", validate_zero_args },
{ "cd", validate_cd },
{ "cat", validate_cat },
{ "nano", validate_nano },
{ "wc", validate_wc },
{ "cp", validate_cp },
{ "whoami", validate_zero_args },
{ "grep", validate_grep },
{ "man", validate_man },
{ "ls", validate_ls },
{ "tree", validate_zero_args },
{ "exit", validate_zero_args },
};
static int validate_flags(command_t *cmd, char *valid_flags, int maximum_positional_args, int minimal_positional_args) {
int flag_end = 0;
int argc = cmd_argslen(cmd);
int positional_args = 0;
int valid_flags_count = strlen(valid_flags);
int i, j;
for (i = 1; i < argc; i++)
{
if (cmd->args[i][0] == '-' && cmd->args[i][1] != '\0' && !flag_end) {
char flag = cmd->args[i][1];
int flag_found = 0;
for (j = 0; j < strlen(valid_flags); j++)
{
if (flag == valid_flags[j])
{
flag_found = 1;
break;
}
}
if (!flag_found)
{
fprintf(stderr, "error: there is no flag -%c for `%s`\n", flag, cmd->args[0]);
return 0;
}
} else {
positional_args++;
flag_end = 1;
}
}
if (positional_args > maximum_positional_args ||
positional_args < minimal_positional_args)
return 0;
return 1;
}
static int validate_noop(command_t *cmd)
{
return 1;
}
static int validate_zero_args(command_t *cmd)
{
if (cmd_argslen(cmd) != 1) {
fprintf(stderr, "usage: %s\n", cmd->args[0]);
return 0;
}
return 1;
}
static int validate_nano(command_t *cmd)
{
if (!validate_flags(cmd, "", 1, 1))
{
fprintf(stderr, "usage: %s <PATH>\n", cmd->args[0]);
return 0;
}
return 1;
}
static int validate_cat(command_t *cmd)
{
if (!validate_flags(cmd, "", 1, 0))
{
fprintf(stderr, "usage: %s <PATH>\n", cmd->args[0]);
return 0;
}
return 1;
}
static int validate_wc(command_t *cmd)
{
if (!validate_flags(cmd, "lcw", 1, 1))
{
fprintf(stderr, "usage: %s [OPTION] <FILE>\n", cmd->args[0]);
fprintf(stderr, "\n");
fprintf(stderr, "-c print the byte counts\n");
fprintf(stderr, "-l print the newline counts\n");
fprintf(stderr, "-w print the word counts\n");
return 0;
}
return 1;
}
static int validate_cp(command_t *cmd)
{
if (!validate_flags(cmd, "", 2, 2))
{
fprintf(stderr, "usage: %s <FILE1> <FILE2>\n", cmd->args[0]);
return 0;
}
return 1;
}
static int validate_grep(command_t *cmd)
{
if (!validate_flags(cmd, "c", 2, 2))
{
fprintf(stderr, "usage: %s [OPTION] <PATTERN> <FILE>\n", cmd->args[0]);
fprintf(stderr, "\n");
fprintf(stderr, "-c Only print the number of matches in file.\n");
return 0;
}
return 1;
}
static int validate_ls(command_t *cmd)
{
if (!validate_flags(cmd, "l", 0, 0))
{
fprintf(stderr, "usage: %s [OPTION]\n", cmd->args[0]);
fprintf(stderr, "\n");
fprintf(stderr, "-l use a long listing format\n");
return 0;
}
return 1;
}
static int validate_cd(command_t *cmd)
{
if (!validate_flags(cmd, "", 1, 1))
{
fprintf(stderr, "usage: %s <PATH>\n", cmd->args[0]);
return 0;
}
return 1;
}
static int validate_man(command_t *cmd)
{
if (cmd_argslen(cmd) != 2)
{
fprintf(stderr, "usage: %s <COMMAND>\n", cmd->args[0]);
return 0;
}
if (validate_info_get(cmd->args[1]) == NULL)
{
fprintf(stderr, "error: there is no man for `%s`\n", cmd->args[1]);
return 0;
}
return 1;
}
struct validate_info *validate_info_get(char * const cmdname)
{
int i;
for (i = 0; i < ARRAY_SIZE(cmds); i++)
if (!strcmp(cmds[i].cmdname, cmdname))
return &cmds[i];
return NULL;
}