command.c
946 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "command.h"
Command* parse_command(char *string) {
size_t len, tmp;
Command *command;
if( string == NULL )
return NULL;
command = malloc(sizeof(Command));
memset(command, '\0', sizeof(Command));
len = strcspn(string, " ");
if( len > 0 ) {
tmp = strcspn(string, "\r\n");
string[tmp] = '\0';
strncpy(command->tag, string, len > 15 ? 15 : len);
} else {
strcpy(command->tag, "gibberish");
return command;
}
tmp = len;
++tmp; /* skip space char */
len = strcspn(string + tmp, " ");
if( len > 0 )
strncpy(command->cmd, string + tmp, len > 15 ? 15 : len);
else {
strcpy(command->cmd, "gibberish");
return command;
}
/* args is the rest of the string */
command->args = string + len + tmp + 1;
if( command->args[0] == '\0' ) /* no args */
command->args = NULL;
return command;
}
void free_command(Command *command) {
free(command);
}