/*! 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; }
static int CLIgen_callback(cligen_handle h, cvec *vars, cg_var *arg) { char *func; CLIgen_handle ch = (CLIgen_handle)h; PyObject *self = (PyObject *)ch->ch_self; PyObject *Ret; PyObject *Value = NULL; PyObject *Cvec = NULL; PyObject *Capsule = NULL; int retval = -1; PyObject *Arg = NULL; if ((Cvec = PyObject_CallMethod(__cligen_module(), "Cvec", NULL)) == NULL) return -1; if ((Capsule = PyCapsule_New((void *)vars, NULL, NULL)) == NULL) { Py_DECREF(Cvec); return -1; } Ret = PyObject_CallMethod(Cvec, "__Cvec_from_cvec", "O", Capsule); Py_DECREF(Capsule); if (Ret == NULL) { Py_DECREF(Cvec); return -1; } Py_DECREF(Ret); /* arg */ if (arg) Arg = (PyObject *)CgVar_Instance(arg); else { Py_INCREF(Py_None); Arg = Py_None; } /* Run callback */ func = cligen_fn_str_get(ch->ch_cligen); Value = PyObject_CallMethod(self, "_cligen_cb", "sOO", func, Cvec, Arg); if (PyErr_Occurred()) PyErr_Print(); if (Value) { retval = PyLong_AsLong(Value); } Py_DECREF(Arg); Py_DECREF(Cvec); Py_XDECREF(Value); return retval; }