Skip to content
Snippets Groups Projects
Commit e91deea7 authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Merge pull request #2 from skylerberg/master

Sync ion up with shell from redox repository
parents 4c62268c e070ad82
No related branches found
No related tags found
No related merge requests found
name=Shell
icon=file:/ui/mimetypes/application-x-executable-script.bmp
author=Jeremy Soller
description=Shell for Redox
extern crate ion;
fn main() {
loop {
ion::repl()
}
}
use std::process::{Command,Output};
pub struct InstructionOut {
pub stdout: String,
pub stderr: String,
}
pub fn run(args: &[&str]) -> Option<InstructionOut> {
let output: Option<Output>;
match args.len() {
0 => output = Command::new("").output().ok(),
1 => output = Command::new(&args[0]).output().ok(),
_ => output = Command::new(&args[0]).args(&args[1..]).output().ok(),
}
if output.is_some() {
let output = output.unwrap();
Some(InstructionOut {
stdout: String::from_utf8(output.stdout).ok().expect("No stdout"),
stderr: String::from_utf8(output.stderr).ok().expect("No stderr"),
})
} else {
None
}
}
#![feature(convert)]
pub mod command;
use std::io;
use command::*;
pub fn repl() {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_) => {
let out_wrap = run(input.trim().split_whitespace().collect::<Vec<&str>>().as_slice());
if out_wrap.is_some() {
let out = out_wrap.unwrap();
if out.stdout.is_empty() {
println!("{}",out.stderr.trim());
} else {
println!("{}",out.stdout.trim());
}
} else {
println!("{} is not a valid command", input.trim());
}
}
Err(error) => println!("Line Read Error: {}", error)
};
}
This diff is collapsed.
//! Types convertable to integers
/// Parse the string to a integer using a given radix
pub trait ToNum {
fn to_num_radix(&self, radix: usize) -> usize;
fn to_num_radix_signed(&self, radix: usize) -> isize;
fn to_num(&self) -> usize;
fn to_num_signed(&self) -> isize;
}
impl ToNum for str {
fn to_num_radix(&self, radix: usize) -> usize {
if radix == 0 {
return 0;
}
let mut num = 0;
for c in self.chars() {
let digit;
if c >= '0' && c <= '9' {
digit = c as usize - '0' as usize
} else if c >= 'A' && c <= 'Z' {
digit = c as usize - 'A' as usize + 10
} else if c >= 'a' && c <= 'z' {
digit = c as usize - 'a' as usize + 10
} else {
break;
}
if digit >= radix {
break;
}
num *= radix;
num += digit;
}
num
}
/// Parse the string as a signed integer using a given radix
fn to_num_radix_signed(&self, radix: usize) -> isize {
if self.starts_with('-') {
-(self[1 ..].to_num_radix(radix) as isize)
} else {
self.to_num_radix(radix) as isize
}
}
/// Parse it as a unsigned integer in base 10
fn to_num(&self) -> usize {
self.to_num_radix(10)
}
/// Parse it as a signed integer in base 10
fn to_num_signed(&self) -> isize {
self.to_num_radix_signed(10)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment