/*! 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; }
/* * 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; }
/*! 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; }