localectl.c revision ff9b60f38bf68eba4a47cabff14547d92e083214
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2012 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Kay Sievers
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <locale.h>
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering#include <stdlib.h>
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering#include <stdbool.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <getopt.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <string.h>
af49ca27ffd790d78dbbb465b978266dfd5c93daLennart Poettering#include <ftw.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "sd-bus.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "bus-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "bus-error.h"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering#include "util.h"
a51c10485af349eb15faa4d1a63b9818bcf3e589Lennart Poettering#include "spawn-polkit-agent.h"
a51c10485af349eb15faa4d1a63b9818bcf3e589Lennart Poettering#include "build.h"
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering#include "strv.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "pager.h"
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering#include "set.h"
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering#include "def.h"
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering#include "virt.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "fileio.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "locale-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringstatic bool arg_no_pager = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool arg_ask_password = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic char *arg_host = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool arg_convert = true;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringstatic void pager_open_if_enabled(void) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (arg_no_pager)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering pager_open(false);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void polkit_agent_open_if_enabled(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Open the polkit agent as a child process if necessary */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!arg_ask_password)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return;
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (arg_transport != BUS_TRANSPORT_LOCAL)
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering return;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
a51c10485af349eb15faa4d1a63b9818bcf3e589Lennart Poettering polkit_agent_open();
eed857b71702f8551b46b66b31fa0d08583cf23cLennart Poettering}
a51c10485af349eb15faa4d1a63b9818bcf3e589Lennart Poettering
af49ca27ffd790d78dbbb465b978266dfd5c93daLennart Poetteringtypedef struct StatusInfo {
af49ca27ffd790d78dbbb465b978266dfd5c93daLennart Poettering char **locale;
ad6c04756115809d615dede330213d73edf732a8Lennart Poettering const char *vconsole_keymap;
8a516214c4412e8a40544bd725a6d499a30cbbbfLennart Poettering const char *vconsole_keymap_toggle;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering const char *x11_layout;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *x11_model;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering const char *x11_variant;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering const char *x11_options;
b4f1862df2e45aba90386887d685b8bf3c840e10Daniel Mack} StatusInfo;
b4f1862df2e45aba90386887d685b8bf3c840e10Daniel Mack
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void print_overridden_variables(void) {
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering int r;
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering char *variables[_VARIABLE_LC_MAX] = {};
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering LocaleVariable j;
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering bool print_warning = true;
6955a3ba9dc5d36487724878333a5745987e6656Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (detect_container(NULL) > 0 || arg_host)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = parse_env_file("/proc/cmdline", WHITESPACE,
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen "locale.LANG", &variables[VARIABLE_LANG],
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "locale.LANGUAGE", &variables[VARIABLE_LANGUAGE],
011696f76233486bc56c266b18a328924f70269cLennart Poettering "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering "locale.LC_TIME", &variables[VARIABLE_LC_TIME],
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering "locale.LC_NAME", &variables[VARIABLE_LC_NAME],
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
c69fa7e3c44240bedc0ee1bd89fecf954783ac85Lennart Poettering NULL);
c69fa7e3c44240bedc0ee1bd89fecf954783ac85Lennart Poettering
c69fa7e3c44240bedc0ee1bd89fecf954783ac85Lennart Poettering if (r < 0 && r != -ENOENT) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering log_warning_errno(r, "Failed to read /proc/cmdline: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen }
011696f76233486bc56c266b18a328924f70269cLennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering for (j = 0; j < _VARIABLE_LC_MAX; j++)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (variables[j]) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (print_warning) {
log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
" Command Line: %s=%s", locale_variable_to_string(j), variables[j]);
print_warning = false;
} else
log_warning(" %s=%s", locale_variable_to_string(j), variables[j]);
}
finish:
for (j = 0; j < _VARIABLE_LC_MAX; j++)
free(variables[j]);
}
static void print_status_info(StatusInfo *i) {
assert(i);
if (strv_isempty(i->locale))
puts(" System Locale: n/a\n");
else {
char **j;
printf(" System Locale: %s\n", i->locale[0]);
STRV_FOREACH(j, i->locale + 1)
printf(" %s\n", *j);
}
printf(" VC Keymap: %s\n", strna(i->vconsole_keymap));
if (!isempty(i->vconsole_keymap_toggle))
printf("VC Toggle Keymap: %s\n", i->vconsole_keymap_toggle);
printf(" X11 Layout: %s\n", strna(i->x11_layout));
if (!isempty(i->x11_model))
printf(" X11 Model: %s\n", i->x11_model);
if (!isempty(i->x11_variant))
printf(" X11 Variant: %s\n", i->x11_variant);
if (!isempty(i->x11_options))
printf(" X11 Options: %s\n", i->x11_options);
}
static int show_status(sd_bus *bus, char **args, unsigned n) {
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) },
{ "X11Variant", "s", NULL, offsetof(StatusInfo, x11_variant) },
{ "X11Options", "s", NULL, offsetof(StatusInfo, x11_options) },
{ "Locale", "as", NULL, offsetof(StatusInfo, locale) },
{}
};
int r;
assert(bus);
r = bus_map_all_properties(bus,
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
map,
&info);
if (r < 0) {
log_error_errno(r, "Could not get properties: %m");
goto fail;
}
print_overridden_variables();
print_status_info(&info);
fail:
strv_free(info.locale);
return r;
}
static int set_locale(sd_bus *bus, char **args, unsigned n) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
assert(bus);
assert(args);
polkit_agent_open_if_enabled();
r = sd_bus_message_new_method_call(
bus,
&m,
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"SetLocale");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append_strv(m, args + 1);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(m, "b", arg_ask_password);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_call(bus, m, 0, &error, NULL);
if (r < 0) {
log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
return r;
}
return 0;
}
static int list_locales(sd_bus *bus, char **args, unsigned n) {
_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_if_enabled();
strv_print(l);
return 0;
}
static int set_vconsole_keymap(sd_bus *bus, char **args, unsigned n) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
const char *map, *toggle_map;
int r;
assert(bus);
assert(args);
if (n > 3) {
log_error("Too many arguments.");
return -EINVAL;
}
polkit_agent_open_if_enabled();
map = args[1];
toggle_map = n > 2 ? args[2] : "";
r = sd_bus_call_method(
bus,
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"SetVConsoleKeyboard",
&error,
NULL,
"ssbb", map, toggle_map, arg_convert, arg_ask_password);
if (r < 0)
log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
return r;
}
static Set *keymaps = NULL;
static int nftw_cb(
const char *fpath,
const struct stat *sb,
int tflag,
struct FTW *ftwbuf) {
char *p, *e;
int r;
if (tflag != FTW_F)
return 0;
if (!endswith(fpath, ".map") &&
!endswith(fpath, ".map.gz"))
return 0;
p = strdup(basename(fpath));
if (!p)
return log_oom();
e = endswith(p, ".map");
if (e)
*e = 0;
e = endswith(p, ".map.gz");
if (e)
*e = 0;
r = set_consume(keymaps, p);
if (r < 0 && r != -EEXIST)
return log_error_errno(r, "Can't add keymap: %m");
return 0;
}
static int list_vconsole_keymaps(sd_bus *bus, char **args, unsigned n) {
_cleanup_strv_free_ char **l = NULL;
const char *dir;
keymaps = set_new(&string_hash_ops);
if (!keymaps)
return log_oom();
NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS)
nftw(dir, nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
l = set_get_strv(keymaps);
if (!l) {
set_free_free(keymaps);
return log_oom();
}
set_free(keymaps);
if (strv_isempty(l)) {
log_error("Couldn't find any console keymaps.");
return -ENOENT;
}
strv_sort(l);
pager_open_if_enabled();
strv_print(l);
return 0;
}
static int set_x11_keymap(sd_bus *bus, char **args, unsigned n) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
const char *layout, *model, *variant, *options;
int r;
assert(bus);
assert(args);
if (n > 5) {
log_error("Too many arguments.");
return -EINVAL;
}
polkit_agent_open_if_enabled();
layout = args[1];
model = n > 2 ? args[2] : "";
variant = n > 3 ? args[3] : "";
options = n > 4 ? args[4] : "";
r = sd_bus_call_method(
bus,
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"SetX11Keyboard",
&error,
NULL,
"ssssbb", layout, model, variant, options,
arg_convert, arg_ask_password);
if (r < 0)
log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
return r;
}
static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_strv_free_ char **list = NULL;
char line[LINE_MAX];
enum {
NONE,
MODELS,
LAYOUTS,
VARIANTS,
OPTIONS
} 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"))
look_for = MODELS;
else if (streq(args[0], "list-x11-keymap-layouts"))
look_for = LAYOUTS;
else if (streq(args[0], "list-x11-keymap-variants"))
look_for = VARIANTS;
else if (streq(args[0], "list-x11-keymap-options"))
look_for = OPTIONS;
else
assert_not_reached("Wrong parameter");
FOREACH_LINE(line, f, break) {
char *l, *w;
l = strstrip(line);
if (isempty(l))
continue;
if (l[0] == '!') {
if (startswith(l, "! model"))
state = MODELS;
else if (startswith(l, "! layout"))
state = LAYOUTS;
else if (startswith(l, "! variant"))
state = VARIANTS;
else if (startswith(l, "! option"))
state = OPTIONS;
else
state = NONE;
continue;
}
if (state != look_for)
continue;
w = l + strcspn(l, WHITESPACE);
if (n > 1) {
char *e;
if (*w == 0)
continue;
*w = 0;
w++;
w += strspn(w, WHITESPACE);
e = strchr(w, ':');
if (!e)
continue;
*e = 0;
if (!streq(w, args[1]))
continue;
} else
*w = 0;
r = strv_extend(&list, l);
if (r < 0)
return log_oom();
}
if (strv_isempty(list)) {
log_error("Couldn't find any entries.");
return -ENOENT;
}
strv_sort(list);
strv_uniq(list);
pager_open_if_enabled();
strv_print(list);
return 0;
}
static void help(void) {
printf("%s [OPTIONS...] COMMAND ...\n\n"
"Query or change system locale and keyboard settings.\n\n"
" -h --help Show this help\n"
" --version Show package version\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\n"
"Commands:\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"
, program_invocation_short_name);
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
ARG_NO_PAGER,
ARG_NO_CONVERT,
ARG_NO_ASK_PASSWORD
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "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 },
{}
};
int c;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
puts(PACKAGE_STRING);
puts(SYSTEMD_FEATURES);
return 0;
case ARG_NO_CONVERT:
arg_convert = false;
break;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case ARG_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
case 'H':
arg_transport = BUS_TRANSPORT_REMOTE;
arg_host = optarg;
break;
case 'M':
arg_transport = BUS_TRANSPORT_MACHINE;
arg_host = optarg;
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return 1;
}
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 },
};
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);
}
int main(int argc, char*argv[]) {
_cleanup_bus_close_unref_ sd_bus *bus = NULL;
int r;
setlocale(LC_ALL, "");
log_parse_environment();
log_open();
r = parse_argv(argc, argv);
if (r <= 0)
goto finish;
r = bus_open_transport(arg_transport, arg_host, false, &bus);
if (r < 0) {
log_error_errno(r, "Failed to create bus connection: %m");
goto finish;
}
r = localectl_main(bus, argc, argv);
finish:
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}