Пример #1
0
int count_tokens( char *s, TOKTYPES *tt )
{
  int    count = 0;
  char  *scp;
  TOKEN  tok;
  
  scp = (char *)malloc( strlen( s ) * sizeof(char));
  strcpy( scp, s );
  if( NULL != tt )
  {
    tt->usr_comment = 0;
    tt->c_comment = 0;
    tt->alnum = 0;
    tt->string = 0;
    tt-> control = 0;
    tt->invalid = 0;
  }
  tok.actual_token = tok.next_token = scp;
  while( 1 )
  {
    str_token( &tok );
    if( tok.tok_type == TOK_EOS ) break;
    switch( tok.tok_type )
    {
      case TOK_ALNUM:
        if( NULL != tt ) tt->alnum++;
        count++;
        break;
      case TOK_USR_COMMENT:
        if( NULL != tt ) tt->usr_comment++;
        count++;
        break;
      case TOK_C_COMMENT:
        if( NULL != tt ) tt->c_comment++;
        count++;
        break;
      case TOK_STRING:
        if( NULL != tt ) tt->string++;
        count++;
        break;
      case TOK_CONTROL:
        if( NULL != tt ) tt->control++;
        count++;
        break;
      case TOK_INVALID:
        if( NULL != tt ) tt->invalid++;
        count++;
        break;
      default:
        break;
    }
  }
  free( scp );
  return( count );
}
Пример #2
0
int main(int argc, const char ** argv)
{
    unsigned x = 0;
    int argi = 1;
    predef_info ** predefs = 0;
    unsigned predef_count = 0;
    unsigned * i = &first_predef_info.tag;
    unsigned * e = &last_predef_info.tag;
    while (i < e)
    {
        i += 1;
        if (*i == 0x67890DEF)
        {
            predef_count += 1;
            predefs = (predef_info**)realloc(predefs,predef_count*sizeof(predef_info*));
            predefs[predef_count-1] = (predef_info*)i;
        }
    }
    qsort(predefs,predef_count,sizeof(predef_info*),predef_info_compare);
    for (argi = 1; argi < argc; ++argi)
    {
        const char * exp = argv[argi];
        const char * exp_name = str_token(&exp, whitespace);
        const char * exp_op = str_token(&exp, whitespace);
        const char * exp_val = str_token(&exp, whitespace);
        unsigned exp_version = 0;
        if (*exp_val != 0)
        {
            exp = exp_val;
            const char * exp_val_a = str_token(&exp, dot);
            const char * exp_val_b = str_token(&exp, dot);
            const char * exp_val_c = str_token(&exp, dot);
            exp_version = BOOST_VERSION_NUMBER(atoi(exp_val_a), atoi(exp_val_b),atoi(exp_val_c));
        }
        for (x = 0; x < predef_count; ++x)
        {
            if (*exp_op == 0 &&
                predefs[x]->value == 0 &&
                strcmp(exp_name, predefs[x]->name) == 0)
            {
                return argi;
            }
            else if (*exp_op != 0 && *exp_val != 0 &&
                     strcmp(exp_name, predefs[x]->name) == 0)
            {
                if (0 == strcmp(">",exp_op) && !(predefs[x]->value > exp_version)) return argi;
                if (0 == strcmp("<",exp_op) && !(predefs[x]->value < exp_version)) return argi;
                if (0 == strcmp(">=",exp_op) && !(predefs[x]->value >= exp_version)) return argi;
                if (0 == strcmp("<=",exp_op) && !(predefs[x]->value <= exp_version)) return argi;
                if (0 == strcmp("==",exp_op) && !(predefs[x]->value == exp_version)) return argi;
                if (0 == strcmp("!=",exp_op) && !(predefs[x]->value != exp_version)) return argi;
            }
        }
    }
    return 0;
}
Пример #3
0
int main( int argc, char **argv )
{
  int   tt, n = 0;
  char *ptr;
  TOKEN testtok;
  
  printf( "\n\nUTILITY_LIB - DEBUG MODE\n\n" );

  if( argc != 2 )
  {
    printf( "usage: utility_lib \"test string\"\n" );
    return( -1 );
  }
    
  printf( "Test string: <%s>\n\n", *(argv+1) );
  testtok.actual_token = testtok.next_token = *(argv+1);
  while( NULL != ( ptr = str_token( &testtok )))
    printf( "#%d : length=%4d token_type=%2d token=<%s>\n", ++n, testtok.tok_length, testtok.tok_type, ptr );

  return( 0 );
}
Пример #4
0
static int if_addr_parse(GQueue *q, char *s, struct ifaddrs *ifas) {
	str name;
	char *c;
	sockaddr_t *addr, adv;
	GQueue addrs = G_QUEUE_INIT;
	struct intf_config *ifa;

	/* name */
	c = strchr(s, '/');
	if (c) {
		*c++ = 0;
		str_init(&name, s);
		s = c;
	}
	else
		str_init(&name, "default");

	/* advertised address */
	c = strchr(s, '!');
	if (c)
		*c++ = 0;

	/* address */
	addr = g_slice_alloc(sizeof(*addr));
	if (!sockaddr_parse_any(addr, s)) {
		if (is_addr_unspecified(addr))
			return -1;
		g_queue_push_tail(&addrs, addr);
	}
	else {
		// could be an interface name?
		ilog(LOG_DEBUG, "Could not parse '%s' as network address, checking to see if "
				"it's an interface", s);
		for (struct ifaddrs *ifa = ifas; ifa; ifa = ifa->ifa_next) {
			if (strcmp(ifa->ifa_name, s))
				continue;
			if (!(ifa->ifa_flags & IFF_UP))
				continue;
			if (!ifa->ifa_addr)
				continue;
			if (ifa->ifa_addr->sa_family == AF_INET) {
				struct sockaddr_in *sin = (void *) ifa->ifa_addr;
				addr->family = __get_socket_family_enum(SF_IP4);
				addr->u.ipv4 = sin->sin_addr;
			}
			else if (ifa->ifa_addr->sa_family == AF_INET6) {
				struct sockaddr_in6 *sin = (void *) ifa->ifa_addr;
				if (sin->sin6_scope_id)
					continue; // link-local
				addr->family = __get_socket_family_enum(SF_IP6);
				addr->u.ipv6 = sin->sin6_addr;
			}
			else
				continue;

			// got one
			ilog(LOG_DEBUG, "Determined address %s for interface '%s'",
					sockaddr_print_buf(addr), s);
			g_queue_push_tail(&addrs, addr);
			addr = g_slice_alloc(sizeof(*addr));
		}

		// free last unused entry
		g_slice_free1(sizeof(*addr), addr);
	}

	if (!addrs.length) // nothing found
		return -1;

	ZERO(adv);
	if (c) {
		if (sockaddr_parse_any(&adv, c))
			return -1;
		if (is_addr_unspecified(&adv))
			return -1;
	}

	while ((addr = g_queue_pop_head(&addrs))) {
		ifa = g_slice_alloc0(sizeof(*ifa));
		ifa->name = name;
		ifa->local_address.addr = *addr;
		ifa->local_address.type = socktype_udp;
		ifa->advertised_address.addr = adv;
		if (is_addr_unspecified(&ifa->advertised_address.addr))
			ifa->advertised_address.addr = *addr;
		ifa->advertised_address.type = ifa->local_address.type;
		ifa->port_min = rtpe_config.port_min;
		ifa->port_max = rtpe_config.port_max;

		// handle "base:suffix" separation for round-robin selection
		ifa->name_rr_spec = ifa->name;
		str_token(&ifa->name_base, &ifa->name_rr_spec, ':'); // sets name_rr_spec to null string if no ':' found

		g_queue_push_tail(q, ifa);

		g_slice_free1(sizeof(*addr), addr);
	}

	return 0;
}
Пример #5
0
/* Event handler for EV_MEM_SYSTEM_COMMAND.
 * The event data is a string of type 'char *' that needs to be deallocated
 * after processing this event. */
void mem_system_command_handler(int event, void *data)
{
	struct list_t *token_list;

	char *command_line = data;
	char command[MAX_STRING_SIZE];

	/* Get command */
	str_token(command, sizeof command, command_line, 0, " ");
	if (!command[0])
		fatal("%s: invalid command syntax.\n\t> %s",
			__FUNCTION__, command_line);

	/* Commands that need to be processed at the end of the simulation
	 * are ignored here. These are command prefixed with 'CheckXXX'. */
	if (!strncasecmp(command, "Check", 5))
	{
		esim_schedule_end_event(EV_MEM_SYSTEM_END_COMMAND, data);
		return;
	}

	/* Split command in tokens, skip command */
	token_list = str_token_list_create(command_line, " ");
	assert(list_count(token_list));
	str_token_list_shift(token_list);

	/* Command 'SetBlock' */
	if (!strcasecmp(command, "SetBlock"))
	{
		struct mod_t *mod;

		int set;
		int way;
		int tag;

		int set_check;
		int tag_check;

		int state;

		mod = mem_system_command_get_mod(token_list, command_line);
		mem_system_command_get_set_way(token_list, command_line, mod, &set, &way);
		tag = mem_system_command_get_hex(token_list, command_line);
		state = mem_system_command_get_state(token_list, command_line);
		mem_system_command_end(token_list, command_line);

		/* Check that module serves address */
		if (!mod_serves_address(mod, tag))
			fatal("%s: %s: module does not serve address 0x%x.\n\t> %s",
				__FUNCTION__, mod->name, tag, command_line);

		/* Check that tag goes to specified set */
		mod_find_block(mod, tag, &set_check, NULL, &tag_check, NULL);
		if (set != set_check)
			fatal("%s: %s: tag 0x%x belongs to set %d.\n\t> %s",
				__FUNCTION__, mod->name, tag, set_check, command_line);
		if (tag != tag_check)
			fatal("%s: %s: tag should be multiple of block size.\n\t> %s",
				__FUNCTION__, mod->name, command_line);

		/* Set tag */
		cache_set_block(mod->cache, set, way, tag, state);
	}

	/* Command 'SetOwner' */
	else if (!strcasecmp(command, "SetOwner"))
	{
		struct mod_t *mod;
		struct mod_t *owner;

		int set;
		int way;

		int sub_block;
		int owner_index;

		/* Get fields */
		mod = mem_system_command_get_mod(token_list, command_line);
		mem_system_command_get_set_way(token_list, command_line, mod, &set, &way);
		sub_block = mem_system_command_get_sub_block(token_list, command_line, mod, set, way);
		owner = mem_system_command_get_mod(token_list, command_line);
		mem_system_command_end(token_list, command_line);

		/* Check that owner is an immediate higher-level module */
		if (owner)
		{
			if (owner->low_net != mod->high_net || !owner->low_net)
				fatal("%s: %s is not a higher-level module of %s.\n\t> %s",
					__FUNCTION__, owner->name, mod->name, command_line);
		}

		/* Set owner */
		owner_index = owner ? owner->low_net_node->index : -1;
		dir_entry_set_owner(mod->dir, set, way, sub_block, owner_index);
	}

	/* Command 'SetSharers' */
	else if (!strcasecmp(command, "SetSharers"))
	{
		struct mod_t *mod;
		struct mod_t *sharer;

		int set;
		int way;

		int sub_block;

		/* Get first fields */
		mod = mem_system_command_get_mod(token_list, command_line);
		mem_system_command_get_set_way(token_list, command_line, mod, &set, &way);
		sub_block = mem_system_command_get_sub_block(token_list, command_line, mod, set, way);

		/* Get sharers */
		mem_system_command_expect(token_list, command_line);
		dir_entry_clear_all_sharers(mod->dir, set, way, sub_block);
		while (list_count(token_list))
		{
			/* Get sharer */
			sharer = mem_system_command_get_mod(token_list, command_line);
			if (!sharer)
				continue;

			/* Check that sharer is an immediate higher-level module */
			if (sharer->low_net != mod->high_net || !sharer->low_net)
				fatal("%s: %s is not a higher-level module of %s.\n\t> %s",
					__FUNCTION__, sharer->name, mod->name, command_line);

			/* Set sharer */
			dir_entry_set_sharer(mod->dir, set, way, sub_block, sharer->low_net_node->index);
		}
	}

	/* Command 'Access' */
	else if (!strcasecmp(command, "Access"))
	{
		struct mod_t *mod;
		enum mod_access_kind_t access_kind;
		unsigned int addr;
		long long cycle;

		/* Read fields */
		mod = mem_system_command_get_mod(token_list, command_line);
		cycle = mem_system_command_get_cycle(token_list, command_line);
		access_kind = mem_system_command_get_mod_access(token_list, command_line);
		addr = mem_system_command_get_hex(token_list, command_line);

		/* If command is scheduled for later, exit */
		if (cycle > esim_cycle)
		{
			str_token_list_free(token_list);
			esim_schedule_event(EV_MEM_SYSTEM_COMMAND, data, cycle - esim_cycle);
			return;
		}

		/* Access module */
		mod_access(mod, access_kind, addr, NULL, NULL, NULL, NULL);
	}

	/* Command not supported */
	else
		fatal("%s: %s: invalid command.\n\t> %s",
			__FUNCTION__, command, command_line);

	/* Free command */
	free(command_line);
	str_token_list_free(token_list);
}
Пример #6
0
int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
				  const char *non_pref_chan)
{
	char *cmd, *token, *context = NULL;
	struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
	size_t num = 0, size = 0;
	unsigned i;

	wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
		   non_pref_chan ? non_pref_chan : "N/A");

	/*
	 * The shortest channel configuration is 10 characters - commas, 3
	 * colons, and 4 values that one of them (oper_class) is 2 digits or
	 * more.
	 */
	if (!non_pref_chan || os_strlen(non_pref_chan) < 10)
		goto update;

	cmd = os_strdup(non_pref_chan);
	if (!cmd)
		return -1;

	while ((token = str_token(cmd, " ", &context))) {
		struct wpa_mbo_non_pref_channel *chan;
		int ret;
		unsigned int _oper_class;
		unsigned int _chan;
		unsigned int _preference;
		unsigned int _reason;

		if (num == size) {
			size = size ? size * 2 : 1;
			tmp_chans = os_realloc_array(chans, size,
						     sizeof(*chans));
			if (!tmp_chans) {
				wpa_printf(MSG_ERROR,
					   "Couldn't reallocate non_pref_chan");
				goto fail;
			}
			chans = tmp_chans;
		}

		chan = &chans[num];

		ret = sscanf(token, "%u:%u:%u:%u", &_oper_class,
			     &_chan, &_preference, &_reason);
		if (ret != 4 ||
		    _oper_class > 255 || _chan > 255 ||
		    _preference > 255 || _reason > 65535 ) {
			wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
				   token);
			goto fail;
		}
		chan->oper_class = _oper_class;
		chan->chan = _chan;
		chan->preference = _preference;
		chan->reason = _reason;

		if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
						    chan->chan, chan->reason)) {
			wpa_printf(MSG_ERROR,
				   "Invalid non_pref_chan: oper class %d chan %d reason %d",
				   chan->oper_class, chan->chan, chan->reason);
			goto fail;
		}

		for (i = 0; i < num; i++)
			if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
				break;
		if (i != num) {
			wpa_printf(MSG_ERROR,
				   "oper class %d chan %d is duplicated",
				   chan->oper_class, chan->chan);
			goto fail;
		}

		num++;
	}

	os_free(cmd);

	if (chans) {
		qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
		      wpa_non_pref_chan_cmp);
	}

update:
	os_free(wpa_s->non_pref_chan);
	wpa_s->non_pref_chan = chans;
	wpa_s->non_pref_chan_num = num;
	wpas_mbo_non_pref_chan_changed(wpa_s);

	return 0;

fail:
	os_free(chans);
	os_free(cmd);
	return -1;
}