commands.c 5.84 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 *	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.
 */

9
#include <stdio.h>
10
#include <ctype.h>
11
#include <stdlib.h>
12

13
#include "nest/bird.h"
14
#include "lib/resource.h"
15
#include "lib/string.h"
16 17 18 19 20 21
#include "client/client.h"

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

25
static struct cmd_info command_table[] = {
26 27
#include "conf/commands.h"
};
28 29 30

struct cmd_node {
  struct cmd_node *sibling, *son, **plastson;
31
  struct cmd_info *cmd, *help;
32
  int len;
33
  signed char prio;
34 35 36 37 38 39 40 41
  char token[1];
};

static struct cmd_node cmd_root;

void
cmd_build_tree(void)
{
Pavel Tvrdík's avatar
Pavel Tvrdík committed
42
  uint i;
43 44 45

  cmd_root.plastson = &cmd_root.son;

46
  for(i=0; i<ARRAY_SIZE(command_table); i++)
47 48 49 50 51 52 53 54 55
    {
      struct cmd_info *cmd = &command_table[i];
      struct cmd_node *old, *new;
      char *c = cmd->command;

      old = &cmd_root;
      while (*c)
	{
	  char *d = c;
56
	  while (*c && !isspace(*c))
57 58 59 60 61 62
	    c++;
	  for(new=old->son; new; new=new->sibling)
	    if (new->len == c-d && !memcmp(new->token, d, c-d))
	      break;
	  if (!new)
	    {
63
	      int size = sizeof(struct cmd_node) + c-d;
64
	      new = malloc(size);
65
	      bzero(new, size);
66 67 68 69 70
	      *old->plastson = new;
	      old->plastson = &new->sibling;
	      new->plastson = &new->son;
	      new->len = c-d;
	      memcpy(new->token, d, c-d);
Ondřej Zajíček's avatar
Ondřej Zajíček committed
71
	      new->prio = (new->len == 3 && (!memcmp(new->token, "roa", 3) || !memcmp(new->token, "rip", 3))) ? 0 : 1; /* Hack */
72 73
	    }
	  old = new;
74
	  while (isspace(*c))
75 76
	    c++;
	}
77 78 79 80
      if (cmd->is_real_cmd)
	old->cmd = cmd;
      else
	old->help = cmd;
81 82 83 84
    }
}

static void
85
cmd_do_display_help(struct cmd_info *c)
86 87 88 89 90 91 92
{
  char buf[strlen(c->command) + strlen(c->args) + 4];

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

93 94 95 96 97 98 99 100 101
static void
cmd_display_help(struct cmd_info *c1, struct cmd_info *c2)
{
  if (c1)
    cmd_do_display_help(c1);
  else if (c2)
    cmd_do_display_help(c2);
}

102
static struct cmd_node *
103
cmd_find_abbrev(struct cmd_node *root, char *cmd, int len, int *pambiguous)
104 105 106
{
  struct cmd_node *m, *best = NULL, *best2 = NULL;

107
  *pambiguous = 0;
108 109 110 111 112 113
  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))
	{
114 115 116 117
	  if (best && best->prio > m->prio)
	    continue;
	  if (best && best->prio == m->prio)
	    best2 = best;
118 119 120
	  best = m;
	}
    }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  if (best2)
    {
      *pambiguous = 1;
      return NULL;
    }
  return best;
}

static void
cmd_list_ambiguous(struct cmd_node *root, char *cmd, int len)
{
  struct cmd_node *m;

  for(m=root->son; m; m=m->sibling)
    if (m->len > len && !memcmp(m->token, cmd, len))	
      cmd_display_help(m->help, m->cmd);
137 138 139 140 141 142 143 144
}

void
cmd_help(char *cmd, int len)
{
  char *end = cmd + len;
  struct cmd_node *n, *m;
  char *z;
145
  int ambig;
146 147 148 149

  n = &cmd_root;
  while (cmd < end)
    {
150
      if (isspace(*cmd))
151 152 153 154 155
	{
	  cmd++;
	  continue;
	}
      z = cmd;
156
      while (cmd < end && !isspace(*cmd))
157
	cmd++;
158 159 160 161 162 163
      m = cmd_find_abbrev(n, z, cmd-z, &ambig);
      if (ambig)
	{
	  cmd_list_ambiguous(n, z, cmd-z);
	  return;
	}
164 165 166 167
      if (!m)
	break;
      n = m;
    }
168
  cmd_display_help(n->cmd, NULL);
169
  for (m=n->son; m; m=m->sibling)
170 171 172 173 174 175 176
    cmd_display_help(m->help, m->cmd);
}

static int
cmd_find_common_match(struct cmd_node *root, char *cmd, int len, int *pcount, char *buf)
{
  struct cmd_node *m;
177
  int best, best_prio, i;
178 179 180

  *pcount = 0;
  best = -1;
181
  best_prio = -1;
182 183 184 185
  for(m=root->son; m; m=m->sibling)
    {
      if (m->len < len || memcmp(m->token, cmd, len))
	continue;
186 187 188 189 190 191 192 193 194 195

      if (best_prio > m->prio)
	continue;

      if (best_prio < m->prio)
	{
	  *pcount = 0;
	  best = -1;
	}

196 197 198 199 200
      (*pcount)++;
      if (best < 0)
	{
	  strcpy(buf, m->token + len);
	  best = m->len - len;
201
	  best_prio = m->prio;
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	}
      else
	{
	  i = 0;
	  while (i < best && i < m->len - len && buf[i] == m->token[len+i])
	    i++;
	  best = i;
	}
    }
  return best;
}

int
cmd_complete(char *cmd, int len, char *buf, int again)
{
  char *start = cmd;
  char *end = cmd + len;
  char *fin;
  struct cmd_node *n, *m;
  char *z;
  int ambig, cnt = 0, common;

  /* Find the last word we want to complete */
  for(fin=end; fin > start && !isspace(fin[-1]); fin--)
    ;

  /* Find the context */
  n = &cmd_root;
  while (cmd < fin && n->son)
    {
232
      if (isspace(*cmd))
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	{
	  cmd++;
	  continue;
	}
      z = cmd;
      while (cmd < fin && !isspace(*cmd))
	cmd++;
      m = cmd_find_abbrev(n, z, cmd-z, &ambig);
      if (ambig)
	{
	  if (!again)
	    return -1;
	  input_start_list();
	  cmd_list_ambiguous(n, z, cmd-z);
	  input_stop_list();
	  return 0;
	}
      if (!m)
	return -1;
      n = m;
    }

  /* Completion of parameters is not yet supported */
  if (!n->son)
    return -1;

  /* We know the context, let's try to complete */
  common = cmd_find_common_match(n, fin, end-fin, &cnt, buf);
  if (!cnt)
    return -1;
  if (cnt == 1)
    {
      buf[common++] = ' ';
      buf[common] = 0;
      return 1;
    }
  if (common > 0)
    {
      buf[common] = 0;
      return 1;
    }
  if (!again)
    return -1;
  input_start_list();
  cmd_list_ambiguous(n, fin, end-fin);
  input_stop_list();
  return 0;
280
}
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

char *
cmd_expand(char *cmd)
{
  struct cmd_node *n, *m;
  char *c, *b, *args;
  int ambig;

  args = c = cmd;
  n = &cmd_root;
  while (*c)
    {
      if (isspace(*c))
	{
	  c++;
	  continue;
	}
      b = c;
      while (*c && !isspace(*c))
	c++;
      m = cmd_find_abbrev(n, b, c-b, &ambig);
      if (!m)
	{
	  if (!ambig)
	    break;
	  puts("Ambiguous command, possible expansions are:");
	  cmd_list_ambiguous(n, b, c-b);
	  return NULL;
	}
      args = c;
      n = m;
    }
  if (!n->cmd)
    {
315
      puts("No such command. Press `?' for help.");
316 317
      return NULL;
    }
318
  b = malloc(strlen(n->cmd->command) + strlen(args) + 1);
319 320 321
  sprintf(b, "%s%s", n->cmd->command, args);
  return b;
}