<commit_before>pub mod source;
pub mod variables;
pub mod functions;
pub mod calc;

mod conditionals;
mod job_control;
mod test;
mod echo;
mod set;
mod exists;
mod ion;

use self::conditionals::{contains, ends_with, starts_with};
use self::echo::echo;
use self::exists::exists;
use self::functions::fn_;
use self::ion::ion_docs;
use self::source::source;
use self::test::test;
use self::variables::{alias, drop_alias, drop_array, drop_variable};

use fnv::FnvHashMap;
use std::error::Error;
use std::io::{self, Write};

use parser::QuoteTerminator;
use shell::{self, FlowLogic, Shell, ShellHistory};
use shell::job_control::{JobControl, ProcessState};
use shell::status::*;
use sys;

/// Structure which represents a Terminal's command.
/// This command structure contains a name, and the code which run the
/// functionnality associated to this one, with zero, one or several argument(s).
pub struct Builtin {
    pub name: &'static str,
    pub help: &'static str,
    pub main: fn(&[&str], &mut Shell) -> i32,
}

impl Builtin {
    /// Return the map from command names to commands
    pub fn map() -> FnvHashMap<&'static str, Self> {
        let mut commands: FnvHashMap<&str, Self> =
            FnvHashMap::with_capacity_and_hasher(32, Default::default());

        // Quick and clean way to insert a builtin, define a function named as the builtin
        // for example:
        // fn builtin_not (args: &[&str], shell: &mut Shell) -> i32 {
        // let cmd = args[1..].join(" ");
        // shell.on_command(&cmd);
        // match shell.previous_status {
        // SUCCESS => FAILURE,
        // FAILURE => SUCCESS,
        // _ => shell.previous_status
        // }
        // }
        //
        // insert_builtin!("not", builtin_not, "Reverses the exit status value of the given
        // command.");
        //

        macro_rules! insert_builtin {
            ($name:expr, $func:ident, $help:expr) => {
                commands.insert(
                    $name,
                    Builtin {
                        name: $name,
                        help: $help,
                        main: $func,
                    }
                );
            }
        }

        // Directories
        insert_builtin!("cd", builtin_cd, "Change the current directory\n    cd <path>");

        insert_builtin!("dirs", builtin_dirs, "Display the current directory stack");
        insert_builtin!("pushd", builtin_pushd, "Push a directory to the stack");
        insert_builtin!("popd", builtin_popd, "Pop a directory from the stack");

        // Aliases
        insert_builtin!("alias", builtin_alias, "View, set or unset aliases");
        insert_builtin!("unalias", builtin_unalias, "Delete an alias");

        // Variables
        insert_builtin!("fn", builtin_fn, "Print list of functions");
        insert_builtin!("read", builtin_read, "Read some variables\n    read <variable>");
        insert_builtin!("drop", builtin_drop, "Delete a variable");

        // Misc
        insert_builtin!("matches", builtin_matches, "Checks if a string matches a given regex");
        insert_builtin!("not", builtin_not, "Reverses the exit status value of the given command.");
        insert_builtin!(
            "set",
            builtin_set,
            "Set or unset values of shell options and positional parameters."
        );
        insert_builtin!("eval", builtin_eval, "evaluates the evaluated expression");
        insert_builtin!("exit", builtin_exit, "Exits the current session");
        insert_builtin!(
            "wait",
            builtin_wait,
            "Waits until all running background processes have completed"
        );
        insert_builtin!(
            "jobs",
            builtin_jobs,
            "Displays all jobs that are attached to the background"
        );
        insert_builtin!("bg", builtin_bg, "Resumes a stopped background process");
        insert_builtin!(
            "fg",
            builtin_fg,
            "Resumes and sets a background process as the active process"
        );
        insert_builtin!("suspend", builtin_suspend, "Suspends the shell with a SIGTSTOP signal");
        insert_builtin!(
            "disown",
            builtin_disown,
            "Disowning a process removes that process from the shell's background process table."
        );
        insert_builtin!(
            "history",
            builtin_history,
            "Display a log of all commands previously executed"
        );
        insert_builtin!(
            "source",
            builtin_source,
            "Evaluate the file following the command or re-initialize the init file"
        );
        insert_builtin!("echo", builtin_echo, "Display a line of text");
        insert_builtin!("test", builtin_test, "Performs tests on files and text");
        insert_builtin!("calc", builtin_calc, "Calculate a mathematical expression");
        insert_builtin!("true", builtin_true, "Do nothing, successfully");
        insert_builtin!("false", builtin_false, "Do nothing, unsuccessfully");
        insert_builtin!(
            "help",
            builtin_help,
            "Display helpful information about a given command or list commands if none \
             specified\n    help <command>"
        );
        insert_builtin!(
            "and",
            builtin_and,
            "Execute the command if the shell's previous status is success"
        );
        insert_builtin!(
            "or",
            builtin_or,
            "Execute the command if the shell's previous status is failure"
        );
        insert_builtin!(
            "starts-with",
            starts_with,
            "Evaluates if the supplied argument starts with a given string"
        );
        insert_builtin!(
            "ends-with",
            ends_with,
            "Evaluates if the supplied argument ends with a given string"
        );
        insert_builtin!(
            "contains",
            contains,
            "Evaluates if the supplied argument contains a given string"
        );
        insert_builtin!("exists", builtin_exists, "Performs tests on files and text");
        insert_builtin!("ion-docs", ion_docs, "Opens the Ion manual");

        commands
    }
}

// Definitions of simple builtins go here

fn builtin_cd(args: &[&str], shell: &mut Shell) -> i32 {
    match shell.directory_stack.cd(args, &shell.variables) {
        Ok(()) => SUCCESS,
        Err(why) => {
            let stderr = io::stderr();
            let mut stderr = stderr.lock();
            let _ = stderr.write_all(why.as_bytes());
            FAILURE
        }
    }
}

fn builtin_dirs(args: &[&str], shell: &mut Shell) -> i32 { shell.directory_stack.dirs(args) }

fn builtin_pushd(args: &[&str], shell: &mut Shell) -> i32 {<commit_msg>Let builtin prints variables<commit_after>pub mod source;
pub mod variables;
pub mod functions;
pub mod calc;

mod conditionals;
mod job_control;
mod test;
mod echo;
mod set;
mod exists;
mod ion;

use self::conditionals::{contains, ends_with, starts_with};
use self::echo::echo;
use self::exists::exists;
use self::functions::fn_;
use self::ion::ion_docs;
use self::source::source;
use self::test::test;
use self::variables::{alias, drop_alias, drop_array, drop_variable};

use fnv::FnvHashMap;
use std::error::Error;
use std::io::{self, Write};

use parser::QuoteTerminator;
use shell::{self, FlowLogic, Shell, ShellHistory};
use shell::job_control::{JobControl, ProcessState};
use shell::status::*;
use sys;

/// Structure which represents a Terminal's command.
/// This command structure contains a name, and the code which run the
/// functionnality associated to this one, with zero, one or several argument(s).
pub struct Builtin {
    pub name: &'static str,
    pub help: &'static str,
    pub main: fn(&[&str], &mut Shell) -> i32,
}

impl Builtin {
    /// Return the map from command names to commands
    pub fn map() -> FnvHashMap<&'static str, Self> {
        let mut commands: FnvHashMap<&str, Self> =
            FnvHashMap::with_capacity_and_hasher(32, Default::default());

        // Quick and clean way to insert a builtin, define a function named as the builtin
        // for example:
        // fn builtin_not (args: &[&str], shell: &mut Shell) -> i32 {
        // let cmd = args[1..].join(" ");
        // shell.on_command(&cmd);
        // match shell.previous_status {
        // SUCCESS => FAILURE,
        // FAILURE => SUCCESS,
        // _ => shell.previous_status
        // }
        // }
        //
        // insert_builtin!("not", builtin_not, "Reverses the exit status value of the given
        // command.");
        //

        macro_rules! insert_builtin {
            ($name:expr, $func:ident, $help:expr) => {
                commands.insert(
                    $name,
                    Builtin {
                        name: $name,
                        help: $help,
                        main: $func,
                    }
                );
            }
        }

        // Directories
        insert_builtin!("cd", builtin_cd, "Change the current directory\n    cd <path>");

        insert_builtin!("dirs", builtin_dirs, "Display the current directory stack");
        insert_builtin!("pushd", builtin_pushd, "Push a directory to the stack");
        insert_builtin!("popd", builtin_popd, "Pop a directory from the stack");

        // Aliases
        insert_builtin!("alias", builtin_alias, "View, set or unset aliases");
        insert_builtin!("unalias", builtin_unalias, "Delete an alias");

        // Variables
        insert_builtin!("fn", builtin_fn, "Print list of functions");
        insert_builtin!("read", builtin_read, "Read some variables\n    read <variable>");
        insert_builtin!("drop", builtin_drop, "Delete a variable");

        // Misc
        insert_builtin!("matches", builtin_matches, "Checks if a string matches a given regex");
        insert_builtin!("not", builtin_not, "Reverses the exit status value of the given command.");
        insert_builtin!(
            "set",
            builtin_set,
            "Set or unset values of shell options and positional parameters."
        );
        insert_builtin!("eval", builtin_eval, "evaluates the evaluated expression");
        insert_builtin!("exit", builtin_exit, "Exits the current session");
        insert_builtin!(
            "wait",
            builtin_wait,
            "Waits until all running background processes have completed"
        );
        insert_builtin!(
            "jobs",
            builtin_jobs,
            "Displays all jobs that are attached to the background"
        );
        insert_builtin!("bg", builtin_bg, "Resumes a stopped background process");
        insert_builtin!(
            "fg",
            builtin_fg,
            "Resumes and sets a background process as the active process"
        );
        insert_builtin!("suspend", builtin_suspend, "Suspends the shell with a SIGTSTOP signal");
        insert_builtin!(
            "disown",
            builtin_disown,
            "Disowning a process removes that process from the shell's background process table."
        );
        insert_builtin!(
            "history",
            builtin_history,
            "Display a log of all commands previously executed"
        );
        insert_builtin!(
            "source",
            builtin_source,
            "Evaluate the file following the command or re-initialize the init file"
        );
        insert_builtin!("echo", builtin_echo, "Display a line of text");
        insert_builtin!("test", builtin_test, "Performs tests on files and text");
        insert_builtin!("calc", builtin_calc, "Calculate a mathematical expression");
        insert_builtin!("true", builtin_true, "Do nothing, successfully");
        insert_builtin!("false", builtin_false, "Do nothing, unsuccessfully");
        insert_builtin!(
            "help",
            builtin_help,
            "Display helpful information about a given command or list commands if none \
             specified\n    help <command>"
        );
        insert_builtin!(
            "and",
            builtin_and,
            "Execute the command if the shell's previous status is success"
        );
        insert_builtin!(
            "or",
            builtin_or,
            "Execute the command if the shell's previous status is failure"
        );
        insert_builtin!(
            "starts-with",
            starts_with,
            "Evaluates if the supplied argument starts with a given string"
        );
        insert_builtin!(
            "ends-with",
            ends_with,
            "Evaluates if the supplied argument ends with a given string"
        );
        insert_builtin!(
            "contains",
            contains,
            "Evaluates if the supplied argument contains a given string"
        );
        insert_builtin!("exists", builtin_exists, "Performs tests on files and text");
        insert_builtin!("ion-docs", ion_docs, "Opens the Ion manual");
        insert_builtin!("let", list_vars, "Displays a list of local variables");
        commands
    }
}

// Definitions of simple builtins go here

fn list_vars(_: &[&str], shell: &mut Shell) -> i32 {
    shell.variables.variables.iter().for_each(
        |(key, val)| println!("{} = {}", key, val)
    );
    SUCCESS
}

fn builtin_cd(args: &[&str], shell: &mut Shell) -> i32 {
    match shell.directory_stack.cd(args, &shell.variables) {
        Ok(()) => SUCCESS,
        Err(why) => {
            let stderr = io::stderr();
            let mut stderr = stderr.lock();
            let _ = stderr.write_all(why.as_bytes());
            FAILURE
        }
    }
}

fn builtin_dirs(args: &[&str], shell: &mut Shell) -> i32 { shell.directory_stack.dirs(args) }

fn builtin_pushd(args: &[&str], shell: &mut Shell) -> i32 {
{'fraction_non_alphanumeric': 0.1139185677158533, 'fraction_numerical': 0.0020213687554143807, 'mean_word_length': 2.2134075620505684, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7016036', 'n_tokens_mistral': 3781, 'n_tokens_neox': 3840, 'n_words': 1171}