Ejemplo n.º 1
0
int eval_file(char* path)
{
  int state;
  rb_load_protect(rb_str_new2(path), 0, &state);

  return state;
}
Ejemplo n.º 2
0
/*
 *  call-seq:
 *    Byebug.debug_load(file, stop = false) -> nil
 *
 *  Same as Kernel#load but resets current context's frames.
 *  +stop+ parameter forces byebug to stop at the first line of code in +file+
 */
static VALUE
Debug_load(int argc, VALUE * argv, VALUE self)
{
  VALUE file, stop, context;
  debug_context_t *dc;
  VALUE status = Qnil;
  int state = 0;

  UNUSED(self);

  if (rb_scan_args(argc, argv, "11", &file, &stop) == 1)
    stop = Qfalse;

  Start(self);

  context = Current_context(self);
  Data_Get_Struct(context, debug_context_t, dc);

  dc->calced_stack_size = 1;

  if (RTEST(stop))
    dc->steps = 1;

  rb_load_protect(file, 0, &state);
  if (0 != state)
  {
    status = rb_errinfo();
    reset_stepping_stop_points(dc);
  }

  return status;
}
Ejemplo n.º 3
0
static VALUE
load_application(char *path, char *name)
{
	VALUE app_class, class_name;
	VALUE filename;
	int state;

	class_name = rb_str_new2(name);
	rb_gc_register_address(&class_name);
	app_class = rb_hash_aref(application_classes, class_name);
	if (NIL_P(app_class)) {
		filename = get_source_filename(name, path);
		if (NIL_P(filename)) {
			Warning("invalid module name: %s\n", name);
			return Qnil;
		}
		
		rb_load_protect(filename, 0, &state);
		if (state && error_handle(state))
			return Qnil;
		app_class = rb_eval_string_protect(name, &state);
		rb_gc_register_address(&app_class);
        
		if (state && error_handle(state))
			return Qnil;
		rb_hash_aset(application_classes, class_name, app_class);
	}
	return app_class;
}
Ejemplo n.º 4
0
/* call a ruby script as CGI via HTTP */
unsigned int http_ruby_script(const char *name, const char *output)
{
  struct stat st;
  char *tempstr;
  int rc;

  updatecontext();

  if (!name)
    return 1;

  if (stat(name, &st) < 0) {
    outerror(OUTERROR_TYPE_WARN_LOUD,
             "cannot access '%s', ignoring: %s",
             name, strerror(errno));
    return 1;
  }
  ruby_script(name);
  tempstr = mymalloc(maxtextlength);
  snprintf(tempstr, maxtextlength, "$stdout = File.new(\"%s\", \"w+\")", output); /* NOTRANSLATE */
  rb_eval_string_protect(tempstr, &rc);
  mydelete(tempstr);
  rb_load_protect(rb_str_new(name, strlen(name)), 0, &rc);
  if (rc != 0) {
    outerror(OUTERROR_TYPE_WARN_LOUD,
             "ruby_exec failed with %d: %s",
             rc, strerror(errno));
    iroffer_ruby_errro(rc);
  }
  rb_eval_string_protect("$stdout.close", &rc); /* NOTRANSLATE */
  if (rc != 0)
    return 1;
  return 0;
}
Ejemplo n.º 5
0
int proxenet_ruby_load_file(plugin_t* plugin)
{
	char* filename;
	char* pathname;
	int res = 0;

        if(plugin->state != INACTIVE){
#ifdef DEBUG
                if(cfg->verbose > 2)
                        xlog(LOG_DEBUG, "Plugin '%s' is already loaded. Skipping...\n", plugin->name);
#endif
                return 0;
        }

	filename = plugin->filename;
        pathname = plugin->fullpath;

	rb_load_protect(rb_str_new_cstr(pathname), 0, &res);
	if (res != 0) {
		xlog(LOG_ERROR, "[Ruby] Error %d when load file '%s'\n", res, pathname);
		return -1;
	}

#ifdef DEBUG
	xlog(LOG_DEBUG, "%s\n", pathname);
#endif
	return 0;
}
Ejemplo n.º 6
0
static void load_script(const char *name)
{
  struct stat st;
  int rc;

  updatecontext();

  if (!name)
    return;

  if (stat(name, &st) < 0) {
    myruby_loaded = -1;
    outerror(OUTERROR_TYPE_WARN_LOUD,
             "cannot access '%s', ignoring: %s",
             name, strerror(errno));
    return;
  }
  ruby_script(name);
  myruby_time = st.st_mtime;
  rb_load_protect(rb_str_new(name, strlen(name)), 0, &rc);
  if (rc != 0) {
    outerror(OUTERROR_TYPE_WARN_LOUD,
             "ruby_exec failed with %d: %s",
             rc, strerror(errno));
    myruby_loaded = 1;
    return;
  }

  myruby_loaded = 1;
  oIrofferEvent = rb_class_new_instance(0, NULL, cIrofferEvent);
  rb_define_variable("objIrofferEvent", &oIrofferEvent); /* NOTRANSLATE */
}
Ejemplo n.º 7
0
void ex_rubyfile(exarg_T *eap)
{
    int state;

    if (ensure_ruby_initialized())
    {
	rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
	if (state) error_print(state);
    }
}
Ejemplo n.º 8
0
void load(const char* filename, int anonymous)
{
    int error = 0;
    rb_load_protect(rb_str_new2(filename), anonymous, &error);

    if(error)
    {
        Exception e;
        e.backtrace();
        throw e;
    }
}
Ejemplo n.º 9
0
/**
 * Safely load a Ruby script in the VM
 *
 * @return 0 upon success, -1 otherwise
 */
int proxenet_ruby_load_file(plugin_t* plugin)
{
	char* pathname;
	int res = 0;

        if (!plugin->interpreter || !plugin->interpreter->ready){
                xlog_ruby(LOG_ERROR, "Interpreter '%s' is not ready\n", _RUBY_VERSION_);
                return -1;
        }

        if(plugin->state != INACTIVE){
#ifdef DEBUG
                if(cfg->verbose > 2)
                        xlog_ruby(LOG_DEBUG, "Plugin '%s' is already loaded. Skipping...\n", plugin->name);
#endif
                return 0;
        }

        pathname = plugin->fullpath;

	rb_load_protect(rb_str_new2(pathname), false, &res);
	if (res != 0) {
	        xlog_ruby(LOG_ERROR, "Error %d when load file '%s'\n", res, pathname);
		proxenet_ruby_print_last_exception();
		return -1;
	}

	if (cfg->verbose)
		xlog_ruby(LOG_INFO, "File '%s' is loaded\n", pathname);


        if (proxenet_ruby_initialize_function(plugin, REQUEST) < 0) {
                proxenet_plugin_set_state(plugin, INACTIVE);
                xlog_ruby(LOG_ERROR, "Failed to init %s in %s\n", CFG_REQUEST_PLUGIN_FUNCTION, plugin->name);
                return -1;
        }

        if (proxenet_ruby_initialize_function(plugin, RESPONSE) < 0) {
                proxenet_plugin_set_state(plugin, INACTIVE);
                xlog_ruby(LOG_ERROR, "Failed to init %s in %s\n", CFG_RESPONSE_PLUGIN_FUNCTION, plugin->name);
                return -1;
        }

	return 0;
}
Ejemplo n.º 10
0
static int
load_script(int argc, char **argv)
{
  VALUE r_argv, fname;
  int state, i;

  if (argc < 1) {
    return 0;
  }

  r_argv = rb_const_get(rb_mKernel, rb_intern("ARGV"));
  rb_ary_clear(r_argv);
  for (i = 1; i < argc; i++) {
    rb_ary_push(r_argv, rb_tainted_str_new2(argv[i]));
  }

  fname = rb_funcall(rb_cFile, ExpandPath, 1, rb_str_new2(argv[0]));
  rb_load_protect(fname, 1, &state);
  if (state) {
    VALUE errinfo, errstr, errat;
    int n, i;
    const char *cstr;

    errinfo = rb_errinfo();
    errstr = rb_obj_as_string(errinfo);
    cstr = StringValueCStr(errstr);
    if (strcmp(cstr, "exit")) {
      ngraph_err_puts(cstr);
      errat = rb_funcall(errinfo, rb_intern("backtrace"), 0);
      if (! NIL_P(errat)) {
	n = RARRAY_LEN(errat);
	for (i = 0; i < n; i ++) {
	  errstr = rb_str_new2("\tfrom ");
	  rb_str_append(errstr, rb_ary_entry(errat, i));
	  ngraph_err_puts(StringValueCStr(errstr));
	}
      }
    }
  }
  rb_gc_start();

  return 0;
}
Ejemplo n.º 11
0
void
init_ruby(struct module *module)
{
	unsigned char *path;

	/* Set up and initialize the interpreter. This function should be called
	 * before any other Ruby-related functions. */
	ruby_init();
	ruby_script("ELinks-ruby");
	ruby_init_loadpath();

	/* ``Trap'' debug prints from scripts. */
	rb_define_singleton_method(rb_stdout, "write", erb_module_message, 1);
	rb_define_global_function("p", erb_stdout_p, -1);

	/* Set up the ELinks module interface. */
	init_erb_module();

	if (elinks_home) {
		path = straconcat(elinks_home, RUBY_HOOKS_FILENAME,
				  (unsigned char *) NULL);

	} else {
		path = stracpy(CONFDIR STRING_DIR_SEP RUBY_HOOKS_FILENAME);
	}

	if (!path) return;

	if (file_can_read(path)) {
		int error;

		/* Load ~/.elinks/hooks.rb into the interpreter. */
		//rb_load_file(path);
		rb_load_protect(rb_str_new2(path), 0, &error);
		if (error)
			erb_report_error(NULL, error);
	}

	mem_free(path);
}
Ejemplo n.º 12
0
/**
 * Load a Ruby file then run the function corresponding to the service by
 * passing the conf, inputs and outputs parameters by refernce as Ruby Hash.
 *
 * @param main_conf the conf maps containing the main.cfg settings
 * @param request the map containing the HTTP request
 * @param s the service structure
 * @param real_inputs the maps containing the inputs
 * @param real_outputs the maps containing the outputs
 * @return SERVICE_SUCCEEDED or SERVICE_FAILED if the service run, -1 
 *  if the service failed to load or throw error at runtime.
 */
int zoo_ruby_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
#if RUBY_API_VERSION_MAJOR >= 2 || RUBY_API_VERSION_MINOR == 9
  ruby_sysinit(&argc,&argv);
  RUBY_INIT_STACK;
#endif
  ruby_init();
  maps* m=*main_conf;
  maps* inputs=*real_inputs;
  maps* outputs=*real_outputs;
  map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd");
  char *ntmp=tmp0->value;
  map* tmp=NULL;
  ruby_init_loadpath();
  ruby_script("ZOO_EMBEDDED_ENV");
  
  VALUE klass=rb_define_module("Zoo");
  rb_define_const(klass,"SERVICE_SUCCEEDED",INT2FIX(3));
  rb_define_const(klass,"SERVICE_FAILED",INT2FIX(4));
  typedef VALUE (*HOOK)(...);
  rb_define_module_function(klass,"Translate",reinterpret_cast<HOOK>(RubyTranslate),-1);
  rb_define_module_function(klass,"UpdateStatus",reinterpret_cast<HOOK>(RubyUpdateStatus),-1);

  int error = 0;
		
  ID rFunc=Qnil;
  tmp=getMap(s->content,"serviceProvider");
  if(tmp!=NULL){
#if RUBY_VERSION_MINOR == 8
    const char* script = ruby_sourcefile = rb_source_filename(tmp->value);
    rb_protect(LoadWrap, reinterpret_cast<VALUE>(script), &error);
#else
    rb_load_protect(rb_str_new2(tmp->value), 0, &error);
#endif
    if(error) {
      ruby_trace_error(m);
      return -1;
    }
#if RUBY_VERSION_MINOR == 8
    ruby_exec();
#else
    ruby_exec_node(NULL);
#endif
  }
  else{
    map* err=createMap("text","Unable to parse serviceProvider please check your zcfg file.");
    addToMap(err,"code","NoApplicableCode");
    printExceptionReportResponse(m,err);
    return -1;
  }
  int res=SERVICE_FAILED;
  rFunc=rb_intern(s->name);
  if(rFunc!=Qnil){
    VALUE arg1=RubyHash_FromMaps(m);
    VALUE arg2=RubyHash_FromMaps(inputs);
    VALUE arg3=RubyHash_FromMaps(outputs);
    VALUE rArgs[3]={arg1,arg2,arg3};
    if (!rArgs)
      return -1;
    struct my_callback data;
    data.obj=Qnil;
    data.method_id=rFunc;
    data.nargs=3;
    data.args[0]=rArgs[0];
    data.args[1]=rArgs[1];
    data.args[2]=rArgs[2];
    typedef VALUE (*HOOK)(VALUE);
    VALUE tres=rb_protect(reinterpret_cast<HOOK>(FunCallWrap),(VALUE)(&data),&error);
    if (TYPE(tres) == T_FIXNUM) {
      res=FIX2INT(tres);
      freeMaps(real_outputs);
      free(*real_outputs);
      freeMaps(main_conf);
      free(*main_conf);
      *main_conf=mapsFromRubyHash(arg1);
      *real_outputs=mapsFromRubyHash(arg3);
#ifdef DEBUG
      dumpMaps(*main_conf);
      dumpMaps(*real_outputs);
#endif
    }else{
      ruby_trace_error(m);
      res=-1;
    }
  }
  else{
    char tmpS[1024];
    sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value);
    map* tmps=createMap("text",tmpS);
    printExceptionReportResponse(m,tmps);
    res=-1;
  }
  ruby_finalize();
  return res;
}