Beispiel #1
0
static void
test_lex(const char *input)
{
    struct ds output;

    ds_init(&output);
    struct lexer lexer;

    lexer_init(&lexer, input);
    ds_clear(&output);
    while (lexer_get(&lexer) != LEX_T_END) {
        size_t len = output.length;
        lex_token_format(&lexer.token, &output);

        /* Check that the formatted version can really be parsed back
         * losslessly. */
        if (lexer.token.type != LEX_T_ERROR) {
            const char *s = ds_cstr(&output) + len;
            struct lexer l2;

            lexer_init(&l2, s);
            lexer_get(&l2);
            compare_token(&lexer.token, &l2.token);
            lexer_destroy(&l2);
        }
        ds_put_char(&output, ' ');
    }
    lexer_destroy(&lexer);

    ds_chomp(&output, ' ');
    puts(ds_cstr(&output));
    ds_destroy(&output);
}
int smpd_get_string_arg(const char *str, const char *flag, char *val, int maxlen)
{
    smpd_enter_fn(FCNAME);
    if (maxlen < 1)
    {
	smpd_exit_fn(FCNAME);
	return SMPD_FALSE;
    }

    /* line up with the first token */
    str = first_token(str);
    if (str == NULL)
    {
	smpd_exit_fn(FCNAME);
	return SMPD_FALSE;
    }

    /* This loop will match the first instance of "flag = value" in the string. */
    do
    {
	if (compare_token(str, flag) == 0)
	{
	    str = next_token(str);
	    if (compare_token(str, SMPD_DELIM_STR) == 0)
	    {
		str = next_token(str);
		if (str == NULL)
		{
		    smpd_exit_fn(FCNAME);
		    return SMPD_FALSE;
		}
		token_copy(str, val, maxlen);
		smpd_exit_fn(FCNAME);
		return SMPD_TRUE;
	    }
	}
	else
	{
	    str = next_token(str);
	}
    } while (str);
    smpd_exit_fn(FCNAME);
    return SMPD_FALSE;
}
int smpd_hide_string_arg(char *str, const char *flag)
{
    smpd_enter_fn(FCNAME);
    /* line up with the first token */
    str = (char*)first_token(str);
    if (str == NULL)
    {
	smpd_exit_fn(FCNAME);
	return SMPD_SUCCESS;
    }

    do
    {
	if (compare_token(str, flag) == 0)
	{
	    str = (char*)next_token(str);
	    if (compare_token(str, SMPD_DELIM_STR) == 0)
	    {
		str = (char*)next_token(str);
		if (str == NULL)
		{
		    smpd_exit_fn(FCNAME);
		    return SMPD_SUCCESS;
		}
		token_hide(str);
		smpd_exit_fn(FCNAME);
		return SMPD_SUCCESS;
	    }
	}
	else
	{
	    str = (char*)next_token(str);
	}
    } while (str);
    smpd_exit_fn(FCNAME);
    return SMPD_SUCCESS;
}