summaryrefslogtreecommitdiff
path: root/zwgc/exec.c
blob: 36d22c030483065af36c3a46663df292d41968ab (plain)
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
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
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
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
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
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
/* This file is part of the Project Athena Zephyr Notification System.
 * It is one of the source files comprising zwgc, the Zephyr WindowGram
 * client.
 *
 *      Created by:     Marc Horowitz <marc@athena.mit.edu>
 *
 *      $Id$
 *
 *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h".
 */

#include <sysdep.h>

#if (!defined(lint) && !defined(SABER))
static const char rcsid_exec_c[] = "$Id$";
#endif

#include <zephyr/mit-copyright.h>

/****************************************************************************/
/*                                                                          */
/*               Module containing code to execute a program:               */
/*                                                                          */
/****************************************************************************/

#include <zephyr/zephyr.h>
#include "new_memory.h"
#include "node.h"
#include "exec.h"
#include "eval.h"
#include "buffer.h"
#include "port.h"
#include "variables.h"
#include "notice.h"
#ifdef CMU_ZWGCPLUS
#include "plus.h"
#endif

static int exec_subtree(Node *);
static int exec_fields(Node *);

/****************************************************************************/
/*                                                                          */
/*                           Utility subroutines:                          */
/*                                                                          */
/****************************************************************************/

static string
eval_exprlist_to_string(Node *exprlist)
{
    string result = string_Copy("");
    string temp;
    int first_time = 1;
    
    for (; exprlist; exprlist=exprlist->next) {
	if (!first_time)
	  result = string_Concat2(result, " ");
	else
	  first_time = 0;
	
	temp = eval_expr(exprlist);
	result = string_Concat2(result, temp);
	free(temp);
    }
    
    return(result);
}

static char **
eval_exprlist_to_args(Node *exprlist)
{
    char **result = (char **)malloc(sizeof(char *));
    int argc = 0;

    for (; exprlist; exprlist=exprlist->next) {
	result[argc] = eval_expr(exprlist);
	argc++;
	result = (char **)realloc(result, (argc+1)*sizeof(char *));
    }
    
    result[argc] = NULL;
    return(result);
}

static void
free_args(char **args)
{
    char **p;

    for (p=args; *p; p++) {
      free(*p);
  }
	
    free(args);
}

/****************************************************************************/
/*                                                                          */
/*          Subroutines to handle each particular statement type:           */
/*                                                                          */
/****************************************************************************/

#define  NOBREAK   0
#define  BREAK     1
#define  EXIT      2

/*ARGSUSED*/
static int
exec_noop(Node *node)
{
    return(NOBREAK);
}

/*ARGSUSED*/
static int
exec_break(Node *node)
{
    return(BREAK);
}

/*ARGSUSED*/
static int
exec_exit(Node *node)
{
    return(EXIT);
}

static int
exec_set(Node *node)
{
    var_set_variable_then_free_value(node->d.nodes.first->d.string_constant,
				     eval_expr(node->d.nodes.second));

    return(NOBREAK);
}

static int
exec_execport(Node *node)
{
    string name = eval_expr(node->d.nodes.first);
    char **argv = eval_exprlist_to_args(node->d.nodes.second);

    create_subprocess_port(name, argv);

    free(name);
    free_args(argv);
    return(NOBREAK);
}

static int
exec_appendport(Node *node)
{
    string name, filename;

    name = eval_expr(node->d.nodes.first);
    filename = eval_expr(node->d.nodes.second);

    create_file_append_port(name, filename);

    free(name);
    free(filename);
    return(NOBREAK);
}

static int
exec_inputport(Node *node)
{
    string name, filename;

    name = eval_expr(node->d.nodes.first);
    filename = eval_expr(node->d.nodes.second);

    create_file_input_port(name, filename);

    free(name);
    free(filename);
    return(NOBREAK);
}

static int
exec_outputport(Node *node)
{
    string name, filename;

    name = eval_expr(node->d.nodes.first);
    filename = eval_expr(node->d.nodes.second);

    create_file_output_port(name, filename);

    free(name);
    free(filename);
    return(NOBREAK);
}

static int
exec_closeinput(Node *node)
{
    string name;

    name = eval_expr(node->d.nodes.first);
    close_port_input(name);

    free(name);
    return(NOBREAK);
}

static int
exec_closeoutput(Node *node)
{
    string name;

    name = eval_expr(node->d.nodes.first);
    close_port_output(name);

    free(name);
    return(NOBREAK);
}

static int
exec_closeport(Node *node)
{
    string name;

    name = eval_expr(node->d.nodes.first);
    close_port_input(name);
    close_port_output(name);

    free(name);
    return(NOBREAK);
}

static int
exec_put(Node *node)
{
    string name, temp;

    if (node->d.nodes.second)
      temp = eval_exprlist_to_string(node->d.nodes.second);
    else
      temp = string_Copy(buffer_to_string());

    if (node->d.nodes.first) {
	name = eval_expr(node->d.nodes.first);

	write_on_port(name, temp, strlen(temp));
	free(name);
    } else
      write_on_port(var_get_variable("output_driver"), temp, strlen(temp));

    free(temp);
    return(NOBREAK);
}

static int
exec_print(Node *node)
{
    string temp;

    temp = eval_exprlist_to_string(node->d.nodes.first);
    append_buffer(temp);
    free(temp);
    
    return(NOBREAK);
}

/*ARGSUSED*/
static int
exec_clearbuf(Node *node)
{
    clear_buffer();

    return(NOBREAK);
}

static int
exec_case(Node *node)
{
    string constant,temp;
    Node *match, *cond;
    int equal_p;

    constant = string_Downcase(eval_expr(node->d.nodes.first));
   
    for (match=node->d.nodes.second; match; match=match->next) {
	cond = match->d.nodes.first;
	if (!cond) {  /* default case */
	    free(constant);
	    return(exec_subtree(match->d.nodes.second));
	}
	for (; cond; cond=cond->next) {
	    temp = string_Downcase(eval_expr(cond));
	    equal_p = string_Eq(constant, temp);
	    free(temp);
	    if (equal_p) {
		free(constant);
		return(exec_subtree(match->d.nodes.second));
	    }
	}
    }

    free(constant);
    return(NOBREAK);
}

static int
exec_while(Node *node)
{
    int continue_code = NOBREAK;

    while (eval_bool_expr(node->d.nodes.first)) {
	continue_code = exec_subtree(node->d.nodes.second);
	if (continue_code != NOBREAK)
	  break;
    }

    if (continue_code == BREAK)
      continue_code = NOBREAK;

    return(continue_code);
}

static int
exec_if(Node *node)
{
    Node *conds;

    for (conds=node->d.nodes.first; conds; conds=conds->next)
      if (eval_bool_expr(conds->d.nodes.first))
	return(exec_subtree(conds->d.nodes.second));

    return(NOBREAK);
}

static int
exec_exec(Node *node)
{
    int pid;
    char **argv = eval_exprlist_to_args(node->d.nodes.first);

    pid = fork();
    if (pid == -1) {
	fprintf(stderr, "zwgc: error while attempting to fork: ");
	perror("");
    } else if (pid == 0) { /* in child */
	execvp(argv[0], argv);
	fprintf(stderr,"zwgc: unable to exec %s: ", argv[0]);
	perror("");
	_exit(errno);
    }
    
    free_args(argv);
    return(NOBREAK);
}

static struct _Opstuff {
    int (*exec)(Node *);
} const opstuff[] = {
    { exec_noop },                         /* string_constant */
    { exec_noop },                         /* varref */
    { exec_noop },                         /* varname */
    { exec_noop },                         /* not */
    { exec_noop },                         /* plus */
    { exec_noop },                         /* and */
    { exec_noop },                         /* or */
    { exec_noop },                         /* eq */
    { exec_noop },                         /* neq */
    { exec_noop },                         /* regeq */
    { exec_noop },                         /* regneq */
    { exec_noop },                         /* buffer */
    { exec_noop },                         /* substitute */
    { exec_noop },                         /* protect */
    { exec_noop },                         /* verbatim */
    { exec_noop },                         /* stylestrip */
    { exec_noop },                         /* getenv */
    { exec_noop },                         /* upcase */
    { exec_noop },                         /* downcase */
    { exec_noop },                         /* zvar */
    { exec_noop },                         /* get */
    { exec_noop },                         /* lany */
    { exec_noop },                         /* rany */
    { exec_noop },                         /* lbreak */
    { exec_noop },                         /* rbreak */
    { exec_noop },                         /* lspan */
    { exec_noop },                         /* rspan */

    { exec_noop },                          /* noop statement */
    { exec_set },
    { exec_fields },

    { exec_print },
    { exec_clearbuf },

    { exec_appendport },
    { exec_execport },
    { exec_inputport },
    { exec_outputport },
    { exec_put },
    { exec_closeinput },
    { exec_closeoutput },
    { exec_closeport },

    { exec_exec },

    { exec_if },
    { exec_case },
    { exec_while },
    { exec_break },
    { exec_exit },

    { exec_noop },                           /* if */
    { exec_noop },                           /* elseif */
    { exec_noop },                           /* else */
    { exec_noop },                           /* matchlist */
    { exec_noop },                           /* default */
};

static int
exec_subtree(Node *node)
{
    int retval = NOBREAK;
    
    for (; node; node=node->next) {
	retval = (opstuff[node->opcode].exec)(node);
	if (retval != NOBREAK)
	  return(retval);
    }

    return(NOBREAK);
}

/***************************************************************************/

static char *notice_fields;
static int notice_fields_length = 0;
static int number_of_fields = 0;

static int
exec_fields(Node *node)
{
    for (node=node->d.nodes.first; node; node=node->next) {
	var_set_variable_then_free_value(node->d.string_constant,
				 get_next_field(&notice_fields,
						&notice_fields_length));
	if (number_of_fields)
	  number_of_fields--;
    }
    
    var_set_variable_to_number("number_of_fields", number_of_fields);

    return(NOBREAK);
}

void
exec_process_packet(Node *program,
		    ZNotice_t *notice)
{
#ifdef CMU_ZWGCPLUS
    set_stored_notice(notice);
#endif

    notice_fields = notice->z_message;
    notice_fields_length = notice->z_message_len;

    var_set_number_variables_to_fields(notice_fields, notice_fields_length);

    number_of_fields = count_nulls(notice_fields, notice_fields_length)+1;
    /* workaround for bug in old zwrite */
    if (notice_fields[notice_fields_length-1] == '\0')
	number_of_fields--;
    var_set_variable_to_number("number_of_fields", number_of_fields);

    clear_buffer();
    (void)exec_subtree(program);

#ifdef CMU_ZWGCPLUS
    plus_queue_notice(notice);
    plus_window_deletions(notice); /* OOPS */
    set_stored_notice(NULL);
#endif
}