Ejemplo n.º 1
0
// Returns true if line is filtered, IOW should not be included.
static bool LineIsFiltered(const ExecConfig *config,
                           const char *line)
{
    // Check whether the line matches mail filters
    int include_filter_len = SeqLength(config->mailfilter_include_regex);
    int exclude_filter_len = SeqLength(config->mailfilter_exclude_regex);
    // Count messages as matched in include set if there is no include set.
    bool included = (include_filter_len == 0);
    bool excluded = false;
    if (include_filter_len > 0)
    {
        for (int i = 0; i < include_filter_len; i++)
        {
            if (StringMatchFullWithPrecompiledRegex(SeqAt(config->mailfilter_include_regex, i),
                                                    line))
            {
                included = true;
            }
        }
    }
    if (exclude_filter_len > 0)
    {
        for (int i = 0; i < exclude_filter_len; i++)
        {
            if (StringMatchFullWithPrecompiledRegex(SeqAt(config->mailfilter_exclude_regex, i),
                                                    line))
            {
                excluded = true;
            }
        }
    }
    return !included || excluded;
}
Ejemplo n.º 2
0
bool RlistMatchesRegex(const Rlist *list, const char *regex)
/*
   Returns true if any of the "list" of strings matches "regex".
   Non-scalars in "list" are skipped.
*/
{
    if (regex == NULL || list == NULL)
    {
        return false;
    }

    pcre *rx = CompileRegex(regex);
    if (!rx)
    {
        return false;
    }

    for (const Rlist *rp = list; rp != NULL; rp = rp->next)
    {
        if (rp->val.type == RVAL_TYPE_SCALAR &&
            StringMatchFullWithPrecompiledRegex(rx, RlistScalarValue(rp)))
        {
            pcre_free(rx);
            return true;
        }
    }

    pcre_free(rx);
    return false;
}
Ejemplo n.º 3
0
static bool IsValidVariableName(const char *var_name)
{
    // must be removed at some point (global), but for now this offers an attractive speedup
    static pcre *rx = NULL;
    if (!rx)
    {
        rx = CompileRegex("[a-zA-Z0-9_\200-\377.]+(\\[.+\\])*");
    }

    return StringMatchFullWithPrecompiledRegex(rx, var_name);
}
Ejemplo n.º 4
0
static bool IsValidVariableName(const char *var_name)
{
    /* TODO: remove at some point (global, leaked), but for now
     * this offers an attractive speedup. */
    static pcre *rx = NULL;
    if (!rx)
    {
        rx = CompileRegex("[a-zA-Z0-9_\200-\377.]+(\\[.+\\])*"); /* Known leak, see TODO. */
    }

    return StringMatchFullWithPrecompiledRegex(rx, var_name);
}
Ejemplo n.º 5
0
Class *ClassTableMatch(const ClassTable *table, const char *regex)
{
    ClassTableIterator *it = ClassTableIteratorNew(table, NULL, true, true);
    Class *cls = NULL;

    pcre *pattern = CompileRegex(regex);
    if (pattern == NULL)
    {
        // TODO: perhaps pcre has can give more info on this error?
        Log(LOG_LEVEL_ERR, "Unable to pcre compile regex '%s'", regex);
        return false;
    }

    while ((cls = ClassTableIteratorNext(it)))
    {
        bool matched;
        if (cls->ns)
        {
            char *class_expr = ClassRefToString(cls->ns, cls->name);
            matched = StringMatchFullWithPrecompiledRegex(pattern, class_expr);
            free(class_expr);
        }
        else
        {
            matched = StringMatchFullWithPrecompiledRegex(pattern, cls->name);
        }

        if (matched)
        {
            break;
        }
    }

    pcre_free(pattern);

    ClassTableIteratorDestroy(it);
    return cls;
}
Ejemplo n.º 6
0
Archivo: regex.c Proyecto: cstroe/core
bool StringMatchFull(const char *regex, const char *str)
{
    pcre *pattern = CompileRegex(regex);

    if (pattern == NULL)
    {
        return false;
    }

    bool ret = StringMatchFullWithPrecompiledRegex(pattern, str);

    pcre_free(pattern);
    return ret;
}
Ejemplo n.º 7
0
int DeleteItemGeneral(Item **list, const char *string, ItemMatchType type)
{
    if (list == NULL)
    {
        return false;
    }

    pcre *rx = NULL;
    if (type == ITEM_MATCH_TYPE_REGEX_COMPLETE_NOT ||
        type == ITEM_MATCH_TYPE_REGEX_COMPLETE)
    {
        rx = CompileRegex(string);
        if (!rx)
        {
            return false;
        }
    }

    Item *ip = *list, *last = NULL;
    CYCLE_DECLARE(ip, slow, toggle);
    while (ip != NULL)
    {
        if (ip->name != NULL)
        {
            bool match = false, flip = false;
            switch (type)
            {
            case ITEM_MATCH_TYPE_LITERAL_START_NOT:
                flip = true; /* and fall through */
            case ITEM_MATCH_TYPE_LITERAL_START:
                match = (strncmp(ip->name, string, strlen(string)) == 0);
                break;

            case ITEM_MATCH_TYPE_LITERAL_COMPLETE_NOT:
                flip = true; /* and fall through */
            case ITEM_MATCH_TYPE_LITERAL_COMPLETE:
                match = (strcmp(ip->name, string) == 0);
                break;

            case ITEM_MATCH_TYPE_LITERAL_SOMEWHERE_NOT:
                flip = true; /* and fall through */
            case ITEM_MATCH_TYPE_LITERAL_SOMEWHERE:
                match = (strstr(ip->name, string) != NULL);
                break;

            case ITEM_MATCH_TYPE_REGEX_COMPLETE_NOT:
                flip = true; /* and fall through */
            case ITEM_MATCH_TYPE_REGEX_COMPLETE:
                match = StringMatchFullWithPrecompiledRegex(rx, ip->name);
                break;
            }
            if (flip)
            {
                match = !match;
            }

            if (match)
            {
                if (ip == *list)
                {
                    *list = ip->next;
                }
                else
                {
                    assert(ip != NULL);
                    assert(last != NULL);
                    assert(last->next == ip);
                    last->next = ip->next;
                }

                free(ip->name);
                free(ip->classes);
                free(ip);
                if (rx)
                {
                    pcre_free(rx);
                }

                return true;
            }
        }
        last = ip;
        ip = ip->next;
        CYCLE_CHECK(ip, slow, toggle);
    }

    if (rx)
    {
        pcre_free(rx);
    }

    return false;
}