-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
70 lines (47 loc) · 1.31 KB
/
parser.cpp
File metadata and controls
70 lines (47 loc) · 1.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
#include <stdlib.h>
#include "parser.h"
#include "util.h"
int type(char *token) {
str_lower(token);
if (str_compare(token, (char *)"exit"))
return -1;
if (str_compare(token, (char *)"copyfrom"))
return 1;
if (str_compare(token, (char *)"copyto"))
return 2;
if (str_compare(token, (char *)"ls"))
return 3;
if (str_compare(token, (char *)"delete"))
return 4;
return 0;
}
Command *parse_command(char *input) {
Command *command = (Command *)malloc(sizeof(Command));
char token[30];
int index = 0;
while (*input != ' ' && *input) {
token[index++] = *input++;
}
token[index] = '\0';
command -> type = type(token);
while (*input == ' ' || *input == '\t')
input++;
if (!*input)
return command;
command -> file_name = (char *)malloc(sizeof(char) * 20);
index = 0;
while (*input && *input != ' ') {
command -> file_name[index++] = *input++;
}
command -> file_name[index] = '\0';
if (command -> type == 2) {
input++;
command -> file_name2 = (char *)malloc(sizeof(char) * 20);
index = 0;
while (*input) {
command -> file_name2[index++] = *input++;
}
command -> file_name2[index] = '\0';
}
return command;
}