-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_parser.c
More file actions
164 lines (133 loc) · 3.75 KB
/
Copy pathcommand_parser.c
File metadata and controls
164 lines (133 loc) · 3.75 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
154
155
156
157
158
159
160
161
162
163
164
#include <ctype.h>
#include <string.h>
#include "command_parser.h"
#include "common.h"
static char *skip_whitespace(char *text)
{
while (*text != '\0' &&
isspace((unsigned char)*text)) {
text++;
}
return text;
}
static void remove_line_ending(char *text)
{
size_t length = strlen(text);
while (length > 0 &&
(text[length - 1] == '\n' ||
text[length - 1] == '\r')) {
text[--length] = '\0';
}
}
ParsedCommand parse_command(char *command)
{
ParsedCommand result = {
.type = CMD_UNKNOWN,
.username = NULL,
.message = NULL,
.filepath = NULL
};
if (command == NULL) {
result.type = CMD_INVALID;
return result;
}
remove_line_ending(command);
char *verb = skip_whitespace(command);
char *cursor = verb;
/* Separate the command name from its arguments. */
while (*cursor != '\0' &&
!isspace((unsigned char)*cursor)) {
cursor++;
}
char *arguments = cursor;
if (*cursor != '\0') {
*cursor = '\0';
arguments = skip_whitespace(cursor + 1);
}
if (strcmp(verb, "/list") == 0) {
result.type = (*arguments == '\0')
? CMD_LIST_USERS
: CMD_INVALID;
return result;
}
if (strcmp(verb, "/help") == 0) {
result.type = (*arguments == '\0')
? CMD_HELP
: CMD_INVALID;
return result;
}
if (strcmp(verb, "/quit") == 0) {
result.type = (*arguments == '\0')
? CMD_QUIT
: CMD_INVALID;
return result;
}
if (strcmp(verb, "/broadcast") == 0) {
if (*arguments == '\0') {
result.type = CMD_INVALID;
return result;
}
result.type = CMD_BROADCAST_MESSAGE;
result.message = arguments;
return result;
}
if (strcmp(verb, "/msg") == 0) {
if (*arguments == '\0') {
result.type = CMD_INVALID;
return result;
}
char *username = arguments;
while (*arguments != '\0' &&
!isspace((unsigned char)*arguments)) {
arguments++;
}
/* `/msg username` has no message. */
if (*arguments == '\0') {
result.type = CMD_INVALID;
return result;
}
*arguments = '\0';
char *message = skip_whitespace(arguments + 1);
if (*message == '\0' ||
strlen(username) >= USERNAME_SIZE) {
result.type = CMD_INVALID;
return result;
}
result.type = CMD_PRIVATE_MESSAGE;
result.username = username;
result.message = message;
return result;
}
if (strcmp(verb, "/file") == 0) {
if (*arguments == '\0') {
result.type = CMD_INVALID;
return result;
}
char *username = arguments;
while (*arguments != '\0' &&
!isspace((unsigned char)*arguments)) {
arguments++;
}
if (*arguments == '\0') {
result.type = CMD_INVALID;
return result;
}
*arguments = '\0';
char *filepath = skip_whitespace(arguments + 1);
size_t filepath_length = strlen(filepath);
while (filepath_length > 0 &&
isspace((unsigned char)filepath[filepath_length - 1])) {
filepath[--filepath_length] = '\0';
}
if (*filepath == '\0' ||
strlen(username) >= USERNAME_SIZE) {
result.type = CMD_INVALID;
return result;
}
result.type = CMD_FILE_TRANSFER; /*the type does not matter */
result.username = username;
result.filepath = filepath;
return result;
}
return result;
}