The 'cton-util cat' command parses the given files and writes them out again to stdout. This has the effect of reformatting and stripping comments. Fix a writer bug that inverted the blank line before the first EBB.
42 lines
1.2 KiB
Rust
42 lines
1.2 KiB
Rust
//! The `cat` sub-command.
|
|
//!
|
|
//! Read a sequence of Cretonne IL files and print them again to stdout. This has the effect of
|
|
//! normalizing formatting and removing comments.
|
|
|
|
use std::fs::File;
|
|
use std::io::{self, Read};
|
|
|
|
use CommandResult;
|
|
|
|
use cton_reader::parser::Parser;
|
|
use cretonne::write::write_function;
|
|
|
|
pub fn run(files: Vec<String>) -> CommandResult {
|
|
for (i, f) in files.into_iter().enumerate() {
|
|
if i != 0 {
|
|
println!("");
|
|
}
|
|
try!(cat_one(f))
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn cat_one(filename: String) -> CommandResult {
|
|
let mut file = try!(File::open(&filename).map_err(|e| format!("{}: {}", filename, e)));
|
|
let mut buffer = String::new();
|
|
try!(file.read_to_string(&mut buffer)
|
|
.map_err(|e| format!("Couldn't read {}: {}", filename, e)));
|
|
let items = try!(Parser::parse(&buffer).map_err(|e| format!("{}: {}", filename, e)));
|
|
|
|
for (idx, func) in items.into_iter().enumerate() {
|
|
if idx != 0 {
|
|
println!("");
|
|
}
|
|
let stdout = io::stdout();
|
|
let mut handle = stdout.lock();
|
|
try!(write_function(&mut handle, &func).map_err(|e| format!("{}: {}", filename, e)));
|
|
}
|
|
|
|
Ok(())
|
|
}
|