jimtcl 0.5.0-beta3

Embed Jim Tcl in Rust.
Documentation
//! Command-line shells.

use std::io::{self, IsTerminal};

use clap::Parser;

use crate::error::ExitCode;
use crate::{Interp, JimObject, JimResult};
use crate::{JimError, sys};

/// Rust wrapper to implement jimsh
#[derive(Parser, Debug)]
#[command(disable_version_flag = true)]
pub struct Jimsh {
    /// Print version information.
    #[arg(long = "version")]
    pub print_version: bool,

    /// Tcl expression to execute.
    #[arg(short = 'e')]
    pub expression: Option<String>,

    /// Tcl script to execute, optionally with arguments.
    #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
    pub script: Vec<String>,
}

impl Jimsh {
    /// Get the script file to run.
    pub fn script_file(&self) -> Option<&str> {
        self.script.first().map(String::as_str)
    }

    /// Get the arguments to be passed to the script.
    pub fn script_args(&self) -> &[String] {
        if !self.script.is_empty() && self.expression.is_none() {
            &self.script[1..]
        } else {
            &self.script
        }
    }

    /// Set up an interpreter environment to run the script.
    pub fn setup_env(&self, interp: &Interp) -> JimResult<()> {
        interp.eval_source("initjimsh.tcl", 1, sys::tclexts::initjimsh)?;
        if let Some(path) = self.script_file() {
            interp.set_variable("argv0", path)?;
        }
        let argv = JimObject::empty(interp);
        for arg in self.script_args() {
            argv.list_append(arg.as_str());
        }
        interp.set_variable("argv", argv)?;
        interp.set_variable("jim::lineedit", 0)?;
        Ok(())
    }

    /// Run the command line in the specified interpreter.  Returns the Jim
    /// exit code on success.
    ///
    /// **Note:** you must call [setup_env] before this function to properly
    /// initialize the interpreter variables. The calls are not automatically
    /// linked, to allow embedding code to perform additional post-setup
    /// actions.
    pub fn run(&self, interp: &Interp) -> JimResult<i32> {
        let result = if let Some(script) = &self.expression {
            interp.eval(script).map(|res| {
                println!("{}", res);
            })
        } else if let Some(path) = self.script_file() {
            if path == "-" {
                interp
                    .eval("eval [info source [stdin read] stdin 1]")
                    .map(|_| ())
            } else {
                interp.eval_file(path)
            }
        } else if io::stdin().is_terminal() {
            interp.interactive_prompt()
        } else {
            // not a terminal, just execute stdin, like jimsh
            interp
                .eval("eval [info source [stdin read] stdin 1]")
                .map(|_| ())
        };

        // special-case unhandled errors to get stacktrace
        if let Err(JimError::Error(msg)) = &result {
            eprintln!("{}", msg);
            let stack = interp.stack_trace();
            let obj = interp.new_string("stackdump");
            obj.list_append(stack);
            let result = interp.eval_object(&obj)?;
            eprintln!("{}", result);
            Ok(1)
        } else if let Err(JimError::OtherCode(ExitCode::Break)) = &result {
            // break is fine
            Ok(0)
        } else {
            interp.resolve_exit(result)
        }
    }
}