Files
ghostty-rs/crates/ghostty-cli/src/options.rs
T
ddidderr 6f9b3129e1 refactor(config): parse config values from raw bytes like Zig
Zig parses config values (cli/args.zig, Config.zig field parsers,
config/string.zig) from raw byte slices, and each field type decides
whether it requires UTF-8 (InvalidValue otherwise) or stores bytes. The
port escape-encoded raw bytes into Strings at the line reader and let
fields parse the escaped text, so fields could observe encoding
artifacts; successive audits kept finding such leaks (RV5-RV8).

Field parsers now receive the original bytes: text fields validate UTF-8
where Zig does, byte/path fields store bytes (EncodedOs/OsString). This
resolves all 9 RV8 findings (docs/rewrite/notes/review-rv8-codex.md),
each with a regression test: Windows path conversion fails with
BadPathName instead of substituting U+FFFD; Windows process launch uses
strict UTF-8 like Zig's Command; the WTF-8 decoder accepts paired-
surrogate encodings like Zig; literal U+FDD0 Unicode key triggers;
selection-word-chars and clipboard-codepoint-map reject invalid UTF-8;
font style / codepoint-map family names use original bytes; C title
actions forward bytes; D-Bus new-window/Flatpak conversions match Zig.
Design, sweep and validation: docs/rewrite/notes/final-bytes-refactor.md.

Also moves the bundled iTerm2-Color-Schemes license out of
resources/themes/ so +list-themes no longer lists it as a theme
(output is now byte-identical to Ghostty 1.3.1).

Done by Codex (gpt-6-astra), reviewed and verified by the orchestrator.

Test Plan:
- cargo test --workspace --no-fail-fast (3428 passed, exit 0)
- just check-macos (both), just check-windows, just check-wasm
- +show-config --default --docs, +list-keybinds, +list-actions --docs,
  +list-themes --plain compared with /usr/bin/ghostty
2026-10-12 11:57:01 +02:00

117 lines
4.2 KiB
Rust

//! Helpers shared by the action `Options` structs.
use ghostty_config::cli_args::{self, ArgsIterator, CliTarget};
use crate::error::Error;
/// Implements [`CliTarget`] for a struct declared with `config_struct!` and
/// gives it a `Default`. Pass `help` to enable `-h`/`--help` (the Zig
/// `pub fn help` decl); structs without it treat `--help` as an ordinary
/// (unknown or declared) field.
macro_rules! cli_options {
($name:ident, help, diagnostics) => {
cli_options!(@base $name);
impl ::ghostty_config::cli_args::CliTarget for $name {
fn parse_into_field(
&mut self,
key: &str,
value: ::core::option::Option<&[u8]>,
) -> ::core::result::Result<(), ::ghostty_config::cli_args::ArgError> {
self.parse_field_bytes(key, value)
}
fn valid_values(&self, key: &str) -> ::core::option::Option<&'static [&'static str]> {
Self::valid_values_for(key)
}
fn diagnostics_mut(
&mut self,
) -> ::core::option::Option<&mut ::ghostty_config::diagnostics::DiagnosticList> {
::core::option::Option::Some(&mut self.diagnostics)
}
fn supports_help(&self) -> bool {
true
}
fn help(&mut self) -> ::core::result::Result<(), ::ghostty_config::cli_args::ArgError> {
::core::result::Result::Err(::ghostty_config::cli_args::ArgError::other(
$crate::error::HELP_ERROR_NAME,
))
}
}
};
($name:ident, help) => {
cli_options!(@base $name);
impl ::ghostty_config::cli_args::CliTarget for $name {
fn parse_into_field(
&mut self,
key: &str,
value: ::core::option::Option<&[u8]>,
) -> ::core::result::Result<(), ::ghostty_config::cli_args::ArgError> {
self.parse_field_bytes(key, value)
}
fn valid_values(&self, key: &str) -> ::core::option::Option<&'static [&'static str]> {
Self::valid_values_for(key)
}
fn supports_help(&self) -> bool {
true
}
fn help(&mut self) -> ::core::result::Result<(), ::ghostty_config::cli_args::ArgError> {
::core::result::Result::Err(::ghostty_config::cli_args::ArgError::other(
$crate::error::HELP_ERROR_NAME,
))
}
}
};
($name:ident) => {
cli_options!(@base $name);
impl ::ghostty_config::cli_args::CliTarget for $name {
fn parse_into_field(
&mut self,
key: &str,
value: ::core::option::Option<&[u8]>,
) -> ::core::result::Result<(), ::ghostty_config::cli_args::ArgError> {
self.parse_field_bytes(key, value)
}
fn valid_values(&self, key: &str) -> ::core::option::Option<&'static [&'static str]> {
Self::valid_values_for(key)
}
}
};
(@base $name:ident) => {
impl ::core::default::Default for $name {
fn default() -> Self {
Self::raw_default()
}
}
};
}
/// Parse the CLI arguments `args` (the full argv, including `argv[0]`,
/// which is skipped; `+action` arguments are ignored) into `opts`
/// (Zig: `args.argsIterator` + `args.parse`).
pub fn parse_options<T: CliTarget>(opts: &mut T, args: &[impl AsRef<[u8]>]) -> Result<(), Error> {
let mut iter = ArgsIterator::new(args.iter().skip(1).map(AsRef::as_ref));
cli_args::parse(opts, &mut iter)?;
Ok(())
}
/// The options of an action that has no flags of its own (only `-h`/`--help`).
#[derive(Debug, Clone, Default)]
pub struct NoOptions;
impl CliTarget for NoOptions {
fn parse_into_field(
&mut self,
_key: &str,
_value: Option<&[u8]>,
) -> Result<(), cli_args::ArgError> {
Err(cli_args::ArgError::InvalidField)
}
fn supports_help(&self) -> bool {
true
}
fn help(&mut self) -> Result<(), cli_args::ArgError> {
Err(cli_args::ArgError::other(crate::error::HELP_ERROR_NAME))
}
}