Skip to content

Commit

Permalink
Add clone
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 23, 2025
1 parent ed50417 commit cb26270
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/elements/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ impl Debug for dyn Command {
}
}

impl Clone for Box::<dyn Command> {
fn clone(&self) -> Box<dyn Command> {
self.boxed_clone()
}
}

pub trait Command {
fn exec(&mut self, core: &mut ShellCore, pipe: &mut Pipe) -> Result<Option<Pid>, ExecError> {
if self.force_fork() || pipe.is_connected() {
Expand Down Expand Up @@ -74,6 +80,7 @@ pub trait Command {
fn get_text(&self) -> String;
fn get_redirects(&mut self) -> &mut Vec<Redirect>;
fn set_force_fork(&mut self);
fn boxed_clone(&self) -> Box<dyn Command>;
fn force_fork(&self) -> bool;
}

Expand Down
3 changes: 2 additions & 1 deletion src/elements/command/brace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::error::parse::ParseError;
use super::{Command, Redirect};
use crate::elements::command;

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct BraceCommand {
text: String,
script: Option<Script>,
Expand All @@ -26,6 +26,7 @@ impl Command for BraceCommand {
fn get_text(&self) -> String { self.text.clone() }
fn get_redirects(&mut self) -> &mut Vec<Redirect> { &mut self.redirects }
fn set_force_fork(&mut self) { self.force_fork = true; }
fn boxed_clone(&self) -> Box<dyn Command> {Box::new(self.clone())}
fn force_fork(&self) -> bool { self.force_fork }
}

Expand Down
3 changes: 2 additions & 1 deletion src/elements/command/if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::error::parse::ParseError;
use crate::elements::command;
use super::{Command, Redirect};

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct IfCommand {
pub text: String,
pub if_elif_scripts: Vec<Script>,
Expand Down Expand Up @@ -36,6 +36,7 @@ impl Command for IfCommand {
fn get_text(&self) -> String { self.text.clone() }
fn get_redirects(&mut self) -> &mut Vec<Redirect> { &mut self.redirects }
fn set_force_fork(&mut self) { self.force_fork = true; }
fn boxed_clone(&self) -> Box<dyn Command> {Box::new(self.clone())}
fn force_fork(&self) -> bool { self.force_fork }
}

Expand Down
3 changes: 2 additions & 1 deletion src/elements/command/paren.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{Command, Pipe, Redirect};
use crate::elements::command;
use nix::unistd::Pid;

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct ParenCommand {
text: String,
script: Option<Script>,
Expand All @@ -35,6 +35,7 @@ impl Command for ParenCommand {
fn get_text(&self) -> String { self.text.clone() }
fn get_redirects(&mut self) -> &mut Vec<Redirect> { &mut self.redirects }
fn set_force_fork(&mut self) { }
fn boxed_clone(&self) -> Box<dyn Command> {Box::new(self.clone())}
fn force_fork(&self) -> bool { true }
}

Expand Down
3 changes: 2 additions & 1 deletion src/elements/command/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn reserved(w: &str) -> bool {
matches!(w, "{" | "}" | "while" | "do" | "done" | "if" | "then" | "elif" | "else" | "fi")
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct SimpleCommand {
text: String,
words: Vec<Word>,
Expand Down Expand Up @@ -66,6 +66,7 @@ impl Command for SimpleCommand {
fn get_text(&self) -> String { self.text.clone() }
fn get_redirects(&mut self) -> &mut Vec<Redirect> { &mut self.redirects }
fn set_force_fork(&mut self) { self.force_fork = true; }
fn boxed_clone(&self) -> Box<dyn Command> {Box::new(self.clone())}
fn force_fork(&self) -> bool { self.force_fork }
}

Expand Down
3 changes: 2 additions & 1 deletion src/elements/command/while.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::error::parse::ParseError;
use super::{Command, Redirect};
use crate::elements::command;

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct WhileCommand {
pub text: String,
pub while_script: Option<Script>,
Expand Down Expand Up @@ -37,6 +37,7 @@ impl Command for WhileCommand {
fn get_text(&self) -> String { self.text.clone() }
fn get_redirects(&mut self) -> &mut Vec<Redirect> { &mut self.redirects }
fn set_force_fork(&mut self) { self.force_fork = true; }
fn boxed_clone(&self) -> Box<dyn Command> {Box::new(self.clone())}
fn force_fork(&self) -> bool { self.force_fork }
}

Expand Down
2 changes: 1 addition & 1 deletion src/elements/io/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::os::unix::prelude::RawFd;
use nix::unistd;
use nix::unistd::Pid;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Pipe {
pub text: String,
pub recv: RawFd,
Expand Down
2 changes: 1 addition & 1 deletion src/elements/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::utils::exit;
use nix::unistd;
use nix::unistd::{Pid, ForkResult};

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Job {
pub pipelines: Vec<Pipeline>,
pub pipeline_ends: Vec<String>,
Expand Down
2 changes: 1 addition & 1 deletion src/elements/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::Pipe;
use nix::unistd::Pid;
use std::sync::atomic::Ordering::Relaxed;

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Pipeline {
pub commands: Vec<Box<dyn Command>>,
pub pipes: Vec<Pipe>,
Expand Down
2 changes: 1 addition & 1 deletion src/elements/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum Status{
NormalEnd,
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Script {
pub jobs: Vec<Job>,
pub job_ends: Vec<String>,
Expand Down

0 comments on commit cb26270

Please sign in to comment.