File: commands.c

package info (click to toggle)
qmail 1.03-14
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 2,648 kB
  • ctags: 1,885
  • sloc: ansic: 14,818; makefile: 2,198; sh: 605; perl: 537
file content (40 lines) | stat: -rw-r--r-- 790 bytes parent folder | download | duplicates (10)
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
#include "commands.h"
#include "substdio.h"
#include "stralloc.h"
#include "str.h"
#include "case.h"

static stralloc cmd = {0};

int commands(ss,c)
substdio *ss;
struct commands *c;
{
  int i;
  char *arg;

  for (;;) {
    if (!stralloc_copys(&cmd,"")) return -1;

    for (;;) {
      if (!stralloc_readyplus(&cmd,1)) return -1;
      i = substdio_get(ss,cmd.s + cmd.len,1);
      if (i != 1) return i;
      if (cmd.s[cmd.len] == '\n') break;
      ++cmd.len;
    }

    if (cmd.len > 0) if (cmd.s[cmd.len - 1] == '\r') --cmd.len;

    cmd.s[cmd.len] = 0;

    i = str_chr(cmd.s,' ');
    arg = cmd.s + i;
    while (*arg == ' ') ++arg;
    cmd.s[i] = 0;

    for (i = 0;c[i].text;++i) if (case_equals(c[i].text,cmd.s)) break;
    c[i].fun(arg);
    if (c[i].flush) c[i].flush();
  }
}