diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-12-19 09:59:34 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-12-19 11:00:54 +0100 |
| commit | 42ebea54e35062a1a998f25e8fe019a5e0205fd9 (patch) | |
| tree | e947d2c86083da913595354d42e98fc74c831865 /crates/nasm/src | |
| parent | 9d9f10295059df278ed558142ce6c58c4f6a634e (diff) | |
| download | tools.nes-42ebea54e35062a1a998f25e8fe019a5e0205fd9.tar.gz tools.nes-42ebea54e35062a1a998f25e8fe019a5e0205fd9.zip | |
Implement the .incbin control statement
This also forced us to add the current working directory to the
`Assembler::assemble` public function, as otherwise this control
statement and others wouldn't know how to resolve relative paths.
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'crates/nasm/src')
| -rw-r--r-- | crates/nasm/src/main.rs | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/crates/nasm/src/main.rs b/crates/nasm/src/main.rs index e6d7c45..fd97ca1 100644 --- a/crates/nasm/src/main.rs +++ b/crates/nasm/src/main.rs @@ -1,7 +1,8 @@ -use anyhow::Result; +use anyhow::{bail, Context, Result}; use clap::Parser as ClapParser; use std::fs::File; use std::io::{self, Read, Write}; +use std::path::Path; use xixanta::assembler::Assembler; use xixanta::mapping::{Mapping, EMPTY, NROM, NROM65}; @@ -31,10 +32,24 @@ struct Args { fn main() -> Result<()> { let args = Args::parse(); - // Select the input stream. - let input: Box<dyn Read> = match args.file { - Some(file) => Box::new(File::open(file)?), - None => Box::new(std::io::stdin()), + // Select the input stream and the current working directory. + let input: Box<dyn Read>; + let working_directory = match &args.file { + Some(file) => { + let path = Path::new(file); + if !path.is_file() { + bail!("Input file must be a valid file"); + } + input = Box::new(File::open(file)?); + + path.parent() + .with_context(|| String::from("Failed to find directory for given file"))? + } + None => { + input = Box::new(std::io::stdin()); + &std::env::current_dir() + .with_context(|| String::from("Could not fetch current directory"))? + } }; // Select the output stream. @@ -60,7 +75,7 @@ fn main() -> Result<()> { // And assemble. let mut assembler = Assembler::new(mapping); - match assembler.assemble(input) { + match assembler.assemble(working_directory.to_path_buf(), input) { Ok(bundles) => { for b in bundles { for i in 0..b.size { |
