Skip to content

Commit

Permalink
Add struct Word
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 17, 2024
1 parent 2642106 commit 2bdf4a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pub mod job;
pub mod pipeline;
pub mod command;
pub mod io;
pub mod word;

use self::io::pipe::Pipe;
21 changes: 14 additions & 7 deletions src/elements/command/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::{ShellCore, Feeder};
use super::{Command, Pipe, Redirect};
use crate::elements::command;
use crate::elements::word::Word;
use nix::unistd;
use std::ffi::CString;
use std::process;
Expand All @@ -21,17 +22,22 @@ fn reserved(w: &str) -> bool {
#[derive(Debug)]
pub struct SimpleCommand {
text: String,
words: Vec<Word>,
args: Vec<String>,
redirects: Vec<Redirect>,
force_fork: bool,
}

impl Command for SimpleCommand {
fn exec(&mut self, core: &mut ShellCore, pipe: &mut Pipe) -> Option<Pid> {
if self.args.len() == 0 {
if self.words.len() == 0 {
return None;
}

for a in &mut self.words {
self.args.extend(a.get_args());
}

if self.force_fork
|| pipe.is_connected()
|| ! core.builtins.contains_key(&self.args[0]) {
Expand Down Expand Up @@ -89,25 +95,26 @@ impl SimpleCommand {
fn new() -> SimpleCommand {
SimpleCommand {
text: String::new(),
words: vec![],
args: vec![],
redirects: vec![],
force_fork: false,
}
}

fn eat_word(feeder: &mut Feeder, ans: &mut SimpleCommand, core: &mut ShellCore) -> bool {
let arg_len = feeder.scanner_word(core);
if arg_len == 0 {
let word_len = feeder.scanner_word(core);
if word_len == 0 {
return false;
}

let word = feeder.consume(arg_len);
if ans.args.len() == 0 && reserved(&word) {
let word = feeder.consume(word_len);
if ans.words.len() == 0 && reserved(&word) {
return false;
}

ans.text += &word.clone();
ans.args.push(word);
ans.words.push(Word{ text: word });
true
}

Expand All @@ -122,7 +129,7 @@ impl SimpleCommand {
}
}

if ans.args.len() + ans.redirects.len() > 0 {
if ans.words.len() + ans.redirects.len() > 0 {
feeder.pop_backup();
Some(ans)
}else{
Expand Down
13 changes: 13 additions & 0 deletions src/elements/word.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//SPDX-FileCopyrightText: 2023 Ryuichi Ueda ryuichiueda@gmail.com
//SPDX-License-Identifier: BSD-3-Clause

#[derive(Debug)]
pub struct Word {
pub text: String,
}

impl Word {
pub fn get_args(&mut self) -> Vec<String> {
vec![self.text.clone()]
}
}

0 comments on commit 2bdf4a4

Please sign in to comment.