Commit 385df792 authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Divide init into two files

parent 6ab16af3
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::io::{BufRead, BufReader, Result};
use std::process::Command;
pub fn main() {
let file = File::open("initfs:etc/init.rc").expect("failed to open init.rc");
pub fn run(file: &str) -> Result<()> {
let file = File::open(file)?;
let reader = BufReader::new(file);
for line_result in reader.lines() {
let line = line_result.expect("failed to read init.rc");
let line = line_result?;
let line = line.trim();
if ! line.is_empty() && ! line.starts_with('#') {
let mut args = line.split(' ');
if let Some(cmd) = args.next() {
match cmd {
"cd" => if let Some(dir) = args.next() {
if let Err(err) = env::set_current_dir(dir) {
println!("init: failed to cd to '{}': {}", dir, err);
}
} else {
println!("init: failed to cd: no argument");
},
"echo" => {
if let Some(arg) = args.next() {
print!("{}", arg);
......@@ -23,12 +30,25 @@ pub fn main() {
}
print!("\n");
},
"cd" => if let Some(dir) = args.next() {
if let Err(err) = env::set_current_dir(dir) {
println!("init: failed to cd to '{}': {}", dir, err);
"export" => if let Some(var) = args.next() {
let mut value = String::new();
if let Some(arg) = args.next() {
value.push_str(&arg);
}
for arg in args {
value.push(' ');
value.push_str(&arg);
}
env::set_var(var, value);
} else {
println!("init: failed to cd: no argument");
println!("init: failed to export: no argument");
},
"run" => if let Some(new_file) = args.next() {
if let Err(err) = run(&new_file) {
println!("init: failed to run '{}': {}", new_file, err);
}
} else {
println!("init: failed to run: no argument");
},
_ => {
let mut command = Command::new(cmd);
......@@ -48,4 +68,12 @@ pub fn main() {
}
}
}
Ok(())
}
pub fn main() {
if let Err(err) = run("initfs:etc/init.rc") {
println!("init: failed to run initfs:etc/init.rc: {}", err);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment