Skip to content

Commit b0a8be1

Browse files
committed
c2rust-refactor: Restore main binary
1 parent c270005 commit b0a8be1

File tree

4 files changed

+198
-1
lines changed

4 files changed

+198
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

c2rust-refactor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ failure = "0.1"
3333
bincode = "1.0.1"
3434
petgraph = "0.4"
3535
cargo-util = "0.1.2"
36+
shlex = "1.3"
3637

3738
[build-dependencies]
3839
c2rust-build-paths = { path = "../c2rust-build-paths", version = "0.20.0" }
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
use clap::{App, ArgMatches, load_yaml};
2+
use log::info;
3+
use std::fs::File;
4+
use std::io::Read;
5+
use std::process;
6+
use std::str::FromStr;
7+
8+
use c2rust_refactor::{file_io, CargoTarget, Command, Cursor, Mark, Options, RustcArgSource};
9+
10+
fn main() {
11+
let yaml = load_yaml!("../refactor.yaml");
12+
let args = App::from_yaml(yaml).get_matches();
13+
14+
let opts = match parse_opts(&args) {
15+
Some(x) => x,
16+
None => return,
17+
};
18+
19+
let ret = match c2rust_refactor::lib_main(opts) {
20+
Ok(()) => 0,
21+
Err(_) => 1,
22+
};
23+
process::exit(ret);
24+
}
25+
26+
fn parse_opts(args: &ArgMatches) -> Option<Options> {
27+
// Parse rewrite mode
28+
let rewrite_modes = match args.values_of("rewrite-mode") {
29+
Some(values) => values
30+
.map(|s| match s {
31+
"inplace" => file_io::OutputMode::InPlace,
32+
"alongside" => file_io::OutputMode::Alongside,
33+
"print" => file_io::OutputMode::Print,
34+
"diff" => file_io::OutputMode::PrintDiff,
35+
"json" => file_io::OutputMode::Json,
36+
"marks" => file_io::OutputMode::Marks,
37+
_ => unreachable!(),
38+
})
39+
.collect(),
40+
None => vec![file_io::OutputMode::Print],
41+
};
42+
43+
// Parse cursors
44+
let cursor_strs = args.values_of_lossy("cursor").unwrap_or(vec![]);
45+
let mut cursors = Vec::with_capacity(cursor_strs.len());
46+
for s in &cursor_strs {
47+
let mut parts = s.split(':');
48+
49+
let file = match parts.next() {
50+
Some(x) => x.to_owned(),
51+
None => {
52+
info!("Bad cursor string: {:?}", s);
53+
return None;
54+
}
55+
};
56+
57+
let line = match parts.next().map(|s| u32::from_str(s).map_err(|_| s)) {
58+
Some(Ok(x)) => x,
59+
Some(Err(s)) => {
60+
info!("Bad cursor line number: {:?}", s);
61+
return None;
62+
}
63+
None => {
64+
info!("Bad cursor string: {:?}", s);
65+
return None;
66+
}
67+
};
68+
69+
let col = match parts.next().map(|s| u32::from_str(s).map_err(|_| s)) {
70+
Some(Ok(x)) => x,
71+
Some(Err(s)) => {
72+
info!("Bad cursor column number: {:?}", s);
73+
return None;
74+
}
75+
None => {
76+
info!("Bad cursor string: {:?}", s);
77+
return None;
78+
}
79+
};
80+
81+
let label = match parts.next() {
82+
Some(s) if s.len() > 0 => Some(s.to_owned()),
83+
_ => None,
84+
};
85+
86+
let kind = parts.next().map(|s| s.to_owned());
87+
88+
if parts.next().is_some() {
89+
info!("Bad cursor string: {:?}", s);
90+
return None;
91+
}
92+
93+
cursors.push(Cursor::new(file, line, col, label, kind));
94+
}
95+
96+
// Parse marks
97+
let mark_strs = args.values_of_lossy("mark").unwrap_or(vec![]);
98+
let mut marks = Vec::with_capacity(mark_strs.len());
99+
for s in &mark_strs {
100+
let mut parts = s.split(':');
101+
102+
let id = match parts.next().map(|s| usize::from_str(s).map_err(|_| s)) {
103+
Some(Ok(x)) => x,
104+
Some(Err(s)) => {
105+
info!("Bad mark node ID: {:?}", s);
106+
return None;
107+
}
108+
None => {
109+
info!("Bad mark string: {:?}", s);
110+
return None;
111+
}
112+
};
113+
114+
let label = parts.next().map(|s| s.to_owned());
115+
116+
if parts.next().is_some() {
117+
info!("Bad mark string: {:?}", s);
118+
return None;
119+
}
120+
121+
marks.push(Mark::new(id, label));
122+
}
123+
124+
// Get plugin options
125+
let plugins = args.values_of_lossy("plugin-name").unwrap_or(vec![]);
126+
let plugin_dirs = args.values_of_lossy("plugin-dir").unwrap_or(vec![]);
127+
128+
// Handle --cargo and rustc-args
129+
let rustc_args = match args.values_of_lossy("rustc-args") {
130+
Some(args) => RustcArgSource::CmdLine(args),
131+
None => {
132+
assert!(args.is_present("cargo"));
133+
let target = if let Some(bin) = args.value_of("bin") {
134+
CargoTarget::Bin(bin.to_string())
135+
} else if args.is_present("bins") {
136+
CargoTarget::AllBins
137+
} else if args.is_present("lib") {
138+
CargoTarget::Lib
139+
} else {
140+
CargoTarget::All
141+
};
142+
RustcArgSource::Cargo(target)
143+
}
144+
};
145+
146+
// Parse command names + args
147+
let transforms_file = match args.value_of("transforms-file") {
148+
Some(file_name) => {
149+
let mut file = File::open(file_name).unwrap_or_else(|e| {
150+
panic!("Could not open transforms file {:?}: {}", file_name, e);
151+
});
152+
let mut buf = String::new();
153+
file.read_to_string(&mut buf).unwrap_or_else(|e| {
154+
panic!("Could not read transforms file {:?}: {}", file_name, e);
155+
});
156+
buf
157+
}
158+
None => String::new(),
159+
};
160+
let transforms: Box<dyn Iterator<Item = String>> = match args.value_of("transforms-file") {
161+
Some(_) => Box::new(shlex::Shlex::new(&transforms_file)),
162+
None => Box::new(args.values_of("transforms").unwrap().map(String::from)),
163+
};
164+
let mut commands = Vec::new();
165+
let mut cur_command = None;
166+
for arg in transforms {
167+
if arg == ";" {
168+
if let Some(cmd) = cur_command.take() {
169+
commands.push(cmd);
170+
} else {
171+
info!("Expected command before ';'");
172+
return None;
173+
}
174+
} else if cur_command.is_none() {
175+
cur_command = Some(Command {
176+
name: arg,
177+
args: Vec::new(),
178+
});
179+
} else {
180+
cur_command.as_mut().unwrap().args.push(arg);
181+
}
182+
}
183+
if let Some(cmd) = cur_command.take() {
184+
commands.push(cmd);
185+
}
186+
187+
Some(Options {
188+
rewrite_modes,
189+
commands,
190+
rustc_args,
191+
cursors,
192+
marks,
193+
plugins,
194+
plugin_dirs,
195+
})
196+
}
File renamed without changes.

0 commit comments

Comments
 (0)