Skip to content

Instantly share code, notes, and snippets.

@minitech
Created August 26, 2013 02:33
Show Gist options
  • Save minitech/6337668 to your computer and use it in GitHub Desktop.
Save minitech/6337668 to your computer and use it in GitHub Desktop.
First Rust program (apart from Hello, world) to actually learn something, since there appear to be no books yet and the thing keeps changing anyways
use std::io::{stdin, stdout};
use std::run;
use std::libc;
use std::libc::c_int;
static SIGCONT: c_int = 18;
static SIGSTOP: c_int = 19;
trait ProcessSignal {
fn kill(&self, signal: c_int);
}
impl ProcessSignal for run::Process {
fn kill(&self, signal: c_int) {
unsafe {
libc::funcs::posix88::signal::kill(self.get_id(), signal);
}
}
}
fn main() {
let mut current_process: Option<run::Process> = None;
loop {
let input = stdin().read_line();
let (action, argument) = {
let mut command = input.splitn_iter(' ', 1);
(command.next(), command.next())
};
match (action, argument) {
(Some("play"), Some(track)) => {
if current_process.is_some() {
current_process.get_mut_ref().destroy();
}
let path = fmt!("/home/ryan/music/%s", track);
let options = run::ProcessOptions {
env: None,
dir: None,
in_fd: None,
out_fd: None,
err_fd: Some(2)
};
current_process = Some(run::Process::new("play", [path], options));
},
(Some("play"), None) => {
if current_process.is_some() {
current_process.get_ref().kill(SIGCONT);
} else {
fail!("No current process to pause!");
}
},
(Some("pause"), None) => {
if current_process.is_some() {
current_process.get_ref().kill(SIGSTOP);
} else {
fail!("No current process to pause!");
}
},
(Some("wtf"), None) => {
let output = current_process.get_mut_ref().finish_with_output();
println("Error:");
stdout().write(output.error);
println("Output:");
stdout().write(output.output);
},
(Some(a), _) => println(fmt!("Unrecognized action %s", a)),
_ => fail!()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment