/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#include <getopt.h>
-
#include "sd-bus.h"
+#include "ansi-color.h"
#include "alloc-util.h"
#include "build.h"
#include "bus-error.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-table.h"
+#include "help-util.h"
#include "kbd-util.h"
#include "locale-setup.h"
#include "main-func.h"
#include "memory-util.h"
+#include "options.h"
#include "pager.h"
#include "parse-argument.h"
#include "path-util.h"
#include "polkit-agent.h"
-#include "pretty-print.h"
#include "runtime-scope.h"
#include "string-util.h"
#include "strv.h"
return table_print_or_warn(table);
}
+VERB(verb_show_status, "status", NULL, VERB_ANY, 1, VERB_DEFAULT,
+ "Show current locale settings");
static int verb_show_status(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(status_info_clear) StatusInfo info = {};
static const struct bus_properties_map map[] = {
return print_status_info(&info);
}
+VERB(verb_set_locale, "set-locale", "LOCALE...", 2, VERB_ANY, 0,
+ "Set system locale");
static int verb_set_locale(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
return 0;
}
+VERB_NOARG(verb_list_locales, "list-locales",
+ "Show known locales");
static int verb_list_locales(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_strv_free_ char **l = NULL;
int r;
return 0;
}
+VERB(verb_set_vconsole_keymap, "set-keymap", "MAP [MAP]", 2, 3, 0,
+ "Set console and X11 keyboard mappings");
static int verb_set_vconsole_keymap(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
const char *map, *toggle_map;
return 0;
}
+VERB_NOARG(verb_list_vconsole_keymaps, "list-keymaps",
+ "Show known virtual console keyboard mappings");
static int verb_list_vconsole_keymaps(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_strv_free_ char **l = NULL;
int r;
return 0;
}
+VERB(verb_set_x11_keymap, "set-x11-keymap", "LAYOUT [MODEL [VARIANT [OPTIONS]]]", 2, 5, 0,
+ "Set X11 and console keyboard mappings");
static int verb_set_x11_keymap(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
const char *layout, *model, *variant, *options;
return cached;
}
+VERB_NOARG(verb_list_x11_keymaps, "list-x11-keymap-models",
+ "Show known X11 keyboard mapping models");
+VERB_NOARG(verb_list_x11_keymaps, "list-x11-keymap-layouts",
+ "Show known X11 keyboard mapping layouts");
+VERB(verb_list_x11_keymaps, "list-x11-keymap-variants", "[LAYOUT]", VERB_ANY, 2, 0,
+ "Show known X11 keyboard mapping variants");
+VERB_NOARG(verb_list_x11_keymaps, "list-x11-keymap-options",
+ "Show known X11 keyboard mapping options");
static int verb_list_x11_keymaps(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_strv_free_ char **list = NULL;
}
static int help(void) {
- _cleanup_free_ char *link = NULL;
+ _cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL;
int r;
- r = terminal_urlify_man("localectl", "1", &link);
+ r = verbs_get_help_table(&verbs);
if (r < 0)
- return log_oom();
+ return r;
- printf("%s [OPTIONS...] COMMAND ...\n\n"
- "%sQuery or change system locale and keyboard settings.%s\n"
- "\nCommands:\n"
- " status Show current locale settings\n"
- " set-locale LOCALE... Set system locale\n"
- " list-locales Show known locales\n"
- " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
- " list-keymaps Show known virtual console keyboard mappings\n"
- " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
- " Set X11 and console keyboard mappings\n"
- " list-x11-keymap-models Show known X11 keyboard mapping models\n"
- " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
- " list-x11-keymap-variants [LAYOUT]\n"
- " Show known X11 keyboard mapping variants\n"
- " list-x11-keymap-options Show known X11 keyboard mapping options\n"
- "\nOptions:\n"
- " -h --help Show this help\n"
- " --version Show package version\n"
- " -l --full Do not ellipsize output\n"
- " --no-pager Do not pipe output into a pager\n"
- " --no-ask-password Do not prompt for password\n"
- " -H --host=[USER@]HOST Operate on remote host\n"
- " -M --machine=CONTAINER Operate on local container\n"
- " --no-convert Don't convert keyboard mappings\n"
- "\nSee the %s for details.\n",
- program_invocation_short_name,
- ansi_highlight(),
- ansi_normal(),
- link);
+ r = option_parser_get_help_table(&options);
+ if (r < 0)
+ return r;
- return 0;
-}
+ (void) table_sync_column_widths(0, verbs, options);
-static int verb_help(int argc, char *argv[], uintptr_t _data, void *userdata) {
- return help();
-}
+ help_cmdline("[OPTIONS…] COMMAND …");
+ help_abstract("Query or change system locale and keyboard settings.");
-static int parse_argv(int argc, char *argv[]) {
+ help_section("Commands");
+ r = table_print_or_warn(verbs);
+ if (r < 0)
+ return r;
- enum {
- ARG_VERSION = 0x100,
- ARG_NO_PAGER,
- ARG_NO_CONVERT,
- ARG_NO_ASK_PASSWORD
- };
+ help_section("Options");
+ r = table_print_or_warn(options);
+ if (r < 0)
+ return r;
- static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { "version", no_argument, NULL, ARG_VERSION },
- { "full", no_argument, NULL, 'l' },
- { "no-pager", no_argument, NULL, ARG_NO_PAGER },
- { "host", required_argument, NULL, 'H' },
- { "machine", required_argument, NULL, 'M' },
- { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
- { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
- {}
- };
+ help_man_page_reference("localectl", "1");
- int r, c;
+ return 0;
+}
+VERB_COMMON_HELP_HIDDEN(help);
+
+static int parse_argv(int argc, char *argv[], char ***remaining_args) {
assert(argc >= 0);
assert(argv);
+ assert(remaining_args);
- while ((c = getopt_long(argc, argv, "hlH:M:", options, NULL)) >= 0)
+ OptionParser opts = { argc, argv };
+ int r;
+ FOREACH_OPTION_OR_RETURN(c, &opts)
switch (c) {
- case 'h':
+ OPTION_COMMON_HELP:
return help();
- case ARG_VERSION:
+ OPTION_COMMON_VERSION:
return version();
- case 'l':
+ OPTION('l', "full", NULL, "Do not ellipsize output"):
arg_full = true;
break;
- case ARG_NO_CONVERT:
- arg_convert = false;
- break;
-
- case ARG_NO_PAGER:
+ OPTION_COMMON_NO_PAGER:
arg_pager_flags |= PAGER_DISABLE;
break;
- case ARG_NO_ASK_PASSWORD:
+ OPTION_COMMON_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
- case 'H':
+ OPTION_COMMON_HOST:
arg_transport = BUS_TRANSPORT_REMOTE;
- arg_host = optarg;
+ arg_host = opts.arg;
break;
- case 'M':
- r = parse_machine_argument(optarg, &arg_host, &arg_transport);
+ OPTION_COMMON_MACHINE:
+ r = parse_machine_argument(opts.arg, &arg_host, &arg_transport);
if (r < 0)
return r;
break;
- case '?':
- return -EINVAL;
-
- default:
- assert_not_reached();
+ OPTION_LONG("no-convert", NULL, "Don't convert keyboard mappings"):
+ arg_convert = false;
+ break;
}
+ *remaining_args = option_parser_get_args(&opts);
return 1;
}
-static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
-
- static const Verb verbs[] = {
- { "status", VERB_ANY, 1, VERB_DEFAULT, verb_show_status },
- { "set-locale", 2, VERB_ANY, 0, verb_set_locale },
- { "list-locales", VERB_ANY, 1, 0, verb_list_locales },
- { "set-keymap", 2, 3, 0, verb_set_vconsole_keymap },
- { "list-keymaps", VERB_ANY, 1, 0, verb_list_vconsole_keymaps },
- { "set-x11-keymap", 2, 5, 0, verb_set_x11_keymap },
- { "list-x11-keymap-models", VERB_ANY, 1, 0, verb_list_x11_keymaps },
- { "list-x11-keymap-layouts", VERB_ANY, 1, 0, verb_list_x11_keymaps },
- { "list-x11-keymap-variants", VERB_ANY, 2, 0, verb_list_x11_keymaps },
- { "list-x11-keymap-options", VERB_ANY, 1, 0, verb_list_x11_keymaps },
- { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it has been created. */
- {}
- };
-
- return dispatch_verb(argc, argv, verbs, bus);
-}
-
static int run(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
setlocale(LC_ALL, "");
log_setup();
- r = parse_argv(argc, argv);
+ char **args = NULL;
+ r = parse_argv(argc, argv, &args);
if (r <= 0)
return r;
if (r < 0)
return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM);
- return localectl_main(bus, argc, argv);
+ return dispatch_verb_with_args(args, bus);
}
DEFINE_MAIN_FUNCTION(run);