forked from bonsai/harakit
commit
b0636b416e
46
src/fop.rs
46
src/fop.rs
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
env::args,
|
env::args,
|
||||||
io::{ Read, stdin, Write },
|
io::{ Read, stdin, stdout, Write },
|
||||||
process::{ Command, exit, Stdio },
|
process::{ Command, exit, Stdio },
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ extern crate sysexits;
|
|||||||
extern crate getopt;
|
extern crate getopt;
|
||||||
|
|
||||||
use getopt::{ Opt, Parser };
|
use getopt::{ Opt, Parser };
|
||||||
use sysexits::{ EX_DATAERR, EX_USAGE };
|
use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let argv = args().collect::<Vec<String>>();
|
let argv = args().collect::<Vec<String>>();
|
||||||
@ -54,43 +54,65 @@ fn main() {
|
|||||||
exit(EX_USAGE);
|
exit(EX_USAGE);
|
||||||
});
|
});
|
||||||
|
|
||||||
let index = argv[index_arg].parse::<usize>().unwrap_or_else(|_| {
|
let index = argv[index_arg].parse::<usize>().unwrap_or_else(|e| {
|
||||||
eprintln!("{}: {}: Not an integer.", argv[0], argv[1]);
|
eprintln!("{}: {}: {}.", argv[0], argv[1], e);
|
||||||
exit(EX_DATAERR);
|
exit(EX_DATAERR);
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
stdin().read_to_string(&mut buf).unwrap();
|
let _ = stdin().read_to_string(&mut buf);
|
||||||
let mut fields = buf.split(d).collect::<Vec<&str>>();
|
let mut fields = buf.split(d).collect::<Vec<&str>>();
|
||||||
|
|
||||||
let opts = argv.iter().clone().skip(command_arg + 1).collect::<Vec<&String>>();
|
let opts = argv
|
||||||
|
.iter()
|
||||||
|
.clone()
|
||||||
|
.skip(command_arg + 1)
|
||||||
|
.collect::<Vec<&String>>();
|
||||||
|
|
||||||
let mut spawned = Command::new(argv.get(command_arg).unwrap())
|
let mut spawned = Command::new(argv.get(command_arg).unwrap())
|
||||||
.args(opts)
|
.args(opts)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.spawn()
|
.spawn()
|
||||||
.unwrap();
|
.unwrap_or_else( |e| {
|
||||||
|
eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e);
|
||||||
|
exit(EX_UNAVAILABLE);
|
||||||
|
});
|
||||||
|
|
||||||
let field = fields.get(index).unwrap_or_else(|| {
|
let field = fields.get(index).unwrap_or_else(|| {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"{}: {}: No such index in input.",
|
"{}: {}: No such index in input.",
|
||||||
argv[0],
|
argv[0],
|
||||||
index.to_string()
|
index.to_string(),
|
||||||
);
|
);
|
||||||
exit(EX_DATAERR);
|
exit(EX_DATAERR);
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(mut child_stdin) = spawned.stdin.take() {
|
if let Some(mut child_stdin) = spawned.stdin.take() {
|
||||||
child_stdin.write_all(field.as_bytes()).unwrap();
|
let _ = child_stdin.write_all(field.as_bytes());
|
||||||
drop(child_stdin);
|
drop(child_stdin);
|
||||||
}
|
}
|
||||||
|
|
||||||
let output = spawned.wait_with_output().unwrap();
|
let output = spawned.wait_with_output().unwrap_or_else(|e| {
|
||||||
|
eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e);
|
||||||
|
exit(EX_IOERR);
|
||||||
|
});
|
||||||
|
|
||||||
let new_field = String::from_utf8(output.stdout).unwrap();
|
let mut replace = output.stdout.clone();
|
||||||
|
|
||||||
|
if replace.pop() != Some(b'\n') { replace = output.stdout; }
|
||||||
|
|
||||||
|
let new_field = String::from_utf8(replace).unwrap_or_else(|e| {
|
||||||
|
eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e);
|
||||||
|
exit(EX_IOERR);
|
||||||
|
});
|
||||||
|
|
||||||
fields[index] = &new_field;
|
fields[index] = &new_field;
|
||||||
|
|
||||||
print!("{}", fields.join(&d.to_string()));
|
stdout().write_all(
|
||||||
|
fields.join(&d.to_string()).as_bytes()
|
||||||
|
).unwrap_or_else(|e|{
|
||||||
|
eprintln!("{}: {}.", argv[0], e);
|
||||||
|
exit(EX_IOERR);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user