Skip to content

Commit

Permalink
Added automatic fallback to gsed if sed fails with an error.
Browse files Browse the repository at this point in the history
This at least somehow fixes #10.
  • Loading branch information
SoptikHa2 committed Apr 22, 2020
1 parent 0732642 commit daea6a3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
9 changes: 3 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn parse_arguments<'a, 'b>() -> Result<Options, String> {
.long("sed-path")
.help("Specify path to sed that should be used.")
.takes_value(true)
.default_value("sed"))
.required(false))
.arg(Arg::with_name("sed-script")
.help("Input file with sed script")
.required(true)
Expand Down Expand Up @@ -73,7 +73,7 @@ pub struct Options {
pub input_file: PathBuf,
pub sed_parameters: Vec<String>,
pub debug: bool,
pub sed_path: String,
pub sed_path: Option<String>,
}
impl Options {
pub fn from_matches(matches: ArgMatches) -> Result<Options, String> {
Expand All @@ -87,10 +87,7 @@ impl Options {
Err(_) => return Err(String::from("Failed to load input file path.")),
};

let sed_path: String = match matches.value_of("sed-path") {
Some(x) => String::from(x),
None => String::from("sed"),
};
let sed_path: Option<String> = matches.value_of("sed-path").map(|s| String::from(s));

let mut sed_parameters: Vec<String> = Vec::with_capacity(4);
let mut debug = false;
Expand Down
34 changes: 27 additions & 7 deletions src/sed/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl SedCommunicator {
pub fn new(options: Options) -> Self {
SedCommunicator { options }
}
pub fn get_execution_info_from_sed(&self) -> Result<DebugInfoFromSed, String> {
pub fn get_execution_info_from_sed(&mut self) -> Result<DebugInfoFromSed, String> {
let output = self.get_sed_output()?;

let program_source = self.parse_program_source(&output);
Expand All @@ -23,7 +23,12 @@ impl SedCommunicator {
last_output: frames.1,
});
}
fn get_sed_output(&self) -> Result<String, String> {
fn get_sed_output(&mut self) -> Result<String, String> {
let mut path_to_be_used: &String = &String::from("sed");
if let Some(path) = &self.options.sed_path {
path_to_be_used = path;
}

let mandatory_parameters = vec![
"--debug",
"-f",
Expand All @@ -43,26 +48,41 @@ impl SedCommunicator {
.map(|s| s.as_str())
.chain(mandatory_parameters.iter().map(|s| *s))
.collect::<Vec<&str>>();
let sed_debug_command = Command::new(&self.options.sed_path)
let sed_debug_command = Command::new(path_to_be_used)
.args(&constructed_cmd_line)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.output()
.ok()
.ok_or(
format!("Sed failed to process your script. Are you using GNU sed? If so, please report the bug.\nINFO: Sed was called using \"{} {}\"", &self.options.sed_path, constructed_cmd_line.join(" ")),
format!("[Error] Sed failed to start. Are you using GNU sed? Is the path correct?\n[Info] Sed was called using \"{} {}\"", &path_to_be_used, constructed_cmd_line.join(" ")),
)?
.stdout;

if self.options.debug {
eprintln!(
"[Info] Called sed with \"{} {}\"",
self.options.sed_path,
constructed_cmd_line.join(" ")
"[Info] Called sed with \"{} {}\", which returned {} lines of output.",
path_to_be_used,
constructed_cmd_line.join(" "),
sed_debug_command.len()
);
}

// If sed returned no output (so it failed) and sed
// path wasn't specified by user,
// change executing path to "gsed" and try again.
if self.options.sed_path.is_none() {
self.options.sed_path = Some(String::from("gsed"));
if self.options.debug {
eprintln!(
"[Info] Sed failed and didn't return any output. As sed path wasn't specified, trying again with \"gsed\". If even that won't work, make sure \
sed is able to process your script. Most common mistake is forgeting to use -E."
);
}
return self.get_sed_output();
}

Ok(String::from_utf8(sed_debug_command).ok().ok_or(String::from("String received from sed doesn't seem to be UTF-8. If this continues to happen, please report the bug."))?)
}

Expand Down
2 changes: 1 addition & 1 deletion src/sed/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Debugger {
impl Debugger {
/// Create new instance of debugger and launch sed.
pub fn new(settings: Options) -> Result<Self, String> {
let communicator = SedCommunicator::new(settings);
let mut communicator = SedCommunicator::new(settings);
let data: DebugInfoFromSed = communicator.get_execution_info_from_sed()?;
// Shift all pattern matches one frame earlier.
// The way it's done now (output appears one frame after it's source)
Expand Down

0 comments on commit daea6a3

Please sign in to comment.