Rewrite and adopt print_help in Rust

This commit is contained in:
David Adam 2023-05-29 09:05:10 +08:00
parent d19a08cd8c
commit 688a28c1d2
8 changed files with 41 additions and 38 deletions

View File

@ -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.

View File

@ -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",

View File

@ -52,6 +52,7 @@ mod parse_tree;
mod parse_util;
mod parser_keywords;
mod path;
mod print_help;
mod re;
mod reader;
mod redirection;

View File

@ -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);
}

View File

@ -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': {

View File

@ -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': {

View File

@ -1,24 +0,0 @@
// Print help message for the specified command.
#include "config.h" // IWYU pragma: keep
#include "print_help.h"
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#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));
}
}

View File

@ -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