diff --git a/CHANGELOG.md b/CHANGELOG.md index 878908c2..dee1a374 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/apps/backend/backend_main.c b/apps/backend/backend_main.c index f7aa1247..e77cccb2 100644 --- a/apps/backend/backend_main.c +++ b/apps/backend/backend_main.c @@ -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; diff --git a/apps/cli/cli_main.c b/apps/cli/cli_main.c index cfdabae0..d08449ab 100644 --- a/apps/cli/cli_main.c +++ b/apps/cli/cli_main.c @@ -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 */ diff --git a/apps/netconf/netconf_main.c b/apps/netconf/netconf_main.c index fd056411..e338d687 100644 --- a/apps/netconf/netconf_main.c +++ b/apps/netconf/netconf_main.c @@ -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); diff --git a/apps/restconf/restconf_main.c b/apps/restconf/restconf_main.c index b4f52141..c08edee9 100644 --- a/apps/restconf/restconf_main.c +++ b/apps/restconf/restconf_main.c @@ -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"); diff --git a/example/example_backend.c b/example/example_backend.c index ba6fa9c1..377b87da 100644 --- a/example/example_backend.c +++ b/example/example_backend.c @@ -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 "-- " where 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; diff --git a/example/example_backend_nacm.c b/example/example_backend_nacm.c index 8b9d5388..b2058817 100644 --- a/example/example_backend_nacm.c +++ b/example/example_backend_nacm.c @@ -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; } diff --git a/example/example_netconf.c b/example/example_netconf.c index e5f8fb37..acab6ef0 100644 --- a/example/example_netconf.c +++ b/example/example_netconf.c @@ -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; } diff --git a/example/example_restconf.c b/example/example_restconf.c index c79d45f1..9a245d19 100644 --- a/example/example_restconf.c +++ b/example/example_restconf.c @@ -54,8 +54,8 @@ 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; /* skips all whitespace anywhere. @@ -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; diff --git a/lib/clixon/clixon_plugin.h b/lib/clixon/clixon_plugin.h index 5e9dc68c..9a84d601 100644 --- a/lib/clixon/clixon_plugin.h +++ b/lib/clixon/clixon_plugin.h @@ -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); diff --git a/lib/src/clixon_options.c b/lib/src/clixon_options.c index 456b38a3..c98adc6a 100644 --- a/lib/src/clixon_options.c +++ b/lib/src/clixon_options.c @@ -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) diff --git a/lib/src/clixon_plugin.c b/lib/src/clixon_plugin.c index 45b97e88..768b63c6 100644 --- a/lib/src/clixon_plugin.c +++ b/lib/src/clixon_plugin.c @@ -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; }