From fc79d703b19a450d2637b9cd1e2e9abf6b689435 Mon Sep 17 00:00:00 2001 From: Tim Stack Date: Fri, 24 May 2024 12:36:13 -0700 Subject: [PATCH] [script] pass full path to script as $0 --- NEWS.md | 2 ++ src/command_executor.cc | 13 ++++--------- ....sh_53a9686102f69b07b034df291f554a00b265ed20.out | 2 +- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/NEWS.md b/NEWS.md index 95607b49..ee60c12a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -34,6 +34,8 @@ Bug Fixes: overlay for log messages. * If a theme does not define `cursor-line` or `selected-text` styles, the styles from the default theme will be used. +* The first argument to a script is now the full path of the + script and not just the script name. Maintenance: * You can now do an `UPDATE` on the `lnav_top_view` SQL view. diff --git a/src/command_executor.cc b/src/command_executor.cc index ce231211..d392a654 100644 --- a/src/command_executor.cc +++ b/src/command_executor.cc @@ -609,7 +609,6 @@ execute_file_contents(exec_context& ec, const std::filesystem::path& path) static const std::filesystem::path stdin_path("-"); static const std::filesystem::path dev_stdin_path("/dev/stdin"); - std::string retval; FILE* file; if (path == stdin_path || path == dev_stdin_path) { @@ -626,13 +625,14 @@ execute_file_contents(exec_context& ec, const std::filesystem::path& path) ssize_t line_size; multiline_executor me(ec, path.string()); + ec.ec_local_vars.top()["0"] = path.string(); ec.ec_path_stack.emplace_back(path.parent_path()); exec_context::output_guard og(ec); while ((line_size = getline(line.out(), &line_max_size, file)) != -1) { TRY(me.push_back(string_fragment::from_bytes(line.in(), line_size))); } - retval = TRY(me.final()); + auto retval = TRY(me.final()); if (file == stdin) { if (isatty(STDOUT_FILENO)) { @@ -677,18 +677,13 @@ execute_file(exec_context& ec, const std::string& path_and_args) auto script_name = split_args[0].se_value; auto& vars = ec.ec_local_vars.top(); - char env_arg_name[32]; std::string star, open_error = "file not found"; add_ansi_vars(vars); - snprintf( - env_arg_name, sizeof(env_arg_name), "%d", (int) split_args.size() - 1); - - vars["#"] = env_arg_name; + vars["#"] = fmt::to_string(split_args.size() - 1); for (size_t lpc = 0; lpc < split_args.size(); lpc++) { - snprintf(env_arg_name, sizeof(env_arg_name), "%lu", lpc); - vars[env_arg_name] = split_args[lpc].se_value; + vars[fmt::to_string(lpc)] = split_args[lpc].se_value; } for (size_t lpc = 1; lpc < split_args.size(); lpc++) { if (lpc > 1) { diff --git a/test/expected/test_cmds.sh_53a9686102f69b07b034df291f554a00b265ed20.out b/test/expected/test_cmds.sh_53a9686102f69b07b034df291f554a00b265ed20.out index 39589f4b..7dd1192d 100644 --- a/test/expected/test_cmds.sh_53a9686102f69b07b034df291f554a00b265ed20.out +++ b/test/expected/test_cmds.sh_53a9686102f69b07b034df291f554a00b265ed20.out @@ -1,2 +1,2 @@ toplevel here 123 456 -nested here nested.lnav abc 789 +nested here {test_dir}/nested.lnav abc 789