Compare commits
No commits in common. "731e62ee7ea81e0aa5b9cdb34a233e83b69ae0e4" and "9aeadd8f8fd73781663dc8c9e301eeca9028fac6" have entirely different histories.
731e62ee7e
...
9aeadd8f8f
@ -8,13 +8,12 @@
|
||||
*/
|
||||
|
||||
#ifdef __OpenBSD__
|
||||
# include <unistd.h> /* NULL, pledge(2), unveil(2) */
|
||||
# include <unistd.h> /* pledge(2) */
|
||||
#endif
|
||||
|
||||
int main(void) {
|
||||
#ifdef __OpenBSD__
|
||||
(void)pledge(NULL, NULL);
|
||||
(void)unveil(NULL, NULL);
|
||||
pledge(NULL, NULL);
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
@ -32,7 +32,8 @@ use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
|
||||
|
||||
#[cfg(target_os="openbsd")] use sysexits::EX_OSERR;
|
||||
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil };
|
||||
#[cfg(target_os="openbsd")]
|
||||
use openbsd::{ Promises, pledge, unveil };
|
||||
|
||||
fn err(argv0: &String, e: Error) {
|
||||
eprintln!("{}: {}", argv0, e.strerror());
|
||||
@ -61,8 +62,6 @@ fn main() -> ExitCode {
|
||||
}
|
||||
}
|
||||
|
||||
if argv.len() == 1 { return ExitCode::from(usage(&argv[0])); }
|
||||
|
||||
while let Some(opt) = argv.getopt("d:") {
|
||||
match opt.opt() {
|
||||
Ok("d") => {
|
||||
|
@ -26,35 +26,27 @@ extern crate getopt;
|
||||
extern crate sysexits;
|
||||
|
||||
use getopt::GetOpt;
|
||||
use sysexits::{ EX_DATAERR, EX_USAGE };
|
||||
use sysexits::EX_USAGE;
|
||||
|
||||
#[cfg(target_os="openbsd")] use sysexits::EX_OSERR;
|
||||
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||
#[cfg(target_os="openbsd")] extern crate strerror;
|
||||
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil };
|
||||
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
|
||||
#[cfg(target_os="openbsd")] use strerror::StrError;
|
||||
|
||||
fn err(argv0: &String, e: String, code: u8) -> ExitCode {
|
||||
eprintln!("{}: {}", argv0, e);
|
||||
ExitCode::from(code)
|
||||
}
|
||||
|
||||
fn usage(argv0: &str) -> ExitCode {
|
||||
eprintln!("Usage: {} [-egl] integer integer...", argv0);
|
||||
ExitCode::from(EX_USAGE)
|
||||
fn usage(s: &str) -> ExitCode {
|
||||
eprintln!("Usage: {} [-egl] integer integer...", s);
|
||||
ExitCode::from(EX_USAGE as u8)
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
|
||||
#[cfg(target_os="openbsd")] {
|
||||
let promises = Promises::new("stdio unveil");
|
||||
let promises = Promises::new("stdio");
|
||||
if let Err(e) = pledge(Some(promises), None) {
|
||||
return err(&argv[0], e.strerror(), EX_OSERR);
|
||||
}
|
||||
|
||||
if let Err(e) = unveil(None, None) {
|
||||
return err(&argv[0], e.strerror(), EX_OSERR);
|
||||
eprintln!("{}: {}", argv[0], e.strerror());
|
||||
return ExitCode::from(EX_OSERR as u8);
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,8 +78,8 @@ fn main() -> ExitCode {
|
||||
match arg.parse::<usize>() { /* parse current operand */
|
||||
Ok(n) => currn = n,
|
||||
Err(e) => {
|
||||
let error = arg.to_owned() + ": " + &e.to_string();
|
||||
return err(&argv[0], error, EX_DATAERR);
|
||||
eprintln!("{}: {}: {}", &argv[0], arg, e);
|
||||
return ExitCode::from(EX_USAGE as u8);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@
|
||||
#include <stdio.h> /* fprintf(3), fputs(3), getc(3), perror(3), putc(3), stdin,
|
||||
* stdout, EOF */
|
||||
#include <sysexits.h> /* EX_IOERR, EX_OK, EX_OSERR, EX_USAGE */
|
||||
#include <unistd.h> /* NULL, getopt(3), pledge(2), unveil(2) */
|
||||
#include <unistd.h> /* pledge(2), getopt(3) */
|
||||
|
||||
char *program_name = "npc";
|
||||
|
||||
@ -44,7 +44,7 @@ int main(int argc, char *argv[]) {
|
||||
char showtab = 0; /* prints tab characters in caret notation */
|
||||
|
||||
#ifdef __OpenBSD__
|
||||
if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL)) {
|
||||
if (pledge("stdio", NULL) == -1) {
|
||||
perror(argv[0] == NULL ? program_name : argv[0]);
|
||||
return EX_OSERR;
|
||||
}
|
||||
|
57
src/rpn.rs
57
src/rpn.rs
@ -60,7 +60,7 @@ use sysexits::{ EX_DATAERR, EX_IOERR };
|
||||
#[cfg(target_os="openbsd")] extern crate strerror;
|
||||
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||
#[cfg(target_os="openbsd")] use strerror::StrError;
|
||||
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil };
|
||||
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
|
||||
|
||||
#[derive(Clone, PartialEq, PartialOrd, Debug)]
|
||||
/* enum CalcType is a type containing operations used in the calculator */
|
||||
@ -138,33 +138,6 @@ fn err<T: StrError>(argv0: &String, e: &T, code: Option<u8>) -> ExitCode {
|
||||
ExitCode::from(code.unwrap_or(1 /* unknown error */))
|
||||
}
|
||||
|
||||
fn operate(
|
||||
mut stack: VecDeque<f64>,
|
||||
op: CalcType,
|
||||
) -> Result<VecDeque<f64>, EvaluationError> {
|
||||
let vals = (stack.pop_back(), stack.pop_back());
|
||||
|
||||
if let (Some(x), Some(y)) = vals {
|
||||
match op {
|
||||
Add => stack.push_back(y + x),
|
||||
Subtract => stack.push_back(y - x),
|
||||
Multiply => stack.push_back(y * x),
|
||||
Divide => stack.push_back(y / x),
|
||||
Power => stack.push_back(y.powf(x)),
|
||||
Floor => stack.push_back((y / x).floor()),
|
||||
Modulo => stack.push_back(y % x),
|
||||
_ => {},
|
||||
};
|
||||
} else {
|
||||
return Err(EvaluationError {
|
||||
message: format!("{}: unexpected operation", op),
|
||||
code: EX_DATAERR,
|
||||
})
|
||||
}
|
||||
|
||||
Ok(stack)
|
||||
}
|
||||
|
||||
fn eval(
|
||||
input: &str,
|
||||
initial_stack: VecDeque<f64>,
|
||||
@ -197,9 +170,27 @@ fn eval(
|
||||
},
|
||||
op => {
|
||||
ops.push_back(op.clone());
|
||||
|
||||
oper = true; /* this is an operation */
|
||||
return operate(stack, op).map(|s| (s, oper));
|
||||
|
||||
let vals = (stack.pop_back(), stack.pop_back());
|
||||
|
||||
if let (Some(x), Some(y)) = vals {
|
||||
match op {
|
||||
Add => stack.push_back(y + x),
|
||||
Subtract => stack.push_back(y - x),
|
||||
Multiply => stack.push_back(y * x),
|
||||
Divide => stack.push_back(y / x),
|
||||
Power => stack.push_back(y.powf(x)),
|
||||
Floor => stack.push_back((y / x).floor()),
|
||||
Modulo => stack.push_back(y % x),
|
||||
_ => {},
|
||||
};
|
||||
} else {
|
||||
return Err(EvaluationError {
|
||||
message: format!("{}: unexpected operation", op),
|
||||
code: EX_DATAERR,
|
||||
})
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -234,14 +225,10 @@ fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
|
||||
#[cfg(target_os="openbsd")] {
|
||||
let promises = Promises::new("stdio unveil");
|
||||
let promises = Promises::new("stdio");
|
||||
if let Err(e) = pledge(Some(promises), None) {
|
||||
return err(&argv[0], &e, Some(EX_OSERR));
|
||||
}
|
||||
|
||||
if let Err(e) = unveil(None, None) {
|
||||
return err(&argv[0], &e, Some(EX_OSERR));
|
||||
}
|
||||
}
|
||||
|
||||
let mut stack = VecDeque::new();
|
||||
|
@ -25,7 +25,7 @@
|
||||
#include <sysexits.h> /* EX_OSERR, EX_USAGE */
|
||||
|
||||
#ifdef __OpenBSD__
|
||||
# include <unistd.h> /* pledge(2), unveil(2) */
|
||||
# include <unistd.h> /* pledge(2) */
|
||||
#endif
|
||||
|
||||
char *program_name = "str";
|
||||
@ -62,7 +62,7 @@ int main(int argc, char *argv[]) {
|
||||
program_name = argv[0] == NULL ? program_name : argv[0];
|
||||
|
||||
#ifdef __OpenBSD__
|
||||
if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL) == -1) {
|
||||
if (pledge("stdio", NULL) == -1) {
|
||||
perror(program_name);
|
||||
return EX_OSERR;
|
||||
}
|
||||
|
@ -20,7 +20,7 @@
|
||||
#include <sysexits.h> /* EX_OK, EX_OSERR, EX_USAGE */
|
||||
|
||||
#ifdef __OpenBSD__
|
||||
# include <unistd.h> /* pledge(2), unveil(2) */
|
||||
# include <unistd.h> /* pledge(2) */
|
||||
#endif
|
||||
|
||||
char *program_name = "strcmp";
|
||||
@ -29,7 +29,7 @@ int main(int argc, char *argv[]) {
|
||||
unsigned int i;
|
||||
|
||||
#ifdef __OpenBSD__
|
||||
if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL) == -1) {
|
||||
if (pledge("stdio", NULL) == -1) {
|
||||
perror(argv[0] == NULL ? program_name : argv[0]);
|
||||
|
||||
return EX_OSERR;
|
||||
|
@ -33,7 +33,7 @@ use sysexits::{ EX_IOERR, EX_OK, EX_OSERR, EX_USAGE };
|
||||
use strerror::StrError;
|
||||
|
||||
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil };
|
||||
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
|
||||
|
||||
fn err(argv0: &str, e: Error, code: u8) -> ExitCode {
|
||||
eprintln!("{}: {}", argv0, e.strerror());
|
||||
@ -49,14 +49,10 @@ fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
|
||||
#[cfg(target_os="openbsd")] {
|
||||
let promises = Promises::new("stdio unveil");
|
||||
let promises = Promises::new("stdio");
|
||||
if let Err(e) = pledge(Some(promises), None) {
|
||||
return err(&argv[0], e, EX_OSERR);
|
||||
}
|
||||
|
||||
if let Err(e) = unveil(None, None) {
|
||||
return err(&argv[0], e, EX_OSERR);
|
||||
}
|
||||
}
|
||||
|
||||
let mut buf: Vec<u8> = Vec::new(); // holds the sequence getting swabbed
|
||||
|
@ -8,12 +8,11 @@
|
||||
*/
|
||||
|
||||
#ifdef __OpenBSD__
|
||||
# include <unistd.h> /* NULL, pledge(2), unveil(2) */
|
||||
# include <unistd.h> /* pledge(2) */
|
||||
#endif
|
||||
|
||||
int main(void) {
|
||||
#ifdef __OpenBSD__
|
||||
(void)pledge(NULL, NULL);
|
||||
(void)unveil(NULL, NULL);
|
||||
pledge(NULL, NULL);
|
||||
#endif
|
||||
}
|
||||
|
@ -24,7 +24,6 @@ fop_fail: $(BIN)/fop
|
||||
! printf 'test\n' | $(BIN)/fop 1 cat
|
||||
! printf 'test\n' | $(BIN)/fop 'test' cat
|
||||
! printf 'test\n' | $(BIN)/fop -d'test' cat
|
||||
! $(BIN)/fop
|
||||
|
||||
.PHONY: fop_functionality
|
||||
fop_functionality: $(BIN)/fop
|
||||
|
Loading…
Reference in New Issue
Block a user