1
0
forked from bonsai/harakit

GNUmakefile, fop(1): added sysexits support

This commit is contained in:
Emma Tebibyte 2023-12-27 22:42:50 -07:00
parent 5d9f6f3245
commit 3355cb3dc3
Signed by untrusted user: emma
GPG Key ID: 06FA419A1698C270
2 changed files with 27 additions and 12 deletions

View File

@ -20,8 +20,9 @@ CC=cc
CFLAGS=-O3 -Lbuild/lib -idirafter include
RUSTC=rustc +nightly
RUSTCFLAGS=-Zlocation-detail=none -Copt-level=z -Ccodegen-units=1 -Cpanic=abort
-Clto=y -Cstrip=symbols -Ctarget-cpu=native
RUSTCFLAGS=-Zlocation-detail=none -Copt-level=z -Ccodegen-units=1 \
-Cpanic=abort -Clto=y -Cstrip=symbols -Ctarget-cpu=native \
--extern sysexits=build/o/libsysexits.rlib
ifeq ($(CC), gcc)
CFLAGS=-O3 -s -Wl,-z,noseparate-code,-z,nosectionheader -flto -Lbuild/lib \
@ -39,7 +40,7 @@ endif
build: build_dir false intcmp scrut str strcmp true
build_dir:
mkdir -p build/o build/lib build/bin
mkdir -p build/bin build/lib build/o
clean:
rm -rf build/
@ -56,10 +57,16 @@ test: build
tests/cc-compat.sh
tests/posix-compat.sh
sysexits: build_dir
bindgen --default-macro-constant-type signed \
"$$(printf '#include <sysexits.h>\n' | cpp -M -idirafter include - \
| sed 's/ /\n/g' | grep sysexits.h)" \
| $(RUSTC) $(RUSTCFLAGS) --crate-type lib -o build/o/libsysexits.rlib -
false: src/false.rs build_dir
$(RUSTC) $(RUSTCFLAGS) -o build/bin/false src/false.rs
fop: src/fop.rs build_dir
fop: src/fop.rs build_dir sysexits
$(RUSTC) $(RUSTCFLAGS) -o build/bin/fop src/fop.rs
intcmp: src/intcmp.c build_dir
@ -76,4 +83,3 @@ strcmp: src/strcmp.c build_dir
true: src/true.rs build_dir
$(RUSTC) $(RUSTCFLAGS) -o build/bin/true src/true.rs

View File

@ -18,13 +18,18 @@
use std::{
env::args,
io::{ Read, stdin, Write },
io::{ stdin, Write },
process::{ Command, exit, Stdio },
};
extern crate sysexits;
use sysexits::EX_USAGE;
fn main() {
let argv = args().collect::<Vec<String>>();
let usage = format!("Usage: {} index command [args...]", argv[0]);
let index = match argv.get(1) {
Some(i) => {
i.parse::<usize>().unwrap_or_else(|_| {
@ -33,18 +38,18 @@ fn main() {
})
},
None => {
eprintln!("Usage: {} index command args...", argv[0]);
exit(1);
eprintln!("{}", usage);
exit(EX_USAGE);
},
};
let mut buf = String::new();
stdin().read_to_string(&mut buf).unwrap();
stdin().read_line(&mut buf).unwrap();
let mut fields = buf.split('␞').collect::<Vec<&str>>();
argv.get(2).unwrap_or_else(|| {
eprintln!("Usage: {} index command args...", argv[0]);
exit(1);
eprintln!("{}", usage);
exit(EX_USAGE);
});
let opts = argv.iter().clone().skip(3).collect::<Vec<&String>>();
@ -57,7 +62,11 @@ fn main() {
.unwrap();
let field = fields.get(index).unwrap_or_else(|| {
eprintln!("{}: {}: No such index in input.", argv[0], index.to_string());
eprintln!(
"{}: {}: No such index in input.",
argv[0],
index.to_string()
);
exit(1);
});