-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.c
52 lines (43 loc) · 1.11 KB
/
command.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
#include <ctype.h>
#include "stdh.h"
#include "command.h"
static const struct command commands[] = {
{ "LOGIN", RANK_ANY, CONN_SOCKET, LOG_NONE, do_login },
{ 0, RANK_OWNER+1, CONN_FREED, LOG_NONE, 0 } // last command / null terminator
};
int parse_command(CONNECTION *connection, char *cmd_buf) {
char cmd[32] = {'\0'};
char *p = cmd;
int i;
// skip any preceding whitespace
while(isspace(*cmd_buf) && *cmd_buf != '\0') {
cmd_buf++;
}
// copy until whitespace or EOF
while(!isspace(*cmd_buf) && *cmd_buf != '\0') {
*p++ = *cmd_buf++;
}
*p = '\0';
// check if command exists
for(i = 0; commands[i].name; i++) {
if(!strcmp(commands[i].name, cmd)) {
break;
}
}
if(commands[i].name == 0) {
connection_write(connection, "Unknown command");
return false;
}
if(!IsSet(commands[i].connection_states, connection->state)) {
connection_write(connection, "Invalid connection state for command.");
return false;
}
// check rank here
// if(commands[i].rank ...)
// call function
(*commands[i].function) (connection);
return true;
}
CMD(do_login) {
connection_write(connection, "logging in");
}