Compare commits

..

10 Commits

Author SHA1 Message Date
731e62ee7e
rpn(1): made evaluation function more readable 2024-08-28 18:24:49 -06:00
f50bfeea92
fop(1): fixes panic on no arguments 2024-08-28 18:05:56 -06:00
8d00fa0afd
fop(1): minor formatting change 2024-08-28 00:46:32 -06:00
9f520df82b
strcmp(1): adds null unveil 2024-08-28 00:31:02 -06:00
41982c2f73
str(1): adds null unveil 2024-08-28 00:30:45 -06:00
6166a3ca36
npc(1): adds null unveil 2024-08-28 00:26:26 -06:00
efedcd3ae4
true(1), false(1): adds null unveils 2024-08-28 00:24:35 -06:00
8fd5bdf5a6
swab(1): adds null unveil 2024-08-28 00:15:15 -06:00
b946469da5
rpn(1): adds null unveil 2024-08-28 00:13:21 -06:00
06384c72fb
intcmp(1): adds null unveil 2024-08-28 00:09:40 -06:00
10 changed files with 75 additions and 46 deletions

View File

@ -8,12 +8,13 @@
*/
#ifdef __OpenBSD__
# include <unistd.h> /* pledge(2) */
# include <unistd.h> /* NULL, pledge(2), unveil(2) */
#endif
int main(void) {
#ifdef __OpenBSD__
pledge(NULL, NULL);
(void)pledge(NULL, NULL);
(void)unveil(NULL, NULL);
#endif
return 1;
}

View File

@ -32,8 +32,7 @@ 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());
@ -62,6 +61,8 @@ 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") => {

View File

@ -26,27 +26,35 @@ extern crate getopt;
extern crate sysexits;
use getopt::GetOpt;
use sysexits::EX_USAGE;
use sysexits::{ EX_DATAERR, 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 };
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil };
#[cfg(target_os="openbsd")] use strerror::StrError;
fn usage(s: &str) -> ExitCode {
eprintln!("Usage: {} [-egl] integer integer...", s);
ExitCode::from(EX_USAGE as u8)
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 main() -> ExitCode {
let argv = args().collect::<Vec<String>>();
#[cfg(target_os="openbsd")] {
let promises = Promises::new("stdio");
let promises = Promises::new("stdio unveil");
if let Err(e) = pledge(Some(promises), None) {
eprintln!("{}: {}", argv[0], e.strerror());
return ExitCode::from(EX_OSERR as u8);
return err(&argv[0], e.strerror(), EX_OSERR);
}
if let Err(e) = unveil(None, None) {
return err(&argv[0], e.strerror(), EX_OSERR);
}
}
@ -78,8 +86,8 @@ fn main() -> ExitCode {
match arg.parse::<usize>() { /* parse current operand */
Ok(n) => currn = n,
Err(e) => {
eprintln!("{}: {}: {}", &argv[0], arg, e);
return ExitCode::from(EX_USAGE as u8);
let error = arg.to_owned() + ": " + &e.to_string();
return err(&argv[0], error, EX_DATAERR);
}
}

View File

@ -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> /* pledge(2), getopt(3) */
#include <unistd.h> /* NULL, getopt(3), pledge(2), unveil(2) */
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", NULL) == -1) {
if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL)) {
perror(argv[0] == NULL ? program_name : argv[0]);
return EX_OSERR;
}

View File

@ -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 };
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil };
#[derive(Clone, PartialEq, PartialOrd, Debug)]
/* enum CalcType is a type containing operations used in the calculator */
@ -138,6 +138,33 @@ 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>,
@ -170,27 +197,9 @@ fn eval(
},
op => {
ops.push_back(op.clone());
oper = true; /* this is an operation */
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,
})
}
return operate(stack, op).map(|s| (s, oper));
},
};
}
@ -225,10 +234,14 @@ fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>();
#[cfg(target_os="openbsd")] {
let promises = Promises::new("stdio");
let promises = Promises::new("stdio unveil");
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();

View File

@ -25,7 +25,7 @@
#include <sysexits.h> /* EX_OSERR, EX_USAGE */
#ifdef __OpenBSD__
# include <unistd.h> /* pledge(2) */
# include <unistd.h> /* pledge(2), unveil(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", NULL) == -1) {
if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL) == -1) {
perror(program_name);
return EX_OSERR;
}

View File

@ -20,7 +20,7 @@
#include <sysexits.h> /* EX_OK, EX_OSERR, EX_USAGE */
#ifdef __OpenBSD__
# include <unistd.h> /* pledge(2) */
# include <unistd.h> /* pledge(2), unveil(2) */
#endif
char *program_name = "strcmp";
@ -29,7 +29,7 @@ int main(int argc, char *argv[]) {
unsigned int i;
#ifdef __OpenBSD__
if (pledge("stdio", NULL) == -1) {
if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL) == -1) {
perror(argv[0] == NULL ? program_name : argv[0]);
return EX_OSERR;

View File

@ -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 };
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil };
fn err(argv0: &str, e: Error, code: u8) -> ExitCode {
eprintln!("{}: {}", argv0, e.strerror());
@ -49,10 +49,14 @@ fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>();
#[cfg(target_os="openbsd")] {
let promises = Promises::new("stdio");
let promises = Promises::new("stdio unveil");
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

View File

@ -8,11 +8,12 @@
*/
#ifdef __OpenBSD__
# include <unistd.h> /* pledge(2) */
# include <unistd.h> /* NULL, pledge(2), unveil(2) */
#endif
int main(void) {
#ifdef __OpenBSD__
pledge(NULL, NULL);
(void)pledge(NULL, NULL);
(void)unveil(NULL, NULL);
#endif
}

View File

@ -24,6 +24,7 @@ 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