예제 #1
0
파일: match_scope.c 프로젝트: awsiv/core
bool ValidateRegEx(const char *regex)
{
    pcre *rx = CompileRegex(regex);
    bool regex_valid = rx != NULL;

    pcre_free(rx);
    return regex_valid;
}
예제 #2
0
Rlist *RlistFromSplitRegex(const char *string, const char *regex, size_t max_entries, bool allow_blanks)
{
    assert(string);
    if (!string)
    {
        return NULL;
    }

    const char *sp = string;
    size_t entry_count = 0;
    int start = 0;
    int end = 0;
    Rlist *result = NULL;
    Buffer *buffer = BufferNewWithCapacity(CF_MAXVARSIZE);

    pcre *rx = CompileRegex(regex);
    if (rx)
    {
        while ((entry_count < max_entries) &&
               StringMatchWithPrecompiledRegex(rx, sp, &start, &end))
        {
            if (end == 0)
            {
                break;
            }

            BufferClear(buffer);
            BufferAppend(buffer, sp, start);

            if (allow_blanks || BufferSize(buffer) > 0)
            {
                RlistAppendScalar(&result, BufferData(buffer));
                entry_count++;
            }

            sp += end;
        }

        pcre_free(rx);
    }

    if (entry_count < max_entries)
    {
        BufferClear(buffer);
        size_t remaining = strlen(sp);
        BufferAppend(buffer, sp, remaining);

        if ((allow_blanks && sp != string) || BufferSize(buffer) > 0)
        {
            RlistAppendScalar(&result, BufferData(buffer));
        }
    }

    BufferDestroy(buffer);

    return result;
}
예제 #3
0
Item *SelectProcesses(const Item *processes, const char *process_name, ProcessSelect a, bool attrselect)
{
    Item *result = NULL;

    if (processes == NULL)
    {
        return result;
    }

    char *names[CF_PROCCOLS];
    int start[CF_PROCCOLS];
    int end[CF_PROCCOLS];

    GetProcessColumnNames(processes->name, &names[0], start, end);

    pcre *rx = CompileRegex(process_name);
    if (rx)
    {
        for (Item *ip = processes->next; ip != NULL; ip = ip->next)
        {
            int s, e;

            if (StringMatchWithPrecompiledRegex(rx, ip->name, &s, &e))
            {
                if (NULL_OR_EMPTY(ip->name))
                {
                    continue;
                }

                if (attrselect && !SelectProcess(ip->name, names, start, end, a))
                {
                    continue;
                }

                pid_t pid = ExtractPid(ip->name, names, end);

                if (pid == -1)
                {
                    Log(LOG_LEVEL_VERBOSE, "Unable to extract pid while looking for %s", process_name);
                    continue;
                }

                PrependItem(&result, ip->name, "");
                result->counter = (int)pid;
            }
        }

        pcre_free(rx);
    }

    for (int i = 0; i < CF_PROCCOLS; i++)
    {
        free(names[i]);
    }

    return result;
}
예제 #4
0
파일: verify_vars.c 프로젝트: awsiv/core
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);
}
예제 #5
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);
}
예제 #6
0
파일: regex.c 프로젝트: 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;
}
예제 #7
0
파일: regex.c 프로젝트: cstroe/core
bool StringMatch(const char *regex, const char *str, int *start, int *end)
{
    pcre *pattern = CompileRegex(regex);

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

    bool ret = StringMatchWithPrecompiledRegex(pattern, str, start, end);

    pcre_free(pattern);
    return ret;

}
예제 #8
0
파일: match_scope.c 프로젝트: cfengine/core
int BlockTextMatch(EvalContext *ctx, const char *regexp, const char *teststring, int *start, int *end)
{
    pcre *rx = CompileRegex(regexp);

    if (rx == NULL)
    {
        return 0;
    }

    if (RegExMatchSubString(ctx, rx, teststring, start, end))
    {
        return true;
    }
    else
    {
        return false;
    }
}
예제 #9
0
/*
 * Splits string on regex, returns a list of (at most max) fragments.
 *
 * NOTE: in contrast with RlistFromSplitRegex() this one will produce at most max number of elements;
 *       last element will contain everything that lefts from original string (we use everything after
 *       the (max-1)-th separator as the final list element, including any separators that may be embedded in it)
 */
Rlist *RlistFromRegexSplitNoOverflow(const char *string, const char *regex, int max)
{
    Rlist *liststart = NULL;
    char node[CF_MAXVARSIZE];
    int start, end;
    int count = 0;

    assert(max > 0); // ensured by FnCallStringSplit() before calling us
    assert(string != NULL); // ensured by FnCallStringSplit() before calling us

    const char *sp = string;
    // We will avoid compiling regex multiple times.
    pcre *pattern = CompileRegex(regex);

    if (pattern == NULL)
    {
        Log(LOG_LEVEL_DEBUG, "Error compiling regex from '%s'", regex);
        return NULL;
    }

    while (count < max - 1 &&
           StringMatchWithPrecompiledRegex(pattern, sp, &start, &end))
    {
        assert(start < CF_MAXVARSIZE);
        memcpy(node, sp, start);
        node[start] = '\0';
        RlistAppendScalar(&liststart, node);
        count++;

        sp += end;
    }

    assert(count < max);
    RlistAppendScalar(&liststart, sp);

    pcre_free(pattern);

    return liststart;
}
예제 #10
0
파일: class.c 프로젝트: maciejmrowiec/core
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;
}
예제 #11
0
파일: match_scope.c 프로젝트: cfengine/core
int FullTextMatch(EvalContext *ctx, const char *regexp, const char *teststring)
{
    pcre *rx;

    if (strcmp(regexp, teststring) == 0)
    {
        return true;
    }

    rx = CompileRegex(regexp);
    if (rx == NULL)
    {
        return false;
    }

    if (RegExMatchFullString(ctx, rx, teststring))
    {
        return true;
    }
    else
    {
        return false;
    }
}
예제 #12
0
/*
 * Format of passwd file on AIX is:
 *
 * user1:
 *         password = hash
 *         lastupdate = 12783612
 * user2:
 *         password = hash
 *         lastupdate = 12783612
 *         <...>
 */
static bool GetAIXShadowHash(const char *puser, const char **result)
{
    FILE *fptr = fopen("/etc/security/passwd", "r");
    if (fptr == NULL)
    {
        return false;
    }

    // Not super pretty with a static variable, but it is how POSIX functions
    // getspnam() and friends do it.
    static char hash_buf[CF_BUFSIZE];

    bool ret = false;
    char *buf = NULL;
    size_t bufsize = 0;
    size_t puser_len = strlen(puser);
    char name_regex_str[strlen(puser) + 3];

    pcre *name_regex = CompileRegex("^(\\S+):");
    pcre *hash_regex = CompileRegex("^\\s+password\\s*=\\s*(\\S+)");
    bool in_user_section = false;

    while (true)
    {
        ssize_t read_result = CfReadLine(&buf, &bufsize, fptr);
        if (read_result < 0)
        {
            if (feof(fptr))
            {
                errno = 0;
            }
            goto end;
        }

        int submatch_vec[6];

        int pcre_result = pcre_exec(name_regex, NULL, buf, strlen(buf), 0, 0, submatch_vec, 6);
        if (pcre_result >= 0)
        {
            if (submatch_vec[3] - submatch_vec[2] == puser_len
                && strncmp(buf + submatch_vec[2], puser, puser_len) == 0)
            {
                in_user_section = true;
            }
            else
            {
                in_user_section = false;
            }
            continue;
        }
        else if (pcre_result != PCRE_ERROR_NOMATCH)
        {
            errno = EINVAL;
            goto end;
        }

        if (!in_user_section)
        {
            continue;
        }

        pcre_result = pcre_exec(hash_regex, NULL, buf, strlen(buf), 0, 0, submatch_vec, 6);
        if (pcre_result >= 0)
        {
            memcpy(hash_buf, buf + submatch_vec[2], submatch_vec[3] - submatch_vec[2]);
            *result = hash_buf;
            ret = true;
            goto end;
        }
        else if (pcre_result != PCRE_ERROR_NOMATCH)
        {
            errno = EINVAL;
            goto end;
        }
    }

end:
    pcre_free(name_regex);
    pcre_free(hash_regex);
    free(buf);
    fclose(fptr);
    return ret;
}
예제 #13
0
// === CODE ===
int main(int argc, char *argv[])
{
	setbuf(stdin, NULL);
	setbuf(stdout, NULL);

	// $X $Y $DIRECTION [$MULTIPLIER=1] $OUTCOME
	CompileRegex(&gRegex_move, "([0-9]+) ([0-9]+) ([A-Z]+)( [0-9]+)? (.*)", REG_EXTENDED);
	// (KILLS|DIES|BOTHDIE) $ATTACKER_RANK $DEFENDER_RANK
	CompileRegex(&gRegex_res, "([A-Z_]+) (.) (.)", REG_EXTENDED);

	{
		 int	colour_id;
		char	colour[6];
		char	opponent[128];
		fscanf(stdin, "%s %s %i %i", colour, opponent, &giBoardWidth, &giBoardHeight);

		if( strcmp(colour, "RED") == 0 )
			colour_id = COLOUR_RED;
		else if( strcmp(colour, "BLUE") == 0 )
			colour_id = COLOUR_BLUE;
		else {
			fprintf(stderr, "Oops... nutty manager, colour = %s\n", colour);
			colour_id = COLOUR_RED;
		}

		DEBUG("colour=%i, opponent='%s', dims = %ix%i", colour_id, opponent, giBoardWidth, giBoardHeight);

		AI_Initialise(colour_id, opponent);
	}
	
	gaBoardState = malloc(giBoardWidth*giBoardHeight);

	for( ;; )
	{
		tMove	mymove, opponent_move;
		char	line[32];

//		DEBUG("Waiting for move");
		ASSERT( fgets(line, sizeof(line), stdin) != NULL );
//		DEBUG("pm line = '%s'", line);

		if( strcmp(line, "\n") == 0 )
			continue ;

		if( strcmp(line, "START\n") == 0 )
		{
//			DEBUG("New game");
			ReadBoardState(stdin, gaBoardState);
			// TODO: Check if this hasn't happened before
			opponent_move.x = 0;
			opponent_move.y = 0;
			opponent_move.dist = 0;
			opponent_move.dir = 0;
		}
		else if( strncmp(line, "QUIT", 4) == 0 )
		{
			// TODO: Result?
			break ;
		}
		else if( strcmp(line, "VICTORY_FLAG\n") == 0 )
		{
			// I win!
			break;
		}
		else
		{
//			DEBUG("GetMove");
			GetMove(line, &opponent_move);
//			DEBUG("Read board state");
			ReadBoardState(stdin, gaBoardState);
		}
		DEBUG("Opposing move %i,%i dir %i dist %i",
			opponent_move.x, opponent_move.y, opponent_move.dir, opponent_move.dist);

		// Silly opponent, you lost
		if( opponent_move.result == RESULT_VICTORY )
			break;

		// Determine move
		AI_HandleMove(0, &opponent_move);
		AI_DoMove(&mymove);
		DEBUG("Chose move %i,%i %i %i", mymove.x, mymove.y, mymove.dir, mymove.dist);
		printf("%i %i %s %i\n", mymove.x, mymove.y, DIR_NAMES[mymove.dir], mymove.dist);

		// Get result of the move
		ASSERT( fgets(line, sizeof(line), stdin) != NULL );
//		DEBUG("res line = '%s'", line);
//
		GetMove(line, &mymove);
		AI_HandleMove(1, &mymove);

		// I WON!
		if( mymove.result == RESULT_VICTORY )
			break;

//		DEBUG("Move over");
	}

	return 0;
}
예제 #14
0
파일: item_lib.c 프로젝트: awsiv/core
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;
}