Statistics
| Branch: | Revision:

iof-bird-daemon / nest / cli.c @ 26c09e1d

History | View | Annotate | Download (5.98 KB)

1
/*
2
 *        BIRD Internet Routing Daemon -- Command-Line Interface
3
 *
4
 *        (c) 1999 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include "nest/bird.h"
10
#include "nest/cli.h"
11
#include "conf/conf.h"
12
#include "lib/string.h"
13

    
14
pool *cli_pool;
15

    
16
static byte *
17
cli_alloc_out(cli *c, int size)
18
{
19
  struct cli_out *o;
20

    
21
  if (!(o = c->tx_write) || o->wpos + size > o->end)
22
    {
23
      if (!o && c->tx_buf)
24
        o = c->tx_buf;
25
      else
26
        {
27
          o = mb_alloc(c->pool, sizeof(struct cli_out) + CLI_TX_BUF_SIZE);
28
          if (c->tx_write)
29
            c->tx_write->next = o;
30
          else
31
            c->tx_buf = o;
32
          o->wpos = o->outpos = o->buf;
33
          o->end = o->buf + CLI_TX_BUF_SIZE;
34
        }
35
      c->tx_write = o;
36
      if (!c->tx_pos)
37
        c->tx_pos = o;
38
      o->next = NULL;
39
    }
40
  o->wpos += size;
41
  return o->wpos - size;
42
}
43

    
44
void
45
cli_printf(cli *c, int code, char *msg, ...)
46
{
47
  va_list args;
48
  byte buf[1024];
49
  int cd = code;
50
  int size, cnt;
51

    
52
  va_start(args, msg);
53
  if (cd < 0)
54
    {
55
      cd = -cd;
56
      if (cd == c->last_reply)
57
        size = bsprintf(buf, " ");
58
      else
59
        size = bsprintf(buf, "%04d-", cd);
60
    }
61
  else
62
    size = bsprintf(buf, "%04d ", cd);
63
  c->last_reply = cd;
64
  cnt = bvsnprintf(buf+size, sizeof(buf)-size-1, msg, args);
65
  if (cnt < 0)
66
    {
67
      cli_printf(c, code < 0 ? -8000 : 8000, "<line overflow>");
68
      return;
69
    }
70
  size += cnt;
71
  buf[size++] = '\n';
72
  memcpy(cli_alloc_out(c, size), buf, size);
73
}
74

    
75
static void
76
cli_copy_message(cli *c)
77
{
78
  byte *p, *q;
79
  unsigned int cnt = 2;
80

    
81
  if (c->ring_overflow)
82
    {
83
      byte buf[64];
84
      int n = bsprintf(buf, "<%d messages lost>\n", c->ring_overflow);
85
      c->ring_overflow = 0;
86
      memcpy(cli_alloc_out(c, n), buf, n);
87
    }
88
  p = c->ring_read;
89
  while (*p)
90
    {
91
      cnt++;
92
      p++;
93
      if (p == c->ring_end)
94
        p = c->ring_buf;
95
      ASSERT(p != c->ring_write);
96
    }
97
  c->async_msg_size += cnt;
98
  q = cli_alloc_out(c, cnt);
99
  *q++ = '+';
100
  p = c->ring_read;
101
  do
102
    {
103
      *q = *p++;
104
      if (p == c->ring_end)
105
        p = c->ring_buf;
106
    }
107
  while (*q++);
108
  c->ring_read = p;
109
  q[-1] = '\n';
110
}
111

    
112
static void
113
cli_hello(cli *c)
114
{
115
  cli_printf(c, 1, "BIRD " BIRD_VERSION " ready.");
116
  c->cont = NULL;
117
}
118

    
119
static void
120
cli_free_out(cli *c)
121
{
122
  struct cli_out *o, *p;
123

    
124
  if (o = c->tx_buf)
125
    {
126
      o->wpos = o->outpos = o->buf;
127
      while (p = o->next)
128
        {
129
          o->next = p->next;
130
          mb_free(p);
131
        }
132
    }
133
  c->tx_write = c->tx_pos = NULL;
134
  c->async_msg_size = 0;
135
}
136

    
137
static byte *cli_rh_pos;
138
static unsigned int cli_rh_len;
139
static int cli_rh_trick_flag;
140
struct cli *this_cli;
141

    
142
static int
143
cli_cmd_read_hook(byte *buf, unsigned int max)
144
{
145
  if (!cli_rh_trick_flag)
146
    {
147
      cli_rh_trick_flag = 1;
148
      buf[0] = '!';
149
      return 1;
150
    }
151
  if (max > cli_rh_len)
152
    max = cli_rh_len;
153
  memcpy(buf, cli_rh_pos, max);
154
  cli_rh_pos += max;
155
  cli_rh_len -= max;
156
  return max;
157
}
158

    
159
static void
160
cli_command(struct cli *c)
161
{
162
  struct config f;
163
  int res;
164

    
165
  if (config->cli_debug > 1)
166
    log(L_TRACE "CLI: %s", c->rx_buf);
167
  bzero(&f, sizeof(f));
168
  f.mem = c->parser_pool;
169
  cf_read_hook = cli_cmd_read_hook;
170
  cli_rh_pos = c->rx_buf;
171
  cli_rh_len = strlen(c->rx_buf);
172
  cli_rh_trick_flag = 0;
173
  this_cli = c;
174
  lp_flush(c->parser_pool);
175
  res = cli_parse(&f);
176
  if (!res)
177
    cli_printf(c, 9001, f.err_msg);
178
}
179

    
180
static void
181
cli_event(void *data)
182
{
183
  cli *c = data;
184
  int err;
185

    
186
  while (c->ring_read != c->ring_write &&
187
      c->async_msg_size < CLI_MAX_ASYNC_QUEUE)
188
    cli_copy_message(c);
189

    
190
  if (c->tx_pos)
191
    ;
192
  else if (c->cont)
193
    c->cont(c);
194
  else
195
    {
196
      err = cli_get_command(c);
197
      if (!err)
198
        return;
199
      if (err < 0)
200
        cli_printf(c, 9000, "Command too long");
201
      else
202
        cli_command(c);
203
    }
204
  if (cli_write(c))
205
    {
206
      cli_free_out(c);
207
      ev_schedule(c->event);
208
    }
209
}
210

    
211
cli *
212
cli_new(void *priv)
213
{
214
  pool *p = rp_new(cli_pool, "CLI");
215
  cli *c = mb_alloc(p, sizeof(cli));
216

    
217
  bzero(c, sizeof(cli));
218
  c->pool = p;
219
  c->priv = priv;
220
  c->event = ev_new(p);
221
  c->event->hook = cli_event;
222
  c->event->data = c;
223
  c->cont = cli_hello;
224
  c->parser_pool = lp_new(c->pool, 4096);
225
  c->rx_buf = mb_alloc(c->pool, CLI_RX_BUF_SIZE);
226
  ev_schedule(c->event);
227
  return c;
228
}
229

    
230
void
231
cli_kick(cli *c)
232
{
233
  if (!c->cont && !c->tx_pos)
234
    ev_schedule(c->event);
235
}
236

    
237
void
238
cli_written(cli *c)
239
{
240
  cli_free_out(c);
241
  ev_schedule(c->event);
242
}
243

    
244
static list cli_log_hooks;
245
static int cli_log_inited;
246

    
247
void
248
cli_set_log_echo(cli *c, unsigned int mask, unsigned int size)
249
{
250
  if (c->ring_buf)
251
    {
252
      mb_free(c->ring_buf);
253
      c->ring_buf = c->ring_end = c->ring_read = c->ring_write = NULL;
254
      rem_node(&c->n);
255
    }
256
  c->log_mask = mask;
257
  if (mask && size)
258
    {
259
      c->ring_buf = mb_alloc(c->pool, size);
260
      c->ring_end = c->ring_buf + size;
261
      c->ring_read = c->ring_write = c->ring_buf;
262
      add_tail(&cli_log_hooks, &c->n);
263
      c->log_threshold = size / 8;
264
    }
265
  c->ring_overflow = 0;
266
}
267

    
268
void
269
cli_echo(unsigned int class, byte *msg)
270
{
271
  unsigned len, free, i, l;
272
  cli *c;
273
  byte *m;
274

    
275
  if (!cli_log_inited || EMPTY_LIST(cli_log_hooks))
276
    return;
277
  len = strlen(msg) + 1;
278
  WALK_LIST(c, cli_log_hooks)
279
    {
280
      if (!(c->log_mask & (1 << class)))
281
        continue;
282
      if (c->ring_read <= c->ring_write)
283
        free = (c->ring_end - c->ring_buf) - (c->ring_write - c->ring_read + 1);
284
      else
285
        free = c->ring_read - c->ring_write - 1;
286
      if (len > free ||
287
          free < c->log_threshold && class < (unsigned) L_INFO[0])
288
        {
289
          c->ring_overflow++;
290
          continue;
291
        }
292
      if (c->ring_read == c->ring_write)
293
        ev_schedule(c->event);
294
      m = msg;
295
      l = len;
296
      while (l)
297
        {
298
          if (c->ring_read <= c->ring_write)
299
            i = c->ring_end - c->ring_write;
300
          else
301
            i = c->ring_read - c->ring_write;
302
          if (i > l)
303
            i = l;
304
          memcpy(c->ring_write, m, i);
305
          m += i;
306
          l -= i;
307
          c->ring_write += i;
308
          if (c->ring_write == c->ring_end)
309
            c->ring_write = c->ring_buf;
310
        }
311
    }
312
}
313

    
314
void
315
cli_free(cli *c)
316
{
317
  cli_set_log_echo(c, 0, 0);
318
  if (c->cleanup)
319
    c->cleanup(c);
320
  rfree(c->pool);
321
}
322

    
323
void
324
cli_init(void)
325
{
326
  cli_pool = rp_new(&root_pool, "CLI");
327
  init_list(&cli_log_hooks);
328
  cli_log_inited = 1;
329
}