Beispiel #1
0
/*
 * This is a generic callback printing the variable vector and argument
 */
int
cb(cligen_handle h, cvec *vars, cg_var *arg)
{
    int i=1;
    cg_var *cv;
    char buf[64];

    fprintf(stderr, "variables:\n");
    cv = NULL;
    while ((cv = cvec_each1(vars, cv)) != NULL) {
        cv2str(cv, buf, sizeof(buf)-1);
        fprintf(stderr, "\t%d name:%s type:%s value:%s\n", 
                i, 
                cv_name_get(cv),
                cv_type2str(cv_type_get(cv)),
                buf
            );

    }
    if (arg){
        cv2str(arg, buf, sizeof(buf)-1);
        fprintf(stderr, "argument: %s\n", buf);
    }
    return 0;
}
Beispiel #2
0
/*! General callback for executing shells. 
 * The argument is a command followed by arguments as defined in the input syntax.
 * Simple example:
 *   CLIgen input syntax:     <a type:int32>, cligen_exec_cb("ls ${a}");
 *   CLI input:               > 42
 *   Shell command:           ls 42
 * More advanced example:
 *   CLIgen input syntax:     [<a type:int> | <b type:ipv4addr>], 
 *                                cligen_exec_cb("foo.sh ${a:-99} ${b:-1.2.3.4}");
 *   CLI input:               > 22
 *   Shell command:           foo.sh 22 1.2.3.4
 *   CLI input:               > 2.3.4.5
 *   Shell command:           foo.sh 99 1.2.3.4.
 */
int
cligen_exec_cb(cligen_handle handle, cvec *cvv, cvec *argv)
{
    cg_var *cv = NULL;
    char    buf[64];
    int     pid;
    int     ret;
    int     status;

    if (argv == NULL)
	return 0;
    if ((pid = fork()) == 0){ /* child */
	while ((cv = cvec_each1(cvv, cv)) != NULL) {
	    if (cv_const_get(cv))
		continue;
	    cv2str(cv, buf, sizeof(buf)-1);
	    setenv(cv_name_get(cv), buf, 1 );
	}
	cv2str(cvec_i(argv, 0), buf, sizeof(buf)-1);
	ret = system(buf);
	exit(0);
    }
    /* Wait for child to finish */
    if(waitpid (pid, &status, 0) == pid)
	ret = WEXITSTATUS(status);
    else
	ret = -1;
    return ret;
}
Beispiel #3
0
/*! CLI generic callback printing the variable vector and argument
 */
int
callback(cligen_handle handle, cvec *cvv, cvec *argv)
{
    int     i = 1;
    cg_var *cv;
    char    buf[64];

    fprintf(stderr, "function: %s\n", cligen_fn_str_get(handle));
    fprintf(stderr, "variables:\n");
    cv = NULL;
    while ((cv = cvec_each1(cvv, cv)) != NULL) {
	cv2str(cv, buf, sizeof(buf)-1);
	fprintf(stderr, "\t%d name:%s type:%s value:%s\n", 
		i++, 
		cv_name_get(cv),
		cv_type2str(cv_type_get(cv)),
		buf
	    );
    }
    if (argv){
	    cv = NULL;
	    i=0;
	    while ((cv = cvec_each(argv, cv)) != NULL) {
		cv2str(cv, buf, sizeof(buf)-1);
		fprintf(stderr, "arg %d: %s\n", i++, buf);
	    }
	}
    return 0;
}
Beispiel #4
0
int
expand3(void *h, char *fn_str, cvec *vars, cg_var *arg, 
	cvec *commands, cvec *helptexts)
{
    char buf[64];
    cg_var *cv;

    if (arg)
	cv2str(arg, buf, sizeof(buf)-1);
    /* Interface name expansion. */
    if ((cv = cvec_add(commands, CGV_STRING)) == NULL)
	return -1;
    cv_string_set(cv, "bu0");
    if ((cv = cvec_add(commands, CGV_STRING)) == NULL)
	return -1;
    cv_string_set(cv, "bu1");

    return 0;
}