⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 一个类似windows
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * The JS shell
 * Copyright (c) 1998 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: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/ksrc/main.c,v $
 * $Id: main.c 21681 2006-04-21 15:00:24Z peterw $
 */

#if HAVE_CONFIG_H
#include "jsconfig.h"
#endif

#include <stdio.h>
#include <assert.h>

#if HAVE_STDC_HEADERS
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#else /* not HAVE_STDC_HEADERS */

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif

#if HAVE_STRING_H
#include <string.h>
#endif

#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#endif /* not HAVE_STDC_HEADERS */

#include "js.h"
#include "getopt.h"

/*
 * Global variables.
 */

char *program;

/* Options. */

/*
 * -a, --annotate-assembler
 *
 * Annotate generated assembler listing with the original source code.
 */

int annotate_assembler = 0;

/*
 * -c, --compile
 *
 * Compile all given JavaScript files to byte-code and save the result
 * to file.
 */

int compile = 0;

/*
 * -d METHOD, --dispatch=METHOD
 *
 * Use byte-code instruction dispatch method METHOD.  Possible values are
 * `switch', `switch-basic' and `jumps'.
 */

JSVMDispatchMethod dispatch_method = JS_VM_DISPATCH_JUMPS;

/*
 * -e CODE, --eval=CODE
 *
 * Evaluate JavaScript code CODE.
 */

/*
 * -E, --events
 *
 * Print virtual machine events to the standard error.
 */
int events = 0;

/*
 * -f, --file
 *
 * Use the next argument as the main source file and pass all
 * remaining arguments to it through the ARGS array.
 */

/*
 * -g, --debug
 *
 * Generate debugging information to the generated byte-code file.
 */
int generate_debug_info = 0;

/*
 * -h, --help
 *
 * Print short help and exit successfully.
 */

/*
 * -l, --load
 *
 * Treat all following arguments, up to option -f / --file, as
 * byte-code or JavaScript files and execute them.  When option -f, --file
 * is encountered, the next argument is the actual main source file that
 * is executed with the remaining arguments.
 */

/*
 * -N, --no-compiler
 *
 * Do not define compiler in the interpreter.  Options makes the
 * interpreter a pure virtual machine that can't compile any JavaScript
 * code.
 */

int no_compiler = 0;

/*
 * -O [LEVEL], --optimize[=LEVEL]
 *
 * Optimize at level LEVEL.  The default level for batch-compile is 1.
 * Value 0 disable optimization.
 */

int optimize = 1;

/*
 * -r OPTION, --secure=OPTIONS
 *
 * Turn on security option OPTION.
 */

int secure_builtin_file = 0;
int secure_builtin_system = 0;

/*
 * -s SIZE, --stack-size=SIZE
 *
 * Set the virtual machine stack size to SIZE.
 */
unsigned int stack_size = 2048;

/*
 * -S, --no-assemble
 *
 * Compile al given JavaScript files to JavaScript assembler and save the
 * result to file.
 */

int no_assemble = 0;

/*
 * -t, --stacktrace
 *
 * Print stack trace on error.
 */

int stacktrace_on_error = 0;

/*
 * -v, --verbose
 *
 * Tell more about what we do.
 */

unsigned int verbose = 0;

/*
 * -V, --version
 *
 * Print version information and exit successfully.
 */

/*
 * -W OPTION, --compiler-option=OPTION
 *
 * Set compiler option OPTION.
 */

int warn_deprecated = 0;
int warn_unused_argument = 0;
int warn_unused_variable = 1;
int warn_undef = 1;
int warn_shadow = 1;
int warn_with_clobber = 1;
int warn_missing_semicolon = 0;
int warn_strict_ecma = 0;

/*
 * -x, --executable
 *
 * Generate executable byte-code files.
 */
int generate_executable_bc_files = 0;


/*
 * Static variables.
 */

static struct option long_options[] =
{
  {"annotate-assembler",	no_argument,		0, 'a'},
  {"compile",			no_argument,		0, 'c'},
  {"dispatch",			required_argument,	0, 'd'},
  {"eval",			required_argument,	0, 'e'},
  {"events",			no_argument,		0, 'E'},
  {"file",			no_argument,		0, 'f'},
  {"debug",			no_argument,		0, 'g'},
  {"help",			no_argument,		0, 'h'},
  {"load",			no_argument,		0, 'l'},
  {"no-compiler",		no_argument,		0, 'N'},
  {"optimize",			optional_argument,	0, 'O'},
  {"secure",			required_argument,	0, 'r'},
  {"stack-size",		required_argument,	0, 's'},
  {"no-assemble",		no_argument,		0, 'S'},
  {"stacktrace",		no_argument,		0, 't'},
  {"verbose",			no_argument,		0, 'v'},
  {"version",			no_argument,		0, 'V'},
  {"compiler-option",		required_argument,	0, 'W'},
  {"executable",		no_argument,		0, 'x'},

  {NULL, 0, 0, 0},
};

/* Compiler options. */

/* Flags for options. */
#define JSC_RUNTIME	0x01
#define JSC_WALL	0x02
#define JSC_PEDANTIC	0x04
#define JSC_LINT	0x08

static struct
{
  char *name;
  int *option;
  unsigned int flags;
} compiler_options[] =
{
  {"deprecated",		&warn_deprecated,		JSC_WALL},
  {"unused-argument",		&warn_unused_argument,		JSC_WALL},
  {"unused-variable",		&warn_unused_variable,		JSC_WALL},
  {"undefined",			&warn_undef,			JSC_RUNTIME},
  {"shadow",			&warn_shadow,			JSC_WALL},
  {"with-clobber",		&warn_with_clobber,		JSC_WALL},
  {"missing-semicolon",		&warn_missing_semicolon,	JSC_PEDANTIC},
  {"strict-ecma",		&warn_strict_ecma,		JSC_LINT},

  {NULL, NULL, 0},
};


/*
 * Prototypes for static functions.
 */

static void handle_compiler_option (char *name);

static JSInterpPtr create_interp (void);

static void usage (void);
static void version (void);

/*
 * Global functions.
 */

int
main (int argc, char *argv[])
{
  JSInterpPtr interp = NULL;
  char *cp;
  int do_load = 0;

  /* Get program's name. */
  program = strrchr (argv[0], '/');
  if (program == NULL)
    program = argv[0];
  else
    program++;

  /* Make getopt_long() to use our modified program name. */
  argv[0] = program;

  /* Parse arguments. */
  while (1)
    {
      int c;
      int option_index = 0;

      c = getopt_long (argc, argv, "acd:e:EfghlNO::r:s:StvVW:x",
		       long_options, &option_index);
      if (c == EOF)
	break;

      switch (c)
	{
	case 'a':		/* --annotate-assembler */
	  annotate_assembler = 1;
	  break;

	case 'c':		/* --compile */
	  compile = 1;
	  break;

	case 'd':		/* --dispatch */
	  if (strcmp (optarg, "switch-basic") == 0)
	    dispatch_method = JS_VM_DISPATCH_SWITCH_BASIC;
	  else if (strcmp (optarg, "switch") == 0)
	    dispatch_method = JS_VM_DISPATCH_SWITCH;
	  else if (strcmp (optarg, "jumps") == 0)
	    dispatch_method = JS_VM_DISPATCH_JUMPS;
	  else
	    {
	      fprintf (stderr, "%s: illegal dispatch method `%s'\n",
		       program, optarg);
	      exit (1);
	    }
	  break;

	case 'e':		/* --eval */
	  if (interp == NULL)
	    interp = create_interp ();

	  if (!js_eval (interp, optarg))
	    {
	      fprintf (stderr, "%s: eval failed: %s\n", program,
		       js_error_message (interp));
	      exit (1);
	    }
	  break;

	case 'E':		/* --events */
	  events = 1;
	  break;

	case 'f':		/* --file */
	  if (optind >= argc)
	    {
	    no_argument_for_file:
	      fprintf (stderr, "%s: no arguments after option --file\n",
		       program);
	      exit (1);
	    }
	  goto arguments_done;
	  break;

	case 'g':		/* --debug */
	  generate_debug_info = 1;
	  break;

	case 'h':		/* --help */
	  usage ();
	  exit (0);
	  break;

	case 'l':		/* --load */
	  do_load = 1;
	  goto arguments_done;
	  break;

	case 'N':		/* --no-compiler */
	  no_compiler = 1;
	  break;

	case 'O':		/* --optimize */
	  if (optarg)
	    optimize = atoi (optarg);
	  break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -