summaryrefslogtreecommitdiff
path: root/src/locale/localectl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/locale/localectl.c')
-rw-r--r--src/locale/localectl.c221
1 files changed, 60 insertions, 161 deletions
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
index af39e431f5..b3ad2820d9 100644
--- a/src/locale/localectl.c
+++ b/src/locale/localectl.c
@@ -1,23 +1,4 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
- Copyright 2013 Kay Sievers
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
#include <ftw.h>
#include <getopt.h>
@@ -39,6 +20,7 @@
#include "spawn-polkit-agent.h"
#include "strv.h"
#include "util.h"
+#include "verbs.h"
#include "virt.h"
static bool arg_no_pager = false;
@@ -49,23 +31,17 @@ static bool arg_convert = true;
typedef struct StatusInfo {
char **locale;
- char *vconsole_keymap;
- char *vconsole_keymap_toggle;
- char *x11_layout;
- char *x11_model;
- char *x11_variant;
- char *x11_options;
+ const char *vconsole_keymap;
+ const char *vconsole_keymap_toggle;
+ const char *x11_layout;
+ const char *x11_model;
+ const char *x11_variant;
+ const char *x11_options;
} StatusInfo;
static void status_info_clear(StatusInfo *info) {
if (info) {
strv_free(info->locale);
- free(info->vconsole_keymap);
- free(info->vconsole_keymap_toggle);
- free(info->x11_layout);
- free(info->x11_model);
- free(info->x11_variant);
- free(info->x11_options);
zero(*info);
}
}
@@ -79,7 +55,7 @@ static void print_overridden_variables(void) {
if (detect_container() > 0 || arg_host)
return;
- r = parse_env_file("/proc/cmdline", WHITESPACE,
+ r = parse_env_file(NULL, "/proc/cmdline", WHITESPACE,
"locale.LANG", &variables[VARIABLE_LANG],
"locale.LANGUAGE", &variables[VARIABLE_LANGUAGE],
"locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
@@ -142,11 +118,10 @@ static void print_status_info(StatusInfo *i) {
printf(" X11 Options: %s\n", i->x11_options);
}
-static int show_status(sd_bus *bus, char **args, unsigned n) {
+static int show_status(int argc, char **argv, void *userdata) {
_cleanup_(status_info_clear) StatusInfo info = {};
static const struct bus_properties_map map[] = {
{ "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
- { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
{ "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) },
{ "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) },
{ "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) },
@@ -157,6 +132,8 @@ static int show_status(sd_bus *bus, char **args, unsigned n) {
};
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
+ sd_bus *bus = userdata;
int r;
assert(bus);
@@ -165,7 +142,9 @@ static int show_status(sd_bus *bus, char **args, unsigned n) {
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
map,
+ 0,
&error,
+ &m,
&info);
if (r < 0)
return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
@@ -176,13 +155,13 @@ static int show_status(sd_bus *bus, char **args, unsigned n) {
return r;
}
-static int set_locale(sd_bus *bus, char **args, unsigned n) {
+static int set_locale(int argc, char **argv, 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;
+ sd_bus *bus = userdata;
int r;
assert(bus);
- assert(args);
polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
@@ -196,7 +175,7 @@ static int set_locale(sd_bus *bus, char **args, unsigned n) {
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_append_strv(m, args + 1);
+ r = sd_bus_message_append_strv(m, argv + 1);
if (r < 0)
return bus_log_create_error(r);
@@ -213,39 +192,32 @@ static int set_locale(sd_bus *bus, char **args, unsigned n) {
return 0;
}
-static int list_locales(sd_bus *bus, char **args, unsigned n) {
+static int list_locales(int argc, char **argv, void *userdata) {
_cleanup_strv_free_ char **l = NULL;
int r;
- assert(args);
-
r = get_locales(&l);
if (r < 0)
return log_error_errno(r, "Failed to read list of locales: %m");
- pager_open(arg_no_pager, false);
+ (void) pager_open(arg_no_pager, false);
strv_print(l);
return 0;
}
-static int set_vconsole_keymap(sd_bus *bus, char **args, unsigned n) {
+static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
const char *map, *toggle_map;
+ sd_bus *bus = userdata;
int r;
assert(bus);
- assert(args);
-
- if (n > 3) {
- log_error("Too many arguments.");
- return -EINVAL;
- }
polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
- map = args[1];
- toggle_map = n > 2 ? args[2] : "";
+ map = argv[1];
+ toggle_map = argc > 2 ? argv[2] : "";
r = sd_bus_call_method(
bus,
@@ -262,42 +234,33 @@ static int set_vconsole_keymap(sd_bus *bus, char **args, unsigned n) {
return r;
}
-static int list_vconsole_keymaps(sd_bus *bus, char **args, unsigned n) {
+static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
_cleanup_strv_free_ char **l = NULL;
int r;
- assert(args);
-
r = get_keymaps(&l);
if (r < 0)
return log_error_errno(r, "Failed to read list of keymaps: %m");
- pager_open(arg_no_pager, false);
+ (void) pager_open(arg_no_pager, false);
strv_print(l);
return 0;
}
-static int set_x11_keymap(sd_bus *bus, char **args, unsigned n) {
+static int set_x11_keymap(int argc, char **argv, void *userdata) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
const char *layout, *model, *variant, *options;
+ sd_bus *bus = userdata;
int r;
- assert(bus);
- assert(args);
-
- if (n > 5) {
- log_error("Too many arguments.");
- return -EINVAL;
- }
-
polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
- layout = args[1];
- model = n > 2 ? args[2] : "";
- variant = n > 3 ? args[3] : "";
- options = n > 4 ? args[4] : "";
+ layout = argv[1];
+ model = argc > 2 ? argv[2] : "";
+ variant = argc > 3 ? argv[3] : "";
+ options = argc > 4 ? argv[4] : "";
r = sd_bus_call_method(
bus,
@@ -315,7 +278,7 @@ static int set_x11_keymap(sd_bus *bus, char **args, unsigned n) {
return r;
}
-static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
+static int list_x11_keymaps(int argc, char **argv, void *userdata) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_strv_free_ char **list = NULL;
char line[LINE_MAX];
@@ -328,22 +291,17 @@ static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
} state = NONE, look_for;
int r;
- if (n > 2) {
- log_error("Too many arguments.");
- return -EINVAL;
- }
-
f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
if (!f)
return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
- if (streq(args[0], "list-x11-keymap-models"))
+ if (streq(argv[0], "list-x11-keymap-models"))
look_for = MODELS;
- else if (streq(args[0], "list-x11-keymap-layouts"))
+ else if (streq(argv[0], "list-x11-keymap-layouts"))
look_for = LAYOUTS;
- else if (streq(args[0], "list-x11-keymap-variants"))
+ else if (streq(argv[0], "list-x11-keymap-variants"))
look_for = VARIANTS;
- else if (streq(args[0], "list-x11-keymap-options"))
+ else if (streq(argv[0], "list-x11-keymap-options"))
look_for = OPTIONS;
else
assert_not_reached("Wrong parameter");
@@ -376,7 +334,7 @@ static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
w = l + strcspn(l, WHITESPACE);
- if (n > 1) {
+ if (argc > 1) {
char *e;
if (*w == 0)
@@ -392,7 +350,7 @@ static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
*e = 0;
- if (!streq(w, args[1]))
+ if (!streq(w, argv[1]))
continue;
} else
*w = 0;
@@ -410,13 +368,13 @@ static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
strv_sort(list);
strv_uniq(list);
- pager_open(arg_no_pager, false);
+ (void) pager_open(arg_no_pager, false);
strv_print(list);
return 0;
}
-static void help(void) {
+static int help(void) {
printf("%s [OPTIONS...] COMMAND ...\n\n"
"Query or change system locale and keyboard settings.\n\n"
" -h --help Show this help\n"
@@ -440,6 +398,12 @@ static void help(void) {
" Show known X11 keyboard mapping variants\n"
" list-x11-keymap-options Show known X11 keyboard mapping options\n"
, program_invocation_short_name);
+
+ return 0;
+}
+
+static int verb_help(int argc, char **argv, void *userdata) {
+ return help();
}
static int parse_argv(int argc, char *argv[]) {
@@ -472,8 +436,7 @@ static int parse_argv(int argc, char *argv[]) {
switch (c) {
case 'h':
- help();
- return 0;
+ return help();
case ARG_VERSION:
return version();
@@ -512,86 +475,22 @@ static int parse_argv(int argc, char *argv[]) {
static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
- static const struct {
- const char* verb;
- const enum {
- MORE,
- LESS,
- EQUAL
- } argc_cmp;
- const int argc;
- int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
- } verbs[] = {
- { "status", LESS, 1, show_status },
- { "set-locale", MORE, 2, set_locale },
- { "list-locales", EQUAL, 1, list_locales },
- { "set-keymap", MORE, 2, set_vconsole_keymap },
- { "list-keymaps", EQUAL, 1, list_vconsole_keymaps },
- { "set-x11-keymap", MORE, 2, set_x11_keymap },
- { "list-x11-keymap-models", EQUAL, 1, list_x11_keymaps },
- { "list-x11-keymap-layouts", EQUAL, 1, list_x11_keymaps },
- { "list-x11-keymap-variants", LESS, 2, list_x11_keymaps },
- { "list-x11-keymap-options", EQUAL, 1, list_x11_keymaps },
+ static const Verb verbs[] = {
+ { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
+ { "set-locale", 2, VERB_ANY, 0, set_locale },
+ { "list-locales", VERB_ANY, 1, 0, list_locales },
+ { "set-keymap", 2, 3, 0, set_vconsole_keymap },
+ { "list-keymaps", VERB_ANY, 1, 0, list_vconsole_keymaps },
+ { "set-x11-keymap", 2, 5, 0, set_x11_keymap },
+ { "list-x11-keymap-models", VERB_ANY, 1, 0, list_x11_keymaps },
+ { "list-x11-keymap-layouts", VERB_ANY, 1, 0, list_x11_keymaps },
+ { "list-x11-keymap-variants", VERB_ANY, 2, 0, list_x11_keymaps },
+ { "list-x11-keymap-options", VERB_ANY, 1, 0, list_x11_keymaps },
+ { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
+ {}
};
- int left;
- unsigned i;
-
- assert(argc >= 0);
- assert(argv);
-
- left = argc - optind;
-
- if (left <= 0)
- /* Special rule: no arguments means "status" */
- i = 0;
- else {
- if (streq(argv[optind], "help")) {
- help();
- return 0;
- }
-
- for (i = 0; i < ELEMENTSOF(verbs); i++)
- if (streq(argv[optind], verbs[i].verb))
- break;
-
- if (i >= ELEMENTSOF(verbs)) {
- log_error("Unknown operation %s", argv[optind]);
- return -EINVAL;
- }
- }
-
- switch (verbs[i].argc_cmp) {
-
- case EQUAL:
- if (left != verbs[i].argc) {
- log_error("Invalid number of arguments.");
- return -EINVAL;
- }
-
- break;
-
- case MORE:
- if (left < verbs[i].argc) {
- log_error("Too few arguments.");
- return -EINVAL;
- }
-
- break;
-
- case LESS:
- if (left > verbs[i].argc) {
- log_error("Too many arguments.");
- return -EINVAL;
- }
-
- break;
-
- default:
- assert_not_reached("Unknown comparison operator.");
- }
-
- return verbs[i].dispatch(bus, argv + optind, left);
+ return dispatch_verb(argc, argv, verbs, bus);
}
int main(int argc, char*argv[]) {