コード例 #1
0
ファイル: helper.c プロジェクト: alepez/rofi
static GRegex * create_regex ( const char *input, int case_sensitive )
{
#define R( s )    g_regex_new ( s, G_REGEX_OPTIMIZE | ( ( case_sensitive ) ? 0 : G_REGEX_CASELESS ), 0, NULL )
    GRegex * retv = NULL;
    if ( config.glob ) {
        gchar *r = glob_to_regex ( input );
        retv = R ( r );
        g_free ( r );
    }
    else if ( config.regex ) {
        retv = R ( input );
        if ( retv == NULL ) {
            gchar *r = g_regex_escape_string ( input, -1 );
            retv = R ( r );
            g_free ( r );
        }
    }
    else{
        // TODO; regex should be default?
        gchar *r = g_regex_escape_string ( input, -1 );
        retv = R ( r );
        g_free ( r );
    }
    return retv;
}
コード例 #2
0
ファイル: match-test.c プロジェクト: hangie/amanda
static gboolean
test_glob_to_regex(void)
{
    gboolean ok = TRUE;
    struct { char *glob, *regex; } tests[] = {
	{ "abc", "^abc$" },
	{ "*.txt", "^[^/]*\\.txt$" },
	{ "?.txt", "^[^/]\\.txt$" },
	{ "?*.txt", "^[^/][^/]*\\.txt$" },
	{ "foo.[tT][xX][tT]", "^foo\\.[tT][xX][tT]$" },
	{ "foo.[tT][!yY][tT]", "^foo\\.[tT][^yY][tT]$" },
	{ "foo\\\\", "^foo\\\\$" },
	{ "(){}+.^$|", "^\\(\\)\\{\\}\\+\\.\\^\\$\\|$" },
	{ "/usr/bin/*", "^/usr/bin/[^/]*$" },
	{ NULL, NULL },
    }, *t;

    for (t = tests; t->glob; t++) {
	char *regex = glob_to_regex(t->glob);
	if (!g_str_equal(regex, t->regex)) {
	    ok = FALSE;
	    g_fprintf(stderr, "glob_to_regex(\"%s\") returned \"%s\"; expected \"%s\"\n",
		    t->glob, regex, t->regex);
	}
    }

    return ok;
}
コード例 #3
0
ファイル: set_commands.c プロジェクト: code-mx/amanda
int
cd_glob(
    char *	glob,
    int		verbose)
{
    char *regex;
    char *regex_path;
    char *s;
    char *uqglob;
    int   result;

    char *tpath_on_disk = NULL;

    if (disk_name == NULL) {
	g_printf(_("Must select disk before changing directory\n"));
	return 0;
    }

    uqglob = unquote_string(glob);
    regex = glob_to_regex(uqglob);
    dbprintf(_("cd_glob (%s) -> %s\n"), uqglob, regex);
    if ((s = validate_regexp(regex)) != NULL) {
        g_printf(_("\"%s\" is not a valid shell wildcard pattern: "), glob);
        puts(s);
	amfree(regex);
	amfree(uqglob);
        return 0;
    }
    /*
     * glob_to_regex() anchors the beginning of the pattern with ^,
     * but we will be tacking it onto the end of the current directory
     * in add_file, so strip that off.  Also, it anchors the end with
     * $, but we need to match a trailing /, add it if it is not there
     */
    regex_path = g_strdup(regex + 1);
    amfree(regex);
    if(regex_path[strlen(regex_path) - 2] != '/' ) {
	regex_path[strlen(regex_path) - 1] = '\0';
	strappend(regex_path, "/$");
    }

    /* convert path (assumed in cwd) to one on disk */
    if (g_str_equal(disk_path, "/"))
        tpath_on_disk = g_strconcat("/", regex_path, NULL);
    else {
        char *clean_disk_tpath = clean_regex(disk_tpath, 0);
        tpath_on_disk = g_strjoin(NULL, clean_disk_tpath, "/", regex_path, NULL);
        amfree(clean_disk_tpath);
    }

    result = cd_dir(tpath_on_disk, uqglob, verbose);

    amfree(regex_path);
    amfree(tpath_on_disk);
    amfree(uqglob);

    return result;
}
コード例 #4
0
ファイル: lldiriterator.cpp プロジェクト: Belxjander/Kirito
LLDirIterator::Impl::Impl(const std::string &dirname, const std::string &mask)
	: mIsValid(false)
{
	fs::path dir_path(dirname);

	bool is_dir = false;

	// Check if path is a directory.
	try
	{
		is_dir = fs::is_directory(dir_path);
	}
	catch (const fs::filesystem_error& e)
	{
		LL_WARNS() << e.what() << LL_ENDL;
		return;
	}

	if (!is_dir)
	{
		LL_WARNS() << "Invalid path: \"" << dir_path.string() << "\"" << LL_ENDL;
		return;
	}

	// Initialize the directory iterator for the given path.
	try
	{
		mIter = fs::directory_iterator(dir_path);
	}
	catch (const fs::filesystem_error& e)
	{
		LL_WARNS() << e.what() << LL_ENDL;
		return;
	}

	// Convert the glob mask to a regular expression
	std::string exp = glob_to_regex(mask);

	// Initialize boost::regex with the expression converted from
	// the glob mask.
	// An exception is thrown if the expression is not valid.
	try
	{
		mFilterExp.assign(exp);
	}
	catch (boost::regex_error& e)
	{
		LL_WARNS() << "\"" << exp << "\" is not a valid regular expression: "
				<< e.what() << LL_ENDL;
		return;
	}

	mIsValid = true;
}
コード例 #5
0
ファイル: autocmds.c プロジェクト: cfillion/vifm
/* Registers action handler for a particular combination of event and path
 * pattern.  Event name is case insensitive.  Returns zero on successful
 * registration or non-zero on error. */
static int
add_aucmd(const char event[], const char pattern[], int negated,
		const char action[], vle_aucmd_handler handler)
{
	char canonic_path[PATH_MAX];
	aucmd_info_t *autocmd;
	char *regexp;

	autocmd = DA_EXTEND(autocmds);
	if(autocmd == NULL)
	{
		return 1;
	}

	if(strchr(pattern, '/') != NULL)
	{
		canonicalize_path(pattern, canonic_path, sizeof(canonic_path));
		if(!is_root_dir(canonic_path))
		{
			chosp(canonic_path);
		}
		pattern = canonic_path;
	}

	regexp = glob_to_regex(pattern, 1);
	if(regexp == NULL)
	{
		return 1;
	}

	if(regcomp(&autocmd->regex, regexp, REG_EXTENDED | REG_ICASE) != 0)
	{
		free(regexp);
		return 1;
	}
	free(regexp);

	autocmd->event = strdup(event);
	autocmd->pattern = strdup(pattern);
	autocmd->negated = negated;
	autocmd->action = strdup(action);
	autocmd->handler = handler;
	if(autocmd->event == NULL || autocmd->pattern == NULL ||
			autocmd->action == NULL)
	{
		free_autocmd_data(autocmd);
		return 1;
	}

	DA_COMMIT(autocmds);
	/* TODO: sort by event name (case insensitive) and then by pattern? */
	return 0;
}
コード例 #6
0
ファイル: match.c プロジェクト: Zealsathish/amanda
char *validate_glob(const char *glob)
{
    char *regex, *ret = NULL;
    regex_t regc;
    static regex_errbuf errmsg;

    regex = glob_to_regex(glob);

    if (!do_regex_compile(regex, &regc, &errmsg, TRUE))
        ret = errmsg;

    regfree(&regc);
    g_free(regex);
    return ret;
}
コード例 #7
0
ファイル: lldiriterator.cpp プロジェクト: Xara/kris-clone
LLDirIterator::Impl::Impl(const std::string &dirname, const std::string &mask)
	: mIsValid(false)
{
	fs::path dir_path(dirname);

	// Check if path exists.
	if (!fs::exists(dir_path))
	{
		llwarns << "Invalid path: \"" << dir_path.string() << "\"" << llendl;
		return;
	}

	// Initialize the directory iterator for the given path.
	try
	{
		mIter = fs::directory_iterator(dir_path);
	}
	catch (fs::basic_filesystem_error<fs::path>& e)
	{
		llerrs << e.what() << llendl;
		return;
	}

	// Convert the glob mask to a regular expression
	std::string exp = glob_to_regex(mask);

	// Initialize boost::regex with the expression converted from
	// the glob mask.
	// An exception is thrown if the expression is not valid.
	try
	{
		mFilterExp.assign(exp);
	}
	catch (boost::regex_error& e)
	{
		llerrs << "\"" << exp << "\" is not a valid regular expression: "
				<< e.what() << llendl;
		return;
	}

	mIsValid = true;
}
コード例 #8
0
ファイル: match.c プロジェクト: Zealsathish/amanda
int match_glob(const char *glob, const char *str)
{
    char *regex;
    regex_t *re;
    int result;
    regex_errbuf errmsg;

    regex = glob_to_regex(glob);
    re = get_regex_from_cache(regex, &errmsg, TRUE);

    if (!re)
        error("glob \"%s\" -> regex \"%s\": %s", glob, regex, errmsg);
        /*NOTREACHED*/

    result = try_match(re, str, &errmsg);

    if (result == MATCH_ERROR)
        error("glob \"%s\" -> regex \"%s\": %s", glob, regex, errmsg);
        /*NOTREACHED*/

    g_free(regex);

    return result;
}
コード例 #9
0
ファイル: expand.cpp プロジェクト: Henauxg/minix
bool
impl::matches_glob(const std::string& glob, const std::string& candidate)
{
    return tools::text::match(candidate, glob_to_regex(glob));
}