From ba25914cbe5f3e24ac1afd2091e6d2839144d07a Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 1 Mar 2024 14:13:43 +0100 Subject: [PATCH] confd: add support for changing text-editor backend Signed-off-by: Joachim Wiberg --- src/confd/src/ietf-system.c | 37 +++++++++++++++++++++ src/confd/yang/infix-system@2024-02-29.yang | 29 ++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/confd/src/ietf-system.c b/src/confd/src/ietf-system.c index b9063fce..01d54f48 100644 --- a/src/confd/src/ietf-system.c +++ b/src/confd/src/ietf-system.c @@ -1092,6 +1092,42 @@ static int change_motd_banner(sr_session_ctx_t *session, uint32_t sub_id, const return SR_ERR_OK; } +static int change_editor(sr_session_ctx_t *session, uint32_t sub_id, const char *module, + const char *xpath, sr_event_t event, unsigned request_id, void *priv) +{ + const char *alt = "/etc/alternatives/editor"; + struct { const char *editor, *path; } map[] = { + { "emacs", "/usr/bin/mg" }, + { "nano", "/usr/bin/nano" }, + { "vi", "/bin/vi" }, + }; + char *editor; + int rc = 0; + + /* Ignore all events except SR_EV_DONE */ + if (event != SR_EV_DONE) + return SR_ERR_OK; + + editor = srx_get_str(session, "%s", xpath); + if (editor) { + for (size_t i = 0; i < NELEMS(map); i++) { + if (strcmp(map[i].editor, editor)) + continue; + + erase(alt); + rc = systemf("ln -s %s %s", map[i].path, alt); + if (rc) + ERROR("Failed setting system editor '%s'", map[i].editor); + } + free(editor); + } + + if (rc) + return SR_ERR_SYS; + + return SR_ERR_OK; +} + static int change_hostname(sr_session_ctx_t *session, uint32_t sub_id, const char *module, const char *xpath, sr_event_t event, unsigned request_id, void *priv) { @@ -1190,6 +1226,7 @@ int ietf_system_init(struct confd *confd) REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/hostname", 0, change_hostname, confd, &confd->sub); REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/infix-system:motd", 0, change_motd, confd, &confd->sub); REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/infix-system:motd-banner", 0, change_motd_banner, confd, &confd->sub); + REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/infix-system:text-editor", 0, change_editor, confd, &confd->sub); REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/clock", 0, change_clock, confd, &confd->sub); REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/ntp", 0, change_ntp, confd, &confd->sub); REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/dns-resolver", 0, change_dns, confd, &confd->sub); diff --git a/src/confd/yang/infix-system@2024-02-29.yang b/src/confd/yang/infix-system@2024-02-29.yang index 7db96eb7..4377939c 100644 --- a/src/confd/yang/infix-system@2024-02-29.yang +++ b/src/confd/yang/infix-system@2024-02-29.yang @@ -49,6 +49,27 @@ module infix-system { reference "internal"; } + /* + * Identities + */ + + identity editor-type { + description "Base identity from which specific editor types are derived."; + } + + identity emacs { + description "Micro Emacs clone (mg)."; + base editor-type; + } + identity nano { + description "GNU Nano."; + base editor-type; + } + identity vi { + description "The classic UNIX Visual editor."; + base editor-type; + } + /* * Typedefs */ @@ -92,6 +113,14 @@ module infix-system { timestamp, etc."; type binary; } + + leaf text-editor { + description "Text editor to use in CLI for text-editor command."; + type identityref { + base editor-type; + } + default emacs; + } } augment "/sys:system/sys:authentication/sys:user" {