Beispiel #1
0
static gboolean
test_validate_regexp(void)
{
    gboolean ok = TRUE;
    struct {
	char *regexp;
	gboolean should_validate;
    } tests[] = {
	{ ".*", TRUE },
	{ "*", FALSE },
	{ "[abc", FALSE },
	{ "(abc", FALSE },
	{ "{1,}", FALSE },
	{ NULL, FALSE },
    }, *t;

    for (t = tests; t->regexp; t++) {
	char *validated_err = validate_regexp(t->regexp);
	if (!validated_err != !!t->should_validate) {
	    ok = FALSE;
	    if (t->should_validate) {
		g_fprintf(stderr, "should have validated regular expr %s: %s\n",
			t->regexp, validated_err);
	    } else {
		g_fprintf(stderr, "unexpectedly validated regular expr %s\n",
			t->regexp);
	    }
	}
    }

    return ok;
}
Beispiel #2
0
int
cd_dir(
    char *tpath_on_disk,
    char *default_dir,
    int	  verbose)
{
    char *dir = NULL;
    int nb_found;
    int result;
    size_t i;

    DIR_ITEM *ditem;

    if (validate_regexp(tpath_on_disk) != NULL) {
	result = set_directory(default_dir, verbose);
	return result;
    }

    nb_found = 0;

    for (ditem=get_dir_list(); ditem!=NULL && nb_found <= 1; 
			       ditem=get_next_dir_item(ditem))
    {
	if (match(tpath_on_disk, ditem->tpath))
	{
	    i = strlen(ditem->tpath);
	    if((i > 0 && ditem->tpath[i-1] == '/')
               || (i > 1 && ditem->tpath[i-2] == '/' && ditem->tpath[i-1] == '.'))
            {   /* It is a directory */
		char *dir1, *dir2;
		nb_found++;
		g_free(dir);
		dir = g_strdup(ditem->path);
		if(dir[strlen(dir)-1] == '/')
		    dir[strlen(dir)-1] = '\0'; /* remove last / */
		/* remove everything before the last / */
		dir1 = strrchr(dir,'/');
		if (dir1) {
		    dir1++;
		    dir2 = g_strdup(dir1);
		    amfree(dir);
		    dir = dir2;
		}
	    }
	}
    }

    if(nb_found==0) {
	result = set_directory(default_dir, verbose);
    }
    else if(nb_found==1) {
	result = set_directory(dir, verbose);
    }
    else {
	g_printf(_("Too many directories matching '%s'\n"), default_dir);
	result = 0;
    }
    amfree(dir);
    return result;
}
Beispiel #3
0
GSList *
cmdline_parse_dumpspecs(
    int argc,
    char **argv,
    int flags)
{
    dumpspec_t *dumpspec = NULL;
    GSList *list = NULL;
    char *errstr;
    char *name;
    int optind = 0;
    enum { ARG_GET_HOST, ARG_GET_DISK, ARG_GET_DATESTAMP, ARG_GET_LEVEL } arg_state = ARG_GET_HOST;

    while (optind < argc) {
        name = argv[optind];
        switch (arg_state) {
            case ARG_GET_HOST:
                arg_state = ARG_GET_DISK;
                dumpspec = dumpspec_new(name, NULL, NULL, NULL, NULL);
		list = g_slist_append(list, (gpointer)dumpspec);
                break;

            case ARG_GET_DISK:
                arg_state = ARG_GET_DATESTAMP;
                dumpspec->disk = g_strdup(name);
                break;

            case ARG_GET_DATESTAMP:
                arg_state = ARG_GET_LEVEL;
		if (!(flags & CMDLINE_PARSE_DATESTAMP)) continue;
                dumpspec->datestamp = g_strdup(name);
                break;

            case ARG_GET_LEVEL:
                arg_state = ARG_GET_HOST;
		if (!(flags & CMDLINE_PARSE_LEVEL)) continue;
                if (name[0] != '\0'
                    && (errstr=validate_regexp(name)) != NULL) {
                    error(_("bad level regex \"%s\": %s\n"), name, errstr);
                }
                dumpspec->level = g_strdup(name);
                break;
        }

	optind++;
    }

    /* if nothing was processed and the caller has requested it, 
     * then add an "empty" element */
    if (list == NULL && (flags & CMDLINE_EMPTY_TO_WILDCARD)) {
        dumpspec = dumpspec_new("", "", 
		(flags & CMDLINE_PARSE_DATESTAMP)?"":NULL,
		(flags & CMDLINE_PARSE_LEVEL)?"":NULL, "");
	list = g_slist_append(list, (gpointer)dumpspec);
    }

    return list;
}
Beispiel #4
0
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;
}
Beispiel #5
0
int
cd_regex(
    char *	regex,
    int		verbose)
{
    char *s;
    char *uq_orig_regex;
    char *uqregex;
    int  len_uqregex;
    int  result;

    char *tpath_on_disk = NULL;

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

    uq_orig_regex = unquote_string(regex);
    uqregex = g_strdup(uq_orig_regex);

    /* Add a terminating '/' if it is not there, maybe before a '$' */
    len_uqregex = strlen(uqregex);
    if (uqregex[len_uqregex-1] == '$') {
	if (uqregex[len_uqregex-2] != '/') {
	    uqregex[len_uqregex-1] = '\0';
	    strappend(uqregex, "/$");
	}
    } else if (uqregex[len_uqregex-1] != '/') {
	//uqregex[len_uqregex-1] = '\0';
	strappend(uqregex, "/");
    }
    if ((s = validate_regexp(uqregex)) != NULL) {
	g_printf(_("\"%s\" is not a valid regular expression: "), uq_orig_regex);
	amfree(uqregex);
	amfree(uq_orig_regex);
	puts(s);
	return 0;
    }

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

    result = cd_dir(tpath_on_disk, uq_orig_regex, verbose);

    amfree(tpath_on_disk);
    amfree(uqregex);
    amfree(uq_orig_regex);

    return result;
}
Beispiel #6
0
void
cd_regex(
    char *	regex,
    int		verbose)
{
    char *s;
    char *uq_orig_regex;
    char *uqregex;
    int  len_uqregex;

    char *path_on_disk = NULL;

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

    uq_orig_regex = unquote_string(regex);
    uqregex = stralloc(uq_orig_regex);

    /* Add a terminating '/' if it is not there, maybe before a '$' */
    len_uqregex = strlen(uqregex);
    if (uqregex[len_uqregex-1] == '$') {
	if (uqregex[len_uqregex-2] != '/') {
	    uqregex[len_uqregex-1] = '\0';
	    strappend(uqregex, "/$");
	}
    } else if (uqregex[len_uqregex-1] != '/') {
	//uqregex[len_uqregex-1] = '\0';
	strappend(uqregex, "/");
    }
    if ((s = validate_regexp(uqregex)) != NULL) {
	g_printf(_("\"%s\" is not a valid regular expression: "), uq_orig_regex);
	amfree(uqregex);
	amfree(uq_orig_regex);
	puts(s);
	return;
    }

    /* convert path (assumed in cwd) to one on disk */
    if (strcmp(disk_path, "/") == 0)
        path_on_disk = stralloc2("/", uqregex);
    else {
        char *clean_disk_path = clean_regex(disk_path);
        path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
        amfree(clean_disk_path);
    }

    cd_dir(path_on_disk, uq_orig_regex, verbose);

    amfree(path_on_disk);
    amfree(uqregex);
    amfree(uq_orig_regex);
}
Beispiel #7
0
void
cd_regex(
    char *	regex)
{
    char *s;
    char *uqregex;

    char *path_on_disk = NULL;

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

    uqregex = unquote_string(regex);
    if ((s = validate_regexp(uqregex)) != NULL) {
	g_printf(_("\"%s\" is not a valid regular expression: "), uqregex);
	amfree(uqregex);
	puts(s);
	return;
    }

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

    cd_dir(path_on_disk, uqregex);

    amfree(path_on_disk);
    amfree(uqregex);
}