|
0
|
1 |
#include "commands.h"
|
|
|
2 |
#include "substdio.h"
|
|
|
3 |
#include "stralloc.h"
|
|
|
4 |
#include "str.h"
|
|
|
5 |
#include "case.h"
|
|
|
6 |
|
|
|
7 |
static stralloc cmd = {0};
|
|
|
8 |
|
|
|
9 |
int commands(ss,c)
|
|
|
10 |
substdio *ss;
|
|
|
11 |
struct commands *c;
|
|
|
12 |
{
|
|
|
13 |
int i;
|
|
|
14 |
char *arg;
|
|
|
15 |
|
|
|
16 |
for (;;) {
|
|
|
17 |
if (!stralloc_copys(&cmd,"")) return -1;
|
|
|
18 |
|
|
|
19 |
for (;;) {
|
|
|
20 |
if (!stralloc_readyplus(&cmd,1)) return -1;
|
|
|
21 |
i = substdio_get(ss,cmd.s + cmd.len,1);
|
|
|
22 |
if (i != 1) return i;
|
|
|
23 |
if (cmd.s[cmd.len] == '\n') break;
|
|
|
24 |
++cmd.len;
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
if (cmd.len > 0) if (cmd.s[cmd.len - 1] == '\r') --cmd.len;
|
|
|
28 |
|
|
|
29 |
cmd.s[cmd.len] = 0;
|
|
|
30 |
|
|
|
31 |
i = str_chr(cmd.s,' ');
|
|
|
32 |
arg = cmd.s + i;
|
|
|
33 |
while (*arg == ' ') ++arg;
|
|
|
34 |
cmd.s[i] = 0;
|
|
|
35 |
|
|
|
36 |
for (i = 0;c[i].text;++i) if (case_equals(c[i].text,cmd.s)) break;
|
|
|
37 |
c[i].fun(arg);
|
|
|
38 |
if (c[i].flush) c[i].flush();
|
|
|
39 |
}
|
|
|
40 |
}
|