Beispiel #1
0
static char * get_dialog_string(window w)
{
    dialog_data *d = data(w);

    if (d->hit < YES) /* cancelled */
	return NULL;
    del_string(d->result);
    if (d->text)	/* question dialog */
	d->result = new_string(GA_gettext(d->text));

    return d->result;
}
Beispiel #2
0
/////////////////////////////////////
// Funkce get_token
/////////////////////////////////////
int get_token(FILE *f, string *str) {
    int c;
    int ret;
    int state = start;
    char hex[] = "00";
    bool must_be_next = false;
    if(del_string(str) == RET_ERR)
        return RET_ERR;

    while((c=fgetc(f)) != EOF) {
        switch(state) {
        case start:
            ////// identifikátor
            if(isalpha(c) || c == '_') {
                state = identificator;
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
            }
            ////// Proměnná
            else if(c == '$') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                state = variable;
                must_be_next = true;
            }
            ////// Řetězcový literál
            else if(c == '"') {
                state = string_literal;
            }
            ////// číslo
            else if(isdigit(c)) {
                state = int_num;
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
            }
            ////////////////////////
            ////// Rovno, nerovno
            ////// jedno nebo tři rovnítka
            else if(c == '=') {
                state = equal;
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
            }
            ////// nerovno
            else if(c == '!') {
                state = not_equal;
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
            }
            ////// konkatenace - další znaky netřeba
            else if(c == '.') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return concatenate;
            }
            //////////////////
            ////// Logické
            ////// menší || menší nebo rovno
            else if(c == '<') {
                state = less;
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
            }
            ////// větší || větší nebo rovno
            else if(c == '>') {
                state = greater;
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
            }
            //////////////////
            ////// Aritmetické
            ////// pouze znak plus - další znaky netřeba
            else if(c == '+') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return plus;
            }
            ////// pouze znak plus - další znaky netřeba
            else if(c == '*') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return multiply;
            }
            ////// pouze znak mínus - další znaky netřeba
            else if(c == '-') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return minus;
            }
            ////// znak děleno || začátek komentáře
            else if(c == '/') {
                if(first)
                    return SYNT_ERR;
                else {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    state = comment;
                }
            }
            //////////////////
            ////// Závorky
            ////// pouze znak ( - další znaky netřeba
            else if(c == '(') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return left_parent;
            }
            ////// pouze znak ) - další znaky netřeba
            else if(c == ')') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return right_parent;
            }
            ////// pouze znak { - další znaky netřeba
            else if(c == '{') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return left_braces;
            }
            ////// pouze znak } - další znaky netřeba
            else if(c == '}') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return right_braces;
            }
            ////// znak děleno || začátek komentáře
            else if(c == ',') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return comma;
            }
            ////// pouze středník - další znaky netřeba
            else if(c == ';') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return semicolon;
            }
            else if(isspace(c)==0) {
                return LEX_ERR;
            }
            else {
                if(first)
                    return SYNT_ERR;
            }
            break;

        //////////////////
        ////// Identifikátor
        case identificator:
            if(isalpha(c) || isdigit(c) || c == '_') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
            }
            else {
                ungetc(c,f);
                int id = 0;
                if((id = is_keyword(str->strg))!=RET_ERR) {
                    return id;
                }
                else if((id = is_type(str->strg))!=RET_ERR) {
                    return id;
                }
                else if((id = is_const(str->strg))!=RET_ERR) {
                    return id;
                }
                else {
                    return identificator;
                }
            }
            break;

        //////////////////
        ////// Proměnná
        case variable:
            if(must_be_next) {
                if(isalpha(c) || c == '_') {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    must_be_next = false;
                }
                else
                    return LEX_ERR;
            }
            else {
                if(isalpha(c) || isdigit(c) || c == '_') {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                }
                else {
                    ungetc(c,f);
                    return variable;
                }
            }
            break;

        //////////////////
        ////// Řetězcový literál
        case string_literal:
            if(c > 31) {
                if(c == '\\') {
                    c=fgetc(f);
                    if(c == '"') {
                        if((ret = add_string(str,'\"')) == RET_ERR) return RET_ERR;
                    }
                    else if(c == 'n') {
                        if((ret = add_string(str,'\n')) == RET_ERR) return RET_ERR;
                    }
                    else if(c == '$') {
                        if((ret = add_string(str,'$')) == RET_ERR) return RET_ERR;
                    }
                    else if(c == 't') {
                        if((ret = add_string(str,'\t')) == RET_ERR) return RET_ERR;
                    }
                    else if(c == '\\') {
                        if((ret = add_string(str,'\\')) == RET_ERR) return RET_ERR;
                    }
                    else if(c == 'x') {
                        hex[0]=fgetc(f);
                        if(hex[0] == EOF)
                            return LEX_ERR;
                        else if(hex[0] == '\"') {
                            if((ret = add_string(str,'\\')) == RET_ERR) return RET_ERR;
                            if((ret = add_string(str,'x')) == RET_ERR) return RET_ERR;
                            return string_literal;
                        }
                        else if(hex[0] > 31) {
                            if(((hex[0]>'f') || (hex[0]<'a')) && ((hex[0]>'F') || (hex[0]<'A')) && ((hex[0]>'9') || (hex[0]<'0'))) {
                                if((ret = add_string(str,'\\')) == RET_ERR) return RET_ERR;
                                if((ret = add_string(str,'x')) == RET_ERR) return RET_ERR;
                                ungetc(hex[0],f);
                            }
                            else {
                                hex[1]=fgetc(f);
                                if(hex[1] == EOF)
                                    return LEX_ERR;
                                else if(hex[1] == '\"') {
                                    if((ret = add_string(str,'\\')) == RET_ERR) return RET_ERR;
                                    if((ret = add_string(str,'x')) == RET_ERR) return RET_ERR;
                                    if((ret = add_string(str,hex[0])) == RET_ERR) return RET_ERR;
                                    return string_literal;
                                }
                                else if(hex[1] > 31) {
                                    if(((hex[1]>'f') || (hex[1]<'a')) && ((hex[1]>'F') || (hex[1]<'A')) && ((hex[1]>'9') || (hex[1]<'0'))) {
                                        if((ret = add_string(str,'\\')) == RET_ERR) return RET_ERR;
                                        if((ret = add_string(str,'x')) == RET_ERR) return RET_ERR;
                                        ungetc(hex[0],f);
                                        ungetc(hex[1],f);

                                    }
                                    else {
                                        char* end = NULL;
                                        int pomoc = strtol(hex,&end,16);
                                        if(pomoc!=0 && *end == '\0') {
                                            if((ret = add_string(str,pomoc)) == RET_ERR) return RET_ERR;
                                        }

                                    }
                                }
                                else {
                                    return LEX_ERR;
                                }
                            }
                        }
                        else {
                            return LEX_ERR;
                        }
                    }
                    else if(c>31) {
                        ungetc(c,f);
                        c='\\';
                        if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    }
                    else
                        return LEX_ERR;
                }
                else if(c == '"') {
                    return string_literal;
                }
                else if(c == '$') {
                    return LEX_ERR;
                }
                else {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                }
            }
            else
                return LEX_ERR;
            break;

        //////////////////
        ////// Číslo
        case int_num:
            if(isdigit(c)) {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
            }
            else if(c=='.') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                state = double_num;
                must_be_next = true;
            }
            else if(c=='e'||c=='E') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                must_be_next = true;
                state = double_num_exp;
                c=fgetc(f);
                if(c=='-' || c=='+') {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                }
                else
                    ungetc(c,f);
            }
            else {
                ungetc(c,f);
                return int_num;
            }
            break;
        ////// desetinné číslo
        case double_num:
            if(must_be_next) {
                if(isdigit(c)) {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    must_be_next = false;
                }
                else {
                    return LEX_ERR;
                }
            }
            else {
                if(isdigit(c)) {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    must_be_next = false;
                }
                else if(c=='e'||c=='E') {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    state = double_num_exp;
                    c=fgetc(f);
                    must_be_next = true;
                    if(c=='-' || c=='+') {
                        if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    }
                    else
                        ungetc(c,f);
                }
                else {
                    ungetc(c,f);
                    return double_num;
                }
            }
            break;
        ////// desetinné číslo
        case double_num_exp:
            if(must_be_next) {
                if(isdigit(c)) {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    must_be_next = false;
                }
                else {
                    return LEX_ERR;
                }
            }
            else {
                if(isdigit(c)) {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                }
                else {
                    ungetc(c,f);
                    return double_num;
                }
            }
            break;

        //////////////////
        ////// Rovno
        case equal:
            if(c == '=') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                c=fgetc(f);
                if(c == '=') {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    return equal;
                }
                else {
                    ungetc(c,f);
                    return LEX_ERR;
                }
            }
            else {
                ungetc(c,f);
                return assign;
            }
            break;

        //////////////////
        ////// Nerovno
        case not_equal:
            if(c == '=') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                c=fgetc(f);
                if(c == '=') {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    return not_equal;
                }
                else {
                    ungetc(c,f);
                    return LEX_ERR;
                }
            }
            else {
                ungetc(c,f);
                return LEX_ERR;
            }
            break;

        //////////////////
        ////// Větší či většíRovno
        case greater:

            if(c == '=') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return greater_equal;
            }
            else {
                ungetc(c,f);
                return greater;
            }
            break;

        //////////////////
        ////// Menší či menšíRovno
        case less:
            if(c == '=') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                return less_equal;
            }
            else if(c == '?') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                state=php_entry;
            }
            else {
                ungetc(c,f);
                return less;
            }
            break;

        case php_entry:
            if(c == 'p') {
                if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                c=fgetc(f);
                if(c == 'h') {
                    if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                    c=fgetc(f);
                    if(c == 'p') {
                        if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                        c=fgetc(f);
                        if(c==EOF) {
                            return php_entry_wrong;
                        }
                        else  if(isspace(c)) {
                            c=fgetc(f);
                            if(c==EOF) {
                                return php_entry_wrong;
                            }
                            ungetc(c,f);
                            if((ret = add_string(str,c)) == RET_ERR) return RET_ERR;
                            first = false;
                            return php_entry;
                        }

                        else {
                            first = false;
                            return php_entry;
                        }
                    }
                    else {
                        return LEX_ERR;
                    }
                }
                else {
                    return LEX_ERR;
                }
            }
            else {
                ungetc(c,f);
                return LEX_ERR;
            }
            break;
        //////////////////
        ////// Komentáře
        case comment:
            if(c == '/') {
                while((c=fgetc(f)) != EOF) {
                    if(c=='\n')
                        break;
                }
                del_string(str);
                state = start;
            }
            else if(c=='*') {
                while((c=fgetc(f)) != EOF) {
                    if(c=='*') {
                        if((c=fgetc(f)) == '/') {
                            state = start;
                            del_string(str);
                            break;
                        }
                        else
                            ungetc(c,f);
                    }
                }
                if(c==EOF && state==comment)
                    return LEX_ERR;

            }
            else {
                ungetc(c,f);
                return divide;
            }
            break;
        }
    }
    return EOF;
}
Beispiel #3
0
static int do_filter(lua_State *s)
{
    if (!s || lua_gettop(s) < 2 || !lua_istable(s, -1) || !lua_islightuserdata(s, -2))
    {
        return 0;
    }

    struct word_filter_t *flt = (struct word_filter_t *) lua_touserdata(s, -2);
    if (!flt)
    {
        return 0;
    }

    struct hash_tree_node_t *root = _get_node(flt, flt->root), *curr = NULL;
    if (!root)
    {
        // No root node
        return 0;
    }

    const char *content = NULL, *replacement = NULL;
    size_t content_len = 0, n, j, seg_len = 0;
    int all = 0, matched = 0, start = 0, replace = 0;
    unsigned char code;
    BSP_STRING *res = NULL;

    lua_getfield(s, -1, "content");
    if (lua_isstring(s, -1))
    {
        content = lua_tolstring(s, -1, &content_len);
    }
    lua_pop(s, 1);

    lua_getfield(s, -1, "all");
    if (lua_isboolean(s, -1))
    {
        all = lua_toboolean(s, -1);
    }
    lua_pop(s, 1);

    lua_getfield(s, -1, "replace");
    if (lua_isboolean(s, -1))
    {
        replace = lua_toboolean(s, -1);
    }
    lua_pop(s, 1);

    lua_getfield(s, -1, "replacement");
    if (lua_isstring(s, -1))
    {
        replacement = lua_tostring(s, -1);
    }
    lua_pop(s, 1);

    lua_newtable(s);
    if (content && content_len)
    {
        if (replace)
        {
            res = new_string(NULL, 0);
            if (!replacement || !strlen(replacement))
            {
                replacement = DEFAULT_REPLACEMENT;
            }
        }
        
        for (n = 0; n < content_len; n ++)
        {
            if (!all && matched)
            {
                break;
            }
            
            curr = root;
            start = n;
            for (j = n; j <= content_len; j ++)
            {
                if (curr->value)
                {
                    // Get word
                    matched ++;
                    lua_pushinteger(s, matched);
                    lua_newtable(s);
                    lua_pushstring(s, "word");
                    lua_pushlstring(s, content + start, j - start);
                    lua_settable(s, -3);
                    lua_pushstring(s, "offset");
                    lua_pushinteger(s, start);
                    lua_settable(s, -3);
                    lua_pushstring(s, "length");
                    lua_pushinteger(s, j - start);
                    lua_settable(s, -3);
                    lua_pushstring(s, "dsp_length");
                    lua_pushinteger(s, curr->length);
                    lua_settable(s, -3);
                    lua_pushstring(s, "value");
                    lua_pushinteger(s, curr->value);
                    lua_settable(s, -3);
                    lua_settable(s, -3);

                    seg_len = j - start;
                    if (!all)
                    {
                        break;
                    }
                }

                if (j == content_len)
                {
                    break;
                }
                
                code = (unsigned char) content[j];
                if (!curr->path[code])
                {
                    // No match
                    break;
                }
                curr = _get_node(flt, curr->path[code]);
                if (!curr)
                {
                    break;
                }
            }

            if (replace)
            {
                if (seg_len > 0)
                {
                    string_printf(res, "%s", replacement);
                    seg_len --;
                }

                else
                {
                    string_printf(res, "%c", (unsigned char) content[n]);
                }
            }
        }

        if (replace)
        {
            lua_pushstring(s, "replaced");
            lua_pushlstring(s, STR_STR(res), STR_LEN(res));
            lua_settable(s, -3);
            del_string(res);
        }
    }

    return 1;
}