summaryrefslogtreecommitdiffstats
path: root/client/commands.c
blob: ceb036b1c769e987f6e9af40e5034c572e002560 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 *	BIRD Client -- Command Handling
 *
 *	(c) 1999--2000 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <stdio.h>

#include "nest/bird.h"
#include "lib/resource.h"
#include "client/client.h"

struct cmd_info {
  char *command;
  char *args;
  char *help;
  int is_real_cmd;
};

static struct cmd_info command_table[] = {
#include "conf/commands.h"
};

/* FIXME: There should exist some system of aliases, so that `show' can be abbreviated as `s' etc. */

struct cmd_node {
  struct cmd_node *sibling, *son, **plastson;
  struct cmd_info *cmd;
  int len;
  char token[1];
};

static struct cmd_node cmd_root;

void
cmd_build_tree(void)
{
  unsigned int i;

  cmd_root.plastson = &cmd_root.son;

  for(i=0; i<sizeof(command_table) / sizeof(struct cmd_info); i++)
    {
      struct cmd_info *cmd = &command_table[i];
      struct cmd_node *old, *new;
      char *c = cmd->command;

      old = &cmd_root;
      while (*c)
	{
	  char *d = c;
	  while (*c && *c != ' ')
	    c++;
	  for(new=old->son; new; new=new->sibling)
	    if (new->len == c-d && !memcmp(new->token, d, c-d))
	      break;
	  if (!new)
	    {
	      new = xmalloc(sizeof(struct cmd_node) + c-d);
	      *old->plastson = new;
	      old->plastson = &new->sibling;
	      new->sibling = new->son = NULL;
	      new->plastson = &new->son;
	      new->cmd = NULL;
	      new->len = c-d;
	      memcpy(new->token, d, c-d);
	      new->token[c-d] = 0;
	    }
	  old = new;
	  while (*c == ' ')
	    c++;
	}
      old->cmd = cmd;
    }
}

static void
cmd_display_help(struct cmd_info *c)
{
  char buf[strlen(c->command) + strlen(c->args) + 4];

  sprintf(buf, "%s %s", c->command, c->args);
  printf("%-45s  %s\n", buf, c->help);
}

static struct cmd_node *
cmd_find_abbrev(struct cmd_node *root, char *cmd, int len)
{
  struct cmd_node *m, *best = NULL, *best2 = NULL;

  for(m=root->son; m; m=m->sibling)
    {
      if (m->len == len && !memcmp(m->token, cmd, len))
	return m;
      if (m->len > len && !memcmp(m->token, cmd, len))
	{
	  best2 = best;
	  best = m;
	}
    }
  return best2 ? NULL : best;
}

void
cmd_help(char *cmd, int len)
{
  char *end = cmd + len;
  struct cmd_node *n, *m;
  char *z;

  n = &cmd_root;
  while (cmd < end)
    {
      if (*cmd == ' ' || *cmd == '\t')
	{
	  cmd++;
	  continue;
	}
      z = cmd;
      while (cmd < end && *cmd != ' ' && *cmd != '\t')
	cmd++;
      m = cmd_find_abbrev(n, z, cmd-z);
      if (!m)
	break;
      n = m;
    }
  if (n->cmd && n->cmd->is_real_cmd)
    cmd_display_help(n->cmd);
  for (m=n->son; m; m=m->sibling)
    cmd_display_help(m->cmd);
}