From e56c8489522e1aebb1309a03d460314138cb35dc Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 16 Aug 2025 20:28:00 +0300 Subject: [PATCH] scm: support --listen flag for uds Support the --listen flag to mu scm, to start listening on a Unix domain socket. --- mu/mu-options.cc | 5 ++- mu/mu-options.hh | 1 + scm/meson.build | 7 ++-- scm/{mu-scm-shell.scm => mu-scm-repl.scm} | 12 ++++++- scm/mu-scm-store.cc | 1 - scm/mu-scm.cc | 41 ++++++++++++++++++----- 6 files changed, 52 insertions(+), 15 deletions(-) rename scm/{mu-scm-shell.scm => mu-scm-repl.scm} (69%) diff --git a/mu/mu-options.cc b/mu/mu-options.cc index 1a11bdb1..b91138eb 100644 --- a/mu/mu-options.cc +++ b/mu/mu-options.cc @@ -632,8 +632,11 @@ sub_server(CLI::App& sub, Options& opts) static void sub_scm(CLI::App& sub, Options& opts) { + sub.add_flag("--listen", opts.scm.listen, + "Start listening on a domain socket"); sub.add_option("script-path", opts.scm.script_path, "Path to script") - ->type_name(""); + ->type_name("") + ->excludes("--listen"); sub.add_option("script-args", opts.scm.params, "Parameters for script") ->type_name(""); } diff --git a/mu/mu-options.hh b/mu/mu-options.hh index ead9ab98..245678dc 100644 --- a/mu/mu-options.hh +++ b/mu/mu-options.hh @@ -259,6 +259,7 @@ struct Options { struct Scm { OptString script_path; /**< Path to script (optional) */ StringVec params; /**< Parameters for script (after "--") */ + bool listen; /**< Whether to start listening on a socket */ } scm; diff --git a/scm/meson.build b/scm/meson.build index f7ef0f70..d0ce3910 100644 --- a/scm/meson.build +++ b/scm/meson.build @@ -17,8 +17,7 @@ mu_scm_dir=join_paths(datadir, 'mu', 'scm') mu_scm_dir_arg='-DMU_SCM_DIR="' + mu_scm_dir + '"' lib_mu_scm=static_library( - 'mu-scm', - [ + 'mu-scm', [ 'mu-scm.cc', 'mu-scm-message.cc', 'mu-scm-mime.cc', @@ -33,7 +32,9 @@ lib_mu_scm=static_library( install: false, cpp_args: [mu_scm_dir_arg]) -install_data(['mu-scm.scm', 'mu-scm-shell.scm'], install_dir : mu_scm_dir) +install_data(['mu-scm.scm', + 'mu-scm-repl.scm'], + install_dir : mu_scm_dir) # note: top-level meson.build defines a dummy replacement for this. mu_scm_dep = declare_dependency( diff --git a/scm/mu-scm-shell.scm b/scm/mu-scm-repl.scm similarity index 69% rename from scm/mu-scm-shell.scm rename to scm/mu-scm-repl.scm index 0efe7f8f..9664071b 100644 --- a/scm/mu-scm-shell.scm +++ b/scm/mu-scm-repl.scm @@ -13,6 +13,16 @@ ;; You should have received a copy of the GNU General Public License ;; along with this program; if not, write to the Free Software Foundation, ;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +(use-modules (system repl server) + (ice-9 threads)) +(use-modules (mu)) + +;; when a socket path is defined, listen on it (blocking) +;; after printing UNIX-CONNECT:\n on stdout +(let ((socket-path (getenv "MU_SCM_SOCKET_PATH"))) + (when socket-path + (format #t "UNIX-CONNECT:~a\n" socket-path) + (run-server + (make-unix-domain-server-socket #:path socket-path)))) (display "Welcome to the mu shell!\n\n") -(use-modules (mu)) diff --git a/scm/mu-scm-store.cc b/scm/mu-scm-store.cc index 44cbcf10..4bf22e27 100644 --- a/scm/mu-scm-store.cc +++ b/scm/mu-scm-store.cc @@ -216,7 +216,6 @@ init_subrs() #pragma GCC diagnostic pop } - void Mu::Scm::init_store(const Store& store) { diff --git a/scm/mu-scm.cc b/scm/mu-scm.cc index 0a0f4e6f..9d460de9 100644 --- a/scm/mu-scm.cc +++ b/scm/mu-scm.cc @@ -85,7 +85,9 @@ make_mu_scm_path(const std::string& fname) { namespace { static std::string mu_scm_path; -static std::string mu_scm_shell_path; +static std::string mu_scm_repl_path; +static std::string mu_scm_socket_path; +constexpr auto SOCKET_PATH_ENV = "MU_SCM_SOCKET_PATH"; } static Result @@ -104,12 +106,11 @@ prepare_run(const Mu::Scm::Config& conf) else return Err(path.error()); - if (const auto path = make_mu_scm_path("mu-scm-shell.scm"); path) - mu_scm_shell_path = *path; + if (const auto path = make_mu_scm_path("mu-scm-repl.scm"); path) + mu_scm_repl_path = *path; else return Err(path.error()); - if (config->options.scm.script_path) { const auto path{config->options.scm.script_path->c_str()}; if (const auto res = ::access(path, R_OK); res != 0) { @@ -121,9 +122,28 @@ prepare_run(const Mu::Scm::Config& conf) return Ok(); } -Result -Mu::Scm::run(const Mu::Scm::Config& conf) { +// make a unique unix-socket path +static std::string +maybe_set_uds_path(bool set) +{ + if (set) { + GRand* grand{g_rand_new()}; + auto path = join_paths(g_get_user_runtime_dir(), + mu_format("mu-scm-socket-{:08x}", + g_rand_int(grand))); + g_rand_free(grand); + g_setenv(SOCKET_PATH_ENV, path.c_str(), 1); + return path; + } else { + g_unsetenv(SOCKET_PATH_ENV); + return {}; + } +} + +Result +Mu::Scm::run(const Mu::Scm::Config& conf) +{ if (const auto res = prepare_run(conf); !res) return Err(res.error()); @@ -151,10 +171,13 @@ Mu::Scm::run(const Mu::Scm::Config& conf) { "-c", cmd.c_str()}) args.emplace_back(arg); } else { - // otherwise, drop us into an interactive shell/repl (and - // shell spec) + // otherwise, drop us into an interactive shell/repl + // or start listening on a domain socket. + mu_scm_socket_path = + maybe_set_uds_path(config->options.scm.listen); + args.emplace_back("--no-auto-compile"); args.emplace_back("-l"); - args.emplace_back(mu_scm_shell_path.c_str()); + args.emplace_back(mu_scm_repl_path.c_str()); } /* ahem...*/ scm_shell(std::size(args), const_cast(args.data()));