command.c 13.9 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
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
/* command.h
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "command.h"

26
27
#include "connection.h"
#include "io.h"
28
#include "werror.h"
29
30
#include "xalloc.h"

31
#define GABA_DEFINE
32
#include "command.h.x"
33
#undef GABA_DEFINE
34
35
36

#include "command.c.x"

37
/* GABA:
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
   (class
     (name command_apply)
     (super command_frame)
     (vars
       (f object command)))
*/

static int do_command_apply(struct command_continuation *s,
			    struct lsh_object *value)
{
  CAST(command_apply, self, s);
  return COMMAND_CALL(self->f, value, self->super.up);
}

struct command_continuation *
make_apply(struct command *f, struct command_continuation *c)
{
  NEW(command_apply, res);
  res->f = f;
  res->super.up = c;
  res->super.super.c = do_command_apply;

  return &res->super.super;
}
62

Niels Möller's avatar
Niels Möller committed
63
64
struct lsh_object *gaba_apply(struct lsh_object *f,
			      struct lsh_object *x)
65
66
67
68
69
{
  CAST_SUBTYPE(command_simple, cf, f);
  return COMMAND_SIMPLE(cf, x);
}

70
int do_call_simple_command(struct command *s,
71
72
73
			   struct lsh_object *arg,
			   struct command_continuation *c)
{
74
  CAST_SUBTYPE(command_simple, self, s);
75
76
77
  return COMMAND_RETURN(c, COMMAND_SIMPLE(self, arg));
}

Niels Möller's avatar
Niels Möller committed
78
79
/* Commands that need to collect some arguments before actually doing
 * anything. */
80

Niels Möller's avatar
Niels Möller committed
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
134
135
136
/* GABA:
   (class
     (name collect_info_4)
     (vars
       (f pointer function "struct lsh_object *"
                  "struct lsh_object *" "struct lsh_object *"
		  "struct lsh_object *" "struct lsh_object *")
       ))
*/

/* GABA:
   (class
     (name collect_info_3)
     (vars
       (f method  "struct lsh_object *"
                  "struct lsh_object *" "struct lsh_object *"
		  "struct lsh_object *")
       (next object collect_info_3)))
*/

/* GABA:
   (class
     (name collect_info_3)
     (vars
       (f method  "struct lsh_object *"
                  "struct lsh_object *" "struct lsh_object *")
       (next object collect_info_2)))
*/
   
/* GABA:
   (class
     (name command_collect)
     (inherit command_simple)
     (vars
       ;; Is called with the argument count (for sanity checks)
       ;; followed by the arguments.
       (f pointer function "struct lsh_object *" void)
       ;; The number of additional arguments to collect before calling f.
       (left . unsigned)))
*/

/* GABA:
   (class
     (name command_simple_1)
     (inherit command_collect)
     (vars
       (a object lsh_object)))
*/

/* GABA:
   (class
     (name command_simple_2)
     (inherit command_simple_0)
     (vars
       (b object lsh_object)))
*/
137

Niels Möller's avatar
Niels Möller committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* GABA:
   (class
     (name command_collect_3)
     (inherit command_collect_2)
     (vars
       (c object lsh_object)))
*/

static void init_command_collect(struct command_collect *self,
				 unsigned left,
				 void * f))(unsigned n, ...))
{
  self->f = f;
  self->left = left;

  self->super.super.call = do_call_simple_command;
}

static struct lsh_object *
do_command_collect_3(struct command_simple *s,
		     struct lsh_object *x)
{
  CAST(command_collect_3, self, s);
  unsigned left = self->left - 1;
  
  if (!left)
    return self->f(3, self->super.a, self->b, x);
  else
    fatal("command.c: comand_collect_4 not implemented\n");
}

static struct lsh_object *
do_command_collect_2(struct command_simple *s,
		     struct lsh_object *x)
{
  CAST(command_collect_2, self, s);
  unsigned left = self->left - 1;
  
  if (!left)
    return self->f(3, self->super.a, self->b, x);
  else
    {
      NEW(command_collect_3, new);
      init_command_simple(&new->super.super.super, self->f, self->left - 1);
      new->super.a = self->a;
      new->b = b;

      new->super.super.super.call_simple = do_command_collect_3;

      return (struct lsh_object *) new;
    }
}

struct lsh_object *do_command_collect_1(struct command_simple *s,
					struct lsh_object *x)
193
{
Niels Möller's avatar
Niels Möller committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  CAST(command_collect_1, self, s);
  unsigned left = self->left - 1;
  
  if (!left)
    return self->f(2, self->a, x);
  else
    {
      NEW(command_collect_2, new);
      init_command_simple(&new->super.super, self->f, left);
      new->super.a = self->a;
      new->b = x;

      new->super.super.call_simple = do_command_collect_2;
      
      return (struct lsh_object *) new;
    }
210
}
Niels Möller's avatar
Niels Möller committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

struct lsh_object *do_command_collect(struct command_simple *s,
				      struct lsh_object *x)
{
  CAST(command_collect, self, s);
  unsigned left = self->left - 1;

  if (!left)
    return self->f(1, x);
  else
    {
      NEW(command_collect_1, new);
      init_command_collect(&new->super, self->f, left);
      f->a = x;

      new->super.call_simple = do_command_collect_1;

      return (struct lsh_object *) new;
    }
}

/* Combinators */

/* Ix == x */
235

236
237
238
static struct lsh_object *
do_simple_command_I(struct command_simple *ignored UNUSED,
		    struct lsh_object *arg)
239
240
241
242
{
  return arg;
}

243
244
struct command_simple command_I =
STATIC_COMMAND_SIMPLE(do_simple_command_I);
245

Niels Möller's avatar
Niels Möller committed
246
static 
247
248
249
/* ((S f) g)x == (f x)(g x) */

/* Continuation called after evaluating (f x) */
250
/* GABA:
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
   (class
     (name command_S_continuation)
     (super command_frame)
     (vars
       (g object command)
       (x object lsh_object)))
*/

static int do_command_S_continuation(struct command_continuation *c,
				     struct lsh_object *value)
{
  CAST(command_S_continuation, self, c);
  CAST_SUBTYPE(command, op, value);
  return COMMAND_CALL(self->g, self->x, make_apply(op, self->super.up));
}

267
/* Represents ((S f) g) */
268
/* GABA:
269
270
271
272
273
274
275
276
   (class
     (name command_S_2)
     (super command_simple)
     (vars
       (f object command)
       (g object command)))
*/

277
278
279
280
281
282
283
284
285
286
287
288
289
290
static int do_command_S_2(struct command *s,
			  struct lsh_object *x,
			  struct command_continuation *up)
{
  CAST(command_S_2, self, s);
  NEW(command_S_continuation, c);
  c->g = self->g;
  c->x = x;
  c->super.up = up;
  c->super.super.c = do_command_S_continuation;
  
  return COMMAND_CALL(self->f, x, &c->super.super);
}

291
292
293
static struct lsh_object *
do_simple_command_S_2(struct command_simple *s,
		      struct lsh_object *x)
294
{
295
296
297
298
  CAST(command_S_2, self, s);
  CAST_SUBTYPE(command_simple, fs, self->f);
  CAST_SUBTYPE(command_simple, gs, self->g);
  CAST_SUBTYPE(command_simple, op, COMMAND_SIMPLE(fs, x));
299
  
300
301
  return COMMAND_SIMPLE(op, COMMAND_SIMPLE(gs, x));
}
302

303
304
struct command *make_command_S_2(struct command *f,
				 struct command *g)
305
306
307
308
309
310
311
312
{
  NEW(command_S_2, res);
  res->f = f;
  res->g = g;
  res->super.super.call = do_command_S_2;
  res->super.call_simple = do_simple_command_S_2;
  
  return &res->super.super;
313
314
}

315
/* Represents (S f) */
316
/* GABA:
317
318
319
320
321
322
323
324
325
326
   (class
     (name command_S_1)
     (super command_simple)
     (vars
       (f object command)))
*/

static struct lsh_object *
do_simple_command_S_1(struct command_simple *s,
		      struct lsh_object *a)
327
{
328
  CAST(command_S_1, self, s);
329
  CAST_SUBTYPE(command, arg, a);
330
331
332
333

  return &make_command_S_2(self->f, arg)->super;
}

334
struct command *make_command_S_1(struct command *f)
335
{
336
  NEW(command_S_1, res);
337
338
339
  res->f = f;
  res->super.super.call = do_call_simple_command;
  res->super.call_simple = do_simple_command_S_1;
340

341
  return &res->super.super;
342
343
}

344
345
346
347
348
349
350
static struct lsh_object *
do_simple_command_S(struct command_simple *ignored UNUSED,
		    struct lsh_object *a)
{
  CAST_SUBTYPE(command, arg, a);
  return &make_command_S_1(arg)->super;
}
351

352
struct command_simple command_S = STATIC_COMMAND_SIMPLE(do_simple_command_S);
353

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
struct lsh_object *gaba_apply_S_1(struct lsh_object *f)
{
  CAST_SUBTYPE(command, cf, f);
  return &make_command_S_1(cf)->super;
}
  
struct lsh_object *gaba_apply_S_2(struct lsh_object *f,
				  struct lsh_object *g)
{
  CAST_SUBTYPE(command, cf, f);
  CAST_SUBTYPE(command, cg, g);
  return &make_command_S_2(cf, cg)->super;
}

/* ((K x) y) == x */

/* Represents (K x) */
371
/* GABA:
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
   (class
     (name command_K_1)
     (super command_simple)
     (vars
       (x object lsh_object)))
*/

static struct lsh_object *
do_simple_command_K_1(struct command_simple *s,
		      struct lsh_object *ignored UNUSED)
{
  CAST(command_K_1, self, s);
  return self->x;
}

struct command *make_command_K_1(struct lsh_object *x)
{
Niels Möller's avatar
Niels Möller committed
389
  NEW(command_K_1, res);
390
391
392
393
394
395
396
  res->x = x;
  res->super.super.call = do_call_simple_command;
  res->super.call_simple = do_simple_command_K_1;

  return &res->super.super;
}

Niels Möller's avatar
Niels Möller committed
397
398
399
400
401
402
403
static struct lsh_object *
do_simple_command_K(struct command_simple *ignored UNUSED,
		    struct lsh_object *a)
{
  return &make_command_K_1(a)->super;
}

404
405
struct command_simple command_K = STATIC_COMMAND_SIMPLE(do_simple_command_K);

406
/* ((B f) g) x == (f (g x)) */
407

408
/* Represents ((B f) g) */
409
/* GABA:
410
411
   (class
     (name command_B_2)
412
     (super command_simple)
413
     (vars
414
415
       (f object command)
       (g object command)))
416
417
418
*/

static int do_command_B_2(struct command *s,
419
			  struct lsh_object *x,
420
421
422
			  struct command_continuation *c)
{
  CAST(command_B_2, self, s);
423
  return COMMAND_CALL(self->g, x, make_apply(self->f, c));
424
425
}

426
427
static struct lsh_object *do_simple_command_B_2(struct command_simple *s,
						struct lsh_object *x)
428
{
429
430
431
432
  CAST(command_B_2, self, s);
  CAST_SUBTYPE(command_simple, fs, self->f);
  CAST_SUBTYPE(command_simple, gs, self->g);
  return COMMAND_SIMPLE(fs, COMMAND_SIMPLE(gs, x));
433
434
}

435
436
437
static struct command *
make_command_B_2(struct command *f,
		 struct command *g)
438
{
439
440
441
442
443
  NEW(command_B_2, res);
  res->f = f;
  res->g = g;
  res->super.super.call = do_command_B_2;
  res->super.call_simple = do_simple_command_B_2;
444

445
  return &res->super.super;
446
447
}

448
/* Represents (B f) */
449
/* GABA:
450
   (class
451
452
     (name command_B_1)
     (super command_simple)
Niels Möller's avatar
Niels Möller committed
453
     (vars
454
       (f object command)))
Niels Möller's avatar
Niels Möller committed
455
456
*/

457
458
459
static struct lsh_object *
do_simple_command_B_1(struct command_simple *s,
		      struct lsh_object *a)
Niels Möller's avatar
Niels Möller committed
460
{
461
462
463
464
  CAST(command_B_1, self, s);
  CAST_SUBTYPE(command, g, a);

  return &make_command_B_2(self->f, g)->super;
Niels Möller's avatar
Niels Möller committed
465
466
}

467
static struct command *make_command_B_1(struct command *f)
Niels Möller's avatar
Niels Möller committed
468
{
469
470
471
472
  NEW(command_B_1, res);
  res->f = f;
  res->super.super.call = do_call_simple_command;
  res->super.call_simple = do_simple_command_B_1;
Niels Möller's avatar
Niels Möller committed
473

474
  return &res->super.super;
Niels Möller's avatar
Niels Möller committed
475
476
}

477
478
479
static struct lsh_object *
do_simple_command_B(struct command_simple *ignored UNUSED,
		    struct lsh_object *a)
Niels Möller's avatar
Niels Möller committed
480
{
481
482
  CAST_SUBTYPE(command, f, a);
  return &make_command_B_1(f)->super;
Niels Möller's avatar
Niels Möller committed
483
}
484
485

struct command_simple command_B = STATIC_COMMAND_SIMPLE(do_simple_command_B);
486

487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/* Unimplemented command */
static int
do_command_unimplemented(struct command *s UNUSED,
			 struct lsh_object *o UNUSED,
			 struct command_continuation *c UNUSED)
{ fatal("command.c: Unimplemented command.\n"); }

static struct lsh_object *
do_command_simple_unimplemented(struct command_simple *s UNUSED,
				struct lsh_object *o UNUSED)
{ fatal("command.c: Unimplemented simple command.\n"); }

struct command_simple command_unimplemented =
{ { STATIC_HEADER, do_command_unimplemented}, do_command_simple_unimplemented};

502
/* Returned by listen */
503
/* GABA:
504
505
506
507
508
509
510
511
   (class
     (name listen_value)
     (vars
       (fd . int)
       (peername string)
       (peerport . int)))
*/

512
/* GABA:
513
514
515
516
517
518
519
520
   (class
     (name listen_command)
     (super command)
     (vars
       (backend object io_backend)
       (address . "struct sockaddr_in")))
*/

521
/* GABA:
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
   (class
     (name listen_command_callback)
     (super fd_listen_callback)
     (vars
       (c object command_continuation)))
*/

static int do_listen_continue(struct fd_listen_callback *s, int fd,
			      size_t addr_len UNUSED,
			      struct sockaddr *peer UNUSED )
{
  CAST(listen_command_callback, self, s);
  NEW(listen_value, res);
  res->fd = fd;
  /* Parse peer addr */
  return COMMAND_RETURN(self->c, &res->super);
}

static struct fd_listen_callback *
make_listen_command_callback(struct command_continuation *c)
{
  NEW(listen_command_callback, closure);
  closure->c = c;
  closure->super.f = do_listen_continue;
  
  return &closure->super;
}

static int do_listen_call(struct command *s,
			  struct lsh_object *arg,
			  struct command_continuation *c)		  
{
  CAST(listen_command, self, s);
  CAST(ssh_connection, connection, arg);
  struct listen_fd *fd = io_listen(self->backend,
				   &self->address,
				   make_listen_command_callback(c));

  if (!fd)
    COMMAND_RETURN(c, NULL);
  
  REMEMBER_RESOURCE(connection->resources,
		    &fd->super.super);

  return LSH_OK | LSH_GOON;
}
Niels Möller's avatar
Niels Möller committed
568

569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
struct command *make_listen_command(struct io_backend *backend,
				    struct lsh_string *interface,
				    UINT32 port)
{
  NEW(listen_command, self);
  self->backend = backend;
  if (!tcp_addr(&self->address,
		interface->length,
		interface->data,
		port))
    {
      KILL(self);
      return NULL;
    }
  self->super.call = do_listen_call;

  return &self->super;
}
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630

#if 0
/* xxCLASS:
   (class
     (name command_compose_continuation)
     (super command_continuation)
     (vars
       (f object command)
       (c object command_continuation)))
*/

static int do_continue_compose(struct command_continuation *c,
			       struct lsh_object *value)
{
  CAST(compose_continuation, self, c);
  return COMMAND_CALL(self->f, value, self->c);
}

static struct command_continuation *
make_compose_continuation(struct command *f, struct command_continuation *c)
{
  NEW(compose_continuation, self);
  self->f = f;
  self->c = c;

  return &self->super;
}

/* xxCLASS:
   (class
     (name command_compose)
     (super command)
     (vars
       (arg object command)
       (f object command)))
*/

int do_compose_call(struct command_continuation *c, lsh_object *value)
{
  CAST(command_compose, self, c);
  return COMMAND_CALL(self->arg,
		      make_compose_continuation(self->f, c));
}
#endif