Skip to content

Commit 2e2f315

Browse files
committed
Merge branch 'main' into man
2 parents 4f17045 + bd6aafa commit 2e2f315

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

text/sed.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use std::{
2525
path::PathBuf,
2626
};
2727

28+
const DEFAULT_TMP_DIR: &str = "../target/tmp";
29+
2830
static ERE: Mutex<bool> = Mutex::new(false);
2931

3032
#[derive(Parser, Debug, Clone)]
@@ -1346,7 +1348,7 @@ fn print_multiline_binary(line: &str) {
13461348
}
13471349
}
13481350
}
1349-
} else if let Some(line) = line.strip_suffix(&['\n']) {
1351+
} else if let Some(line) = line.strip_suffix(['\n']) {
13501352
println!("{}$", line);
13511353
} else {
13521354
print!("{}$", line);
@@ -1416,6 +1418,16 @@ fn filter_comments(raw_script: impl AsRef<str>) -> String {
14161418
raw_script_without_comments
14171419
}
14181420

1421+
/// Get path to [`wfile`] in tmp dir
1422+
fn get_tmp_path(wfile: PathBuf) -> PathBuf {
1423+
let mut tmp_path =
1424+
PathBuf::from(std::env::var("CARGO_TARGET_TMPDIR").unwrap_or(DEFAULT_TMP_DIR.to_string()));
1425+
1426+
tmp_path.extend(&wfile);
1427+
1428+
tmp_path
1429+
}
1430+
14191431
/// Contains [`Command`] sequence of all [`Sed`] session
14201432
/// that applied all to every line of input files
14211433
#[derive(Debug)]
@@ -1821,6 +1833,15 @@ fn execute_replace(
18211833
Some(wfile)
18221834
}) {
18231835
if replace && wfile.components().next().is_some() {
1836+
let mut wfile = wfile.clone();
1837+
if !(wfile.is_absolute()
1838+
|| wfile.starts_with("./")
1839+
|| wfile.starts_with("../")
1840+
|| wfile.exists())
1841+
{
1842+
wfile = get_tmp_path(wfile);
1843+
}
1844+
18241845
if let Ok(mut file) = std::fs::OpenOptions::new()
18251846
.append(true)
18261847
.create(true)
@@ -2213,6 +2234,15 @@ impl Sed {
22132234
}
22142235

22152236
fn execute_w(&mut self, wfile: PathBuf) -> Result<(), SedError> {
2237+
let mut wfile = wfile.clone();
2238+
if !(wfile.is_absolute()
2239+
|| wfile.starts_with("./")
2240+
|| wfile.starts_with("../")
2241+
|| wfile.exists())
2242+
{
2243+
wfile = get_tmp_path(wfile);
2244+
}
2245+
22162246
let _ = match std::fs::OpenOptions::new()
22172247
.append(true)
22182248
.create(true)

text/tests/sed/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,12 +1825,17 @@ mod tests {
18251825
fn test_w() {
18261826
let test_data = [
18271827
// correct
1828-
("w ./tests/sed/assets/newfile", "abc\ncdf\n", "abc\ncdf\n", ""),
18291828
("w atyfv", "abc\ncdf\n", "abc\ncdf\n", ""),
18301829
("w./tests/sed/assets/r", "", "", ""),
1831-
("w./tests/sed/assets/newfile", "a\n", "a\n", ""),
1830+
("w newfile", "a\n", "a\n", ""),
18321831
("w ; h", "abc\ncdf\n", "abc\ncdf\n", ""),
18331832
// wrong
1833+
(
1834+
"w ./dir/newfile",
1835+
"abc\ncdf\n",
1836+
"",
1837+
"sed: read stdin: can't find './dir/newfile': no such file or directory (os error 2)\n",
1838+
),
18341839
(
18351840
"w./tests/s\x04ed/assets/abc",
18361841
"a\n",

0 commit comments

Comments
 (0)