command.c 16.4 KB
Newer Older
1
/* command.c
Niels Möller's avatar
Niels Möller committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Möller
 *
 * 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
32
#include <assert.h>

33
#define GABA_DEFINE
34
#include "command.h.x"
35
#undef GABA_DEFINE
36
37
38

#include "command.c.x"

Niels Möller's avatar
Niels Möller committed
39
static void
40
41
do_discard_continuation(struct command_continuation *ignored UNUSED,
			struct lsh_object *x UNUSED)
Niels Möller's avatar
Niels Möller committed
42
{}
43
44
45
46

struct command_continuation discard_continuation =
{ STATIC_HEADER, do_discard_continuation};

47
48
49
50
51
52
53
struct command_context *
make_command_context(struct command_continuation *c,
		     struct exception_handler *e)
{
  NEW(command_context, self);
  self->c = c;
  self->e = e;
54

55
56
57
  return self;
}
  
58
/* GABA:
59
60
61
62
63
64
65
   (class
     (name command_apply)
     (super command_frame)
     (vars
       (f object command)))
*/

Niels Möller's avatar
Niels Möller committed
66
67
68
static void
do_command_apply(struct command_continuation *s,
		 struct lsh_object *value)
69
70
{
  CAST(command_apply, self, s);
Niels Möller's avatar
Niels Möller committed
71
72
  COMMAND_CALL(self->f, value,
	       self->super.up, self->super.e);
73
74
75
}

struct command_continuation *
76
77
make_apply(struct command *f,
	   struct command_continuation *c, struct exception_handler *e)
78
79
{
  NEW(command_apply, res);
80
81
82
83
  assert(f);
  assert(c);
  assert(e);
  
84
85
  res->f = f;
  res->super.up = c;
86
  res->super.e = e;
87
88
89
90
  res->super.super.c = do_command_apply;

  return &res->super.super;
}
91

92
93
94
95
96
97
98
99
100
101
102
/* GABA:
   (class
     (name gaba_continuation)
     (super command_continuation)
     (vars
       (value object lsh_object)))
*/

static void
do_gaba_continuation(struct command_continuation *c,
		     struct lsh_object *x)
103
{
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  CAST(gaba_continuation, self, c);

  assert(!self->value);
  self->value = x;
}

struct lsh_object *
gaba_apply(struct lsh_object *f,
	   struct lsh_object *x)
{
  CAST_SUBTYPE(command, cf, f);
  struct gaba_continuation c =
  { { STATIC_HEADER, do_gaba_continuation }, NULL };

  COMMAND_CALL(cf, x, &c.super, &default_exception_handler);
  return c.value;
120
121
}

Niels Möller's avatar
Niels Möller committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* A command taking 2 arguments */
/* GABA:
   (class
     (name command_2_invoke)
     (super command)
     (vars
       (f object command_2)
       (a1 object lsh_object)))
*/

static void
do_command_2_invoke(struct command *s, struct lsh_object *a2,
		    struct command_continuation *c,
		    struct exception_handler *e)
{
  CAST(command_2_invoke, self, s);
138
  COMMAND_2_INVOKE(self->f, self->a1, a2, c, e);
Niels Möller's avatar
Niels Möller committed
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
193
194
195
196
197
198
199
200
201
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
232
233
234
235
236
237
238
239
240
241
242
}

struct command *
make_command_2_invoke(struct command_2 *f,
		      struct lsh_object *a1)
{
  NEW(command_2_invoke, self);

  self->super.call = do_command_2_invoke;
  self->f = f;
  self->a1 = a1;

  return &self->super;
}

void
do_command_2(struct command *s,
	     struct lsh_object *a1,
	     struct command_continuation *c,
	     struct exception_handler *e UNUSED)
{
  CAST(command_2, self, s);
  COMMAND_RETURN(c, make_command_2_invoke(self, a1));
}

/* A command taking 3 arguments */
/* GABA:
   (class
     (name command_3_invoke_2)
     (super command)
     (vars
       (f object command_3)
       (a1 object lsh_object)
       (a2 object lsh_object)))
*/

static void
do_command_3_invoke_2(struct command *s,
		      struct lsh_object *a3,
		      struct command_continuation *c,
		      struct exception_handler *e)
{
  CAST(command_3_invoke_2, self, s);
  self->f->invoke(self->a1, self->a2, a3, c, e);
}

struct command *
make_command_3_invoke_2(struct command_3 *f,
			struct lsh_object *a1,
			struct lsh_object *a2)
{
  NEW(command_3_invoke_2, self);

  self->super.call = do_command_3_invoke_2;
  self->f = f;
  self->a1 = a1;
  self->a2 = a2;

  return &self->super;
}

/* GABA:
   (class
     (name command_3_invoke)
     (super command)
     (vars
       (f object command_3)
       (a1 object lsh_object)))
*/

static void
do_command_3_invoke(struct command *s,
		    struct lsh_object *a2,
		    struct command_continuation *c,
		    struct exception_handler *e UNUSED)
{
  CAST(command_3_invoke, self, s);
  COMMAND_RETURN(c, make_command_3_invoke_2(self->f, self->a1, a2));
}

struct command *
make_command_3_invoke(struct command_3 *f,
		      struct lsh_object *a1)
{
  NEW(command_3_invoke, self);

  self->super.call = do_command_3_invoke;
  self->f = f;
  self->a1 = a1;

  return &self->super;
}

void
do_command_3(struct command *s,
	     struct lsh_object *a1,
	     struct command_continuation *c,
	     struct exception_handler *e UNUSED)
{
  CAST(command_3, self, s);
  COMMAND_RETURN(c, make_command_3_invoke(self, a1));
}


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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* A command taking 4 arguments */
/* GABA:
   (class
     (name command_4_invoke_3)
     (super command)
     (vars
       (f object command_4)
       (a1 object lsh_object)
       (a2 object lsh_object)
       (a3 object lsh_object)))
*/

static void
do_command_4_invoke_3(struct command *s,
		      struct lsh_object *a4,
		      struct command_continuation *c,
		      struct exception_handler *e)
{
  CAST(command_4_invoke_3, self, s);
  self->f->invoke(self->a1, self->a2, self->a3, a4, c, e);
}

struct command *
make_command_4_invoke_3(struct command_4 *f,
			struct lsh_object *a1,
			struct lsh_object *a2,
			struct lsh_object *a3)
{
  NEW(command_4_invoke_3, self);

  self->super.call = do_command_4_invoke_3;
  self->f = f;
  self->a1 = a1;
  self->a2 = a2;
  self->a3 = a3;

  return &self->super;
}

/* GABA:
   (class
     (name command_4_invoke_2)
     (super command)
     (vars
       (f object command_4)
       (a1 object lsh_object)
       (a2 object lsh_object)))
*/

static void
do_command_4_invoke_2(struct command *s,
		      struct lsh_object *a3,
		      struct command_continuation *c,
		      struct exception_handler *e UNUSED)
{
  CAST(command_4_invoke_2, self, s);
  COMMAND_RETURN(c, make_command_4_invoke_3(self->f, self->a1, self->a2, a3));
}

struct command *
make_command_4_invoke_2(struct command_4 *f,
			struct lsh_object *a1,
			struct lsh_object *a2)
{
  NEW(command_4_invoke_2, self);

  self->super.call = do_command_4_invoke_2;
  self->f = f;
  self->a1 = a1;
  self->a2 = a2;

  return &self->super;
}


/* GABA:
   (class
     (name command_4_invoke)
     (super command)
     (vars
       (f object command_4)
       (a1 object lsh_object)))
*/

static void
do_command_4_invoke(struct command *s,
		    struct lsh_object *a2,
		    struct command_continuation *c,
		    struct exception_handler *e UNUSED)
{
  CAST(command_4_invoke, self, s);
  COMMAND_RETURN(c, make_command_4_invoke_2(self->f, self->a1, a2));
}

struct command *
make_command_4_invoke(struct command_4 *f,
		      struct lsh_object *a1)
{
  NEW(command_4_invoke, self);

  self->super.call = do_command_4_invoke;
  self->f = f;
  self->a1 = a1;

  return &self->super;
}

void
do_command_4(struct command *s,
	     struct lsh_object *a1,
	     struct command_continuation *c,
	     struct exception_handler *e UNUSED)
{
  CAST(command_4, self, s);
  COMMAND_RETURN(c, make_command_4_invoke(self, a1));
}


Niels Möller's avatar
Niels Möller committed
361
362
363
364
365
void
do_call_simple_command(struct command *s,
		       struct lsh_object *arg,
		       struct command_continuation *c,
		       struct exception_handler *e UNUSED)
366
{
367
  CAST_SUBTYPE(command_simple, self, s);
368
  COMMAND_RETURN(c, COMMAND_SIMPLE_CALL(self, arg));
369
370
}

371

372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* Tracing */

#if DEBUG_TRACE

/* GABA:
   (class
     (name trace_continuation)
     (super command_continuation)
     (vars
       (name . "const char *")
       (real object command_continuation)))
*/

static void
do_trace_continuation(struct command_continuation *s,
		      struct lsh_object *x)
{
  CAST(trace_continuation, self, s);

391
392
  trace("Leaving %z, value of type %t.\n",
	self->name, x);
393
394
395
396
397
398
399
400
401
402
403
404
405
406
  COMMAND_RETURN(self->real, x);
}

static struct command_continuation *
make_trace_continuation(const char *name,
			struct command_continuation *real)
{
  NEW(trace_continuation, self);
  self->super.c = do_trace_continuation;
  self->name = name;
  self->real = real;

  return &self->super;
}
Niels Möller's avatar
Niels Möller committed
407
408
409
410
411
412
413
414
415
416

/* GABA:
   (class
     (name trace_command)
     (super command)
     (vars
       (name . "const char *")
       (real object command)))
*/

Niels Möller's avatar
Niels Möller committed
417
418
419
420
421
static void
do_trace_command(struct command *s,
		 struct lsh_object *x,
		 struct command_continuation *c,
		 struct exception_handler *e)
Niels Möller's avatar
Niels Möller committed
422
423
424
425
{
  CAST(trace_command, self, s);

  trace("Entering %z\n", self->name);
426
427
428
429
430
#if 1
  COMMAND_CALL(self->real, x,
	       make_trace_continuation(self->name, c),
	       e);
#else
Niels Möller's avatar
Niels Möller committed
431
  COMMAND_CALL(self->real, x, c, e);
432
#endif
Niels Möller's avatar
Niels Möller committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
}

struct command *make_trace(const char *name, struct command *real)
{
  NEW(trace_command, self);
  self->super.call = do_trace_command;
  self->name = name;
  self->real = real;

  return &self->super;
}

struct lsh_object *collect_trace(const char *name, struct lsh_object *c)
{
  CAST_SUBTYPE(command, real, c);
  return &make_trace(name, real)->super;
}
450
#endif /* DEBUG_TRACE */
451
452

/* Collecting arguments */
Niels Möller's avatar
Niels Möller committed
453
struct lsh_object *
454
455
456
457
458
do_collect_1(struct command_simple *s, struct lsh_object *a)
{
  CAST(collect_info_1, self, s);
  return self->f(self, a);
}
Niels Möller's avatar
Niels Möller committed
459
460
461

/* GABA:
   (class
462
463
     (name collect_state_1)
     (super command_simple)
Niels Möller's avatar
Niels Möller committed
464
     (vars
465
       (info object collect_info_2)
Niels Möller's avatar
Niels Möller committed
466
467
468
469
470
       (a object lsh_object)))
*/

/* GABA:
   (class
471
472
     (name collect_state_2)
     (super command_simple)
Niels Möller's avatar
Niels Möller committed
473
     (vars
474
475
       (info object collect_info_3)
       (a object lsh_object)
Niels Möller's avatar
Niels Möller committed
476
477
       (b object lsh_object)))
*/
478

Niels Möller's avatar
Niels Möller committed
479
480
/* GABA:
   (class
481
482
     (name collect_state_3)
     (super command_simple)
Niels Möller's avatar
Niels Möller committed
483
     (vars
484
485
486
       (info object collect_info_4)
       (a object lsh_object)
       (b object lsh_object)
Niels Möller's avatar
Niels Möller committed
487
488
489
       (c object lsh_object)))
*/

490
491
492
static struct lsh_object *
do_collect_2(struct command_simple *s,
	     struct lsh_object *x)
Niels Möller's avatar
Niels Möller committed
493
{
494
495
  CAST(collect_state_1, self, s);
  return self->info->f(self->info, self->a, x);
Niels Möller's avatar
Niels Möller committed
496
497
}

498
499
500
struct lsh_object *
make_collect_state_1(struct collect_info_1 *info,
		     struct lsh_object *a)
Niels Möller's avatar
Niels Möller committed
501
{
502
503
504
505
506
507
  NEW(collect_state_1, self);
  self->info = info->next;
  self->a = a;

  self->super.call_simple = do_collect_2;
  self->super.super.call = do_call_simple_command;
Niels Möller's avatar
Niels Möller committed
508
  
509
  return &self->super.super.super;
Niels Möller's avatar
Niels Möller committed
510
511
512
}

static struct lsh_object *
513
514
do_collect_3(struct command_simple *s,
	     struct lsh_object *x)
Niels Möller's avatar
Niels Möller committed
515
{
516
517
  CAST(collect_state_2, self, s);
  return self->info->f(self->info, self->a, self->b, x);
Niels Möller's avatar
Niels Möller committed
518
519
}

520
521
522
523
struct lsh_object *
make_collect_state_2(struct collect_info_2 *info,
		     struct lsh_object *a,
		     struct lsh_object *b)
524
{
525
526
527
528
  NEW(collect_state_2, self);
  self->info = info->next;
  self->a = a;
  self->b = b;
Niels Möller's avatar
Niels Möller committed
529
  
530
531
532
533
  self->super.call_simple = do_collect_3;
  self->super.super.call = do_call_simple_command;
  
  return &self->super.super.super;
534
}
Niels Möller's avatar
Niels Möller committed
535

536
537
538
539
540
541
542
543
/* GABA:
   (class
     (name parallell_progn)
     (super command)
     (vars
       (body object object_list)))
*/

Niels Möller's avatar
Niels Möller committed
544
545
546
547
548
static void
do_parallell_progn(struct command *s,
		   struct lsh_object *x,
		   struct command_continuation *c,
		   struct exception_handler *e)
549
550
551
552
553
554
555
{
  CAST(parallell_progn, self, s);
  unsigned i;
  
  for (i=0; i < LIST_LENGTH(self->body) - 1; i++)
    {
      CAST_SUBTYPE(command, command, LIST(self->body)[i]);
Niels Möller's avatar
Niels Möller committed
556
      COMMAND_CALL(command, x, &discard_continuation, e);
557
558
559
560
    }
  {
    CAST_SUBTYPE(command, command, LIST(self->body)[i]);
    
Niels Möller's avatar
Niels Möller committed
561
    COMMAND_CALL(command, x, c, e);
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  }
}

struct command *make_parallell_progn(struct object_list *body)
{
  assert(LIST_LENGTH(body));
  {
    NEW(parallell_progn, self);
    self->body = body;
    self->super.call = do_parallell_progn;

    return &self->super;
  }
}
Niels Möller's avatar
Niels Möller committed
576

Niels Möller's avatar
Niels Möller committed
577
578
579
580
581
DEFINE_COMMAND(progn_command)
     (struct command *s UNUSED,
      struct lsh_object *a,
      struct command_continuation *c,
      struct exception_handler *e UNUSED)
582
583
{
  CAST(object_list, body, a);
Niels Möller's avatar
Niels Möller committed
584
585
586
587

  COMMAND_RETURN(c, (LIST_LENGTH(body)
		     ? make_parallell_progn(body)
		     : &command_I));
588
}
Niels Möller's avatar
Niels Möller committed
589
   
590
591
592
593
/* Catch command
 *
 * (catch handler body x)
 *
594
595
596
597
 * or
 *
 * ( (catch handler body) x)
 *
598
599
600
601
602
603
604
605
606
 * Invokes (body x), with an exception handler that passes exceptions
 * of certain types to handler. */

/* GABA:
   (class
     (name catch_handler_info)
     (vars
       (mask . UINT32)
       (value . UINT32)
607
       (ignore_value . int)
608
       ; NULL handler means ignore all caught exceptions.
609
610
611
       (handler object command)))
*/

612
struct catch_handler_info *
613
make_catch_handler_info(UINT32 mask, UINT32 value,
614
			int ignore_value,
615
616
617
618
619
			struct command *handler)
{
  NEW(catch_handler_info, self);
  self->mask = mask;
  self->value = value;
620
  self->ignore_value = ignore_value;
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
  self->handler = handler;

  return self;
}

/* GABA:
   (class
     (name catch_handler)
     (super exception_handler)
     (vars
       (c object command_continuation)
       (info object catch_handler_info)))
*/

static void
do_catch_handler(struct exception_handler *s,
		 const struct exception *e)
{
  CAST(catch_handler, self, s);
  
  if ((e->type & self->info->mask) == self->info->value)
642
643
644
645
646
647
648
649
    {
      if (self->info->handler)
	COMMAND_CALL(self->info->handler,
		     e, self->c, self->super.parent);
      else
	trace("do_catch_handler: Ignoring exception: %z.\n",
	      e->msg);
    }
650
651
652
653
654
655
656
  else
    EXCEPTION_RAISE(self->super.parent, e);
}

static struct exception_handler *
make_catch_handler(struct catch_handler_info *info,
		   struct command_continuation *c,
657
658
		   struct exception_handler *e,
		   const char *context)
659
660
661
662
663
{
  NEW(catch_handler, self);

  self->super.raise = do_catch_handler;
  self->super.parent = e;
664
665
  self->super.context = context;
  
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
  self->c = c;
  self->info = info;

  return &self->super;
}

/* GABA:
   (class
     (name catch_apply)
     (super command)
     (vars
       (info object catch_handler_info)
       (body object command)))
*/

static void
do_catch_apply(struct command *s,
	       struct lsh_object *a,
	       struct command_continuation *c,
	       struct exception_handler *e)
{
  CAST(catch_apply, self, s);
688

689
690
691
  COMMAND_CALL(self->body, a, (self->info->ignore_value
			       ? &discard_continuation
			       : c),
692
693
	       make_catch_handler(self->info, c, e,
				  HANDLER_CONTEXT));
694
695
}

696
struct command *
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
make_catch_apply(struct catch_handler_info *info,
		 struct command *body)
{
  NEW(catch_apply, self);
  self->super.call = do_catch_apply;
  self->info = info;
  self->body = body;

  return &self->super;
}


/* GABA:
   (class
     (name catch_collect_body)
     (super command_simple)
     (vars
       (info object catch_handler_info)))
*/

static struct lsh_object *
do_catch_collect_body(struct command_simple *s,
		      struct lsh_object *a)
{
  CAST(catch_collect_body, self, s);
  CAST_SUBTYPE(command, body, a);

  return &make_catch_apply(self->info, body)->super;
}

static struct command *
make_catch_collect_body(struct catch_handler_info *info)
{
  NEW(catch_collect_body, self);
  self->super.super.call = do_call_simple_command;
  self->super.call_simple = do_catch_collect_body;
  self->info = info;

  return &self->super.super;
}

struct lsh_object *
do_catch_simple(struct command_simple *s,
		struct lsh_object *a)
{
  CAST(catch_command, self, s);
  CAST_SUBTYPE(command, f, a);

745
746
  return &(make_catch_collect_body(make_catch_handler_info(self->mask,
							   self->value, self->ignore_value, f))
747
748
	   ->super);
}
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765


/* Catch and report some exceptions.
 *
 * FIXME: This duplicates most of the catch command. */

/* GABA:
   (class
     (name catch_report_apply)
     (super command)
     (vars
       (info object report_exception_info)
       (body object command)))
*/
   
static void
do_catch_report_apply(struct command *s,
Niels Möller's avatar
Niels Möller committed
766
767
768
		      struct lsh_object *a,
		      struct command_continuation *c,
		      struct exception_handler *e)
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
{
  CAST(catch_report_apply, self, s);

  COMMAND_CALL(self->body, a, c,
	       make_report_exception_handler(self->info, e,
					     HANDLER_CONTEXT));
}

struct command *
make_catch_report_apply(struct report_exception_info *info,
			struct command *body)
{
  NEW(catch_report_apply, self);
  self->super.call = do_catch_report_apply;
  self->info = info;
  self->body = body;

  return &self->super;
}

struct lsh_object *
do_catch_report_collect(struct command_simple *s,
			struct lsh_object *a)
{
  CAST(catch_report_collect, self, s);
  CAST_SUBTYPE(command, body, a);

  return &make_catch_report_apply(self->info, body)->super;
}