command.c 12.6 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 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
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

Niels Möller's avatar
Niels Möller committed
92
93
struct lsh_object *gaba_apply(struct lsh_object *f,
			      struct lsh_object *x)
94
95
{
  CAST_SUBTYPE(command_simple, cf, f);
96
  return COMMAND_SIMPLE_CALL(cf, x);
97
98
}

Niels Möller's avatar
Niels Möller committed
99
100
101
102
103
void
do_call_simple_command(struct command *s,
		       struct lsh_object *arg,
		       struct command_continuation *c,
		       struct exception_handler *e UNUSED)
104
{
105
  CAST_SUBTYPE(command_simple, self, s);
106
  COMMAND_RETURN(c, COMMAND_SIMPLE_CALL(self, arg));
107
108
}

109

110
/* Unimplemented command */
Niels Möller's avatar
Niels Möller committed
111
static void
112
113
do_command_unimplemented(struct command *s UNUSED,
			 struct lsh_object *o UNUSED,
114
115
			 struct command_continuation *c UNUSED,
			 struct exception_handler *e UNUSED)
116
{ fatal("command.c: Unimplemented command.\n"); }
Niels Möller's avatar
Niels Möller committed
117

118
119
120
121
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"); }
Niels Möller's avatar
Niels Möller committed
122

123
124
125
struct command_simple command_unimplemented =
{ { STATIC_HEADER, do_command_unimplemented}, do_command_simple_unimplemented};

126

127
128
129
130
131
132
133
134
135
136
137
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
/* 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);
  const char *type;

  if (x)
    type = (x->isa) ? x->isa->name : "<STATIC>";
  else
    type = "<NULL>";
  
  trace("Leaving %z, value of type %z.\n",
	self->name, type);
  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
168
169
170
171
172
173
174
175
176
177

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

Niels Möller's avatar
Niels Möller committed
178
179
180
181
182
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
183
184
185
186
{
  CAST(trace_command, self, s);

  trace("Entering %z\n", self->name);
187
188
189
190
191
#if 1
  COMMAND_CALL(self->real, x,
	       make_trace_continuation(self->name, c),
	       e);
#else
Niels Möller's avatar
Niels Möller committed
192
  COMMAND_CALL(self->real, x, c, e);
193
#endif
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
210
}

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;
}
211
#endif /* DEBUG_TRACE */
212
213

/* Collecting arguments */
Niels Möller's avatar
Niels Möller committed
214
struct lsh_object *
215
216
217
218
219
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
220
221
222

/* GABA:
   (class
223
224
     (name collect_state_1)
     (super command_simple)
Niels Möller's avatar
Niels Möller committed
225
     (vars
226
       (info object collect_info_2)
Niels Möller's avatar
Niels Möller committed
227
228
229
230
231
       (a object lsh_object)))
*/

/* GABA:
   (class
232
233
     (name collect_state_2)
     (super command_simple)
Niels Möller's avatar
Niels Möller committed
234
     (vars
235
236
       (info object collect_info_3)
       (a object lsh_object)
Niels Möller's avatar
Niels Möller committed
237
238
       (b object lsh_object)))
*/
239

Niels Möller's avatar
Niels Möller committed
240
241
/* GABA:
   (class
242
243
     (name collect_state_3)
     (super command_simple)
Niels Möller's avatar
Niels Möller committed
244
     (vars
245
246
247
       (info object collect_info_4)
       (a object lsh_object)
       (b object lsh_object)
Niels Möller's avatar
Niels Möller committed
248
249
250
       (c object lsh_object)))
*/

251
252
253
static struct lsh_object *
do_collect_2(struct command_simple *s,
	     struct lsh_object *x)
Niels Möller's avatar
Niels Möller committed
254
{
255
256
  CAST(collect_state_1, self, s);
  return self->info->f(self->info, self->a, x);
Niels Möller's avatar
Niels Möller committed
257
258
}

259
260
261
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
262
{
263
264
265
266
267
268
  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
269
  
270
  return &self->super.super.super;
Niels Möller's avatar
Niels Möller committed
271
272
273
}

static struct lsh_object *
274
275
do_collect_3(struct command_simple *s,
	     struct lsh_object *x)
Niels Möller's avatar
Niels Möller committed
276
{
277
278
  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
279
280
}

281
282
283
284
struct lsh_object *
make_collect_state_2(struct collect_info_2 *info,
		     struct lsh_object *a,
		     struct lsh_object *b)
285
{
286
287
288
289
  NEW(collect_state_2, self);
  self->info = info->next;
  self->a = a;
  self->b = b;
Niels Möller's avatar
Niels Möller committed
290
  
291
292
293
294
  self->super.call_simple = do_collect_3;
  self->super.super.call = do_call_simple_command;
  
  return &self->super.super.super;
295
}
Niels Möller's avatar
Niels Möller committed
296

297
298
299
static struct lsh_object *
do_collect_4(struct command_simple *s,
	     struct lsh_object *x)
Niels Möller's avatar
Niels Möller committed
300
{
301
302
303
  CAST(collect_state_3, self, s);
  return self->info->f(self->info, self->a, self->b, self->c, x);
}
Niels Möller's avatar
Niels Möller committed
304

305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
struct lsh_object *
make_collect_state_3(struct collect_info_3 *info,
		     struct lsh_object *a,
		     struct lsh_object *b,
		     struct lsh_object *c)
{
  NEW(collect_state_3, self);
  self->info = info->next;
  self->a = a;
  self->b = b;
  self->c = c;
  
  self->super.call_simple = do_collect_4;
  self->super.super.call = do_call_simple_command;
  
  return &self->super.super.super;
Niels Möller's avatar
Niels Möller committed
321
322
}

323
324
325
326
327
328
329
330
/* GABA:
   (class
     (name parallell_progn)
     (super command)
     (vars
       (body object object_list)))
*/

Niels Möller's avatar
Niels Möller committed
331
332
333
334
335
static void
do_parallell_progn(struct command *s,
		   struct lsh_object *x,
		   struct command_continuation *c,
		   struct exception_handler *e)
336
337
338
339
340
341
342
{
  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
343
      COMMAND_CALL(command, x, &discard_continuation, e);
344
345
346
347
    }
  {
    CAST_SUBTYPE(command, command, LIST(self->body)[i]);
    
Niels Möller's avatar
Niels Möller committed
348
    COMMAND_CALL(command, x, c, e);
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  }
}

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
363

364
365
366
367
368
COMMAND_SIMPLE(progn_command)
{
  CAST(object_list, body, a);
  return &make_parallell_progn(body)->super;
}
Niels Möller's avatar
Niels Möller committed
369

Niels Möller's avatar
Niels Möller committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398

/* A continuation that passes on its value only the first time it is
 * invoked. */
/* GABA:
   (class
     (name once_continuation)
     (super command_continuation)
     (vars
       (invoked . int)
       (msg . "const char *")
       (up object command_continuation)))
*/

static void
do_once_continuation(struct command_continuation *s,
		     struct lsh_object *value)
{
  CAST(once_continuation, self, s);

  if (!self->invoked)
    {
      self->invoked = 1;
      COMMAND_RETURN(self->up, value);
    }
  else if (self->msg)
    debug("%z", self->msg);
}

struct command_continuation *
399
make_once_continuation(const char *msg,
Niels Möller's avatar
Niels Möller committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
		      struct command_continuation *up)
{
  NEW(once_continuation, self);
  self->super.c = do_once_continuation;
  self->invoked = 0;
  self->msg = msg;
  self->up = up;

  return &self->super;
}


/* Delayed application */

struct delayed_apply *
make_delayed_apply(struct command *f,
		   struct lsh_object *a)
{
  NEW(delayed_apply, self);
  self->f = f;
  self->a = a;
  return self;
}

/* GABA:
   (class
     (name delay_continuation)
     (super command_continuation)
     (vars
       (f object command)
       (up object command_continuation)))
*/

static void
do_delay_continuation(struct command_continuation *c,
		      struct lsh_object *o)
{
  CAST(delay_continuation, self, c);

  COMMAND_RETURN(self->up, make_delayed_apply(self->f, o));
}

struct command_continuation *
make_delay_continuation(struct command *f,
			struct command_continuation *c)
{
  NEW(delay_continuation, self);
  self->super.c = do_delay_continuation;
  self->up = c;
  self->f = f;

  return &self->super;
}

   
455
456
457
458
/* Catch command
 *
 * (catch handler body x)
 *
459
460
461
462
 * or
 *
 * ( (catch handler body) x)
 *
463
464
465
466
467
468
469
470
471
 * 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)
472
       (ignore_value . int)
473
474
475
476
477
       (handler object command)))
*/

static struct catch_handler_info *
make_catch_handler_info(UINT32 mask, UINT32 value,
478
			int ignore_value,
479
480
481
482
483
			struct command *handler)
{
  NEW(catch_handler_info, self);
  self->mask = mask;
  self->value = value;
484
  self->ignore_value = ignore_value;
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  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)
    COMMAND_CALL(self->info->handler, e, self->c, self->super.parent);
  else
    EXCEPTION_RAISE(self->super.parent, e);
}

static struct exception_handler *
make_catch_handler(struct catch_handler_info *info,
		   struct command_continuation *c,
514
515
		   struct exception_handler *e,
		   const char *context)
516
517
518
519
520
{
  NEW(catch_handler, self);

  self->super.raise = do_catch_handler;
  self->super.parent = e;
521
522
  self->super.context = context;
  
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
  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);
545

546
547
548
  COMMAND_CALL(self->body, a, (self->info->ignore_value
			       ? &discard_continuation
			       : c),
549
550
	       make_catch_handler(self->info, c, e,
				  HANDLER_CONTEXT));
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
}

static struct command *
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);

602
603
  return &(make_catch_collect_body(make_catch_handler_info(self->mask,
							   self->value, self->ignore_value, f))
604
605
	   ->super);
}