예제 #1
0
static int
test_split_quoted_strings_edge(void)
{
    gboolean success = TRUE;
    struct trial trials[] = {
	{ "", { "", NULL, } },
	{ " ", { "", "", NULL } },
	{ " x", { "", "x", NULL } },
	{ "x ", { "x", "", NULL } },
	{ "x\\ y", { "x y", NULL } },
	{ "\\", { "", NULL } }, /* inv */
	{ "z\\", { "z", NULL } }, /* inv */
	{ "z\"", { "z", NULL } }, /* inv */
	{ "\" \" \"", { " ", "", NULL } }, /* inv */
	{ NULL, { NULL, } },
    };
    struct trial *trial = trials;

    while (trial->combined) {
	char **tokens = split_quoted_strings(trial->combined);

	success = compare_strv(trial->expected, tokens,
			       "split_quoted_strings", trial->combined)
	    && success;

	g_strfreev(tokens);
	trial++;
    }

    return success;
}
예제 #2
0
static int
test_split_quoted_strings(void)
{
    char **iter1, **iter2, **iter3;
    gboolean success = TRUE;
    char *middle_strings[] = {
	"",
	"foo",
	"\"foo\"",
	"sp aces",
	NULL,
    };

    /* the idea here is to loop over all triples of strings, forming a
     * string by quoting them with quote_string and inserting a space, then
     * re-splitting with split_quoted_string.  This should get us back to our
     * starting point. */

    for (iter1 = quotable_strings; *iter1; iter1++) {
	for (iter2 = middle_strings; *iter2; iter2++) {
	    for (iter3 = quotable_strings; *iter3; iter3++) {
		char *q1 = quote_string(*iter1);
		char *q2 = quote_string(*iter2);
		char *q3 = quote_string(*iter3);
		const char *expected[4] = { *iter1, *iter2, *iter3, NULL };
		char *combined = vstralloc(q1, " ", q2, " ", q3, NULL);
		char **tokens;

		tokens = split_quoted_strings(combined);

		success = compare_strv(expected, tokens, "split_quoted_strings", combined)
			&& success;

		amfree(q1);
		amfree(q2);
		amfree(q3);
		amfree(combined);
		g_strfreev(tokens);
	    }
	}
    }

    return success;
}
예제 #3
0
파일: driverio.c 프로젝트: samco/amanda
cmd_t
getresult(
    int fd,
    int show,
    int *result_argc,
    char ***result_argv)
{
    cmd_t t;
    char *line;

    if((line = areads(fd)) == NULL) {
	if(errno) {
	    error(_("reading result from %s: %s"), childstr(fd), strerror(errno));
	    /*NOTREACHED*/
	}
	*result_argv = NULL;
	*result_argc = 0;				/* EOF */
    } else {
	*result_argv = split_quoted_strings(line);
	*result_argc = g_strv_length(*result_argv);
    }

    if(show) {
	g_printf(_("driver: result time %s from %s:"),
	       walltime_str(curclock()),
	       childstr(fd));
	if(line) {
	    g_printf(" %s", line);
	    putchar('\n');
	} else {
	    g_printf(" (eof)\n");
	}
	fflush(stdout);
    }
    amfree(line);

    if(*result_argc < 1) return BOGUS;

    for(t = (cmd_t)(BOGUS+1); t < LAST_TOK; t++)
	if(strcmp((*result_argv)[0], cmdstr[t]) == 0) return t;

    return BOGUS;
}
예제 #4
0
struct cmdargs *
getcmd(void)
{
    char *line;
    cmd_t cmd_i;
    struct cmdargs *cmdargs = g_new0(struct cmdargs, 1);

    if (isatty(0)) {
	g_printf("%s> ", get_pname());
	fflush(stdout);
        line = agets(stdin);
    } else {
        line = areads(0);
    }
    if (line == NULL) {
	line = g_strdup("QUIT");
    }

    dbprintf(_("getcmd: %s\n"), line);

    cmdargs->argv = split_quoted_strings(line);
    cmdargs->argc = g_strv_length(cmdargs->argv);
    cmdargs->cmd = BOGUS;

    amfree(line);

    if (cmdargs->argc < 1) {
	return cmdargs;
    }

    for(cmd_i=BOGUS; cmdstr[cmd_i] != NULL; cmd_i++)
	if(g_str_equal(cmdargs->argv[0], cmdstr[cmd_i])) {
	    cmdargs->cmd = cmd_i;
	    return cmdargs;
	}
    return cmdargs;
}