Removed argc/argv parameters from ca_start plugin API function. You may need to change signatures of your startup in your plugins

This commit is contained in:
Olof hagsand 2019-03-28 17:11:55 +01:00
parent cdbce7645e
commit 60cb87f998
12 changed files with 72 additions and 96 deletions

View file

@ -35,6 +35,28 @@
### API changes on existing features (you may need to change your code)
* Removed argc/argv parameters from ca_start plugin API function:
* You may need to change signatures of your startup in your plugins, eg from:
```
int xxx_start(clicon_handle h, int argc, char **argv)
{
return 0;
}
static clixon_plugin_api xxx_api = {
...
.ca_start = xxx_start,
```
to:
```
int xxx_start(clicon_handle h)
{
return 0;
}
static clixon_plugin_api xxx_api = {
...
.ca_start = xxx_start,
```
* If you use argv/argc use `clicon_argv_get()` in the init function instead.
* Changed hash API for better error handling
* hash_dump, hash_keys, clicon_option_dump have new signatures
* Renamed `xml_insert` to `xml_wrap_all`.

View file

@ -163,23 +163,6 @@ backend_server_socket(clicon_handle h)
return ss;
}
/*! Call plugin_start with -- user options */
static int
plugin_start_useroptions(clicon_handle h,
char *argv0,
int argc,
char **argv)
{
char *tmp;
tmp = *(argv-1);
*(argv-1) = argv0;
if (clixon_plugin_start(h, argc+1, argv-1) < 0)
return -1;
*(argv-1) = tmp;
return 0;
}
/*! Load external NACM file
*/
static int
@ -726,7 +709,7 @@ main(int argc,
clicon_log(LOG_NOTICE, "%s: %u %s", __PROGRAM__, getpid(), cbuf_get(cbret));
/* Call backend plugin_start with user -- options */
if (plugin_start_useroptions(h, argv0, argc, argv) <0)
if (clixon_plugin_start(h) < 0)
goto done;
if (once)
goto done;

View file

@ -540,12 +540,9 @@ main(int argc, char **argv)
cligen_match_cgvar_same(1);
/* Call start function in all plugins before we go interactive
Pass all args after the standard options to plugin_start
*/
tmp = *(argv-1);
*(argv-1) = argv0;
clixon_plugin_start(h, argc+1, argv-1);
*(argv-1) = tmp;
if (clixon_plugin_start(h) < 0)
goto done;
cligen_line_scrolling_set(cli_cligen(h), clicon_option_int(h,"CLICON_CLI_LINESCROLLING"));
/*! Start CLI history and load from file */

View file

@ -350,7 +350,6 @@ main(int argc,
char **argv)
{
int c;
char *tmp;
char *argv0 = argv[0];
int quiet = 0;
clicon_handle h;
@ -511,10 +510,8 @@ main(int argc,
goto done;
/* Call start function is all plugins before we go interactive */
tmp = *(argv-1);
*(argv-1) = argv0;
clixon_plugin_start(h, argc+1, argv-1);
*(argv-1) = tmp;
if (clixon_plugin_start(h) < 0)
goto done;
if (!quiet)
send_hello(h, 1);

View file

@ -520,7 +520,6 @@ main(int argc,
char *path;
clicon_handle h;
char *dir;
char *tmp;
int logdst = CLICON_LOG_SYSLOG;
yang_spec *yspec = NULL;
yang_spec *yspecfg = NULL; /* For config XXX clixon bug */
@ -677,12 +676,9 @@ main(int argc,
yang_spec_parse_module(h, "clixon-rfc5277", NULL, yspec)< 0)
goto done;
/* Call start function in all plugins before we go interactive
Pass all args after the standard options to plugin_start
*/
tmp = *(argv-1);
*(argv-1) = argv0;
clixon_plugin_start(h, argc+1, argv-1);
*(argv-1) = tmp;
if (clixon_plugin_start(h) < 0)
goto done;
if ((sockpath = clicon_option_str(h, "CLICON_RESTCONF_PATH")) == NULL){
clicon_err(OE_CFG, errno, "No CLICON_RESTCONF_PATH in clixon configure file");

View file

@ -496,21 +496,12 @@ example_reset(clicon_handle h,
/*! Plugin start.
* @param[in] h Clicon handle
* @param[in] argc Argument vector length (args after -- to backend_main)
* @param[in] argv Argument vector
*
* plugin_start is called once everything has been initialized, right before
* the main event loop is entered.
* From the cli/backend, command line options can be passed to the
* plugins by using "-- <args>" where <args> is any choice of
* options specific to the application. These options are passed to the
* plugin_start function via the argc and argv arguments which
* can be processed with the standard getopt(3).
*/
int
example_start(clicon_handle h,
int argc,
char **argv)
example_start(clicon_handle h)
{
return 0;
}
@ -555,6 +546,7 @@ clixon_plugin_init(clicon_handle h)
clicon_debug(1, "%s backend", __FUNCTION__);
/* Get user command-line options (after --) */
if (clicon_argv_get(h, &argc, &argv) < 0)
goto done;
opterr = 0;

View file

@ -89,9 +89,7 @@ nacm_statedata(clicon_handle h,
}
int
plugin_start(clicon_handle h,
int argc,
char **argv)
plugin_start(clicon_handle h)
{
return 0;
}

View file

@ -51,7 +51,7 @@
* the main event loop is entered.
*/
int
plugin_start(clicon_handle h, int argc, char **argv)
plugin_start(clicon_handle h)
{
return 0;
}

View file

@ -54,7 +54,7 @@ static const char Base64[] =
static const char Pad64 = '=';
/* Use http basic auth. Set by starting restonf with:
clixon_restconf ... -- -a
* clixon_restconf ... -- -a
*/
static int basic_auth = 0;
@ -298,29 +298,11 @@ restconf_client_rpc(clicon_handle h,
}
/*! Start example restonf plugin. Set authentication method
* Arguments are argc/argv after --
* Currently defined: -a enable http basic authentication
* @note There are three hardwired users andy, wilma and guest from RFC8341 A.1
*/
int
example_restconf_start(clicon_handle h,
int argc,
char **argv)
example_restconf_start(clicon_handle h)
{
int c;
clicon_debug(1, "%s argc:%d", __FUNCTION__, argc);
optind = 1;
opterr = 0;
while ((c = getopt(argc, argv, "a")) != -1){
switch (c) {
case 'a':
basic_auth = 1;
break;
default:
break;
}
}
clicon_debug(1, "%s", __FUNCTION__);
return 0;
}
@ -338,11 +320,31 @@ static clixon_plugin_api api = {
* @param[in] h Clixon handle
* @retval NULL Error with clicon_err set
* @retval api Pointer to API struct
* Arguments are argc/argv after --
* Currently defined: -a enable http basic authentication
* @note There are three hardwired users andy, wilma and guest from RFC8341 A.1
*/
clixon_plugin_api *
clixon_plugin_init(clicon_handle h)
{
int argc; /* command-line options (after --) */
char **argv = NULL;
char c;
clicon_debug(1, "%s restconf", __FUNCTION__);
/* Get user command-line options (after --) */
if (clicon_argv_get(h, &argc, &argv) < 0)
return NULL;
opterr = 0;
optind = 1;
while ((c = getopt(argc, argv, "a")) != -1)
switch (c) {
case 'a':
basic_auth = 1;
break;
default:
break;
}
/* Register local netconf rpc client (note not backend rpc client) */
if (rpc_callback_register(h, restconf_client_rpc, NULL, "urn:example:clixon", "client-rpc") < 0)
return NULL;

View file

@ -98,12 +98,9 @@ typedef int (*clicon_upgrade_cb)(
* Backend see config_plugin.c
*/
/* Called when backend started with cmd-line arguments from daemon call.
* Call plugin start functions with argc/argv multiple arguments.
* Typically the argc/argv are the ones appearing after "--", eg
* clicon_cli -f /etc/clicon.xml -- -a myopt
/* Called when application started (eg after daemon call).
*/
typedef int (plgstart_t)(clicon_handle, int, char **); /* Plugin start */
typedef int (plgstart_t)(clicon_handle); /* Plugin start */
/* Called just before plugin unloaded.
*/
@ -141,7 +138,7 @@ typedef char *(cli_prompthook_t)(clicon_handle, char *mode);
* Note that for STARTUP_ERR and _INVALID, running runs in failsafe mode
* and startup contains the erroneous or invalid database.
* The user should repair the startup and
* (1) restart he backend
* (1) restart the backend
* (2) copy startup to candidate and commit.
*/
enum startup_status{
@ -237,7 +234,7 @@ clixon_plugin *clixon_plugin_find(clicon_handle h, char *name);
int clixon_plugins_load(clicon_handle h, char *function, char *dir, char *regexp);
int clixon_plugin_start(clicon_handle h, int argc, char **argv);
int clixon_plugin_start(clicon_handle h);
int clixon_plugin_exit(clicon_handle h);

View file

@ -1076,7 +1076,7 @@ clicon_argv_get(clicon_handle h,
if (argv){
if ((p = hash_value(cdat, "argv", NULL)) == NULL)
return -1;
*argv = *(char***)p;
*argv = (char**)p;
}
return 0;
}
@ -1099,15 +1099,18 @@ clicon_argv_set(clicon_handle h,
int retval = -1;
clicon_hash_t *cdat = clicon_data(h);
char **argvv = NULL;
size_t len;
/* add space for null-termination and argv[0] program name */
if ((argvv = calloc(argc+2, sizeof(char*))) == NULL){
len = argc+2;
if ((argvv = calloc(len, sizeof(char*))) == NULL){
clicon_err(OE_UNIX, errno, "calloc");
goto done;
}
memcpy(argvv+1, argv, argc*sizeof(char*));
argvv[0] = prgm;
if (hash_add(cdat, "argv", &argvv, sizeof(argvv))==NULL)
/* Note the value is the argv vector (which is copied) */
if (hash_add(cdat, "argv", argvv, len*sizeof(char*))==NULL)
goto done;
argc += 1;
if (hash_add(cdat, "argc", &argc, sizeof(argc))==NULL)

View file

@ -295,21 +295,11 @@ done:
/*! Call plugin_start in all plugins
* @param[in] h Clicon handle
* @param[in] argc
* @param[in] argv
* Call plugin start functions (if defined) with argc/argv multiple
* arguments.
* Typically the argc/argv are the ones appearing after "--", eg
* clicon_cli -f /etc/clicon.xml -- -a myopt
* In the example above argc=3 and
* argv[0]: clicon_cli
* argv[1]: -a
* argv[2]: myopt
* Call plugin start functions (if defined)
* @note Start functions used to have argc/argv. Use clicon_argv_get() instead
*/
int
clixon_plugin_start(clicon_handle h,
int argc,
char **argv)
clixon_plugin_start(clicon_handle h)
{
clixon_plugin *cp;
int i;
@ -319,8 +309,7 @@ clixon_plugin_start(clicon_handle h,
cp = &_clixon_plugins[i];
if ((startfn = cp->cp_api.ca_start) == NULL)
continue;
// optind = 0;
if (startfn(h, argc, argv) < 0) {
if (startfn(h) < 0) {
clicon_debug(1, "plugin_start() failed");
return -1;
}