예제 #1
0
파일: driver.c 프로젝트: Synposis/mruby
int
main(int argc, char **argv)
{
  mrb_state *mrb;
  struct RClass *krn;
  int ret;

  print_hint();

  /* new interpreter instance */
  mrb = mrb_open();
  if (mrb == NULL) {
    fprintf(stderr, "Invalid mrb_state, exiting test driver");
    return EXIT_FAILURE;
  }

  if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v') {
    printf("verbose mode: enable\n\n");
    mrb_gv_set(mrb, mrb_intern_lit(mrb, "$mrbtest_verbose"), mrb_true_value());
  }

  krn = mrb->kernel_module;
  mrb_define_method(mrb, krn, "__t_printstr__", mrb_t_printstr, MRB_ARGS_REQ(1));

  mrb_init_mrbtest(mrb);
  ret = eval_test(mrb);
  mrb_close(mrb);

  return ret;
}
예제 #2
0
/* ARGSUSED */
void
lt_display_error(const char *fmt, ...)
{
	va_list vl;
	char tmp[81];
	int l;

	va_start(vl, fmt);
	(void) vsnprintf(tmp, sizeof (tmp), fmt, vl);
	va_end(vl);

	l = strlen(tmp);

	while (l > 0 && (tmp[l - 1] == '\n' || tmp[l - 1] == '\r')) {
		tmp[l - 1] = '\0';
		--l;
	}

	if (!display_initialized) {
		(void) fprintf(stderr, "%s\n", tmp);
	} else if (!show_help) {
		print_hint(tmp);
	}

}
예제 #3
0
int
main(int argc, char **argv)
{
  mrb_state *mrb;
  int ret;

  print_hint();

  /* new interpreter instance */
  mrb = mrb_open();
  if (mrb == NULL) {
    fprintf(stderr, "Invalid mrb_state, exiting test driver");
    return EXIT_FAILURE;
  }

  if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v') {
    printf("verbose mode: enable\n\n");
    mrb_gv_set(mrb, mrb->intern_cstr("$mrbtest_verbose"), mrb_true_value());
  }

  mrb_init_mrbtest(mrb);
  ret = eval_test(mrb);
  mrb_close(mrb);

  return ret;
}
예제 #4
0
static void *the_shell_thread_func(void *arg)
{
    int ret;
    char clean_cmd_line[512];
    cur_histotry_cmds_num = 0;
    shell_buf_cur_len=0;

    print_intro();

    if (login_auth()) goto EXIT;
    
    redirect_io(fd_pty_slave);
    print_hint();

    while (!shell_thread_should_exit)
    {
        ret=read_input(0);
        if (ret<0) goto EXIT;
        if (ret==1)
        {
            if (shell_buf_cur_len==0)
                goto CMD_OVER;
            
            update_histotry_cmds(shell_buf);
            shell_buf_cur_len=0;
            str_trim_all(clean_cmd_line, shell_buf);
            //printf_to_fd(ori_std_output, "== ret=%s\n", shell_buf);
            if (strlen(clean_cmd_line)==0)
                goto CMD_OVER;

            if (strcmp(clean_cmd_line,"quit")==0)
                goto EXIT;

            proccess_cmd(clean_cmd_line);
            printf_to_fd(1, "\n");
CMD_OVER:
            print_hint();
        }
    }

EXIT:
    shell_quit_occurred = 1;
    return NULL;
}
예제 #5
0
파일: ias.cpp 프로젝트: tapickell/ias
void
mrb_setup_arduino() {
  mrb = mrb_open();
  if (mrb == NULL)
    Serial.println("Invalid mrb interpreter, IAS won't work!");
  else
    print_hint();

  cxt = mrbc_context_new(mrb);
  cxt->capture_errors = 1;
  ai = mrb_gc_arena_save(mrb);

  krn = mrb->kernel_module;
  mrb_define_method(mrb, krn, "p", my_p, MRB_ARGS_REQ(1));
  mrb_define_method(mrb, krn, "print", my_print, MRB_ARGS_REQ(1));
  mrb_define_method(mrb, krn, "puts", my_puts, MRB_ARGS_REQ(1));

  led = mrb_define_class(mrb, "Led", mrb->object_class);
  mrb_define_class_method(mrb, led, "high!", mrb_led_high, MRB_ARGS_NONE());
  mrb_define_class_method(mrb, led, "low!", mrb_led_low, MRB_ARGS_NONE());
  mrb_define_class_method(mrb, led, "blink", mrb_led_blink, MRB_ARGS_NONE());
}
예제 #6
0
파일: mirb.c 프로젝트: rubiojr/mruby
int
main(int argc, char **argv)
{
  char ruby_code[1024] = { 0 };
  char last_code_line[1024] = { 0 };
#ifndef ENABLE_READLINE
  int last_char;
  int char_index;
#else
  char *home = NULL;
#endif
  mrbc_context *cxt;
  struct mrb_parser_state *parser;
  mrb_state *mrb;
  mrb_value result;
  struct _args args;
  int n;
  mrb_bool code_block_open = FALSE;
  mrb_value MIRB_BIN;
  int ai;
  unsigned int stack_keep = 0;

  /* new interpreter instance */
  mrb = mrb_open();
  if (mrb == NULL) {
    fputs("Invalid mrb interpreter, exiting mirb\n", stderr);
    return EXIT_FAILURE;
  }
  mrb_define_global_const(mrb, "ARGV", mrb_ary_new_capa(mrb, 0));

  n = parse_args(mrb, argc, argv, &args);
  if (n == EXIT_FAILURE) {
    cleanup(mrb, &args);
    usage(argv[0]);
    return n;
  }

  print_hint();

  cxt = mrbc_context_new(mrb);
  cxt->capture_errors = 1;
  cxt->lineno = 1;
  mrbc_filename(mrb, cxt, "(mirb)");
  if (args.verbose) cxt->dump_result = 1;

  MIRB_BIN= mrb_str_new(mrb, argv[0], strlen(argv[0]));
  mrb_define_global_const(mrb, "MIRB_BIN", MIRB_BIN);

#ifdef ENABLE_REQUIRE
  mrb_value LOAD_PATH = mrb_gv_get(mrb, mrb_intern(mrb, "$:"));

  if (mrb_str_cmp(mrb, MIRB_BIN, mrb_str_new2(mrb, "mirb")) != 0) {
    int len = strrchr(RSTRING_PTR(MIRB_BIN), '/') - RSTRING_PTR(MIRB_BIN);
    mrb_value extdir = mrb_str_substr(mrb, mrb_str_dup(mrb, MIRB_BIN), 0, len);
    mrb_str_cat2(mrb, extdir, "/../ext");

    if (mrb_obj_eq(mrb, mrb_file_exist(mrb, extdir), mrb_true_value())) {
      mrb_ary_push(mrb, LOAD_PATH, extdir);
    }
  }
#endif /* ENABLE_REQUIRE */


  ai = mrb_gc_arena_save(mrb);

#ifdef ENABLE_READLINE
  MIRB_USING_HISTORY();
  home = getenv("HOME");
#ifdef _WIN32
  if (!home)
    home = getenv("USERPROFILE");
#endif
  if (home) {
    strcpy(history_path, home);
    strcat(history_path, "/");
    strcat(history_path, history_file_name);
    MIRB_READ_HISTORY(history_path);
  }
#endif


  while (TRUE) {
#ifndef ENABLE_READLINE
    print_cmdline(code_block_open);

    char_index = 0;
    while ((last_char = getchar()) != '\n') {
      if (last_char == EOF) break;
      last_code_line[char_index++] = last_char;
    }
    if (last_char == EOF) {
      fputs("\n", stdout);
      break;
    }

    last_code_line[char_index] = '\0';
#else
    char* line = MIRB_READLINE(code_block_open ? "* " : "> ");
    if (line == NULL) {
      printf("\n");
      break;
    }
    strncpy(last_code_line, line, sizeof(last_code_line)-1);
    MIRB_ADD_HISTORY(line);
    free(line);
#endif

    if (code_block_open) {
        strcat(ruby_code, "\n");
        strcat(ruby_code, last_code_line);
    }
    else {
      if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) {
        break;
      }
      strcpy(ruby_code, last_code_line);
    }

    /* parse code */
    parser = mrb_parser_new(mrb);
    parser->s = ruby_code;
    parser->send = ruby_code + strlen(ruby_code);
    parser->lineno = cxt->lineno;
    mrb_parser_parse(parser, cxt);
    code_block_open = is_code_block_open(parser);

    if (code_block_open) {
      /* no evaluation of code */
    }
    else {
      if (0 < parser->nerr) {
        /* syntax error */
        printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message);
      }
      else {
        /* generate bytecode */
        struct RProc *proc = mrb_generate_code(mrb, parser);

        if (args.verbose) {
          mrb_codedump_all(mrb, proc);
        }
        /* pass a proc for evaulation */
        /* evaluate the bytecode */
        result = mrb_context_run(mrb,
            proc,
            mrb_top_self(mrb),
            stack_keep);
        stack_keep = proc->body.irep->nlocals;
        /* did an exception occur? */
        if (mrb->exc) {
          p(mrb, mrb_obj_value(mrb->exc), 0);
          mrb->exc = 0;
        }
        else {
          /* no */
          if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))){
            result = mrb_any_to_s(mrb,result);
          }
          p(mrb, result, 1);
        }
      }
      ruby_code[0] = '\0';
      last_code_line[0] = '\0';
      mrb_gc_arena_restore(mrb, ai);
    }
    mrb_parser_free(parser);
    cxt->lineno++;
  }
  mrbc_context_free(mrb, cxt);
  mrb_close(mrb);

#ifdef ENABLE_READLINE
  MIRB_WRITE_HISTORY(history_path);
#endif

  return 0;
}
예제 #7
0
파일: mirb.c 프로젝트: Zyxwvu/mruby
int
main(void)
{
  char last_char, ruby_code[1024], last_code_line[1024];
  int char_index;
  struct mrb_parser_state *parser;
  mrb_state *mrb_interpreter;
  mrb_value mrb_return_value;
  int byte_code;
  int code_block_open = FALSE;

  print_hint();

  /* new interpreter instance */ 
  mrb_interpreter = mrb_open();
  /* new parser instance */
  parser = mrb_parser_new(mrb_interpreter);
  memset(ruby_code, 0, sizeof(*ruby_code));
  memset(last_code_line, 0, sizeof(*last_code_line));

  while (TRUE) {
    print_cmdline(code_block_open);

    char_index = 0;
    while ((last_char = getchar()) != '\n') {
      if (last_char == EOF) break;
      last_code_line[char_index++] = last_char;
    }
    if (last_char == EOF) {
      printf("\n");
      break;
    }

    last_code_line[char_index] = '\0';

    if ((strcmp(last_code_line, "quit") == 0) ||
        (strcmp(last_code_line, "exit") == 0)) {
      if (code_block_open) {
        /* cancel the current block and reset */
        code_block_open = FALSE;
        memset(ruby_code, 0, sizeof(*ruby_code));
        memset(last_code_line, 0, sizeof(*last_code_line));
        continue;
      }
      else {
        /* quit the program */
        break;
      }
    }
    else {
      if (code_block_open) {
	strcat(ruby_code, "\n");
        strcat(ruby_code, last_code_line);
      }
      else {
        memset(ruby_code, 0, sizeof(*ruby_code));
        strcat(ruby_code, last_code_line);
      }

      /* parse code */
      parser->s = ruby_code;
      parser->send = ruby_code + strlen(ruby_code);
      parser->capture_errors = 1;
      parser->lineno = 1;
      mrb_parser_parse(parser);
      code_block_open = is_code_block_open(parser); 

      if (code_block_open) {
        /* no evaluation of code */
      }
      else {
        if (0 < parser->nerr) {
          /* syntax error */
          printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message);
        }
	else {
          /* generate bytecode */
          byte_code = mrb_generate_code(mrb_interpreter, parser->tree);

          /* evaluate the bytecode */
          mrb_return_value = mrb_run(mrb_interpreter,
            /* pass a proc for evaulation */
            mrb_proc_new(mrb_interpreter, mrb_interpreter->irep[byte_code]),
            mrb_top_self(mrb_interpreter));
          /* did an exception occur? */
          if (mrb_interpreter->exc) {
            mrb_p(mrb_interpreter, mrb_obj_value(mrb_interpreter->exc));
            mrb_interpreter->exc = 0;
          }
	  else {
            /* no */
            printf(" => ");
            mrb_p(mrb_interpreter, mrb_return_value);
          }
        }

        memset(ruby_code, 0, sizeof(*ruby_code));
        memset(ruby_code, 0, sizeof(*last_code_line));
      }
    }
  }
  mrb_close(mrb_interpreter);

  return 0;
}
예제 #8
0
파일: mirb.c 프로젝트: Bovi-Li/mruby
int
main(int argc, char **argv)
{
  char ruby_code[1024] = { 0 };
  char last_code_line[1024] = { 0 };
#ifndef ENABLE_READLINE
  int last_char;
  int char_index;
#else
  char *home = NULL;
#endif
  mrbc_context *cxt;
  struct mrb_parser_state *parser;
  mrb_state *mrb;
  mrb_value result;
  struct _args args;
  int n;
  mrb_bool code_block_open = FALSE;
  int ai;
  mrb_bool first_command = TRUE;
  unsigned int nregs;

  /* new interpreter instance */
  mrb = mrb_open();
  if (mrb == NULL) {
    fputs("Invalid mrb interpreter, exiting mirb\n", stderr);
    return EXIT_FAILURE;
  }
  mrb_define_global_const(mrb, "ARGV", mrb_ary_new_capa(mrb, 0));

  n = parse_args(mrb, argc, argv, &args);
  if (n == EXIT_FAILURE) {
    cleanup(mrb, &args);
    usage(argv[0]);
    return n;
  }

  print_hint();

  cxt = mrbc_context_new(mrb);
  cxt->capture_errors = 1;
  cxt->lineno = 1;
  mrbc_filename(mrb, cxt, "(mirb)");
  if (args.verbose) cxt->dump_result = 1;

  ai = mrb_gc_arena_save(mrb);

#ifdef ENABLE_READLINE
  using_history();
  home = getenv("HOME");
#ifdef _WIN32
  if (!home)
    home = getenv("USERPROFILE");
#endif
  if (home) {
    strcpy(history_path, home);
    strcat(history_path, "/");
    strcat(history_path, history_file_name);
    read_history(history_path);
  }
#endif


  while (TRUE) {
#ifndef ENABLE_READLINE
    print_cmdline(code_block_open);

    char_index = 0;
    while ((last_char = getchar()) != '\n') {
      if (last_char == EOF) break;
      last_code_line[char_index++] = last_char;
    }
    if (last_char == EOF) {
      fputs("\n", stdout);
      break;
    }

    last_code_line[char_index] = '\0';
#else
    char* line = readline(code_block_open ? "* " : "> ");
    if (line == NULL) {
      printf("\n");
      break;
    }
    strncpy(last_code_line, line, sizeof(last_code_line)-1);
    add_history(line);
    free(line);
#endif

    if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) {
      if (!code_block_open) {
        break;
      }
      else{
        /* count the quit/exit commands as strings if in a quote block */
        strcat(ruby_code, "\n");
        strcat(ruby_code, last_code_line);
      }
    }
    else {
      if (code_block_open) {
        strcat(ruby_code, "\n");
        strcat(ruby_code, last_code_line);
      }
      else {
        strcpy(ruby_code, last_code_line);
      }
    }

    /* parse code */
    parser = mrb_parser_new(mrb);
    parser->s = ruby_code;
    parser->send = ruby_code + strlen(ruby_code);
    parser->lineno = cxt->lineno;
    mrb_parser_parse(parser, cxt);
    code_block_open = is_code_block_open(parser);

    if (code_block_open) {
      /* no evaluation of code */
    }
    else {
      if (0 < parser->nerr) {
        /* syntax error */
        printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message);
      }
      else {
        /* generate bytecode */
        struct RProc *proc = mrb_generate_code(mrb, parser);

        if (args.verbose) {
          mrb_codedump_all(mrb, proc);
        }
        /* pass a proc for evaulation */
        nregs = first_command ? 0: proc->body.irep->nregs;
        /* evaluate the bytecode */
        result = mrb_context_run(mrb,
            proc,
            mrb_top_self(mrb),
            nregs);
        /* did an exception occur? */
        if (mrb->exc) {
          p(mrb, mrb_obj_value(mrb->exc), 0);
          mrb->exc = 0;
        }
        else {
          /* no */
          if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))){
            result = mrb_any_to_s(mrb,result);
          }
          p(mrb, result, 1);
        }
      }
      ruby_code[0] = '\0';
      last_code_line[0] = '\0';
      mrb_gc_arena_restore(mrb, ai);
    }
    mrb_parser_free(parser);
    cxt->lineno++;
    first_command = FALSE;
  }
  mrbc_context_free(mrb, cxt);
  mrb_close(mrb);

#ifdef ENABLE_READLINE
  write_history(history_path);
#endif

  return 0;
}
예제 #9
0
파일: umain.c 프로젝트: jdigittl/crepes
int main (int argc, char *argv[])
{
  FILE *fopen(), *fpin;
  char *infile, *outbase, *outext, *filter;
  char *ppa, *ppb;
  char **pending_name;
  int *pending_number;
  int i, r;
  int verbose_flag;
  int begin_frame,max_frames;
  int pp_flag;
  int outtype;

  infile = NULL;
  outbase = NULL;
  outext = NULL;
  verbose_flag = 0;
  filter = NULL;
  begin_frame=1;
  max_frames=-1;
  outtype = PPM_RAW;

  /* scan arguments */

  pp_flag = 0;
  pending_number = NULL;
  pending_name = NULL;
  ppa = ppb = NULL;

  if (argc == 1)
    { print_usage(); exit(1); }

  for (i=1; i < argc; i++)
    {
      ppa=argv[i];
      if (pp_flag == 1)
        { SCANINT(ppa, pending_number); pp_flag = 0; }
      else if (pp_flag == 2)
        { *pending_name = ppa; pp_flag = 0; }
      else if (*ppa == '-')
	{
	  ppb = (ppa++);
	  switch (*ppa)
	    {
	    case 'b':   GETNUMBER(begin_frame); break;
	    case 'n':   GETNUMBER(max_frames); break;
	    case 'a':   outtype = PPM_ASCII; break;
	    case 'm':   outtype = FBM_MAPPED; break;
	    case 'f':   GETNAME(filter); break;
	    case 'v':   verbose_flag = 1; break;
	    case 'h':   print_usage(); print_help(); exit(1);
	    default:
	      fprintf(stderr,"Illegal option '%c' in argument %d\n",
		      *ppa,i);
	      print_hint();
	      exit(1);
	    }
	}
      else  if (infile == NULL)
	{
	  infile=argv[i];
	}
      else if (outbase == NULL)
	{
	  outbase=argv[i];
	}
      else if (outext == NULL)
	{
	  outext=argv[i];
	}
      else
	{
	  fprintf(stderr,"Too many parameters specified\n");
	  print_hint();
	  exit(1);
	}
    }

  if (pp_flag == 1)
    {
      fprintf(stderr,"Missing number behind option '%c'\n",*ppb);
      print_hint();
      exit(1);
    }

  if (outbase == NULL)
    {
      print_usage();
      exit(1);
    }

  /* fprintf(stderr,"Open Input File\n"); */
  if ((fpin = fopen(infile, "rb")) == NULL)
    {
      fprintf(stderr,"Error opening input file '%s'\n",infile);
      exit(1);
    }

  r = unfli(fpin,outbase,outext,outtype,
	    begin_frame,max_frames,verbose_flag,filter);

  if (r == 0)
    fprintf(stderr,"Stop\n");
  else if (r == 1)
    fprintf(stderr,"Ready\n");
  else if (r == -1)
    fprintf(stderr,"Read error\n");
  else if (r == -2)
    fprintf(stderr,"Error allocating memory\n");
  else
    fprintf(stderr,"Unknown error\n");

  fclose(fpin);

  return(0);
}
예제 #10
0
/*
 * The event loop for display. It displays data on screen and handles hotkey
 * presses.
 *
 * Parameter :
 *		duration - returns after 'duration'
 *
 * The function also returns if user presses 'q', 'Ctrl+C' or 'r'.
 *
 * Return value:
 *		0 - main() exits
 *		1 - main() calls it again
 */
int
lt_display_loop(int duration)
{
	uint64_t start;
	int remaining;
	struct timeval timeout;
	fd_set read_fd;
	int need_refresh = TRUE;
	pid_t *plist = NULL;
	id_t *tlist = NULL;
	int list_len = 0;
	int list_index = 0;
	int retval = 1;
	int next_snap;
	int gpipe;

	start = lt_millisecond();
	gpipe = lt_gpipe_readfd();

	if (!show_help) {
		print_hint(NULL);
		print_sysglobal();
	}

	get_plist(&plist, &tlist, &list_len, &list_index);

	for (;;) {
		if (need_refresh && !show_help) {
			if (list_len != 0) {
				if (!thread_mode) {
					print_taskbar_process(plist, list_len,
					    list_index);
					print_process(plist[list_index]);
				} else {
					print_taskbar_thread(plist, tlist,
					    list_len, list_index);
					print_thread(plist[list_index],
					    tlist[list_index]);
				}
			} else {
				print_empty_process_bar();
			}
		}

		need_refresh = TRUE;	/* Usually we need refresh. */
		remaining = duration - (int)(lt_millisecond() - start);

		if (remaining <= 0) {
			break;
		}

		/* Embedded dtrace snap action here. */
		next_snap = lt_dtrace_work(0);

		if (next_snap == 0) {
			/*
			 * Just did a snap, check time for the next one.
			 */
			next_snap = lt_dtrace_work(0);
		}

		if (next_snap > 0 && remaining > next_snap) {
			remaining = next_snap;
		}

		timeout.tv_sec = remaining / 1000;
		timeout.tv_usec = (remaining % 1000) * 1000;

		FD_ZERO(&read_fd);
		FD_SET(0, &read_fd);
		FD_SET(gpipe, &read_fd);

		/* Wait for keyboard input, or signal from gpipe */
		if (select(gpipe + 1, &read_fd, NULL, NULL, &timeout) > 0) {
			int k = 0;

			if (FD_ISSET(gpipe, &read_fd)) {
				/* Data from pipe has priority */
				char ch;
				(void) read(gpipe, &ch, 1);
				k = ch; /* Need this for big-endianness */
			} else {
				k = getch();
			}

			/*
			 * Check if we need to update the hint line whenever we
			 * get a chance.
			 * NOTE: current implementation depends on
			 * g_config.lt_cfg_snap_interval, but it's OK because it
			 * doesn't have to be precise.
			 */
			print_hint(NULL);
			/*
			 * If help is on display right now, and a key press
			 * happens, we need to clear the help and continue.
			 */
			if (show_help) {
				(void) werase(stdscr);
				(void) refresh();
				print_title();
				print_sysglobal();
				show_help = FALSE;
				/* Drop this key and continue */
				continue;
			}

			switch (k) {
			case 'Q':
			case 'q':
				retval = 0;
				goto quit;
			case 'R':
			case 'r':
				lt_display_deinit();
				lt_display_init();
				goto quit;
			case 'H':
			case 'h':
				show_help = TRUE;
				(void) werase(stdscr);
				(void) refresh();
				print_help();
				break;
			case ',':
			case '<':
			case KEY_LEFT:
				--list_index;

				if (list_index < 0) {
					list_index = 0;
				}

				break;
			case '.':
			case '>':
			case KEY_RIGHT:
				++list_index;

				if (list_index >= list_len) {
					list_index = list_len - 1;
				}

				break;
			case 'a':
			case 'A':
				sort_type = LT_SORT_AVG;
				print_sysglobal();
				break;
			case 'p':
			case 'P':
				sort_type = LT_SORT_TOTAL;
				print_sysglobal();
				break;
			case 'm':
			case 'M':
				sort_type = LT_SORT_MAX;
				print_sysglobal();
				break;
			case 'c':
			case 'C':
				sort_type = LT_SORT_COUNT;
				print_sysglobal();
				break;
			case 't':
			case 'T':
				if (plist != NULL) {
					selected_pid = plist[list_index];
				}

				selected_tid = INVALID_TID;
				thread_mode = !thread_mode;
				get_plist(&plist, &tlist,
				    &list_len, &list_index);
				break;
			case '1':
			case '!':
				current_list_type = LT_LIST_CAUSE;
				print_sysglobal();
				break;
			case '2':
			case '@':
				if (g_config.lt_cfg_low_overhead_mode) {
					lt_display_error("Switching mode is "
					    "not available for '-f low'.");
				} else {
					current_list_type = LT_LIST_SPECIALS;
					print_sysglobal();
				}

				break;
			case '3':
			case '#':
				if (g_config.lt_cfg_trace_syncobj) {
					current_list_type = LT_LIST_SOBJ;
					print_sysglobal();
				} else if (g_config.lt_cfg_low_overhead_mode) {
					lt_display_error("Switching mode is "
					    "not available for '-f low'.");
				} else {
					lt_display_error("Tracing "
					    "synchronization objects is "
					    "disabled.");
				}

				break;
			default:
				/* Wake up for nothing; no refresh is needed */
				need_refresh = FALSE;
				break;
			}
		} else {
			need_refresh = FALSE;
		}
	}

quit:
	if (plist != NULL) {
		selected_pid = plist[list_index];
	}

	if (tlist != NULL) {
		selected_tid = tlist[list_index];
	}

	lt_stat_proc_list_free(plist, tlist);

	return (retval);
}
예제 #11
0
파일: mirb.c 프로젝트: delonnewman/mruby
int
main(void)
{
  char ruby_code[1024] = { 0 };
  char last_code_line[1024] = { 0 };
#ifndef ENABLE_READLINE
  int last_char;
  int char_index;
#endif
  mrbc_context *cxt;
  struct mrb_parser_state *parser;
  mrb_state *mrb;
  mrb_value result;
  int n;
  int code_block_open = FALSE;
  int ai;

  print_hint();

  /* new interpreter instance */
  mrb = mrb_open();
  if (mrb == NULL) {
    fprintf(stderr, "Invalid mrb interpreter, exiting mirb");
    return EXIT_FAILURE;
  }

  cxt = mrbc_context_new(mrb);
  cxt->capture_errors = 1;

  ai = mrb_gc_arena_save(mrb);
  while (TRUE) {
#ifndef ENABLE_READLINE
    print_cmdline(code_block_open);

    char_index = 0;
    while ((last_char = getchar()) != '\n') {
      if (last_char == EOF) break;
      last_code_line[char_index++] = last_char;
    }
    if (last_char == EOF) {
      printf("\n");
      break;
    }

    last_code_line[char_index] = '\0';
#else
    char* line = readline(code_block_open ? "* " : "> ");
    strncat(last_code_line, line, sizeof(last_code_line)-1);
    add_history(line);
    free(line);
#endif

    if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) {
      if (!code_block_open) {
        break;
      }
      else{
        /* count the quit/exit commands as strings if in a quote block */
        strcat(ruby_code, "\n");
        strcat(ruby_code, last_code_line);
      }
    }
    else {
      if (code_block_open) {
        strcat(ruby_code, "\n");
        strcat(ruby_code, last_code_line);
      }
      else {
        strcpy(ruby_code, last_code_line);
      }
    }

    /* parse code */
    parser = mrb_parser_new(mrb);
    parser->s = ruby_code;
    parser->send = ruby_code + strlen(ruby_code);
    parser->lineno = 1;
    mrb_parser_parse(parser, cxt);
    code_block_open = is_code_block_open(parser);

    if (code_block_open) {
      /* no evaluation of code */
    }
    else {
      if (0 < parser->nerr) {
	/* syntax error */
	printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message);
      }
      else {
	/* generate bytecode */
	n = mrb_generate_code(mrb, parser);

	/* evaluate the bytecode */
	result = mrb_run(mrb,
            /* pass a proc for evaulation */
            mrb_proc_new(mrb, mrb->irep[n]),
            mrb_top_self(mrb));
	/* did an exception occur? */
	if (mrb->exc) {
	  p(mrb, mrb_obj_value(mrb->exc));
	  mrb->exc = 0;
	}
	else {
	  /* no */
	  printf(" => ");
	  p(mrb, result);
	}
      }
      ruby_code[0] = '\0';
      last_code_line[0] = '\0';
      mrb_parser_free(parser);
      mrb_gc_arena_restore(mrb, ai);
    }
  }
  mrbc_context_free(mrb, cxt);
  mrb_close(mrb);

  return 0;
}