From 688a28c1d29bbcd5427815aa6a223b497fc63c24 Mon Sep 17 00:00:00 2001 From: David Adam Date: Mon, 29 May 2023 09:05:10 +0800 Subject: [PATCH] Rewrite and adopt print_help in Rust --- CMakeLists.txt | 4 ++-- fish-rust/build.rs | 1 + fish-rust/src/lib.rs | 1 + fish-rust/src/print_help.rs | 33 +++++++++++++++++++++++++++++++++ src/fish_indent.cpp | 4 ++-- src/fish_key_reader.cpp | 4 ++-- src/print_help.cpp | 24 ------------------------ src/print_help.h | 8 -------- 8 files changed, 41 insertions(+), 38 deletions(-) create mode 100644 fish-rust/src/print_help.rs delete mode 100644 src/print_help.cpp delete mode 100644 src/print_help.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 81de7c106..969d7e914 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -198,12 +198,12 @@ fish_link_deps_and_sign(fish) # Define fish_indent. add_executable(fish_indent - src/fish_indent.cpp src/print_help.cpp) + src/fish_indent.cpp) fish_link_deps_and_sign(fish_indent) # Define fish_key_reader. add_executable(fish_key_reader - src/fish_key_reader.cpp src/print_help.cpp) + src/fish_key_reader.cpp) fish_link_deps_and_sign(fish_key_reader) # Set up the docs. diff --git a/fish-rust/build.rs b/fish-rust/build.rs index 5bc37f7ec..ce9de9149 100644 --- a/fish-rust/build.rs +++ b/fish-rust/build.rs @@ -58,6 +58,7 @@ fn main() { "src/parse_constants.rs", "src/parse_tree.rs", "src/parse_util.rs", + "src/print_help.rs", "src/redirection.rs", "src/signal.rs", "src/smoke.rs", diff --git a/fish-rust/src/lib.rs b/fish-rust/src/lib.rs index b87f5f7ef..61115ec30 100644 --- a/fish-rust/src/lib.rs +++ b/fish-rust/src/lib.rs @@ -52,6 +52,7 @@ mod parse_util; mod parser_keywords; mod path; +mod print_help; mod re; mod reader; mod redirection; diff --git a/fish-rust/src/print_help.rs b/fish-rust/src/print_help.rs new file mode 100644 index 000000000..bad600f24 --- /dev/null +++ b/fish-rust/src/print_help.rs @@ -0,0 +1,33 @@ +//! Helper for executables (not builtins) to print a help message +//! Uses the fish in PATH, not necessarily the matching fish binary + +use libc::c_char; +use std::ffi::{CStr, OsStr, OsString}; +use std::os::unix::ffi::OsStrExt; +use std::process::Command; + +const HELP_ERR: &str = "Could not show help message"; + +#[cxx::bridge] +mod ffi2 { + extern "Rust" { + unsafe fn unsafe_print_help(command: *const c_char); + } +} + +fn print_help(command: &OsStr) { + let mut cmdline = OsString::new(); + cmdline.push("__fish_print_help "); + cmdline.push(command); + + Command::new("fish") + .args([OsStr::new("-c"), &cmdline]) + .spawn() + .expect(HELP_ERR); +} + +unsafe fn unsafe_print_help(command_buf: *const c_char) { + let command_cstr: &CStr = unsafe { CStr::from_ptr(command_buf) }; + let command = OsStr::from_bytes(command_cstr.to_bytes()); + print_help(command); +} diff --git a/src/fish_indent.cpp b/src/fish_indent.cpp index a7a393375..972a25127 100644 --- a/src/fish_indent.cpp +++ b/src/fish_indent.cpp @@ -46,7 +46,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #include "future_feature_flags.h" #include "highlight.h" #include "operation_context.h" -#include "print_help.h" +#include "print_help.rs.h" #include "tokenizer.h" #include "wcstringutil.h" #include "wutil.h" // IWYU pragma: keep @@ -341,7 +341,7 @@ int main(int argc, char *argv[]) { break; } case 'h': { - print_help("fish_indent"); + unsafe_print_help("fish_indent"); exit(0); } case 'v': { diff --git a/src/fish_key_reader.cpp b/src/fish_key_reader.cpp index 4299a8ff1..310c4b338 100644 --- a/src/fish_key_reader.cpp +++ b/src/fish_key_reader.cpp @@ -30,7 +30,7 @@ #include "input_common.h" #include "maybe.h" #include "parser.h" -#include "print_help.h" +#include "print_help.rs.h" #include "proc.h" #include "reader.h" #include "signals.h" @@ -312,7 +312,7 @@ static bool parse_flags(int argc, char **argv, bool *continuous_mode, bool *verb break; } case 'h': { - print_help("fish_key_reader"); + unsafe_print_help("fish_key_reader"); exit(0); } case 'v': { diff --git a/src/print_help.cpp b/src/print_help.cpp deleted file mode 100644 index 541d047e8..000000000 --- a/src/print_help.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Print help message for the specified command. -#include "config.h" // IWYU pragma: keep - -#include "print_help.h" - -#include -#include - -#include - -#include "common.h" - -#define CMD_LEN 1024 - -#define HELP_ERR "Could not show help message\n" - -void print_help(const char *c) { - char cmd[CMD_LEN]; - int printed = snprintf(cmd, CMD_LEN, "fish -c '__fish_print_help %s'", c); - - if (printed < CMD_LEN && system(cmd) == -1) { - write_loop(2, HELP_ERR, std::strlen(HELP_ERR)); - } -} diff --git a/src/print_help.h b/src/print_help.h deleted file mode 100644 index b7067ffcd..000000000 --- a/src/print_help.h +++ /dev/null @@ -1,8 +0,0 @@ -// Print help message for the specified command. -#ifndef FISH_PRINT_HELP_H -#define FISH_PRINT_HELP_H - -/// Print help message for the specified command. -void print_help(const char *cmd); - -#endif