From 42ebea54e35062a1a998f25e8fe019a5e0205fd9 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Thu, 19 Dec 2024 09:59:34 +0100 Subject: Implement the .incbin control statement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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à --- crates/nasm/src/main.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'crates/nasm/src') 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 = 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; + 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 { -- cgit v1.2.3