1
0
forked from bonsai/harakit

Compare commits

...

16 Commits

38 changed files with 223 additions and 117 deletions

119
Makefile
View File

@ -8,126 +8,37 @@
# permitted in any medium without royalty provided the copyright notice and this
# notice are preserved. This file is offered as-is, without any warranty.
.POSIX:
.PRAGMA: posix_202x # future POSIX standard support à la pdpmake(1)
.PRAGMA: command_comment # breaks without this?
#.POSIX:
PREFIX=/usr/local
CC=cc
RUSTC=rustc
include config.mk
.PHONY: all
all: dj false fop hru intcmp mm npc rpn scrut str strcmp swab true
all: prepare
@$(MAKE) -C lib
@$(MAKE) -C bin
build:
.PHONY: prepare
prepare:
# keep build/include until bindgen(1) has stdin support
# https://github.com/rust-lang/rust-bindgen/issues/2703
mkdir -p build/bin build/include build/lib build/o build/test
.PHONY: clean
clean:
rm -rf build/ dist/
rm -rf build dist
dist: all
mkdir -p dist/bin dist/share/man/man1
cp build/bin/* dist/bin/
cp docs/*.1 dist/share/man/man1/
mkdir -p $(DESTDIR)/$(PREFIX)/bin $(DESTDIR)/$(PREFIX)/share/man/man1
cp build/bin/* $(DESTDIR)/$(PREFIX)/bin
cp bin/*/*.1 $(DESTDIR)/$(PREFIX)/share/man/man1
.PHONY: install
install: dist
mkdir -p $(PREFIX)
cp -r dist/* $(PREFIX)/
cp -r $(DESTDIR)/* /
.PHONY: test
test: build
test: prepare
tests/posix-compat.sh
$(RUSTC) --test src/getopt-rs/lib.rs -o build/test/getopt
build/o/libsysexits.rlib: build
# bandage solution until bindgen(1) gets stdin support
printf '#define EXIT_FAILURE 1\n' | cat - include/sysexits.h \
> build/include/sysexits.h
bindgen --default-macro-constant-type signed --use-core --formatter=none \
"$$(printf '#include <sysexits.h>\n' \
| cpp -M -idirafter "build/include" - \
| sed 's/ /\n/g' | grep sysexits.h)" \
| $(RUSTC) $(RUSTFLAGS) --crate-type lib -o build/o/libsysexits.rlib -
build/o/libgetopt.rlib: src/getopt-rs/lib.rs
$(RUSTC) $(RUSTFLAGS) --crate-type=lib --crate-name=getopt \
-o build/o/libgetopt.rlib src/getopt-rs/lib.rs
.PHONY: dj
dj: build/bin/dj
build/bin/dj: src/dj.c build
$(CC) $(CFLAGS) -o $@ src/dj.c
.PHONY: false
false: build/bin/false
build/bin/false: src/false.c build
$(CC) $(CFLAGS) -o $@ src/false.c
.PHONY: fop
fop: build/bin/fop
build/bin/fop: src/fop.rs build build/o/libgetopt.rlib build/o/libsysexits.rlib
$(RUSTC) $(RUSTFLAGS) --extern getopt=build/o/libgetopt.rlib \
--extern sysexits=build/o/libsysexits.rlib \
-o $@ src/fop.rs
.PHONY: hru
hru: build/bin/hru
build/bin/hru: src/hru.rs build build/o/libgetopt.rlib build/o/libsysexits.rlib
$(RUSTC) $(RUSTFLAGS) --extern getopt=build/o/libgetopt.rlib \
--extern sysexits=build/o/libsysexits.rlib \
-o $@ src/hru.rs
.PHONY: intcmp
intcmp: build/bin/intcmp
build/bin/intcmp: src/intcmp.c build
$(CC) $(CFLAGS) -o $@ src/intcmp.c
.PHONY: mm
mm: build/bin/mm
build/bin/mm: src/mm.c build
$(CC) $(CFLAGS) -o $@ src/mm.c
.PHONY: npc
npc: build/bin/npc
build/bin/npc: src/npc.c build
$(CC) $(CFLAGAS) -o $@ src/npc.c
.PHONY: rpn
rpn: build/bin/rpn
build/bin/rpn: src/rpn.rs build build/o/libsysexits.rlib
$(RUSTC) $(RUSTFLAGS) \
--extern sysexits=build/o/libsysexits.rlib \
-o $@ src/rpn.rs
.PHONY: scrut
scrut: build/bin/scrut
build/bin/scrut: src/scrut.c build
$(CC) $(CFLAGS) -o $@ src/scrut.c
.PHONY: str
str: build/bin/str
build/bin/str: src/str.c build
$(CC) $(CFLAGS) -o $@ src/str.c
.PHONY: strcmp
strcmp: build/bin/strcmp
build/bin/strcmp: src/strcmp.c build
$(CC) $(CFLAGS) -o $@ src/strcmp.c
.PHONY: swab
swab: build/bin/swab
build/bin/swab: src/swab.rs build build/o/libsysexits.rlib
$(RUSTC) $(RUSTFLAGS) --extern getopt=build/o/libgetopt.rlib \
--extern sysexits=build/o/libsysexits.rlib \
-o $@ src/swab.rs
.PHONY: true
true: build/bin/true
build/bin/true: src/true.c build
$(CC) $(CFLAGS) -o $@ src/true.c

91
bin/Makefile Normal file
View File

@ -0,0 +1,91 @@
# Copyright (c) 20232024 Emma Tebibyte <emma@tebibyte.media>
# Copyright (c) 20232024 DTB <trinity@trinity.moe>
# Copyright (c) 2023 Sasha Koshka <sashakoshka@tebibyte.media>
# Copyright (c) 2024 Aaditya Aryal <aryalaadi123@gmail.com>
# SPDX-License-Identifier: FSFAP
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and this
# notice are preserved. This file is offered as-is, without any warranty.
include ../config.mk
.PHONY: bin
bin: dj false fop hru intcmp mm npc rpn scrut str strcmp swab true
.PHONY: lib
# TODO: this does not have to be here
.PHONY: prepare
prepare:
# keep ..$(BUILD)/include until bindgen(1) has stdin support
# https://github.com/rust-lang/rust-bindgen/issues/2703
mkdir -p ../$(BUILD)/bin ../$(BUILD)/include ../$(BUILD)/lib
mkdir -p ../$(BUILD)/test ../$(BUILD)/o
.PHONY: dj
dj: ../$(BUILD)/bin/dj
../$(BUILD)/bin/dj: dj/dj.c prepare
$(CC) $(CFLAGS) -o $@ dj/dj.c
.PHONY: false
false: ../$(BUILD)/bin/false
../$(BUILD)/bin/false: false/false.c prepare
$(CC) $(CFLAGS) -o $@ false/false.c
.PHONY: fop
fop: ../$(BUILD)/bin/fop
../$(BUILD)/bin/fop: fop/fop.rs prepare lib
$(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ fop/fop.rs
.PHONY: hru
hru: ../$(BUILD)/bin/hru
../$(BUILD)/bin/hru: hru/hru.rs prepare lib
$(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ hru/hru.rs
.PHONY: intcmp
intcmp: ../$(BUILD)/bin/intcmp
../$(BUILD)/bin/intcmp: intcmp/intcmp.c prepare
$(CC) $(CFLAGS) -o $@ intcmp/intcmp.c
.PHONY: mm
mm: ../$(BUILD)/bin/mm
../$(BUILD)/bin/mm: mm/mm.c prepare
$(CC) $(CFLAGS) -o $@ mm/mm.c
.PHONY: npc
npc: ../$(BUILD)/bin/npc
../$(BUILD)/bin/npc: npc/npc.c prepare
$(CC) $(CFLAGAS) -o $@ npc/npc.c
.PHONY: rpn
rpn: ../$(BUILD)/bin/rpn
../$(BUILD)/bin/rpn: rpn/rpn.rs prepare lib
$(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ rpn/rpn.rs
.PHONY: scrut
scrut: ../$(BUILD)/bin/scrut
../$(BUILD)/bin/scrut: scrut/scrut.c prepare
$(CC) $(CFLAGS) -o $@ scrut/scrut.c
.PHONY: str
str: ../$(BUILD)/bin/str
../$(BUILD)/bin/str: str/str.c prepare
$(CC) $(CFLAGS) -o $@ str/str.c
.PHONY: strcmp
strcmp: ../$(BUILD)/bin/strcmp
../$(BUILD)/bin/strcmp: strcmp/strcmp.c prepare
$(CC) $(CFLAGS) -o $@ strcmp/strcmp.c
.PHONY: swab
swab: ../$(BUILD)/bin/swab
../$(BUILD)/bin/swab: swab/swab.rs prepare lib
$(RUSTC) $(RUSTFLAGS) --extern getopt=../$(BUILD)/o/libgetopt.rlib \
--extern sysexits=../$(BUILD)/o/libsysexits.rlib \
-o $@ swab/swab.rs
.PHONY: true
true: ../$(BUILD)/bin/true
../$(BUILD)/bin/true: true/true.c prepare
$(CC) $(CFLAGS) -o $@ true/true.c

View File

@ -22,10 +22,12 @@ use std::{
process::{ Command, exit, Stdio },
};
extern crate sysexits;
extern crate getopt;
extern crate strerror;
extern crate sysexits;
use getopt::{ Opt, Parser };
use strerror::StrError;
use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
fn main() {
@ -55,7 +57,7 @@ fn main() {
});
let index = argv[index_arg].parse::<usize>().unwrap_or_else(|e| {
eprintln!("{}: {}: {}.", argv[0], argv[1], e);
eprintln!("{}: {}: {}", argv[0], argv[1], e);
exit(EX_DATAERR);
});
@ -75,13 +77,13 @@ fn main() {
.stdout(Stdio::piped())
.spawn()
.unwrap_or_else( |e| {
eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e);
eprintln!("{}: {}: {}", argv[0], argv[command_arg], e.strerror());
exit(EX_UNAVAILABLE);
});
let field = fields.get(index).unwrap_or_else(|| {
eprintln!(
"{}: {}: No such index in input.",
"{}: {}: No such index in input",
argv[0],
index.to_string(),
);
@ -94,7 +96,7 @@ fn main() {
}
let output = spawned.wait_with_output().unwrap_or_else(|e| {
eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e);
eprintln!("{}: {}: {}", argv[0], argv[command_arg], e.strerror());
exit(EX_IOERR);
});
@ -103,7 +105,7 @@ fn main() {
if replace.pop() != Some(b'\n') { replace = output.stdout; }
let new_field = String::from_utf8(replace).unwrap_or_else(|e| {
eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e);
eprintln!("{}: {}: {}", argv[0], argv[command_arg], e);
exit(EX_IOERR);
});
@ -111,8 +113,8 @@ fn main() {
stdout().write_all(
fields.join(&d.to_string()).as_bytes()
).unwrap_or_else(|e|{
eprintln!("{}: {}.", argv[0], e);
).unwrap_or_else(|e| {
eprintln!("{}: {}", argv[0], e.strerror());
exit(EX_IOERR);
});
}

View File

@ -23,8 +23,10 @@ use std::{
process::{ ExitCode, exit },
};
extern crate strerror;
extern crate sysexits;
use strerror::StrError;
use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE };
const LIST: [(u32, &str); 10] = [
@ -49,7 +51,7 @@ fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> {
let c = match 10_u128.checked_pow(n) {
Some(c) => c,
None => {
return Err(format!("10^{}: Integer overflow.", n.to_string()));
return Err(format!("10^{}: Integer overflow", n.to_string()));
},
};
@ -79,7 +81,7 @@ fn main() -> ExitCode {
f
},
Err(err) => {
eprintln!("{}: {}.", argv[0], err);
eprintln!("{}: {}", argv[0], err);
return ExitCode::from(EX_DATAERR as u8);
},
};
@ -87,7 +89,7 @@ fn main() -> ExitCode {
let (number, prefix) = match convert(n) {
Ok(x) => x,
Err(err) => {
eprintln!("{}: {}.", argv[0], err);
eprintln!("{}: {}", argv[0], err);
return ExitCode::from(EX_SOFTWARE as u8);
},
};
@ -98,7 +100,7 @@ fn main() -> ExitCode {
stdout().write_all(format!("{} {}\n", out, si_prefix).as_bytes())
.unwrap_or_else(|e| {
eprintln!("{}: {}.", argv[0], e);
eprintln!("{}: {}", argv[0], e.strerror());
exit(EX_IOERR);
});
}

View File

@ -172,7 +172,7 @@ fn eval(
};
} else {
return Err(EvaluationError {
message: format!("{}: Unexpected operation.", op),
message: format!("{}: Unexpected operation", op),
code: EX_DATAERR,
})
}

10
bin/test/test.rs Normal file
View File

@ -0,0 +1,10 @@
extern crate strerror;
use strerror::raw_message;
fn main() {
stdout.write_all(b"meow\n").unwrap_or_else(|e| {
eprintln!("{}", raw_message(e));
std::process::exit(1);
});
}

29
config.mk Normal file
View File

@ -0,0 +1,29 @@
# Copyright (c) 20232024 Emma Tebibyte <emma@tebibyte.media>
# Copyright (c) 20232024 DTB <trinity@trinity.moe>
# Copyright (c) 2023 Sasha Koshka <sashakoshka@tebibyte.media>
# Copyright (c) 2024 Aaditya Aryal <aryalaadi123@gmail.com>
# SPDX-License-Identifier: FSFAP
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and this
# notice are preserved. This file is offered as-is, without any warranty.
# if using BSD make(1), remove these pragmas because they break it
.PRAGMA: posix_202x # future POSIX standard support à la pdpmake(1)
.PRAGMA: command_comment # breaks without this?
DESTDIR ?= dist
PREFIX ?= /usr/local
BUILD ?= build
SYSEXITS != printf '\043include <sysexits.h>\n' | cpp -M - | sed 's/ /\n/g' \
| sed -n 's/sysexits\.h//p' || printf 'include\n'
CC ?= cc
RUSTC ?= rustc
RUSTLIBS = --extern getopt=../$(BUILD)/o/libgetopt.rlib \
--extern sysexits=../$(BUILD)/o/libsysexits.rlib \
--extern strerror=../$(BUILD)/o/libstrerror.rlib
CFLAGs += --std=c99 -Wall -pedantic -Werror
CFLAGS += -I$(SYSEXITS)

30
lib/Makefile Normal file
View File

@ -0,0 +1,30 @@
# Copyright (c) 20232024 Emma Tebibyte <emma@tebibyte.media>
# Copyright (c) 20232024 DTB <trinity@trinity.moe>
# Copyright (c) 2023 Sasha Koshka <sashakoshka@tebibyte.media>
# Copyright (c) 2024 Aaditya Aryal <aryalaadi123@gmail.com>
# SPDX-License-Identifier: FSFAP
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and this
# notice are preserved. This file is offered as-is, without any warranty.
include ../config.mk
.PHONY: lib
lib: ../$(BUILD)/o/libsysexits.rlib ../$(BUILD)/o/libgetopt.rlib \
../$(BUILD)/o/libstrerror.rlib
../$(BUILD)/o/libgetopt.rlib: getopt-rs/lib.rs
$(RUSTC) $(RUSTFLAGS) --crate-type=lib --crate-name=getopt \
-o $@ getopt-rs/lib.rs
../$(BUILD)/o/libstrerror.rlib: strerror.rs
$(RUSTC) $(RUSTFLAGS) --crate-type=lib -o $@ \
strerror.rs
../$(BUILD)/o/libsysexits.rlib: $(SYSEXITS)sysexits.h
# bandage solution until bindgen(1) gets stdin support
printf '#define EXIT_FAILURE 1\n' | cat - $(SYSEXITS)sysexits.h \
> ../$(BUILD)/include/sysexits.h
bindgen --default-macro-constant-type signed --use-core --formatter=none \
../$(BUILD)/include/sysexits.h | $(RUSTC) $(RUSTFLAGS) --crate-type lib -o $@ -

31
lib/strerror.rs Normal file
View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: FSFAP
*
* Copying and distribution of this file, with or without modification, are
* permitted in any medium without royalty provided the copyright notice and
* this notice are preserved. This file is offered as-is, without any warranty.
*/
use std::ffi::{ c_int, c_char, CStr };
pub trait StrError { fn strerror(&self) -> String; }
impl StrError for std::io::Error {
/* wrapper function for use in Rust */
fn strerror(&self) -> String {
/* Get the raw OS error. If its None, what the hell is going on‽ */
let errno = self.raw_os_error().unwrap_or(0) as c_int;
/* Get a CStr from the error message so that its referenced and then
* convert it to an owned value. If the string is not valid UTF-8,
* return that error instead. */
match unsafe { CStr::from_ptr(strerror(errno)) }.to_str() {
Ok(s) => s.to_owned(), // yay!! :D
Err(e) => e.to_string(), // awww :(
}
}
}
/* binding to strerror(3p) */
extern "C" { fn strerror(errnum: c_int) -> *mut c_char; }