-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::{ | ||
collections::LinkedList, | ||
ffi::OsString, | ||
fs::File, | ||
io::{BufRead, BufReader}, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use crate::error::{Error, ErrorExt}; | ||
|
||
/// A fs entry that is a script (has a shbang at its start) | ||
#[derive(Clone)] | ||
pub struct ScriptFile { | ||
/// The name of the file | ||
pub name: OsString, | ||
|
||
/// The interpreter for the file | ||
/// (<path to the interpreter>, <array of arguments>) | ||
pub interpreter: Option<(PathBuf, Vec<OsString>)>, | ||
} | ||
|
||
impl ScriptFile { | ||
/// Parses an `ELFFile` from the provided path | ||
/// # Arguments | ||
/// * `path` - The path to parse the file from | ||
pub fn parse(path: &Path, name: OsString) -> Result<Self, Error> { | ||
let context = || format!("Parsing SCRIPT file {}", path.to_string_lossy()); | ||
|
||
// Read the first line | ||
let first_line = { | ||
let mut file = BufReader::new(File::open(path).e_context(context)?); | ||
let mut shbang = String::new(); | ||
file.read_line(&mut shbang).e_context(context)?; | ||
shbang | ||
}; | ||
|
||
// Remove the shbang from the start ('#!') | ||
let first_line = first_line.trim_start_matches("#!"); | ||
|
||
// Split the line into its pieces | ||
let mut split: LinkedList<OsString> = first_line | ||
.split(' ') | ||
.map(|s| OsString::from(s.trim())) | ||
.collect(); | ||
|
||
// Split the interpreter path from its arguments | ||
let interpreter = split.pop_front().map(|i| { | ||
( | ||
PathBuf::from(i), | ||
split.into_iter().collect::<Vec<OsString>>(), | ||
) | ||
}); | ||
|
||
Ok(Self { name, interpreter }) | ||
} | ||
} |