blob: 4212296b6ca47d435e87fe98e562d5655cc8d525 (
plain)
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
|
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* TODO */
static int run_cmd(const char *cmd, int argc, char *argv[]) {
puts(cmd);
for (int i = 0; i < argc; i++) {
puts(argv[i]);
}
return 0;
}
/* Tricks with pointers */
int parse(char *str) {
char *cmd = str;
str += strcspn(str, separator);
if (*str) {
*str++ = 0;
}
char *str_counter = str;
int i = 0;
for (; *str_counter; i++) {
str_counter += strspn(str, separator);
if (!*str_counter) {
break;
}
*str_counter++ = 0;
str_counter += strcspn(str_counter, separator);
if(!str) {
return EINVAL;
}
*str++ = 0;
}
char *args[i];
for (int j = 0; *str; j++) {
str += strspn(str, separator);
if (!*str) {
break;
}
*str++ = 0;
args[j] = str;
str += strcspn(str, separator);
if(!str) {
return EINVAL;
}
*str++ = 0;
/* TODO: parse the string left to an array of arguments and pass command
* itself and arguments to the run_cmd() right after end of this loop
*/
}
int response = run_cmd(cmd, i, args);
return response;
}
|