Ejemplo n.º 1
0
/** Listen on endpoint. */
static int net_listen(lua_State *L)
{
    /* Check parameters */
    int n = lua_gettop(L);
    int port = KR_DNS_PORT;
    if (n > 1 && lua_isnumber(L, 2)) {
        port = lua_tointeger(L, 2);
    }

    /* Process interface or (address, port) pair. */
    if (lua_istable(L, 1)) {
        return net_listen_iface(L, port);
    } else if (n < 1 || !lua_isstring(L, 1)) {
        format_error(L, "expected 'listen(string addr, number port = 53)'");
        lua_error(L);
    }

    /* Open resolution context cache */
    struct engine *engine = engine_luaget(L);
    int ret = network_listen(&engine->net, lua_tostring(L, 1), port, NET_TCP|NET_UDP);
    if (ret != 0) {
        format_error(L, kr_strerror(ret));
        lua_error(L);
    }

    lua_pushboolean(L, true);
    return 1;
}
Ejemplo n.º 2
0
/** Load module. */
static int mod_load(lua_State *L)
{
    /* Check parameters */
    int n = lua_gettop(L);
    if (n != 1 || !lua_isstring(L, 1)) {
        format_error(L, "expected 'load(string name)'");
        lua_error(L);
    }
    /* Parse precedence declaration */
    auto_free char *declaration = strdup(lua_tostring(L, 1));
    if (!declaration) {
        return kr_error(ENOMEM);
    }
    const char *name = strtok(declaration, " ");
    const char *precedence = strtok(NULL, " ");
    const char *ref = strtok(NULL, " ");
    /* Load engine module */
    struct engine *engine = engine_luaget(L);
    int ret = engine_register(engine, name, precedence, ref);
    if (ret != 0) {
        if (ret == kr_error(EIDRM)) {
            format_error(L, "referenced module not found");
        } else {
            format_error(L, kr_strerror(ret));
        }
        lua_error(L);
    }

    lua_pushboolean(L, 1);
    return 1;
}
Ejemplo n.º 3
0
static int event_sched(lua_State *L, unsigned timeout, unsigned repeat)
{
    uv_timer_t *timer = malloc(sizeof(*timer));
    if (!timer) {
        format_error(L, "out of memory");
        lua_error(L);
    }

    /* Start timer with the reference */
    uv_loop_t *loop = uv_default_loop();
    uv_timer_init(loop, timer);
    int ret = uv_timer_start(timer, event_callback, timeout, repeat);
    if (ret != 0) {
        free(timer);
        format_error(L, "couldn't start the event");
        lua_error(L);
    }

    /* Save callback and timer in registry */
    lua_newtable(L);
    lua_pushvalue(L, 2);
    lua_rawseti(L, -2, 1);
    lua_pushlightuserdata(L, timer);
    lua_rawseti(L, -2, 2);
    int ref = luaL_ref(L, LUA_REGISTRYINDEX);

    /* Save reference to the timer */
    timer->data = (void *) (intptr_t)ref;
    lua_pushinteger(L, ref);
    return 1;
}
Ejemplo n.º 4
0
/** Listen on interface address list. */
static int net_listen_iface(lua_State *L, int port)
{
    /* Expand 'addr' key if exists */
    lua_getfield(L, 1, "addr");
    if (lua_isnil(L, -1)) {
        lua_pop(L, 1);
        lua_pushvalue(L, 1);
    }

    /* Bind to address list */
    struct engine *engine = engine_luaget(L);
    size_t count = lua_rawlen(L, -1);
    for (size_t i = 0; i < count; ++i) {
        lua_rawgeti(L, -1, i + 1);
        int ret = network_listen(&engine->net, lua_tostring(L, -1),
                                 port, NET_TCP|NET_UDP);
        if (ret != 0) {
            format_error(L, kr_strerror(ret));
            lua_error(L);
        }
        lua_pop(L, 1);
    }

    lua_pushboolean(L, true);
    return 1;
}
Ejemplo n.º 5
0
void
parse_format (void)
{
  format_string = ioparm.format;
  format_string_len = ioparm.format_len;

  saved_token = FMT_NONE;
  error = NULL;

  /* Initialize variables used during traversal of the tree */

  reversion_ok = 0;
  g.reversion_flag = 0;
  saved_format = NULL;

  /* Allocate the first format node as the root of the tree */

  avail = array;

  avail->format = FMT_LPAREN;
  avail->repeat = 1;
  avail++;

  if (format_lex () == FMT_LPAREN)
    array[0].u.child = parse_format_list ();
  else
    error = "Missing initial left parenthesis in format";

  if (error)
    format_error (NULL, error);
}
Ejemplo n.º 6
0
    void read_vertex () {
        int number;
        is >> number;

        // vertex number is terminated by colon
        is >> std::ws;
        int colon;
        if ((colon = is.get ()) != ':')
            format_error ("expected colon, got %c", colon);

        // coords
        double x, y, z;
        is >> x >> y >> z;

        // rest of record (i.e. attributes) is ignored for now
        ignore_rest_of_line ();

        // z coordinate is ignored
        int vert_id = b->insert_vertex (vec_t (x, y));

        // saved for later reference
        if ((int)vertex_map.size () <= number) {
            vertex_map.resize (number + 1, Boundary::INVALID_VERTEX);
        }
        vertex_map[number] = vert_id;
    }
Ejemplo n.º 7
0
    bool    interpret( cstring param_name, cstring source ) const
    {
        static cstring const s_YES( "YES" );
        static cstring const s_Y( "Y" );
        static cstring const s_NO( "NO" );
        static cstring const s_N( "N" );
        static cstring const s_TRUE( "TRUE" );
        static cstring const s_FALSE( "FALSE" );
        static cstring const s_one( "1" );
        static cstring const s_zero( "0" );

        source.trim();

        if( source.is_empty() ||
            case_ins_eq( source, s_YES ) ||
            case_ins_eq( source, s_Y ) ||
            case_ins_eq( source, s_one ) ||
            case_ins_eq( source, s_TRUE ) )
            return true;

        if( case_ins_eq( source, s_NO ) ||
            case_ins_eq( source, s_N ) ||
            case_ins_eq( source, s_zero ) ||
            case_ins_eq( source, s_FALSE ) )
            return false;

        BOOST_TEST_I_THROW( format_error( param_name ) << source << " can't be interpreted as bool value." );
    }
Ejemplo n.º 8
0
 NameValidationError(
   const char * name_type_,
   const char * name_,
   const char * error_msg_,
   size_t invalid_index_)
 : std::invalid_argument(format_error(name_type_, name_, error_msg_, invalid_index_)),
   name_type(name_type_), name(name_), error_msg(error_msg_), invalid_index(invalid_index_)
 {}
Ejemplo n.º 9
0
 void extract_header (const string &tag) {
     std::string tmp;
     std::getline (is, tmp);
     if (tmp != tag) {
         format_error ("format error in POLY file: expected %s, got %s",
             tag.c_str (), tmp.c_str ());
     }
 }
Ejemplo n.º 10
0
 ValueType interpret( cstring param_name, cstring source ) const
 {
     ValueType res;
     if( !unit_test::utils::string_as<ValueType>( source, res ) )
         BOOST_TEST_I_THROW( format_error( param_name ) << source <<
                             " can't be interpreted as value of parameter " << param_name << "." );
     return res;
 }
Ejemplo n.º 11
0
/** Open cache */
static int cache_open(lua_State *L)
{
    /* Check parameters */
    int n = lua_gettop(L);
    if (n < 1 || !lua_isnumber(L, 1)) {
        format_error(L, "expected 'open(number max_size, string config = \"\")'");
        lua_error(L);
    }

    /* Select cache storage backend */
    struct engine *engine = engine_luaget(L);
    unsigned cache_size = lua_tonumber(L, 1);
    const char *conf = n > 1 ? lua_tostring(L, 2) : NULL;
    const char *uri = conf;
    struct storage_api *storage = cache_select_storage(engine, &conf);
    if (!storage) {
        format_error(L, "unsupported cache backend");
        lua_error(L);
    }

    /* Close if already open */
    kr_cache_close(&engine->resolver.cache);

    /* Reopen cache */
    void *storage_opts = storage->opts_create(conf, cache_size);
    int ret = kr_cache_open(&engine->resolver.cache, storage->api(), storage_opts, engine->pool);
    free(storage_opts);
    if (ret != 0) {
        format_error(L, "can't open cache");
        lua_error(L);
    }

    /* Store current configuration */
    lua_getglobal(L, "cache");
    lua_pushstring(L, "current_size");
    lua_pushnumber(L, cache_size);
    lua_rawset(L, -3);
    lua_pushstring(L, "current_storage");
    lua_pushstring(L, uri);
    lua_rawset(L, -3);
    lua_pop(L, 1);

    lua_pushboolean(L, 1);
    return 1;
}
Ejemplo n.º 12
0
 int lookup_vertex (int v_id) {
     if ((int)vertex_map.size () > v_id && v_id >= 0) {
         int ret = vertex_map[v_id];
         if (ret != Boundary::INVALID_VERTEX)
             return ret;
     }
     format_error ("file refers to vertex %i which I do not know (vertex_map size = %i)",
                   v_id, (int)vertex_map.size ());
 }
Ejemplo n.º 13
0
/**
 * Install as a service.
 */
static int service_install()
{
    SC_HANDLE handleManager;
    SC_HANDLE handleService;
    TCHAR service_path[MAX_PATH];

    // Get the path to the executable.
    if (!GetModuleFileName(NULL, service_path, MAX_PATH))
    {
        std::cerr << "Cannot get path to executable: "
                  << format_error(GetLastError()) << std::endl;
        return 1;
    }

    // Get a handle to the SCM database. 
    handleManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (handleManager == NULL)
    {
        std::cerr << "Cannot open service control manager: "
                  << format_error(GetLastError()) << std::endl;
        return 2;
    }

    // Create the service.
    handleService =
        CreateService(handleManager, SERVICE_NAME, SERVICE_NAME,
                      SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
                      SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
                      service_path, NULL, NULL, NULL, NULL, NULL);

    if (handleService == NULL) 
    {
        std::cerr << "Cannot create service: "
                  << format_error(GetLastError()) << std::endl;
        CloseServiceHandle(handleManager);
        return 3;
    }

    std::cerr << "Service installed successfully" << std::endl;
    CloseServiceHandle(handleService); 
    CloseServiceHandle(handleManager);
    return 0;
}
Ejemplo n.º 14
0
/** Unload module. */
static int mod_unload(lua_State *L)
{
    /* Check parameters */
    int n = lua_gettop(L);
    if (n != 1 || !lua_isstring(L, 1)) {
        format_error(L, "expected 'unload(string name)'");
        lua_error(L);
    }
    /* Unload engine module */
    struct engine *engine = engine_luaget(L);
    int ret = engine_unregister(engine, lua_tostring(L, 1));
    if (ret != 0) {
        format_error(L, kr_strerror(ret));
        lua_error(L);
    }

    lua_pushboolean(L, 1);
    return 1;
}
Ejemplo n.º 15
0
const char* parse_one_way_tag(const char* value) {
    const char* one_way = "oneway";
    if (!strcmp(value, "F"))				// F --> FROM reference node
        return YES;
    else if (!strcmp(value, "T"))			// T --> TO reference node
        return "-1";    // todo reverse way instead using "-1"
    else if (!strcmp(value, "B"))			// B --> BOTH ways are allowed
        return nullptr;
    throw(format_error("value '" + std::string(value) + "' for oneway not valid"));
}
Ejemplo n.º 16
0
static int event_recurrent(lua_State *L)
{
    /* Check parameters */
    int n = lua_gettop(L);
    if (n < 2 || !lua_isnumber(L, 1) || !lua_isfunction(L, 2)) {
        format_error(L, "expected 'recurrent(number interval, function)'");
        lua_error(L);
    }
    return event_sched(L, 0, lua_tonumber(L, 1));
}
Ejemplo n.º 17
0
/**
 * \brief returns field from OGRFeature
 *        throws exception if field_value is not
 * \param feat feature from which field is read
 * \param field field name as key
 * \return field value as uint
 */
uint64_t get_uint_from_feature(OGRFeature* feat, const char* field) {
    const char* value = get_field_from_feature(feat, field);
    assert(value);
    try {
        return std::stoul(value);
    } catch (const std::invalid_argument &) {
        throw format_error(
                "Could not parse field='" + std::string(field) + "' with value='" + std::string(value) + "'");
    }
}
Ejemplo n.º 18
0
static int event_after(lua_State *L)
{
    /* Check parameters */
    int n = lua_gettop(L);
    if (n < 2 || !lua_isnumber(L, 1) || !lua_isfunction(L, 2)) {
        format_error(L, "expected 'after(number timeout, function)'");
        lua_error(L);
    }

    return event_sched(L, lua_tonumber(L, 1), 0);
}
Ejemplo n.º 19
0
static void
_setup(enum hdfs_namenode_proto vers)
{
	struct hdfs_error error;

	h = hdfs_namenode_new_version(H_ADDR, "8020", H_USER, HDFS_NO_KERB,
	    vers, &error);
	ck_assert_msg((intptr_t)h,
	    "Could not connect to %s=%s @ %s=%s (port 8020): %s",
	    HDFS_T_USER, H_USER, HDFS_T_ENV, H_ADDR, format_error(error));
}
Ejemplo n.º 20
0
/**
 * \brief check if unit is imperial. area_id(Streets.dbf) -> govt_id(MtdArea.dbf) -> unit_measure(MtdCntryRef.dbf)
 * \param area_id area_id
 * \param area_govt_map maps area_ids to govt_codes
 * \param cntry_map maps govt_codes to cntry_ref_types
 * \return returns false if any of the areas contain metric units or if its unclear
 */
bool is_imperial(area_id_type area_id, area_id_govt_code_map_type* area_govt_map,
        cntry_ref_map_type* cntry_map) {
    if (area_govt_map->find(area_id) != area_govt_map->end()) {
        if (cntry_map->find(area_govt_map->at(area_id)) != cntry_map->end()) {
            auto unit_measure = cntry_map->at(area_govt_map->at(area_id)).unit_measure;
            if (unit_measure == 'E') {
                return true;
            } else if (unit_measure != 'M'){
                format_error("unit_measure in navteq data is invalid: '" + std::to_string(unit_measure) + "'");
            }
        }
    }
    return false;
}
Ejemplo n.º 21
0
    void read_poly () {
        int number;
        is >> number;

        // poly number is terminated by colon
        is >> std::ws;
        int colon;
        if ((colon = is.get ()) != ':')
            format_error ("PolyFileReader::read_poly: expected colon, got %c", colon);

        // first two vertices
        int initial_vertex;
        is >> initial_vertex;
        // translate POLY vertices into Boundary vertices
        initial_vertex = lookup_vertex (initial_vertex);
        int prev_vertex;
        is >> prev_vertex >> std::ws;
        prev_vertex = lookup_vertex (prev_vertex);

        int initial_edge = b->insert_edge (Boundary::INVALID_EDGE,
                                    initial_vertex, prev_vertex,
                                    Boundary::INVALID_EDGE);

        int prev_edge = initial_edge;
        while (is_digit (is.peek ())) {
            int vertex;
            is >> vertex >> std::ws;
            // translate POLY vertices into Boundary vertices
            vertex = lookup_vertex (vertex);

            int edge = b->insert_edge (prev_edge, prev_vertex,
                                       vertex, Boundary::INVALID_EDGE);
            prev_vertex = vertex;
            prev_edge = edge;
        }

        is >> std::ws;
        int closed_indic = is.peek ();
        if (closed_indic == '<') {
            // extract '<' character
            is.get ();
            // close contour
            b->insert_edge (prev_edge, prev_vertex, initial_vertex, initial_edge);
        } else {
            std::cerr << "WARNING: non-closed contour " << number << ". this is probably not what you want.\n";
        }

        ignore_rest_of_line ();
    }
Ejemplo n.º 22
0
fnode *
next_format (void)
{
  format_token t;
  fnode *f;

  if (saved_format != NULL)
    {				/* Deal with a pushed-back format node */
      f = saved_format;
      saved_format = NULL;
      goto done;
    }

  f = next_format0 (&array[0]);
  if (f == NULL)
    {
      if (!reversion_ok)
	{
	  return NULL;
	}

      reversion_ok = 0;
      revert ();

      f = next_format0 (&array[0]);
      if (f == NULL)
	{
	  format_error (NULL, reversion_error);
	  return NULL;
	}

      /* Push the first reverted token and return a colon node in case
       * there are no more data items. */

      saved_format = f;
      return &colon_node;
    }

  /* If this is a data edit descriptor, then reversion has become OK. */
 done:
  t = f->format;

  if (!reversion_ok &&
      (t == FMT_I || t == FMT_B || t == FMT_O || t == FMT_Z || t == FMT_F ||
       t == FMT_E || t == FMT_EN || t == FMT_ES || t == FMT_G || t == FMT_L ||
       t == FMT_A || t == FMT_D))
    reversion_ok = 1;
  return f;
}
Ejemplo n.º 23
0
/** Close endpoint. */
static int net_close(lua_State *L)
{
    /* Check parameters */
    int n = lua_gettop(L);
    if (n < 2) {
        format_error(L, "expected 'close(string addr, number port)'");
        lua_error(L);
    }

    /* Open resolution context cache */
    struct engine *engine = engine_luaget(L);
    int ret = network_close(&engine->net, lua_tostring(L, 1), lua_tointeger(L, 2));
    lua_pushboolean(L, ret == 0);
    return 1;
}
Ejemplo n.º 24
0
void add_ferry_tag(osmium::builder::TagListBuilder* builder, ogr_feature_uptr& f) {
    const char* ferry = get_field_from_feature(f, FERRY);
    builder->add_tag("route", "ferry");
    if (!strcmp(ferry, "B")) {
        if (only_pedestrians(f)) {
            builder->add_tag("foot", YES);
        } else {
            builder->add_tag("foot", parse_bool(get_field_from_feature(f, AR_PEDESTRIANS)) ? YES : NO);
            builder->add_tag("motorcar", parse_bool(get_field_from_feature(f, AR_AUTO)) ? YES : NO);
        }

    } else if (!strcmp(ferry, "R")) {
        builder->add_tag("railway", "ferry");
    } else throw(format_error("value '" + std::string(ferry) + "' for " + std::string(FERRY) + " not valid"));
}
Ejemplo n.º 25
0
Archivo: format.c Proyecto: Lao16/gcc
const fnode *
next_format (st_parameter_dt *dtp)
{
  format_token t;
  const fnode *f;
  format_data *fmt = dtp->u.p.fmt;

  if (fmt->saved_format != NULL)
    {				/* Deal with a pushed-back format node */
      f = fmt->saved_format;
      fmt->saved_format = NULL;
      goto done;
    }

  f = next_format0 (&fmt->array.array[0]);
  if (f == NULL)
    {
      if (!fmt->reversion_ok)
	return NULL;

      fmt->reversion_ok = 0;
      revert (dtp);

      f = next_format0 (&fmt->array.array[0]);
      if (f == NULL)
	{
	  format_error (dtp, NULL, reversion_error);
	  return NULL;
	}

      /* Push the first reverted token and return a colon node in case
       * there are no more data items. */

      fmt->saved_format = f;
      return &colon_node;
    }

  /* If this is a data edit descriptor, then reversion has become OK. */
 done:
  t = f->format;

  if (!fmt->reversion_ok &&
      (t == FMT_I || t == FMT_B || t == FMT_O || t == FMT_Z || t == FMT_F ||
       t == FMT_E || t == FMT_EN || t == FMT_ES || t == FMT_G || t == FMT_L ||
       t == FMT_A || t == FMT_D))
    fmt->reversion_ok = 1;
  return f;
}
Ejemplo n.º 26
0
//print result to somewhere
void emgr_print(emgr_err_struct * estruct){
#ifdef __DEBUG_MODE

  char buffer[256] = "";
  char ErrChar[6];
  uint8_t str_size = 0;

  format_error(ErrChar, estruct->is_error);
  
  str_size = sprintf(buffer, "Result:%s type:%s code:%d\n",
		     ErrChar, TYPE_STRING[estruct->type], estruct->code);
  buffer[str_size] = 0;
  logger_print(str_size, buffer);

#endif
};
Ejemplo n.º 27
0
/** Set UDP maximum payload size. */
static int net_bufsize(lua_State *L)
{
    struct engine *engine = engine_luaget(L);
    knot_rrset_t *opt_rr = engine->resolver.opt_rr;
    if (!lua_isnumber(L, 1)) {
        lua_pushnumber(L, knot_edns_get_payload(opt_rr));
        return 1;
    }
    int bufsize = lua_tointeger(L, 1);
    if (bufsize < KNOT_EDNS_MIN_DNSSEC_PAYLOAD || bufsize > UINT16_MAX) {
        format_error(L, "bufsize must be within <1220, 65535>");
        lua_error(L);
    }
    knot_edns_set_payload(opt_rr, (uint16_t) bufsize);
    return 0;
}
Ejemplo n.º 28
0
void emgr_print_ffl(emgr_err_struct * estruct, const char * File, const char * Func,  int Line){
#ifdef __DEBUG_MODE

  char buffer[256] = "";
  char ErrChar[6];
  uint8_t str_size = 0;

  format_error(ErrChar, estruct->is_error);
  
  str_size = sprintf(buffer, "Result:%s type:%s code:%d file:%s func:%s line:%d\n",
		     ErrChar, TYPE_STRING[estruct->type], estruct->code,
		     File, Func, Line);
  buffer[str_size] = 0;
  logger_print(str_size, buffer);


#endif
};
Ejemplo n.º 29
0
/** Return number of cached records. */
static int cache_count(lua_State *L)
{
    struct engine *engine = engine_luaget(L);
    const knot_db_api_t *storage = engine->resolver.cache.api;

    /* Fetch item count */
    struct kr_cache_txn txn;
    int ret = kr_cache_txn_begin(&engine->resolver.cache, &txn, KNOT_DB_RDONLY);
    if (ret != 0) {
        format_error(L, kr_strerror(ret));
        lua_error(L);
    }

    /* First key is a version counter, omit it. */
    lua_pushinteger(L, storage->count(&txn.t) - 1);
    kr_cache_txn_abort(&txn);
    return 1;
}
Ejemplo n.º 30
0
/**
 * \brief adds maxspeed tag
 */
void add_maxspeed_tags(osmium::builder::TagListBuilder* builder, ogr_feature_uptr& f) {
    const char* from_speed_limit_s = strdup(get_field_from_feature(f, FR_SPEED_LIMIT));
    const char* to_speed_limit_s = strdup(get_field_from_feature(f, TO_SPEED_LIMIT));

    uint from_speed_limit = get_uint_from_feature(f, FR_SPEED_LIMIT);
    uint to_speed_limit = get_uint_from_feature(f, TO_SPEED_LIMIT);

    if (from_speed_limit >= 1000 || to_speed_limit >= 1000)
        throw(format_error(
                "from_speed_limit='" + std::string(from_speed_limit_s) + "' or to_speed_limit='"
                        + std::string(to_speed_limit_s) + "' is not valid (>= 1000)"));

    // 998 is a ramp without speed limit information
    if (from_speed_limit == 998 || to_speed_limit == 998)
        return;

    // 999 means no speed limit at all
    const char* from = from_speed_limit == 999 ? "none" : from_speed_limit_s;
    const char* to = to_speed_limit == 999 ? "none" : to_speed_limit_s;

    if (from_speed_limit != 0 && to_speed_limit != 0) {
        if (from_speed_limit != to_speed_limit) {
            builder->add_tag("maxspeed:forward", from);
            builder->add_tag("maxspeed:backward", to);
        } else {
            builder->add_tag("maxspeed", from);
        }
    } else if (from_speed_limit != 0 && to_speed_limit == 0) {
        builder->add_tag("maxspeed", from);
    } else if (from_speed_limit == 0 && to_speed_limit != 0) {
        builder->add_tag("maxspeed", to);
    }

    if (from_speed_limit > 130 && from_speed_limit < 999)
        std::cerr << "Warning: Found speed limit > 130 (from_speed_limit): " << from_speed_limit << std::endl;
    if (to_speed_limit > 130 && to_speed_limit < 999)
        std::cerr << "Warning: Found speed limit > 130 (to_speed_limit): " << to_speed_limit << std::endl;
}