Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get/set command line arguments in SBLaunchInfo #35

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/launchinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use crate::{lldb_pid_t, sys, LaunchFlags, SBFileSpec, SBListener};
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::ptr;

/// Configuration for launching a process.
Expand Down Expand Up @@ -125,6 +126,38 @@ impl SBLaunchInfo {
unsafe { sys::SBLaunchInfoSetLaunchFlags(self.raw, launch_flags.bits()) }
}

/// Specify the command line arguments.
pub fn set_arguments<'a>(&self, args: impl IntoIterator<Item = &'a str>, append: bool) {
let cstrs: Vec<CString> = args.into_iter().map(|a| CString::new(a).unwrap()).collect();
let mut ptrs: Vec<*const c_char> = cstrs.iter().map(|cs| cs.as_ptr()).collect();
ptrs.push(ptr::null());
let argv = ptrs.as_ptr();
unsafe { sys::SBLaunchInfoSetArguments(self.raw, argv, append) };
}

/// Returns an interator over the command line arguments.
pub fn arguments(&self) -> impl Iterator<Item = &str> {
SBLaunchInfoArgumentsIter {
launch_info: self,
index: 0,
}
}

#[allow(missing_docs)]
fn num_arguments(&self) -> u32 {
unsafe { sys::SBLaunchInfoGetNumArguments(self.raw) }
}

#[allow(missing_docs)]
fn argument_at_index(&self, index: u32) -> &str {
unsafe {
match CStr::from_ptr(sys::SBLaunchInfoGetArgumentAtIndex(self.raw, index)).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
}

#[allow(missing_docs)]
pub fn process_plugin_name(&self) -> Option<&str> {
unsafe {
Expand Down Expand Up @@ -246,3 +279,28 @@ impl Drop for SBLaunchInfo {

unsafe impl Send for SBLaunchInfo {}
unsafe impl Sync for SBLaunchInfo {}

pub struct SBLaunchInfoArgumentsIter<'d> {
launch_info: &'d SBLaunchInfo,
index: u32,
}

impl<'d> Iterator for SBLaunchInfoArgumentsIter<'d> {
type Item = &'d str;

fn next(&mut self) -> Option<&'d str> {
if self.index < self.launch_info.num_arguments() {
self.index += 1;
Some(self.launch_info.argument_at_index(self.index - 1))
} else {
None
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let sz = self.launch_info.num_arguments();
(sz as usize - self.index as usize, Some(sz as usize))
}
}

impl<'d> ExactSizeIterator for SBLaunchInfoArgumentsIter<'d> {}