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