command.c 7.33 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 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "connection.h"
#include "io.h"
#include "xalloc.h"

#define CLASS_DEFINE
#include "command.h.x"
#undef CLASS_DEFINE

#include "command.c.x"

/* CLASS:
   (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;
}
    
/* Combinators */

/* Ix == x */
static int do_command_I(struct command *ignored UNUSED,
			struct lsh_object *arg,
			struct command_continuation *c)			
{
  return COMMAND_RETURN(c, arg);
}

struct command command_I =
{ STATIC_HEADER, do_command_I };

/* ((S f) g)x == (f x)(g x) */

/* Represents (S f) */
/* CLASS:
   (class
     (name command_S_1)
     (super command)
     (vars
       (f object command)))
*/

/* Represents ((S f) g) */
/* CLASS:
   (class
     (name command_S_2)
     (super command)
     (vars
       (f object command)
       (g object command)))
*/

/* Continuation called after evaluating (f x) */
/* CLASS:
   (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));
}

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

static int do_command_S_1(struct command *s,
			  struct lsh_object *a,
			  struct command_continuation *c)
			  
{
  CAST(command_S_1, self, s);
  CAST_SUBTYPE(command, arg, a);
  NEW(command_S_2, res);
  res->f = self->f;
  res->g = arg;
  
  res->super.call = do_command_S_2;

  return COMMAND_RETURN(c, &res->super.super);
}

static int do_command_S(struct command *ignored UNUSED,
			struct lsh_object *a,
			struct command_continuation *c)
{
  CAST_SUBTYPE(command, arg, a);
  NEW(command_S_1, res);
  res->f = arg;
  res->super.call = do_command_S_1;

  return COMMAND_RETURN(c, &res->super.super);
}

struct command command_S = { STATIC_HEADER, do_command_S };

/* ((B x) y) z == (x (y z)) */

/* Represents (B x) */
Niels Möller's avatar
Niels Möller committed
161
162
/* CLASS:
   (class
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
     (name command_B_1)
     (super command)
     (vars
       (x object command)))
*/

/* Represents ((B x) y) */
/* CLASS:
   (class
     (name command_B_2)
     (super command)
     (vars
       (x object command)
       (y object command)))
*/

static int do_command_B_2(struct command *s,
			  struct lsh_object *z,
			  struct command_continuation *c)
{
  CAST(command_B_2, self, s);
  return COMMAND_CALL(self->y, z, make_apply(self->x, c));
}

static int do_command_B_1(struct command *s,
			  struct lsh_object *a,
			  struct command_continuation *c)
{
  CAST(command_B_1, self, s);
  CAST_SUBTYPE(command, y, a);
  NEW(command_B_2, res);
  res->x = self->x;
  res->y = y;
  res->super.call = do_command_B_2;

  return COMMAND_RETURN(c, &res->super.super);
}

static int do_command_B(struct command *ignored UNUSED,
			struct lsh_object *a,
			struct command_continuation *c)
{
  NEW(command_B_1, res);
  CAST_SUBTYPE(command, x, a);
  res->x = x;
  res->super.call = do_command_B_1;

  return COMMAND_RETURN(c, &res->super.super);
}

struct command command_B = { STATIC_HEADER, do_command_B };

#if 0
/* xxCLASS:
   (class
     (name command_compose_continuation)
Niels Möller's avatar
Niels Möller committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
     (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;
}

242
/* xxCLASS:
Niels Möller's avatar
Niels Möller committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
   (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));
}
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
#endif

/* Returned by listen */
/* CLASS:
   (class
     (name listen_value)
     (vars
       (fd . int)
       (peername string)
       (peerport . int)))
*/

/* CLASS:
   (class
     (name listen_command)
     (super command)
     (vars
       (backend object io_backend)
       (address . "struct sockaddr_in")))
*/

/* CLASS:
   (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
325

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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;
}