diff options
author | Lubomir Rintel <lkundrak@v3.sk> | 2022-05-04 09:52:22 +0200 |
---|---|---|
committer | Lubomir Rintel <lkundrak@v3.sk> | 2022-06-15 12:13:26 +0200 |
commit | d576f4df44ce24fe6fd0eb27f501683adbdefe11 (patch) | |
tree | 23ea365afe3d44671483817dc97a51b371b71fe7 | |
parent | e2fc81e2119ec658d76059009555cca8f6e0c231 (diff) | |
download | NetworkManager-d576f4df44ce24fe6fd0eb27f501683adbdefe11.tar.gz |
nmcli/devices: make get_device_list() shift argc/argv
Prior to this patch, get_device_list() would give the caller no clue
about how many options did it consume. That is okay -- it would always
process all argument until the end, so the no callers would really care.
In a further patch, I'd like to allow termination of the device name
list (with a "--" arguments), so it will be possible to specify further
arguments.
Let's change the protype of this routine to use pointers to argc/argv,
that it will be possible to adjust them.
-rw-r--r-- | src/nmcli/devices.c | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/src/nmcli/devices.c b/src/nmcli/devices.c index 6fce9de731..1ed253ff94 100644 --- a/src/nmcli/devices.c +++ b/src/nmcli/devices.c @@ -1110,39 +1110,42 @@ nmc_complete_device(NMClient *client, const char *prefix, gboolean wifi_only) } static GSList * -get_device_list(NmCli *nmc, int argc, const char *const *argv) +get_device_list(NmCli *nmc, int *argc, const char *const **argv) { - int arg_num = argc; + int arg_num; + const char *const *arg_ptr; + gs_strfreev char **arg_arr = NULL; - const char *const *arg_ptr = argv; NMDevice **devices; GSList *queue = NULL; NMDevice *device; int i; - if (argc == 0) { - if (nmc->ask) { - gs_free char *line = NULL; + if (*argc == 0 && nmc->ask) { + gs_free char *line = NULL; - line = nmc_readline(&nmc->nmc_config, PROMPT_INTERFACES); - nmc_string_to_arg_array(line, NULL, FALSE, &arg_arr, &arg_num); - arg_ptr = (const char *const *) arg_arr; - } - if (arg_num == 0) { - g_string_printf(nmc->return_text, _("Error: No interface specified.")); - nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; - goto error; - } + line = nmc_readline(&nmc->nmc_config, PROMPT_INTERFACES); + nmc_string_to_arg_array(line, NULL, FALSE, &arg_arr, &arg_num); + arg_ptr = (const char *const *) arg_arr; + + argc = &arg_num; + argv = &arg_ptr; + } + + if (*argc == 0) { + g_string_printf(nmc->return_text, _("Error: No interface specified.")); + nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; + goto error; } devices = nmc_get_devices_sorted(nmc->client); - while (arg_num > 0) { - if (arg_num == 1 && nmc->complete) - complete_device(devices, *arg_ptr, FALSE); + while (*argc > 0) { + if (*argc == 1 && nmc->complete) + complete_device(devices, **argv, FALSE); device = NULL; for (i = 0; devices[i]; i++) { - if (!g_strcmp0(nm_device_get_iface(devices[i]), *arg_ptr)) { + if (!g_strcmp0(nm_device_get_iface(devices[i]), **argv)) { device = devices[i]; break; } @@ -1152,16 +1155,16 @@ get_device_list(NmCli *nmc, int argc, const char *const *argv) if (!g_slist_find(queue, device)) queue = g_slist_prepend(queue, device); else - g_printerr(_("Warning: argument '%s' is duplicated.\n"), *arg_ptr); + g_printerr(_("Warning: argument '%s' is duplicated.\n"), **argv); } else { if (!nmc->complete) - g_printerr(_("Error: Device '%s' not found.\n"), *arg_ptr); + g_printerr(_("Error: Device '%s' not found.\n"), **argv); g_string_printf(nmc->return_text, _("Error: not all devices found.")); nmc->return_value = NMC_RESULT_ERROR_NOT_FOUND; } /* Take next argument */ - next_arg(nmc->ask ? NULL : nmc, &arg_num, &arg_ptr, NULL); + next_arg(nmc->ask ? NULL : nmc, argc, argv, NULL); } g_free(devices); @@ -2623,7 +2626,7 @@ do_devices_disconnect(const NMCCommand *cmd, NmCli *nmc, int argc, const char *c nmc->timeout = 10; next_arg(nmc, &argc, &argv, NULL); - queue = get_device_list(nmc, argc, argv); + queue = get_device_list(nmc, &argc, &argv); if (!queue) return; if (nmc->complete) @@ -2691,7 +2694,7 @@ do_devices_delete(const NMCCommand *cmd, NmCli *nmc, int argc, const char *const nmc->timeout = 10; next_arg(nmc, &argc, &argv, NULL); - queue = get_device_list(nmc, argc, argv); + queue = get_device_list(nmc, &argc, &argv); if (!queue) return; if (nmc->complete) @@ -2901,7 +2904,7 @@ do_devices_monitor(const NMCCommand *cmd, NmCli *nmc, int argc, const char *cons nmc->should_wait++; g_signal_connect(nmc->client, NM_CLIENT_DEVICE_ADDED, G_CALLBACK(device_added), nmc); } else { - GSList *queue = get_device_list(nmc, argc, argv); + GSList *queue = get_device_list(nmc, &argc, &argv); GSList *iter; /* Monitor the specified devices. */ |