File: b_system.c

package info (click to toggle)
entity 0.7.2-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,352 kB
  • ctags: 5,272
  • sloc: ansic: 61,707; sh: 7,921; makefile: 732; perl: 399
file content (527 lines) | stat: -rw-r--r-- 12,617 bytes parent folder | download | duplicates (3)
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
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
/*
 * The builtin System object.
 * Copyright (c) 1998-1999 New Generation Software (NGS) Oy
 *
 * Author: Markku Rossi <mtr@ngs.fi>
 */

/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 */

/*
 * $Source: /home/cvs/entity/libentitynjs/b_system.c,v $
 * $Id: b_system.c,v 1.6 2000/07/25 05:25:58 imain Exp $
 */

/*
 * Static methods:
 *
 *   chdir (string) => boolean
 *   error ([ANY...]) => undefined
 *   exit (value)
 *   getcwd () => string
 *   getenv (string) => string / undefined
 *   popen (COMMAND, MODE) => file
 *   print ([ANY...]) => undefined
 *   sleep (int) => undefined
 *   strerror (errno) => string
 *   system (string) => integer
 *   usleep (int) => undefined
 *
 * Properties:			type		mutable
 *
 *   bits			integer
 *   canonicalHost		string
 *   canonicalHostCPU		string
 *   canonicalHostVendor	string
 *   canonicalHostOS		string
 *   errno			integer
 *   lineBreakSequence		string
 *   stderr			file
 *   stdin			file
 *   stdout			file
 */

#include "njs/internal.h"

/*
 * Types and definitions.
 */

#define INSECURE()		\
  do {				\
    if (secure_mode)		\
      goto insecure_feature;	\
  } while (0)

struct system_ctx_st
{
  JSSymbol s_chdir;
  JSSymbol s_error;
  JSSymbol s_exit;
  JSSymbol s_getcwd;
  JSSymbol s_getenv;
  JSSymbol s_popen;
  JSSymbol s_print;
  JSSymbol s_sleep;
  JSSymbol s_strerror;
  JSSymbol s_system;
  JSSymbol s_usleep;

  JSSymbol s_bits;
  JSSymbol s_canonicalHost;
  JSSymbol s_canonicalHostCPU;
  JSSymbol s_canonicalHostVendor;
  JSSymbol s_canonicalHostOS;
  JSSymbol s_errno;
  JSSymbol s_lineBreakSequence;
  JSSymbol s_stderr;
  JSSymbol s_stdin;
  JSSymbol s_stdout;

  /* System file handles. */
  JSNode pstderr;
  JSNode pstdin;
  JSNode pstdout;
};

typedef struct system_ctx_st SystemCtx;


/*
 * Static functions.
 */

/* Method proc */
static int
method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info,
	void *instance_context, JSSymbol method, JSNode *result_return,
	JSNode *args)
{
  SystemCtx *ctx = builtin_info->obj_context;
  int i;
  char *cp;
  int secure_mode = vm->security & JS_VM_SECURE_SYSTEM;

  /* The default result. */
  result_return->type = JS_UNDEFINED;

  if (method == ctx->s_chdir)
    {
      INSECURE ();

      if (args->u.vinteger != 1)
	goto argument_error;
      if (args[1].type != JS_STRING)
	goto argument_type_error;

      result_return->type= JS_BOOLEAN;

      cp = js_string_to_c_string (vm, &args[1]);
      result_return->u.vboolean = (chdir (cp) == 0);
      js_free (cp);
    }
  /* ********************************************************************** */
  else if (method == ctx->s_exit)
    {
      INSECURE ();

      if (args->u.vinteger != 1)
	goto argument_error;
      if (args[1].type != JS_INTEGER)
	goto argument_type_error;

      /* Exit. */
      exit (args[1].u.vinteger);
    }
  /* ********************************************************************** */
  else if (method == ctx->s_getcwd)
    {
      int size = 10 * 1024;

      INSECURE ();

      if (args->u.vinteger != 0)
	goto argument_error;

      cp = js_malloc (vm, size);
      if (!getcwd (cp, size))
	{
	  result_return->type = JS_BOOLEAN;
	  result_return->u.vboolean = 0;
	  js_free (cp);
	}
      else
	{
	  js_vm_make_string (vm, result_return, cp, strlen (cp));
	  js_free (cp);
	}
    }
  /* ********************************************************************** */
  else if (method == ctx->s_getenv)
    {
      char *val;

      if (args->u.vinteger != 1)
	goto argument_error;
      if (args[1].type != JS_STRING)
	goto argument_type_error;

      cp = js_string_to_c_string (vm, &args[1]);
      val = getenv (cp);
      js_free (cp);

      if (val)
	js_vm_make_string (vm, result_return, val, strlen (val));
    }
  /* ********************************************************************** */
  else if (method == ctx->s_popen)
    {
      char *cmd, *mode;
      FILE *fp;
      int readp = 0;

      INSECURE ();

      if (args->u.vinteger != 2)
	goto argument_error;
      if (args[1].type != JS_STRING || args[2].type != JS_STRING
	  || args[2].u.vstring->len > 10)
	goto argument_type_error;

      cmd = js_string_to_c_string (vm, &args[1]);
      mode = js_string_to_c_string (vm, &args[2]);

      for (i = 0; mode[i]; i++)
	if (mode[i] == 'r')
	  readp = 1;

      JS_VM_ALLOCATE_FD (vm, "System.popen()");
      fp = popen (cmd, mode);

      if (fp == NULL)
	JS_VM_FREE_FD (vm);
      else
	js_builtin_File_new (vm, result_return, cmd,
			     js_iostream_pipe (fp, readp), 0);

      js_free (cmd);
      js_free (mode);
    }
  /* ********************************************************************** */
  else if (method == ctx->s_print || method == ctx->s_error)
    {
      JSIOStream *stream;

      if (method == ctx->s_print)
	stream = vm->s_stdout;
      else
	stream = vm->s_stderr;

      for (i = 1; i <= args->u.vinteger; i++)
	{
	  JSNode result;

	  js_vm_to_string (vm, &args[i], &result);
	  js_iostream_write (stream, result.u.vstring->data,
			     result.u.vstring->len);
	}
    }
  /* ********************************************************************** */
  else if (method == ctx->s_sleep)
    {
      if (args->u.vinteger != 1)
	goto argument_error;
      if (args[1].type != JS_INTEGER)
	goto argument_type_error;

      sleep (args[1].u.vinteger);
    }
  /* ********************************************************************** */
  else if (method == ctx->s_strerror)
    {
      if (args->u.vinteger != 1)
	goto argument_error;
      if (args[1].type != JS_INTEGER)
	goto argument_type_error;

      cp = strerror (args[1].u.vinteger);
      js_vm_make_string (vm, result_return, cp, strlen (cp));
    }
  /* ********************************************************************** */
  else if (method == ctx->s_system)
    {
      INSECURE ();

      if (args->u.vinteger != 1)
	goto argument_error;
      if (args[1].type != JS_STRING)
	goto argument_type_error;

      result_return->type = JS_INTEGER;

      cp = js_string_to_c_string (vm, &args[1]);
      result_return->u.vinteger = system (cp);
      js_free (cp);
    }
  /* ********************************************************************** */
  else if (method == vm->syms.s_toString)
    {
      if (args->u.vinteger != 0)
	goto argument_error;

      js_vm_make_static_string (vm, result_return, "System", 6);
    }
#ifdef HAVE_USLEEP
  /* ********************************************************************** */
  else if (method == ctx->s_usleep)
    {
      if (args->u.vinteger != 1)
	goto argument_error;
      if (args[1].type != JS_INTEGER)
	goto argument_type_error;
#ifdef HAVE_USLEEP
      usleep (args[1].u.vinteger);
#endif /* HAVE_USLEEP */

    }
  /* ********************************************************************** */
#endif /* HAVE_USLEEP */
  else
    return JS_PROPERTY_UNKNOWN;

  return JS_PROPERTY_FOUND;


  /*
   * Error handling.
   */

 argument_error:
  js_vm_set_err (vm, "System.%s(): illegal amout of arguments",
	   js_vm_symname (vm, method));
  js_vm_error (vm);

 argument_type_error:
  js_vm_set_err (vm, "System.%s(): illegal argument",
	   js_vm_symname (vm, method));
  js_vm_error (vm);

 insecure_feature:
  js_vm_set_err (vm, "System.%s(): not allowed in secure mode",
	   js_vm_symname (vm, method));
  js_vm_error (vm);

  /* NOTREACHED */
  return 0;
}

/* Property proc. */
static int
property (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info,
	  void *instance_context, JSSymbol property, int set, JSNode *node)
{
  SystemCtx *ctx = builtin_info->obj_context;

  if (property == ctx->s_bits)
    {
      if (set)
	goto immutable;

      node->type = JS_INTEGER;
#if SIZEOF_INT == 2
      node->u.vinteger = 16;
#else /* not SIZEOF_INT == 2 */

#if SIZEOF_LONG == 4
      node->u.vinteger = 32;
#else /* not SIZEOF_LONG == 4 */

#if SIZEOF_LONG == 8
      node->u.vinteger = 64;
#else /* not SIZEOF_LONG == 8 */

      /* Do not know. */
      node->u.vinteger = 0;

#endif /* not SIZEOF_LONG == 8 */
#endif /* not SIZEOF_LONG == 4 */
#endif /* not SIZEOF_INT == 2 */
    }
  else if (property == ctx->s_canonicalHost)
    {
      if (set)
	goto immutable;

      js_vm_make_static_string (vm, node, CANONICAL_HOST,
				strlen (CANONICAL_HOST));
    }
  else if (property == ctx->s_canonicalHostCPU)
    {
      if (set)
	goto immutable;

      js_vm_make_static_string (vm, node, CANONICAL_HOST_CPU,
				strlen (CANONICAL_HOST_CPU));
    }
  else if (property == ctx->s_canonicalHostVendor)
    {
      if (set)
	goto immutable;

      js_vm_make_static_string (vm, node, CANONICAL_HOST_VENDOR,
				strlen (CANONICAL_HOST_VENDOR));
    }
  else if (property == ctx->s_canonicalHostOS)
    {
      if (set)
	goto immutable;

      js_vm_make_static_string (vm, node, CANONICAL_HOST_OS,
				strlen (CANONICAL_HOST_OS));
    }
  else if (property == ctx->s_errno)
    {
      if (set)
	goto immutable;

      node->type = JS_INTEGER;
      node->u.vinteger = errno;
    }
  else if (property == ctx->s_lineBreakSequence)
    {
      if (set)
	goto immutable;

      js_vm_make_static_string (vm, node, JS_HOST_LINE_BREAK,
				JS_HOST_LINE_BREAK_LEN);
    }
  else if (property == ctx->s_stderr)
    {
      if (set)
	goto immutable;

      JS_COPY (node, &ctx->pstderr);
    }
  else if (property == ctx->s_stdin)
    {
      if (set)
	goto immutable;

      JS_COPY (node, &ctx->pstdin);
    }
  else if (property == ctx->s_stdout)
    {
      if (set)
	goto immutable;

      JS_COPY (node, &ctx->pstdout);
    }

  else
    {
      if (!set)
	node->type = JS_UNDEFINED;

      return JS_PROPERTY_UNKNOWN;
    }

  return JS_PROPERTY_FOUND;


  /*
   * Error handling.
   */

 immutable:
  js_vm_set_err (vm, "System.%s: immutable property",
	   js_vm_symname (vm, property));
  js_vm_error (vm);

  /* NOTREACHED. */
  return 0;
}

/* Mark proc. */
static void
mark (JSBuiltinInfo *builtin_info, void *instance_context)
{
  SystemCtx *ctx = builtin_info->obj_context;

  js_vm_mark (&ctx->pstderr);
  js_vm_mark (&ctx->pstdin);
  js_vm_mark (&ctx->pstdout);
}


/*
 * Global functions.
 */

void
js_builtin_System (JSVirtualMachine *vm)
{
  JSNode *n;
  JSBuiltinInfo *info;
  SystemCtx *ctx;

  ctx = js_calloc (vm, 1, sizeof (*ctx));

  ctx->s_chdir			= js_vm_intern (vm, "chdir");
  ctx->s_error			= js_vm_intern (vm, "error");
  ctx->s_exit			= js_vm_intern (vm, "exit");
  ctx->s_getcwd			= js_vm_intern (vm, "getcwd");
  ctx->s_getenv			= js_vm_intern (vm, "getenv");
  ctx->s_popen			= js_vm_intern (vm, "popen");
  ctx->s_print			= js_vm_intern (vm, "print");
  ctx->s_sleep			= js_vm_intern (vm, "sleep");
  ctx->s_strerror		= js_vm_intern (vm, "strerror");
  ctx->s_system			= js_vm_intern (vm, "system");
  ctx->s_usleep			= js_vm_intern (vm, "usleep");

  ctx->s_bits			= js_vm_intern (vm, "bits");
  ctx->s_canonicalHost		= js_vm_intern (vm, "canonicalHost");
  ctx->s_canonicalHostCPU	= js_vm_intern (vm, "canonicalHostCPU");
  ctx->s_canonicalHostVendor	= js_vm_intern (vm, "canonicalHostVendor");
  ctx->s_canonicalHostOS	= js_vm_intern (vm, "canonicalHostOS");
  ctx->s_errno			= js_vm_intern (vm, "errno");
  ctx->s_lineBreakSequence	= js_vm_intern (vm, "lineBreakSequence");
  ctx->s_stderr			= js_vm_intern (vm, "stderr");
  ctx->s_stdin			= js_vm_intern (vm, "stdin");
  ctx->s_stdout			= js_vm_intern (vm, "stdout");

  /* Object information. */

  info = js_vm_builtin_info_create (vm);

  info->method_proc 		= method;
  info->property_proc		= property;
  info->mark_proc		= mark;
  info->obj_context 		= ctx;
  info->obj_context_delete	= js_free;

  /* Define it. */
  n = &vm->globals[js_vm_intern (vm, "System")];
  js_vm_builtin_create (vm, n, info, NULL);

  /* Enter system properties. */
  js_builtin_File_new (vm, &ctx->pstderr, "stdout", vm->s_stderr, 1);
  js_builtin_File_new (vm, &ctx->pstdin, "stdin", vm->s_stdin, 1);
  js_builtin_File_new (vm, &ctx->pstdout, "stdout", vm->s_stdout, 1);
}