command.c 12.3 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
  res->f = f;
  res->super.up = c;
83
  res->super.e = e;
84
85
86
87
  res->super.super.c = do_command_apply;

  return &res->super.super;
}
88

Niels Möller's avatar
Niels Möller committed
89
90
struct lsh_object *gaba_apply(struct lsh_object *f,
			      struct lsh_object *x)
91
92
93
94
95
{
  CAST_SUBTYPE(command_simple, cf, f);
  return COMMAND_SIMPLE(cf, x);
}

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

106

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

115
116
117
118
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
119

120
121
122
struct command_simple command_unimplemented =
{ { STATIC_HEADER, do_command_unimplemented}, do_command_simple_unimplemented};

123

Niels Möller's avatar
Niels Möller committed
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Tracing
 *
 * For now, trace only function entry. */

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

Niels Möller's avatar
Niels Möller committed
137
138
139
140
141
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
142
143
144
145
{
  CAST(trace_command, self, s);

  trace("Entering %z\n", self->name);
Niels Möller's avatar
Niels Möller committed
146
  COMMAND_CALL(self->real, x, c, e);
Niels Möller's avatar
Niels Möller committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
}

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;
}

165
166

/* Collecting arguments */
Niels Möller's avatar
Niels Möller committed
167
struct lsh_object *
168
169
170
171
172
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
173
174
175

/* GABA:
   (class
176
177
     (name collect_state_1)
     (super command_simple)
Niels Möller's avatar
Niels Möller committed
178
     (vars
179
       (info object collect_info_2)
Niels Möller's avatar
Niels Möller committed
180
181
182
183
184
       (a object lsh_object)))
*/

/* GABA:
   (class
185
186
     (name collect_state_2)
     (super command_simple)
Niels Möller's avatar
Niels Möller committed
187
     (vars
188
189
       (info object collect_info_3)
       (a object lsh_object)
Niels Möller's avatar
Niels Möller committed
190
191
       (b object lsh_object)))
*/
192

Niels Möller's avatar
Niels Möller committed
193
194
/* GABA:
   (class
195
196
     (name collect_state_3)
     (super command_simple)
Niels Möller's avatar
Niels Möller committed
197
     (vars
198
199
200
       (info object collect_info_4)
       (a object lsh_object)
       (b object lsh_object)
Niels Möller's avatar
Niels Möller committed
201
202
203
       (c object lsh_object)))
*/

204
205
206
static struct lsh_object *
do_collect_2(struct command_simple *s,
	     struct lsh_object *x)
Niels Möller's avatar
Niels Möller committed
207
{
208
209
  CAST(collect_state_1, self, s);
  return self->info->f(self->info, self->a, x);
Niels Möller's avatar
Niels Möller committed
210
211
}

212
213
214
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
215
{
216
217
218
219
220
221
  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
222
  
223
  return &self->super.super.super;
Niels Möller's avatar
Niels Möller committed
224
225
226
}

static struct lsh_object *
227
228
do_collect_3(struct command_simple *s,
	     struct lsh_object *x)
Niels Möller's avatar
Niels Möller committed
229
{
230
231
  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
232
233
}

234
235
236
237
struct lsh_object *
make_collect_state_2(struct collect_info_2 *info,
		     struct lsh_object *a,
		     struct lsh_object *b)
238
{
239
240
241
242
  NEW(collect_state_2, self);
  self->info = info->next;
  self->a = a;
  self->b = b;
Niels Möller's avatar
Niels Möller committed
243
  
244
245
246
247
  self->super.call_simple = do_collect_3;
  self->super.super.call = do_call_simple_command;
  
  return &self->super.super.super;
248
}
Niels Möller's avatar
Niels Möller committed
249

250
251
252
static struct lsh_object *
do_collect_4(struct command_simple *s,
	     struct lsh_object *x)
Niels Möller's avatar
Niels Möller committed
253
{
254
255
256
  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
257

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
274
275
}

276
277
278
279
280
281
282
283
/* GABA:
   (class
     (name parallell_progn)
     (super command)
     (vars
       (body object object_list)))
*/

Niels Möller's avatar
Niels Möller committed
284
285
286
287
288
static void
do_parallell_progn(struct command *s,
		   struct lsh_object *x,
		   struct command_continuation *c,
		   struct exception_handler *e)
289
290
291
292
293
294
295
{
  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
296
      COMMAND_CALL(command, x, &discard_continuation, e);
297
298
299
300
    }
  {
    CAST_SUBTYPE(command, command, LIST(self->body)[i]);
    
Niels Möller's avatar
Niels Möller committed
301
    COMMAND_CALL(command, x, c, e);
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  }
}

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
316
317
318
319
320

static struct lsh_object *do_progn(struct command_simple *s UNUSED,
				   struct lsh_object *x)
{
  CAST(object_list, body, x);
321
  return &make_parallell_progn(body)->super;
Niels Möller's avatar
Niels Möller committed
322
323
324
325
326
}

struct command_simple progn_command =
STATIC_COMMAND_SIMPLE(do_progn);

Niels Möller's avatar
Niels Möller committed
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

/* 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 *
356
make_once_continuation(const char *msg,
Niels Möller's avatar
Niels Möller committed
357
358
359
360
361
362
363
364
365
366
367
368
369
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
399
400
401
402
403
404
405
406
407
408
409
410
411
		      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;
}

   
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
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
514
515
516
517
518
519
520
521
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/* Catch command
 *
 * (catch handler body x)
 *
 * 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)
       (handler object command)))
*/

static struct catch_handler_info *
make_catch_handler_info(UINT32 mask, UINT32 value,
			struct command *handler)
{
  NEW(catch_handler_info, self);
  self->mask = mask;
  self->value = value;
  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,
		   struct exception_handler *e)
{
  NEW(catch_handler, self);

  self->super.raise = do_catch_handler;
  self->super.parent = e;
  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);
  COMMAND_CALL(self->body, a, c,
	       make_catch_handler(self->info, c, e));
}

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);

  return &(make_catch_collect_body(make_catch_handler_info(self->mask, self->value, f))
	   ->super);
}


#if 0    
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);
  COMMAND_CALL(self->f, a, c, self->handler);
}

static struct command
make_catch_apply(struct command *f,
		 struct exception_handler *handler)
{
  NEW(catch_apply, self);
  self->super.call = do_catch_apply;
  self->f = f;
  self->handler = handler;
}
   
/* ;; GABA:
   (class
     (name catch_command)
     (super command_simple)
     (vars
       (mask . UINT32)
       (value . UINT32)))
*/

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

  return make_catch_apply(f, make_catch_handler);
}
#endif