aboutsummaryrefslogtreecommitdiffstats
path: root/src/command.h
blob: 469422cbb1559570bf666995afe69a2373064b2f (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef L2SU_COMMAND_H_INCLUDED
#define L2SU_COMMAND_H_INCLUDED

#include <stddef.h>

enum {
  CMD_NODE_TYPE_NULL,
  CMD_NODE_TYPE_LITERAL,
  CMD_NODE_TYPE_ARGUMENT,
  CMD_NODE_FLAG_EXEC = 0x100
};

enum {
  CMD_INIT_SUCCESS,
  CMD_INIT_ERROR
};

enum {
  CMD_RESULT_SUCCESS,
  CMD_RESULT_FAIL
};

enum {
  CMD_VALIDATE_PASS,      /* yep this argument is correct */
  CMD_VALIDATE_FAIL_SKIP, /* this argument is NOT correct */
  CMD_VALIDATE_FAIL_ERROR /* this argument IS the correct argument, but it is invalid (stop searching siblings) */
};

enum {
  CMD_PARSE_SUCCESS, /* parse successful */
  CMD_PARSE_ESYNTAX, /* invalid command syntax */
  CMD_PARSE_EUSAGE   /* function was called with NULL arguments */
};

struct l2_command_node;

struct l2_context_node {
  struct l2_context_node *next;
  struct l2_command_node *node;

  char *value; /* for argument command nodes */
};

typedef unsigned (l2_cmd_proc_t)(struct l2_context_node * /* ctx */, char ** /* args */);
typedef unsigned (l2_cmd_validate_proc_t)(const char * /* arg */);

struct l2_command_node {
  unsigned type;
  const char *name;

  union {
    /* for literal nodes */
    const char **aliases;

    /* for argument nodes */
    l2_cmd_validate_proc_t *validate_proc; /* a validate_proc of NULL is assumed to always return CMD_VALIDATE_PASS */
  } extra;

  l2_cmd_proc_t *cmd_proc;

  struct l2_command_node *children;
};

extern struct l2_command_node l2_cmd_root;

/* unsigned l2_cmd_init(void); */

struct l2_parseinfo {
  char **argv;
  l2_cmd_proc_t *proc;
  struct l2_context_node *ctx;
};

unsigned l2_cmd_parse_command(char **argv, struct l2_parseinfo *parseinfo);
void l2_cmd_free_ctx(struct l2_context_node *ctx);

#endif /* include guard */