Skip to content
Snippets Groups Projects
Commit 6fe6bb79 authored by Per Lindgren's avatar Per Lindgren
Browse files

parsing

parent c18aeead
No related branches found
No related tags found
No related merge requests found
...@@ -2,15 +2,13 @@ ...@@ -2,15 +2,13 @@
A cargo sub-commond for spanning the call stack for a `cortex-m` bare metal application. A cargo sub-commond for spanning the call stack for a `cortex-m` bare metal application.
## Installing ## Installing
> cargo install --path <PATH_TO cargo-call-stack> > cargo install --path <PATH_TO cargo-call-stack> -f
## Usage ## Usage
Assume we have have an existing `cortex-m` bare metal application, e.g., ... Assume we have have an existing `cortex-m` bare metal application, e.g., ...
> cargo call-stack --release --example hello1 > cargo call-stack --release --example hello1
## Tech notes ## Tech notes
### Cargo sub-commands ### Cargo sub-commands
......
...@@ -4,6 +4,80 @@ use std::io::{self, Read, Write}; ...@@ -4,6 +4,80 @@ use std::io::{self, Read, Write};
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::{env, str}; use std::{env, str};
#[derive(Debug)]
struct Out {
hash: Option<String>,
crate_name: Option<String>,
out_dir: Option<String>,
}
// Parse the output from rustc
// we skip the last line (Finished ...)
// and look for `--crate-name x`, `extra-filename=y` and `--out dir z`
//
// Notice, the parsing is a bit of a hack, implemnting look ahead using flags.
fn parse_out(out_str: &str) -> Out {
let mut out = Out {
hash: None,
crate_name: None,
out_dir: None,
};
let output = out_str;
let mut i = output.lines().into_iter();
i.next_back(); // skip last line (Finished)
if let Some(line) = i.next_back() {
let mut crate_name = false;
let mut out_dir = false;
for part in line.split(' ') {
if crate_name {
out.crate_name = Some(part.to_string());
crate_name = false;
} else if out_dir {
out.out_dir = Some(part.to_string());
out_dir = false;
} else if part.starts_with("--crate-name") {
crate_name = true;
} else if part.starts_with("--out-dir") {
out_dir = true;
} else if part.starts_with("extra-filename=") {
out.hash = Some(part.split('=').nth(1).unwrap().to_string());
}
}
}
out
}
fn parse_output(output: &str) -> (Option<&str>, Option<&str>, Option<&str>) {
let mut suffix = None;
let mut crate_name = None;
let mut example = None;
let output = str::from_utf8(output.as_bytes()).unwrap();
println!("here ...");
let mut i = output.lines().into_iter();
i.next_back(); // skip last line
if let Some(line) = i.next_back() {
let mut b = false;
for part in line.split(' ') {
if b {
crate_name = Some(part);
b = false;
} else if part.starts_with("--crate-name") {
b = true;
println!("----- here ---------- {:?}", crate_name);
} else if part.starts_with("extra-filename=") {
suffix = part.split('=').nth(1);
println!("----- there ----------");
} else if part.starts_with("example") {
example = part.split('/').nth(1);
println!("----- there ----------");
};
}
//}
}
(suffix, crate_name, example)
}
fn main() { fn main() {
println!("start sub command"); println!("start sub command");
// first argument is the path to this binary; the second argument is always "call-stack" -- both can // first argument is the path to this binary; the second argument is always "call-stack" -- both can
...@@ -57,60 +131,74 @@ fn main() { ...@@ -57,60 +131,74 @@ fn main() {
} }
} }
// find out the argument passed to `--example`, if used at all let out = parse_out(str::from_utf8(&output).unwrap());
let mut example = None; println!("{:?}", out);
let mut iargs = args.iter(); // } else {
while let Some(arg) = iargs.next() { // panic!("could not determine hash, please recompile in `--release` mode");
if arg == "--example" { // }
example = iargs.next();
break;
}
}
// get the suffix of the example
let (example, suffix) = if let Some(example) = example {
let mut suffix = None;
let output = str::from_utf8(&output).unwrap();
println!("here ...");
for line in output.lines() {
if line.contains(&format!("examples/{}.rs", example)) {
for part in line.split(' ') {
if part.starts_with("extra-filename=") {
suffix = part.split('=').nth(1);
}
}
}
}
match suffix {
None => panic!(format!(
"Cannot determine hash, please run `touch example/{}.rs` to force recompiltaion.",
example
)),
Some(s) => (example, s),
}
} else {
// nothing to do if the user didn't use `--example`
println!("try to determinte hash");
let output = str::from_utf8(&output).unwrap();
println!("here ...");
let mut suffix = None;
for line in output.lines() {
for part in line.split(' ') {
if part.starts_with("extra-filename=") {
suffix = part.split('=').nth(1);
}
}
}
match suffix {
None => panic!(format!(
"Cannot determine hash, please force recompiltaion."
)),
Some(s) => panic!("main hash {}", s),
}
// return;
};
println!("{:?}", (example, suffix)); // find out the argument passed to `--example`, if used at all
// let mut example = None;
// let mut iargs = args.iter();
// while let Some(arg) = iargs.next() {
// if arg == "--example" {
// example = iargs.next();
// break;
// }
// }
// let output = str::from_utf8(&output).unwrap();
// if let (Some(hash), Some(crate_name), example) = parse_output(&output) {
// println!("{:?}, {:?}, {:?}", example, hash, crate_name);
// } else {
// panic!("could not determine hash, please recompile in `--release` mode");
// }
// // get the suffix of the example
// let (example, suffix) = if let Some(example) = example {
// let mut suffix = None;
// let output = str::from_utf8(&output).unwrap();
// println!("here ...");
// for line in output.lines() {
// if line.contains(&format!("examples/{}.rs", example)) {
// for part in line.split(' ') {
// if part.starts_with("extra-filename=") {
// suffix = part.split('=').nth(1);
// }
// }
// }
// }
// match suffix {
// None => panic!(format!(
// "Cannot determine hash, please run `touch example/{}.rs` to force recompiltaion.",
// example
// )),
// Some(s) => (example, s),
// }
// } else {
// // nothing to do if the user didn't use `--example`
// println!("try to determinte hash");
// let output = str::from_utf8(&output).unwrap();
// println!("here ...");
// let mut suffix = None;
// for line in output.lines() {
// for part in line.split(' ') {
// if part.starts_with("extra-filename=") {
// suffix = part.split('=').nth(1);
// }
// }
// }
// match suffix {
// None => panic!(format!(
// "Cannot determine hash, please force recompiltaion."
// )),
// Some(s) => panic!("main hash {}", s),
// }
// // return;
// };
// println!("{:?}", (example, suffix));
/* /*
let profile = if args.iter().any(|a| a == "--release") { let profile = if args.iter().any(|a| a == "--release") {
...@@ -152,3 +240,12 @@ fn main() { ...@@ -152,3 +240,12 @@ fn main() {
unsafe { libc::getuid() } unsafe { libc::getuid() }
*/ */
} }
// blx511
// ARN
// LINDGREN
// PER
// 03021968 Piteå
// Swedish
// pass#
// Aquamarine
// Tourist
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment